DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_PVT

Source


1 PACKAGE BODY MSC_ATP_PVT AS
2 /* $Header: MSCGATPB.pls 120.39.12010000.2 2008/08/13 11:25:08 sbnaik 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 Procedure Schedule (p_atp_table     IN    MRP_ATP_PUB.ATP_Rec_Typ,
56                p_instance_id        IN    NUMBER,
57                p_assign_set_id      IN    NUMBER,
58                p_refresh_number     IN    NUMBER,
59                x_atp_table          OUT   NoCopy MRP_ATP_PUB.ATP_Rec_Typ,
60                x_return_status      OUT   NoCopy VARCHAR2,
61                x_msg_data           OUT   NoCopy VARCHAR2,
62                x_msg_count          OUT   NoCopy NUMBER,
63                x_atp_supply_demand  OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
64                x_atp_period         OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
65                x_atp_details        OUT   NOCOPY MRP_ATP_PUB.ATP_Details_Typ
66 ) IS
67 
68 i                       PLS_INTEGER := 1;
69 j			PLS_INTEGER := 1;
70 k			PLS_INTEGER := 1;
71 m			PLS_INTEGER := 1;
72 l_sched_flag		PLS_INTEGER := 1;
73 l_search		PLS_INTEGER := 1;
74 l_temp                  NUMBER;
75 l_plan_id               NUMBER;
76 l_temp_plan_id          NUMBER; -- this is for ship set items
77 --Bug 1661545, array to store old plan id for undoing unscheduling in case of failure.
78 l_old_plan_id           MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr(); -- this is for ship set items
79 l_process_item_id	NUMBER;
80 l_process_org_id	NUMBER;
81 l_ship_count            NUMBER;
82 l_arrival_count         NUMBER;
83 l_level			PLS_INTEGER := 0;
84 l_scenario_id		PLS_INTEGER := 1;
85 l_group_date		DATE ;
86 l_schedule_flag		NUMBER;
87 l_requested_item_id	NUMBER;
88 l_total_qty             NUMBER;
89 l_return_status		VARCHAR2(10);
90 l_done_scenario2        VARCHAR2(1);
91 l_quantity_uom		VARCHAR2(3);
92 l_quantity_ordered	NUMBER;
93 l_overall_status	PLS_INTEGER;
94 l_ship_set_status       PLS_INTEGER;
95 l_arrival_set_status    PLS_INTEGER;
96 l_process_arrival_set   VARCHAR2(30) := '@@@';
97 l_sources               MRP_ATP_PVT.Atp_Source_Typ;
98 l_atp_rec		MRP_ATP_PVT.AtpRec;
99 l_future_atp_rec	MRP_ATP_PVT.AtpRec;
100 l_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
101 l_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
102 l_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
103 l_future_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
104 l_future_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
105 l_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
106 l_temp_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
107 l_temp_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
108 l_temp_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
109 l_temp_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
110 l_temp_arr_atp_table             MRP_ATP_PUB.ATP_Rec_Typ;
111 l_temp_arr_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
112 l_temp_arr_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
113 l_temp_arr_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
114 l_curr_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
115 l_curr_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
116 l_complete_scatp	VARCHAR2(1) := 'N';
117 l_multi_scenario	NUMBER;
118 l_null_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
119 l_null_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
120 l_null_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
121 l_set_element_success  PLS_INTEGER;
122 l_oe_demand_flag          VARCHAR2(1) := 'N';
123 my_sqlcode		NUMBER;
124 temp			NUMBER;
125 l_stmt                  PLS_INTEGER;
126 l_assign_set_id		NUMBER;
127 l_temp_assign_set_id         NUMBER;
128 l_conversion_rate       NUMBER;
129 l_atp_flag                      VARCHAR2(1);
130 l_atp_comp_flag                 VARCHAR2(1);
131 l_so_tbl_status         PLS_INTEGER;
132 l_arr_no_date           VARCHAR2(1) := 'Y';
133 l_default_atp_rule_id           NUMBER;
134 l_calendar_code                 VARCHAR2(14);
135 l_calendar_exception_set_id     NUMBER;
136 l_default_demand_class          VARCHAR2(34);
137 l_sd_qty                        NUMBER;
138 l_inv_item_id 			NUMBER;
139 --l_ship_date                     DATE; -- ship_rec_cal changes
140 l_fst_src_pegging_ids	        MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
141 l_date_higher_src		varchar2(1) := 'Y';
142 l_peg_count			NUMBER;
143 l_dblink                        VARCHAR2(128);
144 l_sys_next_date                 DATE;
145 l_sys_next_omc_date             DATE;   -- Bug 3371817
146 l_request_org_id                number;
147 l_sourcing_org_id               number;
148 l_count 			number;
149 l_line_id_count                 NUMBER;  -- keeps track of line identifiers.
150 l_demand_pegging_id 		number;
151 L_DEMAND_CLASS_FLAG             number;
152 
153 -- 1873918: PDS-ODS fix
154 l_inv_ctp                       number;
155 l_allocated_atp                 varchar2(1);
156 l_allocation_method             number;
157 l_summary_flag                  number;
158 -- Bug 2085071: Assignment Set
159 lv_assign_set_id                NUMBER;
160 --2152184
161 l_pf_item_id			MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
162 --l_old_org_id                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
163 
164 ---subst
165 l_item_substitute_rec           MSC_ATP_SUBST.Item_Info_Rec_Typ;
166 l_null_item_sub_rec             MSC_ATP_SUBST.Item_Info_Rec_Typ;
167 l_item_attribute_rec            MSC_ATP_PVT.item_attribute_rec;
168 l_pre_pro_lt                    number;
169 l_post_pro_lt                   number;
170 l_fixed_lt                      number;
171 l_variable_lt                   number;
172 L_INVENTORY_ITEM_ID             number;
173 L_ITEM_NAME                     varchar2(250);
174 l_create_supply_on_orig_item    number;
175 l_substitution_type             number;
176 L_DELIVERY_LEAD_TIME            number;
177 L_REQUESTED_SHIP_DATE           date;
178 L_FROM_LOCATION_ID              number;
179 L_SHIP_METHOD                   varchar2(50);
180 l_demand_ids                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
181 l_old_pf_item_id                number;
182 l_inv_item_ids                  MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
183 
184 l_org_code                      VARCHAR2(7);
185 l_counter                       NUMBER;  -- defined for Bug 2368426
186 
187 -- Error handling insert
188 l_tmp_pegging_id                NUMBER;
189 /* Modularize Item and Org Info */
190 -- Local item record already provided
191 l_org_attribute_rec             MSC_ATP_PVT.org_attribute_rec;
192 -- Used for bug 2392456
193 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
194 l_bkwd_pass_atf_date_qty         NUMBER; --bug3397904
195 
196 --bug 2384224
197 l_del_demand_ids             MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
198 l_del_inv_item_ids           MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
199 l_del_plan_ids               MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
200 l_del_identifiers            MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
201 l_del_demand_source_type     MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();--cmro
202 l_delete_demand_flag         PLS_INTEGER;
203 
204 --diag_atp
205 l_pegging_ids           MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
206 l_highest_rev		NUMBER;
207 
208 -- ATP Override rajjain begin
209 l_latest_acceptable_date	DATE;
210 l_override_flag			VARCHAR2(1) := 'N';
211 -- ATP Override rajjain end
212 
213 l_null_item_info_rec          item_attribute_rec;
214 l_item_info_rec               item_attribute_rec;
215 l_null_org_info_rec           org_attribute_rec;
216 l_null_plan_info_rec          plan_info_rec;
217 
218 -- rajjain 01/30/2003 Bug 2777661
219 l_sd_date                     DATE;
220 
221 -- dsting setproc
222 l_line_status                 NUMBER;
223 l_set_status                  NUMBER;
224 
225 -- bug 2795053-reopen (ssurendr)
226 l_dmd_satisfied_date          DATE;
227 
228 -- 2902265
229 l_req_atp_date                  mrp_atp_pub.date_arr := mrp_atp_pub.date_arr();
230 l_req_date_qty                  mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
231 l_demand_class_atp_flag         mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
232 
233 --s_cto_rearch
234 l_item_sourcing_info_rec       MSC_ATP_CTO.item_sourcing_info_rec;
235 
236 l_null_item_sourcing_info_rec  MSC_ATP_CTO.item_sourcing_info_rec;
237 l_plan_found_for_match         number;
238 --e_cto_rearch
239 
240 -- time_phased_atp
241 l_pf_atp                VARCHAR2(1) := 'N';
242 l_time_phased_atp       VARCHAR2(1) := 'N';
243 l_atf_date              DATE;
244 l_time_phased_set       VARCHAR2(1) := 'N';
245 l_atf_dates             MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
246 l_match_item_family_id  NUMBER;
247 
248 -- For summary enhancement
249 l_copy_demand_ids               mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
250 l_del_copy_demand_ids           mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
251 l_del_copy_demand_plan_ids      mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
252 
253 
254 -- CTO ODR and Simplified Pegging
255 l_atp_peg_items                   MRP_ATP_PUB.Number_Arr ;
256 l_atp_peg_demands                 MRP_ATP_PUB.Number_Arr ;
257 l_atp_peg_supplies                MRP_ATP_PUB.Number_Arr ;
258 l_atp_peg_res_reqs                MRP_ATP_PUB.Number_Arr ;
259 l_demand_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
260 l_supply_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
261 l_res_instance_id                 MRP_ATP_PUB.Number_Arr ; --Bug 3629191
262 l_del_atp_peg_items               MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
263 l_del_atp_peg_demands             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
264 l_del_atp_peg_supplies            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
265 l_del_atp_peg_res_reqs            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
266 l_atp_peg_demands_plan_ids        MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
267 l_atp_peg_supplies_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
268 l_atp_peg_res_reqs_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
269 l_off_demand_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
270 l_off_supply_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
271 l_off_res_instance_id             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
272 -- END CTO ODR and Simplified Pegging
273 --plan by request date enhancment
274 l_original_req_arrival_date    DATE;
275 l_req_arr_date                 mrp_atp_pub.date_arr := mrp_atp_pub.date_arr();
276 -- ship_rec_cal changes
277 l_shipping_cal_code		VARCHAR2(14);
278 l_receiving_cal_code		VARCHAR2(14);
279 l_manufacturing_cal_code	VARCHAR2(14);
280 l_intransit_cal_code		VARCHAR2(14);
281 l_new_ship_date                 DATE;
282 l_new_dock_date                 DATE;
283 l_group_arrival_date            DATE;
284 l_order_date_type               NUMBER;
285 l_ship_arrival_date_rec 	MSC_ATP_PVT.ship_arrival_date_rec_typ;
286 l_encoded_text                  varchar2(4000);
287 l_msg_app                       varchar2(50);
288 l_msg_name                      varchar2(30);
289 l_error_code                    INTEGER;
290 
291 -- Bug 3226083
292 l_atp_insert_rec                MRP_ATP_PVT.AtpRec;
293 l_mem_dest_id                   NUMBER;
294 l_pf_dest_id                    NUMBER;
295 
296 l_sysdate_orc                   DATE; --bug3439591
297 l_trunc_sysdate                 DATE := TRUNC(sysdate); --bug3439591
298 
299 --bug 3751114
300 l_peg_record_type               NUMBER := 3;
301 l_constraint_path_flag          NUMBER := 1;
302 l_delete_atp_rec                MSC_ATP_PVT.DELETE_ATP_REC; --3720018
303 
304 --2814895
305 l_partner_type                  NUMBER := 2;
306 
307 l_sysdate_orc_new               DATE; --bug4291375
308 l_past_due_ship_date            NUMBER; --bug4291375
309 L_MOVE_PAST_DUE_TO_SYSDATE 			varchar2(1);  -- Bug 5584634/5618929
310 
311 BEGIN
312 
313     IF PG_DEBUG in ('Y', 'C') THEN
314        msc_sch_wb.atp_debug('**********IN Schedule Procedure************');
315     END IF;
316     --initialize ATP_SET_FLAG TO NO --4460369
317     MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET := 'N';
318     -- initialize API return status to success
319     x_return_status := FND_API.G_RET_STS_SUCCESS;
320 
321     MSC_ATP_PVT.G_RES_CONSUME := NVL(FND_PROFILE.value('MSC_ATP_RES_CONSUME'), 'Y');
322     IF PG_DEBUG in ('Y', 'C') THEN
323        msc_sch_wb.atp_debug('Schedule: ' || 'G_RES_CONSUME = '||MSC_ATP_PVT.G_RES_CONSUME);
324     END IF;
325 
326     MSC_ATP_PVT.G_PARTNER_ID := NULL;
327     MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
328 
329     MSC_ATP_PVT.G_INSTANCE_ID := p_instance_id;
330 
331     IF MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT <> 0 THEN
332         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
333         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
334         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
335         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
336          IF PG_DEBUG in ('Y', 'C') THEN
337             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 15');
338          END IF;
339 
340     END IF;
341 
342     -- 2902265
343     IF l_fst_src_pegging_ids.COUNT <> 0 THEN
344         l_fst_src_pegging_ids.delete;
345         l_DEMAND_CLASS_ATP_FLAG.DELETE;
346         l_REQ_ATP_DATE.DELETE;
347         l_REQ_DATE_QTY.DELETE;
348         IF PG_DEBUG in ('Y', 'C') THEN
349             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 15.0');
350         END IF;
351     END IF;
352 
353     --- BUG: 1777496
354     --- WE dont want to do Allocated ATP in case of ODS
355     --- Allocattion flag is set to 'NO' if it is ODS
356     IF  MSC_ATP_PVT.G_INV_CTP = 5 THEN
357 	MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
358 	MSC_ATP_PVT.G_ORIG_ALLOC_ATP := 'N'; --4925767
359     END IF;
360     -- we either have a line here or a set (arrival set or ship set)
361     -- this l_ship_count store the # of line for the ship set.
362 
363     -- if l_ship_count > 1 : ship set exists
364     -- if l_arrival_count > 1: arrival set exists
365 
366     l_stmt := 10;
367 
368     -- default the plan_id to -1 and adjust it later.
369 
370     l_plan_id := -1;
371 
372     IF PG_DEBUG in ('Y', 'C') THEN
373        msc_sch_wb.atp_debug('Schedule: ' || 'count :='|| p_atp_table.attribute_02.count);
374     END IF;
375     ---diag_atp: Enable diagnostic ATP only if calling module tells us to do so and for ATP inquiry only
376     ---Also Diag ATP is supported for PDS.
377     IF ((p_atp_table.Action(1) = ATPQUERY) AND (NVL(p_atp_table.attribute_02(1), 2) = 1)
378                                            AND (MSC_ATP_PVT.G_INV_CTP = 4)) THEN
379 
380        MSC_ATP_PVT.G_DIAGNOSTIC_ATP := 1;
381 
382     ELSE
383 
384        MSC_ATP_PVT.G_DIAGNOSTIC_ATP := 2;
385 
386     END IF;
387     IF PG_DEBUG in ('Y', 'C') THEN
388        msc_sch_wb.atp_debug('Schedule: ' || 'G_DIAGNOSTIC_ATP := ' || MSC_ATP_PVT.G_DIAGNOSTIC_ATP);
389     END IF;
390 
391     /************ Bug 2085071 Assignment set changes ************/
392     IF NVL(p_atp_table.Calling_Module(1), -1) = 724 THEN
393           IF PG_DEBUG in ('Y', 'C') THEN
394              msc_sch_wb.atp_debug('Schedule: ' || '724');
395           END IF;
396           lv_assign_set_id := p_assign_set_id;     -- Use new local variable
397     ELSE
398       -- convert the assignment set id from mrp to msc
399       IF PG_DEBUG in ('Y', 'C') THEN
400          msc_sch_wb.atp_debug('Schedule: ' || ' not 724');
401       END IF;
402 
403       -- Need to conver assignment set only if it is NOT NULL. If NULL, do nothing.
404       IF p_assign_set_id IS NOT NULL THEN
405 
406          IF PG_DEBUG in ('Y', 'C') THEN
407             msc_sch_wb.atp_debug('Schedule: p_atp_table.calling_module: ' || p_atp_table.calling_module(1));
408             msc_sch_wb.atp_debug('Schedule: p_atp_table.action: ' || p_atp_table.action(1));
409          END IF;
410 
411          --4567833 , in case of ATP inquiry from source with Org_id as NULL,
412          -- ATP UI is passing assignment set id of destination so no need to convert it.
413          IF (p_atp_table.calling_module(1) is NULL AND p_atp_table.action(1) = 100) THEN
414 
415             lv_assign_set_id :=  p_assign_set_id;
416 
417             IF PG_DEBUG in ('Y', 'C') THEN
418                msc_sch_wb.atp_debug('Schedule: ' || 'source side ATP inquiry with Org_id as NULL');
419             END IF;
420 
421          ELSE   --4567833 end
422             begin
423               select assignment_set_id
424               into   lv_assign_set_id                  -- Use new local variable
425               from   msc_assignment_sets
426               where  sr_instance_id = p_instance_id
427               and    sr_assignment_set_id = p_assign_set_id;
428 
429             IF PG_DEBUG in ('Y', 'C') THEN
430                msc_sch_wb.atp_debug('Schedule: ' || 'source side ATP inquiry with Org_id as not NULL');
431             END IF;
432 
433             exception
434                when no_data_found then
435              -- Bug 1763731, need not do this check for the time being
436         /*
437              x_atp_table := p_atp_table;
438              x_atp_table.error_code(1) := NO_ASSIGNMENT_SET;
439              RAISE FND_API.G_EXC_ERROR ;
440         */
441                lv_assign_set_id := NULL;             -- Use new local variable
442             end;
443 
444          END IF;
445 
446       ELSE  --IF p_assign_set_id IS NOT NULL THEN
447         -- savirine added the following code so that ATP uses
448         -- "MSC: ATP Assignment Set" value when the
449         -- "MRP: ATP Assignment Set" value is NULL for
450         -- the ATP Requests originating from the Source ERP Instance.
451 	-- This is done to support region level sourcing as
452         -- only assignment set on APS can be used.
453 
454         -- lv_assign_set_id := NULL;              -- User new local variable
455         lv_assign_set_id := FND_PROFILE.value('MSC_ATP_ASSIGN_SET');
456       END IF;
457 
458       IF PG_DEBUG in ('Y', 'C') THEN
459          msc_sch_wb.atp_debug('Schedule: ' || 'after converting the assign set id');
460       END IF;
461     END IF;
462     l_assign_set_id  := lv_assign_set_id; -- Set the original local variable
463     /************ Bug 2085071 Assignment set changes ************/
464 
465     IF PG_DEBUG in ('Y', 'C') THEN
466        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
467        msc_sch_wb.atp_debug('Schedule: ' || 'l_plan_id = '||l_plan_id);
468        msc_sch_wb.atp_debug('Schedule: ' || 'l_assign_set_id = '||l_assign_set_id);
469     END IF;
470 
471     IF p_atp_table.Ship_Set_Name(1) IS NOT NULL THEN
472       l_ship_count := p_atp_table.Action.COUNT;
473       l_arrival_count := 1;
474     ELSE
475       l_ship_count := 1;
476       l_arrival_count := p_atp_table.Action.COUNT;
477     END IF;
478 
479     -- since we may have a set of output (record of tables) for one single
480     -- line for complete scatp, we won't work on x_atp_table directly.
481     -- instead, we use l_atp_table to store this information and added this
482     -- to x_atp_table later
483 
484     /* Modularize Item and Org Info */
485     -- Initialize global variables.
486     G_ORG_INFO_REC        :=      l_org_attribute_rec;
487     G_ITEM_INFO_REC       :=      l_item_attribute_rec;
488     /* Modularize Item and Org Info */
489 
490     l_stmt := 20;
491     IF PG_DEBUG in ('Y', 'C') THEN
492        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
493 
494        -- make sure we get the right record of tables
495        i := p_atp_table.Action.FIRST;
496        WHILE i IS NOT NULL LOOP
497           msc_sch_wb.atp_debug('Schedule: ' || 'Inventory_Item_Id:' || to_char(p_atp_table.Inventory_Item_Id(i))
498 	  || 'Source_Organization_Id:' || to_char(p_atp_table.Source_Organization_Id(i))
499           || 'Quantity_Ordered:' || to_char(p_atp_table.Quantity_Ordered(i))
500           || ' old_source organzation id : ' || p_atp_table.old_Source_Organization_Id(i));
501           i := p_atp_table.Action.NEXT(i);
502        END LOOP;
503     END IF;
504 
505     -- if the action is demandmodify, make sure we delete the original
506     -- demand information before we redemand.
507     -- so we won't double count the demand
508     -- we won't do any validation here due to ato impacts.
509 
510     l_stmt := 30;
511     IF PG_DEBUG in ('Y', 'C') THEN
512        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
513        msc_sch_wb.atp_debug('Schedule: ' || 'G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
514        msc_sch_wb.atp_debug('Schedule: ' || 'G_DATABASE_LINK := ' || MSC_ATP_PVT.G_DATABASE_LINK);
515        msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
516        msc_sch_wb.atp_debug('Schedule: ' || 'G_HP_DEMAND_BUCKETING_PREF := ' ||
517                                                         MSC_ATP_PVT.G_HP_DEMAND_BUCKETING_PREF);
518     END IF;
519     IF ((MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND (MSC_ATP_PVT.G_DATABASE_LINK IS NULL) AND
520                                              (MSC_ATP_PVT.G_ALLOCATED_ATP = 'N')) THEN
521         --IF coming from planning server then we want to disable Summary mode for
522         -- instance which is distributed. If it is non distributed then G_DATABASE_LINK will
523         -- be null at this point as it is populated in CALL_SCHEDULE_REMOTE
524        --Bug3765793 adding trim functions to remove spaces from db_link
525        SELECT ltrim(rtrim(M2A_DBLINK)), summary_flag
526        INTO   MSC_ATP_PVT.G_DATABASE_LINK, l_summary_flag
527        FROM   msc_apps_instances
528        WHERE  instance_id = p_instance_id;
529 
530        ---bug 2301524:
531        --- 2301524: Summary mode is not supported for backlog workbench.
532        IF PG_DEBUG in ('Y', 'C') THEN
533           msc_sch_wb.atp_debug('Schedule: ' || 'calling_module := ' ||  p_atp_table.calling_module(1));
534           msc_sch_wb.atp_debug('Schedule: ' || ' l_summary_flag := ' || l_summary_flag);
535           msc_sch_wb.atp_debug('Schedule: ' || ' G_DATABASE_LINK := ' || NVL(MSC_ATP_PVT.G_DATABASE_LINK,'NULL'));
536        END IF;
537 
538        -- After summary enhancement summary ATP is supported for BWB as well - except for ODS case
539        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
540              --- if backlog workbench is used then set the summary falg to 200
541              IF PG_DEBUG in ('Y', 'C') THEN
542                 msc_sch_wb.atp_debug('Schedule: ' || 'Site is using backlog workbench, update tables and turnoff summary for ODS case');
543              END IF;
544 
545              update  msc_apps_instances
546              set     summary_flag = 200;
547 
548              MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
549        ELSIF NVL(l_summary_flag, -1) = 200 AND MSC_ATP_PVT.G_INV_CTP = 5 THEN
550              --site is using backlog workbench, turn off the summary flag - only for ODS case - summary enhancement
551              IF PG_DEBUG in ('Y', 'C') THEN
552                 msc_sch_wb.atp_debug('Schedule: ' || 'Site is using backlog workbench, turnoff summary');
553              END IF;
554              MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
555        END IF;
556 
557     END IF;
558 
559     ----  After summary enhancement summary will be supported for distributed cases as well - except for ODS case
560     IF MSC_ATP_PVT.G_DATABASE_LINK IS NOT NULL AND MSC_ATP_PVT.G_INV_CTP = 5 THEN
561          MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
562          IF PG_DEBUG in ('Y', 'C') THEN
563             msc_sch_wb.atp_debug('Schedule: ' || 'Distributed scenario + ODS, turning summary flag off');
564          END IF;
565     END IF;
566     -- specify l_atp_table here so that for undemand we have a
567     -- place to store the error code.
568     -- this will be reset when we actually go into atp/demand
569 
570     l_atp_table := p_atp_table;
571 
572     /* bug3940999 Code moved to call_schedule_remote.
573     -- Code added for bug 2368426 to match source destination INV_CTP start
574 
575     IF MSC_ATP_PVT.G_INV_CTP_SOURCE <> -1 THEN
576 
577       IF MSC_ATP_PVT.G_INV_CTP_SOURCE <> MSC_ATP_PVT.G_INV_CTP THEN
578 
579         IF PG_DEBUG in ('Y', 'C') THEN
580            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP ' || MSC_ATP_PVT.G_INV_CTP_SOURCE);
581            msc_sch_wb.atp_debug('Schedule: ' || 'Destination INV_CTP ' || MSC_ATP_PVT.G_INV_CTP);
582            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP not in sync with destination ');
583         END IF;
584         -- apply the error code in l_atp_table
585 
586         FOR l_counter IN 1..l_atp_table.error_code.COUNT LOOP
587           IF l_counter=1 THEN
588             l_atp_table.error_code(l_counter) := MSC_ATP_PVT.INV_CTP_NOT_IN_SYNC;
589           ELSE
590             l_atp_table.error_code(l_counter) := MSC_ATP_PVT.GROUPEL_ERROR;
591           END IF;
592         END LOOP;
593         RAISE NO_DATA_FOUND;
594 
595       ELSE
596         IF PG_DEBUG in ('Y', 'C') THEN
597            msc_sch_wb.atp_debug('Schedule: ' || 'Source INV_CTP is in sync with destination ');
598         END IF;
599       END IF;
600 
601     END IF;
602 
603     -- Code added for bug 2368426 to match source destination INV_CTP ends
604     */
605 
606 
607     ---subst demand
608     --- after substitution we might have multiple demands for same order line.
609     --- Therefore we remember the demand id and inv id for the demand that we are manupulating
610     l_demand_ids.Extend(p_atp_table.action.count);
611     l_inv_item_ids.extend(p_atp_table.action.count);
612      l_req_arr_date.extend(p_atp_table.action.count);--for plan by request date
613     -- bug 2152184
614     IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
615        -- we store the pf id and old org id. l_pf_item_id will contain either PF item id if we are doing
616        --- product family for the item or the inventory_item_id itself for regular item
617        ---we store these ids because of change in PF atp approach to do PF ATP on PF item level
618        -- Under new approach, for PF ATP, demand is stored on PF item level instead on member item level
619        -- Therefore to delete the demand, we need to find out the plan for PF
620 
621        --l_old_org_id.extend(p_atp_table.action.count);
622        l_pf_item_id.extend(p_atp_table.action.count);
623 
624        For m in 1..p_atp_table.action.count LOOP
625            IF  p_atp_table.ato_model_line_id(m) IS NOT NULL THEN
626                IF PG_DEBUG in ('Y', 'C') THEN
627                   msc_sch_wb.atp_debug('Schedule: p_atp_table.Identifier(m)' ||
628 	                                       p_atp_table.Identifier(m));
629                   msc_sch_wb.atp_debug('Schedule: ' ||
630 	                          'p_atp_table.Config_item_line_id(m)' ||
631 	                           p_atp_table.Config_item_line_id(m));
632                END IF;
633            END IF;
634            IF  p_atp_table.old_source_organization_id(m) IS NOT NULL THEN
635               l_pf_item_id(m) :=  MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
636                                                           l_plan_id,
637                                                            p_atp_table.inventory_item_id(m),
638                                                            p_atp_table.old_source_organization_id(m));
639               --l_old_org_id(m) := l_atp_table.old_source_organization_id(m);
640               IF PG_DEBUG in ('Y', 'C') THEN
641                  msc_sch_wb.atp_debug('Schedule: ' || 'Inventory item_id := ' || l_atp_table.inventory_item_id(m));
642                  msc_sch_wb.atp_debug('Schedule: ' || 'PF item id := ' || l_pf_item_id(m));
643               END IF;
644            END IF;
645        END LOOP;
646     END IF;
647 
648     -- remove data from table if records are already exists
649     -- ngoel 6/14, this is removed as OM shall be calling with
650     -- action as schedule while it needs to call as re-schedule.
651 
652     IF (p_atp_table.Action(1) = DEMANDMODIFY OR
653         p_atp_table.Action(1) = DEMANDADD OR
654         p_atp_table.Action(1) = DMDRSVADD) THEN --3720018
655 
656         --3720018, the code to call delete row is moved to call_delete_row.
657         -- Call_delete_row will take care of calling delete_row for the line/set.
658         -- The input to this procedure is p_atp_table.
659         -- l_delete_atp_rec will contain array to be used to call undo_delete_row.
660         -- l_delete_atp_rec also contains offset records.
661         MSC_ATP_DB_UTILS.call_delete_row (
662                          p_instance_id,
663                          p_atp_table,
664                          p_refresh_number,
665                          l_delete_atp_rec,
666                          l_return_status);
667 
668         IF PG_DEBUG in ('Y', 'C') THEN --3720018
669             msc_sch_wb.atp_debug('Schedule: l_return_status: ' || l_return_status);
670         END IF;
671 
672         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
673                         IF PG_DEBUG in ('Y', 'C') THEN
674                            msc_sch_wb.atp_debug('Schedule: ' || 'Error in Call_delete_row');
675                         END IF;
676                         x_return_status := FND_API.G_RET_STS_ERROR;
677                         RAISE FND_API.G_EXC_ERROR;
678         END IF;
679 
680        IF (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.TRY_ATP_LATER) OR
681           (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING) THEN
682            l_atp_table.error_code(1) := l_delete_atp_rec.error_code(1);
683            RAISE NO_DATA_FOUND;
684        END IF;
685     END IF;
686     IF PG_DEBUG in ('Y', 'C') THEN --3720018
687        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_demand_instance_id.count ' || l_delete_atp_rec.off_demand_instance_id.count);
688        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_supply_instance_id.count ' || l_delete_atp_rec.off_supply_instance_id.count);
689        msc_sch_wb.atp_debug('Schedule:l_delete_atp_rec.off_res_instance_id.count ' || l_delete_atp_rec.off_res_instance_id.count);
690        msc_sch_wb.atp_debug('Schedule: ' || 'l_old_plan_id.count = '||l_old_plan_id.count);
691     END IF;
692 
693     --3720018, error_code, attribute_07(plan_name) and old_plan_id are populated back to local variables.
694     l_atp_table.error_code   :=   l_delete_atp_rec.error_code;  --3720018
695     l_atp_table.attribute_07 :=   l_delete_atp_rec.attribute_07; --3720018
696     l_old_plan_id := l_delete_atp_rec.old_plan_id; --3720018
697 
698 
699     l_stmt := 40;
700     IF PG_DEBUG in ('Y', 'C') THEN
701        msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
702     END IF;
703 
704     -- bug 1382994: if action is demand, quantity should not be 0.
705     -- otherwise we also treat it as undemand.
706 
707     IF (((p_atp_table.Action(1) = ATPQUERY) or
708         (p_atp_table.Action(1) = DEMANDADD or
709           p_atp_table.Action(1) = DEMANDMODIFY)) AND
710            p_atp_table.Quantity_Ordered(1) <> 0) THEN -- 3720018, changed so that (p_atp_table.Quantity_Ordered(1) <> 0)
711                                                       -- condition is imposed always
712 
713 
714         l_sched_flag := BACKWARD_SCHEDULING;
715 
716    --     SAVEPOINT start_atp;
717 
718         -- get the profile option value
719         -- this profile option will determine what to deal with the output
720         -- will explain later.
721 
722         l_stmt := 50;
723         IF PG_DEBUG in ('Y', 'C') THEN
724            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
725         END IF;
726 
727         -- cchen: disable complete scatp for now
728         -- l_complete_scatp := NVL(FND_PROFILE.value('MRP_COMPLETE_SCATP'), 'N');
729         -- l_complete_scatp := 'Y';
730         -- msc_sch_wb.atp_debug('l_complete_scatp = '||l_complete_scatp);
731 
732         -- we may need to loop through every supply source until we succeed
733         -- if action is atp, depending on profile option mrp_complete_scatp
734         -- we do things differently:
735         -- Action: ATP, mrp_complete_scatp = 'Y'
736         --   return the info in the preferred warehouse. if we cannot
737         --   satisfy the request from the preferred warehouse,  give information
738         --   from every warehouse and don't consider lastest_acceptable_date
739         -- (Action: ATP, mrp_complete_scatp = 'N') or
740         -- (Action : DemandADD/Demandmodify) :
741         --   return just one warehouse which satisfies the request,  or
742         --   if no warehouse satisfies the request, return preferred warehouse.
743 
744         -- k is the index of arrival set.  if no arrival set, it will always be 1.
745 
746         k := 1;
747 
748         l_stmt := 60;
749         IF PG_DEBUG in ('Y', 'C') THEN
750            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
751         END IF;
752 
753         IF ((p_atp_table.action(k) <> ATPQUERY) OR
754              (p_atp_table.action(k) = ATPQUERY AND l_complete_scatp = 'N')) THEN
755             -- case 2
756             l_multi_scenario := 0;
757         ELSE
758             -- case 1
759             l_multi_scenario := 1;
760         END IF;
761 
762         -- loop through the items in the arrival set.
763         l_stmt := 70;
764         IF PG_DEBUG in ('Y', 'C') THEN
765            msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
766         END IF;
767 
768 
769         -- 1873918: PDS-ODS fix
770         -- introduce 2 variables to hold the global variable value.
771         -- we need to know the original values and then reset it each time.
772 
773         l_inv_ctp := MSC_ATP_PVT.G_INV_CTP;
774         l_allocated_atp := MSC_ATP_PVT.G_ALLOCATED_ATP;
775         l_allocation_method := NVL(MSC_ATP_PVT.G_ALLOCATION_METHOD, 2);
776 
777         --  savirine, Sep 05, 2001:  call get_regions to get regions info which will be used for ATP request.
778 
779 msc_sch_wb.atp_debug('p_atp_table.delivery_lead_time: ' || p_atp_table.delivery_lead_time(1));
780 
781         IF (p_atp_table.Source_Organization_id(1) IS NULL or p_atp_table.ship_method(1) IS NULL or
782            p_atp_table.delivery_lead_time(1) IS NULL) and (p_atp_table.Calling_Module(1) = 724)
783 	   and (NVL(p_atp_table.Customer_Site_Id(1), -1) <> NVL(G_SR_PARTNER_SITE_ID, -1)) THEN
784 
785            MSC_SATP_FUNC.Get_Regions (
786                                p_customer_site_id       => p_atp_table.Customer_Site_Id(1),
787                                p_calling_module         => p_atp_table.Calling_Module(1),
788                                 -- i.e. Source (ERP) or Destination (724)
789                                p_instance_id            => p_instance_id,
790                                p_session_id             => MSC_ATP_PVT.G_SESSION_ID,
791                                p_dblink                 => NULL,
792                                x_return_status          => l_return_status );
793 
794            -- ngoel 9/27/2001 : Assigned values to G_SR_PARTNER_SITE_ID GLOBAL Variable
795            -- bug 2108880. Move the line outside of IF stmt
796            --G_SR_PARTNER_SITE_ID := p_atp_table.customer_site_id(1);
797 
798         END IF;
799         G_SR_PARTNER_SITE_ID := p_atp_table.customer_site_id(1);
800         --2814895, Global varaibles created for atp_sources called from atp_check
801         G_SR_PARTY_SITE_ID := p_atp_table.party_site_id(1);
802         G_SR_CUSTOMER_COUNTRY := p_atp_table.customer_country(1);
803 
804         IF PG_DEBUG in ('Y', 'C') THEN
805            msc_sch_wb.atp_debug('Schedule: ' || 'Get_Regions, return status : ' || l_return_status);
806         END IF;
807 
808         IF l_return_status = FND_API.G_RET_STS_ERROR THEN
809            -- something wrong so we want to rollback;
810            IF PG_DEBUG in ('Y', 'C') THEN
811               msc_sch_wb.atp_debug('Schedule: ' || 'expected error in Call to Get_Regions');
812            END IF;
813         ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
814            IF PG_DEBUG in ('Y', 'C') THEN
815               msc_sch_wb.atp_debug('Schedule: ' || 'something wrong in Call to Get_Regions');
816            END IF;
817            RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
818         END IF;
819 
820         -- dsting setproc
821         MSC_ATP_PROC.Initialize_Set_Processing(p_atp_table);
822         l_set_status := null;
823 
824         WHILE (k IS NOT NULL) LOOP
825 
826          -- for bug 1259416: we need to reset the plan_id and assign set
827          -- default the plan_id to -1 and adjust it later.
828 
829          -- 1873918: PDS-ODS fix
830          IF PG_DEBUG in ('Y', 'C') THEN
831             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
832          END IF;
833          MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
834          -- ATP4drp re-set using original profile value.
835          MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
836          -- End ATP4drp
837          MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
838          l_plan_id := -1;
839          IF NVL(p_atp_table.Calling_Module(1), -1) = 724 THEN
840            IF PG_DEBUG in ('Y', 'C') THEN
841               msc_sch_wb.atp_debug('Schedule: ' || '724');
842            END IF;
843            l_assign_set_id := p_assign_set_id;
844          ELSE
845            -- convert the assignment set id from mrp to msc
846            IF PG_DEBUG in ('Y', 'C') THEN
847               msc_sch_wb.atp_debug('Schedule: ' || ' not 724');
848            END IF;
849            /************ Bug 2085071 Assignment set changes ************/
850            /* Remove duplicate query
851            //begin
852            //  select assignment_set_id
853            //  into   l_assign_set_id
854            //  from   msc_assignment_sets
855            //  where  sr_instance_id = p_instance_id
856            //  and    sr_assignment_set_id = p_assign_set_id;
857 
858            //exception
859            //  when no_data_found then
860            //   l_assign_set_id := NULL;
861            //end;
862            */
863            l_assign_set_id := lv_assign_set_id; -- Use new variable instead
864            IF PG_DEBUG in ('Y', 'C') THEN
865               msc_sch_wb.atp_debug('Schedule: ' || 'l_assign_set_id = '||l_assign_set_id);
866            END IF;
867            /************ Bug 2085071 Assignment set changes ************/
868            IF PG_DEBUG in ('Y', 'C') THEN
869               msc_sch_wb.atp_debug('Schedule: ' || 'after converting the assign set id');
870            END IF;
871 
872          END IF;
873 
874          -- for each item in an arrival set, reset the variables
875          l_overall_status := -1;
876          l_requested_item_id := p_atp_table.Inventory_Item_Id(k);
877          l_process_org_id := p_atp_table.Source_Organization_Id(k);
878          l_request_org_id := p_atp_table.organization_id(k);
879 
880          IF PG_DEBUG in ('Y', 'C') THEN
881             msc_sch_wb.atp_debug('Schedule: ' || 'requesting org id := ' || l_request_org_id);
882          END IF;
883          -- for bug 1052654
884          -- l_atp_table := p_atp_table;
885 
886          -- 2902129 move here
887          -- l_atp_table will be the processing record of tables that
888          -- contains the requests and output.
889          -- For an arrival set, l_atp_table only contains info for the
890          -- process item so the length of each table in l_atp_table should
891          -- be one.
892          -- For an ship set, l_atp_table contains info for all items in
893          -- that ship set, so the length of each table will be the same as the
894          -- length from the table in p_atp_table.
895          -- Note, since currently this request is either for a line (which
896          -- we kind of treat it as a ship set with one item only), an
897          -- arrival set, or a ship set but not a combination of arrival
898          -- set and ship set.
899 
900          IF l_arrival_count > 1 THEN
901              l_stmt := 150;
902              IF PG_DEBUG in ('Y', 'C') THEN
903                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
904              END IF;
905 
906              -- arrival set exists
907              l_atp_table := x_atp_table;  -- this is to null out l_atp_table;
908              -- get the info for this process item and put into l_atp_table
909              MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, k,
910                                   l_atp_table, l_return_status );
911          ELSE
912              l_stmt := 160;
913              IF PG_DEBUG in ('Y', 'C') THEN
914                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
915              END IF;
916 
917              l_atp_table := p_atp_table;
918 
919          END IF;
920 
921          --IF MSC_ATP_PVT.G_INV_CTP = 5 THEN
922          -- Bug 4091487 Checking so_tbl_status in PDS ATP also.
923            -- if this is ods atp, we need to make sure planning is not doing
924            -- the complete refresh . populate error code if we are in the
925            -- the complete refresh.
926 
927            SELECT so_tbl_status,
928                   NVL(summary_flag, 1),
929                   apps_ver
930            INTO   l_so_tbl_status,
931                   l_summary_flag,
932                   MSC_ATP_PVT.G_APPS_VER          -- 2300767
933            FROM   msc_apps_instances
934            WHERE  instance_id = p_instance_id;
935 
936            IF PG_DEBUG in ('Y', 'C') THEN
937               msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
938               msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = ' || l_summary_flag);
939               msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_APPS_VER = ' || MSC_ATP_PVT.G_APPS_VER);
940            END IF;  -- 2300767
941 
942 
943            IF NVL(l_so_tbl_status, 1) = 2 THEN
944              -- not available for atp
945              IF PG_DEBUG in ('Y', 'C') THEN
946                 msc_sch_wb.atp_debug('Schedule: ' || 'ATP not available');
947              END IF;
948 
949              l_atp_table.error_code(1) := TRY_ATP_LATER;
950              RAISE NO_DATA_FOUND;
951 
952              ELSIF l_summary_flag = 1 THEN
953                 ---- summary table is not ready. Switch to detail tables
954 		IF PG_DEBUG in ('Y', 'C') THEN
955 		   msc_sch_wb.atp_debug('Schedule: ' || ' summary table is not ready. Switch to detail tables');
956 		END IF;
957                 MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
958 
959              ELSIF l_summary_flag = 2 THEN
960 
961                 IF PG_DEBUG in ('Y', 'C') THEN
962                    msc_sch_wb.atp_debug('Schedule: ' || 'Summary Concurrent program is running');
963                 END IF;
964                 l_atp_table.error_code(1) := SUMM_CONC_PROG_RUNNING;
965                 RAISE NO_DATA_FOUND;
966              END IF;
967 
968          --END IF;
969 
970          -- get the atp source information :
971          -- if specified, use it. otherwise, get it from sourcing rules
972 
973          l_stmt := 80;
974          IF PG_DEBUG in ('Y', 'C') THEN
975             msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
976          END IF;
977 
978 
979          IF p_atp_table.Source_Organization_Id(k) is null  THEN
980             -- get the source org or vendor site
981 
982             l_stmt := 90;
983 
984 /*
985             IF (MSC_ATP_PVT.G_ASSIGN_SET_ID IS NULL) THEN
986                 FND_MESSAGE.set_name('MRP','MRP_NO_ATP_ASSIGN_SET');
987                 --  need to add error message
988                 l_atp_table.Error_Code(k) := ATP_NO_ASSIGN_SET;
989                 RAISE NO_DATA_FOUND;
990             END IF;
991 */
992             -- if this is actually an ship set, then get the common
993             -- warehouses for the whole ship set instead of this item.
994 
995             IF PG_DEBUG in ('Y', 'C') THEN
996                msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
997                msc_sch_wb.atp_debug('Schedule: ' || 'Before Atp_Sources');
998             END IF;
999 
1000             l_item_sourcing_info_rec.sr_inventory_item_id := l_atp_table.inventory_item_id;
1001             l_item_sourcing_info_rec.line_id := l_atp_table.identifier;
1002             l_item_sourcing_info_rec.ato_line_id := l_atp_table.ato_model_line_id;
1003             l_item_sourcing_info_rec.match_item_id := l_atp_table.match_item_id;
1004 
1005             --2814895
1006             IF (p_atp_table.customer_site_id(k) is not NULL ) THEN
1007                l_partner_type := 2;
1008             ELSIF ( p_atp_table.party_site_id(k) is not NULL) THEN
1009                l_partner_type := 4;
1010             ELSIF (p_atp_table.customer_country(k) is not NULL) THEN
1011                l_partner_type := 5;
1012             END IF;
1013 
1014             IF l_ship_count > 1 THEN
1015 
1016               l_stmt := 100;
1017               IF PG_DEBUG in ('Y', 'C') THEN
1018                  msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1019               END IF;
1020 
1021 
1022               MSC_ATP_PROC.Atp_Sources(p_instance_id,
1023                                     l_plan_id,
1024                                     l_requested_item_id,
1025                                     p_atp_table.Organization_Id(k),
1026                                     p_atp_table.Customer_Id(k),
1027                                     p_atp_table.Customer_Site_Id(k),
1028                                     l_assign_set_id,
1029                                     --s_cto_rearch
1030                                     l_item_sourcing_info_rec,
1031 				    --p_atp_table.inventory_item_id,
1032                                     MSC_ATP_PVT.G_SESSION_ID,
1033                                     l_sources,
1034                                     l_return_status,
1035                                     l_partner_type, --2814895
1036                                     p_atp_table.party_site_id(k), --2814895
1037                                     p_atp_table.identifier(k));  --2814895
1038             ELSE
1039 
1040               l_stmt := 110;
1041     	      IF PG_DEBUG in ('Y', 'C') THEN
1042     	         msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1043     	      END IF;
1044 
1045               MSC_ATP_PROC.Atp_Sources(p_instance_id,
1046                                     l_plan_id,
1047                                     l_requested_item_id,
1048                                     p_atp_table.Organization_Id(k),
1049                                     p_atp_table.Customer_Id(k),
1050                                     p_atp_table.Customer_Site_Id(k),
1051                                     l_assign_set_id,
1052                                     --s_cto_rearch
1053                                     l_item_sourcing_info_rec,
1054                                     --MRP_ATP_PUB.number_arr(NULL),
1055                                     MSC_ATP_PVT.G_SESSION_ID,
1056                                     l_sources,
1057                                     l_return_status,
1058                                     l_partner_type, --2814895
1059                                     p_atp_table.party_site_id(k), --2814895
1060                                     p_atp_table.identifier(k));  --2814895
1061             END IF;
1062 
1063             l_stmt := 120;
1064     	    IF PG_DEBUG in ('Y', 'C') THEN
1065     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1066                msc_sch_wb.atp_debug('Schedule: ' || 'After Atp_Sources');
1067                msc_sch_wb.atp_debug('Schedule: ' || 'l_sources.source_type.count = '||
1068                to_char(l_sources.source_type.count));
1069             END IF;
1070 
1071             -- check the return status
1072             IF l_return_status <> FND_API.G_RET_STS_SUCCESS OR
1073                 l_sources.source_type.count = 0  THEN
1074 
1075                 IF PG_DEBUG in ('Y', 'C') THEN
1076                    msc_sch_wb.atp_debug('Schedule: ' || 'atp source fails');
1077                 END IF;
1078                 x_return_status := FND_API.G_RET_STS_ERROR;
1079 
1080                 IF PG_DEBUG in ('Y', 'C') THEN
1081                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Customer_Id.COUNT :='||l_atp_table.Customer_Id.COUNT);
1082                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code.COUNT :='||l_atp_table.Error_Code.COUNT);
1083                 END IF;
1084                 --4091487 In case no records in msc_item_id_lid
1085                 --raise seperate error item not collected in case item not found in
1086 
1087                 IF l_return_status = MSC_ATP_PVT.G_ITEM_ID_NULL THEN
1088                    l_atp_table.error_code(k) := TRY_ATP_LATER;
1089                 ELSIF MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID IS NOT NULL or l_return_status = MSC_ATP_PVT.G_ITEM_NOT_COLL THEN
1090                      FOR l_count in 1..l_atp_table.error_code.count LOOP
1091                           IF l_atp_table.inventory_item_id(l_count) = MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID THEN
1092                               l_atp_table.error_code(l_count) := ATP_ITEM_NOT_COLLECTED;
1093                           END IF;
1094                      END LOOP;
1095                      --- set the variable back to null
1096                      MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1097                 ELSE
1098 
1099                      l_atp_table.Error_Code(k):= ATP_NO_SOURCES;
1100                 END IF;
1101                 RAISE NO_DATA_FOUND;
1102             END IF;
1103 
1104          ELSE
1105             -- we know the org. just assign it to l_sources.
1106             -- assume it is a transfer as well
1107 
1108             l_stmt := 130;
1109     	    IF PG_DEBUG in ('Y', 'C') THEN
1110     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1111                msc_sch_wb.atp_debug('G_MODEL_IS_PRESENT_IN_SET := ' || MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET);
1112     	    END IF;
1113 
1114             --s_cto_rearch
1115             IF NVL(MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET, 2) = 1 THEN
1116 
1117                MSC_ATP_CTO.Extend_Sources_Rec(l_sources);
1118 
1119                --L_atp_table contains either then whole ship set or
1120                --it contains a particular line for arrival set with index K.
1121 
1122                l_sources.organization_id(1) := l_process_org_id;
1123                l_sources.instance_id(1) := p_instance_id;
1124 
1125                --load data in temp table if line > 1
1126                IF l_atp_table.inventory_item_id.count > 1 THEN
1127                   DELETE MSC_SHIP_SET_TEMP;
1128                   FORALL l_count in 1..p_atp_table.inventory_item_id.count
1129                            INSERT INTO MSC_SHIP_SET_TEMP(inventory_item_id,
1130                                                          line_id)
1131                                                          VALUES
1132                                                          (p_atp_table.inventory_item_id(l_count),
1133                                                           p_atp_table.identifier(l_count));
1134                   IF PG_DEBUG in ('Y', 'C') THEN
1135                       msc_sch_wb.atp_debug('Rows inserted in MSC_SHIP_SET_TEMP := ' || SQL%ROWCOUNT);
1136                   END IF;
1137                END IF;
1138 
1139 
1140 
1141                MSC_ATP_CTO.Validate_CTO_Sources(l_sources,
1142                                                 l_atp_table.identifier,
1143                                                 p_instance_id,
1144                                                 MSC_ATP_PVT.G_SESSION_ID,
1145                                                 l_return_status);
1146 
1147                IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
1148                    --warehouse entered is not a valid warehouse for one of the models
1149                    l_atp_table.Error_Code(k):= MSC_ATP_PVT.INVALID_OSS_WAREHOUSE;
1150                    RAISE NO_DATA_FOUND;
1151                END IF;
1152 
1153             ELSE
1154                l_sources.Organization_Id:= MRP_ATP_PUB.number_arr(l_process_org_id);
1155                l_sources.Rank := MRP_ATP_PUB.number_arr(1);
1156                l_sources.Source_Type := MRP_ATP_PUB.number_arr(1);
1157                l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(-1);
1158                l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(-1);
1159                l_sources.Preferred := MRP_ATP_PUB.number_arr(1);
1160             END IF;
1161             --e_cto_rearch
1162          END IF;
1163 
1164          l_stmt := 140;
1165     	 IF PG_DEBUG in ('Y', 'C') THEN
1166     	    msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1167     	 END IF;
1168 
1169          -- go over each sources: j is the index for sources (warehouses)
1170          j := 1;
1171 
1172          /* 2902129 problem occurs if error is raised before we get here
1173           * so moved this chunk up before exception can be raised
1174          IF l_arrival_count > 1 THEN
1175              l_stmt := 150;
1176              IF PG_DEBUG in ('Y', 'C') THEN
1177                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1178              END IF;
1179 
1180              -- arrival set exists
1181              l_atp_table := x_atp_table;  -- this is to null out l_atp_table;
1182              -- get the info for this process item and put into l_atp_table
1183              MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, k,
1184                                   l_atp_table, l_return_status );
1185          ELSE
1186              l_stmt := 160;
1187              IF PG_DEBUG in ('Y', 'C') THEN
1188                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1189              END IF;
1190 
1191              l_atp_table := p_atp_table;
1192 
1193          END IF;
1194          */
1195 
1196          l_stmt := 170;
1197          IF PG_DEBUG in ('Y', 'C') THEN
1198             msc_sch_wb.atp_debug('Schedule: Stmt = ' ||l_stmt || ' : '||j || 'status : '|| l_overall_status);
1199          END IF;
1200 
1201          -- 2902265 do sane var initialization
1202          l_date_higher_src := 'N';
1203          l_arr_no_date := 'Y';
1204 
1205          WHILE (j IS NOT NULL) AND (l_overall_status <> ALLSUCCESS)  LOOP
1206 
1207           -- j is the index for sources
1208           ---- bug 1819638:  Get the next work day for sysdate
1209           IF (l_sources.Organization_Id(j) = -1) or (l_sources.Organization_Id(j) IS NULL) THEN
1210                  l_sourcing_org_id := l_request_org_id;
1211           ELSE
1212                  l_sourcing_org_id := l_sources.Organization_Id(j);
1213           END IF;
1214           --4386906
1215            l_ship_method := p_atp_table.ship_method(k);
1216            l_delivery_lead_time := p_atp_table.delivery_lead_time(k);
1217           --4386906
1218           IF PG_DEBUG in ('Y', 'C') THEN
1219              msc_sch_wb.atp_debug('Schedule: ' || 'l_sourcing_org_id :=' || l_sourcing_org_id);
1220              msc_sch_wb.atp_debug('Schedule: ' || 'p_instance_id := ' || p_instance_id);
1221              msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_method := ' || l_ship_method); --4386906
1222              msc_sch_wb.atp_debug('Schedule: ' || 'l_delivery_lead_tim := ' || l_delivery_lead_time); --4386906
1223           END IF;
1224 
1225 	  /* ship_rec_cal
1226 	  -- ngoel 9/25/2001, wrapped since this function raises an exception which needs to be handled.
1227 	  BEGIN
1228               l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(l_sourcing_org_id,
1229                                                         p_instance_id,
1230                                                         1,
1231                                                         sysdate);
1232 	  EXCEPTION
1233 	      WHEN others THEN
1234 		   null;
1235 	  END;
1236 
1237           IF l_sys_next_date IS NULL THEN
1238                IF PG_DEBUG in ('Y', 'C') THEN
1239                   msc_sch_wb.atp_debug('Schedule: ' || 'Sysdate not found in Calendar');
1240                END IF;
1241                l_atp_table.error_code(1) := NO_MATCHING_CAL_DATE;
1242                RAISE NO_DATA_FOUND;
1243           END IF;
1244 
1245           l_stmt := 180;
1246           IF PG_DEBUG in ('Y', 'C') THEN
1247              msc_sch_wb.atp_debug('Schedule: ' || 'l_sys_next_date = ' || l_sys_next_date);
1248              msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1249           END IF;*/
1250 
1251           -- initialize the group date in case it is a ship set
1252           -- Bug 2259824
1253 	  l_group_date := TRUNC(sysdate);--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
1254 	  IF PG_DEBUG in ('Y', 'C') THEN
1255              msc_sch_wb.atp_debug('Schedule: 2259824 l_group_date = '||
1256 		to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
1257           END IF;
1258 
1259           -- initialize the # of element success in a set.
1260           -- we will use this to decide if the whole set is successful or not.
1261           l_set_element_success := 0;
1262 
1263           -- dsting setproc
1264           -- 2902129
1265 --          if l_ship_count > 1 then
1266              MSC_ATP_PROC.Initialize_Set_Processing(l_atp_table);
1267              l_set_status := null;
1268 --          end if;
1269 
1270           -- i is the index for the record of tables in l_atp_table
1271           i := 1;
1272           WHILE (i IS NOT NULL)  LOOP
1273 
1274             l_stmt := 190;
1275 
1276             -- time_phased_atp - Reset variables
1277             l_time_phased_atp := 'N';
1278             l_pf_atp := 'N';
1279             l_atf_date := NULL;
1280             l_past_due_ship_date := 2; --bug4291375 resetting variable to no past due date
1281              --4386906
1282             l_atp_table.ship_method(i):=l_ship_method ;
1283             l_atp_table.delivery_lead_time(i) :=l_delivery_lead_time;
1284             --4386906
1285             -- 1873918: PDS-ODS fix
1286             IF PG_DEBUG in ('Y', 'C') THEN
1287     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1288                msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
1289                msc_sch_wb.atp_debug('Schedule: Reinitialize global org/item/plan recs');
1290             END IF;
1291             MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
1292             -- ATP4drp re-set using original profile value
1293             MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
1294             -- End ATP4drp
1295             MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
1296 
1297 	    -- 2741997
1298 	    MSC_ATP_PVT.G_PLAN_INFO_REC := l_null_plan_info_rec;
1299 	    MSC_ATP_PVT.G_ITEM_INFO_REC := l_null_item_info_rec;
1300 	    MSC_ATP_PVT.G_ORG_INFO_REC :=  l_null_org_info_rec;
1301 
1302             --diag_atp: Reset the allocation rule name varibale as it may get reset.
1303             MSC_ATP_PVT.G_ALLOCATION_RULE_NAME := null;
1304             IF PG_DEBUG in ('Y', 'C') THEN
1305                msc_sch_wb.atp_debug('Schedule: ' || 'G_INV_CTP '||G_INV_CTP|| ' G_ALLOCATED_ATP '||G_ALLOCATED_ATP);
1306             END IF;
1307 
1308             -- ship_rec_cal
1309             -- MSC_ATP_PVT.G_CALENDAR_CONSTRAINT := 'N';
1310             -- Bug3593394 - No need to reset it here. If contraint is not there then this will be explicitly set to 'N'
1311 
1312             -- assign l_process_item_id.
1313             -- this is for product family atp.  when we do
1314             -- product family atp, make sure l_process_item_id is
1315             -- the product_family_item_id, and l_requested_item_id
1316             -- is the product fmily member.
1317 
1318             -- 1072816
1319             -- default the plan_id to -1 and adjust it later.
1320             -- since we know the org already, we don't need
1321             -- to default the assignment set
1322 
1323             l_plan_id := -1;
1324             --bug 2301524
1325             -- After summary enhancement Summary will be disabled only for ODS case
1326             IF  (
1327                     (
1328                         MSC_ATP_PVT.G_DATABASE_LINK IS NOT NULL -- (Distributed case or BWB)
1329                         OR l_summary_flag = 200                 -- and ODS
1330                         OR p_atp_table.calling_module(1) = -1
1331                     )
1332                     AND MSC_ATP_PVT.G_INV_CTP = 5
1333                 )
1334                 OR
1335                 (
1336                     MSC_ATP_PVT.G_INV_CTP = 4                   -- PDS and
1337                     AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'       -- user defined allocated ATP
1338                     AND MSC_ATP_PVT.G_ALLOCATION_METHOD <> 1
1339                 ) THEN
1340                 IF PG_DEBUG in ('Y', 'C') THEN
1341                     msc_sch_wb.atp_debug('Schedule: ' || 'Distributed environment+BWB+ODS or rule-based-allocated-ATP+PDS, ' ||
1342                                          'always do ATP against details table');
1343                     msc_sch_wb.atp_debug('Schedule: ' || 'G_DATABASE_LINK := ' || MSC_ATP_PVT.G_DATABASE_LINK);
1344                     msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
1345                     msc_sch_wb.atp_debug('Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
1346                     msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag := ' || l_summary_flag);
1347                 END IF;
1348                 MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
1349             ELSIF MSC_ATP_PVT.G_INV_CTP = 4 THEN
1350                --- we re-read this summary profile option because it might get changed with plan
1351                MSC_ATP_PVT.G_SUMMARY_FLAG := NVL(FND_PROFILE.VALUE('MSC_ENABLE_ATP_SUMMARY'), 'N');
1352             END IF;
1353             IF PG_DEBUG in ('Y', 'C') THEN
1354                msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
1355             END IF;
1356 
1357             MSC_ATP_PVT.G_ORDER_LINE_ID := l_atp_table.Identifier(i);
1358             MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := l_atp_table.Identifier(i);
1359             MSC_ATP_PVT.G_COMP_LINE_ID := l_atp_table.Identifier(i);
1360             MSC_ATP_PVT.G_DEMAND_PEGGING_ID := null;
1361             MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'N';
1362 
1363             -- krajan : 2408902 - populate global variable (for use with CTO items)
1364             MSC_ATP_PVT.G_ATP_DEMAND_CLASS := l_atp_table.Demand_class(i);
1365             IF PG_DEBUG in ('Y', 'C') THEN
1366                msc_sch_wb.atp_debug('Schedule: ' || 'Setting global Demand_Class variable to : '|| MSC_ATP_PVT.G_ATP_DEMAND_CLASS );
1367             END IF;
1368 
1369 	    -- 9/24/2001, ngoel added to identify if current line is a MATO line, initializing here.
1370             MSC_ATP_PVT.G_CTO_LINE := 'N';
1371 
1372             l_stmt := 180;
1373 
1374             --6014992
1375             l_requested_item_id :=  nvl(l_atp_table.request_item_id(i), l_atp_table.inventory_item_id(i));
1376             -- l_process_org_id := l_atp_table.Source_Organization_Id(i);
1377             l_process_org_id := l_sources.Organization_Id(j);
1378 
1379             -- here we first to see if the item in ods is atpable or not.
1380             -- if not atpable, don't bother to look for plan_id
1381 
1382             -- set the variable to null before calling get_atp_flag
1383             MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1384 
1385             /* Modularize Item and Org Info */
1386             -- Get item information into the local variable
1387             -- and assign it to the global.
1388             --s_cto_rearch
1389             l_item_attribute_rec.parent_pegging_id := null;
1390             l_item_attribute_rec.parent_bom_item_type := 4;
1391             l_item_attribute_rec.parent_comp_flag := null;
1392             l_item_attribute_rec.parent_repl_ord_flag := 'N';
1393             --e_cto_rearch
1394             MSC_ATP_PROC.get_item_attributes(p_instance_id,
1395                                          l_plan_id,
1396                                          l_requested_item_id,
1397                                          l_process_org_id,
1398                                          l_item_attribute_rec);
1399             G_ITEM_INFO_REC := l_item_attribute_rec;
1400             l_atp_flag  := G_ITEM_INFO_REC.atp_flag;
1401             l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
1402             /* Modularize Item and Org Info */
1403             /*l_atp_flag := MSC_ATP_FUNC.get_atp_flag(p_instance_id,
1404                                        l_plan_id,
1405                                        l_requested_item_id,
1406                                        l_process_org_id);
1407              Modularize Item and Org Info */
1408 
1409             -- Bug 1878093, if item not collected, but was ATPable on the source. If YES,
1410             -- raise error as in PDS plan won't be available and ATP can't be done in ODS as well.
1411 
1412 			-- Need to check for atp_components_flag as well and fail if either of them were not 'N' on source.
1413 
1414             IF MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID IS NOT NULL  AND
1415                (NVL(l_atp_table.attribute_06(i), 'N') <> 'N' OR NVL(l_atp_table.atp_components_flag(i), 'N') <> 'N') THEN
1416 
1417                IF PG_DEBUG in ('Y', 'C') THEN
1418                   msc_sch_wb.atp_debug('Schedule: ' || 'Item not found : ' || MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID);
1419                END IF;
1420 
1421                FOR l_count in 1..l_atp_table.error_code.count LOOP
1422                    IF l_atp_table.inventory_item_id(l_count) = MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID THEN
1423                       l_atp_table.error_code(l_count) := ATP_ITEM_NOT_COLLECTED;
1424                    END IF;
1425                END LOOP;
1426 
1427                -- set variable as null and raise exception, dont proceed for item/ ship set.
1428                MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
1429 
1430                RAISE NO_DATA_FOUND;
1431 
1432             END IF;
1433 
1434             IF PG_DEBUG in ('Y', 'C') THEN
1435                 msc_sch_wb.atp_debug('Schedule: ' || 'ship_method: ' || l_atp_table.ship_method(i));
1436             END IF;
1437             -- dsting dlt
1438             IF nvl(l_atp_table.delivery_lead_time(i), -1) = -1 THEN
1439                 -- Bug 3449812 - Base DLT on internal_org_id if available
1440                 -- Bug 3515520, don't use org in case customer/site is populated.
1441                 -- IF l_atp_table.internal_org_id(i) IS NOT NULL THEN
1442 
1443                 IF (l_atp_table.internal_org_id(i) IS NOT NULL) OR
1444                    (l_atp_table.organization_id(i) IS NOT NULL AND l_atp_table.customer_id(i) is NULL AND
1445                     l_atp_table.customer_site_id(1) IS NULL AND
1446                     l_atp_table.party_site_id(1) IS NULL AND --2814895
1447                     l_atp_table.customer_country(1) IS NULL) THEN  --2814895
1448                     IF PG_DEBUG in ('Y', 'C') THEN
1449                        msc_sch_wb.atp_debug('Schedule: ' || 'Inside Org code');
1450                     END IF;
1451                     MSC_ATP_PROC.get_delivery_lead_time(
1452                                 l_sourcing_org_id,
1453                                 NULL,		-- from location id
1454                                 p_instance_id,
1455                                 -- l_atp_table.internal_org_id(i), -- Bug 3515520
1456                                 NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i)), -- Bug 3515520
1457                                 NULL,           -- to location id
1458                                 p_instance_id,
1459                                 NULL, -- customer id
1460                                 NULL, -- customer site id
1461                                 NULL, -- supplier id
1462                                 NULL, -- supplier site id
1463                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1464                                 NULL, -- partner site id
1465                                 l_atp_table.ship_method(i),
1466                                 l_atp_table.delivery_lead_time(i),
1467                                 NULL,
1468                                 NULL,
1469                                 NULL
1470                                 );
1471 
1472                 ELSIF (l_atp_table.customer_site_id(1) IS NOT NULL) THEN
1473                 IF PG_DEBUG in ('Y', 'C') THEN
1474                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside customer_site_id code');
1475                 END IF;
1476                     MSC_ATP_PROC.get_delivery_lead_time(
1477                                 l_sourcing_org_id,
1478                                 NULL,		-- from location id
1479                                 p_instance_id,
1480                                 --l_atp_table.organization_id(i), -- Bug 3515520
1481                                 NULL,           -- Bug 3515520
1482                                 NULL,           -- to location id
1483                                 p_instance_id,
1484                                 l_atp_table.customer_id(i),
1485                                 l_atp_table.customer_site_id(1),
1486                                 NULL,
1487                                 NULL,
1488                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1489                                 l_atp_table.customer_site_id(i),
1490                                 l_atp_table.ship_method(i),
1491                                 l_atp_table.delivery_lead_time(i),
1492                                 NULL,
1493                                 NULL,
1494                                 NULL
1495                                 );
1496 
1497                 ELSIF ( l_atp_table.party_site_id(1) IS NOT NULL ) THEN --2814895
1498                 IF PG_DEBUG in ('Y', 'C') THEN
1499                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside party_site_id code');
1500                 END IF;
1501                     MSC_ATP_PROC.get_delivery_lead_time(
1502                                 l_sourcing_org_id,
1503                                 NULL,		-- from location id
1504                                 p_instance_id,
1505                                 --l_atp_table.organization_id(i), -- Bug 3515520
1506                                 NULL,           -- Bug 3515520
1507                                 NULL, --l_atp_table.location_id(1), --NULL, --2814895, to location id
1508                                 p_instance_id,
1509                                 NULL, --l_atp_table.customer_id(i), 2814895
1510                                 NULL, --l_atp_table.customer_site_id(1), 2814895
1511                                 NULL,
1512                                 NULL,
1513                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1514                                 NULL, --l_atp_table.customer_site_id(i),
1515                                 l_atp_table.ship_method(i),
1516                                 l_atp_table.delivery_lead_time(i),
1517                                 4, --2814895, partner_type for party_site_id
1518                                 l_atp_table.party_site_id(1), --2814895
1519                                 NULL
1520                                 );
1521 
1522                 ELSIF ( l_atp_table.customer_country(1) IS NOT NULL) THEN  --2814895
1523                 IF PG_DEBUG in ('Y', 'C') THEN
1524                    msc_sch_wb.atp_debug('Schedule: ' || 'Inside address parameter code');
1525                 END IF;
1526                     MSC_ATP_PROC.get_delivery_lead_time(
1527                                 l_sourcing_org_id,
1528                                 NULL,		-- from location id
1529                                 p_instance_id,
1530                                 --l_atp_table.organization_id(i), -- Bug 3515520
1531                                 NULL,           -- Bug 3515520
1532                                 NULL,           -- to location id
1533                                 p_instance_id,
1534                                 NULL, --l_atp_table.customer_id(i), 2814895
1535                                 NULL, --l_atp_table.customer_site_id(1), 2814895
1536                                 NULL,
1537                                 NULL,
1538                                 MSC_ATP_PVT.G_SESSION_ID, -- ???
1539                                 NULL, --l_atp_table.customer_site_id(i), 2814895
1540                                 l_atp_table.ship_method(i),
1541                                 l_atp_table.delivery_lead_time(i),
1542                                 5, --2814895 , partner_type for address parameters
1543                                 NULL, --2814895
1544                                 l_atp_table.identifier(i));  --2814895
1545                 END IF;
1546             END IF;
1547 
1548             l_atp_table.delivery_lead_time(i) := CEIL(l_atp_table.delivery_lead_time(i));
1549             l_delivery_lead_time := l_atp_table.delivery_lead_time(i);
1550             l_ship_method := l_atp_table.ship_method(i);
1551 
1552             -- moved this here for ship_rec_cal
1553             IF l_atp_table.customer_id(i) IS NOT NULL THEN
1554                 -- bug3593394 - Run this SQL only if customer has changed.
1555                 IF (i=1) OR (l_atp_table.customer_id(i) <> NVL(l_atp_table.customer_id(i-1),-1)) THEN
1556                     BEGIN
1557                         SELECT TP_ID
1558                         INTO   MSC_ATP_PVT.G_PARTNER_ID
1559                         FROM   msc_tp_id_lid tp
1560                         WHERE  tp.SR_TP_ID = l_atp_table.customer_id(i)
1561                         AND    tp.SR_INSTANCE_ID = p_instance_id
1562                         AND    tp.PARTNER_TYPE = 2;
1563                     EXCEPTION
1564                         WHEN NO_DATA_FOUND THEN
1565                             MSC_ATP_PVT.G_PARTNER_ID := NULL;
1566                     END ;
1567                 END IF;
1568             ELSE
1569                 -- bug3593394 - reset to NULL
1570                 MSC_ATP_PVT.G_PARTNER_ID := NULL;
1571             END IF;
1572 
1573             IF PG_DEBUG in ('Y', 'C') THEN
1574                 msc_sch_wb.atp_debug('Schedule: ' || 'G_PARTNER_ID = '||MSC_ATP_PVT.G_PARTNER_ID);
1575             END IF;
1576 
1577             IF l_atp_table.customer_site_id(i) IS NOT NULL THEN
1578                 -- bug3593394 - Run this SQL only if customer site has changed.
1579                 IF (i=1) OR (l_atp_table.customer_site_id(i) <> NVL(l_atp_table.customer_site_id(i-1),-1)) THEN
1580                     BEGIN
1581                         SELECT TP_SITE_ID
1582                         INTO   MSC_ATP_PVT.G_PARTNER_SITE_ID
1583                         FROM   msc_tp_site_id_lid tpsite
1584                         WHERE  tpsite.SR_TP_SITE_ID = l_atp_table.customer_site_id(i)
1585                         AND    tpsite.SR_INSTANCE_ID =  p_instance_id
1586                         AND    tpsite.PARTNER_TYPE = 2;
1587                     EXCEPTION
1588                         WHEN NO_DATA_FOUND THEN
1589                             MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
1590                     END ;
1591                 END IF;
1592             ELSE
1593                 -- bug3593394 - reset to NULL
1594                 MSC_ATP_PVT.G_PARTNER_SITE_ID := NULL;
1595             END IF;
1596 
1597             IF PG_DEBUG in ('Y', 'C') THEN
1598                 msc_sch_wb.atp_debug('Schedule: ' || 'G_PARTNER_SITE_ID = '||MSC_ATP_PVT.G_PARTNER_SITE_ID);
1599             END IF;
1600 
1601             -- ship_rec_cal changes begin
1602             IF PG_DEBUG in ('Y', 'C') THEN
1603                msc_sch_wb.atp_debug('**************** Calendar Codes ******************************');
1604                msc_sch_wb.atp_debug('*  ___________________Input_____________________');
1605                msc_sch_wb.atp_debug('*  ');
1606                msc_sch_wb.atp_debug('*  Source Cust ID          : '|| l_atp_table.customer_id(i) );
1607                msc_sch_wb.atp_debug('*  Partner ID              : '|| MSC_ATP_PVT.G_PARTNER_ID );
1608                msc_sch_wb.atp_debug('*  Partner Site ID         : '|| MSC_ATP_PVT.G_PARTNER_SITE_ID );
1609                msc_sch_wb.atp_debug('*  Ship Method             : '|| l_atp_table.ship_method(i) );
1610                msc_sch_wb.atp_debug('*  Org ID                  : '|| l_atp_table.organization_id(i) );
1611                msc_sch_wb.atp_debug('*  Internal Org ID         : '|| l_atp_table.internal_org_id(i) );
1612                msc_sch_wb.atp_debug('*  Source Org ID           : '|| l_sourcing_org_id );
1613             END IF;
1614             -- Bug 3449812 - get receiving cal from ISO's destination org if available
1615             -- Bug 3515520, don't use org in case customer/site is populated.
1616             -- IF NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i))  IS NOT NULL THEN
1617 
1618             -- Bug 3647208 -- Move the check to individual "IF"s
1619             -- IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1620 
1621             IF (l_atp_table.internal_org_id(i) IS NOT NULL) OR
1622                 (l_atp_table.organization_id(i) IS NOT NULL AND l_atp_table.customer_id(i) is NULL AND
1623                 MSC_ATP_PVT.G_PARTNER_SITE_ID IS NULL) THEN
1624                 -- Bug3593394 - Call this proc only if parameters have changed
1625                 IF (i=1) OR (l_atp_table.internal_org_id(i)<>l_atp_table.internal_org_id(i-1))
1626                     OR (l_atp_table.organization_id(i)<>l_atp_table.organization_id(i-1))
1627                     OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1628 
1629                     l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1630                                                      p_instance_id,
1631                                                      null,
1632                                                      null,
1633                                                      null,
1634                                                      null,
1635                                                      null,
1636                                                      NVL(l_atp_table.internal_org_id(i), l_atp_table.organization_id(i)),
1637                                                      l_atp_table.ship_method(i),
1638                                                      MSC_CALENDAR.ORC);
1639                 ELSE
1640                     l_receiving_cal_code := l_atp_table.receiving_cal_code(i-1);
1641                     IF PG_DEBUG in ('Y', 'C') THEN
1642                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for ORC');
1643                     END IF;
1644                 END IF;
1645             ELSIF l_atp_table.customer_id(i) IS NOT NULL AND MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1646                 -- Bug 3647208 - Call CRC only if using ship/rec cal
1647                 -- receiving party is customer
1648                 -- Bug3593394 - Call this proc only if parameters have changed
1649                 IF (i=1) OR (NVL(l_atp_table.customer_id(i),-1)<>NVL(l_atp_table.customer_id(i-1),-1))
1650                     OR (NVL(l_atp_table.customer_site_id(i),-1)<>NVL(l_atp_table.customer_site_id(i-1),-1))
1651                     OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1652 
1653                     l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1654                                                      p_instance_id,
1655                                                      null,
1656                                                      null,
1657                                                      MSC_ATP_PVT.G_PARTNER_ID,
1658                                                      MSC_ATP_PVT.G_PARTNER_SITE_ID,
1659                                                      2,
1660                                                      null,
1661                                                      l_atp_table.ship_method(i),
1662                                                      MSC_CALENDAR.CRC);
1663                 ELSE
1664                     l_receiving_cal_code := l_atp_table.receiving_cal_code(i-1);
1665                     IF PG_DEBUG in ('Y', 'C') THEN
1666                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for CRC');
1667                     END IF;
1668                 END IF;
1669             ELSE
1670                 -- Neither org nor customer provided
1671                 l_receiving_cal_code := MSC_CALENDAR.FOC;
1672             END IF;
1673 
1674             IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
1675                 -- Bug 3647208 -- Move the check to individual "IF"s
1676                 IF (i=1) OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1677                     -- Bug3593394 - Call this proc only if parameters have changed
1678                     l_intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1679                                              p_instance_id,
1680                                              null,
1681                                              null,
1682                                              null,
1683                                              null,
1684                                              4,
1685                                              null,
1686                                              l_atp_table.ship_method(i),
1687                                              MSC_CALENDAR.VIC);
1688                 ELSE
1689                     l_intransit_cal_code := l_atp_table.intransit_cal_code(i-1);
1690                     IF PG_DEBUG in ('Y', 'C') THEN
1691                         msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for VIC');
1692                     END IF;
1693                 END IF;
1694             ELSE
1695                 l_intransit_cal_code := MSC_CALENDAR.FOC;
1696             END IF;
1697 
1698 
1699             -- Bug 3647208 -- Moved OSC and VIC to different IF blocks
1700             IF (i=1) OR (l_atp_table.ship_method(i)<>l_atp_table.ship_method(i-1)) THEN
1701                 -- Bug3593394 - Call this proc only for the first line in the ship set unless ship method has changed
1702                 --              No check on source org as "i" will be greater than one only for ship set.
1703                 l_shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1704                                              p_instance_id,
1705                                              null,
1706                                              null,
1707                                              null,
1708                                              null,
1709                                              null,
1710                                              l_sourcing_org_id,
1711                                              l_atp_table.ship_method(i),
1712                                              MSC_CALENDAR.OSC);
1713             ELSE
1714                 l_shipping_cal_code := l_atp_table.shipping_cal_code(i-1);
1715                 IF PG_DEBUG in ('Y', 'C') THEN
1716                     msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for OSC');
1717                 END IF;
1718             END IF;
1719 
1720 
1721             IF (i=1) THEN
1722                 -- Bug3593394 - Call this proc only for the first line in the ship set.
1723                 --              "i" will be greater than one only for ship set.
1724                 -- ATP4drp Call to obtain get_org_defaults.
1725                 /*
1726                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
1727                                              p_instance_id,
1728                                              null,
1729                                              null,
1730                                              null,
1731                                              null,
1732                                              null,
1733                                              l_sourcing_org_id,
1734                                              null,
1735                                              MSC_CALENDAR.OMC);
1736                */
1737                -- Bug 2783787 fix involving  removing the call to obtain org defaults
1738                -- restored here
1739                MSC_ATP_PROC.get_global_org_info(p_instance_id,
1740                                                 l_process_org_id);
1741                l_manufacturing_cal_code := MSC_ATP_PVT.G_ORG_INFO_REC.cal_code;
1742                -- End ATP4drp
1743             ELSE
1744                 l_manufacturing_cal_code := l_atp_table.manufacturing_cal_code(i-1);
1745                 IF PG_DEBUG in ('Y', 'C') THEN
1746                     msc_sch_wb.atp_debug('Schedule: ' || 'Calendar parameters have not changed for OMC');
1747                 END IF;
1748             END IF;
1749 
1750             IF PG_DEBUG in ('Y', 'C') THEN
1751                msc_sch_wb.atp_debug('  ');
1752                msc_sch_wb.atp_debug('*  ___________________Output____________________');
1753                msc_sch_wb.atp_debug('*  ');
1754                msc_sch_wb.atp_debug('*  Receiving calendar code         : ' || l_receiving_cal_code);
1755                msc_sch_wb.atp_debug('*  Intransit calendar code         : ' || l_intransit_cal_code);
1756                msc_sch_wb.atp_debug('*  Shipping calendar code          : ' || l_shipping_cal_code);
1757                msc_sch_wb.atp_debug('*  Manufacturing calendar code     : ' || l_manufacturing_cal_code);
1758                msc_sch_wb.atp_debug('**************************************************************');
1759             END IF;
1760 
1761             -- Add the calendar codes to l_atp_table
1762             l_atp_table.receiving_cal_code(i) := l_receiving_cal_code;
1763             l_atp_table.intransit_cal_code(i) := l_intransit_cal_code;
1764             l_atp_table.shipping_cal_code(i) := l_shipping_cal_code;
1765             l_atp_table.manufacturing_cal_code(i) := l_manufacturing_cal_code;
1766 
1767 	    /* Moved l_sys_next_date calculation code here */
1768 	    BEGIN
1769                 -- Bug 3371817 - sys_next_date should be as per OSC
1770                 --               maintaining l_sys_next_omc_date for PTF assignment.
1771                 -- Bug3593394 - Call this proc only if calendar has changed
1772                 IF (i=1) OR (l_shipping_cal_code<>l_atp_table.shipping_cal_code(i-1)) THEN
1773                     l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(
1774                                                         l_shipping_cal_code,
1775                                                         p_instance_id,
1776                                                         l_trunc_sysdate);
1777                 END IF;
1778 
1779                 IF l_manufacturing_cal_code=l_shipping_cal_code THEN
1780                     l_sys_next_omc_date := l_sys_next_date;
1781                 ELSIF (i=1) OR (l_manufacturing_cal_code<>l_atp_table.manufacturing_cal_code(i-1)) THEN
1782                     -- Bug3593394 - Call this proc only if calendar has changed
1783                     l_sys_next_omc_date := MSC_CALENDAR.NEXT_WORK_DAY(
1784                                                         l_manufacturing_cal_code,
1785                                                         p_instance_id,
1786                                                         l_trunc_sysdate);
1787 	        END IF;
1788 
1789 	        --bug3439591
1790 	        -- Bug3593394 - Call this proc only if calendar has changed
1791 	        IF l_receiving_cal_code=l_shipping_cal_code THEN
1792 	            l_sysdate_orc := l_sys_next_date;
1793 	        ELSIF l_receiving_cal_code=l_manufacturing_cal_code THEN
1794 	            l_sysdate_orc := l_sys_next_omc_date;
1795 	        ELSIF (i=1) OR (l_receiving_cal_code<>l_atp_table.receiving_cal_code(i-1)) THEN
1796 	            l_sysdate_orc := MSC_CALENDAR.NEXT_WORK_DAY(
1797                                                         l_receiving_cal_code,
1798                                                         p_instance_id,
1799                                                         l_trunc_sysdate);
1800                 END IF;
1801 	    EXCEPTION
1802                 WHEN others THEN
1803                    null;
1804 	    END;
1805 
1806             IF PG_DEBUG in ('Y', 'C') THEN
1807                 msc_sch_wb.atp_debug('Schedule: ' || 'l_sys_next_date = ' || l_sys_next_date);
1808                 msc_sch_wb.atp_debug('Schedule: ' || 'l_sysdate_orc = ' || l_sysdate_orc); --bug3439591
1809                 msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
1810             END IF;
1811 
1812             IF l_sys_next_date IS NULL THEN
1813                 IF PG_DEBUG in ('Y', 'C') THEN
1814                   msc_sch_wb.atp_debug('Schedule: ' || 'Sysdate not found in Calendar');
1815                 END IF;
1816                 l_atp_table.error_code(1) := NO_MATCHING_CAL_DATE;
1817                 RAISE NO_DATA_FOUND;
1818             END IF;
1819 	    -- ship_rec_cal changes end
1820 
1821             --- 2178544: We assign it to null so that this parameter set for last item is not used for current item.
1822             -- Bug 3512996 - For top level G_PTF_DATE should be as per OSC
1823             -- MSC_ATP_PVT.G_PTF_DATE := l_sys_next_omc_date;
1824             MSC_ATP_PVT.G_PTF_DATE := l_sys_next_date;
1825 
1826             --- bug 2152184: Moved this call here so that PF item could be determined based on ODS data
1827             IF (MSC_ATP_PVT.G_INV_CTP = 5) OR
1828                  (MSC_ATP_PVT.G_INV_CTP = 4 AND
1829                      NVL(l_atp_table.old_source_organization_id(i), -1) <> l_process_org_id ) THEN
1830                   IF PG_DEBUG in ('Y', 'C') THEN
1831                      msc_sch_wb.atp_debug('Schedule: ' || 'Either old source ors is null or is not same as l_process_org_id');
1832                      msc_sch_wb.atp_debug('Schedule: ' || 'Old org :=' || l_atp_table.old_source_organization_id(i));
1833                   END IF;
1834                   --- we get PF item id only if its a case of new schedulling
1835                   -- if its a reschedulling then we mayalready have got the id just for deleting
1836                   ---the previous order. For reschedulling we handel it in the elase part
1837                   l_process_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
1838                                                                        l_plan_id,
1839                                                                        l_requested_item_id,
1840                                                                        l_process_org_id);
1841             ELSE
1842                 IF l_arrival_count > 1 THEN
1843                      IF PG_DEBUG in ('Y', 'C') THEN
1844                         msc_sch_wb.atp_debug('Schedule: ' || 'Arrival Set');
1845                         msc_sch_wb.atp_debug('Schedule: ' || 'l_pf_item_id(k) := ' || l_pf_item_id(k));
1846                      END IF;
1847                      l_process_item_id := l_pf_item_id(k);
1848                 ELSE
1849                      IF PG_DEBUG in ('Y', 'C') THEN
1850                         msc_sch_wb.atp_debug('Schedule: ' || 'Ship Set');
1851                         msc_sch_wb.atp_debug('Schedule: ' || 'l_pf_item_id(i) := ' || l_pf_item_id(i));
1852                      END IF;
1853                      l_process_item_id := l_pf_item_id(i);
1854                 END IF;
1855             END IF;
1856             IF PG_DEBUG in ('Y', 'C') THEN
1857                msc_sch_wb.atp_debug('Schedule: ' || 'l_process_item_id := ' || l_process_item_id);
1858             END IF;
1859 
1860             IF (l_process_item_id <> l_requested_item_id) and (l_atp_flag = 'N') then
1861                   --For Product family based ATP we will flag an error if request comes for a
1862                   -- member item which has ATP flag = 'N'. For other members we will continue as usual
1863                   IF PG_DEBUG in ('Y', 'C') THEN
1864                      msc_sch_wb.atp_debug('Schedule: ' || 'Member item is not atpable');
1865                   END IF;
1866                   x_return_status := FND_API.G_RET_STS_ERROR;
1867                   l_atp_table.Error_Code(i):= PF_MEMBER_ITEM_NOT_ATPABLE;
1868                   RAISE NO_DATA_FOUND;
1869             END IF;
1870             -- since we know the org now, we can get the plan_id if it is PDS
1871 
1872             IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
1873               /* Modularize Item and Org Info */
1874               -- Information already obtained
1875               /*l_atp_comp_flag := MSC_ATP_FUNC.get_atp_comp_flag(p_instance_id,
1876                                         l_plan_id,
1877                                         l_requested_item_id,
1878                                         l_process_org_id);
1879                Modularize Item and Org Info */
1880 
1881 
1882               IF l_atp_flag <> 'N' OR l_atp_comp_flag <> 'N' THEN
1883                 /*
1884 		MSC_ATP_PROC.Get_Plan_Info(p_instance_id,
1885                             --2152184
1886                             l_process_item_id,
1887                             --l_requested_item_id,
1888    			    l_process_org_id,
1889                             l_atp_table.demand_class(i),
1890                             l_plan_id,
1891                             l_assign_set_id);
1892 
1893 		-- code changes for bug 2392456 starts
1894                  MSC_ATP_PROC.Get_Plan_Info(p_instance_id,
1895                             --2152184
1896                             l_process_item_id,
1897                             --l_requested_item_id,
1898                             l_process_org_id,
1899                             l_atp_table.demand_class(i),
1900                             l_plan_info_rec);
1901                 */
1902                 --s_cto_reach
1903                 l_plan_found_for_match := 2;
1904                 IF PG_DEBUG in ('Y', 'C') THEN
1905                      msc_sch_wb.atp_debug('Schedule: matc_id.count := ' || l_atp_table.match_item_id.count);
1906                      msc_sch_wb.atp_debug('Schedule: bom_item_type count := ' || l_atp_table.bom_item_type.count);
1907                      msc_sch_wb.atp_debug('match item is := ' || l_atp_table.match_item_id(i));
1908                 END IF;
1909                 IF l_atp_table.match_item_id(i) is not null and l_atp_table.bom_item_type(i) = 1 THEN
1910 
1911                    IF PG_DEBUG in ('Y', 'C') THEN
1912                       msc_sch_wb.atp_debug('Match Found, find plan for  matched item');
1913                    END IF;
1914 
1915                    /* time_phased_atp changes begin*/
1916                    l_match_item_family_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(
1917                                                   p_instance_id,
1918                                                   l_plan_id,
1919                                                   l_atp_table.match_item_id(i),
1920                                                   l_process_org_id
1921                                              );
1922 
1923                    /* Call new procedure Get_PF_Plan_Info*/
1924                    MSC_ATP_PF.Get_PF_Plan_Info(
1925                                p_instance_id,
1926                                l_atp_table.match_item_id(i),
1927                                l_match_item_family_id,
1928                                l_process_org_id,
1929                                l_atp_table.demand_class(i),
1930                                l_atf_date,
1931                                l_atp_table.error_code(i),
1932                                l_return_status,
1933                                NULL --bug3510475
1934                    );
1935 
1936                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1937                         IF PG_DEBUG in ('Y', 'C') THEN
1938                            msc_sch_wb.atp_debug('Schedule: ' || 'ATP Downtime during Re-schedule');
1939                         END IF;
1940                         x_return_status := FND_API.G_RET_STS_ERROR;
1941                         RAISE FND_API.G_EXC_ERROR;
1942                    END IF;
1943 
1944                    IF l_atf_date is not null THEN
1945                        l_time_phased_atp := 'Y';
1946                    ELSIF l_match_item_family_id <> l_atp_table.match_item_id(i) THEN
1947                        l_pf_atp := 'Y';
1948                    END IF;
1949                    /* time_phased_atp changes end*/
1950 
1951                    -- ATP4drp changes begin
1952                    -- No changes here since DRP plan should not support ATO.
1953                    -- ATP4drp changes end
1954 
1955                    IF  MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id is not null and
1956                            NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id, -1) <> -1 then
1957                         IF PG_DEBUG in ('Y', 'C') THEN
1958                             msc_sch_wb.atp_debug('plan found for  matched item');
1959                         END IF;
1960                         l_plan_found_for_match := 1;
1961                         l_atp_table.base_model_id(i) := l_process_item_id;
1962                         l_process_item_id := l_match_item_family_id;
1963 
1964                         --bug 4604500: pass correct request item it when match is found.
1965                         l_requested_item_id := l_atp_table.match_item_id(i);
1966 
1967                         --get item attributes of the matched item
1968                         MSC_ATP_PROC.get_item_attributes(p_instance_id,
1969                                          --3917625: Read attributes from correct plan
1970                                          --l_plan_id,
1971                                          MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id,
1972                                          l_atp_table.match_item_id(i),
1973                                          l_process_org_id,
1974                                          l_item_attribute_rec);
1975 
1976                          l_atp_table.matched_item_name(i) := l_item_attribute_rec.item_name;
1977 
1978                          l_atp_table.matched_item_name(i) := l_item_attribute_rec.item_name;
1979 
1980                    ELSE
1981                         IF PG_DEBUG in ('Y', 'C') THEN
1982                             msc_sch_wb.atp_debug('plan  not found for  matched item');
1983                         END IF;
1984                         l_plan_found_for_match := 2;
1985                         l_atp_table.match_item_id(i) := null;
1986 
1987                         -- we got plan for match. All fields in G_ITEM_INFO_REC were populated
1988                         --as a results plan was not getting read.
1989                         MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id := null;
1990                    END IF;
1991                 END IF;
1992 
1993                 IF PG_DEBUG in ('Y', 'C') THEN
1994                      msc_sch_wb.atp_debug('Schedule: l_plan_found_for_match := ' || l_plan_found_for_match);
1995                 END IF;
1996                 IF l_plan_found_for_match = 2 THEN
1997                    /* time_phased_atp changes begin
1998                       Call new procedure Get_PF_Plan_Info*/
1999                    MSC_ATP_PF.Get_PF_Plan_Info(
2000                                p_instance_id,
2001                                l_requested_item_id,
2002                                l_process_item_id,
2003                                l_process_org_id,
2004                                l_atp_table.demand_class(i),
2005                                l_atf_date,
2006                                l_atp_table.error_code(i),
2007                                l_return_status,
2008                                NULL --bug3510475
2009                    );
2010 
2011                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2012                         IF PG_DEBUG in ('Y', 'C') THEN
2013                            msc_sch_wb.atp_debug('Schedule: ' || 'ATP Downtime during Re-schedule');
2014                         END IF;
2015                         x_return_status := FND_API.G_RET_STS_ERROR;
2016                         RAISE FND_API.G_EXC_ERROR;
2017                    END IF;
2018 
2019                    -- ATP4drp changes begin
2020                    IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
2021                       -- time_phased_atp - Reset variables
2022                       l_process_item_id := l_requested_item_id;
2023                       MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2024                       IF PG_DEBUG in ('Y', 'C') THEN
2025                          msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2026                          msc_sch_wb.atp_debug('Schedule: ' || 'PF and Allocated ATP not applicable for DRP plans');
2027                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
2028                          msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_item_id ' || l_requested_item_id);
2029                          msc_sch_wb.atp_debug('Schedule: ' || 'l_process_item_id ' || l_process_item_id);
2030                          msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2031                       END IF;
2032                    ELSE
2033                       IF l_atf_date is not null THEN
2034                           l_time_phased_atp := 'Y';
2035                       ELSIF l_process_item_id <> l_requested_item_id THEN
2036                           l_pf_atp := 'Y';
2037                       END IF;
2038 
2039                    END IF;
2040                    -- ATP4drp changes end
2041                    /* time_phased_atp changes end*/
2042 
2043                 END IF;
2044 
2045                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
2046                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
2047                 l_plan_id       := l_plan_info_rec.plan_id;
2048                 /* time_phased_atp Add plan_id to l_atp_table so that it can later be used
2049                    this way we save call to get_global_plan_info*/
2050                 l_atp_table.plan_id(i) := l_plan_info_rec.plan_id;
2051                 l_assign_set_id := l_plan_info_rec.assignment_set_id;
2052                 IF l_atp_table.attribute_07.Exists(i) THEN
2053                         l_atp_table.attribute_07(i) := l_plan_info_rec.plan_name;
2054                 END IF;
2055                 --diag_atp
2056                 l_atp_rec.plan_name := l_plan_info_rec.plan_name;
2057                 -- code changes for bug 2392456 ends
2058 
2059                 -- 1873918: PDS-ODS fix
2060                 -- PDS-ODS fix
2061                 IF l_plan_id is NULL THEN
2062                   IF PG_DEBUG in ('Y', 'C') THEN
2063                      msc_sch_wb.atp_debug('Schedule: ' || 'no plan available');
2064                   END IF;
2065                   x_return_status := FND_API.G_RET_STS_ERROR;
2066                   --bug 2854351: Raise plan down time error
2067                   --l_atp_table.Error_Code(i):= PLAN_NOT_FOUND;
2068                   l_atp_table.Error_Code(i):= MSC_ATP_PVT.PLAN_DOWN_TIME;
2069                   RAISE NO_DATA_FOUND;
2070                 ELSIF l_plan_id = -100 THEN
2071                   IF PG_DEBUG in ('Y', 'C') THEN
2072                      msc_sch_wb.atp_debug('Schedule: ' || 'Summary Concurrent program is running');
2073                   END IF;
2074                   x_return_status := FND_API.G_RET_STS_ERROR;
2075                   l_atp_table.Error_Code(i):= SUMM_CONC_PROG_RUNNING;
2076                   RAISE NO_DATA_FOUND;
2077                 ELSIF l_plan_id = -200 THEN
2078                   IF PG_DEBUG in ('Y', 'C') THEN
2079                      msc_sch_wb.atp_debug('Schedule: ' || 'Post Plan Alloc progranm has not been run');
2080                   END IF;
2081                   x_return_status := FND_API.G_RET_STS_ERROR;
2082                   l_atp_table.Error_Code(i):= RUN_POST_PLAN_ALLOC;
2083                   RAISE NO_DATA_FOUND;
2084 
2085                 -- 24x7 ATP
2086                 ELSIF l_plan_id = -300 THEN
2087                   if PG_DEBUG in ('Y','C') THEN
2088                       msc_sch_wb.atp_debug ('Schedule: ' || 'ATP Downtime . 24x7 ATP');
2089                   END IF;
2090                   x_return_status := FND_API.G_RET_STS_ERROR;
2091                   --- bug 2854351: Show planned down time error
2092                   --l_atp_table.Error_Code(i) := TRY_ATP_LATER;
2093                   l_atp_table.Error_Code(i) := PLAN_DOWN_TIME;
2094                   RAISE NO_DATA_FOUND;
2095 
2096                 ELSIF l_plan_id = -1 THEN
2097                   IF PG_DEBUG in ('Y', 'C') THEN
2098                      msc_sch_wb.atp_debug('Schedule: ' || 'no plan available, switch to ods');
2099                   END IF;
2100 
2101                   l_atp_table.Error_Code(i):= PDS_TO_ODS_SWITCH;
2102                   IF PG_DEBUG in ('Y', 'C') THEN
2103                      msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code(i)='||l_atp_table.Error_Code(i));
2104                   END IF;
2105                   MSC_ATP_PVT.G_INV_CTP := 5;
2106                   MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
2107                   MSC_ATP_PVT.G_ALLOCATION_METHOD := 2;
2108 
2109                   /* Switching to ODS, Set the Global comp flag to NO */
2110                   --s_cto_rearch
2111                   IF G_ITEM_INFO_REC.bom_item_type in (1, 4) and G_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
2112                       --ATO Model or item need be exploded to do ATP on model level
2113                       IF G_ITEM_INFO_REC.atp_comp_flag = 'C' THEN
2114 
2115                          G_ITEM_INFO_REC.atp_comp_flag := 'Y';
2116 
2117                       ELSIF G_ITEM_INFO_REC.atp_comp_flag = 'R' THEN
2118 
2119                           G_ITEM_INFO_REC.atp_comp_flag := 'N';
2120 
2121                       END IF;
2122 
2123                   ELSE
2124                       G_ITEM_INFO_REC.atp_comp_flag := 'N';
2125                   END IF;
2126                   --e_cto_rearch
2127 
2128                   l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
2129                   --Moved below bug 4091487
2130                   /*
2131                   SELECT so_tbl_status,
2132                          NVL(summary_flag, 1)
2133                   INTO   l_so_tbl_status,
2134                          l_summary_flag
2135                   FROM   msc_apps_instances
2136                   WHERE  instance_id = p_instance_id;
2137 
2138                   IF PG_DEBUG in ('Y', 'C') THEN
2139                      msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
2140                      msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = '||l_summary_flag);
2141                   END IF;
2142 
2143                   -- Doing PDS-ODS switch. Summary will be diabled in all cases because ODS summary
2144                   -- will commit even PDS data from previous lines
2145                   msc_sch_wb.atp_debug('Schedule: ' || 'Summary will be disabled');
2146                   MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2147 
2148                   IF NVL(l_so_tbl_status, 1) = 2 THEN
2149                     -- not available for atp
2150                     IF PG_DEBUG in ('Y', 'C') THEN
2151                        msc_sch_wb.atp_debug('Schedule: ' || 'ods not available');
2152                     END IF;
2153 
2154                     l_atp_table.error_code(1) := TRY_ATP_LATER;
2155                     RAISE NO_DATA_FOUND;
2156 
2157                   END IF;
2158                   */
2159                 END IF; -- END IF l_plan_id is NULL
2160               --ELSE
2161               --- if ATP and ATPComp falg is no then we dont want to do summary approach
2162               --  MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2163                 -- Bug 4091487 Checking so_tbl_status in PDS ATP also.
2164                 SELECT so_tbl_status,
2165                          NVL(summary_flag, 1)
2166                 INTO   l_so_tbl_status,
2167                          l_summary_flag
2168                 FROM   msc_apps_instances
2169                 WHERE  instance_id = p_instance_id;
2170 
2171                 IF PG_DEBUG in ('Y', 'C') THEN
2172                    msc_sch_wb.atp_debug('Schedule: ' || 'l_so_tbl_status = '||l_so_tbl_status);
2173                    msc_sch_wb.atp_debug('Schedule: ' || 'l_summary_flag = '||l_summary_flag);
2174                 END IF;
2175 
2176                 IF NVL(l_so_tbl_status, 1) = 2 THEN
2177                     -- not available for atp
2178                     IF PG_DEBUG in ('Y', 'C') THEN
2179                        msc_sch_wb.atp_debug('Schedule: ' || 'ATP not available');
2180                     END IF;
2181 
2182                     l_atp_table.error_code(1) := TRY_ATP_LATER;
2183                     RAISE NO_DATA_FOUND;
2184                 END IF;
2185                 -- Summary will be diabled in all cases because ODS summary
2186                 -- will commit even PDS data from previous lines
2187                 IF l_plan_id = -1 THEN
2188 
2189                    MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2190 
2191                    IF PG_DEBUG in ('Y', 'C') THEN
2192                     msc_sch_wb.atp_debug('Schedule: ' || 'Summary will be disabled');
2193                    END IF;
2194 
2195                 END IF;
2196                END IF;
2197 
2198             END IF;
2199             --- bug 2152184: Retrival of PF item needs to be done on ODS data. Moved the call before plan is retrieved
2200             /*l_process_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
2201                                                     l_plan_id,
2202                                                     l_requested_item_id,
2203                                                     l_process_org_id); */
2204 
2205             l_stmt := 200;
2206     	    IF PG_DEBUG in ('Y', 'C') THEN
2207     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
2208     	    END IF;
2209 
2210             -- reinitialize some elements in l_atp_rec
2211             l_atp_rec.error_code := ALLSUCCESS;
2212             l_atp_rec.ship_date := NULL;
2213             l_atp_rec.available_quantity := NULL;
2214             l_atp_rec.requested_date_quantity := NULL;
2215             -- 2382963 : krajan for vivek
2216             l_atp_rec.arrival_date := NULL;
2217 
2218             -- cchen: 1238941
2219             l_atp_rec.combined_requested_date_qty := NULL;
2220 
2221             -- need to do uom quantity conversion
2222 
2223             MSC_ATP_PROC.inv_primary_uom_conversion(p_instance_id,
2224                                        l_sources.Organization_Id(j),
2225                                        l_process_item_id,
2226                                        l_atp_table.Quantity_UOM(i),
2227                                        l_quantity_uom,
2228                                        l_conversion_rate);
2229 
2230             l_quantity_ordered := l_atp_table.Quantity_Ordered(i)* l_conversion_rate;
2231             l_quantity_ordered := ROUND( l_quantity_ordered, 6); --4562646
2232             l_atp_rec.instance_id := p_instance_id;
2233             l_atp_rec.demand_source_line := l_atp_table.Identifier(i);
2234             l_atp_rec.identifier := l_atp_table.Identifier(i);
2235             l_atp_rec.demand_source_header_id:=l_atp_table.Demand_Source_Header_Id(i);
2236 	    l_atp_rec.demand_source_delivery:=l_atp_table.Demand_Source_Delivery(i);
2237 	    l_atp_rec.demand_source_type:=l_atp_table.Demand_Source_type(i);--cmro
2238             l_atp_rec.inventory_item_id := l_process_item_id;
2239             l_atp_rec.request_item_id := l_requested_item_id;
2240             l_atp_rec.organization_id := l_sources.Organization_Id(j);
2241             l_atp_rec.supplier_id := l_sources.Supplier_Id(j);
2242             l_atp_rec.supplier_site_id := l_sources.Supplier_Site_Id(j);
2243             l_atp_rec.quantity_ordered := l_quantity_ordered;
2244             l_atp_rec.quantity_uom := l_quantity_uom;
2245             l_atp_rec.requested_ship_date := l_atp_table.Requested_Ship_Date(i);
2246             l_atp_rec.requested_arrival_date := l_atp_table.Requested_Arrival_Date(i);
2247             l_atp_rec.latest_acceptable_date := l_atp_table.Latest_Acceptable_Date(i);
2248 	    -- dsting 2741997
2249             l_atp_rec.delivery_lead_time := l_atp_table.Delivery_Lead_Time(i);
2250             l_atp_rec.freight_carrier := l_atp_table.Freight_Carrier(i);
2251             l_atp_rec.ship_method := l_atp_table.Ship_Method(i);
2252 
2253             -- time_phased_atp
2254             l_atp_rec.atf_date := l_atf_date;
2255 
2256             -- ship_rec_cal
2257             l_atp_rec.shipping_cal_code       := l_atp_table.shipping_cal_code(i);
2258             l_atp_rec.receiving_cal_code      := l_atp_table.receiving_cal_code(i);
2259             l_atp_rec.intransit_cal_code      := l_atp_table.intransit_cal_code(i);
2260             l_atp_rec.manufacturing_cal_code  := l_atp_table.manufacturing_cal_code(i);
2261 
2262             -- Bug 3226083
2263             l_mem_dest_id := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2264             l_pf_dest_id := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
2265             /* ship_rec_cal changes begin
2266                If ship date is provided validate it on OSC
2267                If arrival date is provided validate it on ORC, offset using VIC, validate it on OSC*/
2268             IF PG_DEBUG in ('Y', 'C') THEN
2269                   msc_sch_wb.atp_debug('Schedule: ' || '___________________Start Offset___________________');
2270                   msc_sch_wb.atp_debug('Schedule: ' || 'Req Ship Date: ' || l_atp_rec.requested_ship_date);
2271                   msc_sch_wb.atp_debug('Schedule: ' || 'Req Arrival Date: ' || l_atp_rec.requested_arrival_date);
2272                   msc_sch_wb.atp_debug('Schedule:  ');
2273             END IF;
2274             -- Bug3593394 - Recalculate date only if parameters have changed
2275             IF (i=1)
2276                 OR (l_atp_rec.receiving_cal_code        <> l_atp_table.receiving_cal_code(i-1))
2277                 OR (l_atp_rec.intransit_cal_code        <> l_atp_table.intransit_cal_code(i-1))
2278                 OR (nvl(l_atp_rec.delivery_lead_time,0) <> nvl(l_atp_table.delivery_lead_time(i-1),0))
2279                 OR (l_atp_rec.shipping_cal_code         <> l_atp_table.shipping_cal_code(i-1))
2280                 OR (
2281                       (
2282                          (l_atp_rec.requested_arrival_date IS NULL)
2283                          OR (l_atp_table.Requested_Arrival_Date(i-1) IS NULL)
2284                          OR (l_atp_rec.requested_arrival_date    <> l_atp_table.Requested_Arrival_Date(i-1))
2285                       )
2286                       AND
2287                       (
2288                          (l_atp_rec.requested_ship_date IS NULL)
2289                          OR (l_atp_table.requested_ship_date(i-1) IS NULL)
2290                          OR (l_atp_rec.requested_ship_date       <> l_atp_table.requested_ship_date(i-1))
2291                       )
2292                    ) THEN
2293 
2294                 IF (l_atp_rec.requested_arrival_date IS NOT NULL) THEN
2295 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2296 						l_atp_rec.receiving_cal_code,
2297 						l_atp_rec.instance_id,
2298 						l_atp_rec.requested_arrival_date);
2299 
2300                     IF PG_DEBUG in ('Y', 'C') THEN
2301                           msc_sch_wb.atp_debug('Schedule: ' || 'Date after validating on ORC/CRC: ' || l_requested_ship_date);
2302                     END IF;
2303 
2304 		    IF trunc(l_requested_ship_date) <> trunc(l_atp_rec.requested_arrival_date) THEN
2305 		        G_CALENDAR_CONSTRAINT := 'Y';
2306 		    ELSE
2307 		        G_CALENDAR_CONSTRAINT := 'N'; -- bug3593394 - reset the global variable
2308 		    END IF;
2309 		    l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET(
2310 		                                l_atp_rec.intransit_cal_code,
2311 		                                l_atp_rec.instance_id,
2312 		                                l_requested_ship_date,
2313 		                                -1 * nvl(l_atp_rec.delivery_lead_time, 0),
2314 		                                -1);
2315 
2316                     IF PG_DEBUG in ('Y', 'C') THEN
2317                           msc_sch_wb.atp_debug('Schedule: ' || 'Date after subtracting intransit LT using VIC: ' || l_requested_ship_date);
2318                     END IF;
2319 
2320 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2321 						l_atp_rec.shipping_cal_code,
2322 						l_atp_rec.instance_id,
2323 						l_requested_ship_date);
2324 
2325                 ELSE
2326 		    l_requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
2327 						l_atp_rec.shipping_cal_code,
2328 						l_atp_rec.instance_id,
2329 						l_atp_rec.requested_ship_date);
2330 		    IF trunc(l_requested_ship_date) <> trunc(l_atp_rec.requested_ship_date) THEN
2331 		        G_CALENDAR_CONSTRAINT := 'Y';
2332 		    ELSE
2333 		        G_CALENDAR_CONSTRAINT := 'N'; -- bug3593394 - reset the global variable
2334 		    END IF;
2335                 END IF;
2336 
2337                 IF PG_DEBUG in ('Y', 'C') THEN
2338                   msc_sch_wb.atp_debug('Schedule: ' || 'Date after validating on OSC: ' || l_requested_ship_date);
2339                   msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_ship_date: ' || l_requested_ship_date);
2340                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CALENDAR_CONSTRAINT: ' || G_CALENDAR_CONSTRAINT);
2341                   msc_sch_wb.atp_debug('Schedule: ' || '___________________End Offset___________________');
2342                 END IF;
2343 
2344                 IF l_requested_ship_date is NULL THEN
2345                   l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_INVALID_DATE;
2346                   RAISE FND_API.G_EXC_ERROR;
2347                 END IF;
2348 
2349                 --bug 3322846: Move past due ship date to the first working day after sysdate
2350                 --L_MOVE_PAST_DUE_TO_SYSDATE := NVL(FND_PROFILE.value('MSC_MOVE_PAST_DUE_TO_SYSDATE'), 'Y'); -- Bug 5584634/5618929
2351                 L_MOVE_PAST_DUE_TO_SYSDATE := MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF;--6316476
2352                 IF PG_DEBUG in ('Y', 'C') THEN
2353                    msc_sch_wb.atp_debug('Schedule: ' || 'MOVE_PAST_DUE_TO_SYS_DATE :'|| L_MOVE_PAST_DUE_TO_SYSDATE);
2354                 END IF;
2355                 if L_MOVE_PAST_DUE_TO_SYSDATE = 'Y' THEN  -- Bug 5584634/5618929
2356                 IF l_requested_ship_date < l_sys_next_date THEN
2357 
2358                     IF PG_DEBUG in ('Y', 'C') THEN
2359                         msc_sch_wb.atp_debug('Schedule: ' || 'Request date is less than sysdate');
2360                     END IF;
2361                     l_requested_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
2362                                                 l_atp_rec.shipping_cal_code,
2363                                                 l_atp_rec.instance_id,
2364                                                 l_sys_next_date);
2365 
2366                     l_past_due_ship_date := 1; --bug4291375 setting for past due ship date
2367                     IF PG_DEBUG in ('Y', 'C') THEN
2368                         msc_sch_wb.atp_debug('Schedule: ' || 'New l_requested_ship_date := ' || l_requested_ship_date);
2369                     END IF;
2370                 END IF;
2371                 END IF; -- Bug 5584634/5618929
2372 
2373                 IF l_requested_ship_date is NULL THEN
2374                     l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_INVALID_DATE;
2375                     RAISE FND_API.G_EXC_ERROR;
2376                 END IF;
2377 
2378             ELSE -- Bug3593394
2379                 IF PG_DEBUG in ('Y', 'C') THEN
2380                   msc_sch_wb.atp_debug('Schedule: ' || 'Ship date parameters have not changed.');
2381                   msc_sch_wb.atp_debug('Schedule: ' || 'l_requested_ship_date: ' || l_requested_ship_date);
2382                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CALENDAR_CONSTRAINT: ' || G_CALENDAR_CONSTRAINT);
2383                   msc_sch_wb.atp_debug('Schedule: ' || '___________________End Offset___________________');
2384                 END IF;
2385             END IF; -- Bug3593394
2386 
2387             l_atp_rec.requested_ship_date := l_requested_ship_date;
2388             l_atp_rec.requested_arrival_date := null;
2389 
2390             /* ship_rec_cal changes end */
2391 
2392             -- aatp: in case we are doing allocated atp and demand class
2393             -- is not specified.
2394 
2395             -- 2783787 (ssurendr) Null demand class in request should be considered as "Others"
2396             /*
2397             IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2398                             (l_atp_table.Demand_Class(i) IS NULL) THEN
2399               MSC_ATP_PROC.get_global_org_info(l_atp_rec.instance_id,
2400                                                 l_atp_rec.organization_id);
2401               l_default_atp_rule_id := G_ORG_INFO_REC.default_atp_rule_id;
2402               l_calendar_code := G_ORG_INFO_REC.cal_code;
2403               l_calendar_exception_set_id := G_ORG_INFO_REC.cal_exception_set_id;
2404               l_default_demand_class := G_ORG_INFO_REC.default_demand_class;
2405               l_org_code := G_ORG_INFO_REC.org_code;
2406 
2407               l_atp_table.Demand_Class(i) := l_default_demand_class;
2408             END IF;
2409             */
2410 
2411             -- hierarchy changes
2412             -- due to arrival set issue, the dummy demand class will
2413             -- be done in atp_check
2414 
2415             l_atp_rec.demand_class := l_atp_table.Demand_Class(i);
2416 
2417             -- rajjain 02/11/2003 Bug 2788144
2418             --IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
2419 
2420             -- rajjain 02/11/2003 Bug 2788144
2421             --END IF;
2422 	    -- ATP Override rajjain begin
2423 	    IF l_atp_table.Action(i) = ATPQUERY THEN
2424                 IF PG_DEBUG in ('Y', 'C') THEN
2425                    msc_sch_wb.atp_debug('Schedule: ' || 'Override Flag: '||l_atp_rec.override_flag);
2426                 END IF;
2427 
2428 		l_atp_rec.override_flag := 'N';
2429 
2430                 IF PG_DEBUG in ('Y', 'C') THEN
2431 		   msc_sch_wb.atp_debug('Schedule: ' || 'ATPQUERY - Setting the Override Flag to : '||l_atp_rec.override_flag);
2432                 END IF;
2433 	    ELSE
2434                 l_atp_rec.override_flag := l_atp_table.Override_Flag(i);
2435 
2436 	    END IF;
2437 	    -- ATP Override rajjain end
2438 
2439             l_atp_rec.action := l_atp_table.Action(i);
2440             l_atp_rec.insert_flag := l_atp_table.Insert_Flag(i);
2441 
2442             -- for bug 1105741.  l_atp_rec.to_organization_id should be populated .
2443             l_atp_rec.to_organization_id := l_atp_table.Organization_Id(i);
2444             l_atp_rec.to_instance_id := p_instance_id;
2445 	    l_atp_rec.customer_id := l_atp_table.customer_id(i);
2446             l_atp_rec.refresh_number := p_refresh_number;
2447             l_atp_rec.atp_lead_time := l_atp_table.Atp_Lead_Time(i);
2448             l_atp_rec.Order_Number := l_atp_table.Order_Number(i);
2449             l_atp_rec.component_identifier := MSC_ATP_PVT.G_COMP_LINE_ID;
2450 
2451             ---This will be populated in case of reschedulling.
2452             --- This will be demand id for the old demand
2453             --bug 2384224: since we are always going to add demand, we dont need to remember old demand id
2454             --l_atp_rec.old_demand_id        := l_demand_ids(i);
2455 
2456 	    -- for multi_level/ multi_org cto support
2457             l_atp_rec.calling_module := l_atp_table.calling_module(i);
2458             l_stmt := 210;
2459     	    IF PG_DEBUG in ('Y', 'C') THEN
2460     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
2461 	       msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Inventory_Item_Id:' || to_char(l_atp_rec.Inventory_Item_Id)
2462 		||' l_atp_rec.request_item_id:' || to_char(l_atp_rec.request_item_id)
2463 		||' l_atp_rec.organization_id:' || to_char(l_atp_rec.organization_id) );
2464 	    END IF;
2465 
2466             -- get the search method:  this should come from
2467             -- a profile option (1 for depth and 2 for breadth).
2468             -- but for now, let's assume it is a depth search.
2469             -- the following logic is for depth first!!!!
2470 
2471             l_search := DEPTH ;
2472 
2473             -- since this is the top item, this is the first level
2474             -- make the first level to be 0
2475             l_level := 0;
2476 
2477             -- for a single line or ship set, the scenario id is the
2478             -- the order of sources
2479 
2480             l_scenario_id := j;
2481 
2482             -- CTO_PF_PRJ_2 changes for CTO PF Cross Project Impacts
2483             -- Set the default to No PF ATP Items being processed.
2484             MSC_ATP_PVT.G_CTO_PF_ATP := 'N';
2485             IF PG_DEBUG in ('Y', 'C') THEN
2486                msc_sch_wb.atp_debug('Schedule: ' || 'Before ATP_Check for this l_atp_rec ');
2487                msc_sch_wb.atp_debug('Schedule: Init G_CTO_PF_ATP to ' ||
2488                                                        MSC_ATP_PVT.G_CTO_PF_ATP);
2489             END IF;
2490             -- End CTO_PF_PRJ_2 changes for CTO PF Cross Project Impacts
2491 
2492             --- subst
2493 
2494             IF PG_DEBUG in ('Y', 'C') THEN
2495                msc_sch_wb.atp_debug('Schedule: ' || 'G_PLAN_SUBST_FLAG := ' || MSC_ATP_PVT.G_PLAN_SUBST_FLAG);
2496             END IF;
2497 
2498             IF MSC_ATP_PVT.G_INV_CTP = 4 AND
2499                NVL(MSC_ATP_PVT.G_PLAN_SUBST_FLAG, 2) = 1 THEN
2500                IF PG_DEBUG in ('Y', 'C') THEN
2501                   msc_sch_wb.atp_debug('Schedule: ' || 'count := ' || l_item_substitute_rec.inventory_item_id.count);
2502                   msc_sch_wb.atp_debug('Schedule: ' || 'G_CREATE_SUPPLY_FLAG := ' || MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG);
2503                   msc_sch_wb.atp_debug('Schedule: ' || 'get item attributes for the top item');
2504                END IF;
2505 
2506                -- check if one and only one of the requested_ship_date and
2507                -- requested_arrival_date is provided
2508                IF ((l_atp_rec.requested_ship_date IS NULL) and
2509                (l_atp_rec.requested_arrival_date IS NULL)) THEN
2510 
2511                    l_atp_rec.error_code := MSC_ATP_PVT.NOREQ_DATE;
2512                    -- krajan : 2437459
2513                    l_atp_table.error_code(i) := MSC_ATP_PVT.NOREQ_DATE;
2514 
2515                    RAISE FND_API.G_EXC_ERROR;
2516 
2517                ELSIF ((l_atp_rec.requested_ship_date IS NOT NULL) and
2518                     (l_atp_rec.requested_arrival_date IS NOT NULL)) THEN
2519 
2520                     l_atp_rec.error_code := MSC_ATP_PVT.ATP_MULTI_REQ_DATES;
2521                     -- krajan : 2437459
2522                     l_atp_table.error_code(i) := MSC_ATP_PVT.ATP_MULTI_REQ_DATES;
2523 
2524                     RAISE FND_API.G_EXC_ERROR;
2525                END IF;
2526                IF PG_DEBUG in ('Y', 'C') THEN
2527                   msc_sch_wb.atp_debug('Schedule: ' || 'get item attributes for the top item');
2528                END IF;
2529 
2530                -- old dlt code
2531 
2532                -- 1143475:
2533                -- IF atp lead time > 0 then we must do the offset no matter that item
2534                -- is atpable or not.  Otherwise, only if that item is atpable
2535                -- item we need to do the offset.  The reason of doing this is to avoid
2536                -- database hit.
2537 
2538                IF PG_DEBUG in ('Y', 'C') THEN
2539                   msc_sch_wb.atp_debug('Schedule: ' || 'Point 3');
2540                END IF;
2541 
2542                l_atp_rec.delivery_lead_time := l_delivery_lead_time;
2543                l_atp_rec.ship_method := l_ship_method;
2544                l_atp_rec.requested_ship_date := l_requested_ship_date;
2545                l_atp_rec.requested_arrival_date := null;
2546                IF PG_DEBUG in ('Y', 'C') THEN
2547                   msc_sch_wb.atp_debug('Schedule: ' || 'ship_method = '||l_ship_method ||' lead time = '||l_delivery_lead_time);
2548                END IF;
2549 
2550                -----------
2551                /* Modularize Item and Org Info */
2552                -- Information already obtained.
2553                /*MSC_ATP_PROC.get_global_item_info( p_instance_id,
2554                                                  -1,
2555                                                 l_atp_rec.inventory_item_id,
2556                                                 l_atp_rec.organization_id );
2557                */
2558                -- Changed assignments
2559                l_atp_flag := G_ITEM_INFO_REC.atp_flag;
2560                l_atp_comp_flag := G_ITEM_INFO_REC.atp_comp_flag;
2561                l_pre_pro_lt := G_ITEM_INFO_REC.pre_pro_lt;
2562                l_fixed_lt := G_ITEM_INFO_REC.fixed_lt;
2563                l_variable_lt := G_ITEM_INFO_REC.variable_lt;
2564                l_post_pro_lt := G_ITEM_INFO_REC.post_pro_lt;
2565                l_inventory_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
2566                l_item_name := G_ITEM_INFO_REC.item_name;
2567                IF PG_DEBUG in ('Y', 'C') THEN
2568                   msc_sch_wb.atp_debug('Schedule: ' || ' Create supply flag :=' ||
2569                                       G_ITEM_INFO_REC.create_supply_flag);
2570                END IF;
2571                /* Modularize Item and Org Info */
2572 
2573                l_substitution_type := l_atp_table.substitution_typ_code(i);
2574                IF PG_DEBUG in ('Y', 'C') THEN
2575                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_flag := ' || l_atp_flag);
2576                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_comp_flag := ' || l_atp_comp_flag);
2577                   msc_sch_wb.atp_debug('Schedule: ' || 'l_pre_pro_lt := ' || l_pre_pro_lt);
2578                   msc_sch_wb.atp_debug('Schedule: ' || 'l_fixed_lt := ' || l_fixed_lt);
2579                   msc_sch_wb.atp_debug('Schedule: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
2580                   msc_sch_wb.atp_debug('Schedule: ' || 'l_item_name :=' || l_item_name);
2581                   msc_sch_wb.atp_debug('Schedule: ' || 'l_substitution_type := ' || l_substitution_type);
2582                END IF;
2583                ---bug 2341073 : null out the l_item_substitute_rec else values for last item might get used again
2584                l_item_substitute_rec := l_null_item_sub_rec;
2585 
2586                /*
2587                time_phased_atp
2588                In non-time-phased PF Atp cases there won't be any end item substitution
2589                so no need to call Get_Item_Substitutes but we want to do breadth first search*/
2590                IF l_pf_atp <> 'Y' THEN
2591                        MSC_ATP_SUBST.Get_Item_Substitutes(l_inventory_item_id,
2592                                                         l_item_substitute_rec,
2593                                                         p_instance_id,
2594                                                         l_plan_id,
2595                                                         l_atp_rec.customer_id,
2596                                                         l_atp_table.customer_site_id(i),
2597                                                         l_requested_ship_date,
2598                                                         l_atp_rec.organization_id);
2599                END IF;
2600 
2601                IF PG_DEBUG in ('Y', 'C') THEN
2602                   msc_sch_wb.atp_debug('Schedule: ' || ' subst count := ' || l_item_substitute_rec.inventory_item_id.count);
2603                END IF;
2604 
2605                IF l_item_substitute_rec.inventory_item_id.count > 0 AND
2606                                                (NVL(l_substitution_type, 4) = 4 OR
2607                                                       l_atp_rec.override_flag = 'Y'
2608  					        ---diag_atp
2609                            			OR MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1) THEN
2610                   --- if item has substitutes but we dont want to do any substitution then
2611                   --- we will do breadth first search. But we dont want to use any substitutes.
2612                   --- Therefore we remove all the substitutes from the table
2613                   IF PG_DEBUG in ('Y', 'C') THEN
2614                      msc_sch_wb.atp_debug('Schedule: ' || 'Substitution defined but not doing substitution');
2615                   END IF;
2616                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'Y';
2617                   l_highest_rev := l_item_substitute_rec.highest_revision_item_id(1);
2618                   l_item_substitute_rec := l_null_item_sub_rec;
2619 
2620                ELSIF  NVL(l_substitution_type, 4) = 4 or
2621                      (l_item_substitute_rec.inventory_item_id.count = 0) THEN
2622                    IF PG_DEBUG in ('Y', 'C') THEN
2623                       msc_sch_wb.atp_debug('Schedule: ' || 'Substitute not defined');
2624                    END IF;
2625                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'N';
2626                ELSE
2627                   IF PG_DEBUG in ('Y', 'C') THEN
2628                      msc_sch_wb.atp_debug('Schedule: ' || 'Substitution defined and doing substitution');
2629                   END IF;
2630                   MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'Y';
2631                END IF;
2632 
2633                IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' THEN
2634 
2635                   IF MSC_ATP_SUBST.G_CREATE_SUPPLY_FLAG = 702
2636                            AND l_item_substitute_rec.inventory_item_id.count = 0 THEN
2637                       l_create_supply_on_orig_item := 1;
2638                   ELSE
2639                       -- Modular re-use of info.
2640                       l_create_supply_on_orig_item := G_ITEM_INFO_REC.create_supply_flag;
2641                       IF PG_DEBUG in ('Y', 'C') THEN
2642                          msc_sch_wb.atp_debug('Schedule: ' || ' Re-use Create supply flag :=' ||
2643                                       G_ITEM_INFO_REC.create_supply_flag);
2644                       END IF;
2645                       -- Modular re-use of info.
2646 
2647                   END IF;
2648 
2649 
2650                   --- now add the original item
2651                   l_count := l_item_substitute_rec.inventory_item_id.count;
2652                   IF PG_DEBUG in ('Y', 'C') THEN
2653                      msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
2654                   END IF;
2655                   MSC_ATP_SUBST.Extend_Item_Info_Rec_Typ(l_item_substitute_rec, l_return_status);
2656                   IF PG_DEBUG in ('Y', 'C') THEN
2657                      msc_sch_wb.atp_debug('Schedule: ' || 'After extending Item info');
2658                   END IF;
2659                   l_count := l_item_substitute_rec.inventory_item_id.count;
2660                   IF PG_DEBUG in ('Y', 'C') THEN
2661                      msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
2662                   END IF;
2663                  l_item_substitute_rec.inventory_item_id(l_count) := l_inventory_item_id;
2664                  --l_item_substitute_rec.sr_inventory_item_id(l_count) := l_atp_rec.inventory_item_id;
2665                  --l_requested_item_id has member_item_id.l_atp_rec.inventory_item_id has family id.
2666                  l_item_substitute_rec.sr_inventory_item_id(l_count) := l_requested_item_id; --bug3467631
2667 
2668                  -- time_phased_atp
2669                  IF l_time_phased_atp = 'Y' or l_pf_atp = 'Y' THEN
2670                      l_item_substitute_rec.Family_sr_id(l_count) := l_process_item_id;
2671                      l_item_substitute_rec.Family_dest_id(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.product_family_id;
2672                      l_item_substitute_rec.Family_item_name(l_count) := MSC_ATP_FUNC.Get_Inv_Item_Name(
2673                                                                                 p_instance_id,
2674                                                                                 l_process_item_id,
2675                                                                                 l_atp_rec.organization_id
2676                                                                         );
2677                      l_item_substitute_rec.atf_date(l_count) := l_atf_date;
2678                  ELSE
2679                      --bug3709707 passing correct item id
2680                      --l_item_substitute_rec.Family_sr_id(l_count) := l_inventory_item_id;
2681                      l_item_substitute_rec.Family_sr_id(l_count) := l_requested_item_id;
2682                      l_item_substitute_rec.Family_dest_id(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id;
2683                      l_item_substitute_rec.Family_item_name(l_count) := l_item_name;
2684                      l_item_substitute_rec.atf_date(l_count) := null;
2685                  END IF;
2686 
2687                  l_item_substitute_rec.plan_id(l_count) := l_plan_id;
2688                  --l_item_substitute_rec.assign_set_id.extend(l_count);
2689                  l_item_substitute_rec.assign_set_id(l_count) := l_assign_set_id;
2690                  l_item_substitute_rec.item_name(l_count) := l_item_name;
2691                  l_item_substitute_rec.atp_comp_flag(l_count) := l_atp_comp_flag;
2692                  l_item_substitute_rec.atp_flag(l_count) := l_atp_flag;
2693                  l_item_substitute_rec.post_pro_lt(l_count) := l_post_pro_lt;
2694                  l_item_substitute_rec.create_supply_flag(l_count) := l_create_supply_on_orig_item;
2695                  l_item_substitute_rec.highest_revision_item_id(l_count) := l_highest_rev;
2696 
2697                  -- dsting additional attributes for diagnostic atp
2698                  /* time_phased_atp
2699                  Substitution window should not be more than aggregate time fence days*/
2700                  IF (l_pf_atp = 'Y' OR l_time_phased_atp = 'Y') THEN
2701                   l_item_substitute_rec.substitution_window(l_count) := LEAST(MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window,
2702                                                                                 nvl(MSC_ATP_PF.Get_Atf_Days(
2703                                                                                         p_instance_id,
2704                                                                                         l_inventory_item_id,
2705                                                                                         l_atp_rec.organization_id
2706                                                                                     ),
2707                                                                                  0)
2708                                                                               );
2709                  ELSE
2710                   l_item_substitute_rec.substitution_window(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.substitution_window;
2711                  END IF;
2712 
2713                  l_item_substitute_rec.unit_weight(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_weight;
2714                  l_item_substitute_rec.weight_uom(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.weight_uom;
2715                  l_item_substitute_rec.unit_volume(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.unit_volume;
2716                  l_item_substitute_rec.volume_uom(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.volume_uom;
2717                  l_item_substitute_rec.rounding_control_type(l_count) := MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type;
2718                  l_item_substitute_rec.plan_name(l_count) := l_atp_rec.plan_name;
2719                  l_item_substitute_rec.pre_pro_lt(l_count) := l_pre_pro_lt;
2720                  l_item_substitute_rec.fixed_lt(l_count) := l_fixed_lt;
2721                  l_item_substitute_rec.variable_lt(l_count) := l_variable_lt;
2722 
2723 
2724                  --l_item_substitute_rec.requested_ship_date(l_count) := l_requested_ship_date;
2725                  FOR i in  1..l_item_substitute_rec.inventory_item_id.count LOOP
2726                     IF PG_DEBUG in ('Y', 'C') THEN
2727                        msc_sch_wb.atp_debug('Schedule: ' || 'Item ' || i || '  :=  ' || l_item_substitute_rec.inventory_item_id(i));
2728                     END IF;
2729                  END LOOP;
2730 
2731 
2732                  IF PG_DEBUG in ('Y', 'C') THEN
2733                     msc_sch_wb.atp_debug('Schedule: ' || 'l_item_count := ' || l_item_substitute_rec.inventory_item_id.count);
2734                  END IF;
2735                END IF;
2736 
2737             ELSE
2738 
2739                MSC_ATP_PVT.G_SUBSTITUTION_FLAG := 'N';
2740 
2741             END IF;
2742             IF PG_DEBUG in ('Y', 'C') THEN
2743                msc_sch_wb.atp_debug('Schedule: ' || 'G_SUBSTITUTION_FLAG := ' || MSC_ATP_PVT.G_SUBSTITUTION_FLAG);
2744                msc_sch_wb.atp_debug('Schedule: ' || 'l_plan_id := ' || l_plan_id);
2745                msc_sch_wb.atp_debug('Schedule: ' || 'G_INV_CTP := ' || MSC_ATP_PVT.G_INV_CTP);
2746             END IF;
2747 
2748             IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
2749 
2750                l_atp_rec.substitution_type := l_atp_table.substitution_typ_code(i);
2751                l_atp_rec.req_item_detail_flag := l_atp_table.req_item_detail_flag(i);
2752 
2753                --bug 4568088: pass original request date
2754                l_atp_rec.original_request_date := l_atp_table.original_request_date(i); --plan by request date
2755 
2756                MSC_ATP_SUBST.ATP_Check_SUBST(l_atp_rec,
2757                          l_item_substitute_rec,
2758                          l_requested_ship_date,
2759                          l_plan_id,
2760                          l_level,
2761                          l_scenario_id,
2762                          l_search,
2763                          p_refresh_number,
2764                          null, -- previous_pegging_id
2765                          l_assign_set_id,
2766                          l_atp_period,
2767                          l_atp_supply_demand,
2768                          l_return_status);
2769                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2770                    IF PG_DEBUG in ('Y', 'C') THEN
2771                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails');
2772                    END IF;
2773                    x_return_status := FND_API.G_RET_STS_ERROR;
2774 		   --2741997
2775                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
2776                    RAISE NO_DATA_FOUND;
2777                END IF;
2778 
2779                --bug3467631 start
2780                --Setting the l_bkwd_pass_atf_date_qty which is to be used in move_pf_bucketed_demands
2781                --for new time phase atp logic.This qty has backward pass request date atp+ctp qantity.
2782                l_atp_rec.requested_date_quantity := GREATEST(NVL(l_atp_rec.requested_date_quantity, 0), 0);
2783 
2784                IF l_atp_rec.requested_ship_date <= l_atf_date THEN
2785                 l_bkwd_pass_atf_date_qty :=l_atp_rec.requested_date_quantity;
2786                ELSE
2787                 l_bkwd_pass_atf_date_qty := l_atp_rec.atf_date_quantity;
2788                END IF;
2789 
2790                IF PG_DEBUG in ('Y', 'C') THEN
2791                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atf_date = '||l_atf_date);
2792                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date_quantity = '||l_atp_rec.atf_date_quantity);
2793                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.used_available_quantity = '||l_atp_rec.used_available_quantity);
2794                     msc_sch_wb.atp_debug('Schedule: ' || 'l_bkwd_pass_atf_date_qty = '||l_bkwd_pass_atf_date_qty);
2795                END IF;
2796                --bug3467631 end
2797 
2798                ---- add this item to MSC_ATP_PVT.G_PEGGING_FOR_SET as this is used to remove the demands if needed
2799                MSC_ATP_PVT.G_PEGGING_FOR_SET.extend;
2800                MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.extend;
2801                MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.count) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
2802                MSC_ATP_PVT.G_REQ_ATP_DATE.extend;
2803                MSC_ATP_PVT.G_REQ_DATE_QTY.extend;
2804 
2805                ---bug 2361283: For determining the date on which we want to see the substitution we offset the
2806                --- lead time from requested_ship_date and save it in l_atp_rec.requested_ship_date itself
2807                --- As a result we are loosing time component. So we restore the date back.
2808                IF l_atp_table.requested_ship_date(i) is not null THEN
2809                     l_atp_rec.requested_ship_date := l_atp_table.requested_ship_date(i);
2810                     IF PG_DEBUG in ('Y', 'C') THEN
2811                        msc_sch_wb.atp_debug('Schedule: ' || 'Orig Date := ' || to_char(l_atp_table.requested_ship_date(i), 'mm-dd-yy hh:mm:ss'));
2812                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset the ship date bug 2361283');
2813                        msc_sch_wb.atp_debug('Schedule: ' || 'Orig Date := ' || to_char(l_atp_rec.requested_ship_date, 'mm-dd-yy hh:mm:ss'));
2814                     END IF;
2815                END IF;
2816 
2817                  -- Commented for bug 2748730. G_END_OF_DAY not required here.
2818 		 /*
2819 		 -- for bug 2259824, add end of day to ship date
2820 		IF l_atp_rec.ship_date IS NOT NULL THEN
2821 		  l_atp_rec.ship_date := TRUNC(l_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
2822 		  IF PG_DEBUG in ('Y', 'C') THEN
2823              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
2824                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
2825           	  END IF;
2826 		END IF;
2827 		*/
2828 
2829             ELSE
2830                -- dsting 2465370
2831 		G_LINE_OVERRIDE_FLAG := l_atp_rec.override_flag;
2832                --s_cto_rearch
2833                l_atp_rec.Top_Model_line_id := l_atp_table.Top_Model_line_id(i);
2834                l_atp_rec.ATO_Parent_Model_Line_Id := l_atp_table.ATO_Parent_Model_Line_Id(i);
2835                l_atp_rec.ATO_Model_Line_Id := l_atp_table.ATO_Model_Line_Id(i);
2836                l_atp_rec.Parent_line_id := l_atp_table.Parent_line_id(i);
2837                l_atp_rec.wip_supply_type := l_atp_table.wip_supply_type(i);
2838                l_atp_rec.parent_atp_flag := null;
2839                l_atp_rec.parent_atp_comp_flag := null;
2840                l_atp_rec.parent_repl_order_flag := null;
2841                l_atp_rec.parent_bom_item_type := null;
2842                l_atp_rec.bom_item_type := G_ITEM_INFO_REC.bom_item_type;
2843                l_atp_rec.rep_ord_flag  := G_ITEM_INFO_REC.replenish_to_ord_flag;
2844                l_atp_rec.mand_comp_flag := 2;
2845                l_atp_rec.parent_so_quantity := l_atp_rec.quantity_ordered;
2846                l_atp_rec.original_request_date := l_atp_table.original_request_date(i); --plan by request date
2847                l_atp_rec.ship_set_name 	  :=l_atp_table.ship_set_name(i);               --plan by request date
2848                l_atp_rec.arrival_set_name :=l_atp_table.arrival_set_name(i);            --plan by request date
2849                l_atp_rec.override_flag	  :=l_atp_table.override_flag(i);               --plan by request date
2850 
2851 
2852                IF PG_DEBUG in ('Y', 'C') THEN
2853                     msc_sch_wb.atp_debug('Call ATP_Check');
2854                END IF;
2855                --e_cto_rearch
2856                MSC_ATP_PVT.ATP_Check(l_atp_rec,
2857                          l_plan_id,
2858                          l_level,
2859                          l_scenario_id,
2860                          l_search,
2861                          p_refresh_number,
2862                          null, -- previous_pegging_id
2863                          l_assign_set_id,
2864                          l_atp_period,
2865                          l_atp_supply_demand,
2866                          l_return_status);
2867 
2868                IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
2869                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
2870 
2871                ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2872                    IF PG_DEBUG in ('Y', 'C') THEN
2873                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails');
2874                    END IF;
2875                    x_return_status := FND_API.G_RET_STS_ERROR;
2876 		   --2741997
2877 --                   l_atp_table.Error_Code(k):= l_atp_rec.Error_Code;
2878                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
2879                    RAISE NO_DATA_FOUND;
2880                END IF;
2881 
2882                IF PG_DEBUG in ('Y', 'C') THEN
2883                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date_quantity = '||l_atp_rec.atf_date_quantity);
2884                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.used_available_quantity = '||l_atp_rec.used_available_quantity);
2885                END IF;
2886                -- No NVL is needed on atf_date_quantity bug3555084
2887                --l_atp_rec.atf_date_quantity := GREATEST(NVL(l_atp_rec.atf_date_quantity, 0), 0);
2888                --bug3397904 start
2889                IF l_atp_rec.requested_ship_date <= l_atf_date THEN
2890                 l_bkwd_pass_atf_date_qty := GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
2891                                           l_atp_rec.requested_date_quantity), 0);
2892                ELSE
2893                 l_bkwd_pass_atf_date_qty := NVL(l_atp_rec.atf_date_quantity,0);
2894                END IF;
2895                --bug3397904 end
2896                 -- Commented for bug 2748730. G_END_OF_DAY not required here.
2897 		/*
2898 		-- for bug 2259824, add end of day to ship date
2899 		IF l_atp_rec.ship_date IS NOT NULL THEN
2900 		  l_atp_rec.ship_date := TRUNC(l_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
2901 		  IF PG_DEBUG in ('Y', 'C') THEN
2902              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
2903                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
2904           	  END IF;
2905 		END IF;
2906 	       */
2907 	       --diag_atp
2908                IF MSC_ATP_PVT.G_FIND_FUTURE_DATE = 'Y'
2909                        AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 THEN
2910 
2911                  l_future_atp_rec := l_atp_rec;
2912 
2913                  l_future_atp_rec.error_code := ALLSUCCESS;
2914                  l_future_atp_rec.ship_date := NULL;
2915                  l_future_atp_rec.available_quantity := NULL;
2916                  l_future_atp_rec.requested_date_quantity := NULL;
2917                  -- cchen :1238941
2918                  l_future_atp_rec.combined_requested_date_qty := NULL;
2919                  l_future_atp_rec.quantity_uom := l_atp_rec.quantity_uom;
2920 
2921                  -- added greatest to ensure we won't set the quantity_ordered
2922                  -- wrong when requested_date_quantity is less than 0
2923 
2924                  -- cchen:1238941
2925 	         -- l_future_atp_rec.quantity_ordered := l_atp_rec.quantity_ordered-
2926                  --    GREATEST(l_atp_rec.requested_date_quantity, 0);
2927 
2928 
2929                  l_future_atp_rec.quantity_ordered := l_atp_rec.quantity_ordered -
2930                        GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
2931                                     l_atp_rec.requested_date_quantity), 0);
2932                  l_future_atp_rec.atf_date_quantity := NULL;
2933 
2934                  -- The date might have been set by a lower level component. we reset it again
2935                  -- Bug 3512996 - For top level G_PTF_DATE should be as per OSC
2936                  -- MSC_ATP_PVT.G_PTF_DATE := l_sys_next_omc_date;
2937                  MSC_ATP_PVT.G_PTF_DATE := l_sys_next_date;
2938 
2939                  IF PG_DEBUG in ('Y', 'C') THEN
2940                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.quantity_ordered ='||l_future_atp_rec.quantity_ordered );
2941                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity ='||l_atp_rec.requested_date_quantity);
2942 	            msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty ='||l_atp_rec.combined_requested_date_qty);
2943 	         END IF;
2944 
2945                  MSC_ATP_PVT.ATP_Check(l_future_atp_rec,
2946                          l_plan_id,
2947                          l_level,
2948                          l_scenario_id,
2949                          2,
2950                          p_refresh_number,
2951                          null, -- previous_pegging_id
2952                          l_assign_set_id,
2953                          l_future_atp_period,
2954                          l_future_atp_supply_demand,
2955                          l_return_status);
2956 
2957                  IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
2958                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
2959                 -- dsting 2764213
2960                 ELSIF l_return_status in (
2961                      MSC_ATP_PVT.G_ATO_SRC_MISMATCH,
2962                      MSC_ATP_PVT.G_ATO_UNCOLL_ITEM,
2963                      MSC_ATP_PVT.G_NO_PLAN_FOUND)
2964                 THEN
2965                    IF PG_DEBUG in ('Y', 'C') THEN
2966                       msc_sch_wb.atp_debug('Schedule: ' || 'atp check fails in future case');
2967                    END IF;
2968 		   --2741997
2969                    x_return_status := FND_API.G_RET_STS_ERROR;
2970                    l_atp_rec.error_code := l_future_atp_rec.Error_code;
2971                    l_atp_table.Error_Code(i):= l_atp_rec.Error_Code;
2972                    RAISE NO_DATA_FOUND;
2973                  END IF;
2974 
2975 		-- Commented for bug 2748730. G_END_OF_DAY not required here.
2976                  /*
2977 		 -- for bug 2259824, add end of day to ship date
2978 		l_atp_rec.ship_date := TRUNC(l_future_atp_rec.ship_date) + MSC_ATP_PVT.G_END_OF_DAY ;
2979 
2980 		IF PG_DEBUG in ('Y', 'C') THEN
2981              		msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_rec.ship_date = '||
2982                 		to_char(l_atp_rec.ship_date, 'DD-MON-YYYY HH24:MI:SS'));
2983           	END IF;
2984 		*/
2985 
2986                  l_atp_rec.ship_date := l_future_atp_rec.ship_date;
2987 		 -- make sure we won't ouput any available_quantity which is
2988                  -- greater than the infinite number
2989 
2990                  IF PG_DEBUG in ('Y', 'C') THEN
2991                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.available_quantity = '||
2992 			   to_char(nvl(l_future_atp_rec.available_quantity, -99)));
2993                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.used_available_quantity = '||
2994 			   l_future_atp_rec.used_available_quantity); --bug3409973
2995                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.combined_requested_date_qty = '||
2996 			   to_char(nvl(l_atp_rec.combined_requested_date_qty, -99)));
2997                     msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.requested_date_quantity = '||
2998 			   to_char(nvl(l_atp_rec.requested_date_quantity, -99)));
2999                     msc_sch_wb.atp_debug('Schedule: ' || 'atf_date_quantity before adding qty from future = '||l_atp_rec.atf_date_quantity);
3000                     msc_sch_wb.atp_debug('Schedule: ' || 'l_future_atp_rec.atf_date_quantity = '||l_future_atp_rec.atf_date_quantity);
3001                  END IF;
3002 
3003                  IF (l_future_atp_rec.atf_date_quantity is null) and (l_future_atp_rec.ship_date <= l_atf_date) THEN
3004                     l_future_atp_rec.atf_date_quantity := l_future_atp_rec.quantity_ordered;
3005                  END IF;
3006 
3007                  /* Bug 3265577*/
3008                  IF l_atp_rec.requested_ship_date > l_atf_date THEN
3009                          l_atp_rec.atf_date_quantity :=
3010                                  GREATEST(NVL(l_atp_rec.atf_date_quantity, 0), 0) +
3011                                  GREATEST(NVL(l_future_atp_rec.atf_date_quantity, 0), 0);
3012                  ELSE
3013                          l_atp_rec.atf_date_quantity :=
3014                                  GREATEST(NVL(l_atp_rec.requested_date_quantity, 0), 0) +
3015                                  GREATEST(NVL(l_future_atp_rec.atf_date_quantity, 0), 0);
3016                  END IF;
3017 
3018                  IF PG_DEBUG in ('Y', 'C') THEN
3019                     msc_sch_wb.atp_debug('Schedule: ' || 'atf_date_quantity after adding qty from future = '||l_atp_rec.atf_date_quantity);
3020                  END IF;
3021 
3022                  -- cchen 1238941
3023                  -- 1411818 : add greatest
3024                  l_atp_rec.available_quantity := LEAST(
3025                          GREATEST(l_future_atp_rec.available_quantity, 0) +
3026                          GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3027                              l_atp_rec.requested_date_quantity), 0),
3028                          INFINITE_NUMBER);
3029 
3030                  l_atp_rec.used_available_quantity:= LEAST(
3031                          GREATEST(l_future_atp_rec.used_available_quantity, 0) +
3032                          GREATEST(NVL(l_atp_rec.combined_requested_date_qty,
3033                              l_atp_rec.requested_date_quantity), 0),
3034                          INFINITE_NUMBER); --bug3409973
3035                  -- for aatp
3036                  -- 1411818: add greatest
3037                  l_atp_rec.requested_date_quantity :=
3038                          GREATEST(l_atp_rec.requested_date_quantity, 0) +
3039                          GREATEST(l_future_atp_rec.combined_requested_date_qty, 0);
3040 
3041                  -- 1524689
3042                  -- IF requested_date_quantity is greater than order quantity
3043                  -- due to stealing or we happen to find enough when we
3044                  -- do the forward case, then we should set the error code
3045                  -- to success.
3046 
3047                  IF l_atp_rec.requested_date_quantity >=
3048                     l_atp_rec.quantity_ordered  THEN
3049                    l_atp_rec.error_code := ALLSUCCESS;
3050                  END IF;
3051 
3052                  /* Bug 4211058 */
3053                  IF l_atp_rec.ship_date = l_atp_rec.requested_ship_date then
3054                     l_atp_rec.requested_date_quantity :=
3055                          l_atp_rec.available_quantity;
3056                  END IF;
3057 
3058                  MSC_ATP_PROC.Details_Output(l_future_atp_period,
3059                                 l_future_atp_supply_demand,
3060                                 l_atp_period,
3061                                 l_atp_supply_demand,
3062                                 l_return_status);
3063                ELSE
3064                  -- for aatp
3065                  l_atp_rec.requested_date_quantity :=
3066                     l_atp_rec.combined_requested_date_qty;
3067                END IF;
3068             END IF; -- IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4
3069             IF PG_DEBUG in ('Y', 'C') THEN
3070                msc_sch_wb.atp_debug('Schedule: ' || 'After ATP_Check for this l_atp_rec ');
3071             END IF;
3072             -- check the return status
3073             IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
3074                    l_atp_table.Error_Code(i):= MSC_ATP_PVT.OSS_SOURCING_ERROR;
3075             ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3076                 RAISE FND_API.G_EXC_ERROR;
3077             END IF;
3078 
3079             --bug 3328421: For past dues sales orders we start ATP check from first working
3080             -- day after sysdate as per shipping calender. Therefore, days late for request date type = ship
3081             -- should be calculated from this date as well.
3082             -- For req date type = arrival: Days late should be calculated from first work day after sysdate
3083             --- as per receiving party's receiving calendas. Since UI doesn't know this date we populate this date
3084 
3085             IF l_atp_table.requested_ship_date(i) is not null then
3086                 l_atp_table.first_valid_ship_arrival_date(i) := l_sys_next_date;
3087 
3088             ELSE
3089                 --arrival
3090                 l_atp_table.first_valid_ship_arrival_date(i) := l_sysdate_orc;
3091             END IF;
3092 
3093             l_stmt := 220;
3094     	    IF PG_DEBUG in ('Y', 'C') THEN
3095     	       msc_sch_wb.atp_debug('Schedule: Stmt = '||l_stmt);
3096     	    END IF;
3097 
3098 
3099             -- now we get the results back and we need to analyze them
3100             -- here is the logic:
3101             -- if l_multi_scenario = 1
3102             --      < case 1 >
3103             --      if error_code = ALLSUCCESS and it is a preferred WH
3104             --         this is the answer we want, exit the loop
3105             --      else
3106             --         add the results to temp table or arr table
3107             --      end if
3108             -- else
3109             --      < case 2 >
3110             --      if error_code = ALLSUCESS or
3111             --      inside the latest acceptable range
3112             --          this is the answer we want, exit the loop
3113             --      else if we do have ship_date and temp table is empty
3114             --          add the results to temp table
3115             --      end if
3116             -- end
3117 
3118             -- ATP Override -- Agilent changes Begin
3119 
3120             -- bug 2795053-reopen (ssurendr)
3121             --s_cto_rearch: do not honor atp lead time
3122             l_dmd_satisfied_date := l_atp_rec.ship_date;
3123             /*
3124                l_dmd_satisfied_date := MSC_CALENDAR.DATE_OFFSET(
3125                                                    l_atp_rec.organization_id,
3126                                                    p_instance_id,
3127                                                    1,
3128                                                    l_atp_rec.ship_date,
3129                                                    -NVL(l_atp_rec.atp_lead_time, 0));
3130             */
3131             --e_cto_rearch
3132             IF PG_DEBUG in ('Y', 'C') THEN
3133               msc_sch_wb.atp_debug('Schedule: ' || 'l_dmd_satisfied_date: ' || l_dmd_satisfied_date);
3134             END IF;
3135 
3136             IF ( (NVL(l_atp_rec.Override_Flag, 'N') = 'Y')
3137                AND l_atp_rec.Action <> ATPQUERY ) THEN
3138 
3139                /* ship_rec_cal changes begin */
3140                IF PG_DEBUG in ('Y', 'C') THEN
3141                   msc_sch_wb.atp_debug('Schedule: ' || '_______________Override Scenario_______________ ');
3142                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.requested_ship_date(i): ' || l_atp_table.requested_ship_date(i));
3143                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.requested_arrival_date(i): ' || l_atp_table.requested_arrival_date(i));
3144                END IF;
3145                IF (l_atp_table.requested_ship_date(i) IS NOT NULL) THEN
3146                   l_atp_rec.ship_date := l_atp_table.requested_ship_date(i);
3147 
3148                   -- Bug 3520301 - dont consider first calendar in override cases
3149                   l_atp_rec.arrival_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3150         					l_atp_rec.ship_date, MSC_CALENDAR.FOC, 1,
3151         					l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3152         					l_atp_rec.receiving_cal_code, 1, p_instance_id);
3153                ELSE
3154                   l_atp_rec.arrival_date := l_atp_table.requested_arrival_date(i);
3155 
3156                   l_atp_rec.ship_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3157 						-- Bug 3520301 - dont consider first calendar in override cases
3158 						-- l_atp_table.requested_arrival_date(i), l_atp_rec.receiving_cal_code, -1,
3159 						l_atp_table.requested_arrival_date(i), MSC_CALENDAR.FOC, -1,
3160 						l_atp_rec.intransit_cal_code, -1 * nvl(l_atp_rec.delivery_lead_time, 0), -1,
3161 						l_atp_rec.shipping_cal_code, -1, p_instance_id);
3162                END IF;
3163                IF PG_DEBUG in ('Y', 'C') THEN
3164                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.ship_date: ' || l_atp_rec.ship_date);
3165                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.arrival_date: ' || l_atp_rec.arrival_date);
3166                END IF;
3167 
3168             /* If ship date returned is greater than requested ship date move it to OSC */
3169             ELSIF trunc(l_atp_rec.ship_date) > trunc(l_atp_rec.requested_ship_date) THEN
3170                l_atp_rec.ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
3171                                                 l_atp_rec.shipping_cal_code,
3172                                                 p_instance_id,
3173                                                 l_atp_rec.ship_date);
3174                IF PG_DEBUG in ('Y', 'C') THEN
3175                   msc_sch_wb.atp_debug('Schedule: ' || 'ship_date > req_ship_date');
3176                   msc_sch_wb.atp_debug('Schedule: ' || '_______________After validating ship date on OSC_______________ ');
3177                   msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.ship_date: ' || l_atp_rec.ship_date);
3178                END IF;
3179             END IF;
3180             /* ship_rec_cal changes end */
3181 
3182             l_stmt := 230;
3183             --- Bug 1621816 Calculate arrival date and take date offset into account
3184             IF (l_atp_rec.ship_date IS NOT NULL)  AND (l_atp_rec.arrival_date IS NULL) THEN
3185 
3186                 /* ship_rec_cal changes begin */
3187                 l_atp_rec.arrival_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3188         					l_atp_rec.ship_date, l_atp_rec.shipping_cal_code, 1,
3189         					l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3190         					l_atp_rec.receiving_cal_code, 1, p_instance_id) ;
3191                                           --4460369+ MSC_ATP_PVT.G_END_OF_DAY;
3192                 /* ship_rec_cal changes end */
3193 
3194                 IF l_atp_rec.requested_arrival_date is not null THEN
3195                    l_atp_rec.arrival_date := TRUNC(GREATEST(l_atp_rec.requested_arrival_date,
3196                                                l_atp_rec.arrival_date));--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3197                 END IF;
3198 
3199                 IF PG_DEBUG in ('Y', 'C') THEN
3200                         msc_sch_wb.atp_debug('Schedule: l_atp_rec.arrival_date = '||
3201                                 to_char(l_atp_rec.arrival_date, 'DD-MON-YYYY HH24:MI:SS'));
3202                 END IF;
3203 
3204             END IF;
3205             /*plan by request date changes begin */
3206             IF l_atp_rec.requested_arrival_date IS NULL THEN
3207 	        -- date type is ship
3208 	    	l_original_req_arrival_date     := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3209                                                         NVL(l_atp_rec.original_request_date,l_atp_rec.requested_ship_date),
3210                                                         l_atp_rec.shipping_cal_code, 1,
3211                                                         l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3212                                                         l_atp_rec.receiving_cal_code, 1, p_instance_id) ;
3213                                                         --4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3214             ELSE
3215 	    	-- date type is arrival
3216 	    	l_original_req_arrival_date     := NVL(l_atp_rec.original_request_date,l_atp_rec.requested_arrival_date);
3217             END IF;
3218             IF PG_DEBUG in ('Y', 'C') THEN
3219                 msc_sch_wb.atp_debug('Schedule: request arrival date = '||
3220                         to_char(l_atp_rec.requested_arrival_date, 'DD-MON-YYYY HH24:MI:SS'));
3221             END IF;
3222             l_req_arr_date(i) := l_original_req_arrival_date;
3223             /*plan by request date changes end */
3224             -- populate the output info l_atp_table
3225             IF PG_DEBUG in ('Y', 'C') THEN
3226                msc_sch_wb.atp_debug('Schedule: ' || 'Before Assigning the values to l_atp_table');
3227                msc_sch_wb.atp_debug('Schedule: ' || 'i = '||to_char(i));
3228                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.error_code='||l_atp_rec.error_code);
3229                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.org_id='|| l_atp_rec.Organization_Id);
3230                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Ship_Method='|| l_atp_rec.Ship_Method);
3231                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Delivery_Lead_Time='|| l_atp_rec.Delivery_Lead_Time);
3232                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Ship_Date='||l_atp_rec.Ship_Date);
3233                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Available_Quantity='||
3234                                   l_atp_rec.Available_Quantity);
3235                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.Requested_Date_Quantity='||
3236                                   l_atp_rec.Requested_Date_Quantity);
3237                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.error_code='||l_atp_rec.error_code);
3238                --bug3709707 added debug messages
3239                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.request_item_name='||l_atp_rec.request_item_name);
3240                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.inventory_item_name='||l_atp_rec.inventory_item_name);
3241                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.original_item_name='||l_atp_rec.original_item_name);
3242                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Inventory_item_id(i)='||l_atp_table.Inventory_item_id(i));
3243                msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_rec.atf_date='||l_atp_rec.atf_date);
3244             END IF;
3245             --- subst
3246             IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' THEN
3247 
3248                IF l_atp_rec.inventory_item_id <> l_atp_table.Inventory_item_id(i) THEN
3249                   --- substitution has occured. Load subst_flag for substitution workflow
3250                   l_atp_table.subst_flag(i) := 1;
3251                END IF;
3252 
3253                IF l_atp_rec.atf_date is not null THEN --bug3467631
3254                  l_atp_table.Inventory_Item_Name(i) := l_atp_rec.request_item_name;
3255                  --bug3709707
3256                  l_atp_table.Inventory_item_id(i) := l_atp_rec.request_item_id;
3257                ELSE
3258                  l_atp_table.Inventory_Item_Name(i) := l_atp_rec.inventory_item_name;
3259                  --bug3709707
3260                  l_atp_table.Inventory_item_id(i) := l_atp_rec.inventory_item_id;
3261                END IF;
3262 
3263                IF PG_DEBUG in ('Y', 'C') THEN
3264                    msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Inventory_Item_Name(i) '||l_atp_table.Inventory_Item_Name(i));
3265                END IF;
3266                --bug3709707 used original item id and name
3267                --l_atp_table.Inventory_item_id(i) := l_atp_rec.inventory_item_id;
3268                --l_atp_table.request_item_id(i) := l_atp_rec.request_item_id;
3269                --l_atp_table.request_item_name(i) := l_atp_rec.request_item_name;
3270                l_atp_table.request_item_id(i) := l_atp_rec.original_item_id;
3271                l_atp_table.request_item_name(i) := l_atp_rec.original_item_name;
3272 
3273                IF l_atp_table.req_item_detail_flag(i) = 1 THEN
3274                   l_atp_table.req_item_req_date_qty(i) := l_atp_rec.req_item_req_date_qty;
3275 
3276 		  -- Bug 2259824
3277 		  l_atp_table.req_item_available_date(i) := TRUNC(l_atp_rec.req_item_available_date) ;
3278 									--4460369+ MSC_ATP_PVT.G_END_OF_DAY;
3279                   IF PG_DEBUG in ('Y', 'C') THEN
3280                         msc_sch_wb.atp_debug('Schedule: 2259824 l_atp_table.req_item_available_date(i) = '||
3281                                 to_char(l_atp_table.req_item_available_date(i), 'DD-MON-YYYY HH24:MI:SS'));
3282                   END IF;
3283 
3284 		  l_atp_table.req_item_available_date_qty(i) := l_atp_rec.req_item_available_date_qty;
3285 		  --bug3709707 commenting as assigned above
3286 		  --l_atp_table.request_item_name(i) := l_atp_rec.request_item_name;
3287                   --l_atp_table.inventory_item_name(i) := l_atp_rec.inventory_item_name;
3288 
3289                END IF;
3290             ELSE
3291 
3292                -- 1219371
3293              /* Modularize Item and Org Info */
3294              IF (G_ITEM_INFO_REC.instance_id = p_instance_id) AND
3295                 (G_ITEM_INFO_REC.organization_id = l_atp_rec.organization_id) AND
3296                 (G_ITEM_INFO_REC.sr_inv_item_id = l_atp_table.inventory_item_id(i))
3297              THEN
3298                 -- If item has not changed then re-use
3299                 l_atp_table.Inventory_Item_Name(i) := NVL(G_ITEM_INFO_REC.item_name,
3300                                                 l_atp_table.Inventory_Item_Name(i)) ;
3301              ELSE
3302                 -- If item has changed then make the call.
3303                l_atp_table.Inventory_Item_Name(i) := NVL(
3304                                  MSC_ATP_FUNC.get_inv_item_name(p_instance_id,
3305                                                    --- bug 2152184
3306                                                    l_atp_table.inventory_item_id(i),
3307                                                    --l_requested_item_id, -- 1197692
3308                                                    l_atp_rec.organization_id),
3309                                  l_atp_table.Inventory_Item_Name(i)) ;
3310              END IF;
3311              /* Modularize Item and Org Info */
3312 		   IF PG_DEBUG in ('Y', 'C') THEN
3313 		      msc_sch_wb.atp_debug ('Schedule: ' || 'after item name');
3314 		   END IF;
3315             END IF; --- IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y'
3316             l_atp_table.Source_Organization_Id(i):=
3317                                 l_atp_rec.organization_id;
3318             IF PG_DEBUG in ('Y', 'C') THEN
3319                msc_sch_wb.atp_debug ('Schedule: ' || 'after source org id ');
3320             END IF;
3321 
3322             -- since we do a uom conversion, so we need to repopulate
3323             -- the primary uom and primary uom quantity back
3324 
3325             l_atp_table.Quantity_UOM(i) := l_atp_rec.quantity_uom;
3326             l_atp_table.Quantity_Ordered(i) := l_atp_rec.quantity_ordered;
3327 
3328             /* Modularize Item and Org Info */
3329             MSC_ATP_PROC.get_global_org_info (p_instance_id,
3330                                               l_atp_rec.organization_id);
3331             l_atp_table.Source_Organization_Code(i) := G_ORG_INFO_REC.org_code;
3332             /*l_atp_table.Source_Organization_Code(i):=
3333                                 MSC_ATP_FUNC.get_org_code(p_instance_id, l_atp_rec.organization_id);
3334              Modularize Item and Org Info */
3335             IF PG_DEBUG in ('Y', 'C') THEN
3336                msc_sch_wb.atp_debug ('Schedule: ' || 'after code');
3337             END IF;
3338 
3339             l_atp_table.Ship_Method(i) := l_atp_rec.ship_method;
3340             l_atp_table.Delivery_Lead_Time(i) := l_atp_rec.delivery_lead_time;
3341             -- Bug 2748730. Return the end of day as the timestamp for outgoing dates.
3342 	    -- For ship date and arrival date, this is the right place to move the date
3343 	    -- to the end of day.
3344 	    l_atp_table.Ship_Date(i) := l_atp_rec.ship_date;--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3345 	    l_atp_table.arrival_date(i) := l_atp_rec.arrival_date;--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
3346 
3347 	    l_atp_table.Available_Quantity(i):= l_atp_rec.available_quantity;
3348             l_atp_table.Requested_Date_Quantity(i) := l_atp_rec.requested_date_quantity;
3349 
3350             l_atp_table.Scenario_Id(i) := j;
3351             l_atp_table.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
3352 
3353             -- time_phased_atp
3354             l_atp_table.atf_date(i) := l_atp_rec.atf_date;
3355             l_atp_table.atp_flag(i) := l_atp_flag; --4460369
3356             l_atp_table.atp_components_flag(i) := l_atp_comp_flag; --4460369
3357 
3358             -- we may adjust the error code later based on latest accep date
3359 
3360             -- 1873918: PDS-ODS fix
3361             IF (l_atp_table.Error_Code(i) <> PDS_TO_ODS_SWITCH) OR
3362                (l_atp_rec.error_code <> ALLSUCCESS) THEN
3363               l_atp_table.Error_Code(i) := l_atp_rec.error_code;
3364             END IF;
3365 
3366             l_stmt := 240;
3367 
3368             /* ship_rec_cal commented the code as l_ship_date is not used
3369             -- bug 1609918 To retain the req_date if ship_date is less than
3370 	    -- req date so that time could be retained
3371 
3372             l_ship_date := NVL(l_atp_rec.requested_ship_date,
3373                                l_atp_rec.requested_arrival_date - nvl(l_atp_rec.delivery_lead_time,0));
3374 
3375             -- bug 1929645.  In case we have the following case,
3376             -- today D0, requested_ship_date D10 5pm is a non-working day
3377             -- and l_atp_rec.ship_date is D9 (previous working day) 0:00.
3378             -- we want to return ship date as D9 0:00 instead of D10 5pm.*/
3379 
3380 -- old bad code was here
3381             IF l_multi_scenario = 0 THEN
3382                 -- case 2
3383                 IF PG_DEBUG in ('Y', 'C') THEN
3384                    msc_sch_wb.atp_debug('Schedule: ' || 'In case 2: l_multi_scenario = 0 ');
3385                 END IF;
3386                 -- since case 2 is what oe wants, we can always set this
3387                 -- flag to 'Y'
3388                 l_atp_table.OE_Flag(i) := 'Y';
3389 
3390                 l_stmt := 250;
3391 
3392                 -- 1497368
3393                 IF l_ship_count > 1 THEN
3394                   -- ship set exists
3395 
3396                   IF i = 1 THEN
3397                   -- this is the begin of a set, we need to clean the
3398                   -- temp tables for details before we add data into them
3399                       l_curr_atp_period := l_null_atp_period;
3400                       l_curr_atp_supply_demand := l_null_atp_supply_demand;
3401                   END IF;
3402 
3403                   MSC_ATP_PROC.Details_Output(l_atp_period,
3404                                  l_atp_supply_demand,
3405                                  l_curr_atp_period,
3406                                  l_curr_atp_supply_demand,
3407                                  l_return_status);
3408 
3409                 END IF;
3410                 ---diag_atp
3411                 ---In diagnostic ATP we continue as if we are able to meet the demand on request date
3412                 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
3413                     IF PG_DEBUG in ('Y', 'C') THEN
3414                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset The error code for diag ATP mode');
3415                     END IF;
3416                     l_atp_rec.error_code := ALLSUCCESS;
3417                     --here we mark the constraint path for UI so that they can show the constraiend nodes
3418                     ---bug 27744106: Improve the query for performance
3419                     /*-- performance dsting merged queries, added distinct
3420                     update mrp_atp_details_temp
3421                     set    constrained_path = 1
3422                     where  record_type = 3
3423                     and    session_id = MSC_ATP_PVT.G_SESSION_ID
3424                     and    pegging_id in
3425                     (select distinct madt.pegging_id
3426                      from   mrp_atp_details_temp  madt
3427                      where  record_type = 3
3428                      and    session_id = MSC_ATP_PVT.G_SESSION_ID
3429                      start with madt.pegging_id in (
3430                         select pegging_id
3431                         from mrp_atp_details_temp madt1
3432                         where session_id = MSC_ATP_PVT.G_SESSION_ID
3433                         and   record_type = 3
3434                         and   end_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
3435                         and   constraint_type is not null)
3436                      connect by madt.pegging_id = PRIOR parent_pegging_id);
3437                      */
3438                      --3751114: replace hardcoded values with local varibales
3439                     --- so that they do not show up as literals
3440                     UPDATE mrp_atp_details_temp
3441                     set    constrained_path = l_constraint_path_flag
3442                     where  record_type = l_peg_record_type
3443                     and    session_id = MSC_ATP_PVT.G_SESSION_ID
3444                     and    pegging_id in
3445                        (select pegging_id
3446                         from   mrp_atp_details_temp
3447                         where  record_type = l_peg_record_type
3448                         and    session_id = MSC_ATP_PVT.G_SESSION_ID
3449                         start with session_id = MSC_ATP_PVT.G_SESSION_ID
3450                         and   record_type = l_peg_record_type
3451                         and   end_pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
3452                         and   constraint_type is not null
3453                         connect by pegging_id = PRIOR parent_pegging_id
3454                                 and record_type = l_peg_record_type
3455                                 and  session_id = MSC_ATP_PVT.G_SESSION_ID);
3456 
3457 
3458                 END IF;
3459 
3460                 ---bug 1819638: Compare ship_date with greatest of lat_acceptable_date and l_sys_next_date
3461 		IF PG_DEBUG in ('Y', 'C') THEN
3462 		   msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - Before IF l_atp_rec.error_code = ALLSUCCESS: ' ||l_latest_acceptable_date);
3463                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - error_code: ' ||l_atp_rec.error_code);
3464                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_latest_acceptable_date:  ' ||l_latest_acceptable_date);
3465                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_atp_rec.override_flag: ' || l_atp_rec.override_flag);
3466                    msc_sch_wb.atp_debug('Schedule: ' || 'stmt16 - l_atp_rec.action: ' || l_atp_rec.action);
3467                 END IF;
3468 
3469                 -- dsting setproc
3470                 l_atp_table.instance_id(i) := p_instance_id;
3471                 IF NVL(l_atp_table.override_flag(i), 'N') = 'Y' THEN
3472 
3473                    /* ship_rec_cal do we need this???
3474                    l_atp_table.ship_date(i) := TRUNC(l_atp_table.requested_ship_date(i)) + MSC_ATP_PVT.G_END_OF_DAY;
3475 
3476                    IF l_atp_table.ship_date(i) IS NULL THEN
3477                       l_atp_table.ship_date(i) := TRUNC(MSC_CALENDAR.prev_work_day(
3478                                                 l_atp_table.source_organization_id(i),
3479                                                 p_instance_id,
3480                                                 MSC_CALENDAR.TYPE_DAILY_BUCKET,
3481                                                 l_atp_table.requested_arrival_date(i)
3482                                                 - nvl(l_atp_table.delivery_lead_time(i), 0)
3483                                             )) + MSC_ATP_PVT.G_END_OF_DAY;
3484                    END IF;
3485 
3486                    l_atp_table.arrival_date(i) := TRUNC(l_atp_table.requested_arrival_date(i))
3487                                                 + MSC_ATP_PVT.G_END_OF_DAY;
3488                    IF l_atp_table.arrival_date(i) IS NULL THEN
3489                       -- dsting 2815549
3490                       IF l_atp_table.customer_id(i) is null and l_atp_table.organization_id(i) is not null THEN
3491                          l_atp_table.arrival_date(i) := TRUNC(MSC_CALENDAR.next_work_day(
3492                                                    l_atp_table.organization_id(i),
3493                                                    p_instance_id,
3494                                                    MSC_CALENDAR.TYPE_DAILY_BUCKET,
3495                                                    l_atp_table.requested_ship_date(i)
3496                                                    + nvl(l_atp_table.delivery_lead_time(i), 0)
3497                                                )) + MSC_ATP_PVT.G_END_OF_DAY;
3498                       ELSE
3499                          l_atp_table.arrival_date(i) := TRUNC(l_atp_table.requested_ship_date(i)
3500                             + nvl(l_atp_table.delivery_lead_time(i), 0)) + MSC_ATP_PVT.G_END_OF_DAY;
3501                       END IF;
3502                    END IF;*/
3503 
3504                    IF PG_DEBUG in ('Y', 'C') THEN
3505                       msc_sch_wb.atp_debug('override ship_date: ' || l_atp_table.ship_date(i));
3506                       msc_sch_wb.atp_debug('override arrival_date: ' || l_atp_table.arrival_date(i));
3507                    END IF;
3508                 ELSE
3509                    -- xxx dsting hack to make sure things don't ship before sysdate
3510                    -- for an overridden ship set with req_arrival_date
3511                    /* ship_rec_cal
3512                    l_atp_table.earliest_acceptable_date(i) := l_sys_next_date;*/
3513                    l_atp_table.earliest_acceptable_date(i) := MSC_CALENDAR.NEXT_WORK_DAY(
3514                                                                         l_shipping_cal_code,
3515                                                                         p_instance_id,
3516                                                                         sysdate);
3517                 END IF; -- override
3518 
3519                 IF PG_DEBUG in ('Y', 'C') THEN
3520                    msc_sch_wb.atp_debug('ship_date: ' || l_atp_table.ship_date(i));
3521                    msc_sch_wb.atp_debug('arrival_date: ' || l_atp_table.arrival_date(i));
3522                 END IF;
3523                 --bug3439591 start
3524                 --L_MOVE_PAST_DUE_TO_SYSDATE := NVL(FND_PROFILE.value('MSC_MOVE_PAST_DUE_TO_SYSDATE'), 'Y'); -- Bug 5584634/5618929
3525                 L_MOVE_PAST_DUE_TO_SYSDATE := MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF;--6316476
3526                 IF PG_DEBUG in ('Y', 'C') THEN
3527                    msc_sch_wb.atp_debug('Schedule: ' || 'MOVE_PAST_DUE_TO_SYS_DATE :'|| L_MOVE_PAST_DUE_TO_SYSDATE);
3528                 END IF;
3529                 if L_MOVE_PAST_DUE_TO_SYSDATE = 'Y' THEN  -- Bug 5584634/5618929
3530                 IF   l_atp_table.requested_ship_date(i) IS NOT NULL THEN
3531                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
3532                                                                 l_atp_table.requested_ship_date(i)),l_atp_table.requested_ship_date(i),
3533                                                                 l_sys_next_date);
3534                 ELSE
3535                    IF l_past_due_ship_date = 2 THEN
3536                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
3537                                                                 l_atp_table.requested_arrival_date(i)),l_atp_table.requested_arrival_date(i),
3538                                                                 l_sysdate_orc);
3539                    ELSE --bug4291375 If requested ship date is past due date then LAD needs to be offseted by the lead time.
3540                      -- Calculate date after offset lead time
3541                      l_sysdate_orc_new := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
3542                                                 l_trunc_sysdate, l_atp_rec.shipping_cal_code, 1,
3543                                                 l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), 1,
3544                                                 l_atp_rec.receiving_cal_code, 1, p_instance_id);
3545                      -- Calculate LAD after lead time taken in consideration
3546                      l_atp_table.latest_acceptable_date(i) := GREATEST(NVL(l_atp_table.latest_acceptable_date(i),
3547                                                                 l_atp_table.requested_arrival_date(i)),l_atp_table.requested_arrival_date(i),
3548                                                                 l_sysdate_orc_new);
3549                    END IF;
3550                 END IF;
3551 								END IF;
3552 
3553                 IF PG_DEBUG in ('Y', 'C') THEN
3554                    msc_sch_wb.atp_debug('LAD: ' || l_atp_table.latest_acceptable_date(i));
3555                    msc_sch_wb.atp_debug('l_sysdate_orc: ' || l_sysdate_orc);
3556                    msc_sch_wb.atp_debug('l_sysdate_osc: ' || l_sys_next_date);
3557                    msc_sch_wb.atp_debug('l_sysdate_orc_new: ' || l_sysdate_orc_new); --bug4291375
3558                    msc_sch_wb.atp_debug('l_past_due_ship_date : ' || l_past_due_ship_date); --bug4291375
3559                    msc_sch_wb.atp_debug('requested_arrival_date: ' || l_atp_table.requested_arrival_date(i));
3560                    msc_sch_wb.atp_debug('requested_ship_date: ' || l_atp_table.requested_ship_date(i));
3561                 END IF;
3562                 --bug3439591 end
3563                 -- dsting setproc
3564                 MSC_ATP_PROC.Process_Set_Line(l_atp_table, i, l_line_status);
3565                 Process_Time_Stamp_Errors(l_atp_table,i);
3566                 msc_sch_wb.atp_debug('ship_date:(After) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
3567                 msc_sch_wb.atp_debug('Arrival_date:(After) ' ||  to_char(l_atp_table.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));
3568                 IF l_ship_count = 1 and l_arrival_count = 1 THEN
3569                    l_set_status := l_line_status;
3570                 END iF;
3571                 -- 2902129
3572                 IF l_arrival_count > 1 THEN
3573                    l_overall_status := l_line_status;
3574                 END IF;
3575                 IF l_line_status = ALLSUCCESS THEN
3576                   l_stmt := 260;
3577                   IF PG_DEBUG in ('Y', 'C') THEN
3578                      msc_sch_wb.atp_debug('Schedule: ' || 'inside the range in case 2');
3579                   END IF;
3580 
3581                   -- we are inside the latest acceptable range
3582                   -- remember, l_atp_rec.error_code = ALLSUCCESS means
3583                   -- we can satisfy the request on the request date.
3584 
3585                   -- if it is just an inquiry, we need to rollback
3586 
3587                   -- 2266808: krajan : 04/16
3588 
3589                   --IF (l_atp_rec.Action = ATPQUERY) THEN
3590                     -- 1225631: we only undo when
3591                     -- 1: the ship set is done (ship set exists) or
3592                     -- 2: the arrival set is done (arrival set exists) or
3593                     -- 3: the single line is done (no set exists)
3594 
3595                   IF (l_atp_rec.Action = ATPQUERY) and
3596                       (
3597                       -- 2902265 handle arrival set later
3598                       --(l_arrival_count > 1 AND k = l_arrival_count) OR
3599                       (l_ship_count > 1 AND i = l_ship_count) OR
3600                       (l_ship_count = 1 AND l_arrival_count = 1)) THEN
3601 
3602                     IF PG_DEBUG in ('Y', 'C') THEN
3603                        msc_sch_wb.atp_debug('Schedule: ' || 'bug 1225631');
3604                     END IF;
3605                      --3720018 , Global array MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC is used to append records
3606                      -- in case of ATP Inquiry. This global array will be used to call remove_invalid_sd_rec
3607                      -- From request level (Call_schedule)
3608                     IF MSC_ATP_PVT.G_PEGGING_FOR_SET IS NOT NULL and MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT > 0 THEN
3609                        l_count := MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.count;
3610                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
3611                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
3612                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
3613                        FOR m in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
3614                           IF PG_DEBUG in ('Y', 'C') THEN
3615                               msc_sch_wb.atp_debug('Schedule: ' || MSC_ATP_PVT.G_PEGGING_FOR_SET(m));
3616                           END IF;
3617                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
3618                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(l_count + m) := l_plan_id;
3619                           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
3620                        END LOOP;
3621                     END IF;
3622 
3623                     /* --3720018, donot call remove_invalid_sd_rec here
3624                      FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
3625                       IF PG_DEBUG in ('Y', 'C') THEN
3626                          msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
3627                       END IF;
3628                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
3629   			MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
3630                         p_instance_id,
3631                         l_plan_id,
3632                         UNDO,
3633                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
3634   			l_return_status);
3635 
3636                     END LOOP; */ --3720018
3637 
3638                   ELSE
3639                 -- 2266808: krajan : 04/16
3640 
3641                     IF l_atp_rec.error_code = ATP_REQ_DATE_FAIL THEN
3642                       l_sd_qty := l_atp_rec.quantity_ordered;
3643                       IF PG_DEBUG in ('Y', 'C') THEN
3644                          msc_sch_wb.atp_debug('Schedule: ' || 'i := ' || i);
3645                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_REQ_DATE_QTY(i) := ' || MSC_ATP_PVT.G_REQ_DATE_QTY(i));
3646                          msc_sch_wb.atp_debug('Schedule: ' || 'MSC_ATP_PVT.G_REQ_ATP_DATE(i) := ' || MSC_ATP_PVT.G_REQ_ATP_DATE(i));
3647                       END IF;
3648                       -- rajjain begin 01/30/2003 Bug 2777661
3649                       -- bug 2795053-reopen (ssurendr): Do date offset for override case as well
3650                       -- IF NVL(l_atp_rec.override_flag, 'N') = 'Y' THEN
3651                       --    l_sd_date := l_atp_rec.ship_date;
3652                       -- ELSE
3653                       l_sd_date := l_atp_rec.ship_date;
3654                       /* s_cto_rearch: do not honor atp lead time
3655                          l_sd_date := MSC_CALENDAR.DATE_OFFSET(
3656                                                    l_atp_rec.organization_id,
3657                                                    p_instance_id,
3658                                                    1,
3659                                                    l_atp_rec.ship_date,
3660                                                    -NVL(l_atp_rec.atp_lead_time, 0));
3661                       e_cto_rearch*/
3662 
3663                       /* ship_rec_cal Determine order date type*/
3664                       IF l_atp_table.requested_arrival_date(i) is not null THEN
3665                             l_order_date_type := 2;
3666                       ELSE
3667                             l_order_date_type := 1;
3668                       END IF;
3669 
3670 	              IF PG_DEBUG in ('Y', 'C') THEN
3671 	                   msc_sch_wb.atp_debug('Schedule: ' || 'l_order_date_type : ' || l_order_date_type);
3672 	              END IF;
3673 
3674                       --Bug 3226083
3675                       l_atp_insert_rec := l_atp_rec;
3676                       l_atp_insert_rec.inventory_item_id := l_pf_dest_id;
3677                       l_atp_insert_rec.request_item_id := l_mem_dest_id;
3678                       l_atp_insert_rec.origination_type := 30;
3679 
3680                       MSC_ATP_DB_UTILS.Update_SD_Date(l_atp_rec.demand_source_line ,
3681                              p_instance_id,
3682                              l_sd_date,
3683                              l_plan_id,
3684                              l_sd_qty,
3685                              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
3686                              MSC_ATP_PVT.G_REQ_ATP_DATE(i),
3687                              MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
3688                              l_dmd_satisfied_date, -- bug 2795053-reopen (ssurendr)
3689                              l_atp_rec.used_available_quantity,  -- For time_phased_atp bug3409973
3690                              l_atp_rec.atf_date,            -- For time_phased_atp
3691                              l_atp_rec.atf_date_quantity,   -- For time_phased_atp
3692 			     l_atp_rec.arrival_date,            -- For ship_rec_cal
3693                              l_order_date_type,                 -- For ship_rec_cal
3694                              l_atp_rec.latest_acceptable_date,  -- For ship_rec_cal
3695                              l_atp_rec.ship_set_name,           -- For Plan by Request Date
3696                              l_atp_rec.arrival_set_name,        -- For Plan by Request Date
3697                              l_atp_rec.override_flag,           -- For Plan by Request Date
3698                              l_original_req_arrival_date,       -- For Plan by Request Date
3699                              l_bkwd_pass_atf_date_qty,            -- For time_phased_atp bug3397904
3700                              l_atp_insert_rec                  -- For bug 3226083
3701                              );
3702 
3703                     END IF;
3704                   END IF;
3705                   l_stmt := 270;
3706 
3707                   -- 1873918: PDS-ODS fix
3708 
3709                   IF ((l_atp_table.Action(i) = ATPQUERY) AND
3710                      ((l_atp_table.Error_Code(i) = ATP_NOT_APPL) OR
3711                      (l_atp_table.Error_Code(i) = PDS_TO_ODS_SWITCH))) THEN
3712                     IF PG_DEBUG in ('Y', 'C') THEN
3713                        msc_sch_wb.atp_debug('Schedule: ' || 'l_atp_table.Error_Code(i)='||l_atp_table.Error_Code(i));
3714                     END IF;
3715                   ELSE
3716                     l_atp_table.Error_Code(i) := ALLSUCCESS;
3717                   END IF;
3718 
3719                   l_stmt := 280;
3720 
3721                   -- since we are successful,increase l_set_element_success by 1
3722 
3723                   l_set_element_success := l_set_element_success + 1;
3724 
3725                   -- need to do something for the set if we are done
3726                   -- with this set
3727                   IF PG_DEBUG in ('Y', 'C') THEN
3728                      msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count := ' ||l_ship_count);
3729                      msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt := ' || l_stmt);
3730                      msc_sch_wb.atp_debug('Schedule: ' || 'stmt - l_set_element_success := ' || l_set_element_success);
3731                   END IF;
3732                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
3733 
3734                       l_stmt := 290;
3735                       IF PG_DEBUG in ('Y', 'C') THEN
3736                          msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt:= ' || l_stmt);
3737                       END IF;
3738 
3739                       -- this is a set, make sure this set is successful
3740 
3741                       -- dsting setproc
3742                       MSC_ATP_PROC.Process_Set_Dates_Errors(l_atp_table, 'D', l_set_status);
3743                       -- 2902265
3744                       l_overall_status := l_set_status;
3745                       l_group_date := l_atp_table.group_ship_date(1);
3746                       IF l_set_status = ALLSUCCESS THEN
3747 --                      IF l_set_element_success  = l_atp_table.Action.COUNT AND
3748 
3749                         l_stmt := 300;
3750 
3751 			IF PG_DEBUG in ('Y', 'C') THEN
3752 			   msc_sch_wb.atp_debug('Schedule: ' || 'stmt17');
3753                         END IF;
3754 
3755                         MSC_ATP_PROC.Update_Set_SD_Dates(l_atp_table,l_req_arr_date);
3756                       END IF;
3757                       l_stmt := 320;
3758                   END IF;
3759 
3760                   /* ship_rec_cal changes begin
3761                      flush sch arrival date, lat acceptable date, order date type in pds for single line */
3762                   IF (i = 1) AND (l_ship_count = 1 ) THEN
3763                         l_ship_arrival_date_rec.scheduled_arrival_date := l_atp_table.arrival_date(1);
3764                         l_ship_arrival_date_rec.latest_acceptable_date := l_atp_table.latest_acceptable_date(1);
3765                         l_ship_arrival_date_rec.arrival_set_name       := l_atp_table.arrival_set_name(1);
3766                         l_ship_arrival_date_rec.ship_set_name          := l_atp_table.ship_set_name(1);
3767                         l_ship_arrival_date_rec.atp_override_flag      := l_atp_table.override_flag(1);
3768                         l_ship_arrival_date_rec.request_arrival_date   := l_original_req_arrival_date;
3769                         l_ship_arrival_date_rec.instance_id := p_instance_id;
3770                         l_ship_arrival_date_rec.plan_id := l_plan_id;
3771 
3772                         /* Read demand_id from madt*/
3773                         BEGIN
3774                             SELECT identifier3
3775                             INTO   l_ship_arrival_date_rec.demand_id
3776                             FROM   mrp_atp_details_temp
3777                             WHERE  pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
3778                             AND    session_id = MSC_ATP_PVT.G_SESSION_ID
3779                             AND    record_type = 3;
3780                         EXCEPTION
3781                             WHEN OTHERS THEN
3782                                 l_ship_arrival_date_rec.demand_id := null;
3783                         END;
3784                         IF PG_DEBUG in ('Y', 'C') THEN
3785                             msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_arrival_date_rec.demand_id = ' || l_ship_arrival_date_rec.demand_id);
3786                         END IF;
3787 
3788                         /* Determine order date type*/
3789                         IF l_atp_table.requested_arrival_date(i) is not null THEN
3790                             l_ship_arrival_date_rec.order_date_type := 2;
3791                         ELSE
3792                             l_ship_arrival_date_rec.order_date_type := 1;
3793                         END IF;
3794                         IF l_ship_arrival_date_rec.demand_id is not null THEN
3795 	                        IF (G_INV_CTP = 4)THEN
3796 	                        	MSC_ATP_DB_UTILS.Flush_Data_In_Pds(l_ship_arrival_date_rec, l_return_status);
3797 	                        	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3798 	                        		IF PG_DEBUG in ('Y', 'C') THEN
3799 	                                        msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Pds procedure ');
3800 	                        		END IF;
3801 	                        	END IF;
3802 	                        ELSE
3803 	                        	MSC_ATP_DB_UTILS.Flush_Data_In_Ods(l_ship_arrival_date_rec, l_return_status);
3804 	                        	IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3805 	                        		IF PG_DEBUG in ('Y', 'C') THEN
3806 	                                        msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Ods procedure ');
3807 	                        		END IF;
3808 	                        	END IF;
3809 	                        END IF;
3810                         END IF;
3811 
3812                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3813                                 IF PG_DEBUG in ('Y', 'C') THEN
3814                                         msc_sch_wb.atp_debug('Schedule: ' || 'Error in call to Flush_Data_In_Pds procedure ');
3815                                 END IF;
3816                                 RAISE FND_API.G_EXC_ERROR;
3817                         END IF;
3818                   END IF;
3819                   /* ship_rec_cal changes end */
3820 
3821                   l_stmt := 330;
3822                   -- dsting setproc
3823                   IF i = l_atp_table.action.count AND l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
3824 --                  IF l_set_element_success  = l_atp_table.Action.COUNT THEN
3825                      l_overall_status := ALLSUCCESS ;
3826                   END IF;
3827 
3828                   l_stmt := 360;
3829                   IF PG_DEBUG in ('Y', 'C') THEN
3830                      msc_sch_wb.atp_debug('Schedule: ' || 'l_stmt := ' || l_stmt);
3831                   END IF;
3832 
3833                   -- Added by NGOEL for BUG 1533251, in case ship_date is <= requested_date
3834                   -- and G_FIND_FUTURE = Y, copy available quantity into request_date quantity.
3835                   -- This will take care of the situation in case we were able to find some
3836                   -- additional quantity in forward case but still meet the request date.
3837 
3838                   IF (((l_atp_rec.error_code = ATP_REQ_DATE_FAIL) AND
3839                      (l_atp_rec.requested_ship_date IS NOT NULL) AND
3840                      (l_atp_rec.ship_date <= l_atp_rec.requested_ship_date))
3841                     OR
3842                      ((l_atp_rec.error_code = ATP_REQ_DATE_FAIL) AND
3843                      (l_atp_rec.requested_arrival_date IS NOT NULL) AND
3844                      (l_atp_rec.ship_date+NVL(l_atp_rec.delivery_lead_time,0)
3845                         <= l_atp_rec.requested_arrival_date))) THEN
3846                         IF PG_DEBUG in ('Y', 'C') THEN
3847                            msc_sch_wb.atp_debug ('Schedule: ' || 'Bug 1533251');
3848                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.requested_date_quantity : '||
3849                                 l_atp_rec.requested_date_quantity);
3850                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.combined_requested_date_qty : '||
3851                                 l_atp_rec.combined_requested_date_qty);
3852                            msc_sch_wb.atp_debug ('Schedule: ' || 'l_atp_rec.available_quantity : '||
3853                                 l_atp_rec.available_quantity);
3854                         END IF;
3855 
3856 			l_atp_rec.requested_date_quantity := l_atp_rec.available_quantity;
3857                   END IF;
3858                   -- End of New Code
3859 
3860                   --diag_atp
3861                   IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
3862                     IF PG_DEBUG in ('Y', 'C') THEN
3863                        msc_sch_wb.atp_debug('Schedule: ' || 'Reset The error code for diag ATP mode 2');
3864                     END IF;
3865                     l_atp_table.Error_Code(i) := MSC_ATP_PVT.DIAGNOSTIC_ATP_ENABLED;
3866                   END IF;
3867 		ELSE --- bug 1104565
3868 
3869 		  -- The request_date fails
3870 		  IF PG_DEBUG in ('Y', 'C') THEN
3871 		     msc_sch_wb.atp_debug('Schedule: ' || 'Date requirements fail vivek');
3872 		  END IF;
3873                   -- Bug 2266808 : krajan
3874                   -- dsting setproc
3875 	          IF l_ship_count > 1 AND i = l_ship_count THEN
3876                      MSC_ATP_PROC.Process_Set_Dates_Errors(l_atp_table, 'D', l_set_status);
3877                      l_group_date := l_atp_table.group_ship_date(1);
3878                   END IF;
3879 
3880 	          --IF i <> l_ship_count OR (l_atp_table.Action(i) = ATPQUERY AND l_ship_count =1)
3881 	          --OR i = l_ship_count THEN --3720018 (removed the IF condition, needto be executed always)
3882                      ---- this part will be executed for ship set only
3883                      ---- this part will be executed for ship set only
3884 	             ---- the ship set is not complete as yet
3885                      ----- we update the S/D date and quantity
3886                      -- 1225631
3887                      -- in order to keep the demand picture correctly between
3888                      -- the items inside a ship/arrival set,
3889                      -- we need to move the demand date for this set item
3890                      -- to the ship date
3891 
3892                      -- 1580127
3893 		     IF (l_atp_rec.error_code = ATP_REQ_DATE_FAIL) THEN
3894 
3895 	                IF PG_DEBUG in ('Y', 'C') THEN
3896 	                   msc_sch_wb.atp_debug('Schedule: ' || 'Inside ATP_REQ_DATE_FAIL, i : ' || i);
3897 	                   msc_sch_wb.atp_debug('Schedule: ' || 'quantity_ordered : ' || l_atp_rec.quantity_ordered);
3898 	                   msc_sch_wb.atp_debug('Schedule: ' || 'combined qty : ' || l_atp_rec.combined_requested_date_qty);
3899 	                   msc_sch_wb.atp_debug('Schedule: ' || 'order line : ' || l_atp_rec.demand_source_line);
3900 	                END IF;
3901 
3902                         l_sd_qty := l_atp_rec.quantity_ordered;
3903 	                IF PG_DEBUG in ('Y', 'C') THEN
3904 	                   msc_sch_wb.atp_debug('Schedule: ' || 'quantity : ' || l_sd_qty);
3905 	                END IF;
3906 
3907 			/* ship_rec_cal Determine order date type*/
3908                         IF l_atp_table.requested_arrival_date(i) is not null THEN
3909                              l_order_date_type := 2;
3910                         ELSE
3911                             l_order_date_type := 1;
3912                         END IF;
3913 	                IF PG_DEBUG in ('Y', 'C') THEN
3914 	                   msc_sch_wb.atp_debug('Schedule: ' || 'l_order_date_type : ' || l_order_date_type);
3915 	                END IF;
3916 
3917                         --Bug 3226083
3918                         l_atp_insert_rec := l_atp_rec;
3919                         l_atp_insert_rec.inventory_item_id := l_pf_dest_id;
3920                         l_atp_insert_rec.request_item_id := l_mem_dest_id;
3921                         l_atp_insert_rec.origination_type := 30;
3922 
3923                         MSC_ATP_DB_UTILS.Update_SD_Date(l_atp_rec.demand_source_line ,
3924                              p_instance_id,
3925                              l_atp_rec.ship_date,
3926                              --s_cto_rearch do not honor atp_lead time
3927                              /*
3928                              MSC_CALENDAR.DATE_OFFSET(
3929                              l_atp_rec.organization_id,
3930                              p_instance_id,
3931                              1,
3932                              l_atp_rec.ship_date,
3933                              -NVL(l_atp_rec.atp_lead_time, 0)),
3934                              */
3935                              l_plan_id,
3936                              l_sd_qty,
3937                               MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
3938                               MSC_ATP_PVT.G_REQ_ATP_DATE(i),
3939                               MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
3940                               l_dmd_satisfied_date, -- bug 2795053-reopen (ssurendr)
3941                               l_atp_rec.used_available_quantity,  -- For time_phased_atp bug3409973
3942                               l_atp_rec.atf_date,            -- For time_phased_atp
3943                               l_atp_rec.atf_date_quantity, -- For time_phased_atp
3944                               l_atp_rec.arrival_date,            -- For ship_rec_cal
3945                               l_order_date_type,                 -- For ship_rec_cal
3946                               l_atp_rec.latest_acceptable_date,
3947                               l_atp_rec.ship_set_name,
3948                               l_atp_rec.arrival_set_name,
3949                               l_atp_rec.override_flag,
3950                               l_original_req_arrival_date,
3951                               l_bkwd_pass_atf_date_qty,             -- For time_phased_atp bug3397904
3952                               l_atp_insert_rec                   -- For bug 3226083
3953                               );  -- For ship_rec_cal
3954 	             END IF;
3955 		  --END IF; ---if i = l_ship_count
3956                    --- Bug 1104565
3957                   --IF j = l_sources.source_type.count THEN
3958                      --- ADD the pegging id for the best source
3959                   --   FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
3960                   --      l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(l_peg_count);
3961                   --   END LOOP;
3962                   --END IF;
3963 
3964 --		  IF (l_atp_rec.error_code = ATP_REQ_DATE_FAIL) THEN
3965 		  IF (l_atp_table.error_code(i) = ATP_REQ_DATE_FAIL) THEN
3966 		     IF PG_DEBUG in ('Y', 'C') THEN
3967 		        msc_sch_wb.atp_debug('Schedule: ' || 'in case 2, not in acceptable range');
3968 		     END IF;
3969                      l_atp_table.Error_Code(i) := ATP_ACCEPT_FAIL;
3970 
3971                   END IF;
3972 	        END IF; --- IF l_atp_rec.error_code = "ALLSUCCESS"
3973 
3974                      IF PG_DEBUG in ('Y', 'C') THEN
3975                         msc_sch_wb.atp_debug('arrival cntr k: ' || k);
3976                         msc_sch_wb.atp_debug('fst src peg count: ' || l_fst_src_pegging_ids.count);
3977                         msc_sch_wb.atp_debug('i: ' || i);
3978                         msc_sch_wb.atp_debug('p cnt: ' ||MSC_ATP_PVT.G_pegging_for_set.count);
3979                         msc_sch_wb.atp_debug('f cnt: ' ||MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count);
3980                         msc_sch_wb.atp_debug('d cnt: ' ||MSC_ATP_PVT.G_REQ_ATP_DATE.count);
3981                         msc_sch_wb.atp_debug('q cnt: ' ||MSC_ATP_PVT.G_REQ_DATE_QTY.count);
3982                         msc_sch_wb.atp_debug('l_date_higher_src: ' || l_date_higher_src);
3983                         msc_sch_wb.atp_debug('l_overall_status: ' || l_overall_status);
3984                      END IF;
3985 
3986                      IF i = l_ship_count THEN
3987 
3988                         IF j=1 or
3989                            l_overall_status = ALLSUCCESS or
3990                            (l_date_higher_src = 'N' and l_overall_status
3991                             in (ATP_REQ_DATE_FAIL, ATP_ACCEPT_FAIL, GROUPEL_ERROR))
3992                         THEN
3993                            IF PG_DEBUG in ('Y', 'C') THEN
3994                               msc_sch_wb.atp_debug('used src for fst_src_peg');
3995                            END IF;
3996 
3997                            IF j > 1 THEN
3998                               FOR l_peg_count in k..l_fst_src_pegging_ids.count loop
3999                                  MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4000                                                 l_fst_src_pegging_ids(l_peg_count),
4001                                                 null,
4002                                                 l_plan_id,
4003                                                 UNDO,
4004                                                 l_DEMAND_CLASS_ATP_FLAG(l_peg_count),
4005                                                 l_return_status);
4006                               END LOOP;
4007 
4008                               l_fst_src_pegging_ids.trim(l_fst_src_pegging_ids.count - k + 1);
4009                               l_req_atp_date.trim(l_fst_src_pegging_ids.count - k + 1);
4010                               l_req_date_qty.trim(l_fst_src_pegging_ids.count - k + 1);
4011                               l_demand_class_atp_flag.trim(l_fst_src_pegging_ids.count - k + 1);
4012                            END IF;
4013 
4014                            msc_sch_wb.atp_debug('adding to fst_src_peg p');
4015                            MSC_ATP_PROC.number_arr_cat(l_fst_src_pegging_ids, MSC_ATP_PVT.G_PEGGING_FOR_SET);
4016                            msc_sch_wb.atp_debug('adding to fst_src_peg d');
4017                            MSC_ATP_PROC.date_arr_cat(l_req_atp_date, MSC_ATP_PVT.G_REQ_ATP_DATE);
4018                            msc_sch_wb.atp_debug('adding to fst_src_peg q');
4019                            MSC_ATP_PROC.number_arr_cat(l_req_date_qty, MSC_ATP_PVT.G_REQ_DATE_QTY);
4020                            msc_sch_wb.atp_debug('adding to fst_src_peg f');
4021                            MSC_ATP_PROC.number_arr_cat(l_demand_class_atp_flag, MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4022 
4023                            IF l_overall_status in (MSC_ATP_PVT.ALLSUCCESS,
4024                                                    MSC_ATP_PVT.ATP_REQ_DATE_FAIL,
4025                                                    MSC_ATP_PVT.GROUPEL_ERROR,
4026                                                    MSC_ATP_PVT.ATP_ACCEPT_FAIL)
4027                            THEN
4028                               l_date_higher_src := 'Y';
4029                               msc_sch_wb.atp_debug('have date. l_date_higher_src: ' || l_date_higher_src);
4030                            END IF;
4031                            -- cleanup only if not arrival set
4032                            --3720018 , Global array MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC is used to append records
4033                            -- in case of ATP Inquiry. This global array will be used to call remove_invalid_sd_rec
4034                            -- From request level (Call_schedule)
4035                            IF l_set_status <> ALLSUCCESS and l_arrival_count = 1 THEN
4036                               IF (l_atp_rec.Action = ATPQUERY AND l_ship_count =1) THEN
4037                                  IF MSC_ATP_PVT.G_PEGGING_FOR_SET IS NOT NULL and MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT > 0 THEN
4038                                     l_count := MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.count;
4039                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4040                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4041                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST.extend(MSC_ATP_PVT.G_PEGGING_FOR_SET.count);
4042                                     FOR m in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4043                                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(l_count + m) := MSC_ATP_PVT.G_PEGGING_FOR_SET(m);
4044                                        MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(l_count + m) := l_plan_id;
4045                                        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);
4046                                     END LOOP;
4047                                  END IF;
4048                               ELSE  --3720018
4049                                 MSC_ATP_PROC.cleanup_set(p_instance_id,
4050                                                        l_plan_id,
4051                                                        MSC_ATP_PVT.G_PEGGING_FOR_SET,
4052                                                        MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4053                               END IF; --3720018
4054                            END IF; --3720018
4055                         ELSE
4056                            -- not using these in fst src peg, so always clean up
4057                            MSC_ATP_PROC.cleanup_set(p_instance_id,
4058                                                     l_plan_id,
4059                                                     MSC_ATP_PVT.G_PEGGING_FOR_SET,
4060                                                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG);
4061                         END IF;
4062 
4063                         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4064                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4065                         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4066                         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4067 
4068                         IF j =  l_sources.source_type.count THEN
4069                           IF PG_DEBUG in ('Y', 'C') THEN
4070                              msc_sch_wb.atp_debug('Schedule: ' || 'l_fst_src_pegging_ids.count'|| l_fst_src_pegging_ids.count);
4071                           END IF;
4072 
4073                            -- dsting 2741997
4074                           IF l_ship_count > 1 THEN
4075                              FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
4076                                 l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(l_peg_count);
4077 
4078                              END LOOP;
4079                           ELSE
4080                                 l_atp_table.end_pegging_id(1) := l_fst_src_pegging_ids(l_fst_src_pegging_ids.count);
4081                           END IF;
4082                         END IF;
4083                      END IF;
4084 --                END IF; -- 2902265 set <> success thing
4085 
4086                 -- bug 2266808 code changes end
4087             ELSE
4088                 -- dsting case 1 => multi_scenario = 1.
4089                 -- multi_scenario is always 0 since complete_scatp is disabled
4090                 -- case 1
4091                 IF PG_DEBUG in ('Y', 'C') THEN
4092                    msc_sch_wb.atp_debug('Schedule: ' || 'In case 1');
4093                 END IF;
4094                 IF ((l_atp_rec.error_code = ALLSUCCESS) AND
4095                     (l_sources.Preferred(j) = 1))
4096                   OR (l_atp_rec.error_code = ATP_NOT_APPL) THEN
4097 
4098                   IF (l_atp_rec.Action = ATPQUERY) THEN
4099                     -- 1225631: we only undo when
4100                     -- 1: the ship set is done (ship set exists) or
4101                     -- 2: the arrival set is done (arrival set exists) or
4102                     -- 3: the single line is done (no set exists)
4103                    IF (l_arrival_count > 1 AND k = l_arrival_count) OR
4104                       (l_ship_count > 1 AND i = l_ship_count) OR
4105                       (l_ship_count = 1 AND l_arrival_count = 1) THEN
4106                       IF PG_DEBUG in ('Y', 'C') THEN
4107                          msc_sch_wb.atp_debug('Schedule: ' || 'bug 1225631');
4108                       END IF;
4109 
4110 --                      ROLLBACK to SAVEPOINT start_atp;
4111                       FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4112                         IF PG_DEBUG in ('Y', 'C') THEN
4113                            msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4114                         END IF;
4115                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4116                           MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4117                           p_instance_id,
4118                           l_plan_id,
4119                           UNDO,
4120                           MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4121                           l_return_status);
4122 
4123                       END LOOP;
4124 
4125                       MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4126                       MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4127                       MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4128                       MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4129                        IF PG_DEBUG in ('Y', 'C') THEN
4130                           msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 7');
4131                        END IF;
4132 
4133                    END IF;
4134                   END IF;
4135 
4136                   -- since we are successful,increase l_set_element_success by 1
4137 
4138                   l_set_element_success := l_set_element_success + 1;
4139 
4140                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
4141                       -- this is a set, make sure this set is successful
4142                         -- dsting setproc
4143 --                      IF l_set_element_success  = l_atp_table.Action.COUNT THEN
4144                         IF l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
4145                         FOR m in 1.. l_ship_count LOOP
4146                           l_atp_table.Group_Ship_Date(m):= l_group_date;
4147 
4148                           IF (l_atp_table.Action(m)<> ATPQUERY) THEN
4149 
4150                             IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
4151                                 /* time_phased_atp use plan_id populated in l_atp_table.plan_id
4152                                 -- New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
4153                                 -- (SCLT)
4154                                 MSC_ATP_PROC.get_global_plan_info(p_instance_id,
4155                                             l_atp_table.inventory_item_id(m),
4156                                             l_atp_table.organization_id(m),
4157                                             l_atp_table.demand_class(m));
4158 
4159                                 l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
4160                                 -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
4161 
4162                                 l_temp_plan_id          := l_plan_info_rec.plan_id;
4163                                 l_temp_assign_set_id    := l_plan_info_rec.assignment_set_id;
4164                                 IF l_atp_table.attribute_07.Exists(m) THEN
4165                                   l_atp_table.attribute_07(m) := l_plan_info_rec.plan_name;
4166                                 END IF;
4167                                 -- changes for bug 2392456 ends*/
4168                                 l_temp_plan_id := l_atp_table.plan_id(i);
4169 
4170                             ELSE
4171                                 l_temp_plan_id := l_plan_id;
4172                             END IF;
4173 
4174                             -- ship_rec_cal
4175                             IF l_atp_table.requested_arrival_date(i) is not null THEN
4176                                 l_order_date_type := 2;
4177                             ELSE
4178                                 l_order_date_type := 1;
4179                             END IF;
4180 
4181                             MSC_ATP_DB_UTILS.Update_SD_Date(p_atp_table.Identifier(m),
4182                              p_instance_id, l_group_date, l_temp_plan_id,null,
4183                                MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4184                                MSC_ATP_PVT.G_REQ_ATP_DATE(i),
4185                                MSC_ATP_PVT.G_REQ_DATE_QTY(i), -- Bug 1501787
4186                                l_group_date, -- bug 2795053-reopen (ssurendr)
4187                                null,                    -- For time_phased_atp
4188                                l_atp_table.atf_date(m), -- For time_phased_atp
4189                                null,                    -- For time_phased_atp
4190                                l_group_arrival_date,                    -- For ship_rec_cal
4191                                l_order_date_type,                       -- For ship_rec_cal
4192                                l_atp_table.latest_acceptable_date(m),   -- For ship_rec_cal
4193                                l_atp_table.ship_set_name(m),		--Plan by request date
4194                                l_atp_table.arrival_set_name(m),		--Plan by request date
4195                                l_atp_table.override_flag(m),		--Plan by request date
4196 			       l_original_req_arrival_date,     	--Plan by request date
4197 			       null                             --l_bkwd_pass_atf_date_qty bug3397904
4198                                );
4199                               END IF;
4200 	                 END LOOP;
4201 
4202                       ELSE
4203 
4204                         -- this set is not successful
4205                         FOR m in 1.. l_ship_count LOOP
4206                           l_atp_table.Group_Ship_Date(m):= l_group_date;
4207                           IF (l_atp_table.Error_Code(m)= ALLSUCCESS) THEN
4208                             l_atp_table.Error_Code(m) := GROUPEL_ERROR;
4209                           END IF;
4210 
4211                         END LOOP;
4212 
4213                         -- 1072816  need to do something to undo
4214 
4215                         FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4216                           IF PG_DEBUG in ('Y', 'C') THEN
4217                              msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4218                           END IF;
4219                           MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4220                             MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4221                             p_instance_id,
4222                             l_plan_id,
4223                             UNDO,
4224                             MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4225                             l_return_status);
4226 
4227                         END LOOP;
4228 
4229                         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4230                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4231 		        MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4232                         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4233                          IF PG_DEBUG in ('Y', 'C') THEN
4234                             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 8');
4235                          END IF;
4236 
4237                       END IF;
4238                   END IF;
4239 
4240                   IF i = l_ship_count THEN
4241                     l_temp_atp_table := l_atp_table;
4242                   END IF;
4243 
4244                   MSC_ATP_PROC.Details_Output(l_atp_period,
4245                                  l_atp_supply_demand,
4246                                  l_temp_atp_period,
4247                                  l_temp_atp_supply_demand,
4248                                  l_return_status);
4249 
4250                   -- now exit the loop since we are successful
4251 --                  IF l_set_element_success  = l_atp_table.Action.COUNT THEN
4252                   IF l_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
4253                      l_overall_status := ALLSUCCESS ;
4254                   END IF;
4255 
4256 
4257                 ELSE
4258                   -- here we want the the info from ALL warehouses, so
4259                   -- we cannot just copy the records of table over,
4260                   -- we need to append to the temp.
4261 
4262 --                  ROLLBACK to SAVEPOINT start_atp;
4263 
4264                   -- 1225631
4265                   IF ((l_ship_count = 1 AND l_arrival_count = 1) OR
4266                      (l_arrival_count > 1 AND k = l_arrival_count) OR
4267                      (l_ship_count > 1 AND i = l_ship_count) ) THEN
4268 
4269                     FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4270                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4271                           MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4272                           p_instance_id,
4273                           l_plan_id,
4274                           UNDO,
4275                           MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4276                           l_return_status);
4277 
4278                     END LOOP;
4279 
4280                     MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4281                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4282                     MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4283                     MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4284                      IF PG_DEBUG in ('Y', 'C') THEN
4285                         msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 9');
4286                      END IF;
4287                   END IF;
4288 
4289                   IF (i = l_ship_count) AND (l_ship_count > 1 ) THEN
4290                     -- this set is not successful
4291                     FOR m in 1.. l_ship_count LOOP
4292                       l_atp_table.Group_Ship_Date(m):= l_group_date;
4293                       IF (l_atp_table.Error_Code(m)= ALLSUCCESS) THEN
4294                         l_atp_table.Error_Code(m) := GROUPEL_ERROR;
4295                       END IF;
4296                     END LOOP;
4297 
4298                     -- 1072816  need to do something to undo
4299 
4300                     FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4301                       IF PG_DEBUG in ('Y', 'C') THEN
4302                          msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4303                       END IF;
4304                       MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4305                         MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4306                         p_instance_id,
4307                         l_plan_id,
4308                         UNDO,
4309                         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4310                         l_return_status);
4311 
4312                     END LOOP;
4313 
4314                     MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4315                     MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4316 		    MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4317                     MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4318                      IF PG_DEBUG in ('Y', 'C') THEN
4319                         msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 10');
4320                      END IF;
4321 
4322                   END IF;
4323 
4324                   IF i = l_ship_count THEN
4325 
4326                     MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table,
4327                                           l_temp_atp_table,
4328                                           l_return_status );
4329 
4330                   END IF;
4331 
4332                   MSC_ATP_PROC.Details_Output(l_atp_period,
4333                                  l_atp_supply_demand,
4334                                  l_temp_atp_period,
4335                                  l_temp_atp_supply_demand,
4336                                  l_return_status);
4337 
4338                 END IF;
4339 
4340             END IF; -- end if l_multi_scenario = 0
4341 
4342             -- 1497368: now work on the output record of tables.
4343             -- do this only if this is the end of the set or line
4344 
4345             IF i = l_ship_count THEN
4346                IF l_overall_status = ALLSUCCESS THEN
4347 
4348                   IF PG_DEBUG in ('Y', 'C') THEN
4349                      msc_sch_wb.atp_debug('Schedule: ' || 'l_overall_status = ALLSUCCESS');
4350                   END IF;
4351 
4352                   l_temp_atp_table := l_atp_table;
4353 
4354                   IF l_ship_count > 1 THEN
4355                     -- ship set case
4356                     -- the curr one is sucessful
4357                     IF PG_DEBUG in ('Y', 'C') THEN
4358                        msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count > 1');
4359                     END IF;
4360 
4361                     l_temp_atp_period := l_curr_atp_period;
4362                     l_temp_atp_supply_demand := l_curr_atp_supply_demand;
4363                   ELSE
4364                     IF PG_DEBUG in ('Y', 'C') THEN
4365                        msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of (l_ship_count > 1)');
4366                     END IF;
4367 
4368                     -- single line (do not need to consider  arrival set here)
4369                     l_temp_atp_period := l_atp_period;
4370                     l_temp_atp_supply_demand := l_atp_supply_demand;
4371 
4372                   END IF;
4373 
4374                ELSE
4375                   -- we fails
4376                   -- we need to know if we got a date or not, and we need to know
4377                   -- if l_temp_atp_table is empty or not
4378 
4379                   IF PG_DEBUG in ('Y', 'C') THEN
4380                      msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of l_overall_status = ALLSUCCESS');
4381                   END IF;
4382 
4383                   IF l_ship_count > 1 THEN
4384 
4385                     IF PG_DEBUG in ('Y', 'C') THEN
4386                        msc_sch_wb.atp_debug('Schedule: ' || 'l_ship_count > 1');
4387                     END IF;
4388 
4389                     -- this is a set
4390                     IF (l_group_date IS NOT NULL) AND
4391                         (l_arr_no_date = 'Y') THEN
4392 
4393                       IF PG_DEBUG in ('Y', 'C') THEN
4394                          msc_sch_wb.atp_debug('Schedule: ' || 'in the l_group_date is not null and l_arr_no_date = Y');
4395                       END IF;
4396                       l_temp_atp_table := l_atp_table;
4397                       l_temp_atp_period := l_curr_atp_period;
4398                       l_temp_atp_supply_demand := l_curr_atp_supply_demand;
4399                       l_arr_no_date := 'N';
4400                     ELSIF j = 1 THEN -- this is the first warehouse
4401                       IF PG_DEBUG in ('Y', 'C') THEN
4402                          msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSIF j = 1 of (l_group_date is not null and l_arr_no_date = Y)');
4403                       END IF;
4404                       l_temp_atp_table := l_atp_table;
4405                       l_temp_atp_period := l_curr_atp_period;
4406                       l_temp_atp_supply_demand := l_curr_atp_supply_demand;
4407                       l_arr_no_date := 'Y';
4408                     END IF;
4409 
4410                   ELSE
4411 
4412                     IF PG_DEBUG in ('Y', 'C') THEN
4413                        msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSE of (l_ship_count > 1)');
4414                     END IF;
4415 
4416                     -- this is a line
4417                     IF (l_atp_rec.ship_date IS NOT NULL) AND
4418                         (l_arr_no_date = 'Y') THEN
4419                       IF PG_DEBUG in ('Y', 'C') THEN
4420                          msc_sch_wb.atp_debug('Schedule: ' || 'in the l_atp_rec.ship_date is not null and l_arr_no_date = Y');
4421                       END IF;
4422                       l_temp_atp_table := l_atp_table;
4423                       l_temp_atp_period := l_atp_period;
4424                       l_temp_atp_supply_demand := l_atp_supply_demand;
4425                       l_arr_no_date := 'N';
4426                     ELSIF j = 1 THEN -- this is the first warehouse
4427                       IF PG_DEBUG in ('Y', 'C') THEN
4428                          msc_sch_wb.atp_debug('Schedule: ' || 'in the ELSIF j = 1 of (l_group_date is not null and l_arr_no_date = Y)');
4429                       END IF;
4430                       l_temp_atp_table := l_atp_table;
4431                       l_temp_atp_period := l_atp_period;
4432                       l_temp_atp_supply_demand := l_atp_supply_demand;
4433                       l_arr_no_date := 'Y';
4434                     END IF;
4435                   END IF; -- l_ship_count > 1
4436                END IF; -- IF l_overall_status = ALLSUCCESS THEN
4437             END IF;-- i = l_ship_count
4438             -- reinitialize record of tables
4439             l_atp_supply_demand := l_null_atp_supply_demand ;
4440             l_atp_period := l_null_atp_period;
4441             l_atp_details := l_null_atp_details ;
4442 
4443             /* time_phased_atp
4444                set l_time_phased_set to Y if it has already not been set*/
4445             IF l_time_phased_set <> 'Y' and l_time_phased_atp = 'Y' THEN
4446                 l_time_phased_set := 'Y';
4447             END IF;
4448 
4449             /* time_phased_atp
4450                set G_TIME_PHASED_SET to Y if it has already not been set*/
4451             IF G_TIME_PHASED_SET <> 'Y' and l_time_phased_atp = 'Y' THEN
4452                 G_TIME_PHASED_SET := 'Y';
4453             END IF;
4454 
4455             -- if ship set exists, we need to loop through the set
4456             IF l_ship_count > 1 THEN
4457               i := l_atp_table.Action.NEXT(i);
4458             ELSE
4459               i := NULL;
4460             END IF;
4461 
4462           -- BEGIN Bug 2400781
4463           -- Re-assign the global variables
4464           MSC_ATP_PVT.G_INV_CTP := l_inv_ctp;
4465           -- ATP4drp re-set using original profile value
4466           MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
4467           -- End ATP4drp
4468           MSC_ATP_PVT.G_ALLOCATION_METHOD := l_allocation_method;
4469           IF PG_DEBUG in ('Y', 'C') THEN
4470              msc_sch_wb.atp_debug('Schedule: ' || 'Reset Destination INV_CTP '
4471                                    || MSC_ATP_PVT.G_INV_CTP);
4472           END IF;
4473           -- The local variables were assigned at the beginning of the Loop
4474           -- Values should be reset after every ship-set row.
4475           -- END Bug 2400781
4476 
4477           END LOOP; -- end of i loop for ship set
4478 
4479           -- reinitialize record of tables
4480           l_atp_supply_demand := l_null_atp_supply_demand ;
4481           l_atp_period := l_null_atp_period;
4482           l_atp_details := l_null_atp_details ;
4483           l_set_element_success := 0;
4484 
4485           l_curr_atp_supply_demand := l_null_atp_supply_demand ;
4486           l_curr_atp_period := l_null_atp_period;
4487 
4488           j := l_sources.source_type.NEXT(j);
4489 
4490          END LOOP;  -- end of j loop for warehouse
4491          --- Bug 1104565
4492          --IF j = l_sources.source_type.count THEN
4493          --   --- ADD the pegging id for the best source
4494          --   FOR l_peg_count in 1..l_atp_table.ship_date.count LOOP
4495          --     l_atp_table.end_pegging_id(l_peg_count) := l_fst_src_pegging_ids(1);
4496          --   END LOOP;
4497          --END IF;
4498 
4499          IF (l_temp_atp_table.Action.COUNT > 0) THEN
4500 
4501            IF PG_DEBUG in ('Y', 'C') THEN
4502               msc_sch_wb.atp_debug('Schedule: ' || 'I am here!!!');
4503            END IF;
4504            -- we need to assign oe_flag for oe to indicate which
4505            -- scenario they should show to users if we return multiple
4506            -- scenarios.  Now we need to pick one which is inside the
4507            -- latest acceptable range
4508 
4509 
4510            IF l_multi_scenario = 1 THEN
4511 
4512             l_oe_demand_flag := 'N';
4513 
4514              FOR m in 1..l_sources.source_type.COUNT LOOP
4515 
4516                IF l_oe_demand_flag = 'N' THEN -- if we haven't find the scenario
4517                  l_temp := 0;
4518 
4519                  FOR j in ((m-1)*l_ship_count+1)..(m*l_ship_count) LOOP
4520                    IF(((l_temp_atp_table.requested_ship_date(j) IS NOT NULL) AND
4521                        (l_temp_atp_table.ship_date(j)
4522                       <= NVL(l_temp_atp_table.latest_acceptable_date(j),
4523                              l_temp_atp_table.requested_ship_date(j))))
4524                       OR
4525                     ((l_temp_atp_table.requested_arrival_date(j)IS NOT NULL) AND
4526                       (l_temp_atp_table.ship_date(j)+
4527                       NVL(l_temp_atp_table.delivery_lead_time(j), 0)
4528                       <= NVL(l_temp_atp_table.latest_acceptable_date(j),
4529                              l_temp_atp_table.requested_arrival_date(j))))) THEN
4530 
4531 
4532                     -- inside the acceptable range
4533                        l_temp := l_temp +1;
4534                    ELSE
4535                        -- not in the range, then we can exit the j loop since
4536                        -- this i th scenario won't be the right one.
4537                        -- however, if we only have one source here, then
4538                        -- even if it is not in the range, we should return
4539                        -- this to OE.
4540                        IF l_sources.source_type.COUNT = 1 THEN
4541                           l_temp :=  l_temp +1;
4542                        ELSE
4543                           EXIT;
4544                        END IF;
4545                    END IF;
4546                  END LOOP;  -- end of j loop
4547 
4548                  IF l_temp = l_ship_count THEN  -- l_ship_count = 1 for a line
4549                  -- we find the right scenario (m) for oe
4550                    l_oe_demand_flag := 'Y';
4551                    FOR j in ((m-1)*l_ship_count+1)..(m*l_ship_count) LOOP
4552                      l_temp_atp_table.OE_Flag(j) := 'Y';
4553                    END LOOP;
4554                    EXIT;
4555                  END IF;
4556                END IF; -- end if of l_oe_demand_flag
4557              END LOOP; -- m loop;
4558            END IF; -- end of l_multi_scenario = 1
4559 
4560            IF l_arrival_count > 1 THEN
4561 
4562              -- if this is an arrival set, since we need to organize the
4563              -- scenario before we actually store the info into x table,
4564              -- let's store the info in temp arr table.
4565              MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_atp_table,
4566                             l_temp_arr_atp_table,
4567                             x_return_status );
4568 
4569              MSC_ATP_PROC.Details_Output(l_temp_atp_period,
4570                      l_temp_atp_supply_demand,
4571                      l_temp_arr_atp_period,
4572                      l_temp_arr_atp_supply_demand,
4573                      x_return_status);
4574            ELSE
4575              MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_atp_table,
4576                             x_atp_table,
4577                             x_return_status );
4578 
4579              MSC_ATP_PROC.Details_Output(l_temp_atp_period,
4580                      l_temp_atp_supply_demand,
4581                      x_atp_period,
4582                      x_atp_supply_demand,
4583                      x_return_status);
4584            END IF;
4585          END IF;
4586 
4587 	 IF PG_DEBUG in ('Y', 'C') THEN
4588 	    msc_sch_wb.atp_debug('Schedule: ' || 'before k := p_atp_table.Arrival_Set_Name.NEXT(i)');
4589 	    msc_sch_wb.atp_debug ('Schedule: ' || 'k :='||to_char(k));
4590 	    msc_sch_wb.atp_debug ('Schedule: ' || 'i :='||to_char(i));
4591 	 END IF;
4592 
4593          IF l_arrival_count > 1 THEN
4594            k := p_atp_table.Arrival_Set_Name.NEXT(k);
4595          ELSE
4596            k := null;
4597          END IF;
4598 
4599 	 IF PG_DEBUG in ('Y', 'C') THEN
4600 	    msc_sch_wb.atp_debug('Schedule: ' || 'after k := p_atp_table.Arrival_Set_Name.NEXT(i)');
4601 	    msc_sch_wb.atp_debug ('Schedule: ' || 'k :='||to_char(k));
4602 	    msc_sch_wb.atp_debug ('Schedule: ' || 'i :='||to_char(i));
4603 	 END IF;
4604 
4605          l_temp_atp_supply_demand := l_null_atp_supply_demand;
4606          l_temp_atp_period := l_null_atp_period;
4607          l_temp_atp_details := l_null_atp_details;
4608          l_temp_atp_table := x_atp_table;
4609         END LOOP;  -- end of k loop for arrival set
4610 
4611     -- Mark moving ELSE and END IF towards the end of the Schedule Procedure. MARK
4612     /*
4613     ELSE
4614 
4615       -- for oe bug fix :  no record for undemand
4616 
4617       MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, x_return_status );
4618 
4619     END IF; -- end if action = ATPQUERY or DEMANDADD or DEMANDMODIFY
4620     */
4621 
4622 
4623       -- now we organize the scenarios for arrival set if arrival set exists.
4624       -- we will only have 2 scenarios.
4625       -- 1.  what OE wants.  one warehouse for each item in the arrival set.
4626       --     consider latest_acceptable_dates
4627       -- 2.  what MRP wants. may display multiple warehouses for a line
4628       --     in the arrival set as long as we can satisfy (or close) to the
4629       --     request.
4630       -- AND we need to work on group date.
4631 
4632       -- xxxx to be consistent with ship sets...
4633       IF l_arrival_count > 1 and p_atp_table.quantity_ordered(1) <> 0 THEN
4634         -- Bug 2259824
4635 	l_group_date := TRUNC(sysdate);--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
4636         IF PG_DEBUG in ('Y', 'C') THEN
4637             msc_sch_wb.atp_debug('Schedule: 2259824 l_group_date = '||
4638                  to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
4639         END IF;
4640 
4641 	IF l_multi_scenario = 1 THEN
4642 
4643         IF PG_DEBUG in ('Y', 'C') THEN
4644            msc_sch_wb.atp_debug('Schedule: ' || 'print data from l_temp_arr_atp_table');
4645            i := l_temp_arr_atp_table.Action.FIRST;
4646            WHILE i IS NOT NULL LOOP
4647 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Inventory_Item_Id( ' ||
4648 		   to_char(i) || '):'|| to_char(l_temp_arr_atp_table.Inventory_Item_Id(i)) );
4649 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Source_Organization_Id:' ||
4650 		   to_char(l_temp_arr_atp_table.Source_Organization_Id(i)) );
4651 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.Quantity_Ordered:' ||
4652 		   to_char(l_temp_arr_atp_table.Quantity_Ordered(i)) );
4653 	      msc_sch_wb.atp_debug('Schedule: ' || 'l_temp_arr_atp_table.OE_FLAG:' ||
4654 		   l_temp_arr_atp_table.OE_FLAG(i) );
4655 	      i := l_temp_arr_atp_table.Action.NEXT(i);
4656            END LOOP;
4657 	END IF;
4658 
4659            -- work on the first scenario
4660            FOR i in 1..l_temp_arr_atp_table.Action.COUNT LOOP
4661              IF l_temp_arr_atp_table.OE_Flag(i) = 'Y' THEN
4662                -- For bug 2748730. Move the group date to end of day
4663 	       -- This ensures that group dates are also moved to the end of day
4664 	       l_group_date := greatest(l_group_date,
4665                                l_temp_arr_atp_table.Ship_Date(i)+
4666                              NVL(l_temp_arr_atp_table.delivery_lead_time(i),0));
4667 			     --4460369+ MSC_ATP_PVT.G_END_OF_DAY;
4668 
4669                MSC_SATP_FUNC.Assign_Atp_Input_Rec(l_temp_arr_atp_table,
4670                                     i,
4671                                     x_atp_table,
4672                                     l_return_status);
4673 
4674                MSC_ATP_PROC.Get_SD_Period_Rec(l_temp_arr_atp_period,
4675                                  l_temp_arr_atp_supply_demand,
4676                                  l_temp_arr_atp_table.Identifier(i),
4677                                  l_temp_arr_atp_table.Scenario_Id(i),
4678                                  1,
4679                                  x_atp_period,
4680                                  x_atp_supply_demand,
4681                                  l_return_status);
4682              END IF;
4683            END LOOP;
4684 
4685            -- first scenario: populate group date
4686            IF PG_DEBUG in ('Y', 'C') THEN
4687               msc_sch_wb.atp_debug('Schedule: ' || 'working on group date');
4688            END IF;
4689            FOR i in 1..x_atp_table.Action.COUNT LOOP
4690              x_atp_table.Scenario_Id(i) := 1;
4691            END LOOP;
4692 
4693            j := x_atp_table.Action.COUNT;
4694            -- we need to remember this COUNT so that j + 1 is the beginning
4695            -- of scenario 2.
4696 
4697            -- work on the second scenario
4698 
4699            i:=1;
4700            FOR k in 1..p_atp_table.Action.COUNT LOOP
4701              l_total_qty := 0.0;
4702              l_done_scenario2 := 'N';
4703 
4704              WHILE i IS NOT NULL LOOP
4705                IF (l_temp_arr_atp_table.Identifier(i)=p_atp_table.Identifier(k))
4706                    AND (l_temp_arr_atp_table.inventory_item_id(i) =
4707                         p_atp_table.inventory_item_id(k) ) THEN
4708 
4709                  IF l_done_scenario2 = 'N' THEN
4710 
4711                    -- we only care the warehouse that provides positive
4712                    -- requested_date_quantity
4713 
4714                      l_total_qty:=l_total_qty +greatest(0,
4715                                l_temp_arr_atp_table.Requested_Date_Quantity(i));
4716                      MSC_SATP_FUNC.Assign_Atp_Input_Rec(l_temp_arr_atp_table,
4717                                         i,
4718                                         x_atp_table,
4719                                         l_return_status );
4720 
4721                      MSC_ATP_PROC.Get_SD_Period_Rec(l_temp_arr_atp_period,
4722                                  l_temp_arr_atp_supply_demand,
4723                                  l_temp_arr_atp_table.Identifier(i),
4724                                  l_temp_arr_atp_table.Scenario_Id(i),
4725                                  2,
4726                                  x_atp_period,
4727                                  x_atp_supply_demand,
4728                                  l_return_status);
4729 
4730                      IF l_total_qty>=l_temp_arr_atp_table.Quantity_Ordered(i) THEN
4731                        l_done_scenario2 := 'Y';
4732                      END IF;
4733                  END IF;
4734                  i := l_temp_arr_atp_table.Action.NEXT(i);
4735 
4736                ELSE
4737                  -- for this line we are done, we need to move to next line
4738                  -- (in k loop)
4739                  EXIT;
4740                END IF;
4741              END LOOP;  -- end of i loop
4742              IF l_done_scenario2 = 'Y' THEN
4743                -- for this line we can satisfy on the request date
4744 	       -- For bug 2748730. Move the group date to end of day even if line is overridden
4745 	       -- This ensures that group dates are also moved to the end of day
4746                l_group_date := TRUNC(Greatest(l_group_date,
4747                                    p_atp_table.Requested_Arrival_Date(k)));--4460369 + MSC_ATP_PVT.G_END_OF_DAY;
4748              ELSE
4749                l_group_date := NULL;
4750              END IF;
4751            END LOOP;  -- end of k loop
4752 
4753            -- second scenario: populate group date
4754 
4755            FOR i in j+1..x_atp_table.Action.COUNT LOOP
4756              x_atp_table.Group_Arrival_Date(i) := l_group_date;
4757              x_atp_table.OE_Flag(i) := 'N';
4758              x_atp_table.Scenario_Id(i) := 2;
4759            END LOOP;
4760 
4761         ELSE
4762            -- multi_scenario = 0
4763            -- this is the OE case
4764            -- get the group date.
4765            IF PG_DEBUG in ('Y', 'C') THEN
4766               msc_sch_wb.atp_debug('Schedule: ' || 'in arrival set, oe case');
4767            END IF;
4768 
4769            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table,
4770                             x_atp_table,
4771                             x_return_status );
4772 
4773            MSC_ATP_PROC.Details_Output(l_temp_arr_atp_period,
4774                      l_temp_arr_atp_supply_demand,
4775                      x_atp_period,
4776                      x_atp_supply_demand,
4777                      x_return_status);
4778 
4779            /* 2896854
4780            FOR i in 1..x_atp_table.Action.COUNT LOOP
4781                -- For bug 2748730. Move the group date to end of day even if line is overridden
4782 	       -- This ensures that group dates are also moved to the end of day
4783 	       l_group_date := TRUNC(greatest(l_group_date,
4784                                l_temp_arr_atp_table.Ship_Date(i)+
4785                              NVL(l_temp_arr_atp_table.delivery_lead_time(i),0)))
4786 			     + MSC_ATP_PVT.G_END_OF_DAY;
4787            END LOOP;
4788            */
4789 
4790 
4791            -- 1072816  need to do something to undo
4792 
4793            IF PG_DEBUG in ('Y', 'C') THEN
4794               msc_sch_wb.atp_debug('Schedule: ' || 'l_group_date ='||
4795 			to_char(l_group_date, 'DD-MON-YYYY HH24:MI:SS'));
4796               msc_sch_wb.atp_debug('Schedule: ' || 'p_atp_table.Requested_Arrival_Date(1) = '||
4797 			to_char(p_atp_table.Requested_Arrival_Date(1), 'DD-MON-YYYY HH24:MI:SS'));
4798            END IF;
4799            -- dsting setproc
4800 
4801            -- 2902129 delay set processing til here for arrival set
4802            MSC_ATP_PROC.Initialize_Set_Processing(x_atp_table);
4803            for k in 1..x_atp_table.action.count() loop
4804                MSC_ATP_PROC.Process_Set_Line(x_atp_table, k, l_line_status);
4805            end loop;
4806 
4807            MSC_ATP_PROC.Process_Set_Dates_Errors(x_atp_table, 'D', l_set_status);
4808            l_group_date := x_atp_table.group_arrival_date(1);
4809 
4810 --           IF l_set_status <> ALLSUCCESS THEN
4811            IF l_set_status <> ALLSUCCESS OR x_atp_table.action(1) = ATPQUERY
4812            THEN
4813              -- arrival set fails/ we need to remove demands
4814              IF PG_DEBUG in ('Y', 'C') THEN
4815                 msc_sch_wb.atp_debug('Schedule: ' || 'before removing the data for arrival set');
4816              END IF;
4817 
4818              -- 2902265
4819              MSC_ATP_PROC.cleanup_set(p_instance_id,
4820                                       l_plan_id,
4821                                       l_fst_src_pegging_ids,
4822                                       l_demand_class_atp_flag);
4823 /*
4824              FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
4825                IF PG_DEBUG in ('Y', 'C') THEN
4826                   msc_sch_wb.atp_debug('Schedule: ' || 'in the loop');
4827                END IF;
4828                MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
4829                  MSC_ATP_PVT.G_PEGGING_FOR_SET(i)  ,
4830                  p_instance_id,
4831                  l_plan_id,
4832                  UNDO,
4833                  MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),
4834                  l_return_status);
4835              END LOOP;
4836 */
4837              MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
4838              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
4839              MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
4840              MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
4841               IF PG_DEBUG in ('Y', 'C') THEN
4842                  msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 12');
4843               END IF;
4844            ELSE
4845              IF PG_DEBUG in ('Y', 'C') THEN
4846                 msc_sch_wb.atp_debug('arrival set succeeded');
4847              END IF;
4848              -- dsting setproc
4849 
4850              -- 2902265
4851              MSC_ATP_PVT.G_PEGGING_FOR_SET := l_fst_src_pegging_ids;
4852              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG := l_demand_class_atp_flag;
4853              MSC_ATP_PVT.G_REQ_ATP_DATE := l_req_atp_DATE;
4854              MSC_ATP_PVT.G_REQ_DATE_QTY := l_req_date_qty;
4855 
4856              MSC_ATP_PROC.Update_Set_SD_Dates(x_atp_table,l_req_arr_date); --Plan by request date
4857            END IF;
4858 
4859            FOR i in 1..x_atp_table.Action.COUNT LOOP
4860              x_atp_table.Scenario_Id(i) := 1;
4861            END LOOP;
4862 
4863            FOR i in 1..x_atp_period.Level.COUNT LOOP
4864              x_atp_period.Scenario_Id(i) := 1;
4865            END LOOP;
4866 
4867            FOR i in 1..x_atp_supply_demand.Level.COUNT LOOP
4868              x_atp_supply_demand.Scenario_Id(i) := 1;
4869            END LOOP;
4870 
4871            FOR i in 1..x_atp_details.Level.COUNT LOOP
4872              x_atp_details.Scenario_Id(i) := 1;
4873            END LOOP;
4874 
4875         END IF;
4876       END IF;
4877 
4878       -- Bug 1661545, if scheduling was unsuccessful, make sure that old demand record is
4879       -- preserved back, as it was updated to 0 in the begining in case of reschedule.
4880 
4881       ---bug 2384224
4882       IF PG_DEBUG in ('Y', 'C') THEN
4883         msc_sch_wb.atp_debug('Schedule: Debug l_inv_ctp ' || l_inv_ctp);
4884         msc_sch_wb.atp_debug('Schedule: Debug G_INV_CTP ' || MSC_ATP_PVT.G_INV_CTP);
4885         msc_sch_wb.atp_debug('Schedule: Debug x_atp_table.Action(1)' || x_atp_table.Action(1));
4886       END IF;
4887       IF /*(l_inv_ctp = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
4888           x_atp_table.Action(1) = DEMANDADD OR
4889           x_atp_table.Action(1) = DMDRSVADD) THEN
4890 
4891         /*-- Loop thru the whole set to check if any of the records had failed Scheduling
4892         -- Update demands to reset the quantity and date in such cases
4893         -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
4894         l_delete_demand_flag := 0;
4895         FOR m in 1..x_atp_table.Action.COUNT LOOP
4896             IF (NVL(x_atp_table.error_code(m), -1) NOT IN (0, 61)) THEN
4897                 l_delete_demand_flag := 1;
4898                 EXIT;
4899             END IF;
4900         END LOOP; */
4901 
4902         IF NVL(x_atp_table.error_code(1), -1) not in (0, 61) THEN --3720018
4903             IF PG_DEBUG in ('Y', 'C') THEN
4904                 msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
4905                 msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
4906             END IF;
4907 
4908             -- rajjain single call to Undo_Delete_Row procedure --3720018
4909             MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
4910                     l_delete_atp_rec.del_plan_ids,
4911                     p_instance_id,
4912                     --subst
4913                     l_delete_atp_rec.del_demand_ids,
4914                     l_delete_atp_rec.del_inv_item_ids,
4915                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
4916                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
4917                     l_time_phased_set,            -- For time_phased_atp
4918                     -- CTO ODR and Simplified Pegging
4919                     l_delete_atp_rec.del_atp_peg_items,
4920                     l_delete_atp_rec.del_atp_peg_demands ,
4921                     l_delete_atp_rec.del_atp_peg_supplies,
4922                     l_delete_atp_rec.del_atp_peg_res_reqs,
4923                     l_delete_atp_rec.del_demand_source_type, --cmro
4924                     -- End CTO ODR and Simplified Pegging
4925                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
4926                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
4927                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
4928                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
4929                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
4930                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
4931                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
4932                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
4933                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
4934                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
4935                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
4936                     );
4937 
4938             IF PG_DEBUG in ('Y', 'C') THEN
4939                 msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
4940             END IF;
4941 
4942         ELSE    -- For summary enhancement
4943 
4944            --3720018 ,changes to refer all the records from l_delete_atp_rec
4945 
4946            -- Append copy SO ids for summary enhancement --3720018
4947            IF l_delete_atp_rec.del_copy_demand_ids IS NOT NULL and l_delete_atp_rec.del_copy_demand_ids.COUNT > 0 THEN
4948                l_count := MSC_ATP_PVT.G_COPY_DEMAND_ID.count;
4949                MSC_ATP_PVT.G_COPY_DEMAND_ID.extend(l_delete_atp_rec.del_copy_demand_ids.count);
4950                MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID.extend(l_delete_atp_rec.del_copy_demand_ids.count);
4951                FOR i in 1..l_delete_atp_rec.del_copy_demand_ids.count LOOP
4952                    MSC_ATP_PVT.G_COPY_DEMAND_ID(l_count + i) := l_delete_atp_rec.del_copy_demand_ids(i);
4953                    MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID(l_count + i) := l_delete_atp_rec.del_copy_demand_plan_ids(m);
4954                END LOOP;
4955            END IF;
4956 
4957            --Changes for Bug 3629191 : Population of Offset data in Global Records.
4958 
4959            IF PG_DEBUG in ('Y', 'C') THEN
4960               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
4961            END IF;
4962 
4963            IF l_delete_atp_rec.del_demand_ids IS NOT NULL and l_delete_atp_rec.del_demand_ids.COUNT > 0 THEN
4964               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
4965               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_demand_ids.count);
4966               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
4967               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
4968               IF PG_DEBUG in ('Y', 'C') THEN
4969                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_demand_ids ' || l_delete_atp_rec.del_demand_ids.count);
4970               END IF;
4971               FOR i in 1..l_delete_atp_rec.del_demand_ids.count LOOP
4972                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := p_instance_id;
4973                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_demand_ids(i);
4974                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.del_plan_ids(i);
4975               END LOOP;
4976            END IF;
4977 
4978            IF PG_DEBUG in ('Y', 'C') THEN
4979               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
4980            END IF;
4981 
4982            IF l_delete_atp_rec.del_atp_peg_demands IS NOT NULL and l_delete_atp_rec.del_atp_peg_demands.COUNT > 0 THEN
4983               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
4984               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_demands.count);
4985               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
4986               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
4987               IF PG_DEBUG in ('Y', 'C') THEN
4988                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_demands.count ' || l_delete_atp_rec.del_atp_peg_demands.count);
4989               END IF;
4990               FOR i in 1..l_delete_atp_rec.del_atp_peg_demands.count LOOP
4991                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_demand_instance_id(i);
4992                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_demands(i);
4993                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_demands_plan_ids(i);
4994               END LOOP;
4995            END IF;
4996 
4997            IF PG_DEBUG in ('Y', 'C') THEN
4998               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
4999            END IF;
5000 
5001            IF l_delete_atp_rec.del_atp_peg_supplies IS NOT NULL and l_delete_atp_rec.del_atp_peg_supplies.COUNT > 0 THEN
5002               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5003               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_supplies.count);
5004               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5005               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5006               IF PG_DEBUG in ('Y', 'C') THEN
5007                  msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_supplies.count ' || l_delete_atp_rec.del_atp_peg_supplies.count );
5008               END IF;
5009               FOR i in 1..l_delete_atp_rec.del_atp_peg_supplies.count LOOP
5010                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_supply_instance_id(i);
5011                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_supplies(i);
5012                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_supplies_plan_ids(i);
5013               END LOOP;
5014            END IF;
5015 
5016            IF PG_DEBUG in ('Y', 'C') THEN
5017               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5018            END IF;
5019 
5020            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
5021               l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5022               MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5023               MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5024               MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5025               IF PG_DEBUG in ('Y', 'C') THEN
5026                  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 );
5027               END IF;
5028               FOR i in 1..l_delete_atp_rec.del_atp_peg_res_reqs.count LOOP
5029                   MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_res_instance_id(i);
5030                   MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_res_reqs(i);
5031                   MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_res_reqs_plan_ids(i);
5032               END LOOP;
5033            END IF;
5034            --3720018
5035 
5036            IF PG_DEBUG in ('Y', 'C') THEN
5037               msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5038            END IF;
5039 
5040            --Changes for Bug 3629191: Population of Offset data in Global record ends.
5041 
5042            -- CTO ODR and Simplified Pegging
5043            IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1 THEN
5044 
5045            -- ATP request successfull, create atp pegging
5046             IF PG_DEBUG in ('Y', 'C') THEN
5047                 msc_sch_wb.atp_debug('Schedule: ' || 'Model present in Set Creating ATP Pegging');
5048             END IF;
5049 
5050              FOR i in 1..x_atp_table.inventory_item_id.COUNT LOOP
5051                IF ((x_atp_table.ATO_Model_Line_Id(i) IS NOT NULL) AND
5052                    (x_atp_table.identifier(i) = NVL(x_atp_table.ATO_Model_Line_Id(i), 0)) AND
5053 
5054                    -- Bug 3334643 Create Pegging only if the Model  configuration
5055                    -- item ATP request goes against a plan.
5056                    (x_atp_table.plan_id(i) > 0) AND
5057                    -- Bug 3343757 Create ATP Simplified pegging only if
5058                    -- plan_id > 0 and pegging has been generated.
5059                    (x_atp_table.End_Pegging_Id(i) IS NOT NULL) ) THEN
5060 
5061                  IF PG_DEBUG in ('Y', 'C') THEN
5062                     msc_sch_wb.atp_debug('Schedule: Before Call to Create_Atp_Pegging ');
5063                     msc_sch_wb.atp_debug('Schedule: Plan_id : '|| x_atp_table.plan_id(i) );
5064                     msc_sch_wb.atp_debug('Schedule: Pegging_id : '|| x_atp_table.End_Pegging_id(i));
5065                     msc_sch_wb.atp_debug('Schedule: Match Item : '|| x_atp_table.match_item_id(i));
5066                     msc_sch_wb.atp_debug('Schedule: Item Id : '|| x_atp_table.Inventory_Item_Id(i));
5067                     msc_sch_wb.atp_debug('Schedule: Req Item : '|| x_atp_table.request_item_id(i));
5068                  END IF;
5069 
5070                     MSC_ATP_PEG.Create_Atp_Pegging (
5071                           x_atp_table.End_Pegging_Id(i),
5072                           p_instance_id,
5073                           l_old_plan_id(i),
5074                           x_atp_table.ATO_Model_Line_Id(i),
5075                           x_atp_table.Config_item_line_id(i),
5076                           x_atp_table.demand_source_type(i),--cmro
5077                           l_return_status
5078                           );
5079                ELSE
5080                    IF PG_DEBUG in ('Y', 'C') THEN
5081                       msc_sch_wb.atp_debug('Schedule: No Call to Create_Atp_Pegging ');
5082                       msc_sch_wb.atp_debug('Schedule: Plan_id : '|| x_atp_table.plan_id(i) );
5083                    END IF;
5084 
5085                  -- End Bug 3334643
5086 
5087                END IF;
5088 
5089              END LOOP;
5090 
5091            END IF; -- MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1
5092            -- End CTO ODR and Simplified Pegging
5093 
5094         END IF;
5095         --e_cto_rearch
5096       END IF;     -- Bug 1661545
5097 
5098       /*-- we now insert new picture for each session. Hence we do not need to update old picture
5099       IF l_inv_ctp = 4 and MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1
5100                        AND NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5101          IF  (x_atp_table.action(1) = ATPQUERY or
5102              ((x_atp_table.Action(1) = DEMANDMODIFY OR
5103              x_atp_table.Action(1) = DEMANDADD OR
5104              x_atp_table.Action(1) = DMDRSVADD)
5105              AND NVL(x_atp_table.error_code(1), -1) not in (0, 61))) THEN
5106 
5107              --failure case
5108              MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5109                                                x_atp_table,
5110                                                MSC_ATP_CTO.FAIL);
5111          ELSE
5112               --scheduling and success
5113               MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5114                                                 x_atp_table,
5115                                                 MSC_ATP_CTO.Success);
5116          END IF;
5117 
5118       END IF;
5119       */
5120 
5121     -- Mark moved ELSE and END IF to here from the previous MARK point.
5122     ELSE
5123 
5124     IF PG_DEBUG in ('Y', 'C') THEN
5125        msc_sch_wb.atp_debug('Schedule: Unscheduling...');
5126     END IF;
5127 
5128     --Changes for Bug 3629191 : Population of Offset data in Global Records.
5129 
5130     --3720018, arrays to be refered from l_delete_atp_rec.
5131 
5132     IF PG_DEBUG in ('Y', 'C') THEN
5133        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5134     END IF;
5135 
5136     IF l_delete_atp_rec.del_demand_ids IS NOT NULL and l_delete_atp_rec.del_demand_ids.COUNT > 0 THEN
5137        l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5138        MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_demand_ids.count);
5139        MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5140        MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_demand_ids.count);
5141        IF PG_DEBUG in ('Y', 'C') THEN
5142           msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_demand_ids ' || l_delete_atp_rec.del_demand_ids.count);
5143        END IF;
5144        FOR i in 1..l_delete_atp_rec.del_demand_ids.count LOOP
5145            MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := p_instance_id;
5146            MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_demand_ids(i);
5147            MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.del_plan_ids(i);
5148        END LOOP;
5149     END IF;
5150 
5151 
5152     IF PG_DEBUG in ('Y', 'C') THEN
5153        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5154     END IF;
5155 
5156     IF l_delete_atp_rec.del_atp_peg_demands IS NOT NULL and l_delete_atp_rec.del_atp_peg_demands.COUNT > 0 THEN
5157        l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5158        MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_demands.count);
5159        MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5160        MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_demands.count);
5161        IF PG_DEBUG in ('Y', 'C') THEN
5162           msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_demands.count ' || l_delete_atp_rec.del_atp_peg_demands.count);
5163        END IF;
5164        FOR i in 1..l_delete_atp_rec.del_atp_peg_demands.count LOOP
5165            MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_demand_instance_id(i);
5166            MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_demands(i);
5167            MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_demands_plan_ids(i);
5168        END LOOP;
5169      END IF;
5170 
5171 
5172      IF PG_DEBUG in ('Y', 'C') THEN
5173        msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5174      END IF;
5175 
5176      IF l_delete_atp_rec.del_atp_peg_supplies IS NOT NULL and l_delete_atp_rec.del_atp_peg_supplies.COUNT > 0 THEN
5177         l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5178         MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_supplies.count);
5179         MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5180         MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_supplies.count);
5181         IF PG_DEBUG in ('Y', 'C') THEN
5182            msc_sch_wb.atp_debug('Schedule: l_delete_atp_rec.del_atp_peg_supplies.count ' || l_delete_atp_rec.del_atp_peg_supplies.count );
5183         END IF;
5184         FOR i in 1..l_delete_atp_rec.del_atp_peg_supplies.count LOOP
5185              MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_supply_instance_id(i);
5186              MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_supplies(i);
5187              MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_supplies_plan_ids(i);
5188         END LOOP;
5189      END IF;
5190 
5191 
5192      IF PG_DEBUG in ('Y', 'C') THEN
5193         msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5194      END IF;
5195 
5196      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
5197         l_count := MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count;
5198         MSC_ATP_PVT.G_OFF_INSTANCE_ID.extend(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5199         MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5200         MSC_ATP_PVT.G_OFF_PLAN_ID.EXTEND(l_delete_atp_rec.del_atp_peg_res_reqs.count);
5201         IF PG_DEBUG in ('Y', 'C') THEN
5202            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 );
5203         END IF;
5204         FOR i in 1..l_delete_atp_rec.del_atp_peg_res_reqs.count LOOP
5205             MSC_ATP_PVT.G_OFF_INSTANCE_ID(l_count + i)  := l_delete_atp_rec.off_res_instance_id(i);
5206             MSC_ATP_PVT.G_OFF_DEM_TRANS_ID(l_count + i) := l_delete_atp_rec.del_atp_peg_res_reqs(i);
5207             MSC_ATP_PVT.G_OFF_PLAN_ID(l_count + i)      := l_delete_atp_rec.atp_peg_res_reqs_plan_ids(i);
5208         END LOOP;
5209       END IF;
5210       IF PG_DEBUG in ('Y', 'C') THEN
5211          msc_sch_wb.atp_debug('Schedule: G_OFF_DEM_TRANS_ID.count ' || MSC_ATP_PVT.G_OFF_DEM_TRANS_ID.count );
5212       END IF;
5213       --3720018
5214 
5215       --Changes for Bug 3629191: Population of Offset data in Global record ends.
5216 
5217       -- for oe bug fix :  no record for undemand
5218 
5219       MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, x_return_status );
5220       -- CTO ODR and Simplified Pegging
5221       -- Dealing with the Unschedule process
5222       IF (x_atp_table.Action(1) = DEMANDMODIFY OR
5223           x_atp_table.Action(1) = DEMANDADD OR
5224           x_atp_table.Action(1) = DMDRSVADD) THEN
5225         IF PG_DEBUG in ('Y', 'C') THEN
5226            msc_sch_wb.atp_debug('Schedule: Model in set : ' ||
5227                                      MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET);
5228         END IF;
5229 
5230            IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET = 1 THEN
5231              -- Dealing with Unschedule ATP Process.
5232              -- So Just Delete the old data.
5233              FOR i in 1..x_atp_table.inventory_item_id.COUNT LOOP
5234                IF (x_atp_table.ATO_Model_Line_Id(i) IS NOT NULL AND
5235                    (x_atp_table.identifier(i) = NVL(x_atp_table.ATO_Model_Line_Id(i), 0))) THEN
5236 
5237 
5238                   IF PG_DEBUG in ('Y', 'C') THEN
5239                      msc_sch_wb.atp_debug('End Pegging Id Pegging Identifier : ' ||
5240                                                       x_atp_table.End_Pegging_Id(i));
5241                      msc_sch_wb.atp_debug('Instance Id p_instance_id : ' || p_instance_id);
5242                      msc_sch_wb.atp_debug('Model Line Id Model_order_line_id : ' ||
5243                                                          x_atp_table.ATO_Model_Line_Id(i));
5244                      msc_sch_wb.atp_debug('Config. Line Id Config_order_line_id : ' ||
5245                                                        x_atp_table.Config_item_line_id(i));
5246                      msc_sch_wb.atp_debug('Old Plan Id : ' || l_old_plan_id(i));
5247                   END IF;
5248 
5249                   DELETE from msc_atp_pegging
5250                   WHERE  plan_id = l_old_plan_id(i)
5251                   AND    sales_order_line_id in (
5252                               NVL( x_atp_table.Config_item_line_id(i), -1),
5253                                x_atp_table.ATO_Model_Line_Id(i) );
5254 
5255                   IF PG_DEBUG in ('Y', 'C') THEN
5256                      msc_sch_wb.atp_debug('Schedule: Number of rows deleted '||
5257                                ' from Atp Pegging ' || SQL%ROWCOUNT);
5258                   END IF;
5259                END IF;
5260              END LOOP;
5261            END IF;
5262 
5263       END IF;
5264       -- End CTO ODR and Simplified Pegging
5265 
5266     END IF; -- end if action = ATPQUERY or DEMANDADD or DEMANDMODIFY
5267     -- commit;
5268     IF PG_DEBUG in ('Y', 'C') THEN
5269        msc_sch_wb.atp_debug('**********End Schedule Procedure************');
5270     END IF;
5271 
5272 EXCEPTION
5273 
5274     WHEN NO_DATA_FOUND THEN
5275         x_return_status := FND_API.G_RET_STS_ERROR;
5276 
5277         -- Resetting global variables for additional issue encountered while bug 3295831
5278         IF PG_DEBUG in ('Y', 'C') THEN
5279             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5280         END IF;
5281         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5282         -- ATP4drp re-set using original profile value
5283         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5284         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5285         -- End ATP4drp
5286         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5287 
5288         -- Modified by NGOEL 12/13/00, replaced x_return_status with l_return_status
5289         -- Earlier error status was getting overwritten with the error status of the proc.
5290 
5291         -- 2625800: krajan
5292         ---agilent arrival set bug
5293         IF l_arrival_count > 1 THEN
5294            IF PG_DEBUG in ('Y', 'C') THEN
5295               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5296            END IF;
5297            --for arrivalset l_atp_table contains only the record that we are processing
5298            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5299            ---first we add the record we have processed so far
5300            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5301            ---now we add the current reocrd
5302            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5303            --now we assign rest of the record.
5304            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5305            IF PG_DEBUG in ('Y', 'C') THEN
5306               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5307            END IF;
5308            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5309               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5310            END LOOP;
5311         ELSE
5312            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status );
5313         END IF;
5314 
5315         IF PG_DEBUG in ('Y', 'C') THEN
5316            msc_sch_wb.atp_debug('Schedule: NO_DATA_FOUND: Stmt = '||l_stmt);
5317         END IF;
5318         -- 1072816  need to do something to undo
5319 
5320         FOR i in 1..MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT LOOP
5321             IF PG_DEBUG in ('Y', 'C') THEN
5322                msc_sch_wb.atp_debug('Schedule: ' || 'in the loop, i:='||i);
5323             END IF;
5324             MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(MSC_ATP_PVT.G_PEGGING_FOR_SET(i), p_instance_id,
5325               l_plan_id, UNDO, MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(i),l_return_status);
5326         END LOOP;
5327 
5328         -- 2902265
5329         MSC_ATP_PROC.cleanup_set(p_instance_id,
5330                                  l_plan_id,
5331                                  l_fst_src_pegging_ids,
5332                                  l_demand_class_atp_flag);
5333 
5334         MSC_ATP_PVT.G_PEGGING_FOR_SET.DELETE;
5335         MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.DELETE;
5336         MSC_ATP_PVT.G_REQ_ATP_DATE.DELETE;
5337         MSC_ATP_PVT.G_REQ_DATE_QTY.DELETE;
5338          IF PG_DEBUG in ('Y', 'C') THEN
5339             msc_sch_wb.atp_debug('Schedule: ' || 'Do I come here 13');
5340            msc_sch_wb.atp_debug('Schedule: ' || 'after the fix to remove the data');
5341         END IF;
5342 
5343     -- Bug 1661545, if scheduling was unsuccessful, make sure that old demand record is
5344     -- preserved back, as it was updated to 0 in the begining in case of reschedule.
5345 
5346     IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5347         x_atp_table.Action(1) = DEMANDADD OR
5348         x_atp_table.Action(1) = DMDRSVADD) THEN
5349 
5350         IF PG_DEBUG in ('Y', 'C') THEN
5351            msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5352         END IF;
5353 
5354         -- Loop thru the whole set to check if any of the records had failed Scheduling
5355         -- Update demands to reset the quantity and date in such cases
5356         -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5357 
5358         --bug 2384224: un delete only those demands which are deleted
5359         IF PG_DEBUG in ('Y', 'C') THEN
5360            msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5361       	   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5362         END IF;
5363 
5364         -- rajjain single call to Undo_Delete_Row procedure
5365         --3720018,changes to refer all the records from l_delete_atp_rec
5366         MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5367             l_delete_atp_rec.del_plan_ids,
5368             p_instance_id,
5369             --subst
5370             l_delete_atp_rec.del_demand_ids,
5371             l_delete_atp_rec.del_inv_item_ids,
5372             l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5373             l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5374             l_time_phased_set,            -- For time_phased_atp
5375             -- CTO ODR and Simplified Pegging
5376             l_delete_atp_rec.del_atp_peg_items,
5377             l_delete_atp_rec.del_atp_peg_demands ,
5378             l_delete_atp_rec.del_atp_peg_supplies,
5379             l_delete_atp_rec.del_atp_peg_res_reqs,
5380             l_delete_atp_rec.del_demand_source_type, --cmro
5381             -- End CTO ODR and Simplified Pegging
5382             l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5383             l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5384             l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5385             l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5386             l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5387             l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5388             l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5389             l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5390             l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5391             l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
5392             l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
5393             );
5394 
5395         IF PG_DEBUG in ('Y', 'C') THEN
5396            msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
5397         END IF;
5398 
5399         /* --s_cto_rearch
5400         IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5401               MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5402                                                x_atp_table,
5403                                                MSC_ATP_CTO.FAIL);
5404         END IF;
5405         --e_cto_rearch
5406         */
5407 
5408     END IF;     -- Bug 1661545
5409 
5410 
5411     WHEN FND_API.G_EXC_ERROR THEN
5412         x_return_status := FND_API.G_RET_STS_ERROR;
5413 
5414         -- Resetting global variables for additional issue encountered while bug 3295831
5415         IF PG_DEBUG in ('Y', 'C') THEN
5416             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5417         END IF;
5418         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5419         -- ATP4drp re-set using original profile value
5420         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5421         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5422         -- End ATP4drp
5423         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5424 
5425         -- 2625800: krajan
5426         ---agilent arrival set bug
5427         IF l_arrival_count > 1 THEN
5428            IF PG_DEBUG in ('Y', 'C') THEN
5429               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5430            END IF;
5431            --for arrivalset l_atp_table contains only the record that we are processing
5432            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5433            ---first we add the record we have processed so far
5434            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5435            ---now we add the current reocrd
5436            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5437            --now we assign rest of the record.
5438            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5439            IF PG_DEBUG in ('Y', 'C') THEN
5440               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5441            END IF;
5442            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5443               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5444            END LOOP;
5445         END IF;
5446 
5447         -- Error Handling Changes
5448         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
5449                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
5450                         x_atp_table := p_atp_table;
5451                 ELSE
5452                         x_atp_table := l_atp_table;
5453                 END IF;
5454         END IF;
5455         FOR i IN 1..x_atp_table.Action.COUNT LOOP
5456                 --IF x_atp_table.error_code(i) IS NULL OR
5457                  --  x_atp_table.error_code(i) in (0,61,150) THEN
5458                   --      x_atp_table.Error_Code(i) := GROUPEL_ERROR;
5459                 --END IF;
5460                 -- Remove invalid SD recs.
5461                 BEGIN
5462                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
5463                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
5464                                 AND order_line_id = x_atp_table.identifier(i)
5465                                 AND parent_pegging_id is NULL
5466                                  AND record_type in (3,4);
5467                 EXCEPTION
5468                         WHEN others then
5469                                 l_tmp_pegging_id := -1; --Setting to error
5470                 END;
5471                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
5472                         IF PG_DEBUG in ('Y', 'C') THEN
5473                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
5474                         END IF;
5475                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
5476                                 l_plan_id, UNDO, 1,l_return_status);
5477                 END IF;
5478 
5479         END LOOP;
5480         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5481                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5482         END IF;
5483         -- Error Handling Changes End.
5484 
5485 -- INSERT HERE
5486         -- Error Handling changes - handling for rescheduling case
5487         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5488                 x_atp_table.Action(1) = DEMANDADD OR
5489                 x_atp_table.Action(1) = DMDRSVADD) THEN
5490 
5491                 IF PG_DEBUG in ('Y', 'C') THEN
5492                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5493                 END IF;
5494 
5495                 -- Loop thru the whole set to check if any of the records had failed Scheduling
5496                 -- Update demands to reset the quantity and date in such cases
5497                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5498 
5499                 --bug 2384224: un delete only those demands which are deleted
5500                 IF PG_DEBUG in ('Y', 'C') THEN
5501                    msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5502                    msc_sch_wb.atp_debug('Schedule: ' || 'single call to Undo_Delete_Row');
5503                END IF;
5504                -- rajjain single call to Undo_Delete_Row procedure
5505                --3720018,changes to refer all the records from l_delete_atp_rec
5506                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5507                     l_delete_atp_rec.del_plan_ids,
5508                     p_instance_id,
5509                     --subst
5510                     l_delete_atp_rec.del_demand_ids,
5511                     l_delete_atp_rec.del_inv_item_ids,
5512                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5513                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5514                     l_time_phased_set,            -- For time_phased_atp
5515                     -- CTO ODR and Simplified Pegging
5516                     l_delete_atp_rec.del_atp_peg_items,
5517                     l_delete_atp_rec.del_atp_peg_demands ,
5518                     l_delete_atp_rec.del_atp_peg_supplies,
5519                     l_delete_atp_rec.del_atp_peg_res_reqs,
5520                     l_delete_atp_rec.del_demand_source_type, --cmro
5521                     -- End CTO ODR and Simplified Pegging
5522                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5523                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5524                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5525                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5526                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5527                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5528                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5529                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5530                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5531                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
5532                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
5533                     );
5534 
5535                IF PG_DEBUG in ('Y', 'C') THEN
5536                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
5537                END IF;
5538 
5539                /* --s_cto_rearch
5540                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5541                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5542                                                       x_atp_table,
5543                                                       MSC_ATP_CTO.FAIL);
5544                END IF;
5545                --e_cto_rearch
5546                */
5547 
5548         END IF;
5549 
5550     -- Error Handling Changes
5551     -- New Exception Block
5552     -- Error Handling Insert krajan
5553     WHEN MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
5554 
5555         IF PG_DEBUG in ('Y', 'C') THEN
5556            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5557            msc_sch_wb.atp_debug('Schedule: Invalid Object Found. In it');
5558         END IF;
5559         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5560 
5561         -- Resetting global variables for additional issue encountered while bug 3295831
5562         IF PG_DEBUG in ('Y', 'C') THEN
5563             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5564         END IF;
5565         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5566         -- ATP4drp re-set using original profile value
5567         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5568         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5569         -- End ATP4drp
5570         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5571 
5572         -- 2625800: krajan
5573         ---agilent arrival set bug
5574         IF l_arrival_count > 1 THEN
5575            IF PG_DEBUG in ('Y', 'C') THEN
5576               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5577            END IF;
5578            --for arrivalset l_atp_table contains only the record that we are processing
5579            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5580            ---first we add the record we have processed so far
5581            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5582            ---now we add the current reocrd
5583            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5584            --now we assign rest of the record.
5585            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5586            IF PG_DEBUG in ('Y', 'C') THEN
5587               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5588            END IF;
5589            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5590               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5591            END LOOP;
5592         END IF;
5593 
5594         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
5595                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
5596                         x_atp_table := p_atp_table;
5597                 ELSE
5598                         x_atp_table := l_atp_table;
5599                 END IF;
5600         END IF;
5601         FOR i IN 1..x_atp_table.Action.COUNT LOOP
5602                 IF ((NVL(x_atp_table.Error_code(i),-1)) in (-1,0,61,150)) THEN
5603                         x_atp_table.Error_Code(i) := ATP_INVALID_OBJECTS; -- NO OBJECTS FOUND
5604                 END IF;
5605                  BEGIN
5606                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
5607                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
5608                                 AND order_line_id = x_atp_table.identifier(i)
5609                                 AND parent_pegging_id is NULL
5610                                  AND record_type in (3,4);
5611                 EXCEPTION
5612                         WHEN others then
5613                                 IF PG_DEBUG in ('Y', 'C') THEN
5614                                    msc_sch_wb.atp_debug('Schedule: ' || 'Select EXCEPTION KR :invalid obj');
5615                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5616                                 END IF;
5617                                 l_tmp_pegging_id := -1; --Setting to error
5618                 END;
5619                 IF PG_DEBUG in ('Y', 'C') THEN
5620                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
5621                 END IF;
5622                 IF (NVL(l_tmp_pegging_id, -1) <> -1) THEN
5623                         IF PG_DEBUG in ('Y', 'C') THEN
5624                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
5625                         END IF;
5626                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
5627                                 l_plan_id, UNDO, 1,l_return_status);
5628                 END IF;
5629         END LOOP;
5630 
5631         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5632             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5633         END IF;
5634 
5635         -- Error Handling changes - handling for rescheduling case
5636         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5637                 x_atp_table.Action(1) = DEMANDADD OR
5638                 x_atp_table.Action(1) = DMDRSVADD) THEN
5639 
5640                 IF PG_DEBUG in ('Y', 'C') THEN
5641                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5642                 END IF;
5643 
5644                 -- Loop thru the whole set to check if any of the records had failed Scheduling
5645                 -- Update demands to reset the quantity and date in such cases
5646                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5647 
5648                 --bug 2384224: un delete only those demands which are deleted
5649                 IF PG_DEBUG in ('Y', 'C') THEN
5650                    msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5651                	   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5652                 END IF;
5653                 -- rajjain single call to Undo_Delete_Row procedure
5654                 --3720018,changes to refer all the records from l_delete_atp_rec
5655                 MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5656                     l_delete_atp_rec.del_plan_ids,
5657                     p_instance_id,
5658                     --subst
5659                     l_delete_atp_rec.del_demand_ids,
5660                     l_delete_atp_rec.del_inv_item_ids,
5661                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5662                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5663                     l_time_phased_set,            -- For time_phased_atp
5664                     -- CTO ODR and Simplified Pegging
5665                     l_delete_atp_rec.del_atp_peg_items,
5666                     l_delete_atp_rec.del_atp_peg_demands ,
5667                     l_delete_atp_rec.del_atp_peg_supplies,
5668                     l_delete_atp_rec.del_atp_peg_res_reqs,
5669                     l_delete_atp_rec.del_demand_source_type, --cmro
5670                     -- End CTO ODR and Simplified Pegging
5671                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5672                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5673                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5674                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5675                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5676                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5677                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5678                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5679                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5680                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
5681                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
5682                     );
5683 
5684                 IF PG_DEBUG in ('Y', 'C') THEN
5685                    msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
5686                 END IF;
5687 
5688                 /*--s_cto_rearch
5689                 IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5690                       MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5691                                                        x_atp_table,
5692                                                        MSC_ATP_CTO.FAIL);
5693                 END IF;
5694                 --e_cto_rearch
5695                 */
5696         END IF;
5697         IF PG_DEBUG in ('Y', 'C') THEN
5698            msc_sch_wb.atp_debug('Schedule: Invalid object encountered in schedule');
5699         END IF;
5700 
5701     -- Added to handle calendar exceptions : ship_rec_cal
5702     WHEN NO_MATCHING_DATE_IN_CAL THEN
5703         IF PG_DEBUG in ('Y', 'C') THEN
5704            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5705            msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_DATE_IN_CAL');
5706         END IF;
5707         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5708 
5709         -- Resetting global variables for additional issue encountered while bug 3295831
5710         IF PG_DEBUG in ('Y', 'C') THEN
5711             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5712         END IF;
5713         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5714         -- ATP4drp re-set using original profile value
5715         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5716         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5717         -- End ATP4drp
5718         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5719 
5720         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5721             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5722         END IF;
5723 
5724         -- 2625800: krajan
5725         ---agilent arrival set bug
5726         IF l_arrival_count > 1 THEN
5727            IF PG_DEBUG in ('Y', 'C') THEN
5728               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5729            END IF;
5730            --for arrivalset l_atp_table contains only the record that we are processing
5731            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5732            ---first we add the record we have processed so far
5733            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5734            ---now we add the current reocrd
5735            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5736            --now we assign rest of the record.
5737            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5738            IF PG_DEBUG in ('Y', 'C') THEN
5739               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5740            END IF;
5741            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5742               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5743            END LOOP;
5744         END IF;
5745 
5746         -- Error Handling krajan
5747         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
5748                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
5749                         x_atp_table := p_atp_table;
5750                 ELSE
5751                         x_atp_table := l_atp_table;
5752                 END IF;
5753         END IF;
5754         FOR i IN 1..x_atp_table.Action.COUNT LOOP
5755                 IF x_atp_table.error_code(i) IS NULL OR
5756                    x_atp_table.error_code(i) in (0,61,150) THEN
5757                         x_atp_table.Error_Code(i) := NO_MATCHING_CAL_DATE;
5758                 END IF;
5759                 BEGIN
5760                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
5761                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
5762                                 AND order_line_id = x_atp_table.identifier(i)
5763                                 AND parent_pegging_id is NULL
5764                                  AND record_type in (3,4);
5765                 EXCEPTION
5766                         WHEN others then
5767                                 IF PG_DEBUG in ('Y', 'C') THEN
5768                                    msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_DATE_IN_CAL');
5769                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5770                                 END IF;
5771                                 l_tmp_pegging_id := -1; --Setting to error
5772                 END;
5773                 IF PG_DEBUG in ('Y', 'C') THEN
5774                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
5775                 END IF;
5776                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
5777                         IF PG_DEBUG in ('Y', 'C') THEN
5778                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
5779                         END IF;
5780                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
5781                                 l_plan_id, UNDO, 1,l_return_status);
5782                 END IF;
5783         END LOOP;
5784         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5785                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5786         END IF;
5787         -- Error Handling Changes End.
5788         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR  --3720018, commented for support of rescheduling in ODS
5789                 x_atp_table.Action(1) = DEMANDADD OR
5790                 x_atp_table.Action(1) = DMDRSVADD) THEN
5791 
5792                 IF PG_DEBUG in ('Y', 'C') THEN
5793                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5794                 END IF;
5795 
5796                 -- Loop thru the whole set to check if any of the records had failed Scheduling
5797                 -- Update demands to reset the quantity and date in such cases
5798                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5799 
5800                --bug 2384224: un delete only those demands which are deleted
5801                IF PG_DEBUG in ('Y', 'C') THEN
5802                   msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5803                   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5804                END IF;
5805                -- rajjain single call to Undo_Delete_Row procedure
5806                --3720018,changes to refer all the records from l_delete_atp_rec
5807                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5808                     l_delete_atp_rec.del_plan_ids,
5809                     p_instance_id,
5810                     --subst
5811                     l_delete_atp_rec.del_demand_ids,
5812                     l_delete_atp_rec.del_inv_item_ids,
5813                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5814                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5815                     l_time_phased_set,            -- For time_phased_atp
5816                     -- CTO ODR and Simplified Pegging
5817                     l_delete_atp_rec.del_atp_peg_items,
5818                     l_delete_atp_rec.del_atp_peg_demands ,
5819                     l_delete_atp_rec.del_atp_peg_supplies,
5820                     l_delete_atp_rec.del_atp_peg_res_reqs,
5821                     l_delete_atp_rec.del_demand_source_type, --cmro
5822                     -- End CTO ODR and Simplified Pegging
5823                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5824                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5825                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5826                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5827                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
5828                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
5829                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
5830                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5831                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
5832                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
5833                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
5834                     );
5835 
5836                IF PG_DEBUG in ('Y', 'C') THEN
5837                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
5838                END IF;
5839 
5840                /* --s_cto_rearch
5841                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
5842                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
5843                                                       x_atp_table,
5844                                                       MSC_ATP_CTO.FAIL);
5845                END IF;
5846                --e_cto_rearch
5847                */
5848 
5849         END IF;
5850 
5851     WHEN OTHERS THEN
5852         IF PG_DEBUG in ('Y', 'C') THEN
5853            msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5854         END IF;
5855         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5856 
5857         -- Resetting global variables for additional issue encountered while bug 3295831
5858         IF PG_DEBUG in ('Y', 'C') THEN
5859             msc_sch_wb.atp_debug('Schedule: ' || 'Resetting G_INV_CTP and G_ALLOCATED_ATP' );
5860         END IF;
5861         MSC_ATP_PVT.G_INV_CTP := NVL(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
5862         -- ATP4drp re-set using original profile value
5863         --MSC_ATP_PVT.G_ALLOCATED_ATP := NVL(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
5864         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
5865         -- End ATP4drp
5866         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_allocation_method, MSC_ATP_PVT.G_ALLOCATION_METHOD);
5867 
5868         /* Check if this is actually coming from a calendar routine*/
5869         l_encoded_text := fnd_message.GET_ENCODED;
5870         IF l_encoded_text IS NULL THEN
5871                 l_msg_app := NULL;
5872                 l_msg_name := NULL;
5873         ELSE
5874                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
5875                 IF PG_DEBUG in ('Y', 'C') THEN
5876                    msc_sch_wb.atp_debug('Schedule: ' || 'l_msg_name := ' || l_msg_name);
5877                 END IF;
5878         END IF;
5879         IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
5880                 l_error_code := NO_MATCHING_CAL_DATE;
5881            IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
5882                    msc_sch_wb.atp_debug('Schedule: ' || 'NO_MATCHING_CAL_DATE ' );
5883            END IF;
5884         ELSE
5885                 l_error_code := ATP_PROCESSING_ERROR;
5886            IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
5887                    msc_sch_wb.atp_debug('Schedule: ' || 'ATP_PROCESSING_ERROR ' );
5888            END IF;
5889         END IF;
5890 
5891         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5892             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5893         END IF;
5894 
5895         -- 2625800: krajan
5896         ---agilent arrival set bug
5897         IF l_arrival_count > 1 THEN
5898            IF PG_DEBUG in ('Y', 'C') THEN
5899               msc_sch_wb.atp_debug ('Schedule: ' || 'Inside arrival set error processing');
5900            END IF;
5901            --for arrivalset l_atp_table contains only the record that we are processing
5902            --- so we need to make sure that we add all the records in arrival set to x_atp_tbale
5903            ---first we add the record we have processed so far
5904            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_temp_arr_atp_table, x_atp_table, l_return_status);
5905            ---now we add the current reocrd
5906            MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_table, x_atp_table, l_return_status);
5907            --now we assign rest of the record.
5908            l_count := l_temp_arr_atp_table.inventory_item_id.count + 2;
5909            IF PG_DEBUG in ('Y', 'C') THEN
5910               msc_sch_wb.atp_debug('Schedule: ' || 'l_count := ' || l_count);
5911            END IF;
5912            FOR i in l_count..p_atp_table.inventory_item_id.count LOOP
5913               MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table, i, x_atp_table, l_return_status);
5914            END LOOP;
5915         END IF;
5916 
5917         -- Error Handling krajan
5918         IF (x_atp_table.Inventory_item_id.COUNT = 0) THEN
5919                 IF (l_atp_table.Inventory_item_id.COUNT = 0) THEN
5920                         x_atp_table := p_atp_table;
5921                 ELSE
5922                         x_atp_table := l_atp_table;
5923                 END IF;
5924         END IF;
5925 
5926         FOR i IN 1..x_atp_table.Action.COUNT LOOP
5927                 IF x_atp_table.error_code(i) IS NULL OR
5928                    x_atp_table.error_code(i) in (0,61,150) THEN
5929                         x_atp_table.Error_Code(i) := l_error_code;
5930                 END IF;
5931                 BEGIN
5932                         SELECT pegging_id INTO l_tmp_pegging_id FROM mrp_atp_details_temp
5933                                 WHERE session_id = MSC_ATP_PVT.G_SESSION_ID
5934                                 AND order_line_id = x_atp_table.identifier(i)
5935                                 AND parent_pegging_id is NULL
5936                                  AND record_type in (3,4);
5937                 EXCEPTION
5938                         WHEN others then
5939                                 IF PG_DEBUG in ('Y', 'C') THEN
5940                                    msc_sch_wb.atp_debug('Schedule: ' || 'Select EXCEPTION KR: others ');
5941                                    msc_sch_wb.atp_debug('Schedule: ' || 'error := ' || sqlerrm);
5942                                 END IF;
5943                                 l_tmp_pegging_id := -1; --Setting to error
5944                 END;
5945                 IF PG_DEBUG in ('Y', 'C') THEN
5946                    msc_sch_wb.atp_debug('Schedule: ' || 'l_tmp_pegging_id is ' || l_tmp_pegging_id);
5947                 END IF;
5948                 IF (NVL(l_tmp_pegging_id,-1) <> -1) THEN
5949                         IF PG_DEBUG in ('Y', 'C') THEN
5950                            msc_sch_wb.atp_debug('Schedule: ' || 'Now deleting- calling remove invalid SD REC');
5951                         END IF;
5952                         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_tmp_pegging_id, null,
5953                                 l_plan_id, UNDO, 1,l_return_status);
5954                 END IF;
5955         END LOOP;
5956         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5957                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Schedule');
5958         END IF;
5959         -- Error Handling Changes End.
5960         IF /*(MSC_ATP_PVT.G_INV_CTP = 4) AND */ (x_atp_table.Action(1) = DEMANDMODIFY OR --3720018, commented for support of rescheduling in ODS
5961                 x_atp_table.Action(1) = DEMANDADD OR
5962                 x_atp_table.Action(1) = DMDRSVADD) THEN
5963 
5964                 IF PG_DEBUG in ('Y', 'C') THEN
5965                    msc_sch_wb.atp_debug('Schedule: ' || 'check if need to reset demands when scheduling failed');
5966                 END IF;
5967 
5968                 -- Loop thru the whole set to check if any of the records had failed Scheduling
5969                 -- Update demands to reset the quantity and date in such cases
5970                 -- Not needed if error_code is ALLSUCCESS or ATP_NOT_APPL.
5971 
5972                --bug 2384224: un delete only those demands which are deleted
5973                IF PG_DEBUG in ('Y', 'C') THEN
5974                   msc_sch_wb.atp_debug('Schedule: ' || 'before resetting the demands when scheduling failed');
5975                   msc_sch_wb.atp_debug('Schedule: ' || 'rajjain - Single call to Undo_Delete_Row');
5976                END IF;
5977                -- rajjain single call to Undo_Delete_Row procedure
5978                --3720018,changes to refer all the records from l_delete_atp_rec
5979                MSC_ATP_DB_UTILS.Undo_Delete_Row(l_delete_atp_rec.del_identifiers,
5980                     l_delete_atp_rec.del_plan_ids,
5981                     p_instance_id,
5982                     --subst
5983                     l_delete_atp_rec.del_demand_ids,
5984                     l_delete_atp_rec.del_inv_item_ids,
5985                     l_delete_atp_rec.del_copy_demand_ids,        -- For summary enhancement
5986                     l_delete_atp_rec.del_copy_demand_plan_ids,   -- For summary enhancement
5987                     l_time_phased_set,            -- For time_phased_atp
5988                     -- CTO ODR and Simplified Pegging
5989                     l_delete_atp_rec.del_atp_peg_items,
5990                     l_delete_atp_rec.del_atp_peg_demands ,
5991                     l_delete_atp_rec.del_atp_peg_supplies,
5992                     l_delete_atp_rec.del_atp_peg_res_reqs,
5993                     l_delete_atp_rec.del_demand_source_type, --cmro
5994                     -- End CTO ODR and Simplified Pegging
5995                     l_delete_atp_rec.atp_peg_demands_plan_ids,  --Bug 3629191
5996                     l_delete_atp_rec.atp_peg_supplies_plan_ids, --Bug 3629191
5997                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,  --Bug 3629191
5998                     l_delete_atp_rec.del_ods_demand_ids, --3720018, added for support of rescheduling in ODS
5999                     l_delete_atp_rec.del_ods_inv_item_ids, --3720018, added for support of rescheduling in ODS
6000                     l_delete_atp_rec.del_ods_demand_src_type, --3720018, added for support of rescheduling in ODS
6001                     l_delete_atp_rec.del_ods_cto_demand_ids, --3720018, added for support of rescheduling in ODS
6002                     l_delete_atp_rec.del_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
6003                     l_delete_atp_rec.del_ods_cto_dem_src_type, --3720018, added for support of rescheduling in ODS
6004                     l_delete_atp_rec.del_ods_atp_refresh_no, --3720018, added for support of rescheduling in ODS
6005                     l_delete_atp_rec.del_ods_cto_atp_refresh_no --3720018, added for support of rescheduling in ODS
6006                     );
6007 
6008                IF PG_DEBUG in ('Y', 'C') THEN
6009                   msc_sch_wb.atp_debug('Schedule: ' || 'after resetting the demands when scheduling failed');
6010                END IF;
6011 
6012                /* --s_cto_rearch
6013                IF MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET =1 and NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' THEN
6014                      MSC_ATP_CTO.Maintain_OS_Sourcing(p_instance_id,
6015                                                       x_atp_table,
6016                                                       MSC_ATP_CTO.FAIL);
6017                END IF;
6018                --e_cto_rearch
6019                */
6020 
6021         END IF;
6022 
6023         -- Commented on 9/13/2001 by ngoel as this was causing subscript beyond count
6024 	-- since exception wasn't trapped until Call_ATP_No_Commit.
6025 --        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6026 END Schedule;
6027 
6028 
6029 PROCEDURE ATP_Check (p_atp_record     		IN OUT  NoCopy MRP_ATP_PVT.AtpRec,
6030                      p_plan_id             	IN   NUMBER,
6031                      p_level               	IN   NUMBER,
6032                      p_scenario_id         	IN   NUMBER,
6033                      p_search              	IN   NUMBER,
6034                      p_refresh_number      	IN   NUMBER,
6035                      p_parent_pegging_id   	IN   NUMBER,
6036                      p_assign_set_id       	IN   NUMBER,
6037                      x_atp_period          	OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
6038                      x_atp_supply_demand   	OUT NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
6039                      x_return_status       	OUT  NoCopy VARCHAR2,
6040                      p_pre_processing_lead_time   IN NUMBER :=0
6041 )
6042 IS
6043 
6044 l_delivery_lead_time 		NUMBER;
6045 l_atp_flag 			VARCHAR2(1);
6046 l_atp_comp_flag			VARCHAR2(1);
6047 l_return_status 		VARCHAR2(10);
6048 l_requested_date_quantity	NUMBER;
6049 l_requested_quantity		NUMBER; -- 2621270
6050 l_sub_requested_date_quantity   NUMBER := 0.0;
6051 l_stealing_requested_date_qty   NUMBER := 0.0;
6052 l_stealing_qty                  NUMBER := 0.0;
6053 l_from_location_id		NUMBER;
6054 l_requested_ship_date		DATE;
6055 --Added by avjain for atp impact due to Plan by request date
6056 l_original_req_ship_date	DATE;   -- when item was requested to be shipped
6057 l_ship_method			VARCHAR2(30);
6058 l_to_location_id		NUMBER;
6059 l_atp_date_this_level		DATE;
6060 l_atp_date_quantity_this_level	NUMBER;
6061 l_sources			MRP_ATP_PVT.Atp_Source_Typ;
6062 i                               PLS_INTEGER;
6063 j				PLS_INTEGER;
6064 l_atp_rec			MRP_ATP_PVT.AtpRec;
6065 l_net_demand			NUMBER := 0; --5478124
6066 l_sched_status			BOOLEAN;
6067 l_atp_insert_rec		MRP_ATP_PVT.AtpRec;
6068 l_atp_period                    MRP_ATP_PUB.ATP_Period_Typ;
6069 l_atp_period_first              MRP_ATP_PUB.ATP_Period_Typ;
6070 l_atp_supply_demand             MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6071 l_res_atp_period                MRP_ATP_PUB.ATP_Period_Typ;
6072 l_res_atp_supply_demand         MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6073 l_comp_atp_period               MRP_ATP_PUB.ATP_Period_Typ;
6074 l_comp_atp_supply_demand        MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6075 l_tb_atp_period                 MRP_ATP_PUB.ATP_Period_Typ;
6076 l_tb_atp_supply_demand          MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6077 l_future_atp_period             MRP_ATP_PUB.ATP_Period_Typ;
6078 l_future_atp_supply_demand      MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6079 
6080 l_inv_item_id                   NUMBER;
6081 
6082 temp				NUMBER;
6083 g_atp_record                    MRP_ATP_PVT.AtpRec;
6084 l_pegging_rec			mrp_atp_details_temp%ROWTYPE;
6085 l_pegging_id                    NUMBER;
6086 l_parent_pegging_id             NUMBER;
6087 l_atp_pegging_id                NUMBER;
6088 l_future_pegging_id             NUMBER;
6089 l_demand_id                     NUMBER;
6090 l_quantity_ordered		NUMBER;
6091 l_quantity_uom			VARCHAR2(3);
6092 l_transaction_id                NUMBER;
6093 l_avail_assembly_qty		NUMBER;
6094 l_comp_atp_date			DATE;
6095 l_res_atp_date			DATE;
6096 l_conversion_rate               NUMBER;
6097 l_sysdate                       DATE;
6098 l_ship_date                     DATE;
6099 l_plan_id                       NUMBER;
6100 l_assign_set_id                 NUMBER;
6101 l_peg_qty                       NUMBER;
6102 l_inv_item_name                 VARCHAR2(250); --bug 2246200
6103 l_org_code                      VARCHAR2(7);
6104 l_supplier_name                 VARCHAR2(80);
6105 l_supplier_site_name            VARCHAR2(80);
6106 l_temp_date                     DATE;   -- Bug 1449555
6107 l_cto_bom                       NUMBER := 0;
6108 
6109 l_coproducts_flag 		VARCHAR2(1) := 'Y'; -- default to Yes
6110 l_make_buy_cd			NUMBER;
6111 l_processing_lead_time 		NUMBER;
6112 l_pre_processing_date		DATE;
6113 l_mso_lead_time_factor		NUMBER;
6114 l_demand_class_flag             NUMBER :=0;
6115 l_inventory_item_id             number;
6116 l_coprod_pegging_id             NUMBER;
6117 temp_sd_qty 			NUMBER;
6118 l_summary_flag			VARCHAR2(1);
6119 l_old_summary_flag              VARCHAR2(1);
6120 l_new_summary_flag              VARCHAR2(1);
6121 l_site_id			NUMBER;
6122 l_request_item_id               NUMBER;
6123 l_pre_pro_lt                    NUMBER;
6124 l_fixed_lt			NUMBER;
6125 l_variable_lt			NUMBER;
6126 l_ptf_flag			NUMBER;
6127 l_ptf_date                      DATE;
6128 l_net_processing_lt             NUMBER;
6129 l_process_lt                    NUMBER;
6130 l_order_date                    DATE;
6131 l_start_date                    DATE;
6132 l_post_pro_lt                   NUMBER;
6133 l_dock_date			DATE;
6134 l_atp_request_date              DATE;
6135 l_atp_ship_date                 DATE;
6136 l_parent_ptf_date               DATE;
6137 l_ptf_date_for_child            DATE;   -- Bug 3782472
6138 l_req_date_qty                  NUMBER;
6139 l_item_attribute_rec            MSC_ATP_PVT.item_attribute_rec;   -- Obsoleted
6140 l_substitution_window           NUMBER;
6141 l_atp_pegging_tab		MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
6142 -- Only specific to this procedure
6143 PF_ITEM_INFO_REC                 MSC_ATP_PVT.item_attribute_rec; -- Only for PF info
6144 C_ITEM_INFO_REC                  MSC_ATP_PVT.item_attribute_rec;  -- Copy of the global.
6145 -- Added for bug 2392456
6146 l_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
6147 
6148 --diag_atp
6149 L_CONSTRAINT_FLAG               NUMBER;
6150 L_PTF_DUE_DATE                  DATE;
6151 L_LT_DUE_DATE                   DATE;
6152 L_RES_AVAIL_QTY                 NUMBER;
6153 L_SUPPLIER_START_DATE           DATE;
6154 L_ROUNDING_CONTROL_FLAG         number;
6155 L_UNIT_WEIGHT                   number;
6156 L_WEIGHT_UOM                    varchar2(3);
6157 L_UNIT_VOLUME                   number;
6158 L_VOLUME_UOM                    varchar2(3);
6159 L_DEST_INV_ITEM_ID              number;
6160 L_GET_MAT_IN_REC                MSC_ATP_REQ.get_mat_in_rec;
6161 l_get_mat_out_rec               MSC_ATP_REQ.get_mat_out_rec;
6162 l_allocation_rule_name          varchar2(30);
6163 
6164 l_update_PO_mode		NUMBER := MSC_ATP_PVT.UNDO;
6165 l_atp_quantity_this_level_fwd	NUMBER;
6166 l_last_good_pegging_id		NUMBER;
6167 l_have_make_buy_parent		NUMBER := G_HAVE_MAKE_BUY_PARENT;
6168 l_subst_existing_PO_qty		NUMBER;
6169 
6170 --- Enhance CTO Phase 1 Req #17
6171 -- New variable to deal with forward stealing for CTO components.
6172 l_parent_peg_forward            NUMBER := 0;
6173 -- End Enhance CTO Phase 1 Req #17
6174 
6175 -- for nocopy changes
6176 l_res_demand			NUMBER;
6177 --  Supplier Capacity and Lead Time (SCLT) changes
6178 l_sup_cap_cum_start            NUMBER;
6179 
6180 -- dsting 2764213
6181 l_planned_sources             NUMBER := 0;
6182 
6183 --2798667
6184 --this variable contains the ship date of the item from the org
6185 -- For ATO components, this variable contains availability date in org + atp lead time
6186 -- for other itmes, this variable contains the availabilty date.
6187 l_atp_lt_offset_ship_date                date;
6188 
6189 -- rajjain 02/19/2003 Bug 2788302 Begin
6190 l_process_seq_id                NUMBER;
6191 l_src_org_id                    NUMBER;
6192 -- rajjain 02/19/2003 Bug 2788302 End
6193 
6194 -- dsting 2754446
6195 l_PO_qty			NUMBER;
6196 l_prim_uom_avail_qty            NUMBER;
6197 l_prim_uom_req_date_qty         NUMBER;
6198 
6199 -- 2897278
6200 l_avail_PO_qty                  number;
6201 
6202 --(ssurendr) Bug 2865389 OPM fix.
6203 l_opm_start_date                DATE;
6204 l_routing_seq_id                NUMBER;
6205 l_bill_seq_id                   NUMBER;
6206 
6207 --4570421
6208 l_op_seq_id                     NUMBER;
6209 
6210 --s_cto_rearch
6211 l_item_sourcing_info_rec       MSC_ATP_CTO.Item_Sourcing_Info_Rec;
6212 l_null_item_sourcing_info_rec       MSC_ATP_CTO.Item_Sourcing_Info_Rec;
6213 l_get_comp_info_rec            MSC_ATP_REQ.get_comp_info_rec;
6214 --l_get_sup_info_rec             MSC_ATP_PVT.Supplier_Info_rec;
6215 l_item_info_rec                MSC_ATP_PVT.item_attribute_rec;
6216 l_atp_rule_name                varchar2(80);
6217 l_enforce_model_lt             VARCHAR2(1);
6218 
6219 -- time_phased_atp
6220 l_family_item_id                NUMBER;
6221 l_pf_atp                        VARCHAR2(1) := 'N';
6222 l_time_phased_atp               VARCHAR2(1) := 'N';
6223 l_mem_stealing_qty              NUMBER;
6224 l_pf_stealing_qty               NUMBER;
6225 l_sub_qty_before_atf            NUMBER;
6226 l_sub_qty_after_atf             NUMBER;
6227 l_bucketed_demands_rec          MSC_ATP_PF.Bucketed_Demands_Rec;
6228 l_mat_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
6229 l_atf_date_qty                  NUMBER;
6230 l_item_to_use                   NUMBER;
6231 l_used_available_quantity       NUMBER; --bug3409973
6232 l_used_available_quantity_fwd   NUMBER; --bug3409973
6233 
6234 l_ato_date                      date;
6235 l_check_model_capacity          NUMBER := 2;
6236 l_sr_inv_item_id                NUMBER;
6237 
6238 l_bkwd_pass_atf_date_qty         NUMBER; --bug3397904
6239 
6240 -- variables added for ship_rec_cal
6241 l_sup_atp_info_rec              MSC_ATP_REQ.Atp_Info_Rec;
6242 l_sysdate_seq_num	        NUMBER;
6243 l_sup_cap_type		        NUMBER;
6244 l_intransit_lt                  NUMBER;
6245 l_planned_order_date            DATE;
6246 l_new_dock_date                 DATE;
6247 l_new_ship_date                 DATE;
6248 l_sugg_start_date               DATE;   -- Bug 3241766
6249 l_encoded_text                  varchar2(4000);
6250 l_msg_app                       varchar2(50);
6251 l_msg_name                      varchar2(30);
6252 
6253 /* Enforce Pur LT */
6254 l_trunc_sysdate                 DATE := TRUNC(sysdate);
6255 l_trunc_started                 NUMBER := 2;
6256 -- l_supplier_request_date         DATE; -- Bug 3828494 (done with 3828469): Not required anymore
6257 l_sys_next_date                 DATE; --bug3821358
6258 
6259 --Bug 3821358
6260 l_store_dock_date               DATE;
6261 l_store_ship_date               DATE;
6262 l_store_start_date              DATE;
6263 l_store_order_date              DATE;
6264 l_store_due_date                DATE;
6265 l_store_g_ptf_date              DATE;
6266 
6267 -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
6268 l_ptf_enabled                   NUMBER;
6269 
6270 --2814895
6271 l_partner_type    NUMBER := 2;
6272 l_party_site_id   NUMBER := NULL;
6273 l_order_line_id   NUMBER := NULL;
6274 l_location_id     NUMBER := NULL;
6275 
6276 -- ATP4drp Declare a variable for creating Planned Arrival.
6277 l_supply_rec                    MSC_ATP_DB_UTILS.supply_rec_typ;
6278 -- End ATP4drp
6279 
6280 --bug 4394904
6281 l_infinite_time_fence          DATE;
6282 l_substitutes_rec  MSC_ATP_REQ.get_subs_out_rec;-- 4902658/5216528
6283 l_temp_net_demand  NUMBER; --4902658 temporary variable to get qty during ctp 5216528
6284 l_atp_ctp_qty NUMBER; --4902658 contains atp + ctp qty 5216528
6285 l_tmp_parent_pegging_id NUMBER; --5216528
6286 l_net_demand_qty NUMBER;	--5598066
6287 
6288 BEGIN
6289     IF PG_DEBUG in ('Y', 'C') THEN
6290        msc_sch_wb.atp_debug('**********Begin ATP_Check Procedure************');
6291     END IF;
6292 
6293     -- initialize API return status to success
6294     x_return_status := FND_API.G_RET_STS_SUCCESS;
6295 
6296     -- initialize the error_code to ALLSUCCESS
6297     p_atp_record.error_code := ALLSUCCESS;
6298         IF PG_DEBUG in ('Y', 'C') THEN
6299            msc_sch_wb.atp_debug('in atp_check');
6300         END IF;
6301 
6302     -- print the record
6303 IF PG_DEBUG in ('Y', 'C') THEN
6304    msc_sch_wb.atp_debug('ATP_Check: ' || '********** INPUT DATA: p_atp_record **********');
6305    msc_sch_wb.atp_debug('ATP_Check: ' || 'Inventory_Item_Id:' || to_char(p_atp_record.Inventory_Item_Id) );
6306    msc_sch_wb.atp_debug('ATP_Check: ' || 'request_item_id:' || to_char(p_atp_record.request_item_id) );
6307    msc_sch_wb.atp_debug('ATP_Check: ' || 'organization_id:' || to_char(p_atp_record.organization_id) );
6308    msc_sch_wb.atp_debug('ATP_Check: ' || 'Quantity_Ordered:' || to_char(p_atp_record.Quantity_Ordered) );
6309    msc_sch_wb.atp_debug('ATP_Check: ' || 'Quantity_UOM:' || p_atp_record.Quantity_UOM );
6310    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Ship_Date:' || to_char(p_atp_record.Requested_Ship_Date) );
6311    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Arrival_Date:' || to_char(p_atp_record.Requested_Arrival_Date) );
6312    msc_sch_wb.atp_debug('ATP_Check: ' || 'Latest_Acceptable_Date:' || to_char(p_atp_record.Latest_Acceptable_Date) );
6313    msc_sch_wb.atp_debug('ATP_Check: ' || 'Delivery_Lead_Time:' || to_char(p_atp_record.Delivery_Lead_Time) );
6314    msc_sch_wb.atp_debug('ATP_Check: ' || 'Freight_Carrier:' || p_atp_record.Freight_Carrier );
6315    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_Method:' || p_atp_record.Ship_Method );
6316    msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand_Class:' || p_atp_record.Demand_Class );
6317    msc_sch_wb.atp_debug('ATP_Check: ' || 'Override_Flag:' || p_atp_record.Override_Flag );
6318    msc_sch_wb.atp_debug('ATP_Check: ' || 'Action:' || to_char(p_atp_record.Action) );
6319    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_Date:' || to_char(p_atp_record.Ship_Date) );
6320    msc_sch_wb.atp_debug('ATP_Check: ' || 'Available_Quantity:' || to_char(p_atp_record.Available_Quantity) );
6321    msc_sch_wb.atp_debug('ATP_Check: ' || 'Used_available_Quantity:' || to_char(p_atp_record.used_available_quantity) ); --bug3409973
6322    msc_sch_wb.atp_debug('ATP_Check: ' || 'Requested_Date_Quantity:' || to_char(p_atp_record.Requested_Date_Quantity) );
6323    msc_sch_wb.atp_debug('ATP_Check: ' || 'supplier_id:' || to_char(p_atp_record.supplier_id) );
6324    msc_sch_wb.atp_debug('ATP_Check: ' || 'supplier_site_id:' || to_char(p_atp_record.supplier_site_id) );
6325    msc_sch_wb.atp_debug('ATP_Check: ' || 'Insert_Flag:' || to_char(p_atp_record.Insert_Flag) );
6326    msc_sch_wb.atp_debug('ATP_Check: ' || 'Error_Code:' || to_char(p_atp_record.Error_Code) );
6327    msc_sch_wb.atp_debug('ATP_Check: ' || 'Order_Number:' || to_char(p_atp_record.Order_Number) );
6328    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
6329    msc_sch_wb.atp_debug('ATP_Check: ' || 'original_item_flag := ' || p_atp_record.original_item_flag);
6330    -- 2462661 : krajan
6331    msc_sch_wb.atp_debug('ATP_Check: ' || 'SRC_ATP_FLAG := ' || p_atp_record.src_atp_flag);
6332    msc_sch_wb.atp_debug('ATP_Check: ' || 'Line Id := ' ||  p_atp_record.demand_source_line);
6333    msc_sch_wb.atp_debug('ATP_Check: ' || 'Top_Model_line_id := ' || p_atp_record.Top_Model_line_id);
6334    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATO_Parent_Model_Line_Id := ' || p_atp_record.ATO_Parent_Model_Line_Id);
6335    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATO_Model_Line_Id := ' || p_atp_record.ATO_Model_Line_Id);
6336    msc_sch_wb.atp_debug('ATP_Check: ' || 'Parent_line_id := ' || p_atp_record.Parent_line_id);
6337    msc_sch_wb.atp_debug('ATP_Check: ' || 'wip_supply_type := ' || p_atp_record.wip_supply_type);
6338    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_atp_flag := ' || p_atp_record.parent_atp_flag);
6339    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_atp_comp_flag := ' || p_atp_record.parent_atp_comp_flag);
6340    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_repl_order_flag := ' || p_atp_record.parent_repl_order_flag);
6341    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_bom_item_type := ' || p_atp_record.parent_bom_item_type);
6342    msc_sch_wb.atp_debug('ATP_Check: ' || 'mand_comp_flag := ' || p_atp_record.mand_comp_flag);
6343    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent_so_quantity := ' || p_atp_record.parent_so_quantity);
6344    msc_sch_wb.atp_debug('ATP_Check: ' || 'base_model_id := ' || p_atp_record.base_model_id);
6345    msc_sch_wb.atp_debug('ATP_Check: ' || 'rep_ord_flag := ' || p_atp_record.rep_ord_flag);
6346    msc_sch_wb.atp_debug('ATP_Check: ' || 'bom_item_type := '|| p_atp_record.bom_item_type);
6347    msc_sch_wb.atp_debug('ATP_Check: ' || 'parent item id := ' || p_atp_record.parent_item_id);
6348    -- ship_rec_cal
6349    msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code := ' || p_atp_record.receiving_cal_code);
6350    msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code := ' || p_atp_record.intransit_cal_code);
6351    msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code := ' || p_atp_record.shipping_cal_code);
6352    msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code := ' || p_atp_record.manufacturing_cal_code);
6353    msc_sch_wb.atp_debug('ATP_Check: ' || 'demand_source_type := ' || p_atp_record.demand_source_type);--cmro
6354    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
6355    msc_sch_wb.atp_debug('ATP_Check: ' || 'bill_seq_id := ' || p_atp_record.bill_seq_id); --4741012
6356    msc_sch_wb.atp_debug('ATP_Check: ' || 'after printing the values');
6357 END IF;
6358 
6359     -- dsting
6360     p_atp_record.children_type := NO_MAKE_BUY_CHILDREN;
6361 
6362     -- check if one and only one of the requested_ship_date and
6363     -- requested_arrival_date is provided
6364 
6365     IF ((p_atp_record.requested_ship_date IS NULL) and
6366         (p_atp_record.requested_arrival_date IS NULL)) THEN
6367 
6368         p_atp_record.error_code := NOREQ_DATE;
6369         RAISE FND_API.G_EXC_ERROR;
6370 
6371     ELSIF ((p_atp_record.requested_ship_date IS NOT NULL) and
6372         (p_atp_record.requested_arrival_date IS NOT NULL)) THEN
6373 
6374         p_atp_record.error_code := ATP_MULTI_REQ_DATES;
6375         RAISE FND_API.G_EXC_ERROR;
6376     END IF;
6377     --bug3821358
6378     l_sys_next_date := MSC_CALENDAR.NEXT_WORK_DAY(p_atp_record.manufacturing_cal_code, -- use OMC
6379                                                   p_atp_record.instance_id,
6380                                                   l_trunc_sysdate);
6381     IF PG_DEBUG in ('Y', 'C') THEN
6382        msc_sch_wb.atp_debug('ATP_Check: Sys Next Date is: ' || l_sys_next_date);
6383     END IF;
6384     /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
6385        Reset global variable*/
6386     MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'N';
6387 
6388     IF (NVL(p_atp_record.supplier_id, -1) = -1) THEN
6389         -- doing atp check in an inventory org
6390 
6391         IF PG_DEBUG in ('Y', 'C') THEN
6392            msc_sch_wb.atp_debug('ATP_Check: ' || 'do atp check at an inventory org');
6393         END IF;
6394 
6395         -- krajan : 2462661 : post fix
6396         -- set the global variable to null
6397         MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := null;
6398 
6399         /* time_phased_atp changes begin
6400            In PDS Case, find if this is time phased atp scenario*/
6401         l_request_item_id       := nvl(p_atp_record.request_item_id, p_atp_record.inventory_item_id); --do I really need this??
6402         l_family_item_id        := p_atp_record.inventory_item_id;
6403 
6404         IF (G_INV_CTP = 4) and (l_request_item_id <> l_family_item_id) THEN
6405 
6406             IF p_atp_record.atf_date is not null THEN
6407                 l_time_phased_atp := 'Y';
6408                 l_pf_atp := 'N';
6409 
6410                 /* To support new logic for dependent demands allocation in time phased PF rule based AATP scenarios
6411                    Set global variable too. This is used in Get_Item_Demand_Alloc_Percent function*/
6412                 MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED := 'Y';
6413 
6414                 -- CTO_PF_PRJ changes for CTO PF Cross Project Impacts
6415                 -- If dealing with Time Phased ATP Item set the global for CTO_PF
6416                 MSC_ATP_PVT.G_CTO_PF_ATP := 'Y';
6417 
6418                 IF PG_DEBUG in ('Y', 'C') THEN
6419                     msc_sch_wb.atp_debug('ATP_Check: ' || '*** Perform Time Phased ATP for this item *** ');
6420                     msc_sch_wb.atp_debug('ATP_Check: ' || 'ATF Date := ' || p_atp_record.atf_date);
6421                     msc_sch_wb.atp_debug('ATP_Check: Re-set G_CTO_PF_ATP to ' ||
6422                                                        MSC_ATP_PVT.G_CTO_PF_ATP);
6423                 END IF;
6424                 -- CTO_PF_PRJ changes for CTO PF Cross Project Impacts
6425             ELSE
6426                 l_pf_atp := 'Y';
6427                 l_time_phased_atp := 'N';
6428                 l_request_item_id := p_atp_record.inventory_item_id;
6429 
6430                 IF PG_DEBUG in ('Y', 'C') THEN
6431                     msc_sch_wb.atp_debug('ATP_Check: ' || '*** Perform Product Family(non-time phased) ATP for this item *** ');
6432                 END IF;
6433             END IF;
6434         END IF;
6435 
6436         IF PG_DEBUG in ('Y', 'C') THEN
6437             msc_sch_wb.atp_debug('ATP_Check: ' || 'G_TIME_PHASED_PF_ENABLED := ' || MSC_ATP_PVT.G_TIME_PHASED_PF_ENABLED);
6438         END IF;
6439 
6440         /* Now we store member item's info in G_ITEM_INFO_REC (C_ITEM_INFO_REC)
6441            PF_ITEM_INFO_REC is used to store family item's info*/
6442         --s_cto_rearch
6443         l_item_info_rec.parent_repl_ord_flag := p_atp_record.parent_repl_order_flag;
6444         l_item_info_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
6445         l_item_info_rec.parent_atp_flag := p_atp_record.parent_atp_flag;
6446         l_item_info_rec.parent_comp_flag := p_atp_record.parent_atp_comp_flag;
6447         l_item_info_rec.parent_pegging_id := p_parent_pegging_id;
6448         l_item_info_rec.parent_item_id :=  p_atp_record.parent_item_id;
6449         IF NVL(p_atp_record.parent_item_id, -1) = p_atp_record.inventory_item_id THEN
6450             -- if we are rechecking model as atp flag = 'Y' then
6451             -- we should recheck model's attribute so that model flags are set correctly.
6452             IF PG_DEBUG in ('Y', 'C') THEN
6453                msc_sch_wb.atp_debug('This models atp flag = Y. We are coming here for second time');
6454             END IF;
6455             MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id := null;
6456         END IF;
6457         --e_cto_rearch
6458         MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
6459                                             --3917625: read item attributes from planned data
6460                                             -- -1,
6461                                             p_plan_id,
6462                                             l_request_item_id,
6463                                             p_atp_record.organization_id,
6464                                             l_item_info_rec );
6465         /* Make a copy */
6466         C_ITEM_INFO_REC := G_ITEM_INFO_REC;
6467 
6468         /* Now get family item's info*/
6469         IF (l_family_item_id = G_ITEM_INFO_REC.sr_inv_item_id) THEN
6470            PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
6471            IF PG_DEBUG in ('Y', 'C') THEN
6472               msc_sch_wb.atp_debug('ATP_Check: ' || 'Local Rec Inst. '||PF_ITEM_INFO_REC.instance_id);
6473            END IF;
6474         ELSE
6475            --s_cto_rearch
6476             PF_ITEM_INFO_REC.parent_pegging_id := p_parent_pegging_id;
6477             PF_ITEM_INFO_REC.parent_bom_item_type := p_atp_record.parent_bom_item_type;
6478             PF_ITEM_INFO_REC.parent_atp_flag := p_atp_record.parent_atp_flag;
6479             PF_ITEM_INFO_REC.parent_comp_flag := p_atp_record.parent_atp_comp_flag;
6480             PF_ITEM_INFO_REC.parent_repl_ord_flag := p_atp_record.parent_repl_order_flag;
6481             --e_cto_rearch
6482            MSC_ATP_PROC.get_item_attributes(p_atp_record.instance_id,
6483                                          --3917625: Read item attribute from plani data
6484                                          -- -1,
6485                                          p_plan_id,
6486                                          l_family_item_id,
6487                                          p_atp_record.organization_id,
6488                                          PF_ITEM_INFO_REC);
6489 
6490         END IF;
6491         /* time_phased_atp changes end */
6492 
6493         --subst
6494         IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and NVL(p_atp_record.original_item_flag, 2) = 1 and
6495            MSC_ATP_SUBST.G_REQ_ITEM_SR_INV_ID <> p_atp_record.inventory_item_id THEN
6496            -- if it a subst item then we consider subst window
6497             l_substitution_window := C_ITEM_INFO_REC.substitution_window;
6498         ELSE
6499             -- If it is an original item or a bom item then we do not consider window
6500             l_substitution_window := 0;
6501         END IF;
6502         l_atp_flag := C_ITEM_INFO_REC.atp_flag;
6503         l_atp_comp_flag := C_ITEM_INFO_REC.atp_comp_flag;
6504         l_pre_pro_lt := C_ITEM_INFO_REC.pre_pro_lt;
6505         l_fixed_lt := C_ITEM_INFO_REC.fixed_lt;
6506         l_variable_lt := C_ITEM_INFO_REC.variable_lt;
6507         l_post_pro_lt := C_ITEM_INFO_REC.post_pro_lt;
6508 	l_process_lt  := C_ITEM_INFO_REC.processing_lt;  -- 5438149
6509         --diag_atp
6510         l_rounding_control_flag := C_ITEM_INFO_REC.rounding_control_type;
6511         l_unit_weight := C_ITEM_INFO_REC.unit_weight;
6512         l_weight_uom := C_ITEM_INFO_REC.weight_uom;
6513         l_unit_volume := C_ITEM_INFO_REC.unit_volume;
6514         l_volume_uom := C_ITEM_INFO_REC.volume_uom;
6515         l_dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
6516 
6517         /* Modularize Item and Org Info */
6518 
6519         IF PG_DEBUG in ('Y', 'C') THEN
6520            msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP flag is '||l_atp_flag);
6521            msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP components flag is ' ||l_atp_comp_flag);
6522            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pre_pro_lt := ' || l_pre_pro_lt);
6523            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_fixed_lt := ' || l_fixed_lt);
6524            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_variable_lt := ' || l_variable_lt);
6525            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_post_pro_lt := ' || l_post_pro_lt);
6526 	   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);  -- 5438149
6527         END IF;
6528 
6529         -- 2462661 : krajan
6530         -- Check to see if the item is uncollected.
6531         -- If we get here, then it means that the item is not ATPABle.
6532         -- If we defer, then error out
6533         IF PG_DEBUG in ('Y', 'C') THEN
6534             msc_sch_wb.atp_debug ('Checking for uncollected item ' );
6535         end if;
6536         if (MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID is not NULL) AND
6537             (NVL(p_atp_record.src_atp_flag,'N') <> 'N') then
6538             IF PG_DEBUG in ('Y', 'C') THEN
6539                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Not Collected as flags differ');
6540             END IF;
6541             p_atp_record.error_code := ATP_ITEM_NOT_COLLECTED;
6542 
6543             RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
6544         end if;
6545               -- end changes for 2462661
6546         /************ Bug 1510853 ATP Rule Check ************/
6547         -- Initialize the rule flag
6548         G_ATP_RULE_FLAG := 'Y';
6549         IF PG_DEBUG in ('Y', 'C') THEN
6550            msc_sch_wb.atp_debug('ATP_Check: ' || 'C_ITEM_INFO_REC atp_rule_id := '
6551                              || NVL(C_ITEM_INFO_REC.atp_rule_id, 0));
6552         END IF;
6553         IF ((G_INV_CTP = 5) AND (C_ITEM_INFO_REC.atp_rule_id IS NULL)) THEN
6554            G_ATP_RULE_FLAG := 'N';   -- Rule not found at item level.
6555            IF PG_DEBUG in ('Y', 'C') THEN
6556               msc_sch_wb.atp_debug('ATP_Check: ' || ' NO ATP RULE FOR ITEM ');
6557            END IF;
6558         END IF;
6559         /************ Bug 1510853 ATP Rule Check ************/
6560 
6561 
6562         l_delivery_lead_time := p_atp_record.delivery_lead_time;
6563         l_ship_method := p_atp_record.ship_method;
6564 
6565         -- get the default delivery_lead_time if it is not specified.
6566         -- dsting dlt
6567         IF (NVL(p_atp_record.delivery_lead_time, -1) = -1 ) THEN
6568 
6569                 msc_sch_wb.atp_debug('delivery lead time is not specified');
6570 
6571                 -- if we can not get the lead time from sourcing,
6572                 -- we need to find the default between locations.
6573 
6574 		-- Set l_site_id to null in case destination is an org to avoid getting lead time/ ship method
6575 		-- based on region of customer site.
6576 
6577 	        --2814895, populating variables for the call to atp_sources
6578 
6579 	        IF  MSC_ATP_PVT.G_SR_PARTNER_SITE_ID IS NOT NULL THEN
6580 	           l_partner_type := 2;
6581 	           l_party_site_id := NULL;
6582 	           l_order_line_id := NULL;
6583 		   l_site_id := MSC_ATP_PVT.G_SR_PARTNER_SITE_ID;
6584 		   l_location_id := p_atp_record.to_location_id;
6585 	        ELSIF MSC_ATP_PVT.G_SR_PARTY_SITE_ID IS NOT NULL THEN
6586 	           l_site_id := NULL;
6587 	           l_partner_type := 4;
6588 	           l_party_site_id := MSC_ATP_PVT.G_SR_PARTY_SITE_ID;
6589 	           l_order_line_id := NULL;
6590 	           l_location_id := NULL; --MSC_ATP_PVT.G_SR_LOCATION_ID;
6591 	        ELSIF MSC_ATP_PVT.G_SR_CUSTOMER_COUNTRY IS NOT NULL THEN
6592 	           l_partner_type := 5;
6593 	           l_party_site_id := NULL;
6594 	           l_order_line_id := p_atp_record.identifier;
6595 		   l_site_id := NULL;
6596 		   l_location_id := NULL;
6597 		ELSIF p_atp_record.to_organization_id IS NOT NULL THEN
6598 		   l_site_id := NULL;
6599 		   l_partner_type := NULL;
6600 	           l_party_site_id := NULL;
6601 	           l_order_line_id := NULL;
6602 	           l_location_id := p_atp_record.to_location_id;
6603 	        ELSE
6604 	          IF PG_DEBUG in ('Y', 'C') THEN
6605                      msc_sch_wb.atp_debug('ATP_Check: Nothing passed');
6606                    END IF;
6607                 END IF;
6608 
6609 -- cchen : changes for ship method
6610 
6611 		MSC_ATP_PROC.get_delivery_lead_time(p_atp_record.organization_id,
6612 						NULL,	-- from location id
6613 						p_atp_record.instance_id,
6614 						p_atp_record.to_organization_id,
6615 						l_location_id, --p_atp_record.to_location_id, --2814895
6616 						p_atp_record.to_instance_id,
6617 						NULL,	-- customer_id
6618 						NULL,	-- customer_site_id
6619 						NULL,	-- supplier_id
6620 						NULL,	-- supplier_site_id
6621 						MSC_ATP_PVT.G_SESSION_ID,
6622 						l_site_id,
6623 						l_ship_method,
6624 						l_delivery_lead_time,
6625 						l_partner_type, --2814895
6626 						l_party_site_id, --2814895
6627 						l_order_line_id); --2814895
6628 		-- dsting replaced old dlt code
6629 
6630         END IF;
6631 
6632         -- dsting dlt Round delivery lead time up
6633         l_delivery_lead_time := CEIL(l_delivery_lead_time);
6634         IF PG_DEBUG in ('Y', 'C') THEN
6635            msc_sch_wb.atp_debug('rounded l_delivery_lead_time: ' || l_delivery_lead_time);
6636         END IF;
6637 
6638         -- BUG 1621816- We offset the delivery lead time instead of
6639         -- reducing it. To off set we use calender code of the target_org
6640 
6641         IF (p_atp_record.requested_arrival_date IS NOT NULL) THEN
6642             -- we need to calculate the requested_ship_date
6643             -- requested_ship_date = requested_arrival_date - delivery_lead_time
6644 
6645             -- ship_rec_cal do we come here??
6646             l_requested_ship_date := MSC_CALENDAR.prev_work_day(
6647                                         p_atp_record.organization_id,
6648                                         p_atp_record.instance_id,
6649                                         MSC_CALENDAR.TYPE_DAILY_BUCKET,
6650                                         p_atp_record.requested_arrival_date
6651                                         - l_delivery_lead_time);
6652 
6653 /* dlt
6654            IF (p_atp_record.customer_id IS NOT NULL) THEN
6655                 ---Inquiry from a customer site
6656                 l_requested_ship_date := p_atp_record.requested_arrival_date -
6657                                      l_delivery_lead_time ;
6658            ELSE
6659                  --- Org
6660                 l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET
6661                                         (p_atp_record.to_organization_id,
6662                                          p_atp_record.to_instance_id,
6663                                          1,
6664                                          p_atp_record.requested_arrival_date,
6665                                          -NVL(l_delivery_lead_time, 0));
6666            END IF;
6667 */
6668         ELSE
6669             l_requested_ship_date := p_atp_record.requested_ship_date;
6670         END IF;
6671         /*
6672             Added by avjain to capture original request date to insert in msc-demands
6673             original_request_date corresponds to request date for 24*7 ATP which gives
6674             the request date
6675             */
6676         l_original_req_ship_date := nvl(p_atp_record.original_request_date,l_requested_ship_date);
6677         IF PG_DEBUG in ('Y', 'C') THEN
6678            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_original_req_ship_date: '||l_original_req_ship_date);
6679         END IF;
6680 
6681         l_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
6682         --- postprocess summary
6683         --- The following array stores 1 if we are doing Demand Class ATP for item-org
6684         --- We are doing this way because if we need to reomve or update the demand then
6685         --- we know that we need to do demand class based  update or removal of the demand in
6686         ---- case of ODS
6687         IF ((p_search = 1) and (p_parent_pegging_id is null)) THEN
6688            IF (MSC_ATP_PVT.G_INV_CTP = 5) AND (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') THEN
6689               --- THIS IS OSD case, backward case, and top demand
6690               IF PG_DEBUG in ('Y', 'C') THEN
6691                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Find out ATP_DEMAND_class flag for that item ');
6692               END IF;
6693 
6694               BEGIN
6695                  SELECT NVL(r.demand_class_atp_flag, 0)
6696                  INTO   l_demand_class_flag
6697                  FROM   msc_system_items I,
6698                         msc_trading_partners P,
6699                         msc_atp_rules  R
6700                  WHERE  I.sr_instance_id = p_atp_record.instance_id
6701                  AND    I.organization_id = p_atp_record.organization_id
6702                  AND    I.sr_inventory_item_id = p_atp_record.inventory_item_id
6703                  AND    I.plan_id = -1
6704                  AND    I.organization_id = P.sr_tp_id
6705                  AND    P.sr_instance_id = I.sr_instance_id
6706                  AND    P.partner_type = 3 --- organization
6707                  AND    I.sr_instance_id = R.sr_instance_id
6708                  AND    R.rule_id = NVL(I.atp_rule_id, P.default_atp_rule_id);
6709 
6710                  IF PG_DEBUG in ('Y', 'C') THEN
6711                     msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_demand_class_flag  : = ' || l_demand_class_flag);
6712                  END IF;
6713               EXCEPTION
6714                  WHEN OTHERS THEN
6715                        l_demand_class_flag := 0;
6716               END;
6717 
6718            ELSE
6719               ---pds cae
6720               l_demand_class_flag := 0;
6721            END IF;
6722            --- now extend the array which holds the demand_class atp_flag and add l_demand_class_flag to it
6723            MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.EXTEND;
6724            IF PG_DEBUG in ('Y', 'C') THEN
6725               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);
6726            END IF;
6727             MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG( MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG.count) := l_demand_class_flag;
6728         END IF;
6729 
6730         -- 1143475:
6731         -- IF atp lead time > 0 then we must do the offset no matter that item
6732         -- is atpable or not.  Otherwise, only if that item is atpable
6733         -- item we need to do the offset.  The reason of doing this is to avoid
6734         -- database hit.
6735 
6736         -- Bugs 2020607, 2104018, 2031894, 1869748
6737         -- Shift dates to work days irrespective of atp_flag.
6738 
6739 /*        If  (NVL(p_atp_record.atp_lead_time, 0) >0)  OR
6740           (l_atp_flag <> 'N') OR (l_atp_comp_flag <> 'N') THEN  */
6741 
6742         /* ship_rec_cal changes begin
6743         -- translate the l_requested_ship_date to a workday.
6744         l_requested_ship_date := MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
6745                                             p_atp_record.instance_id,
6746                                             l_requested_ship_date);
6747 
6748         -- bug 1192311
6749         IF l_requested_ship_date is NULL THEN
6750           p_atp_record.error_code := ATP_INVALID_DATE;
6751           RAISE FND_API.G_EXC_ERROR;
6752         END IF;*/
6753 
6754         -- since we may have atp lead time specified for the item,
6755         -- we need to offset x working days from the the ship date.
6756 
6757         /* S_CTO_REACH : do not honor atp lead time
6758 
6759         l_requested_ship_date := MSC_CALENDAR.DATE_OFFSET
6760                                   (p_atp_record.organization_id,
6761                                    p_atp_record.instance_id,
6762                                    1,
6763                                    l_requested_ship_date,
6764                                    -NVL(p_atp_record.atp_lead_time, 0));
6765         */
6766 
6767         -- Bugs 2020607, 2104018, 2031894, 1869748
6768         -- Find the next working date for sysdate
6769         -- and comment out the call related to bug 1124538.
6770 
6771         BEGIN  -- Bug2031894. Handle Exception here.
6772           l_sysdate := NVL(MSC_CALENDAR.next_work_day(
6773                                         p_atp_record.shipping_cal_code,
6774                                         p_atp_record.instance_id,
6775                                         sysdate), sysdate);
6776         EXCEPTION
6777           WHEN OTHERS THEN
6778                 NULL;
6779         END;
6780 
6781 /*        -- for bug 1124538 : find the prev working date of sysdate
6782         l_sysdate := NVL(MSC_ATP_FUNC.prev_work_day(p_atp_record.organization_id,
6783                                             p_atp_record.instance_id,
6784                                             sysdate), sysdate);   */
6785 
6786         IF PG_DEBUG in ('Y', 'C') THEN
6787            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_sysdate ='||l_sysdate);
6788         END IF;
6789         --END IF;    Comment out atp_flag block
6790         -- Bugs 2020607, 2104018, 2031894, 1869748
6791 
6792         p_atp_record.delivery_lead_time := l_delivery_lead_time;
6793         p_atp_record.ship_method := l_ship_method;
6794 
6795         IF PG_DEBUG in ('Y', 'C') THEN
6796            msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_method = '||l_ship_method ||' lead time = '||l_delivery_lead_time);
6797             msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date :=' || l_requested_ship_date);
6798          END IF;
6799 
6800         IF (l_atp_flag = 'N') and (l_atp_comp_flag = 'N') THEN
6801             IF PG_DEBUG in ('Y', 'C') THEN
6802                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_flag and l_atp_comp_flag are both N');
6803             END IF;
6804             p_atp_record.error_code := ATP_NOT_APPL;
6805 
6806 	    -- Bug 1916086, modified Schedule procedure to extend G_PEGGING_FOR_SET in case of
6807 	    -- top-level Non-ATPable items as well. This was needed so that l_atp_table and
6808 	    -- l_fst_src_pegging_ids would have same number of records.
6809 
6810             IF p_parent_pegging_id is null THEN
6811                 IF PG_DEBUG in ('Y', 'C') THEN
6812                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
6813                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
6814                 END IF;
6815 
6816                 MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
6817                 IF PG_DEBUG in ('Y', 'C') THEN
6818                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
6819                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting null pegging id for Non-ATPable items');
6820                 END IF;
6821 
6822                 MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := null;
6823 
6824                 MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
6825                 MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
6826 
6827                 MSC_ATP_PVT.G_REQ_ATP_DATE(MSC_ATP_PVT.G_REQ_ATP_DATE.count) := l_requested_ship_date;
6828                 MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.count) := p_atp_record.quantity_ordered;
6829 
6830             END IF;
6831 
6832             -- Bugs 2020607, 2104018, 2031894, 1869748
6833             -- Redo changes made for Bug 1124538
6834             -- This is a desired functionality
6835 
6836             -- bug 1143475
6837             -- no matter it is past due or not, we always satisfy users
6838             -- on the date they want. that is, we should not offset the date
6839             -- users request unless atp lead time is specified and not equal 0
6840 
6841             -- for bug 1124538 : if request date is less than sysdate,
6842             -- than the requested_date_quantity should be 0 and ship_date
6843             -- will be sysdate
6844 
6845             -- Bug 2625791, 2623816: If it is a transfer case then We should take
6846             -- into consideration PTF date passed to us from the top organization.
6847             -- We should satisfy the damand as per the PTF date from the top org.
6848             IF PG_DEBUG in ('Y', 'C') THEN
6849                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
6850             END IF;
6851 
6852             --s_cto_rearch: bug 3169831 : Honor lead time only when profile option is turned on
6853             l_enforce_model_lt := NVL(FND_PROFILE.VALUE('MSC_ENFORCE_MODEL_LT'), 'Y');
6854 
6855             IF PG_DEBUG in ('Y', 'C') THEN
6856                msc_sch_wb.atp_debug('l_enforce_model_lt := ' || l_enforce_model_lt);
6857             END IF;
6858 
6859             IF MSC_ATP_PVT.G_INV_CTP = 5 and C_ITEM_INFO_REC.bom_item_type = 1
6860                                          and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y'
6861                                          and l_enforce_model_lt = ' Y' THEN
6862 
6863                -- in ODS ATP we want to honor lead times for ATO models
6864                l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
6865                l_net_processing_lt := (l_fixed_lt + l_variable_lt * p_atp_record.quantity_ordered)*
6866                                                                          (1 + l_mso_lead_time_factor);
6867                l_ato_date := MSC_CALENDAR.DATE_OFFSET
6868                                   (p_atp_record.organization_id,
6869                                    p_atp_record.instance_id,
6870                                    1,
6871                                    l_sysdate,
6872                                    l_net_processing_lt);
6873             ELSE
6874               l_ato_date := l_sysdate;
6875             END IF;
6876             --5160663, commenting out.
6877             /*
6878             IF l_requested_ship_date < GREATEST(l_sysdate,l_ato_date)  THEN
6879                 IF PG_DEBUG in ('Y', 'C') THEN
6880                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date < l_sysdate');
6881                 END IF;
6882                 p_atp_record.requested_date_quantity := 0.0;
6883                 p_atp_record.ship_date := GREATEST(l_sysdate, MSC_ATP_PVT.G_PTF_DATE, l_ato_date);
6884             ELSE
6885                 IF PG_DEBUG in ('Y', 'C') THEN
6886                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date >= l_sysdate');
6887                 END IF;
6888                 p_atp_record.requested_date_quantity :=
6889                         p_atp_record.quantity_ordered;
6890                 p_atp_record.ship_date :=  GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
6891             END IF;
6892             */
6893 
6894             -- Bugs 2020607, 2104018, 2031894, 1869748
6895             -- Redo changes made for Bug 1124538
6896             -- This is a desired functionality
6897             -- Comments are uncommented out END.
6898 
6899             p_atp_record.requested_date_quantity :=
6900                         p_atp_record.quantity_ordered;
6901 
6902             -- cchen 1238941
6903             p_atp_record.combined_requested_date_qty :=
6904                 p_atp_record.quantity_ordered;
6905 
6906             -- cchen 1276131 make sure we offset if atp_lead time exists
6907             --p_atp_record.ship_date := l_requested_ship_date;
6908             p_atp_record.ship_date := GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE); --5160663
6909             /* s_cto_arch: do not honor atp lead time
6910             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
6911               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
6912                                  (p_atp_record.organization_id,
6913                                   p_atp_record.instance_id,
6914                                   1,
6915                                   l_requested_ship_date,
6916                                   p_atp_record.atp_lead_time);
6917             --  Bug2302403
6918             --ELSE
6919             --  p_atp_record.ship_date := l_requested_ship_date;
6920             END IF;
6921             */
6922 
6923             IF PG_DEBUG in ('Y', 'C') THEN
6924                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date:='||
6925                                   p_atp_record.ship_date );
6926                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_date_quantity:='||
6927                                   p_atp_record.requested_date_quantity);
6928             END IF;
6929             p_atp_record.available_quantity :=  p_atp_record.quantity_ordered;
6930 
6931             -- In case the call is for multi-level/ multi-org CTO with a
6932             -- reduced ship set, we need to generate the pegging tree
6933             -- even if the item is non-ATPable. - ngoel 10/18/00
6934 
6935             -- Check if Records exist in MSC_BOM_TEMP table in case of PDS. If yes,
6936             -- create pegging record. Since for models and option classes, atp_comp_flag
6937 	    -- shall always be yes in case of CTO, this situation may only occur at
6938 	    -- components level.
6939 
6940             IF PG_DEBUG in ('Y', 'C') THEN
6941                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.Calling_Module : '||p_atp_record.Calling_Module);
6942             END IF;
6943 
6944               --s_cto_rearch
6945             /* bug 3564511: We do not need to add demand and pegging for non-atpable items
6946                because we now use data stored in msc_cto_bom to retrieve bom for 24x7 processing.
6947                Removed code for adding demands and pegging
6948 
6949             */
6950 
6951         ELSIF (NVL(p_atp_record.override_flag, 'N') = 'Y') AND (G_INV_CTP = 5)
6952              --bug 3742009: For ATO models/items, do the regular processing as their bom
6953              --- need to be exploded and demands need to be placed on the bom components
6954              AND NOT( C_ITEM_INFO_REC.bom_item_type in (1,4) and  C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
6955             -- ATP Override -- Agilent changes -- ODS ATP
6956 
6957             --- bug 2249118
6958             -- Bug 1916086, modified Schedule procedure to extend G_PEGGING_FOR_SET in case of
6959             -- top-level Non-ATPable items as well. This was needed so that l_atp_table and
6960             -- l_fst_src_pegging_ids would have same number of records.
6961 
6962             IF p_parent_pegging_id is null THEN
6963 
6964                 MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
6965                 IF PG_DEBUG in ('Y', 'C') THEN
6966                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
6967                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
6968                    msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
6969                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting null pegging id for Non-ATPable items');
6970                 END IF;
6971 
6972                 MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := null;
6973 
6974                 MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
6975                 MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
6976 
6977                 MSC_ATP_PVT.G_REQ_ATP_DATE(MSC_ATP_PVT.G_REQ_ATP_DATE.count) := l_requested_ship_date;
6978                 MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.count) := p_atp_record.quantity_ordered;
6979 
6980             END IF;
6981 
6982             -- cchen 1276131 make sure we offset if atp_lead time exists
6983             /* s_cto_rearch: do not honor atp lead time
6984             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
6985               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
6986                                  (p_atp_record.organization_id,
6987                                   p_atp_record.instance_id,
6988                                   1,
6989                                   -- Bug 2625791, 2623816
6990                                   GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE),
6991                                   p_atp_record.atp_lead_time);
6992             ELSE
6993               p_atp_record.ship_date := l_requested_ship_date;
6994             END IF;
6995             */
6996              p_atp_record.ship_date := l_requested_ship_date;
6997 
6998             p_atp_record.error_code := ALLSUCCESS;
6999 
7000             -- prepare the insert record
7001             -- no matter it is a demand or not, we need to insert this
7002             -- record into database since it is a recursive procedure
7003             -- and we will rollback in ATP procedure if it is not a
7004             -- demand.
7005 
7006             l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7007             /* Modularize Item and Org Info */
7008             -- Re_use Information.
7009             -- time_phased_atp - Now C_ITEM_INFO_REC stores member item info in ODS case
7010             l_atp_insert_rec.inventory_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7011             -- No need to call function below.
7012             /*l_atp_insert_rec.inventory_item_id := MSC_ATP_FUNC.get_inv_item_id
7013                                                 (p_atp_record.instance_id,
7014                                                 --- 2152184
7015                                                 l_request_item_id,
7016                                                 --p_atp_record.request_item_id,
7017                                                 null,
7018                                                 p_atp_record.organization_id);
7019              Modularize Item and Org Info */
7020             l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7021             l_atp_insert_rec.identifier := p_atp_record.identifier;
7022             l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7023             l_atp_insert_rec.demand_source_header_id :=
7024                             nvl(p_atp_record.demand_source_header_id, -1);
7025             l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
7026             l_atp_insert_rec.demand_source_delivery :=
7027                             p_atp_record.demand_source_delivery;
7028             l_atp_insert_rec.quantity_ordered :=
7029                              p_atp_record.quantity_ordered;
7030             l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7031             l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7032             l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7033 
7034 	    -- Modified by ngoel on 1/12/2001 for origination_type = 30
7035             --l_atp_insert_rec.origination_type := 6;
7036             l_atp_insert_rec.origination_type := 30;
7037 
7038             l_atp_insert_rec.order_number := p_atp_record.order_number;
7039             -- added by avjain for plan by request date
7040             l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7041             l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7042             l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7043             IF p_search = 1 and p_parent_pegging_id is null THEN
7044                --- we need to pass old demand id so that we can update that demand
7045                --- this will happen in case of reschedulling
7046                l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
7047             ELSE
7048                l_atp_insert_rec.old_demand_id := null;
7049             END IF;
7050 
7051             l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7052             l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7053 
7054             MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
7055                            p_plan_id,
7056                            l_demand_class_flag,
7057                            l_demand_id);
7058             IF PG_DEBUG in ('Y', 'C') THEN
7059                msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
7060                            l_demand_id);
7061             END IF;
7062 
7063 
7064 --        END IF;
7065 
7066         ELSIF l_atp_flag in ('Y', 'C', 'N') THEN
7067             -- ATP Override -- Agilent changes -- PDS ATP
7068             -- Allow Override for PDS also
7069             --(NVL(p_atp_record.override_flag, 'N') <> 'Y')) THEN
7070 
7071             -- get the following information
7072             -- requested_date_quantity
7073             -- atp_date_this_level
7074             -- atp_date_quantity_this_level
7075 
7076           -- hierarchy changes due to arrival set
7077 
7078           IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') THEN
7079                IF PG_DEBUG in ('Y', 'C') THEN
7080                   msc_sch_wb.atp_debug('ATP_Check: ' || 'customer class or demand class : same calls');
7081                END IF;
7082 
7083               /* Modularize Item and Org Info */
7084               -- Re-Use info instead of making unnecessary call.
7085               l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7086               /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id(
7087                                           p_atp_record.instance_id,
7088                                           p_atp_record.inventory_item_id,
7089                                           null,
7090                                           p_atp_record.organization_id);
7091                Modularize Item and Org Info */
7092 
7093               /* New allocation logic for time_phased_atp changes begin */
7094               IF l_time_phased_atp = 'Y' THEN
7095                       MSC_ATP_PF.Set_Alloc_Rule_Variables(
7096                           l_inv_item_id,
7097                           PF_ITEM_INFO_REC.dest_inv_item_id,
7098                           p_atp_record.organization_id,
7099                           p_atp_record.instance_id,
7100                           p_atp_record.demand_class,
7101                           p_atp_record.atf_date,
7102                           l_return_status
7103                       );
7104                       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7105                            IF PG_DEBUG in ('Y', 'C') THEN
7106                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Set_Alloc_Rule_Variables');
7107                            END IF;
7108                            RAISE FND_API.G_EXC_ERROR;
7109                       END IF;
7110 
7111                       IF l_requested_ship_date <= p_atp_record.atf_date THEN
7112                           IF MSC_ATP_PVT.G_MEM_RULE_WITHIN_ATF = 'Y' THEN
7113                               l_item_to_use := l_inv_item_id;
7114                           ELSE
7115                               l_item_to_use := PF_ITEM_INFO_REC.dest_inv_item_id;
7116                           END IF;
7117                       ELSE
7118                           IF MSC_ATP_PVT.G_PF_RULE_OUTSIDE_ATF = 'Y' THEN
7119                               l_item_to_use := PF_ITEM_INFO_REC.dest_inv_item_id;
7120                           ELSE
7121                               l_item_to_use := l_inv_item_id;
7122                           END IF;
7123                       END IF;
7124               ELSE
7125                       l_item_to_use := l_inv_item_id;
7126               END IF;
7127               IF PG_DEBUG in ('Y', 'C') THEN
7128                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date = '||l_requested_ship_date);
7129                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Item to be used = '||l_item_to_use);
7130               END IF;
7131               /* New allocation logic for time_phased_atp changes end */
7132 
7133               p_atp_record.demand_class :=
7134 
7135                   MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
7136                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
7137                            l_item_to_use, -- For new allocation logic for time_phased_atp
7138                            p_atp_record.organization_id,
7139                            p_atp_record.instance_id,
7140                            l_requested_ship_date,
7141                            NULL, -- level_id
7142                            p_atp_record.Demand_Class);
7143                   --diag_atp
7144                   l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
7145               IF PG_DEBUG in ('Y', 'C') THEN
7146                  msc_sch_wb.atp_debug('ATP_Check: ' || 'after getting the dummy demand class');
7147                  msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.demand_class = '||p_atp_record.demand_class);
7148               END IF;
7149 
7150           END IF;
7151           ---subst
7152           IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and
7153              ((p_search = BACKWARD_SCHEDULING AND NVL(p_atp_record.original_item_flag, 2) = 1) OR
7154                (p_search = FORWARD_SCHEDULING AND p_atp_record.top_tier_org_flag = 1)) THEN
7155               --- in case of product substitution
7156               ---1) Backward case- we do not want to look for top item's availability as it is already been
7157               --- taken care of during breadth level item availability search in Check_atp-subst
7158               ---2) Forward: we do not want to look for item's availability in top org
7159               IF PG_DEBUG in ('Y', 'C') THEN
7160                  msc_sch_wb.atp_debug('ATP_Check: ' || 'Original item, substitution');
7161               END IF;
7162               IF l_atp_comp_flag = 'N' THEN
7163                  --- if ATP components flag is "N' and if it top level item then
7164                  --- we cant make anything. Therefore, we set the l_re_date_qty to be zero
7165                  l_requested_date_quantity := 0;
7166                  l_net_demand := 0;
7167               ELSE
7168                  l_net_demand := p_atp_record.quantity_ordered;
7169                  IF p_search = BACKWARD_SCHEDULING and NVL(p_atp_record.top_tier_org_flag, 2) = 2 THEN
7170                      -- we add demand for only second tier org. Demand for first tier org has
7171                      --- already been created while looking for substitute availability
7172                      IF PG_DEBUG in ('Y', 'C') THEN
7173                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Backward sch and not a top org, add demand');
7174                      END IF;
7175                      l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7176                      /* Modularize Item and Org Info */
7177                      -- time_phased_atp changes begin
7178                      l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
7179                      l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7180                      l_atp_insert_rec.atf_date := p_atp_record.atf_date;
7181                      l_atp_insert_rec.atf_date_quantity := 0; -- check this??
7182                      l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
7183                      -- time_phased_atp changes end
7184 
7185                      l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7186                      l_atp_insert_rec.identifier := p_atp_record.identifier;
7187                      l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7188                      l_atp_insert_rec.demand_source_header_id :=
7189                                      nvl(p_atp_record.demand_source_header_id, -1);
7190                      l_atp_insert_rec.demand_source_delivery :=
7191                                      p_atp_record.demand_source_delivery;
7192 
7193                      l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
7194 
7195                      l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7196                      l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7197                      l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7198                      l_atp_insert_rec.order_number := p_atp_record.order_number;
7199 
7200 
7201                      -- ATP4drp If we are dealing with a component
7202                      -- then set the origination type to Constrained Kit Demand
7203                      -- for DRP plans.
7204                      IF (NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id) <> p_atp_record.inventory_item_id AND
7205                          NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
7206                          l_atp_insert_rec.origination_type := 47;
7207                          IF PG_DEBUG in ('Y', 'C') THEN
7208                            msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7209                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP origination_type ='|| l_atp_insert_rec.origination_type);
7210                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP parent_item_id ='|| p_atp_record.parent_item_id);
7211                            msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP child_item_id ='|| p_atp_record.inventory_item_id);
7212                            msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7213                          END IF;
7214                      ELSE
7215                          l_atp_insert_rec.origination_type := 1;
7216                      END IF;
7217                      -- End ATP4drp
7218                      -- for performance reason, we call these function here and
7219                      -- then populate the pegging tree with the values
7220 
7221                      /* Modularize Item and Org Info */
7222                      -- Re-Use info instead of making unnecessary call.
7223                      l_inv_item_name := C_ITEM_INFO_REC.item_name;
7224                      MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
7225                                                     p_atp_record.organization_id);
7226                      l_org_code := G_ORG_INFO_REC.org_code;
7227 
7228                      l_atp_insert_rec.old_demand_id := null;
7229 
7230 
7231                      --s_cto_rearch
7232                      l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
7233                      l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
7234                      l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
7235                      l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
7236                      l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
7237                      l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
7238                      l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
7239                      --e_cto_rearch
7240                      --plan by request date enhancement
7241                      l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7242                      l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7243                      l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7244 
7245                      -- ship_rec_cal
7246                      IF p_search = 1 and p_parent_pegging_id is null THEN
7247                          IF  p_atp_record.ship_method <> '@@@' THEN
7248                             l_atp_insert_rec.ship_method := p_atp_record.ship_method;
7249                          ELSE
7250                             l_atp_insert_rec.ship_method := null;
7251                          END IF;
7252                      END IF;
7253                      l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7254                      l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7255 
7256                      MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
7257                                    p_plan_id,
7258                                    l_demand_class_flag,
7259                                    l_demand_id);
7260 
7261                      IF PG_DEBUG in ('Y', 'C') THEN
7262                         msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
7263                                 l_demand_id);
7264                      END IF;
7265 
7266                      -- populate insert rec to pegging tree for this demand
7267                      --bug 2422847: add demand pegging
7268 
7269                      l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7270                      l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
7271                      l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
7272                      l_pegging_rec.atp_level:= p_level;
7273                      l_pegging_rec.organization_id:= p_atp_record.organization_id;
7274                      l_pegging_rec.organization_code := l_org_code;
7275                      l_pegging_rec.identifier1:= p_atp_record.instance_id;
7276                      l_pegging_rec.identifier2 := p_plan_id;
7277                      l_pegging_rec.identifier3 := l_demand_id;
7278 
7279                      -- time_phased_atp changes begin
7280                      IF l_pf_atp = 'Y' THEN
7281                          l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
7282                          l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
7283                      ELSE
7284                          l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
7285                          l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
7286                      END IF;
7287                      l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
7288                      -- time_phased_atp changes end
7289 
7290                      l_pegging_rec.resource_id := NULL;
7291                      l_pegging_rec.resource_code := NULL;
7292                      l_pegging_rec.department_id := NULL;
7293                      l_pegging_rec.department_code := NULL;
7294                      l_pegging_rec.supplier_id := NULL;
7295                      l_pegging_rec.supplier_name := NULL;
7296                      l_pegging_rec.supplier_site_id := NULL;
7297                      l_pegging_rec.supplier_site_name := NULL;
7298                      l_pegging_rec.scenario_id:= p_scenario_id;
7299                      l_pegging_rec.supply_demand_source_type:= 6;
7300                      l_pegging_rec.supply_demand_quantity:=
7301                          p_atp_record.quantity_ordered ;
7302                       l_pegging_rec.supply_demand_type:= 1;
7303                        -- Bug 2625791, 2623816: consider PTF date passed from the top org.
7304                       l_pegging_rec.supply_demand_date:= GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
7305 
7306 
7307 		     -- dsting ATO 2465370
7308 		     --	 IF NVL(p_atp_record.override_flag, 'N') = 'Y'
7309 		     IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'Y'
7310 			AND p_parent_pegging_id is null
7311 		     THEN
7312                         l_pegging_rec.required_date:= l_requested_ship_date;
7313                         --bug 3328421: Actual Supply demand date
7314                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
7315 		     ELSE
7316                         -- Bug 2748730. No need to move date to the end of as we will be doing that
7317 		        -- inside the procedure Add_Pegging
7318                         -- l_pegging_rec.required_date:= TRUNC(
7319 				-- GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE)) + MSC_ATP_PVT.G_END_OF_DAY;
7320                         --bug 3328421: required date always conatin the original req date
7321 			l_pegging_rec.required_date:= l_requested_ship_date;
7322                         l_pegging_rec.actual_supply_demand_date :=
7323 				GREATEST(l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
7324 		     END IF;
7325 
7326                       -- for demo:1153192
7327                       l_pegging_rec.constraint_flag := 'N';
7328 	              l_pegging_rec.component_identifier :=
7329                                  NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
7330 
7331                       l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
7332                       --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
7333                       --- cto looks at pegging tree to place their demands. Since CTO expects to find
7334                       --  id for the requested item, we add the following column. CTO will now read from this column
7335                       l_pegging_rec.request_item_id := p_atp_record.request_item_id;
7336 
7337                       --s_cto_rearch
7338                       l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7339                       l_pegging_rec.base_model_id := p_atp_record.base_model_id;
7340                       --e_cto_rearch
7341                       l_pegging_rec.demand_class :=  p_atp_record.demand_class;
7342                       --4570421
7343                       l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
7344                       l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
7345                       l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
7346                       l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
7347                       l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
7348                       l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
7349                       l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
7350 
7351                       MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
7352 
7353                  END IF;
7354               END IF;
7355               IF PG_DEBUG in ('Y', 'C') THEN
7356                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand := ' || l_net_demand);
7357               END IF;
7358 
7359               IF  NVL(p_atp_record.top_tier_org_flag, 2) = 1 THEN
7360                   --for top tier org l_pegging_id should be mapped to demand in
7361                   -- the top org. Since demand is created in atp)_check_subst procedure
7362                   -- and corresponding peggign id is passed as p_parent_pegging_id
7363                   --- we mapped l_pegging_id to p_parent_pegging_id
7364                   l_pegging_id := p_parent_pegging_id;
7365 
7366                   --5088719
7367                   l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7368                      /* Modularize Item and Org Info */
7369                      -- time_phased_atp changes begin
7370                   l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
7371                   l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7372                   l_atp_insert_rec.atf_date := p_atp_record.atf_date;
7373                   l_atp_insert_rec.atf_date_quantity := 0; -- check this??
7374                   l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
7375                      -- time_phased_atp changes end
7376 
7377                   l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7378                   l_atp_insert_rec.identifier := p_atp_record.identifier;
7379                   l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7380                   l_atp_insert_rec.demand_source_header_id :=
7381                                      nvl(p_atp_record.demand_source_header_id, -1);
7382                   l_atp_insert_rec.demand_source_delivery :=
7383                                      p_atp_record.demand_source_delivery;
7384 
7385                   l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
7386 
7387                   l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7388                   l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7389                   l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7390                   l_atp_insert_rec.order_number := p_atp_record.order_number;
7391 
7392 
7393                   l_atp_insert_rec.origination_type := 30; --1; 5088719
7394                      -- for performance reason, we call these function here and
7395                      -- then populate the pegging tree with the values
7396 
7397                      /* Modularize Item and Org Info */
7398                      -- Re-Use info instead of making unnecessary call.
7399                   l_inv_item_name := C_ITEM_INFO_REC.item_name;
7400                   MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
7401                                                     p_atp_record.organization_id);
7402                   l_org_code := G_ORG_INFO_REC.org_code;
7403 
7404                   l_atp_insert_rec.old_demand_id := null;
7405 
7406 
7407                      --s_cto_rearch
7408                   l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
7409                   l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
7410                   l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
7411                   l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
7412                   l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
7413                   l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
7414                   l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
7415                      --e_cto_rearch
7416                      --plan by request date enhancement
7417                   l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7418                   l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7419                   l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7420 
7421                      -- ship_rec_cal
7422                   IF p_search = 1 and p_parent_pegging_id is null THEN
7423                       IF  p_atp_record.ship_method <> '@@@' THEN
7424                          l_atp_insert_rec.ship_method := p_atp_record.ship_method;
7425                       ELSE
7426                          l_atp_insert_rec.ship_method := null;
7427                       END IF;
7428                   END IF;
7429                   l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7430                   l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7431 
7432                   l_demand_id := p_atp_record.subs_demand_id; --5088719
7433 
7434               END IF;
7435               IF PG_DEBUG in ('Y', 'C') THEN
7436                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id := ' || l_pegging_id);
7437               END IF;
7438               /* Modularize Item and Org Info */
7439               -- Re-Use info instead of making unnecessary call.
7440               l_inv_item_name := C_ITEM_INFO_REC.item_name;
7441               /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
7442                                          p_atp_record.inventory_item_id,
7443                                          p_atp_record.organization_id);
7444 
7445                Modularize Item and Org Info */
7446                MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
7447                                                  p_atp_record.organization_id );
7448               l_org_code := G_ORG_INFO_REC.org_code;
7449               /*l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
7450                                           p_atp_record.organization_id);
7451                Modularize Item and Org Info */
7452               l_atp_date_this_level := null;
7453               --MSC_ATP_PVT.G_DEMAND_PEGGING_ID := p_parent_pegging_id;
7454               --5026618 Start 4902658 Fix 5216528
7455               l_substitutes_rec.inventory_item_id.EXTEND;
7456               l_substitutes_rec.pegging_id.EXTEND;
7457               l_substitutes_rec.sub_atp_qty.EXTEND;
7458               l_substitutes_rec.demand_id.EXTEND;
7459               l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
7460               l_substitutes_rec.quantity_ordered.EXTEND;
7461               l_substitutes_rec.pf_item_id.EXTEND;
7462 
7463               l_substitutes_rec.demand_id(1) := l_demand_id; --4902658 / 5216528
7464               l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id;
7465               l_substitutes_rec.pegging_id(1) := p_parent_pegging_id;
7466               l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity,0),
7467                                                          p_atp_record.quantity_ordered);
7468               --5026618 Ends 4902658/5216528 Fix
7469           ELSE
7470              --s_cto_rearch
7471              IF (l_atp_flag in ('Y', 'C')) and
7472                                  NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
7473                                     --bug 3423277: Do ATP Check on model when
7474                                     --ATP flag for model = 'Y' and atp_comp_flag in ('R', 'N')
7475                                     -- as in these cases ATP on model will not be done  when
7476                                     -- we are checking component's availability
7477                                     -- Since we are using 'Not' clause, we check for other values
7478                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
7479                                       p_atp_record.parent_item_id is null)) THEN
7480               -- for bug 1124538:
7481               -- we need to get atp info even if requested ship date < sysdate
7482               -- if this is the top level and backward,
7483               -- otherwise we only do it if requested ship date >= sysdate
7484 
7485               IF ((l_requested_ship_date >= l_sysdate) OR
7486                  (p_search = BACKWARD_SCHEDULING AND
7487                   p_parent_pegging_id is null) OR
7488                  (p_search = FORWARD_SCHEDULING)) THEN
7489 
7490                  --- bug 2178544: In forward schedulling for components we start looking for
7491                  --- availabilty on greatest of (PTF of parent , req_date)
7492                  --- Here PTF_date is ptf date for parent i.e if we came here from get_comp_req then
7493                  --- it is PTF for parent item. If we came here for transfer then PTF will be that of last org
7494                  IF PG_DEBUG in ('Y', 'C') THEN
7495                     msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE from  parent := ' || MSC_ATP_PVT.G_PTF_DATE);
7496                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
7497                  END IF;
7498 
7499                  --diag_atp
7500                  l_get_mat_in_rec.rounding_control_flag := l_rounding_control_flag;
7501                  l_get_mat_in_rec.dest_inv_item_id := l_dest_inv_item_id;
7502                  l_get_mat_in_rec.plan_name := p_atp_record.plan_name;
7503 
7504 	         IF ((p_search = FORWARD_SCHEDULING) AND
7505                      --- Enhance CTO Phase 1 Req #17
7506                        -- Support Forward Stealing for components of ATO model in
7507                        -- Model's sourcing organization.
7508                      --support forward scheduling for all level
7509                      --(p_parent_pegging_id IS NULL)  AND
7510                     --- End Enhance CTO Phase 1 Req #17
7511 		    (MSC_ATP_PVT.G_INV_CTP = 4) AND
7512                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
7513                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
7514                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
7515 
7516                     /*
7517                     --- Enhance CTO Phase 1 Req #17
7518                        -- Support Forward Stealing for components of ATO model in
7519                        -- Model's sourcing organization.
7520                     IF (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) THEN
7521                        l_parent_peg_forward := p_parent_pegging_id;
7522                     ELSE
7523                        l_parent_peg_forward := 0;
7524                     END IF;
7525                     */
7526 
7527                     l_parent_peg_forward := p_parent_pegging_id;
7528                     IF PG_DEBUG in ('Y', 'C') THEN
7529                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Parent Pegging Id :' || l_parent_peg_forward);
7530                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Future Pegging Id :' || l_future_pegging_id);
7531                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Pegging Id :' || MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
7532                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Global Future Pegging Id :' || MSC_ATP_PVT.G_CTO_FORWARD_DMD_PEG);
7533                     END IF;
7534 
7535                     l_get_mat_in_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
7536                     l_get_mat_in_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
7537                     l_get_mat_in_rec.replenish_to_ord_flag := C_ITEM_INFO_REC.replenish_to_ord_flag;
7538                     l_get_mat_in_rec.parent_repl_order_flag := p_atp_record.parent_repl_order_flag;
7539                     l_get_mat_in_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
7540                     l_get_mat_in_rec.shipping_cal_code := p_atp_record.shipping_cal_code;
7541                     l_get_mat_in_rec.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
7542                     l_get_mat_in_rec.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
7543                     l_get_mat_in_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
7544                     l_get_mat_in_rec.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
7545                     -- Bug 3371817 - Pass calendar code to calculate sys_next_date
7546                     --               In make case this will actually contain OMC for lower level components
7547 
7548 		    MSC_AATP_REQ.Get_Forward_Material_Atp(
7549 				p_atp_record.instance_id,
7550 				p_plan_id,
7551 				p_level + 1,
7552 				p_atp_record.identifier,
7553 				p_atp_record.demand_source_type,--cmro
7554 				p_scenario_id,
7555 				p_atp_record.inventory_item_id,
7556 				p_atp_record.request_item_id, -- For time_phased_atp
7557 				p_atp_record.organization_id,
7558 				C_ITEM_INFO_REC.item_name,   -- Modular re-use
7559 				PF_ITEM_INFO_REC.item_name,  -- For time_phased_atp
7560 				l_requested_ship_date,
7561 				p_atp_record.quantity_ordered,
7562 				p_atp_record.demand_class,
7563 				l_requested_date_quantity,
7564 				l_atf_date_qty, -- For time_phased_atp
7565 				l_atp_date_this_level,
7566 				l_atp_date_quantity_this_level,
7567 				l_atp_pegging_tab,
7568 				l_return_status,
7569 				l_used_available_quantity, --bug3409973
7570                                 --diag_atp,
7571                                 null,
7572                                 l_get_mat_in_rec,
7573                                 l_get_mat_out_rec,
7574                                 p_atp_record.atf_date, -- For time_phased_atp
7575                                 p_atp_record.order_number,
7576                                 p_refresh_number,
7577                                 l_parent_peg_forward);
7578 
7579                      IF p_parent_pegging_id is not null THEN
7580 
7581                         l_pegging_id := l_get_mat_out_rec.demand_pegging_id;
7582                          --bug3432341: store demand id for demands added in forward scheduling
7583                         l_demand_id := l_get_mat_out_rec.demand_id;
7584 
7585                      ELSE
7586 
7587                         l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
7588 
7589                      END IF;
7590                      p_atp_record.atf_date_quantity := NVL(l_atf_date_qty, 0);
7591 
7592                      --- End Enhance CTO Phase 1 Req #17
7593 	             -- print the results from Get_Forward_Material_Atp
7594 	             IF PG_DEBUG in ('Y', 'C') THEN
7595 	                msc_sch_wb.atp_debug('ATP_Check: ' || '********** Result from Get_Forward_Material_Atp **********');
7596 	                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity); --bug3409973
7597 	             END IF;
7598 
7599 	         ELSE 		-- IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1
7600                         -- time_phased_atp changes begin
7601                         l_mat_atp_info_rec.instance_id               := p_atp_record.instance_id;
7602                         l_mat_atp_info_rec.plan_id                   := p_plan_id;
7603                         l_mat_atp_info_rec.level                     := p_level + 1;
7604                         l_mat_atp_info_rec.identifier                := p_atp_record.identifier;
7605                         l_mat_atp_info_rec.scenario_id               := p_scenario_id;
7606                         l_mat_atp_info_rec.inventory_item_id         := p_atp_record.inventory_item_id;
7607                         l_mat_atp_info_rec.request_item_id           := p_atp_record.request_item_id;
7608                         l_mat_atp_info_rec.organization_id           := p_atp_record.organization_id;
7609                         l_mat_atp_info_rec.requested_date            := l_requested_ship_date;
7610                         l_mat_atp_info_rec.quantity_ordered          := p_atp_record.quantity_ordered;
7611                         l_mat_atp_info_rec.demand_class              := p_atp_record.demand_class;
7612                         l_mat_atp_info_rec.insert_flag               := p_atp_record.insert_flag;
7613                         l_mat_atp_info_rec.rounding_control_flag     := l_get_mat_in_rec.rounding_control_flag;
7614                         l_mat_atp_info_rec.dest_inv_item_id          := l_get_mat_in_rec.dest_inv_item_id;
7615                         l_mat_atp_info_rec.infinite_time_fence_date  := l_get_mat_in_rec.infinite_time_fence_date;
7616                         l_mat_atp_info_rec.plan_name                 := l_get_mat_in_rec.plan_name;
7617                         l_mat_atp_info_rec.optimized_plan            := l_get_mat_in_rec.optimized_plan;
7618                         l_mat_atp_info_rec.substitution_window       := l_substitution_window;
7619                         l_mat_atp_info_rec.atf_date                  := p_atp_record.atf_date;
7620                         l_mat_atp_info_rec.refresh_number            := p_refresh_number;   -- For summary enhancement
7621                         l_mat_atp_info_rec.shipping_cal_code         := p_atp_record.shipping_cal_code;
7622                         -- Bug 3371817 - Pass calendar code to calculate sys_next_date
7623                         --               In make case this will actually contain OMC for lower level components
7624 
7625                         MSC_ATP_REQ.Get_Material_Atp_Info(
7626                                 l_mat_atp_info_rec,
7627                                 l_atp_period,
7628                                 l_atp_supply_demand,
7629                                 l_return_status);
7630 
7631                         l_atf_date_qty                               := l_mat_atp_info_rec.atf_date_quantity;
7632                         l_atp_date_this_level                        := l_mat_atp_info_rec.atp_date_this_level;
7633                         l_atp_date_quantity_this_level               := l_mat_atp_info_rec.atp_date_quantity_this_level;
7634                         l_get_mat_out_rec.atp_rule_name              := l_mat_atp_info_rec.atp_rule_name;
7635                         l_get_mat_out_rec.infinite_time_fence_date   := l_mat_atp_info_rec.infinite_time_fence_date;
7636                         l_requested_date_quantity                    := l_mat_atp_info_rec.requested_date_quantity;
7637                         p_atp_record.atf_date_quantity               := l_atf_date_qty;
7638                         -- time_phased_atp changes end
7639                         /* rajjain changes for re-review comments
7640                            removed check for rule based AATP as this should be set
7641                            for all scenarios
7642                         IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 2 THEN
7643                           l_used_available_quantity := l_mat_atp_info_rec.atp_date_quantity_this_level;
7644                         END IF; --bug3409973*/
7645                         l_used_available_quantity := l_mat_atp_info_rec.atp_date_quantity_this_level;
7646 
7647 	             -- print the results from Get_Material_Atp_Info
7648 	             IF PG_DEBUG in ('Y', 'C') THEN
7649 	                msc_sch_wb.atp_debug('ATP_Check: ' || '********** Result from Get_Material_Atp_Info **********');
7650 	             END IF;
7651 
7652 	         END IF;	-- IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 1
7653 
7654                  -- Bug 3782472
7655                  IF PG_DEBUG in ('Y', 'C') THEN
7656                     msc_sch_wb.atp_debug('p_search '||p_search);
7657                     msc_sch_wb.atp_debug('MSC_ATP_PVT.G_PTF_DATE '||MSC_ATP_PVT.G_PTF_DATE);
7658                     msc_sch_wb.atp_debug('l_ptf_date '||l_ptf_date);
7659                     msc_sch_wb.atp_debug('l_requested_ship_date '||l_requested_ship_date);
7660                     msc_sch_wb.atp_debug('l_net_demand '||l_net_demand);
7661                  END IF;
7662                  IF p_search = FORWARD_SCHEDULING AND
7663                           l_requested_date_quantity >= p_atp_record.quantity_ordered and
7664                               MSC_ATP_PVT.G_PTF_DATE > l_requested_ship_date THEN
7665                     l_net_demand := l_net_demand;
7666                     l_atp_comp_flag := 'N';
7667                  ELSE
7668 
7669                     l_net_demand := p_atp_record.quantity_ordered -
7670                                               greatest(l_requested_date_quantity, 0);
7671                  END IF;
7672 
7673 	         IF PG_DEBUG in ('Y', 'C') THEN
7674 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_return_status : '|| l_return_status);
7675 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date : '|| l_requested_ship_date);
7676 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity : '|| l_requested_date_quantity);
7677 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level : '|| l_atp_date_this_level);
7678 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level : '|| l_atp_date_quantity_this_level);
7679 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity);
7680 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atf_date_qty : '|| l_atf_date_qty);
7681 	            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : '|| l_atp_pegging_tab.COUNT);
7682 	         END IF;
7683               ELSE
7684                IF PG_DEBUG in ('Y', 'C') THEN
7685                   msc_sch_wb.atp_debug('ATP_Check: ' || 'did not call get_material_atp_info');
7686                END IF;
7687                l_net_demand := p_atp_record.quantity_ordered;
7688                l_requested_date_quantity := 0.0; -- cchen 08/31
7689 
7690               END IF;
7691              ELSE
7692                /* --bug 4428128 : move this code down
7693                l_net_demand := p_atp_record.quantity_ordered;
7694                -- Bug 1587015, ngoel 1/19/2001
7695                l_requested_date_quantity := 0.0;
7696                */
7697 
7698                -- Bug 1566260, Initialize atp_date_this_level and atp_date_quantity_this_level to
7699                -- infinite_time_fence_date and INFINITE_NUMBER to avoid them to be null later on.
7700 
7701 	       IF PG_DEBUG in ('Y', 'C') THEN
7702 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'Initialize atp_date and atp_quantity_this_level');
7703 	       END IF;
7704 
7705                /* s_cto_rearch: chnage call to MSC_ATP_PROC.get_infinite_time_fence_date procedure
7706                l_atp_date_this_level := GREATEST(MSC_ATP_FUNC.get_infinite_time_fence_date(
7707                            p_atp_record.instance_id, p_atp_record.inventory_item_id,
7708                            p_atp_record.organization_id,p_plan_id), l_requested_ship_date);
7709                 e_cto_rearch*/
7710 
7711                --bug 4394904: Store ITF in seperate variable
7712                MSC_ATP_PROC.get_infinite_time_fence_date(
7713                             p_atp_record.instance_id, p_atp_record.inventory_item_id,
7714                             p_atp_record.organization_id,p_plan_id,
7715                             --l_atp_date_this_level, l_atp_rule_name);
7716                             l_infinite_time_fence, l_atp_rule_name);
7717                l_atp_date_this_level := GREATEST(l_infinite_time_fence, l_requested_ship_date);
7718 
7719 	       IF l_atp_date_this_level IS NOT NULL THEN
7720                   l_atp_date_quantity_this_level := INFINITE_NUMBER;
7721 	       END IF;
7722 
7723                --bug 4428128: if request date > IFT then do not explode sources
7724                IF l_requested_ship_date >= l_infinite_time_fence THEN
7725                    l_requested_date_quantity := INFINITE_NUMBER;
7726                    l_net_demand := 0;
7727                ELSE
7728                    l_net_demand := p_atp_record.quantity_ordered;
7729                    -- Bug 1587015, ngoel 1/19/2001
7730                    l_requested_date_quantity := 0.0;
7731                END IF;
7732 
7733 	       IF PG_DEBUG in ('Y', 'C') THEN
7734 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level : '|| l_atp_date_this_level);
7735 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level : '|| l_atp_date_quantity_this_level);
7736 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity : '|| l_used_available_quantity); --bug3409973
7737 	       END IF;
7738 
7739              END IF;
7740 
7741 	     -- print the net_demand
7742 	     IF PG_DEBUG in ('Y', 'C') THEN
7743 	        msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand : '|| to_char(l_net_demand));
7744 	     END IF;
7745 
7746              -- cchen 1238941
7747 
7748              -- this should be a good place to do substitute.
7749              -- we change the logic so that we do the substitutes check
7750              -- before we populate the supply pegging.
7751              -- for the primary. however, we need to populate the demand
7752              -- pegging tree so that the order in the pegging tree will be ok.
7753 
7754                -- prepare the insert record
7755                -- no matter it is a demand or not, we need to insert this
7756                -- record into database since it is a recursive procedure
7757                -- and we will rollback in ATP procedure if it is not a
7758                -- demand.
7759                --- Enhance CTO Phase 1 Req  #16 and #17
7760 
7761              IF NOT((p_search = FORWARD_SCHEDULING) AND
7762                     (MSC_ATP_PVT.G_INV_CTP = 4) AND
7763                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
7764                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
7765                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) and l_atp_flag = 'Y'
7766 
7767                   /*---bug 3524230 ---
7768                    Demand in demand priority allocated ATP for items with
7769                    atp flag is 'Y'  is added in get_forward_material_ATP info procedure.
7770                    The above condition restricts demand addition for such items.
7771 
7772                    If model's ATP flag is yes then ATP on model is performed only when
7773                    that model is made as model's supply statement is statement of resource capacity.
7774                    Therefore, resource capacity is used only when model is being made. Therefore,
7775                    the supply chian for model would be  POD for Model (Step 1) --> Make PO for Model
7776                       (Step 2) ---> POD for model + other components (step 3)
7777 
7778                    We do not need to check availability for model but do need to add demand in setp 1.
7779                    We do not call get_forward_material ATP info because we have a condition to not
7780                    call the procedure if we are in step 1. We were not adding demand here because
7781                    of above condition "l_atp_flag = 'Y'". As a result, demand for model was not getting added.
7782                    To correct this problem, we added the following condition.
7783                    If we are coming for second time for model then parent_item_is is populated.
7784                    In step 1 the condition below becomes false due
7785                    to null parent_item_id and the overall condition becomes true. Therefore, demand
7786                    for model gets added correctly. In setp 3, the demand becomes tru and hence overall
7787                    condition becomes false. Therfore, demand doesn't get added here but gets added
7788                    in get_forward_material_info. Thus, demand for model is maintained correctly.
7789 
7790                    */
7791                    AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
7792                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
7793                                       p_atp_record.parent_item_id is null))) THEN
7794                 -- Only Add Create Demand Record and Pegging when not dealing with Forward Stealing
7795                l_atp_insert_rec.instance_id := p_atp_record.instance_id;
7796                /* Modularize Item and Org Info */
7797                -- time_phased_atp changes begin
7798                IF G_INV_CTP = 5 THEN
7799                   l_atp_insert_rec.inventory_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7800                ELSE
7801                   l_atp_insert_rec.inventory_item_id := PF_ITEM_INFO_REC.dest_inv_item_id;
7802                END IF;
7803                l_atp_insert_rec.request_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
7804                l_atp_insert_rec.atf_date := p_atp_record.atf_date;
7805                l_atp_insert_rec.atf_date_quantity := l_atf_date_qty;
7806                l_atp_insert_rec.requested_date_quantity := l_requested_date_quantity;
7807                -- time_phased_atp changes end
7808 
7809                 -- No need to call function below.
7810                /*l_atp_insert_rec.inventory_item_id := MSC_ATP_FUNC.get_inv_item_id
7811                                                    (p_atp_record.instance_id,
7812                                                    --2152184
7813                                                    --p_atp_record.request_item_id,
7814 						   l_request_item_id,
7815                                                    null,
7816                                                    p_atp_record.organization_id);
7817                 Modularize Item and Org Info */
7818                l_atp_insert_rec.organization_id := p_atp_record.organization_id;
7819                l_atp_insert_rec.identifier := p_atp_record.identifier;
7820                l_atp_insert_rec.demand_source_type:=nvl(p_atp_record.demand_source_type, 2);
7821                l_atp_insert_rec.demand_source_header_id :=
7822                                nvl(p_atp_record.demand_source_header_id, -1);
7823                l_atp_insert_rec.demand_source_delivery :=
7824                                p_atp_record.demand_source_delivery;
7825 
7826                -- AATP: need to do something later on the reset the quantity
7827                -- back
7828 
7829                --steal_before_ctp
7830                --- if we are in allocated ATP and we want to do stealing then
7831                --- we will create demand for what is available. This way we wouldn't need to
7832                --- adjust the demand
7833 
7834                IF (l_net_demand > 0)
7835                     --stealing will be supported at all levels
7836                     -- AND ((p_parent_pegging_id IS NULL) OR -- Bug 2745728 Stealing for components.
7837                     --  NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id )
7838                    AND (p_search =1)
7839                    AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y')
7840                    --bug 3524230: When ATP flag for model is 'Y' then we do ATP on this model
7841                    --- only when we are making it. Therefore, we need to add full quantity
7842                    -- when not doing ATP check on the model
7843                    AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
7844                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
7845                                       p_atp_record.parent_item_id is null)) THEN
7846                   l_atp_insert_rec.quantity_ordered :=  LEAST(GREATEST(l_requested_date_quantity, 0),
7847                                                                       p_atp_record.quantity_ordered);
7848 	          IF PG_DEBUG in ('Y', 'C') THEN
7849            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.quantity_ordered : '||
7850                                                                     l_atp_insert_rec.quantity_ordered);
7851            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.quantity_ordered : '||
7852                                                                     p_atp_record.quantity_ordered);
7853            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity : '||
7854                                                                     l_requested_date_quantity);
7855            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'steal_before_ctp condition : ' );
7856 	          END IF;
7857                ELSE
7858                   l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
7859 	          IF PG_DEBUG in ('Y', 'C') THEN
7860            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.quantity_ordered : '||
7861                                                                     l_atp_insert_rec.quantity_ordered);
7862            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.quantity_ordered : '||
7863                                                                     p_atp_record.quantity_ordered);
7864            	      msc_sch_wb.atp_debug('ATP_Check: ' || 'steal_before_ctp Else : ' );
7865 	          END IF;
7866                END IF;
7867 
7868                l_atp_insert_rec.requested_ship_date := l_requested_ship_date;
7869                l_atp_insert_rec.demand_class := p_atp_record.demand_class;
7870                l_atp_insert_rec.refresh_number := p_refresh_number; -- summary enhancement p_atp_record.refresh_number;
7871                l_atp_insert_rec.order_number := p_atp_record.order_number;
7872 
7873                /* Enhance CTO Phase 1 Req #16 */
7874                -- Create Sales Orders instead of Plan Order Demands for
7875                -- CTO items and set the order line id for the item
7876                -- only for the appropriate organizations.
7877                IF (p_parent_pegging_id is NULL) or MSC_ATP_PVT.G_INV_CTP = 5 THEN
7878                    --NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) THEN
7879 
7880                  -- this is top level demand, which we should consider it
7881                  -- as sales order demand.
7882 
7883                  IF PG_DEBUG in ('Y', 'C') THEN
7884                     msc_sch_wb.atp_debug('Either ODS based ATP or top level demand. Add as a SO Demand');
7885                  END IF;
7886 
7887 	         -- Modified by ngoel on 1/12/2001 for origination_type = 30
7888                  --l_atp_insert_rec.origination_type := 6;
7889                  l_atp_insert_rec.origination_type := 30;
7890 
7891                  l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
7892                  l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
7893                ELSE
7894                  -- this is not the top level demand, which we should consider it
7895                  -- as planned order demand
7896 
7897                  -- ATP4drp If we are dealing with a component
7898                  -- then set the origination type to Constrained Kit Demand
7899                  -- for DRP plans.
7900                  IF (NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id) <> p_atp_record.inventory_item_id AND
7901                       NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN
7902                       l_atp_insert_rec.origination_type := 47;
7903                       IF PG_DEBUG in ('Y', 'C') THEN
7904                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7905                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP origination_type ='|| l_atp_insert_rec.origination_type);
7906                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP parent_item_id ='|| p_atp_record.parent_item_id);
7907                           msc_sch_wb.atp_debug('ATP_Check: ' || 'DRP child_item_id ='|| p_atp_record.inventory_item_id);
7908                           msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
7909                       END IF;
7910                  ELSE
7911                       l_atp_insert_rec.origination_type := 1;
7912                  END IF;
7913                  -- End ATP4drp
7914                  IF (p_atp_record.parent_bom_item_type in (1, 4) and p_atp_record.parent_repl_order_flag = 'Y')
7915                     --parent is model entity
7916                     OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
7917                      ---always add line id for lower level model components
7918                      l_atp_insert_rec.demand_source_line := p_atp_record.demand_source_line;
7919                  END IF;
7920 
7921                END IF;
7922                /* End Enhance CTO Phase 1 Req #16 */
7923                -- for performance reason, we call these function here and
7924                -- then populate the pegging tree with the values
7925 
7926                -- for performance reason, we call these function here and
7927                -- then populate the pegging tree with the values
7928 
7929                /* Modularize Item and Org Info */
7930                -- Re-Use info instead of making unnecessary call.
7931                l_inv_item_name := C_ITEM_INFO_REC.item_name;
7932                /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
7933                                          p_atp_record.inventory_item_id,
7934                                          p_atp_record.organization_id);
7935 
7936                 Modularize Item and Org Info */
7937                MSC_ATP_PROC.get_global_org_info (p_atp_record.instance_id,
7938                                                   p_atp_record.organization_id);
7939                l_org_code := G_ORG_INFO_REC.org_code;
7940                /* l_org_code := MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
7941                                           p_atp_record.organization_id);
7942                 Modularize Item and Org Info */
7943 
7944                IF p_search = 1 and p_parent_pegging_id is null THEN
7945                   --- we need to pass old demand id so that we can update that demand
7946                   --- this will happen in case of reschedulling
7947                   l_atp_insert_rec.old_demand_id := p_atp_record.old_demand_id;
7948                ELSE
7949                   l_atp_insert_rec.old_demand_id := null;
7950                END IF;
7951 
7952 
7953    -- temporary
7954 	       IF PG_DEBUG in ('Y', 'C') THEN
7955 	          msc_sch_wb.atp_debug('ATP_Check: ' || 'temp l_net_demand : '|| l_net_demand);
7956 	       END IF;
7957                IF (p_search = 1 OR
7958                   (p_search= 2 AND p_parent_pegging_id is not null))
7959                   --- IN ODS ATP, we do not want add demand for model only once.
7960                   AND NOT (MSC_ATP_PVT.G_INV_CTP = 5 and p_atp_record.parent_item_id is not null) THEN
7961 
7962                  --s_cto_rearch
7963                  l_atp_insert_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
7964                  l_atp_insert_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
7965                  l_atp_insert_rec.Parent_line_id := p_atp_record.Parent_line_id;
7966                  l_atp_insert_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
7967                  l_atp_insert_rec.wip_supply_type := p_atp_record.wip_supply_type;
7968                  l_atp_insert_rec.mand_comp_flag := p_atp_record.mand_comp_flag;
7969                  --plan by request date enhancement
7970                  l_atp_insert_rec.ship_set_name := p_atp_record.ship_set_name;
7971                  l_atp_insert_rec.arrival_set_name := p_atp_record.arrival_set_name;
7972                  l_atp_insert_rec.original_request_ship_date :=l_original_req_ship_date;
7973                  IF PG_DEBUG in ('Y', 'C') THEN
7974                     msc_sch_wb.atp_debug('ATP_Check: ' || 'latest_acceptable_date: '|| l_atp_insert_rec.latest_acceptable_date);
7975                     msc_sch_wb.atp_debug('ATP_Check: ' || 'latest_acceptable_date: '|| l_atp_insert_rec.original_request_ship_date);
7976                  END IF;
7977                  l_atp_insert_rec.wip_supply_type := p_atp_record.mand_comp_flag;
7978                  --e_cto_rearch
7979 
7980                  -- ship_rec_cal
7981                  IF p_search = 1 and p_parent_pegging_id is null THEN
7982                      IF  p_atp_record.ship_method <> '@@@' THEN
7983                         l_atp_insert_rec.ship_method := p_atp_record.ship_method;
7984                      ELSE
7985                         l_atp_insert_rec.ship_method := null;
7986                      END IF;
7987                  END IF;
7988                  IF PG_DEBUG in ('Y', 'C') THEN
7989                      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_insert_rec.ship_method : '|| l_atp_insert_rec.ship_method);
7990                  END IF;
7991                  l_atp_insert_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
7992                  l_atp_insert_rec.delivery_lead_time := l_delivery_lead_time;
7993 
7994                  ---bug 4267076: Do not create demand for planned outbound shipment
7995                  IF NOT(p_parent_pegging_id is not null
7996                         AND NVL(p_atp_record.parent_item_id, p_atp_record.inventory_item_id)
7997                                                                   = p_atp_record.inventory_item_id
7998                         AND NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) THEN  --5067205
7999 
8000                     MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
8001                                  p_plan_id,
8002                                  l_demand_class_flag,
8003                                  l_demand_id);
8004                  END IF;
8005 
8006                  IF PG_DEBUG in ('Y', 'C') THEN
8007                     msc_sch_wb.atp_debug('ATP_Check: ' || 'after calling Add_Mat_Demand, l_demand_id ='||
8008                               l_demand_id);
8009                  END IF;
8010 
8011                  -- populate insert rec to pegging tree for this demand
8012 
8013                  l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8014                  --s_cto_rearch: store actual line id for lower level components
8015                  --l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8016                  l_pegging_rec.order_line_id :=  p_atp_record.demand_source_line;
8017                  l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
8018                  l_pegging_rec.atp_level:= p_level;
8019                  l_pegging_rec.organization_id:= p_atp_record.organization_id;
8020                  l_pegging_rec.organization_code := l_org_code;
8021                  l_pegging_rec.identifier1:= p_atp_record.instance_id;
8022                  l_pegging_rec.identifier2 := p_plan_id;
8023                  l_pegging_rec.identifier3 := l_demand_id;
8024 
8025                  -- time_phased_atp changes begin
8026                  IF (l_pf_atp = 'Y') or (G_INV_CTP = 5) THEN
8027                      l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
8028                      l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
8029                  ELSE
8030                      l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
8031                      l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
8032                  END IF;
8033                  l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
8034                  -- time_phased_atp changes end
8035 
8036                  l_pegging_rec.resource_id := NULL;
8037                  l_pegging_rec.resource_code := NULL;
8038                  l_pegging_rec.department_id := NULL;
8039                  l_pegging_rec.department_code := NULL;
8040                  l_pegging_rec.supplier_id := NULL;
8041                  l_pegging_rec.supplier_name := NULL;
8042                  l_pegging_rec.supplier_site_id := NULL;
8043                  l_pegging_rec.supplier_site_name := NULL;
8044                  l_pegging_rec.scenario_id:= p_scenario_id;
8045                  l_pegging_rec.supply_demand_source_type:= 6;
8046                  l_pegging_rec.supply_demand_quantity:=
8047                     p_atp_record.quantity_ordered ;
8048                  l_pegging_rec.supply_demand_type:= 1;
8049                  l_pegging_rec.supply_demand_date:= l_requested_ship_date;
8050 
8051 		 -- dsting ATO 2465370
8052 		 IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'Y'
8053 		    --AND (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id
8054 		    --OR  p_parent_pegging_id is null)
8055 		 THEN
8056 		    l_pegging_rec.required_date := l_requested_ship_date;
8057                     --bug 3328421
8058                     l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8059 		 ELSE
8060                     -- Bug 2748730. No need to move date to the end of as we will be doing that
8061 		    -- inside the procedure Add_Pegging
8062                     -- l_pegging_rec.required_date := TRUNC(l_requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY;
8063 		    l_pegging_rec.required_date := l_requested_ship_date ;
8064                     --bug 3328421
8065                     l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8066 		 END IF;
8067 
8068                  /* ship_rec_cal
8069                     calendar constraint*/
8070                  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 and MSC_ATP_PVT.G_CALENDAR_CONSTRAINT='Y'
8071                         and p_parent_pegging_id is NULL THEN
8072                         l_pegging_rec.constraint_type :=7;
8073                  END IF;
8074 
8075                  -- for demo:1153192
8076                  l_pegging_rec.constraint_flag := 'N';
8077 	         l_pegging_rec.component_identifier :=
8078                             NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
8079 
8080                  l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8081                  --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
8082                  --- cto looks at pegging tree to place their demands. Since CTO expects to find
8083                  --  id for the requested item, we add the following column. CTO will now read from this column
8084                  l_pegging_rec.request_item_id := p_atp_record.request_item_id;
8085 
8086                  --diag_atp
8087                  l_pegging_rec.pegging_type := MSC_ATP_PVT.ORG_DEMAND; --item demand node
8088 
8089                  --s_cto_rearch
8090 
8091                  IF (p_atp_record.parent_bom_item_type in (1, 4) and p_atp_record.parent_repl_order_flag = 'Y')
8092                     --parent is model entity
8093                     OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
8094                      l_pegging_rec.model_sd_flag := 1;
8095                  END IF;
8096                  l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8097                  l_pegging_rec.base_model_id := p_atp_record.base_model_id;
8098                  --e_cto_rearch
8099 
8100                  -- ship_rec_cal
8101                  IF p_search = 1 and p_parent_pegging_id is null THEN
8102                      IF  p_atp_record.ship_method <> '@@@' THEN
8103                         l_pegging_rec.ship_method := p_atp_record.ship_method;
8104                      ELSE
8105                         l_pegging_rec.ship_method := null;
8106                      END IF;
8107                  END IF;
8108 
8109                  -- Only Add Pegging when not dealing with Forward Stealing
8110                  l_pegging_rec.demand_class :=  p_atp_record.demand_class;
8111 
8112                  --4570421
8113                  l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
8114                  l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
8115                  l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
8116                  l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
8117                  l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
8118                  l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
8119                  l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
8120 
8121                  MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
8122 
8123                  IF p_search = 1 AND p_parent_pegging_id is null THEN
8124                    MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_pegging_id;
8125                  END IF;
8126                ELSE
8127                  l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8128 
8129 		 --dsting Stealing was here but commented out 10/17/02.
8130                END IF;
8131              ELSE
8132                   --for demand priority forward ATP we have already set l_pegging_id after get_forward
8133                   /*
8134                   l_pegging_id := MSC_ATP_PVT.G_CTO_FORWARD_DMD_PEG;
8135                   IF PG_DEBUG in ('Y', 'C') THEN
8136                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Pegging Id for Forward Stealing is : ' || l_pegging_id);
8137                   END IF;
8138 
8139                   */
8140                   IF p_parent_pegging_id is not null THEN
8141 
8142                     l_pegging_id := l_get_mat_out_rec.demand_pegging_id;
8143 
8144                   ELSE
8145 
8146                     l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8147 
8148                   END IF;
8149 
8150              END IF;
8151              --- End Enhance CTO Phase 1 Req #17
8152 
8153                IF PG_DEBUG in ('Y', 'C') THEN
8154                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Right before pegging');
8155                END IF;
8156 
8157                IF (p_parent_pegging_id is null) AND (p_search = 1) THEN
8158                    IF PG_DEBUG in ('Y', 'C') THEN
8159                       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_parent_pegging_id is null');
8160                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
8161                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_ATP_DATE.COUNT = ' || MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT);
8162                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_DATE_QTY.COUNT = ' || MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT);
8163                    END IF;
8164 
8165                    MSC_ATP_PVT.G_PEGGING_FOR_SET.Extend;
8166                    MSC_ATP_PVT.G_REQ_ATP_DATE.EXTEND;
8167  		   MSC_ATP_PVT.G_REQ_DATE_QTY.EXTEND;
8168 
8169                    IF PG_DEBUG in ('Y', 'C') THEN
8170                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PEGGING_FOR_SET.COUNT = ' ||MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT);
8171                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_ATP_DATE.COUNT = ' || MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT);
8172                       msc_sch_wb.atp_debug('ATP_Check: ' || 'G_REQ_DATE_QTY.COUNT = ' || MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT);
8173                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id );
8174                    END IF;
8175 
8176 
8177                    MSC_ATP_PVT.G_PEGGING_FOR_SET(MSC_ATP_PVT.G_PEGGING_FOR_SET.COUNT) := l_pegging_id;
8178                    MSC_ATP_PVT.G_REQ_ATP_DATE( MSC_ATP_PVT.G_REQ_ATP_DATE.COUNT) := l_requested_ship_date;
8179                    MSC_ATP_PVT.G_REQ_DATE_QTY(MSC_ATP_PVT.G_REQ_DATE_QTY.COUNT) := p_atp_record.Quantity_Ordered;
8180 
8181                END IF;
8182                IF PG_DEBUG in ('Y', 'C') THEN
8183                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Right after pegging');
8184                END IF;
8185 		-- dsting: 9/19/2002 component substitute check moved to
8186 		-- after move_sd_temp_to_mrp_details.
8187 
8188                IF (( p_search = 2 AND p_parent_pegging_id is not null)
8189                    OR (p_search=1)) AND
8190                   --bug 4428128: Add supply pegging if req date >= ITF
8191                   ((l_atp_flag in ('Y', 'C') AND
8192                   NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8193                                        --bug 3423277
8194                                        C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8195                                       p_atp_record.parent_item_id is null))
8196                  ---bug 4523103: Add pegging corretly if  req date >= ITF
8197                  or (l_atp_flag = 'N' and l_requested_ship_date >= l_infinite_time_fence)) AND
8198                  -- populate pegging tree for existing supply
8199                  -- If this is the backward search, no matter what we have
8200                  -- on the request date, we need to populate it.
8201                  (NVL(l_parent_peg_forward, 0) = 0) THEN
8202                    --- Enhance CTO Phase 1 Req #17
8203 
8204                  l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
8205                  l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
8206                  l_pegging_rec.parent_pegging_id:= l_pegging_id;
8207                  l_pegging_rec.atp_level:= p_level + 1;
8208                  l_pegging_rec.organization_id:= p_atp_record.organization_id;
8209                  l_pegging_rec.organization_code := l_org_code;
8210                  l_pegging_rec.identifier1:= p_atp_record.instance_id;
8211                  l_pegging_rec.identifier2 := p_plan_id;
8212                  l_pegging_rec.identifier3 := NULL;
8213 
8214                  -- time_phased_atp changes begin
8215                  IF l_time_phased_atp = 'Y' and l_requested_ship_date <= p_atp_record.atf_date THEN
8216                          l_pegging_rec.inventory_item_id:= l_request_item_id;
8217                          l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
8218                  ELSE
8219                          l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
8220                          l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
8221                  END IF;
8222                  l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
8223                  -- time_phased_atp changes end
8224                  --s_cto_rearch
8225                  l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
8226 
8227                  l_pegging_rec.resource_id := NULL;
8228                  l_pegging_rec.resource_code := NULL;
8229                  l_pegging_rec.department_id := NULL;
8230                  l_pegging_rec.department_code := NULL;
8231                  l_pegging_rec.supplier_id := NULL;
8232                  l_pegging_rec.supplier_name := NULL;
8233                  l_pegging_rec.supplier_site_id := NULL;
8234                  l_pegging_rec.supplier_site_name := NULL;
8235                  l_pegging_rec.scenario_id:= p_scenario_id;
8236                  l_pegging_rec.supply_demand_source_type:= ATP;
8237                  l_pegging_rec.supply_demand_quantity:=l_requested_date_quantity;
8238                  l_pegging_rec.supply_demand_date:= l_requested_ship_date;
8239                  l_pegging_rec.supply_demand_type:= 2;
8240                  l_pegging_rec.source_type := 0;
8241 	         l_pegging_rec.component_identifier :=
8242                             NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
8243 
8244                  -- for demo:1153192
8245                  IF ((p_search = 1)
8246                       AND (l_atp_comp_flag = 'N')
8247                       AND ( p_atp_record.quantity_ordered >=
8248                            l_requested_date_quantity)) THEN
8249                      l_pegging_rec.constraint_flag := 'Y';
8250                  ELSE
8251                      l_pegging_rec.constraint_flag := 'N';
8252 
8253                  END IF;
8254 
8255                  l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
8256                  --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
8257                  --- cto looks at pegging tree to place their demands. Since CTO expects to find
8258                  --  id for the requested item, we add the following column. CTO will now read from this column
8259                  l_pegging_rec.request_item_id := p_atp_record.request_item_id;
8260 
8261                  --diag_atp
8262                  l_pegging_rec.plan_name := p_atp_record.plan_name;
8263                  l_pegging_rec.required_quantity:= p_atp_record.quantity_ordered;
8264                  l_pegging_rec.required_date := l_requested_ship_date;
8265                  --bug 3328421
8266                  l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
8267                  l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
8268                  l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
8269                  l_pegging_rec.rounding_control := l_rounding_control_flag;
8270                  l_pegging_rec.atp_flag := l_atp_flag;
8271                  l_pegging_rec.atp_component_flag := l_atp_comp_flag;
8272                  l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
8273                  l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
8274 		 l_pegging_rec.processing_lead_time := l_process_lt; -- 5438149
8275                  l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
8276                  l_pegging_rec.fixed_lead_time := l_fixed_lt;
8277                  l_pegging_rec.variable_lead_time := l_variable_lt;
8278                  l_pegging_rec.weight_capacity := l_unit_weight;
8279                  l_pegging_rec.volume_capacity := l_unit_volume;
8280                  l_pegging_rec.weight_uom := l_weight_uom;
8281                  l_pegging_rec.volume_uom := l_volume_uom;
8282                  l_pegging_rec.allocation_rule := l_allocation_rule_name;
8283                  l_pegging_rec.substitution_window  := l_substitution_window;
8284                  l_pegging_rec.REVERSE_CUM_YIELD := p_atp_record.reverse_cumulative_yield;
8285                  l_pegging_rec.model_sd_flag := 2;
8286 
8287 
8288                  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 THEN
8289                      IF ((p_search = 1) and (l_atp_comp_flag = 'N') and
8290                            (p_atp_record.quantity_ordered > l_requested_date_quantity)) THEN
8291                         l_pegging_rec.constraint_type := 1;
8292                         p_atp_record.combined_requested_date_qty := l_requested_date_quantity;
8293 			p_atp_record.requested_date_quantity := l_requested_date_quantity;
8294                      END IF;
8295                  END IF;
8296 
8297                  l_pegging_rec.demand_class :=  p_atp_record.demand_class;
8298                  -- Bug 3826234
8299                  IF PG_DEBUG in ('Y', 'C') THEN
8300                     msc_sch_wb.atp_debug('ATP_Check: ' || '----------- Calendars passed to Pegging -----------------');
8301                     msc_sch_wb.atp_debug('ATP_Check: ' || 'shipping_cal_code = '      ||p_atp_record.shipping_cal_code);
8302                     msc_sch_wb.atp_debug('ATP_Check: ' || 'receiving_cal_code = '     ||p_atp_record.receiving_cal_code);
8303                     msc_sch_wb.atp_debug('ATP_Check: ' || 'intransit_cal_code = '     ||p_atp_record.intransit_cal_code);
8304                     msc_sch_wb.atp_debug('ATP_Check: ' || 'manufacturing_cal_code = ' ||p_atp_record.manufacturing_cal_code);
8305                  END IF;
8306                  IF p_parent_pegging_id is null then
8307                     l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
8308                     l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
8309                     l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
8310                     l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
8311                     IF PG_DEBUG in ('Y', 'C') THEN
8312                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside IF');
8313                     END IF;
8314                  ELSIF NVL(p_atp_record.to_organization_id,p_atp_record.organization_id)
8315                                                              <> p_atp_record.organization_id THEN
8316 
8317                     l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
8318                     l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
8319                     l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
8320                     l_pegging_rec.manufacturing_cal_code :=  NULL;
8321                     IF PG_DEBUG in ('Y', 'C') THEN
8322                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSIF');
8323                     END IF;
8324                  ELSE
8325                     l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
8326                     l_pegging_rec.shipping_cal_code      :=  NULL;
8327                     l_pegging_rec.receiving_cal_code     :=  NULL;
8328                     l_pegging_rec.intransit_cal_code     :=  NULL;
8329                     IF PG_DEBUG in ('Y', 'C') THEN
8330                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Inside ELSE');
8331                     END IF;
8332                  END IF;
8333                  -- Bug 3826234
8334 
8335                  --4570421
8336                  l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
8337                  l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
8338                  l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
8339                  l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
8340                  l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
8341                  l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
8342                  l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
8343 
8344                  MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
8345 
8346                  -- Add pegging_id to the l_atp_period and l_atp_supply_demand
8347 
8348                  FOR i in 1..l_atp_period.Level.COUNT LOOP
8349                    l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
8350                    l_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8351                  END LOOP;
8352 
8353 
8354                  -- dsting: supply/demand details pl/sql table no longer used
8355 /*                 FOR i in 1..l_atp_supply_demand.Level.COUNT LOOP
8356                    l_atp_supply_demand.Pegging_Id(i) := l_atp_pegging_id;
8357                    l_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8358                  END LOOP;
8359 */
8360 		 IF p_atp_record.insert_flag <> 0 THEN
8361 	   	         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
8362 				MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
8363    		 END IF;
8364 
8365 
8366                -- Now if this item was a component of a BOM, then check the
8367                -- substitutes and see if we can satisfy the demand
8368 
8369    -- Nagaraj
8370                IF ( p_search = BACKWARD_SCHEDULING) and (p_parent_pegging_id is not null --5006799
8371                    and l_net_demand > 0  and MSC_ATP_PVT.G_SUB_COMP = 'Y') THEN
8372                  IF PG_DEBUG in ('Y', 'C') THEN
8373                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Before check substitutes call:'||
8374                  to_char(p_parent_pegging_id) || ':' || to_char(l_net_demand));
8375                  END IF;
8376 
8377                  -- Setup new g_atp_record during each loop, we should not
8378                  -- resue the original record, because we need the original one
8379                  -- for CTP if there is not enough supply of components
8380                  g_atp_record.error_code := ALLSUCCESS;
8381                  g_atp_record.inventory_item_id := p_atp_record.inventory_item_id;
8382                  -- time_phased_atp changes begin
8383                  IF l_pf_atp = 'Y' THEN
8384                      g_atp_record.request_item_id := p_atp_record.inventory_item_id;
8385                  ELSE
8386                      g_atp_record.request_item_id := p_atp_record.request_item_id;
8387                  END IF;
8388                  g_atp_record.atf_date := p_atp_record.atf_date;
8389                  -- time_phased_atp changes end
8390                  g_atp_record.organization_id := p_atp_record.organization_id;
8391                  g_atp_record.instance_id := p_atp_record.instance_id;
8392                  g_atp_record.quantity_ordered := l_net_demand;
8393                  g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
8394                  g_atp_record.requested_ship_date :=
8395                              p_atp_record.requested_ship_date;
8396                  g_atp_record.requested_arrival_date :=
8397                              p_atp_record.requested_arrival_date;
8398                  g_atp_record.latest_acceptable_date :=
8399                              p_atp_record.latest_acceptable_date;
8400                  g_atp_record.delivery_lead_time :=
8401                              p_atp_record.delivery_lead_time;
8402                  g_atp_record.freight_carrier := p_atp_record.freight_carrier;
8403                  g_atp_record.ship_method := p_atp_record.ship_method;
8404                  g_atp_record.demand_class := p_atp_record.demand_class;
8405                  g_atp_record.override_flag := p_atp_record.override_flag;
8406                  g_atp_record.action := p_atp_record.action;
8407                  g_atp_record.ship_date := p_atp_record.ship_date;
8408                  g_atp_record.available_quantity :=
8409                              p_atp_record.available_quantity;
8410                  g_atp_record.used_available_quantity :=
8411                              p_atp_record.used_available_quantity; --bug3409973
8412                  g_atp_record.requested_date_quantity :=
8413                              p_atp_record.requested_date_quantity;
8414                  g_atp_record.supplier_id := p_atp_record.supplier_id;
8415                  g_atp_record.supplier_site_id := p_atp_record.supplier_site_id;
8416                  g_atp_record.insert_flag := p_atp_record.insert_flag;
8417                  g_atp_record.order_number := p_atp_record.order_number;
8418                  g_atp_record.demand_source_line :=
8419                              p_atp_record.demand_source_line;
8420                  g_atp_record.demand_source_header_id :=
8421                              p_atp_record.demand_source_header_id;
8422                  g_atp_record.demand_source_type :=
8423                              p_atp_record.demand_source_type;
8424                  g_atp_record.shipping_cal_code := p_atp_record.shipping_cal_code; -- Bug 3371817
8425                  g_atp_record.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
8426 
8427                  -- cchen : 1238941
8428                  -- temporary we set the the l_sub_requested_date_quantity
8429                  -- to the original net demand
8430                  l_sub_requested_date_quantity := l_net_demand;
8431                  --plan by request date enhancement
8432                  g_atp_record.ship_set_name := p_atp_record.ship_set_name;
8433                  g_atp_record.arrival_set_name := p_atp_record.arrival_set_name;
8434                  g_atp_record.original_request_ship_date := l_original_req_ship_date;
8435                  g_atp_record.bill_seq_id := p_atp_record.bill_seq_id; --4741012 passing bill id
8436                  -- l_sub_net_demand := l_net_demand;
8437                  --4902658 Start This is for primary component / 5216528
8438                  l_substitutes_rec.inventory_item_id.EXTEND;
8439                  l_substitutes_rec.pegging_id.EXTEND;
8440                  l_substitutes_rec.sub_atp_qty.EXTEND;
8441                  l_substitutes_rec.demand_id.EXTEND;
8442                  l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
8443                  l_substitutes_rec.quantity_ordered.EXTEND;
8444                  l_substitutes_rec.pf_item_id.EXTEND;
8445 
8446                  MSC_ATP_REQ.Check_Substitutes(
8447                   g_atp_record,
8448                   p_parent_pegging_id,
8449                   g_atp_record.instance_id,
8450                   p_scenario_id,
8451                   p_level,
8452                   p_search,
8453                   p_plan_id,
8454                   g_atp_record.inventory_item_id,
8455                   g_atp_record.organization_id,
8456                   g_atp_record.quantity_ordered,
8457                   l_net_demand,  -- out variable
8458                   x_atp_supply_demand,
8459                   x_atp_period,
8460                   l_substitutes_rec,--4902658/5216528
8461                   l_return_status,
8462                   p_refresh_number);    -- For summary enhancenment
8463                 -- this is to keep track how many we have in substitutes
8464 
8465                  -- 4902658/5216528 start this has main items's details
8466                  l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id; --4902658/5216528
8467                  l_substitutes_rec.pegging_id(1) := l_pegging_Id; --4902658/5216528
8468                  l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
8469                  l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
8470                                                                       p_atp_record.quantity_ordered);
8471                IF PG_DEBUG in ('Y', 'C') THEN
8472                  for ii in 1..l_substitutes_rec.inventory_item_id.count
8473                   loop
8474                     msc_sch_wb.atp_debug('From Substitute Array Item   : '|| l_substitutes_rec.inventory_item_id(ii));
8475                     msc_sch_wb.atp_debug('From Substitute Array Pegging: '|| l_substitutes_rec.pegging_id(ii));
8476                     msc_sch_wb.atp_debug('From Substitute Array ATP QTY: '|| l_substitutes_rec.sub_atp_qty(ii));
8477                   end loop;
8478                END IF;
8479                --4902658/5216528 end
8480                 -- cchen : 1238941
8481                 -- l_sub_net_demand := l_sub_net_demand - l_net_demand;
8482                 l_sub_requested_date_quantity := l_sub_requested_date_quantity -
8483                                                 greatest(l_net_demand, 0);
8484 
8485                 --4570421
8486                 IF PG_DEBUG in ('Y', 'C') THEN
8487                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand:'|| l_net_demand);
8488                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_sub_requested_date_quantity:'|| l_sub_requested_date_quantity);
8489                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.scaling_type'|| p_atp_record.scaling_type);
8490                    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);
8491                 END IF;
8492                 IF ( ( MSC_ATP_PVT.G_ORG_INFO_REC.org_type = MSC_ATP_PVT.DISCRETE_ORG AND p_atp_record.scaling_type = 2) OR
8493                      (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
8494                       -- Delete the supply pegging of the main component, if substitute can satisfy the lot-size.
8495                       MSC_ATP_DB_UTILS.delete_pegging( l_atp_pegging_id);
8496                 END IF;
8497 
8498                 IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP =1 THEN
8499                      IF ((p_search = 1) and (l_atp_comp_flag = 'N') and
8500                            (p_atp_record.quantity_ordered > l_requested_date_quantity)) THEN
8501                         l_pegging_rec.constraint_type := 1;
8502                         p_atp_record.combined_requested_date_qty := l_requested_date_quantity +
8503                                                                                    l_sub_requested_date_quantity;
8504                      END IF;
8505                 END IF;
8506 
8507                 IF PG_DEBUG in ('Y', 'C') THEN
8508                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After check substitutes:'||
8509                                              to_char(l_net_demand));
8510                 END IF;
8511 
8512                -- In order to show the correct demand quantity that got considered
8513                -- for the primary component, we need to change the requested
8514                -- quantity for that primary component to what was requested minus
8515                -- what got satisfied by the substitutes
8516 
8517                -- cchen : 1238941
8518                -- p_atp_record.quantity_ordered :=
8519                --          (p_atp_record.quantity_ordered - l_net_demand);
8520                -- l_atp_insert_rec.quantity_ordered :=
8521                --          (l_atp_insert_rec.quantity_ordered - l_net_demand);
8522                -- l_pegging_rec.supply_demand_quantity :=
8523                --          (l_pegging_rec.supply_demand_quantity - l_net_demand);
8524 
8525                -- update the primary demand pegging tree with right quantity since
8526                -- we do the substitute now.
8527                   --5220357 start
8528                  IF l_sub_requested_date_quantity > 0 THEN
8529                    IF PG_DEBUG in ('Y', 'C') THEN
8530                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Update POD for Primary Component');
8531                    END IF;
8532 		   IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
8533                    	MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id,
8534                                   null,
8535                                   p_atp_record.quantity_ordered-
8536                                   l_sub_requested_date_quantity );
8537 		   END IF;
8538 
8539                    update msc_demands
8540                    set    USING_REQUIREMENT_QUANTITY =
8541                           MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-
8542                            greatest(l_sub_requested_date_quantity, 0)))  -- 5598066
8543                    where  demand_id = l_demand_id
8544                    and    plan_id = p_plan_id
8545                    returning inventory_item_id into l_inventory_item_id;
8546 
8547                    IF PG_DEBUG in ('Y', 'C') THEN
8548                       msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
8549                    END IF;
8550                    /* time_phased_atp
8551                       need to check this while UT*/
8552                    IF l_time_phased_atp = 'Y' THEN
8553 		        --using the same insert rec we prepared earlier
8554 		        l_atp_insert_rec.quantity_ordered :=  l_atp_insert_rec.quantity_ordered - greatest(l_sub_requested_date_quantity, 0);
8555 		        l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.requested_date_quantity - greatest(l_sub_requested_date_quantity, 0);
8556 		        -- No need to change ATF date qty, let it be the same. Increment_Bucketed_Demands_Qty will take care of whether to use
8557 		        -- qty upto ATF completely.
8558 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
8559                                 l_atp_insert_rec,
8560                                 p_plan_id,
8561                                 l_demand_id,
8562                                 l_return_status
8563 		        );
8564                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8565                            IF PG_DEBUG in ('Y', 'C') THEN
8566                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
8567                            END IF;
8568                            RAISE FND_API.G_EXC_ERROR;
8569                         END IF;
8570                    ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
8571                       AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
8572                           update msc_alloc_demands
8573                           set allocated_quantity = (allocated_quantity -
8574                                                     greatest(l_sub_requested_date_quantity, 0))
8575                           where parent_demand_id = l_demand_id --updating for primary only
8576                           and   plan_id = p_plan_id;
8577 
8578                           IF PG_DEBUG in ('Y', 'C') THEN
8579                              msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
8580                           END IF;
8581                    END IF;
8582                    IF PG_DEBUG in ('Y', 'C') THEN
8583                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
8584                       msc_sch_wb.atp_debug('ATP_Check: ' || ' MSC_ATP_PVT.G_SUMMARY_FLAG := ' ||  MSC_ATP_PVT.G_SUMMARY_FLAG);
8585                    END IF;
8586 
8587                    -- update summary records removed in ODS case - for summary enhancement
8588                    IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
8589                        MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
8590                                                           p_plan_id,
8591                                                           p_atp_record.organization_id,
8592                                                           l_inventory_item_id,
8593                                                           l_requested_ship_date,
8594                                                           null,
8595                                                           null,
8596                                                           null,
8597                                                           null,
8598                                                           null,
8599                                                           null,
8600                                                           2);
8601                        update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
8602                        set sd_qty = sd_qty + greatest(l_sub_requested_date_quantity, 0)
8603                        where plan_id = p_plan_id
8604                        and sr_instance_id  = p_atp_record.instance_id
8605                        and inventory_item_id = l_inventory_item_id
8606                        and organization_id = p_atp_record.organization_id
8607                        and sd_date = trunc(l_requested_ship_date);
8608                        commit;
8609                    END IF;
8610 
8611                  END IF;
8612                  --5220357 end
8613              ELSE --4902658/5216528 start if no check_substitutes gets called in b/w pass
8614                 l_substitutes_rec.inventory_item_id.EXTEND;
8615                 l_substitutes_rec.pegging_id.EXTEND;
8616                 l_substitutes_rec.sub_atp_qty.EXTEND;
8617                 l_substitutes_rec.demand_id.EXTEND;
8618                 l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
8619                 l_substitutes_rec.quantity_ordered.EXTEND;
8620                 l_substitutes_rec.pf_item_id.EXTEND;
8621 
8622                 l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
8623 
8624                 l_substitutes_rec.inventory_item_id(1) := p_atp_record.request_Item_Id;
8625 
8626                 l_substitutes_rec.pegging_id(1) := l_pegging_Id;
8627                 l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
8628                                                            p_atp_record.quantity_ordered);
8629                 --4902658/5216528 end
8630                END IF; -- for substitute check
8631    -- Nagaraj
8632 
8633                  -- 1413608: instead of replace x_atp with l_atp, append it
8634                  -- x_atp_supply_demand := l_atp_supply_demand;
8635                  -- x_atp_period := l_atp_period;
8636 
8637                  MSC_ATP_PROC.Details_Output(l_atp_period,
8638                                 l_atp_supply_demand,
8639                                 x_atp_period,
8640                                 x_atp_supply_demand,
8641                                 l_return_status);
8642               ELSE --4902658/5216528 start For f/w pass when check_substitutes is not callled.
8643                  l_substitutes_rec.inventory_item_id.EXTEND;
8644                  l_substitutes_rec.pegging_id.EXTEND;
8645                  l_substitutes_rec.sub_atp_qty.EXTEND;
8646                  l_substitutes_rec.demand_id.EXTEND;
8647                  l_substitutes_rec.atf_date_quantity.EXTEND; --5283809
8648                  l_substitutes_rec.quantity_ordered.EXTEND;
8649                  l_substitutes_rec.pf_item_id.EXTEND;
8650 
8651                  l_substitutes_rec.demand_id(1) := l_demand_id; --4902658/5216528
8652                  l_substitutes_rec.inventory_item_id(1) := p_atp_record.Request_Item_Id;
8653                  l_substitutes_rec.pegging_id(1) := l_pegging_Id;
8654                  l_substitutes_rec.sub_atp_qty(1) :=  LEAST(GREATEST(l_requested_date_quantity, 0),
8655                                                             p_atp_record.quantity_ordered);
8656                  --4902658/5216528 end
8657                END IF;
8658 
8659                --- Enhance CTO Phase 1 Req #17
8660                -- for aatp --- steal_before_ctp
8661                -- Only do stealing if ATP flag for the item is Yes.
8662                --s_cto_rearch: Do stealing for all levels in all orgs
8663                /* IF (l_net_demand > 0) AND -- Bug 2745728 removed p_parent_pegging_id clause
8664                    --- Enhance CTO Phase 1 Req #17
8665                        -- Support Backward Stealing for components of ATO model in
8666                        -- Model's sourcing organization.
8667                     ( (p_parent_pegging_id IS NULL) OR
8668                         NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) AND
8669                     --- End Enhance CTO Phase 1 Req #17
8670                   (p_search =1)
8671                   AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y') THEN
8672 
8673                --IF ((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_net_demand > 0) AND
8674                --    (l_atp_flag = 'Y') AND (l_atp_comp_flag = 'N') AND
8675                --    (p_parent_pegging_id is null)) THEN
8676                */
8677                IF (l_net_demand > 0) AND
8678                    (p_search =1) AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (l_atp_flag = 'Y')
8679                               --bug 3423277: Restrict stealing only for cases where we are
8680                                ---doing atp check for model.
8681                               AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
8682                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
8683                                       p_atp_record.parent_item_id is null)) THEN
8684                    --- Enhance CTO Phase 1 Req #17
8685                        -- Support Backward Stealing for components of ATO model in
8686                        -- Model's sourcing organization.
8687                        -- Do not reset the parent pegging Id in such a case.
8688                    /* We support stealing at all levels.
8689                    IF (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id) THEN
8690                        l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8691                    END IF;
8692                    */
8693 
8694                    IF p_parent_pegging_id is null THEN
8695                         l_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8696                    END IF;
8697                    --- End Enhance CTO Phase 1 Req #17
8698 
8699                    IF PG_DEBUG in ('Y', 'C') THEN
8700                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Start stealing in backward case, before CTP');
8701                    END IF;
8702                    -- AATP: here we need to do the stealing.
8703                    -- Only do stealing if ATP flag for the item is Yes.
8704                    g_atp_record.error_code := ALLSUCCESS;
8705                    g_atp_record.inventory_item_id := p_atp_record.inventory_item_id;
8706                    -- time_phased_atp
8707                    IF l_pf_atp='Y' THEN
8708                        g_atp_record.request_item_id := p_atp_record.inventory_item_id;
8709                    ELSE
8710                        g_atp_record.request_item_id := p_atp_record.request_item_id;
8711                    END IF;
8712                    g_atp_record.organization_id := p_atp_record.organization_id;
8713                    g_atp_record.instance_id := p_atp_record.instance_id;
8714                    g_atp_record.quantity_ordered := l_net_demand;
8715                    g_atp_record.quantity_UOM := p_atp_record.quantity_UOM;
8716                    g_atp_record.requested_ship_date := l_requested_ship_date;
8717                    g_atp_record.requested_arrival_date := null;
8718                    g_atp_record.latest_acceptable_date :=
8719                              p_atp_record.latest_acceptable_date;
8720                    g_atp_record.delivery_lead_time :=
8721                              p_atp_record.delivery_lead_time;
8722                    g_atp_record.freight_carrier := p_atp_record.freight_carrier;
8723                    g_atp_record.ship_method := p_atp_record.ship_method;
8724                    g_atp_record.demand_class := p_atp_record.demand_class;
8725                    g_atp_record.override_flag := p_atp_record.override_flag;
8726                    g_atp_record.action := p_atp_record.action;
8727                    g_atp_record.ship_date := p_atp_record.ship_date;
8728                    g_atp_record.available_quantity :=
8729                              p_atp_record.available_quantity;
8730                    g_atp_record.used_available_quantity :=
8731                              p_atp_record.used_available_quantity;
8732                    g_atp_record.requested_date_quantity :=
8733                              p_atp_record.requested_date_quantity;
8734                    g_atp_record.supplier_id := p_atp_record.supplier_id;
8735                    g_atp_record.supplier_site_id := p_atp_record.supplier_site_id;
8736                    g_atp_record.insert_flag := p_atp_record.insert_flag;
8737                    g_atp_record.order_number := p_atp_record.order_number;
8738                    g_atp_record.demand_source_line :=
8739                              p_atp_record.demand_source_line;
8740                    g_atp_record.demand_source_header_id :=
8741                              p_atp_record.demand_source_header_id;
8742                    g_atp_record.demand_source_type :=
8743                              p_atp_record.demand_source_type;
8744                    g_atp_record.shipping_cal_code := p_atp_record.shipping_cal_code; -- Bug 3371817
8745                    g_atp_record.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
8746                    g_atp_record.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
8747                    g_atp_record.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
8748                    g_atp_record.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
8749 
8750                    -- for bug 1410327
8751                    g_atp_record.identifier :=
8752                              p_atp_record.identifier;
8753 
8754                    l_stealing_requested_date_qty := l_net_demand;
8755 
8756                   --pumehta Bug2471377 Copy the period ATP to another table
8757                   --Which can be used later for another forward pass
8758                   --to find better date for the case where component
8759                   --Flag is set to 'N'and stealing has happened.
8760 		  --dsting port 2621270. For sd details, we save the pegging id
8761                   --and copy the data from mrp_atp_details and give it a new pegging id
8762 
8763                    l_atp_period_first := l_atp_period;
8764                    --diag-atp
8765                    g_atp_record.plan_name := p_atp_record.plan_name;
8766 
8767                    -- time_phased_atp
8768                    g_atp_record.atf_date := p_atp_record.atf_date;
8769                    g_atp_record.ato_model_line_id := p_atp_record.ato_model_line_id;
8770                    MSC_AATP_PVT.Stealing(
8771                             g_atp_record,
8772                             l_pegging_id,
8773                             p_scenario_id,
8774                             p_level,
8775                             p_search,
8776                             p_plan_id,
8777                             l_net_demand,  -- out variable
8778                             l_mem_stealing_qty, -- For time_phased_atp
8779                             l_pf_stealing_qty,  -- For time_phased_atp
8780                             l_atp_supply_demand,
8781                             l_atp_period,
8782                             l_return_status,
8783                             p_refresh_number);   -- For summary enhancement
8784 
8785                    l_stealing_qty := l_stealing_requested_date_qty -
8786                                      greatest(l_net_demand, 0);
8787 
8788                    IF PG_DEBUG in ('Y', 'C') THEN
8789                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_stealing_qty = '||l_stealing_qty);
8790                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mem_stealing_qty = '||l_mem_stealing_qty);
8791                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pf_stealing_qty = '||l_pf_stealing_qty);
8792                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand = ' || l_net_demand);
8793                    END IF;
8794 
8795                    -- using the pegging_id to find the demand_id and plan_id
8796                    ---demandxyz
8797                    --- Initially we created the demand for l_requested_date_qty
8798                    --- now we update the quantity
8799                    update msc_demands
8800                    set USING_REQUIREMENT_QUANTITY = p_atp_record.quantity_ordered
8801                    where plan_id = p_plan_id and
8802                          demand_id = l_demand_id;
8803 
8804                    /* time_phased_atp
8805                       put qty stolen upto req date ((not upto ATF) on member item */
8806                    IF l_time_phased_atp = 'Y' THEN
8807 		        --using the same insert rec we prepared earlier
8808 		        l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
8809 		        l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.requested_date_quantity + nvl(l_stealing_qty, 0);
8810 		        l_atp_insert_rec.atf_date_quantity := l_atp_insert_rec.atf_date_quantity + nvl(l_mem_stealing_qty, 0);
8811 		        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
8812                                 l_atp_insert_rec,
8813                                 p_plan_id,
8814                                 l_demand_id,
8815                                 l_return_status
8816 		        );
8817                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8818                            IF PG_DEBUG in ('Y', 'C') THEN
8819                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
8820                            END IF;
8821                            RAISE FND_API.G_EXC_ERROR;
8822                         END IF;
8823                         /* update atf date quantity */
8824                         p_atp_record.atf_date_quantity := l_atp_insert_rec.atf_date_quantity;
8825                         /* Reset l_atf_date_qty to 0*/
8826                         l_atf_date_qty := 0;
8827                    ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
8828                           AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
8829 
8830                              update msc_alloc_demands
8831                              set allocated_quantity = p_atp_record.quantity_ordered
8832                              where parent_demand_id = l_demand_id
8833                              and   plan_id = p_plan_id;
8834 
8835                    END IF;
8836 
8837                    IF (l_stealing_qty > 0) THEN
8838                          ----bug 1892779
8839                         MSC_ATP_PROC.Details_Output(l_atp_period,
8840                                  l_atp_supply_demand,
8841                                  x_atp_period,
8842                                  x_atp_supply_demand,
8843                                  l_return_status);
8844                    END IF;
8845 
8846                    l_stealing_requested_date_qty := l_stealing_requested_date_qty -
8847                                                 l_net_demand;
8848 
8849                    l_net_demand := greatest(l_net_demand, 0);
8850 
8851                    IF PG_DEBUG in ('Y', 'C') THEN
8852                       msc_sch_wb.atp_debug('ATP_Check: ' || 'After Stealing :'|| l_net_demand);
8853                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_stealing_requested_date_qty = '||
8854                                           l_stealing_requested_date_qty);
8855                    END IF;
8856    /* bug 1680212
8857                    -- In order to show the correct demand quantity that got
8858                    -- considered for the primary component,
8859                    -- we need to change the requested quantity for that primary
8860                    -- component to what was requested minus
8861                    -- what got satisfied by the stealing
8862 
8863                    update msc_demands
8864                    set    USING_REQUIREMENT_QUANTITY =
8865                           (p_atp_record.quantity_ordered - l_stealing_qty)
8866                    where  demand_id = l_demand_id
8867                    and    plan_id = p_plan_id;
8868      bug 1680212 */
8869                END IF; -- end if G_ALLOCATED_ATP
8870                -- PTF
8871                IF (p_search = FORWARD_SCHEDULING) AND
8872                   (p_parent_pegging_id IS NOT  NULL) AND
8873                   (MSC_ATP_PVT.G_PTF_DATE > l_requested_ship_date) THEN
8874                   IF PG_DEBUG in ('Y', 'C') THEN
8875                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Look forward to meet the remaining shortage of comp');
8876                   END IF;
8877 		   --Bug2682094 Do not reset the demand
8878 		   --l_net_demand := p_atp_record.quantity_ordered;
8879                   l_req_date_qty := 0;
8880                ELSE
8881                   l_req_date_qty := null;
8882                END IF;
8883 
8884           END IF; -- if SUBS_FLAG = 'Y'
8885 
8886 
8887             ---here we add hack for config item where flag  = 'Y', 'N'
8888             IF l_net_demand > 0 and C_ITEM_INFO_REC.bom_item_type = 1 and
8889                                     C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' and
8890                                     C_ITEM_INFO_REC.atp_flag = 'Y' and
8891                                     C_ITEM_INFO_REC.atp_comp_flag = 'N' and
8892                                     MSC_ATP_PVT.G_INV_CTP = 4 THEN
8893 
8894                 MSC_ATP_CTO.Check_Base_Model_For_Cap_Check(C_ITEM_INFO_REC.dest_inv_item_id,
8895                                                            C_ITEM_INFO_REC.base_item_id,
8896                                                            l_requested_ship_date,
8897                                                            p_atp_record.instance_id,
8898                                                            p_plan_id,
8899                                                            p_atp_record.organization_id,
8900                                                            l_net_demand,
8901                                                            l_sr_inv_item_id,
8902                                                            l_check_model_capacity);
8903 
8904                 IF PG_DEBUG in ('Y', 'C') THEN
8905                    msc_sch_wb.atp_debug('l_check_model_capacity := ' || l_check_model_capacity);
8906                 END IF;
8907                 IF l_check_model_capacity = 1 THEN
8908                     --- we set comp flag to  Y so that it goes into the logic of make
8909                     l_atp_comp_flag := 'Y';
8910                     --null out the lead time as well as we want to check the base model's capacity same day
8911 
8912                     l_fixed_lt := 0;
8913                     l_variable_lt := 0;
8914                     l_pre_pro_lt := 0;
8915                 END IF;
8916             END IF;
8917 
8918             IF l_net_demand <= 0 THEN
8919               IF PG_DEBUG in ('Y', 'C') THEN
8920                  msc_sch_wb.atp_debug('Demabnd is Met');
8921                  msc_sch_wb.atp_debug('Requested Date quantity := ' ||  l_requested_date_quantity);
8922               END IF;
8923                -- we meet the requirements!!!
8924                --commenting for bug3550292
8925               /*p_atp_record.requested_date_quantity:= l_requested_date_quantity
8926                                                      +  l_sub_requested_date_quantity
8927                                                      + l_stealing_qty;*/
8928               --bug3550292
8929               p_atp_record.requested_date_quantity:= l_requested_date_quantity;
8930               -- cchen: 1238941
8931               -- 1411818: add greatest
8932               p_atp_record.combined_requested_date_qty :=
8933                   GREATEST(p_atp_record.requested_date_quantity, 0) +
8934                   l_sub_requested_date_quantity + l_stealing_requested_date_qty;
8935 
8936               /* s_cto_rearch: do not honor atp lead time
8937               p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
8938                                  (p_atp_record.organization_id,
8939                                   p_atp_record.instance_id,
8940                                   1,
8941                                   l_requested_ship_date, -- bug 1338599
8942                                   NVL(p_atp_record.atp_lead_time, 0));
8943               */
8944               p_atp_record.ship_date := l_requested_ship_date;
8945               -- for aatp
8946               p_atp_record.available_quantity:=
8947                   p_atp_record.combined_requested_date_qty;
8948               p_atp_record.used_available_quantity:=
8949                   p_atp_record.combined_requested_date_qty; --bug3409973
8950               -- p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
8951 
8952 	      IF PG_DEBUG in ('Y', 'C') THEN
8953 	         msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atf_date_quantity = '||p_atp_record.atf_date_quantity);
8954 	         msc_sch_wb.atp_debug('ATP_Check: ' || 'after insert row, 1');
8955 	      END IF;
8956             --diag_atp
8957             ELSIF (l_atp_comp_flag = 'N') AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
8958                 IF PG_DEBUG in ('Y', 'C') THEN
8959                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Component flag = N');
8960                 END IF;
8961 
8962                 -- we cannot meet the requirements, so we provide
8963                 -- the information at this level
8964                 p_atp_record.requested_date_quantity:=l_requested_date_quantity;
8965 
8966 		IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
8967 		   (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
8968 		   (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1)
8969                    --support forward stealing for all levels
8970 		   AND p_parent_pegging_id IS NULL
8971 		THEN
8972 		   IF PG_DEBUG in ('Y', 'C') THEN
8973 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'Get forward mat...');
8974 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'requested_date quantity: ' || l_requested_date_quantity);
8975 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_demand: ' || l_net_demand);
8976 		      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_demand_id: ' || l_demand_id);
8977 		   END IF;
8978 
8979 		   update msc_demands
8980 		   set using_requirement_quantity = MSC_ATP_UTILS.Truncate_Demand(p_atp_record.requested_date_quantity + l_stealing_requested_date_qty)
8981 		   where demand_id = l_demand_id
8982 	      	   and plan_id = p_plan_id;
8983 
8984 	     	   -- time_phased_atp changes begin
8985 		   IF l_time_phased_atp = 'Y' THEN
8986         	        --using the same insert rec we prepared earlier
8987         	        l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered - l_net_demand;
8988                         l_atp_insert_rec.requested_date_quantity := GREATEST(p_atp_record.requested_date_quantity, 0)
8989                                                                                  + nvl(l_stealing_requested_date_qty, 0);
8990         	        l_atp_insert_rec.atf_date_quantity := NVL(p_atp_record.atf_date_quantity, 0);
8991 
8992         	        MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
8993                                         l_atp_insert_rec,
8994                                         p_plan_id,
8995                                         l_demand_id,
8996                                         l_return_status
8997         		        );
8998                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8999                            IF PG_DEBUG in ('Y', 'C') THEN
9000                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
9001                             END IF;
9002                             RAISE FND_API.G_EXC_ERROR;
9003                         END IF;
9004 	           ELSE
9005         	        update msc_alloc_demands
9006         		set allocated_quantity = p_atp_record.requested_date_quantity + l_stealing_requested_date_qty
9007         		where parent_demand_id = l_demand_id
9008         		and plan_id = p_plan_id;
9009 		   END IF;
9010 	       	   -- time_phased_atp changes end
9011 
9012                    --diag_atp
9013                    l_get_mat_in_rec.rounding_control_flag := l_rounding_control_flag;
9014                    l_get_mat_in_rec.dest_inv_item_id := l_dest_inv_item_id;
9015                    l_get_mat_in_rec.parent_bom_item_type := p_atp_record.parent_bom_item_type;
9016                    l_get_mat_in_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
9017                    l_get_mat_in_rec.replenish_to_ord_flag := C_ITEM_INFO_REC.replenish_to_ord_flag;
9018                    l_get_mat_in_rec.parent_repl_order_flag := p_atp_record.parent_repl_order_flag;
9019                    l_get_mat_in_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
9020                    l_get_mat_in_rec.shipping_cal_code := p_atp_record.shipping_cal_code;
9021                    l_get_mat_in_rec.receiving_cal_code := p_atp_record.receiving_cal_code; -- Bug 3826234
9022                    l_get_mat_in_rec.intransit_cal_code := p_atp_record.intransit_cal_code; -- Bug 3826234
9023                    l_get_mat_in_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code; -- Bug 3826234
9024                    l_get_mat_in_rec.to_organization_id := p_atp_record.to_organization_id; -- Bug 3826234
9025                    -- Bug 3371817 - Pass calendar code to calculate sys_next_date
9026                    --               In make case this will actually contain OMC for lower level components
9027 
9028 
9029 		   MSC_AATP_REQ.Get_Forward_Material_ATP(
9030                             p_atp_record.instance_id,
9031                             p_plan_id,
9032                             p_level+1,
9033                             p_atp_record.identifier,
9034                             p_atp_record.demand_source_type,--cmro
9035                             p_scenario_id,
9036                             p_atp_record.inventory_item_id,
9037                             p_atp_record.request_item_id, -- For time_phased_atp
9038                             p_atp_record.organization_id,
9039                             C_ITEM_INFO_REC.item_name,   -- Modular re-use
9040                             PF_ITEM_INFO_REC.item_name,  -- For time_phased_atp
9041                             l_requested_ship_date,
9042                             l_net_demand,
9043                             p_atp_record.demand_class,
9044                             l_requested_date_quantity,
9045                             l_atf_date_qty, -- For time_phased_atp
9046                             l_atp_date_this_level,
9047                             l_atp_quantity_this_level_fwd,
9048                             l_atp_pegging_tab,
9049                             l_return_status,
9050                             l_used_available_quantity_fwd, --bug3409973
9051                             --diag_atp,
9052                             null,
9053                             l_get_mat_in_rec,
9054                             l_get_mat_out_rec,
9055                             p_atp_record.atf_date, -- For time_phased_atp
9056                             null,               -- order number
9057                             p_refresh_number    -- For summary enhancement
9058 			);
9059 		   l_atp_date_quantity_this_level :=
9060                   		GREATEST(p_atp_record.requested_date_quantity, 0) +
9061                   		l_sub_requested_date_quantity +
9062 				l_atp_quantity_this_level_fwd;
9063 	           l_used_available_quantity :=
9064                   		GREATEST(p_atp_record.requested_date_quantity, 0) +
9065                   		l_sub_requested_date_quantity +
9066 				l_used_available_quantity_fwd; --bug3409973
9067                    -- time_phased_atp
9068                    IF l_requested_ship_date <= p_atp_record.atf_date THEN
9069                        p_atp_record.atf_date_quantity := NVL(l_atp_insert_rec.requested_date_quantity, 0) + NVL(l_atf_date_qty, 0);
9070                    ELSE
9071                        p_atp_record.atf_date_quantity := NVL(p_atp_record.atf_date_quantity, 0) + NVL(l_atf_date_qty, 0);
9072                    END IF;
9073 
9074 		   IF PG_DEBUG in ('Y', 'C') THEN
9075 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_date_this_level: ' || l_atp_date_this_level);
9076 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'atp_qty_this_level: ' || l_atp_date_quantity_this_level);
9077 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_used_available_quantity: ' || l_used_available_quantity); --bug3409973
9078 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atf_date_qty : ' || l_atf_date_qty);
9079 		       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atf_date_quantity : ' || p_atp_record.atf_date_quantity);
9080 		   END IF;
9081 
9082 		ELSE
9083 		   IF (l_stealing_requested_date_qty > 0) AND
9084 		      (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2)
9085 		   THEN
9086         	        --pumehta Bug2471377 Begin Changes
9087         	       	--Make another pass to check if we can get a better date
9088 		        --as stealing has happened.
9089         	       	l_requested_quantity := p_atp_record.quantity_ordered -
9090                                 l_stealing_requested_date_qty;
9091 	               	IF PG_DEBUG in ('Y', 'C') THEN
9092 	               	   msc_sch_wb.atp_debug('ATP_Check: ' || 'Do a Second pass to find better date for Qty:' || l_requested_quantity);
9093 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Count:' || l_atp_period_first.period_start_date.COUNT);
9094 			END IF;
9095 
9096 			FOR i in 1..l_atp_period_first.period_start_date.COUNT LOOP
9097 				IF i <> l_atp_period_first.period_start_date.COUNT THEN
9098 					IF (l_atp_period_first.cumulative_quantity(i) >=
9099 					    l_requested_quantity) AND
9100 					   ((l_atp_period_first.period_start_date(i) <=
9101                        				l_sysdate AND
9102                        			    l_atp_period_first.period_start_date(i+1) >
9103                            			l_sysdate)
9104                           		   OR (l_atp_period_first.period_start_date(i) >
9105                               			l_sysdate))
9106 					THEN
9107                        				IF (MSC_ATP_PVT.G_ITEM_INFO_REC.rounding_control_type = 1) THEN
9108                           				l_atp_date_quantity_this_level :=
9109                           				FLOOR(l_atp_period_first.cumulative_quantity(i));
9110                        				ELSE
9111                            				l_atp_date_quantity_this_level :=
9112                            				l_atp_period_first.cumulative_quantity(i);
9113                        				END IF;
9114                       				l_atp_date_this_level :=
9115                       				GREATEST(l_atp_period_first.period_start_date(i),l_sysdate);
9116                       				EXIT;
9117                     			END IF;
9118                    		END IF;
9119                   	END LOOP;
9120 			IF PG_DEBUG in ('Y', 'C') THEN
9121 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Avail Date II pass is ' || l_atp_date_this_level);
9122 			   msc_sch_wb.atp_debug('ATP_Check: ' || 'Avail Qty II Pass is ' || l_atp_date_quantity_this_level);
9123 			END IF;
9124 			--End Changes bug2471377 pumehta
9125                    END IF;
9126 
9127                    l_used_available_quantity := l_atp_date_quantity_this_level; --bug3409973
9128 
9129 		END IF;
9130 
9131                 -- cchen: 1238941
9132                 p_atp_record.combined_requested_date_qty :=
9133                      p_atp_record.requested_date_quantity +
9134                      l_sub_requested_date_quantity+l_stealing_requested_date_qty;
9135 
9136                 p_atp_record.available_quantity:=LEAST(INFINITE_NUMBER,
9137                                                (l_atp_date_quantity_this_level+
9138                                                 l_stealing_qty));
9139 
9140                 p_atp_record.used_available_quantity:=LEAST(INFINITE_NUMBER,
9141                                                (l_used_available_quantity+
9142                                                 l_stealing_qty)); --bug3409973
9143                 -- 2859130 move to next work day for forward pass
9144                 /* Do not honor atp lead time
9145                 IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
9146                    p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
9147                                     (p_atp_record.organization_id,
9148                                      p_atp_record.instance_id,
9149                                      1,
9150                                      l_atp_date_this_level,
9151                                      NVL(p_atp_record.atp_lead_time, 0));
9152                 ELSE
9153                    p_atp_record.ship_date := MSC_CALENDAR.NEXT_WORK_DAY
9154                                     (p_atp_record.organization_id,
9155                                      p_atp_record.instance_id,
9156                                      1,
9157                                      l_atp_date_this_level);
9158                 END IF;
9159                 */
9160                 --Bug 3855866: If l_atp_date_this_level is NULL, set p_atp_record.ship_date
9161                 -- to NULL, thus error_code is set to ATP_REQ_QTY_FAIL(52)
9162 
9163                 IF (l_atp_date_this_level is NULL ) THEN
9164                    p_atp_record.ship_date := NULL;
9165                 ELSE
9166                    p_atp_record.ship_date := MSC_CALENDAR.next_work_day(
9167                                         p_atp_record.shipping_cal_code,
9168                                         p_atp_record.instance_id,
9169                                         l_atp_date_this_level);
9170                 END IF;
9171 
9172                 IF PG_DEBUG in ('Y', 'C') THEN
9173                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date' || p_atp_record.ship_date);
9174                    msc_sch_wb.atp_debug('ATP_Check: ' || 'avail qty: ' || p_atp_record.available_quantity);
9175                    msc_sch_wb.atp_debug('ATP_Check: ' || 'used avail qty: ' || p_atp_record.used_available_quantity); --bug3409973
9176                    msc_sch_wb.atp_debug('ATP_Check: ' || 'qty level: ' || l_atp_date_quantity_this_level);
9177                    msc_sch_wb.atp_debug('ATP_Check: ' || 'stealing qty level: ' || l_stealing_qty);
9178                    msc_sch_wb.atp_debug('ATP_Check: ' || 'atf date qty: ' || p_atp_record.atf_date_quantity);
9179                 END IF;
9180                 -- p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
9181                 IF p_atp_record.ship_date IS NOT NULL THEN
9182                     p_atp_record.error_code := ATP_REQ_DATE_FAIL;
9183 
9184 /* 1421413: move into the if below
9185                     -- bug 1302394: we should adjust the date
9186                     -- of the demand which causes this planned order
9187                     -- as well so that they can cancel each other.
9188 
9189                     UPDATE msc_demands
9190                     SET  USING_ASSEMBLY_DEMAND_DATE = l_atp_date_this_level
9191 --ngoel 8/30/2000 Check if this needs to be changed to l_atp_date_this_level.
9192                     WHERE  demand_id = l_demand_id
9193                     AND    plan_id = p_plan_id;
9194 
9195                     -- 1262907: no matter this is the components or not,
9196                     -- we need to populate the pegging tree
9197                     -- IF p_parent_pegging_id is null THEN
9198 */
9199                     -- 1421413: only populate pegging if
9200                     -- this is for top assembly or
9201                     -- this is for components and it is the future case
9202 
9203                     -- IF (p_parent_pegging_id is null) OR   commented for bug 2738280.
9204 		    -- This piece of code moves the demand date which is also done in procedure update_sd_date
9205 		    -- Since update_sd_date was not called for last line of SLSO ATO model, therefore, the summary
9206 		    -- picture was not matching with details picture for last line.
9207 		    -- On commenting this line, this piece of code is never executed for SLSO ATO model lines
9208 		    -- making the summary picture consistent with details picture for all the lines.
9209                     ---bug 3098127: the following condition was removed by the bug 2738280.
9210                     -- this shouldn't have been removed.
9211 
9212                     IF   (p_parent_pegging_id is null) OR
9213                         ((p_parent_pegging_id is not null AND
9214                         p_search = FORWARD_SCHEDULING)) THEN
9215 
9216 			-- dsting ATO 2465370
9217 			-- in case of override for CTO, do not adjust the demand date
9218                         --bug 3564511: This condition is not necessary as we always want to
9219                         -- adjust dates
9220 		        --IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
9221 			   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
9222 			   --AND p_parent_pegging_id is not null)
9223 			--THEN
9224                                 --bug 3328421: required date contains the original request date
9225                                 -- actual_supply_demand_date contain the actual sd date
9226 				--update mrp_atp_details_temp set required_date = TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY
9227 				update mrp_atp_details_temp set actual_supply_demand_date =
9228                                                                  TRUNC(l_atp_date_this_level)--4460369 + MSC_ATP_PVT.G_END_OF_DAY
9229 				where session_id = MSC_ATP_PVT.G_SESSION_ID
9230 				and pegging_id = l_pegging_id
9231 				and record_type = 3;
9232 
9233 				IF PG_DEBUG in ('Y', 'C') THEN
9234 					msc_sch_wb.atp_debug('ATO update details_temp 1: ' || l_pegging_id || ' date: ' || l_atp_date_this_level);
9235                                         msc_sch_wb.atp_debug('Update demand with demand id := ' || l_demand_id);
9236                                         msc_sch_wb.atp_debug('Update demand on date : = ' || l_atp_date_this_level);
9237 				END IF;
9238 
9239                                 -- bug 1302394: we should adjust the date
9240                                 -- of the demand which causes this planned order
9241                                 -- as well so that they can cancel each other.
9242 
9243                 -- For bug 2259824, move the demand to the end of day
9244                                 --s_cto_rearch: For ODS based ATP, we should be updating msc_sales_orders
9245                                 IF p_plan_id = -1 THEN
9246                                    UPDATE msc_sales_orders
9247                                    SET REQUIREMENT_DATE = TRUNC(l_atp_date_this_level) --4460369+ MSC_ATP_PVT.G_END_OF_DAY
9248                                    WHERE sr_instance_id  = p_atp_record.instance_id
9249                                    AND   DEMAND_ID = l_demand_id
9250                                    returning inventory_item_id into l_inventory_item_id;
9251                                 ELSE
9252                                    UPDATE msc_demands
9253                                    SET
9254                                    --start changes for plan by request date
9255                                    USING_ASSEMBLY_DEMAND_DATE =
9256                                         DECODE(ORIGINATION_TYPE,
9257                                                6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
9258                                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
9259                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9260                                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
9261                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9262                                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
9263                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9264                                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
9265                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9266                                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
9267                                                               l_original_req_ship_date,
9268                                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
9269                                                               l_original_req_ship_date,
9270                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9271                                                30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
9272                                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
9273                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9274                                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
9275                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9276                                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
9277                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9278                                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
9279                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9280                                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
9281                                                               l_original_req_ship_date,
9282                                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
9283                                                               l_original_req_ship_date,
9284                                                               TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9285                                                TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY),
9286                                    schedule_ship_date = DECODE(ORIGINATION_TYPE,
9287 	                                                       6,  TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9288 	                                                       30, TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9289 	                                                       NULL),
9290                                    promise_ship_date  = DECODE(ORIGINATION_TYPE,
9291                                                                6,  TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9292                                                                30, TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY,
9293                                                                NULL)
9294                                    --end of plan by request date enhanc
9295                                    WHERE  demand_id = l_demand_id
9296                                    AND    plan_id = p_plan_id
9297                                    returning inventory_item_id into l_inventory_item_id;
9298                                 END IF;
9299 
9300 
9301                                 -- time_phased_atp changes begin
9302                                 IF l_time_phased_atp = 'Y' THEN
9303                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'I come here 123');
9304                                          --bug3397904 start
9305                                          IF p_atp_record.requested_ship_date <= p_atp_record.atf_date THEN
9306                                             l_bkwd_pass_atf_date_qty := GREATEST(NVL(p_atp_record.combined_requested_date_qty,
9307                                             p_atp_record.requested_date_quantity), 0);
9308                                          ELSE
9309                                             l_bkwd_pass_atf_date_qty := p_atp_record.atf_date_quantity;
9310                                          END IF;
9311                                         --bug3397904 end
9312                                         MSC_ATP_PF.Move_PF_Bucketed_Demands(
9313                                                 p_plan_id,
9314                                                 l_demand_id,
9315                                                 p_atp_record.requested_ship_date,
9316                                                 l_atp_date_this_level,
9317                                                 p_atp_record.quantity_ordered,--bug3397904 changed from NULL
9318                                                 p_atp_record.used_available_quantity,--bug3397904 changed from NULL
9319                                                 p_atp_record.atf_date,
9320                                                 p_atp_record.atf_date_quantity, -- --bug3397904 changed from NULL
9321                                                 l_return_status,
9322                                                 l_bkwd_pass_atf_date_qty --bug3397904
9323                                         );
9324                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9325                                                 IF PG_DEBUG in ('Y', 'C') THEN
9326                                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Move_PF_Bucketed_Demands');
9327                                                 END IF;
9328                                                 RAISE FND_API.G_EXC_ERROR;
9329                                         END IF;
9330                                 ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
9331                                     AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
9332                                     update msc_alloc_demands
9333                                     set DEMAND_DATE = l_atp_date_this_level
9334                                     where parent_demand_id = l_demand_id
9335                                     and   plan_id = p_plan_id;
9336                                 END IF;
9337                                 -- time_phased_atp changes end
9338 
9339                                 -- update summary records removed in ODS cases - for summary enhancement
9340                                 IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y'  and (p_parent_pegging_id is not null) AND p_plan_id = -1 THEN
9341                                     IF PG_DEBUG in ('Y', 'C') THEN
9342                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update old date demand');
9343                                     END IF;
9344                                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
9345                                                         p_plan_id,
9346                                                         p_atp_record.organization_id,
9347                                                         l_inventory_item_id,
9348                                                         l_requested_ship_date,
9349                                                         null,
9350                                                         null,
9351                                                         null,
9352                                                         null,
9353                                                         null,
9354                                                         null,
9355                                                         2);
9356 
9357                                     update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
9358                                     set sd_qty = sd_qty + p_atp_record.quantity_ordered
9359                                     where plan_id = p_plan_id
9360                                     and   sr_instance_id = p_atp_record.instance_id
9361                                     and   organization_id = p_atp_record.organization_id
9362                                     and   inventory_item_id = l_inventory_item_id
9363                                     and   sd_date = trunc(l_requested_ship_date);
9364                                     commit;
9365                                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
9366                                                         p_plan_id,
9367                                                         p_atp_record.organization_id,
9368                                                         l_inventory_item_id,
9369                                                         l_atp_date_this_level,
9370                                                         null,
9371                                                         null,
9372                                                         null,
9373                                                         null,
9374                                                         null,
9375                                                         null,
9376                                                         2);
9377 
9378 
9379                                     IF PG_DEBUG in ('Y', 'C') THEN
9380                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update demand on new date');
9381                                     END IF;
9382                                     update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
9383                                     set sd_qty = sd_qty - p_atp_record.quantity_ordered
9384                                     where plan_id = p_plan_id
9385                                     and   sr_instance_id = p_atp_record.instance_id
9386                                     and   organization_id = p_atp_record.organization_id
9387                                     and   inventory_item_id = l_inventory_item_id
9388                                     and   sd_date = trunc(l_atp_date_this_level);
9389                                     commit;
9390                                     IF SQL%NOTFOUND THEN
9391                                         IF PG_DEBUG in ('Y', 'C') THEN
9392                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'insert demand on new date');
9393                                         END IF;
9394                                         MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
9395                                                                 p_atp_record.instance_id,
9396                                                                 p_atp_record.organization_id,
9397                                                                 l_inventory_item_id,
9398                                                                 l_atp_date_this_level,
9399                                                                 -1 * p_atp_record.quantity_ordered,
9400                                                                 '@@@');
9401                                     END IF;
9402                                     commit;
9403                                 END IF;
9404 			--END IF; -- end changes cto override
9405 
9406                         --- Enhance CTO Phase 1 Req #17
9407                         IF NOT (MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
9408                            (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
9409                            (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1)) THEN
9410 
9411               		   l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
9412               		   l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
9413               		   l_pegging_rec.parent_pegging_id:= l_pegging_id;
9414               		   l_pegging_rec.atp_level:= p_level+ 1;
9415               		   l_pegging_rec.organization_id:=
9416                                p_atp_record.organization_id;
9417                            l_pegging_rec.organization_code := l_org_code;
9418               		   l_pegging_rec.identifier1:= p_atp_record.instance_id;
9419                            l_pegging_rec.identifier2 := p_plan_id;
9420               		   l_pegging_rec.identifier3 := NULL;
9421 
9422                            -- time_phased_atp changes begin
9423                            IF l_time_phased_atp = 'Y' and l_atp_date_this_level <= p_atp_record.atf_date THEN
9424                                    l_pegging_rec.inventory_item_id:= l_request_item_id;
9425                                    l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
9426                            ELSE
9427                                    l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
9428                                    l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
9429                            END IF;
9430                            l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
9431                            -- time_phased_atp changes end
9432 
9433               		   l_pegging_rec.resource_id := NULL;
9434                            l_pegging_rec.resource_code := NULL;
9435               		   l_pegging_rec.department_id := NULL;
9436                            l_pegging_rec.department_code := NULL;
9437               		   l_pegging_rec.supplier_id := NULL;
9438                            l_pegging_rec.supplier_name := NULL;
9439               		   l_pegging_rec.supplier_site_id := NULL;
9440                            l_pegging_rec.supplier_site_name := NULL;
9441               		   l_pegging_rec.scenario_id:= p_scenario_id;
9442               		   l_pegging_rec.supply_demand_source_type:= ATP;
9443                            -- bug 1304991: make sure  we following the same
9444                            -- way to populate pegging tree. that is,
9445                            -- future case's net supply should not consider the
9446                            -- request date quantity.
9447                            IF p_atp_record.available_quantity <> INFINITE_NUMBER THEN
9448                               --- IF req_date < PTF date and if enough quanity is available on req date then
9449                               --- we show peggin for two days. On req date we show wahtever quantity is
9450                               --- available on that day. On PTF date , we show req-date qty + whatever qty is available
9451                               --- between req date and ptf date. If req_date > ptf then we show atp date qty - req date qty
9452                               --- if req date < PTF date then we set l_req_date_qty = 0 else it is null
9453 
9454                               IF nvl(l_req_date_qty, -1) = 0 THEN
9455                                  l_pegging_rec.supply_demand_quantity:= p_atp_record.available_quantity;
9456                               ELSE
9457                                  l_pegging_rec.supply_demand_quantity:=
9458                                     p_atp_record.available_quantity - greatest(0,
9459                                     p_atp_record.requested_date_quantity +
9460 			 	   NVL(l_stealing_requested_date_qty, 0));
9461                               END IF;
9462                            ELSE
9463                               l_pegging_rec.supply_demand_quantity:=INFINITE_NUMBER;
9464                            END IF;
9465                            ---bug 2798667: ship_date = l_atp_date_this_level + atp_lead_time
9466                            ---The option item should be requested on l_atp_date_this_level instead of ship date
9467                            --l_pegging_rec.supply_demand_date:=p_atp_record.ship_date;
9468                            l_pegging_rec.supply_demand_date:= l_atp_date_this_level;
9469                            l_pegging_rec.actual_supply_demand_date:= l_atp_date_this_level;
9470               		   l_pegging_rec.supply_demand_type:= 2;
9471                            IF PG_DEBUG in ('Y', 'C') THEN
9472                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Qty := ' || l_pegging_rec.supply_demand_quantity);
9473                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date := ' || l_pegging_rec.supply_demand_date);
9474                            END IF;
9475               		   l_pegging_rec.source_type := 0;
9476 
9477                            -- for demo:1153192
9478                            l_pegging_rec.constraint_flag := 'N';
9479 			   l_pegging_rec.component_identifier :=
9480 	                            NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
9481 
9482                            --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
9483                            --- cto looks at pegging tree to place their demands. Since CTO expects to find
9484                            --  id for the requested item, we add the following column. CTO will now read from this column
9485                            l_pegging_rec.request_item_id := p_atp_record.request_item_id;
9486 
9487                            --diag_atp
9488                            l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
9489 
9490                            --s_cto_rearch
9491                            l_pegging_rec.dest_inv_item_id := l_dest_inv_item_id;
9492                            l_pegging_rec.model_sd_flag := 2;
9493                            --s_cto_rearch
9494                            IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 2
9495                                AND l_requested_date_quantity < p_atp_record.quantity_ordered THEN
9496                                -- Bug 3828469 - Add constraint only if it exists
9497                                IF PG_DEBUG in ('Y', 'C') THEN
9498                                   msc_sch_wb.atp_debug('Forward ATP, add pegging constriant');
9499                                END IF;
9500                                l_pegging_rec.constraint_type := 1;
9501                            END IF;
9502                            --e_cto_rearch
9503 
9504 			   -- dsting: get_forward_material_info has already added the pegging
9505 			   -- in this case
9506 			   IF NOT (MSC_ATP_PVT.G_INV_CTP = 4 and
9507 			      MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
9508 			      MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 AND
9509 			      MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND
9510 			      l_atp_comp_flag = 'N' AND
9511 			      --p_parent_pegging_id is null AND
9512 			      MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1)
9513 			   THEN
9514 	              	      l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;     -- for summary enhancement
9515                               l_pegging_rec.demand_class :=  p_atp_record.demand_class;
9516                               --4570421
9517                               l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
9518                               l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
9519                               l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
9520                               l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
9521                               l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
9522                               l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
9523                               l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
9524 
9525 	              	      MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_atp_pegging_id);
9526 			   END IF;
9527 
9528                            -- 1250141: we need to populate the details
9529                            -- 1300692: we only populate the details for
9530                            -- PDS case here.  Because the form use end_pegging_id
9531                            -- to retrive info and we will end up display
9532                            -- same info twice.
9533 
9534                            --Bug2471377 Begin changes
9535                            IF NVL(p_atp_record.insert_flag,0) <> 0 THEN
9536                               IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
9537                                  IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
9538                                    (MSC_ATP_PVT.G_ALLOCATION_METHOD = 2) THEN
9539                                    -- Link the Supply Demand Details for
9540                                    --first Demand class to the pegging details
9541 
9542 				   IF l_atp_period_first.pegging_id.COUNT > 0 THEN
9543 				 	MSC_ATP_UTILS.copy_mrp_sd_recs(
9544 				 		l_atp_period_first.pegging_id(1),
9545 				 		l_atp_pegging_id);
9546 				   END IF;
9547 
9548                                    FOR i in 1..l_atp_period_first.Level.COUNT LOOP
9549                                       l_atp_period_first.Pegging_Id(i) :=
9550                                              l_atp_pegging_id;
9551                                       l_atp_period_first.End_Pegging_Id(i) :=
9552                                              MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9553                                    END LOOP;
9554 
9555                                    MSC_ATP_PROC.Details_Output(l_atp_period_first,
9556                                                l_atp_supply_demand,
9557                                                x_atp_period,
9558                                                x_atp_supply_demand,
9559                                                l_return_status);
9560                                  ELSIF MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
9561 				       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) AND
9562 				       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
9563 				       p_parent_pegging_id IS NULL THEN
9564 				     null;
9565                                  ELSE
9566 
9567                                  FOR i in 1..l_atp_period.Level.COUNT LOOP
9568                                    l_atp_period.Pegging_Id(i) := l_atp_pegging_id;
9569                                    l_atp_period.End_Pegging_Id(i) :=
9570                                      MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9571                                  END LOOP;
9572 
9573 			         IF p_atp_record.insert_flag <> 0 THEN
9574    		                         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_atp_pegging_id,
9575 				    		   MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
9576 			         END IF;
9577 
9578                                  MSC_ATP_PROC.Details_Output(l_atp_period,
9579                                             l_atp_supply_demand,
9580                                             x_atp_period,
9581                                             x_atp_supply_demand,
9582                                             l_return_status);
9583                                  END IF; -- MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y
9584                               END IF; --MSC_ATP_PVT.G_INV_CTP = 4
9585                            END IF;
9586                          --- End Enhance CTO Phase 1 Req #17
9587                         END IF;  --Bug2471377 End Changes
9588 
9589                     END IF; -- 1421413 IF (p_parent_pegging_id is null) OR
9590                             -- ((p_parent_pegging_id is not null and
9591                             -- p_search = FORWARD_SCHEDULING))
9592 
9593                 ELSE
9594                     p_atp_record.error_code := ATP_REQ_QTY_FAIL;
9595                 END IF;
9596             --diag_atp
9597             ELSIF NOT(l_atp_comp_flag = 'N') THEN -- if net_demand > 0 and atp_comp_flag = 'Y'
9598 
9599               --- bug 2178544: Implement PTF
9600               -- PTF changes
9601               -- If the requested ship date is before planning time fence (PTF) date then
9602               -- we fail the request. The logic is that production schedule is fixed before
9603               -- PTF. Hence no planned orders should be placed before PTF
9604 
9605 	      -- 3/4/2002, modified to honor planning_time_fence_flag from msc_plans to adjust for
9606 	      -- ptf only if plan option is checked.
9607 
9608               -- Bug 3535999 - l_ptf_date should be validated on mgf cal
9609               -- l_ptf_date := l_sysdate;
9610               l_ptf_date := NVL(MSC_CALENDAR.next_work_day(
9611                                         p_atp_record.manufacturing_cal_code,
9612                                         p_atp_record.instance_id,
9613                                         l_trunc_sysdate), l_trunc_sysdate);
9614 
9615               -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
9616               l_ptf_enabled := 2;
9617 
9618               -- when we are checking base model's capacity then we do not want to check PTF
9619               IF l_check_model_capacity = 2 THEN
9620                  BEGIN
9621                    Select DECODE(pl.planning_time_fence_flag,
9622                                         1, trunc(NVL(itm.planning_time_fence_date, l_ptf_date)), -- Bug 3535999 - l_sysdate)),
9623                                         l_ptf_date), -- Bug 3535999 - l_sysdate),
9624                           pl.planning_time_fence_flag   -- Bug 3525582
9625                    into   l_ptf_date, l_ptf_enabled     -- Bug 3525582
9626                    from   msc_system_items itm,
9627 		          msc_plans pl
9628                    where  itm.plan_id = p_plan_id
9629                    and    itm.sr_instance_id = p_atp_record.instance_id
9630                    and    itm.organization_id = p_atp_record.organization_id
9631                    --and    itm.sr_inventory_item_id = p_atp_record.inventory_item_id
9632                    and    itm.sr_inventory_item_id = nvl(p_atp_record.request_item_id,p_atp_record.inventory_item_id) --Bug-5160663
9633 		   and    pl.plan_id = itm.plan_id
9634 		   and    pl.sr_instance_id = itm.sr_instance_id;
9635                  EXCEPTION
9636                    WHEN OTHERS THEN
9637                        IF PG_DEBUG in ('Y', 'C') THEN
9638                           msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured while getting PTF : ' || sqlerrm);
9639                        END IF;
9640                        l_ptf_date := l_ptf_date; -- Bug 3535999 - l_sysdate;
9641                  END;
9642               END IF;
9643               IF PG_DEBUG in ('Y', 'C') THEN
9644                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' || l_ptf_date);
9645                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_enabled := ' || l_ptf_enabled);
9646               END IF;
9647               -- for bug 1124538: do not pass down the demand to the
9648               -- next level if it is backward scheduling and
9649               -- requested_ship_date is less than sysdate.
9650 
9651               IF ((p_search = BACKWARD_SCHEDULING) AND
9652                  --bug 2235012
9653                  --(l_requested_ship_date < GREATEST(l_sysdate, l_ptf_date))) THEN
9654                  --diag_atp: we dont want to stop in case of diag ATP. We still want to explode sources
9655                  (l_requested_ship_date < l_sysdate ) AND (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2)) THEN
9656                    IF PG_DEBUG in ('Y', 'C') THEN
9657                       msc_sch_wb.atp_debug('ATP_Check: ' || 'backward, request ship date<sysdate or ptf_date');
9658                    END IF;
9659                    p_atp_record.requested_date_quantity:=0.0;
9660                    -- cchen : 1238941
9661                    p_atp_record.combined_requested_date_qty := 0.0;
9662 
9663                    IF p_parent_pegging_id is null THEN
9664                        IF PG_DEBUG in ('Y', 'C') THEN
9665                           msc_sch_wb.atp_debug('ATP_Check: ' || 'top level, set future date Y');
9666                        END IF;
9667                        MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'Y';
9668                        MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := p_atp_record.Identifier;
9669                        MSC_ATP_PVT.G_COMP_LINE_ID := p_atp_record.Identifier;
9670                        -- 1275751
9671                        p_atp_record.error_code := ATP_REQ_DATE_FAIL;
9672                        ---- bug 1819638
9673                        ---remove the demand record as supply is 0 on the req_date
9674 
9675                        UPDATE msc_demands
9676                        SET  USING_REQUIREMENT_QUANTITY = 0
9677                        WHERE  demand_id = l_demand_id
9678                        AND    plan_id = p_plan_id
9679                        returning inventory_item_id into l_inventory_item_id;
9680 
9681                        -- time_phased_ATP
9682                        IF (l_time_phased_atp = 'Y')
9683                           OR
9684                           MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
9685                            AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
9686 
9687                            update msc_alloc_demands
9688                            set ALLOCATED_QUANTITY = 0
9689                            where parent_demand_id = l_demand_id
9690                            and   plan_id = p_plan_id;
9691                        END IF;
9692 
9693                         -- update summary records removed in ODS cases - for summary enhancement
9694                         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
9695                            IF PG_DEBUG in ('Y', 'C') THEN
9696                               msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
9697                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
9698                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
9699                            END IF;
9700 
9701                            MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
9702                                                                 p_plan_id,
9703                                                                 p_atp_record.organization_id,
9704                                                                 l_inventory_item_id,
9705                                                                 l_requested_ship_date,
9706                                                                 null,
9707                                                                 null,
9708                                                                 null,
9709                                                                 null,
9710                                                                 null,
9711                                                                 null,
9712                                                                 2);
9713 
9714 
9715                            update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
9716                            set sd_qty = sd_qty + p_atp_record.quantity_ordered
9717                            where plan_id = p_plan_id
9718                            and  inventory_item_id = l_inventory_item_id
9719                            and  sr_instance_id = p_atp_record.instance_id
9720                            and  organization_id = p_atp_record.organization_id
9721                            and  sd_date = trunc(l_requested_ship_date);
9722                            commit;
9723                        END IF;
9724 
9725                        IF PG_DEBUG in ('Y', 'C') THEN
9726                           msc_sch_wb.atp_debug('ATP_Check: ' || 'demand updated for demand id = '|| l_demand_id);
9727                        END IF;
9728 
9729                    END IF;
9730               ELSE
9731 
9732                 -- pass the net demand down for next level
9733                 -- we try to meet the requirement by going down to the
9734                 -- components.
9735                 -- first we check the supply chain bill
9736 
9737                 IF PG_DEBUG in ('Y', 'C') THEN
9738                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside net_demand > 0');
9739                 END IF;
9740                 -- for bug 1094773
9741                 IF (p_search = FORWARD_SCHEDULING) AND
9742                    --subst : for top org in case of subst we do not add pegg
9743                    NOT (MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' and
9744                               p_atp_record.top_tier_org_flag = 1) THEN
9745 
9746                    -- assume the atp date and quantity to be single level.
9747                    -- this is the worst that we can get.
9748 
9749                    IF PG_DEBUG in ('Y', 'C') THEN
9750                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Forward ATP Qty := ' || l_atp_date_quantity_this_level);
9751                       msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Date : FUTURE : ' || l_atp_date_this_level);
9752                    END IF;
9753 
9754                    p_atp_record.available_quantity:= l_atp_date_quantity_this_level;
9755                    p_atp_record.used_available_quantity:= l_used_available_quantity;--bug3409973
9756 
9757                    -- 2859130 move to next work day for forward pass
9758                    /* do not honor atp lead time
9759                    IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
9760                       p_atp_record.ship_date := MSC_CALENDAR.DATE_OFFSET
9761                                         (p_atp_record.organization_id,
9762                                          p_atp_record.instance_id,
9763                                          1,
9764                                          l_atp_date_this_level,
9765                                          NVL(p_atp_record.atp_lead_time, 0));
9766                    ELSE
9767                       p_atp_record.ship_date := MSC_CALENDAR.NEXT_WORK_DAY
9768                                         (p_atp_record.organization_id,
9769                                          p_atp_record.instance_id,
9770                                          1,
9771                                          l_atp_date_this_level);
9772                    END IF;
9773                    */
9774                    p_atp_record.ship_date := MSC_CALENDAR.next_work_day(
9775                                                 p_atp_record.shipping_cal_code,
9776                                                 p_atp_record.instance_id,
9777                                                 l_atp_date_this_level);
9778 
9779                    IF PG_DEBUG in ('Y', 'C') THEN
9780                       msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date : ' || p_atp_record.ship_date);
9781                    END IF;
9782 
9783 		   -- Planned Pegging Based AATP Changes
9784                    --enable forward pegging for all cases.
9785                    /* IF (
9786                        --- Enhance CTO Phase 1 Req #17
9787                           -- Support Forward Stealing for components of ATO model in
9788                           -- Model's sourcing organization.
9789                        ( (p_parent_pegging_id IS NULL) OR
9790                            NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id ) AND
9791                        --- End Enhance CTO Phase 1 Req #17
9792                    */
9793                    IF (
9794                       (MSC_ATP_PVT.G_INV_CTP = 4) AND
9795                       (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
9796                       (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
9797                       (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) AND C_ITEM_INFO_REC.atp_flag = 'Y'
9798                                 --3423277
9799                                 AND NOT(C_ITEM_INFO_REC.bom_item_type = 1 and
9800                                     ( C_ITEM_INFO_REC.atp_comp_flag in ('Y', 'C') and
9801                                       p_atp_record.parent_item_id is null)) THEN
9802 
9803 		      -- Already Called MSC_AATP_REQ.Get_Forward_Material_Atp, thus don't add
9804 		      -- pegging for future case here as its already added.
9805 
9806 		      IF PG_DEBUG in ('Y', 'C') THEN
9807 		         msc_sch_wb.atp_debug('ATP_Check: ' || 'Already added future supply pegging : '|| l_atp_pegging_tab.COUNT);
9808 		      END IF;
9809 		   ELSE
9810                       l_future_atp_period := l_atp_period;
9811                       l_future_atp_supply_demand := l_atp_supply_demand;
9812 
9813                       l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
9814                       l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
9815                       l_pegging_rec.parent_pegging_id:= l_pegging_id;
9816                       l_pegging_rec.atp_level:= p_level+1;
9817                       l_pegging_rec.organization_id:= p_atp_record.organization_id;
9818                       l_pegging_rec.organization_code := l_org_code;
9819                       l_pegging_rec.identifier1:= p_atp_record.instance_id;
9820                       l_pegging_rec.identifier2 := p_plan_id;
9821                       l_pegging_rec.identifier3 := NULL;
9822 
9823                       -- time_phased_atp changes begin
9824                       IF l_time_phased_atp = 'Y' and l_atp_date_this_level <= p_atp_record.atf_date THEN
9825                           l_pegging_rec.inventory_item_id:= l_request_item_id;
9826                           l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
9827                       ELSE
9828                           l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
9829                           l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
9830                       END IF;
9831                       l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
9832                       -- time_phased_atp changes end
9833 
9834                       l_pegging_rec.resource_id := NULL;
9835                       l_pegging_rec.resource_code := NULL;
9836                       l_pegging_rec.department_id := NULL;
9837                       l_pegging_rec.department_code := NULL;
9838                       l_pegging_rec.supplier_id := NULL;
9839                       l_pegging_rec.supplier_name := NULL;
9840                       l_pegging_rec.supplier_site_id := NULL;
9841                       l_pegging_rec.supplier_site_name := NULL;
9842                       l_pegging_rec.scenario_id:= p_scenario_id;
9843                       l_pegging_rec.supply_demand_source_type:= ATP;
9844 
9845                       -- bug 1304991: make sure we follow the same way to populate pegging tree.
9846 		      -- that is, future case's net supply should not consider the request date quantity.
9847                       --- IF req_date < PTF date and if enough quanity is available on req date then
9848                       --- we show peggin for two days. On req date we show wahtever quantity is
9849                       --- available on that day. On PTF date , we show req-date qty + whatever qty is available
9850                       --- between req date and ptf date. If req_date > ptf then we show atp date qty - req date qty
9851                       --- if req date < PTF date then we set l_req_date_qty = 0 else it is null
9852                       --4410439 start
9853                       IF nvl(l_req_date_qty, -1) = 0 THEN
9854                          l_pegging_rec.supply_demand_quantity:= p_atp_record.available_quantity;
9855                       ELSE
9856                          IF PG_DEBUG in ('Y', 'C') THEN
9857 		            msc_sch_wb.atp_debug('ATP_Check: p_atp_record_available_qty : ' || p_atp_record.available_quantity);
9858                             msc_sch_wb.atp_debug('ATP_Check: requested_day_quantity : ' || p_atp_record.available_quantity);
9859 		         END IF;
9860                          IF(p_atp_record.available_quantity <>  INFINITE_NUMBER) THEN
9861                             IF PG_DEBUG in ('Y', 'C') THEN
9862 		               msc_sch_wb.atp_debug('ATP_Check: Available Quantity is not INFINITE, subtracting requested_day_qty');
9863                             END IF;
9864                          l_pegging_rec.supply_demand_quantity:=
9865                                 p_atp_record.available_quantity - greatest(0, l_requested_date_quantity);
9866                          ELSE
9867                             IF PG_DEBUG in ('Y', 'C') THEN
9868 		               msc_sch_wb.atp_debug('ATP_Check: Available Quantity INFINITE');
9869                             END IF;
9870                              l_pegging_rec.supply_demand_quantity:=  p_atp_record.available_quantity;
9871                          END IF;
9872                       END IF;
9873                       --4410439 end
9874                       ---bug 2798667: ship_date = l_atp_date_this_level + atp_lead_time
9875                       ---The option item should be requested on l_atp_date_this_level instead of ship date
9876                       --l_pegging_rec.supply_demand_date:=p_atp_record.ship_date;
9877                       l_pegging_rec.supply_demand_date:=l_atp_date_this_level;
9878                       --bug 3328421: store actual supply demand date
9879                       l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
9880 
9881                       l_pegging_rec.supply_demand_type:= 2;
9882                       l_pegging_rec.source_type := 0;
9883 
9884                       -- for demo:1153192
9885                       l_pegging_rec.constraint_flag := 'N';
9886 		      l_pegging_rec.component_identifier :=
9887                              NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
9888 
9889                       l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
9890 
9891                       --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
9892                       --- cto looks at pegging tree to place their demands. Since CTO expects to find
9893                       --  id for the requested item, we add the following column. CTO will now read from this column
9894                       --diag_atp
9895                       l_pegging_rec.plan_name := p_atp_record.plan_name;
9896                       l_pegging_rec.required_quantity:= p_atp_record.quantity_ordered;
9897                       l_pegging_rec.required_date := l_requested_ship_date;
9898                       l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
9899                       l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
9900                       l_pegging_rec.rounding_control := l_rounding_control_flag;
9901                       l_pegging_rec.atp_flag := l_atp_flag;
9902                       l_pegging_rec.atp_component_flag := l_atp_comp_flag;
9903                       l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLY; ---atp supply node
9904                       l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
9905                       l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
9906                       l_pegging_rec.fixed_lead_time := l_fixed_lt;
9907                       l_pegging_rec.variable_lead_time := l_variable_lt;
9908                       l_pegging_rec.weight_capacity := l_unit_weight;
9909                       l_pegging_rec.volume_capacity := l_unit_volume;
9910                       l_pegging_rec.weight_uom := l_weight_uom;
9911                       l_pegging_rec.volume_uom := l_volume_uom;
9912                       l_pegging_rec.allocation_rule := l_allocation_rule_name;
9913 
9914 
9915                       l_pegging_rec.request_item_id := p_atp_record.request_item_id;
9916 
9917                       --s_cto_rearch
9918                       IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 2 THEN
9919                         IF PG_DEBUG in ('Y', 'C') THEN
9920                            msc_sch_wb.atp_debug('Forward ATP, add pegging constriant');
9921                         END IF;
9922                         l_pegging_rec.constraint_type := 1;
9923                       END IF;
9924                       l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
9925                       l_pegging_rec.model_sd_flag := 2;
9926                       --e_cto_rearch
9927                       l_pegging_rec.demand_class :=  p_atp_record.demand_class;
9928 
9929                       --4570421
9930                       l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
9931                       l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
9932                       l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
9933                       l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
9934                       l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
9935                       l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
9936                       l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
9937 
9938                       MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_future_pegging_id);
9939 
9940                       -- bug 1225478
9941                       FOR i in 1..l_future_atp_period.Level.COUNT LOOP
9942                           l_future_atp_period.Pegging_Id(i) := l_future_pegging_id;
9943                           l_future_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9944                       END LOOP;
9945 
9946                  -- dsting: supply/demand details pl/sql table no longer used
9947 /*
9948                       FOR i in 1..l_future_atp_supply_demand.Level.COUNT LOOP
9949                           l_future_atp_supply_demand.Pegging_Id(i) := l_future_pegging_id;
9950                           l_future_atp_supply_demand.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
9951                       END LOOP;
9952 */
9953 		      IF p_atp_record.insert_flag <> 0 THEN
9954 		      	MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_future_pegging_id,
9955 					MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
9956 		      END IF;
9957 
9958                    END IF; 	--IF (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1) THEN
9959                 END IF;
9960                 -- store this pegging id to l_parent_pegging_id so
9961                 -- that we know how to peg to the demand for the planned
9962                 -- order that we are going to create for each child.
9963                 --4902658/5216528
9964                 IF PG_DEBUG in ('Y', 'C') THEN
9965                     msc_sch_wb.atp_debug('l_parent_pegging_id := ' || l_parent_pegging_id);
9966                 END IF;
9967 
9968                 l_parent_pegging_id := l_pegging_id;
9969                 -- Bug 2814872
9970                 IF PG_DEBUG in ('Y', 'C') THEN
9971                     msc_sch_wb.atp_debug('l_parent_pegging_id := ' || l_parent_pegging_id);
9972                     msc_sch_wb.atp_debug('l_pegging_id := ' || l_pegging_id);
9973                     msc_sch_wb.atp_debug('ATP_Check: l_atp_date_this_level ' || l_atp_date_this_level);
9974                     msc_sch_wb.atp_debug('ATP_Check: l_atp_date_quantity_this_level ' ||
9975                                                                 l_atp_date_quantity_this_level);
9976                     msc_sch_wb.atp_debug('ATP_Check: l_used_available_quantity ' ||
9977                                                                 l_used_available_quantity);
9978                     msc_sch_wb.atp_debug('ATP_Check: p_atp_record.Quantity_Ordered '||
9979                                                                 p_atp_record.Quantity_Ordered);
9980                     msc_sch_wb.atp_debug('ATP_Check: l_temp_net_demand '|| l_net_demand );
9981                     msc_sch_wb.atp_debug('ATP_Check: Count of Subs Items '|| l_substitutes_rec.inventory_item_id.count );
9982                END IF;
9983                l_tmp_parent_pegging_id := l_parent_pegging_id;
9984                --4902658/5216528
9985                for l_index in 1..l_substitutes_rec.inventory_item_id.count
9986                   LOOP
9987 
9988                   IF l_substitutes_rec.inventory_item_id.count > 1 THEN
9989                      IF PG_DEBUG in ('Y', 'C') THEN
9990                         msc_sch_wb.atp_debug('Substitute Loop Starts with l_index := ' || l_index);
9991                END IF;
9992                END IF;
9993 
9994                   IF (l_net_demand <= 0) then
9995                      EXIT;
9996                   END IF;
9997 
9998                   l_temp_net_demand := l_net_demand;
9999                   l_parent_pegging_id :=  l_substitutes_rec.pegging_id(l_index);
10000                   l_request_item_id :=  l_substitutes_rec.inventory_item_id(l_index);
10001                   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
10002                                             --3917625: read item attributes from planned data
10003                                             -- -1,
10004                                             p_plan_id,
10005                                             l_request_item_id,
10006                                             p_atp_record.organization_id,
10007                                             l_item_info_rec );
10008                   C_ITEM_INFO_REC := G_ITEM_INFO_REC;
10009                   --In regular cases PF rec should point to PF Item .
10010                   if l_substitutes_rec.inventory_item_id.count > 1 then
10011                      PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
10012                   end if;
10013                   p_atp_record.request_item_id :=     l_substitutes_rec.inventory_item_id(l_index);
10014 
10015                -- Do not do Any Forward processing of sources for past due orders .
10016                IF ((p_search = FORWARD_SCHEDULING) AND
10017                    (l_atp_date_quantity_this_level >= p_atp_record.Quantity_Ordered) AND
10018                    (l_atp_date_this_level = l_sysdate ) AND (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2)) THEN
10019                    --diag_atp: we dont want to stop in case of diag ATP. We still want to explode sources
10020                    IF PG_DEBUG in ('Y', 'C') THEN
10021                       msc_sch_wb.atp_debug('ATP_Check: ' ||
10022                           'Past Due Order Satisfied on Sysdate, Stop unless dia_atp ');
10023                    END IF;
10024                   p_atp_record.error_code := ATP_REQ_DATE_FAIL;
10025                 --END IF;
10026                 -- End Bug 2814872
10027 
10028                 ----bug 2235012
10029                 --diag_atp
10030                 ELSIF NOT (p_search = BACKWARD_SCHEDULING AND l_requested_ship_date < l_ptf_date
10031                                                        AND  MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2) THEN
10032 
10033                    IF PG_DEBUG in ('Y', 'C') THEN
10034                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Find Sources');
10035                        msc_sch_wb.atp_debug('bom_item_type := ' || C_ITEM_INFO_REC.bom_item_type);
10036                        msc_sch_wb.atp_debug('wip_supply_type := ' || p_atp_record.wip_supply_type);
10037                        msc_sch_wb.atp_debug('replenish_to_ord_flag := ' || C_ITEM_INFO_REC.replenish_to_ord_flag);
10038                    END IF;
10039                    --s_cto_rearch
10040                    l_item_sourcing_info_rec := l_null_item_sourcing_info_rec;
10041                    l_item_sourcing_info_rec.sr_inventory_item_id.extend;
10042                    l_item_sourcing_info_rec.line_id.extend;
10043                    l_item_sourcing_info_rec.ato_line_id.extend;
10044                    l_item_sourcing_info_rec.match_item_id.extend;
10045                    --l_item_sourcing_info_rec.sr_inventory_item_id(1) := p_atp_record.inventory_item_id;
10046                    l_item_sourcing_info_rec.sr_inventory_item_id(1) := l_request_item_id; -- for time_phased_atp
10047                    l_item_sourcing_info_rec.line_id(1) := p_atp_record.demand_source_line;
10048                    l_item_sourcing_info_rec.match_item_id(1) := null;
10049                    IF C_ITEM_INFO_REC.bom_item_type = 1 and
10050                           C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
10051                       l_item_sourcing_info_rec.ato_line_id(1) := p_atp_record.ATO_Model_Line_Id;
10052                    ELSE
10053                       l_item_sourcing_info_rec.ato_line_id(1) := null;
10054                    END IF;
10055 
10056                    --if ods ATP and ATO model/item or if checking base model's capacity  then we only make
10057                    IF MSC_ATP_PVT.G_INV_CTP = 5 or l_check_model_capacity = 1 THEN
10058                       IF PG_DEBUG in ('Y', 'C') THEN
10059                           msc_sch_wb.atp_debug('ATP_Check: ' || 'ods atp, we always make');
10060                       END IF;
10061 
10062                       --we should come here only for ATO models or ATO items
10063                       l_sources.Source_Type := MRP_ATP_PUB.number_arr(2); -- make
10064                       l_sources.Organization_Id:=
10065                            MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10066                       l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10067                       l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(NULL);
10068                       l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(NULL);
10069                       l_sources.instance_id :=
10070                             MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10071                       l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10072                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10073                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10074                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10075                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10076 
10077                    --do not get sources for phantom model
10078                    ELSIF NOT (C_ITEM_INFO_REC.bom_item_type = 1 and NVL(p_atp_record.wip_supply_type,2) = 6 and
10079                               C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
10080 
10081 
10082                       MSC_ATP_PROC.Atp_Sources(p_atp_record.instance_id,
10083 			         p_plan_id,
10084                                  l_request_item_id, -- for time_phased_atp
10085                                  p_atp_record.organization_id,
10086                                  NULL,
10087                                  NULL,
10088                                  p_assign_set_id,
10089                                  l_item_sourcing_info_rec,
10090                                  --MRP_ATP_PUB.number_arr(NULL),
10091                                  MSC_ATP_PVT.G_SESSION_ID,
10092                                  l_sources,
10093 			         l_return_status);
10094 
10095                    END IF;
10096                    --e_cto_rearch
10097                    IF PG_DEBUG in ('Y', 'C') THEN
10098                       msc_sch_wb.atp_debug('ATP_Check: ' || 'after atp_sources');
10099                    END IF;
10100 
10101                    IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR Then
10102                        --- Model has option specific sources which do not match with sourcing of model
10103                        --update the demand pegging
10104                        update mrp_atp_details_temp
10105                        set error_code = MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL
10106                        where session_id = MSC_ATP_PVT.G_SESSION_ID
10107                        and   pegging_id = l_pegging_id
10108                        and   record_type = 3;
10109 
10110                        RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
10111 
10112                    ELSIF l_return_status <> FND_API.G_RET_STS_SUCCESS  OR
10113                      l_sources.source_type.count = 0  THEN
10114                        -- no sources in supply chain bill, then we assume
10115                        -- it has "make at" type
10116                        IF PG_DEBUG in ('Y', 'C') THEN
10117                           msc_sch_wb.atp_debug('ATP_Check: ' || 'no sources');
10118                        END IF;
10119                        --- select make buy code  to decide if we want to make it or buy it
10120                        --EFTC Changes
10121                        BEGIN
10122                		   SELECT planning_make_buy_code INTO  l_make_buy_cd
10123                		   FROM MSC_SYSTEM_ITEMS I
10124                		   --bug3842035 Using member item id instead of family
10125                		   --WHERE  I.sr_inventory_item_id = p_atp_record.inventory_item_id and
10126                		   WHERE  I.sr_inventory_item_id = p_atp_record.request_item_id and
10127                       	   I.sr_instance_id = p_atp_record.instance_id and
10128                        	   I.organization_id = p_atp_record.organization_id and
10129                        	   I.plan_id = p_plan_id ;
10130                            IF PG_DEBUG in ('Y', 'C') THEN
10131                               msc_sch_wb.atp_debug('ATP_Check: ' || 'make-buy Code = '|| l_make_buy_cd);
10132                            END IF;
10133                        EXCEPTION
10134                		   WHEN OTHERS THEN
10135 			   l_make_buy_cd := 1; --- make 'make' default
10136                        END;
10137                        IF (l_make_buy_cd = 2) THEN ---  buy
10138                            l_sources.Source_Type := MRP_ATP_PUB.number_arr(3); --- BUY
10139                            l_sources.Organization_Id:=
10140                                    MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10141                            l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10142                            ----- Since supplier id  and supplier site id is no known
10143                            ----  we set it to -99. We cant set it to null because
10144                            ----  when atp_check procedure is called from buy logic below
10145                            ----  then it will go into inventory org to do atp check rather
10146                            ----- then calling get_supplier_atp_info
10147                            l_sources.Supplier_Id :=MRP_ATP_PUB.number_arr(-99);
10148                            l_sources.Supplier_Site_Id :=MRP_ATP_PUB.number_arr(-99);
10149                            l_sources.instance_id :=
10150                              MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10151    			   l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10152                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10153                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10154                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10155                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10156 
10157 			  -- dsting
10158 			  IF PG_DEBUG in ('Y', 'C') THEN
10159 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have buy source');
10160 			  END IF;
10161 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
10162 		       ELSE
10163                            l_sources.Source_Type := MRP_ATP_PUB.number_arr(2); -- make
10164                            l_sources.Organization_Id:=
10165                               MRP_ATP_PUB.number_arr(p_atp_record.organization_id);
10166                            l_sources.Rank := MRP_ATP_PUB.number_arr(1);
10167                            l_sources.Supplier_Id := MRP_ATP_PUB.number_arr(NULL);
10168                            l_sources.Supplier_Site_Id := MRP_ATP_PUB.number_arr(NULL);
10169                            l_sources.instance_id :=
10170                               MRP_ATP_PUB.number_arr(p_atp_record.instance_id);
10171                            l_sources.Lead_Time := MRP_ATP_PUB.number_arr(0); -- ship_rec_cal
10172                            l_sources.Ship_Method := MRP_ATP_PUB.char30_arr('@@@'); -- ship_rec_cal
10173                            l_sources.Preferred := MRP_ATP_PUB.number_arr(NULL); -- ship_rec_cal
10174                            l_sources.make_flag := MRP_ATP_PUB.char1_arr(NULL); -- ship_rec_cal
10175                            l_sources.Sup_Cap_Type := MRP_ATP_PUB.number_arr(2); -- ship_rec_cal
10176 
10177 			  -- dsting
10178 			  IF PG_DEBUG in ('Y', 'C') THEN
10179 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have make source');
10180 			  END IF;
10181 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
10182 		       END IF;
10183 
10184                    END IF;
10185                 END IF;
10186                 -- go over each sources
10187                 j := l_sources.source_type.FIRST;
10188 
10189 
10190                 --- we store parent_ptf in a local varibale as it might get changed
10191                 l_parent_ptf_date := NVL(MSC_ATP_PVT.G_PTF_DATE, l_ptf_date);
10192                 IF PG_DEBUG in ('Y', 'C') THEN
10193                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_parent_ptf_date := ' || l_parent_ptf_date);
10194                 END IF;
10195                 WHILE (j IS NOT NULL)   LOOP
10196 
10197                     -- Bug 3667729 - Reset G_ITEM_INFO_REC as it may have changed in last source.
10198                     G_ITEM_INFO_REC := C_ITEM_INFO_REC;
10199 
10200                     --Bug 3821358
10201                     l_store_g_ptf_date := NULL;
10202 
10203                     --bug 2178544: Since the ptf flag may be chnaged by transfer org, we reassign it for the curent plan
10204                     MSC_ATP_PVT.G_PTF_DATE := l_parent_ptf_date;
10205 
10206                     IF PG_DEBUG in ('Y', 'C') THEN
10207                        msc_sch_wb.atp_debug('ATP_Check: ' || 'inside while loop for the children');
10208                     END IF;
10209 
10210                     /* ship_rec_cal changes begin */
10211                     -- populate calendar codes in l_atp_rec
10212                     IF PG_DEBUG in ('Y', 'C') THEN
10213                        msc_sch_wb.atp_debug('**************** Calendar Codes ******************************');
10214                        msc_sch_wb.atp_debug('*  ___________________Input_____________________');
10215                        msc_sch_wb.atp_debug('*  ');
10216                        msc_sch_wb.atp_debug('*  Source Type             : '|| l_sources.source_type(j) );
10217                        msc_sch_wb.atp_debug('*  Sup Cap Type            : '|| l_sources.sup_cap_type(j) );
10218                        msc_sch_wb.atp_debug('*  Instance ID             : '|| p_atp_record.instance_id );
10219                        msc_sch_wb.atp_debug('*  Source Instance ID      : '|| l_sources.instance_id(j) );
10220                        msc_sch_wb.atp_debug('*  Source Org ID           : '|| l_sources.organization_id(j) );
10221                        msc_sch_wb.atp_debug('*  Receiving Org ID        : '|| p_atp_record.organization_id );
10222                        msc_sch_wb.atp_debug('*  Plan ID                 : '|| p_plan_id );
10223                        msc_sch_wb.atp_debug('*  Inv Item ID             : '|| p_atp_record.inventory_item_id );
10224                        msc_sch_wb.atp_debug('*  Supplier ID             : '|| l_sources.supplier_id(j) );
10225                        msc_sch_wb.atp_debug('*  Supplier Site ID        : '|| l_sources.supplier_site_id(j) );
10226                        msc_sch_wb.atp_debug('*  Ship Method             : '|| l_sources.Ship_Method(j) );
10227                     END IF;
10228 
10229                     IF l_sources.source_type(j) = MAKE THEN
10230 
10231                         l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
10232                         l_atp_rec.intransit_cal_code := p_atp_record.manufacturing_cal_code;
10233                         l_atp_rec.shipping_cal_code := p_atp_record.manufacturing_cal_code;
10234                         l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;
10235 
10236                     ELSIF l_sources.source_type(j) = TRANSFER THEN
10237 
10238                          --bug 3953635: calculate lead time if its not populated on sourcing rule
10239                         IF l_sources.Ship_Method(j) <> '@@@' THEN
10240                             l_atp_rec.ship_method := l_sources.Ship_Method(j);
10241                             l_atp_rec.delivery_lead_time := l_sources.lead_time(j);
10242                         ELSE
10243                             l_atp_rec.ship_method := null;
10244                             l_atp_rec.delivery_lead_time := null;
10245 
10246                             MSC_ATP_PROC.get_delivery_lead_time(
10247                                         l_sources.organization_id(j), -- p_from_org_id
10248                                         NULL,              -- p_from_loc_id
10249                                         l_sources.instance_id(j),
10250                                         p_atp_record.organization_id,
10251                                         NULL,              -- p_to_loc_id
10252                                         l_sources.instance_id(j),
10253                                         NULL,              -- p_customer_id
10254                                         NULL,              -- p_customer_site_id
10255                                         null,              -- Supplier_id
10256                                         null,              --suplier_site_id
10257                                         MSC_ATP_PVT.G_SESSION_ID,
10258                                         NULL,              -- p_partner_site_id
10259                                         l_atp_rec.ship_method,
10260                                         l_atp_rec.delivery_lead_time);
10261 
10262                             --reset the ship method and lead time because
10263                             --both l_sources.ship_method(j) and l_atp_rec.ship_method is used at
10264                             --different places.
10265                             l_sources.Ship_Method(j) := l_atp_rec.ship_method;
10266                             l_sources.lead_time(j) := l_atp_rec.delivery_lead_time;
10267 
10268                            IF PG_DEBUG in ('Y', 'C') THEN
10269                                msc_sch_wb.atp_debug('Lead time := ' || l_atp_rec.delivery_lead_time);
10270                                msc_sch_wb.atp_debug('Ship Method := ' || l_atp_rec.ship_method);
10271                            END IF;
10272                         END IF;
10273 
10274                         -- Bug 3737759 - Moved from below
10275                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10276                                                              l_sources.instance_id(j),
10277                                                              null,
10278                                                              null,
10279                                                              null,
10280                                                              null,
10281                                                              null,
10282                                                              l_sources.organization_id(j),
10283                                                              null,
10284                                                              MSC_CALENDAR.OMC);
10285 
10286                         -- Bug 3593394
10287                         IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
10288                             -- receiving party is org
10289                             l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10290                                                              p_atp_record.instance_id,
10291                                                              null,
10292                                                              null,
10293                                                              null,
10294                                                              null,
10295                                                              null,
10296                                                              p_atp_record.organization_id,
10297                                                              l_sources.Ship_Method(j),
10298                                                              MSC_CALENDAR.ORC);
10299 
10300                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10301                                                              l_sources.instance_id(j),
10302                                                              null,
10303                                                              null,
10304                                                              null,
10305                                                              null,
10306                                                              4,
10307                                                              null,
10308                                                              l_sources.Ship_Method(j),
10309                                                              MSC_CALENDAR.VIC);
10310 
10311                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10312                                                              l_sources.instance_id(j),
10313                                                              null,
10314                                                              null,
10315                                                              null,
10316                                                              null,
10317                                                              null,
10318                                                              l_sources.organization_id(j),
10319                                                              l_sources.Ship_Method(j),
10320                                                              MSC_CALENDAR.OSC);
10321                         ELSE
10322                             -- Bug 3593394
10323                             IF PG_DEBUG in ('Y', 'C') THEN
10324                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'G_USE_SHIP_REC_CAL is N');
10325                             END IF;
10326                             -- Bug 3737759 - Use receiving org's OMC - l_atp_rec.receiving_cal_code := MSC_CALENDAR.FOC;
10327                             l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
10328                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.FOC;
10329                             -- Bug 3737759 - Use shipping org's OMC - l_atp_rec.shipping_cal_code := MSC_CALENDAR.FOC;
10330                             l_atp_rec.shipping_cal_code := l_atp_rec.manufacturing_cal_code;
10331                         END IF;
10332 
10333                         /* Bug 3737759 - Moved to beginning of IF
10334                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10335                                                              l_sources.instance_id(j),
10336                                                              null,
10337                                                              null,
10338                                                              null,
10339                                                              null,
10340                                                              null,
10341                                                              l_sources.organization_id(j),
10342                                                              null,
10343                                                              MSC_CALENDAR.OMC);
10344                         */
10345                     ELSE
10346                         -- receiving party is org
10347                         /* Bug 3593394 - Moved after get_delivery_lead_time
10348                         l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10349                                                              p_atp_record.instance_id,
10350                                                              null,
10351                                                              null,
10352                                                              null,
10353                                                              null,
10354                                                              null,
10355                                                              p_atp_record.organization_id,
10356                                                              l_sources.Ship_Method(j),
10357                                                              MSC_CALENDAR.ORC);
10358                         */
10359 
10360                         -- supplier intransit LT
10361                         MSC_ATP_PROC.Get_Supplier_Regions(l_sources.Supplier_Site_Id(j),
10362                                                           724,  -- calling module is immaterial here
10363                                                           l_sources.instance_id(j),
10364                                                           l_return_status);
10365                         --3953635: If ship method is null then l_sources.Ship_Method(j) contains '@@@'
10366                         -- Decode it back to null if it contains '@@@'
10367                         IF l_sources.Ship_Method(j) <> '@@@' THEN
10368                             l_atp_rec.ship_method := l_sources.Ship_Method(j);
10369                         ELSE
10370                             l_atp_rec.ship_method := null;
10371                         END IF;
10372 
10373                         MSC_ATP_PROC.get_delivery_lead_time(
10374                                         NULL,              -- p_from_org_id
10375                                         NULL,              -- p_from_loc_id
10376                                         l_sources.instance_id(j),
10377                                         p_atp_record.organization_id,
10378                                         NULL,              -- p_to_loc_id
10379                                         l_sources.instance_id(j),
10380                                         NULL,              -- p_customer_id
10381                                         NULL,              -- p_customer_site_id
10382                                         l_sources.Supplier_Id(j),
10383                                         l_sources.Supplier_Site_Id(j),
10384                                         MSC_ATP_PVT.G_SESSION_ID,
10385                                         NULL,              -- p_partner_site_id
10386                                         l_atp_rec.ship_method,
10387                                         l_atp_rec.delivery_lead_time);
10388 
10389                         IF PG_DEBUG in ('Y', 'C') THEN
10390                            msc_sch_wb.atp_debug('ATP_Check: Use SMC irrespective of profile G_USE_SHIP_REC_CAL');
10391                         END IF;
10392                         --l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;
10393                         --Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
10394 
10395                         l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10396                                                              l_sources.instance_id(j),
10397                                                              p_plan_id,
10398                                                              --s_cto_rearch
10399                                                              --In case of ATP item pass model's id
10400                                                              --base item id is populated only for ATO items
10401                                                              NVL(C_ITEM_INFO_REC.base_item_id,
10402                                                                      C_ITEM_INFO_REC.dest_inv_item_id),
10403                                                              l_sources.supplier_id(j),
10404                                                              l_sources.supplier_site_id(j),
10405                                                              null,
10406                                                              null,
10407                                                              null,
10408                                                              MSC_CALENDAR.SMC);
10409 
10410                         -- Bug 3593394
10411                         IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
10412 
10413                             l_atp_rec.receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10414                                                              p_atp_record.instance_id,
10415                                                              null,
10416                                                              null,
10417                                                              null,
10418                                                              null,
10419                                                              null,
10420                                                              p_atp_record.organization_id,
10421                                                              l_atp_rec.ship_method,
10422                                                              -- Bug 3593394 - l_sources.Ship_Method(j),
10423                                                              MSC_CALENDAR.ORC);
10424 
10425                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10426                                                              p_atp_record.instance_id,
10427                                                              null,
10428                                                              null,
10429                                                              null,
10430                                                              null,
10431                                                              4,
10432                                                              null,
10433                                                              l_atp_rec.ship_method,
10434                                                              MSC_CALENDAR.VIC);
10435 
10436                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10437                                                              l_sources.instance_id(j),
10438                                                              null,
10439                                                              null,
10440                                                              l_sources.supplier_id(j),
10441                                                              l_sources.supplier_site_id(j),
10442                                                              1,
10443                                                              null,
10444                                                              l_atp_rec.ship_method,
10445                                                              MSC_CALENDAR.SSC);
10446 
10447                             /* Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
10448                             l_atp_rec.manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
10449                                                              l_sources.instance_id(j),
10450                                                              p_plan_id,
10451                                                              --s_cto_rearch
10452                                                              --In case of ATP item pass model's id
10453                                                              --base item id is populated only for ATO items
10454                                                              NVL(C_ITEM_INFO_REC.base_item_id,
10455                                                                      C_ITEM_INFO_REC.dest_inv_item_id),
10456                                                              l_sources.supplier_id(j),
10457                                                              l_sources.supplier_site_id(j),
10458                                                              null,
10459                                                              null,
10460                                                              null,
10461                                                              MSC_CALENDAR.SMC);
10462                             */
10463 
10464                         ELSE
10465 
10466                             -- Bug 3593394
10467                             IF PG_DEBUG in ('Y', 'C') THEN
10468                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'G_USE_SHIP_REC_CAL is N');
10469                             END IF;
10470                             -- Bug 3737759 - Use receiving org's OMC - l_atp_rec.receiving_cal_code := MSC_CALENDAR.FOC;
10471                             l_atp_rec.receiving_cal_code := p_atp_record.manufacturing_cal_code;
10472                             l_atp_rec.intransit_cal_code := MSC_CALENDAR.FOC;
10473                             l_atp_rec.shipping_cal_code := MSC_CALENDAR.FOC;
10474 
10475                             /*Bug 3687353: Use SMC irrespective of profile MSC_ATP_PVT.G_USE_SHIP_REC_CAL
10476                             l_atp_rec.manufacturing_cal_code := p_atp_record.manufacturing_cal_code;*/
10477 
10478                         END IF;
10479                     END IF;
10480                     IF PG_DEBUG in ('Y', 'C') THEN
10481                        msc_sch_wb.atp_debug('  ');
10482                        msc_sch_wb.atp_debug('*  ___________________Output____________________');
10483                        msc_sch_wb.atp_debug('*  ');
10484                        msc_sch_wb.atp_debug('*  Receiving calendar code         : ' || l_atp_rec.receiving_cal_code);
10485                        msc_sch_wb.atp_debug('*  Intransit calendar code         : ' || l_atp_rec.intransit_cal_code);
10486                        msc_sch_wb.atp_debug('*  Shipping calendar code          : ' || l_atp_rec.shipping_cal_code);
10487                        msc_sch_wb.atp_debug('*  Manufacturing calendar code     : ' || l_atp_rec.manufacturing_cal_code);
10488                        msc_sch_wb.atp_debug('**************************************************************');
10489                     END IF;
10490 
10491                     /* planned order due date as per OMC-D */
10492                     l_planned_order_date := MSC_CALENDAR.PREV_WORK_DAY(
10493                                                 p_atp_record.manufacturing_cal_code,
10494                                                 p_atp_record.instance_id,
10495                                                 l_requested_ship_date);
10496                     IF PG_DEBUG in ('Y', 'C') THEN
10497                        msc_sch_wb.atp_debug('*  Planned Order Due Date as per OMC-D: ' || l_planned_order_date);
10498                     END IF;
10499                     -- ship_rec_cal changes end
10500 
10501                     IF ((l_sources.source_type(j) = MAKE) or
10502 			(l_sources.source_type(j) = BUY))
10503 		    THEN
10504 			  -- dsting
10505 			  IF PG_DEBUG in ('Y', 'C') THEN
10506 			     msc_sch_wb.atp_debug('ATP_Check: ' || 'MAKE_BUY_CHILDREN: have make/buy source');
10507 			  END IF;
10508 			  p_atp_record.children_type := MAKE_BUY_CHILDREN;
10509 			  G_HAVE_MAKE_BUY_PARENT := 1;
10510 		    END IF;
10511 
10512                     --diag_atp
10513                     --reinitialize all the variables so that values from last source is not carried over
10514                     l_pegging_rec.plan_name := null;
10515                     l_pegging_rec.required_quantity:= null;
10516                     l_pegging_rec.required_date := null;
10517                     --bug 3328421
10518                     l_pegging_rec.actual_supply_demand_date := null;
10519                     l_pegging_rec.postprocessing_lead_time := null;
10520                     l_pegging_rec.preprocessing_lead_time := null;
10521                     l_pegging_rec.processing_lead_time := null;
10522                     l_pegging_rec.constraint_date := null;
10523                     l_pegging_rec.constraint_type := null;
10524                     l_pegging_rec.intransit_lead_time := null;
10525                     l_pegging_rec.ship_method := null;
10526 
10527                     IF ((l_sources.source_type(j) = TRANSFER) or
10528                         (l_sources.source_type(j) = BUY)) THEN
10529 
10530                         -- 1214740
10531                         l_atp_rec.identifier := p_atp_record.identifier;
10532                         l_atp_rec.demand_source_type := p_atp_record.demand_source_type;--cmro
10533                         --- Enhance CTO Phase 1 Req #16, #17
10534                         -- Pass the demand source line across only for CTO case.
10535                         --IF  NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) = p_atp_record.organization_id THEN
10536                         --IF ( MSC_ATP_PVT.G_CTO_LINE = 'Y' ) THEN
10537                         --    l_atp_rec.demand_source_line := p_atp_record.demand_source_line;
10538                         --END IF;
10539                         --- End Enhance CTO Phase 1 Req #16, #17
10540 
10541                         l_atp_rec.instance_id := l_sources.instance_id(j);
10542 
10543                         /* time_phased_atp changes begin
10544                            If time phased atp and buy scenario then populate member item id.
10545                            This is because we are not supporting aggregate supplier capacity currently*/
10546                         IF l_sources.source_type(j) = BUY THEN
10547                             l_atp_rec.inventory_item_id := l_request_item_id;
10548                         ELSE
10549                             l_atp_rec.inventory_item_id := p_atp_record.inventory_item_id;
10550                         END IF;
10551                         -- time_phased_atp changes end
10552 
10553                         l_atp_rec.request_item_id :=
10554 				p_atp_record.request_item_id;
10555 			---- EFTC: check for buy_make code. If it is 3 then no sources defined and
10556 			---- therefore no source is known
10557 
10558 			---    l_atp_rec.supplier_id := NULL;
10559 		       	---     l_atp_rec.supplier_site_id := NULL;
10560 		        ---	ELSE
10561                         l_atp_rec.supplier_id :=  l_sources.Supplier_Id(j);
10562                         l_atp_rec.supplier_site_id := l_sources.Supplier_Site_Id(j);
10563 			---END IF;
10564                         IF l_sources.source_type(j) = BUY  THEN
10565                           -- this is to make sure we know what's the
10566                           -- receiving org since supplier capacities are
10567                           -- defined this way.
10568 
10569                           -- dsting 2764213
10570                           l_planned_sources := 1;
10571 
10572                           -- for 1381331 fix
10573                           l_atp_rec.instance_id := p_atp_record.instance_id;
10574 
10575                           l_atp_rec.organization_id :=
10576 				p_atp_record.organization_id;
10577 
10578                           -- this is the plan id for next level
10579                           l_plan_id := p_plan_id;
10580                           l_assign_set_id := p_assign_set_id;
10581 
10582                           -- for bug 1124206: to be consistant with planning,
10583                           -- the lead time from a vendor site to an inv org
10584                           -- should be obtained from the postprocessing
10585                           -- lead time instead of leave it blank and
10586                           -- have atp pick up
10587                           -- the default one in mtl_interorg_ship_methods.
10588 
10589                           -- bug 1303196: delivery lead time is
10590                           -- postprocessing lead time,
10591                           -- not postprocessing lead time+1
10592 
10593                           -- Bug2085872 Check if l_inv_item_id is null.
10594                           IF l_inv_item_id IS NULL THEN
10595                              /* Modularize Item and Org Info */
10596                              -- Buy case
10597                              -- Re-Use info instead of making unnecessary call.
10598                              l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
10599                              /*l_inv_item_id := MSC_ATP_FUNC.get_inv_item_id(
10600                                           		l_atp_rec.instance_id,
10601                                           		l_atp_rec.inventory_item_id,
10602                                                         null,
10603                                           		l_atp_rec.organization_id);
10604                               Modularize Item and Org Info */
10605                           END IF;
10606                           ---  2178544 replace this query as we already have processing lead time
10607                           /*begin
10608                           SELECT nvl(postprocessing_lead_time, 0)
10609                           INTO   l_atp_rec.delivery_lead_time
10610                           FROM   msc_system_items
10611                           WHERE  plan_id = p_plan_id
10612                           AND    organization_id = l_atp_rec.organization_id
10613                           AND    sr_instance_id = l_atp_rec.instance_id
10614                           AND    inventory_item_id = l_inv_item_id; -- 1665483
10615                           exception
10616                             when others then
10617                               l_atp_rec.delivery_lead_time := 0;
10618                           end; */
10619                           -- l_atp_rec.delivery_lead_time := l_post_pro_lt; -- suuplier intransit lt
10620 
10621                           --l_atp_rec.ship_method := 'DEFAULT';
10622                           l_new_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
10623 			  l_old_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
10624 
10625                           --diag_atp
10626                           l_atp_rec.plan_name := p_atp_record.plan_name;
10627 
10628 
10629                         ELSE
10630                           --s_cto_rearch
10631                           IF  PG_DEBUG in ('Y', 'C') THEN
10632                               msc_Sch_wb.atp_debug('Transfer case: bom_item_type := ' || C_ITEM_INFO_REC.bom_item_type);
10633                               msc_Sch_wb.atp_debug('Transfer case: parent_repl_ord_flag := ' || C_ITEM_INFO_REC.replenish_to_ord_flag);
10634 
10635                           END IF;
10636                           IF C_ITEM_INFO_REC.bom_item_type in (1, 2) and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
10637                               IF PG_DEBUG in ('Y', 'C') THEN
10638                                  msc_Sch_wb.atp_debug('This is model entity. Pass Line IDs');
10639                               END IF;
10640                               --if model then pass the
10641                               l_atp_rec.Top_Model_line_id := p_atp_record.Top_Model_line_id;
10642                               l_atp_rec.ATO_Parent_Model_Line_Id := p_atp_record.ATO_Parent_Model_Line_Id;
10643                               l_atp_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
10644                               l_atp_rec.Parent_line_id := p_atp_record.Parent_line_id;
10645                               l_atp_rec.wip_supply_type := p_atp_record.wip_supply_type;
10646                               l_atp_rec.parent_atp_flag := null;
10647                               l_atp_rec.parent_atp_comp_flag := null;
10648                               l_atp_rec.parent_repl_order_flag := null;
10649                               l_atp_rec.parent_bom_item_type := null;
10650                               l_atp_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
10651                               l_atp_rec.rep_ord_flag  := C_ITEM_INFO_REC.replenish_to_ord_flag;
10652                               l_atp_rec.mand_comp_flag := 2;
10653                               l_atp_rec.parent_so_quantity := p_atp_record.parent_so_quantity;
10654                               l_atp_rec.demand_source_line := p_atp_record.demand_source_line;
10655                               l_atp_rec.refresh_number :=  p_atp_record.refresh_number;
10656 
10657                           ELSIF C_ITEM_INFO_REC.bom_item_type  = 4 and C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
10658                               l_atp_rec.ATO_Model_Line_Id := p_atp_record.ATO_Model_Line_Id;
10659                           END IF;
10660                           --e_cto_rearch
10661                           -- transfer from which org.
10662                           l_atp_rec.organization_id :=
10663                                 l_sources.Organization_Id(j);
10664 
10665 
10666                           ---- store summary flags before and after get_plan_info
10667                           ---- The reason we are doing it is that we haven't added
10668                           ---- planned order as yet. If summary is not available for
10669                           ---- new plann then MSC_ATP_PVT.G_SUMMARY_FLAG will be set to 'N'.
10670                           ---- When we try to add planned order then the sumamry tables will
10671                           ----- not be updated even though the G_SUMMARY_FLAG for the old org's
10672                           ----- plan was 'Y'
10673 
10674                           l_old_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
10675 
10676                           /*
10677                           -- New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
10678                                 -- (SCLT)
10679                           MSC_ATP_PROC.get_global_plan_info(l_atp_rec.instance_id,
10680                                       l_atp_rec.inventory_item_id,
10681                                       l_atp_rec.organization_id,
10682                                       l_atp_rec.demand_class);*/
10683 
10684                           --Bug4548245 When we call this procedure from schedule then we check in ODS data
10685                            l_atp_rec.inventory_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(l_atp_rec.instance_id,
10686                                                                              -1,
10687                                                                              l_atp_rec.request_item_id,
10688                                                                              l_atp_rec.organization_id);
10689                           --set the correct family item value
10690                           --Bug4548245 end
10691                           /* time_phased_atp changes begin
10692                              Call new procedure Get_PF_Plan_Info*/
10693                           MSC_ATP_PF.Get_PF_Plan_Info(
10694                                        l_atp_rec.instance_id,
10695                                        l_atp_rec.request_item_id,
10696                                        l_atp_rec.inventory_item_id,
10697                                        l_atp_rec.organization_id,
10698                                        l_atp_rec.demand_class,
10699                                        l_atp_rec.atf_date,
10700                                        l_atp_rec.error_code,
10701                                        l_return_status,
10702                                        p_plan_id --bug3510475 same plan when
10703                                                  --moving from one org to another
10704                           );
10705 
10706                           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10707                                 IF PG_DEBUG in ('Y', 'C') THEN
10708                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime');
10709                                 END IF;
10710                                 x_return_status := FND_API.G_RET_STS_ERROR;
10711                                 RAISE FND_API.G_EXC_ERROR;
10712                           END IF;
10713                           /* time_phased_atp changes end*/
10714 
10715                           l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
10716                           -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
10717 
10718                           l_plan_id             := l_plan_info_rec.plan_id;
10719                           l_assign_set_id       := l_plan_info_rec.assignment_set_id;
10720                           -- changes for bug 2392456 ends
10721 
10722 
10723                           l_new_summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
10724 
10725                           /* bug 3953635: Del_lead_time and ship method have already been populated
10726                           -- for bug 1124206
10727                           l_atp_rec.delivery_lead_time :=
10728                                 l_sources.Lead_Time(j);
10729                           l_atp_rec.ship_method :=
10730                                 l_sources.Ship_Method(j);
10731                           */
10732                           --diag_atp
10733                           l_atp_rec.plan_name := l_plan_info_rec.plan_name;
10734 
10735                         END IF;
10736 
10737                         -- need to do uom quantity conversion
10738 
10739                         -- Bug 2382963, ATP was erroring out in case a component is being transferred
10740                         -- from an organization with no ATPable plan due to PDS-ODS switch
10741 
10742                         -- 24x7 ATP
10743                         -- IF (l_plan_id is NULL) or (l_plan_id = -100) THEN
10744                         IF NVL(l_plan_id, -1) = -1 or (l_plan_id  = -100  ) THEN
10745                            IF PG_DEBUG in ('Y', 'C') THEN
10746                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan not found for : '||
10747 				                    l_atp_rec.inventory_item_id
10748 				                    ||' in org : '||l_atp_rec.organization_id);
10749                            END IF;
10750                         ELSIF l_plan_id = -300 then
10751                            IF PG_DEBUG in ('Y', 'C') THEN
10752                               msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime in plan for' ||
10753 				                    l_atp_rec.inventory_item_id
10754 				                    ||' in org : '||l_atp_rec.organization_id);
10755                            END IF;
10756                            --bug 2854351
10757                            --l_atp_rec.error_code := TRY_ATP_LATER;
10758                            l_atp_rec.error_code := PLAN_DOWN_TIME;
10759                            MSC_ATP_PVT.G_DOWNTIME_HIT := 'Y';
10760                            RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
10761 
10762                         ELSIF l_plan_id is NOT NULL THEN
10763                            -- dsting 2764213
10764                            l_planned_sources := 1;
10765 
10766                            MSC_ATP_PROC.inv_primary_uom_conversion(l_atp_rec.instance_id,
10767                                           l_atp_rec.organization_id,
10768                                           l_atp_rec.inventory_item_id,
10769                                           p_atp_record.quantity_uom,
10770                                           l_atp_rec.quantity_uom,
10771                                           l_conversion_rate);
10772 
10773                            -- dsting 2754446 adjust planned order amt according to rounding_control_type
10774                            --bug3397904 start
10775                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
10776                            IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
10777                               --l_PO_qty := CEIL(l_net_demand); --bug3397904
10778                               IF (p_search = FORWARD_SCHEDULING)
10779                                  and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
10780                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
10781                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
10782 
10783                                    l_PO_qty := CEIL(p_atp_record.quantity_ordered);
10784                                    IF PG_DEBUG in ('Y', 'C') THEN
10785                                       msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
10786                                    END IF;
10787                               ELSE
10788                                l_PO_qty := CEIL(l_net_demand);
10789                                IF PG_DEBUG in ('Y', 'C') THEN
10790                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
10791                                END IF;
10792                               END IF;
10793                            ELSE
10794                               --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
10795                               IF (p_search = FORWARD_SCHEDULING)
10796                                  and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
10797                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
10798                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
10799 
10800                                    l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);   --5598066
10801                                    IF PG_DEBUG in ('Y', 'C') THEN
10802                                       msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
10803                                    END IF;
10804                               ELSE
10805                                l_PO_qty := trunc(l_net_demand, 6);	-- 5598066
10806                                IF PG_DEBUG in ('Y', 'C') THEN
10807                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
10808                                END IF;
10809                               END IF;
10810                            END IF;
10811                           --bug3397904 end
10812                            l_atp_rec.quantity_ordered:=l_PO_qty*
10813                                                        l_conversion_rate;
10814                           l_atp_rec.quantity_ordered := ROUND(l_atp_rec.quantity_ordered, 6);  --4562646
10815 --                        l_atp_rec.quantity_ordered:=l_net_demand*
10816 --                                                    l_conversion_rate;
10817 
10818                         -- since it is a transfer/buy, we specify request
10819 		        -- arrival date not request ship date
10820 
10821                         -- Need to initialize these dates so that only one is populated
10822 
10823                         l_atp_rec.requested_ship_date := NULL;
10824                         l_atp_rec.requested_arrival_date := NULL;
10825 
10826                         IF PG_DEBUG in ('Y', 'C') THEN
10827                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
10828                         END IF;
10829 
10830                         --- 2178544 calculate start date
10831                         --diag_atp
10832                         l_constraint_flag := 2;
10833                         -- Enforce Pur LT
10834                         l_trunc_started := 2;
10835                         IF l_sources.source_type(j) = TRANSFER  THEN
10836 
10837                             IF PG_DEBUG in ('Y', 'C') THEN
10838                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Backward Scheduling ');
10839                                msc_sch_wb.atp_debug('ATP_Check: ' || 'From Org : '|| l_atp_rec.organization_id);
10840                                msc_sch_wb.atp_debug('ATP_Check: ' || 'To Org : '|| p_atp_record.organization_id);
10841                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Lead Time : '|| l_atp_rec.delivery_lead_time);
10842                                msc_sch_wb.atp_debug('ATP_Check: ' || 'PPLT '||l_post_pro_lt);
10843                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship_method := ' || l_sources.ship_method(j));
10844                                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' ||MSC_ATP_PVT.G_PTF_DATE );
10845                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' ||l_ptf_date_for_child );
10846                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' ||l_ptf_date);
10847                             END IF;
10848 
10849                             -- Bug 3782472 - calculate ptf date for lower level
10850                             l_ptf_date_for_child := MSC_CALENDAR.DATE_OFFSET (
10851                                                 p_atp_record.manufacturing_cal_code,
10852                                                 p_atp_record.instance_id,
10853                                                 l_parent_ptf_date,
10854                                                 -1 * l_post_pro_lt, -1);
10855                             IF PG_DEBUG in ('Y', 'C') THEN
10856                                msc_sch_wb.atp_debug('ATP_Check: ' ||
10857                                                     'After Man. Cal Offset l_ptf_date_for_child := ' ||l_ptf_date_for_child );
10858                             END IF;
10859                             l_ptf_date_for_child := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
10860         					l_ptf_date_for_child, l_atp_rec.receiving_cal_code, -1,
10861         					l_atp_rec.intransit_cal_code, -1 * l_atp_rec.delivery_lead_time, -1,
10862         					l_atp_rec.shipping_cal_code, -1, p_atp_record.instance_id);
10863                             IF PG_DEBUG in ('Y', 'C') THEN
10864                                msc_sch_wb.atp_debug('ATP_Check: ' || 'After 3 Set Cal  Offset l_ptf_date_for_child := ' ||l_ptf_date_for_child );
10865                                msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_PTF_DATE := ' ||MSC_ATP_PVT.G_PTF_DATE );
10866                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' ||l_ptf_date_for_child );
10867                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' ||l_ptf_date);
10868                             END IF;
10869 
10870                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child,l_ptf_date,MSC_ATP_PVT.g_ptf_date); --5053993
10871 
10872                             IF PG_DEBUG in ('Y', 'C') THEN
10873                                msc_sch_wb.atp_debug('ATP_Check: ' || 'After Offset MSC_ATP_PVT.G_PTF_DATE := '
10874                                                     ||MSC_ATP_PVT.G_PTF_DATE );
10875                             END IF;
10876 
10877                             /* ship_rec_cal changes begin */
10878                            l_dock_date := MSC_CALENDAR.DATE_OFFSET (
10879                                                 p_atp_record.manufacturing_cal_code,
10880                                                 p_atp_record.instance_id,
10881                                                 l_requested_ship_date,
10882                                                 -1 * l_post_pro_lt, -1);
10883 
10884                            IF PG_DEBUG in ('Y', 'C') THEN
10885                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child: '||l_ptf_date_for_child);
10886                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
10887                               msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
10888                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PPLT using OMC: '||l_dock_date);
10889                            END IF;
10890 
10891                            l_dock_date := MSC_CALENDAR.PREV_WORK_DAY(
10892                                                 l_atp_rec.receiving_cal_code,
10893                                                 p_atp_record.instance_id,
10894                                                 l_dock_date);
10895                            /* populating new_dock_date for transfer orders also, this is to support requirement of supporting
10896                               org modeled as supplier which might come in future*/
10897                            l_new_dock_date := l_dock_date;
10898 
10899                            IF PG_DEBUG in ('Y', 'C') THEN
10900                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_dock_date);
10901                            END IF;
10902 
10903                            l_start_date := MSC_CALENDAR.DATE_OFFSET (
10904                                                 l_atp_rec.intransit_cal_code,
10905                                                 p_atp_record.instance_id,
10906                                                 l_dock_date,
10907                                                 -1 * l_atp_rec.delivery_lead_time, -1);
10908                            -- Bug No  4592289/4667756/5053993 Start
10909                            IF PG_DEBUG in ('Y', 'C') THEN
10910                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE before offset  : '||MSC_ATP_PVT.G_PTF_DATE);
10911                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_PTF_DATE before offset  : '||l_PTF_DATE);
10912                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_START_DATE before offset  : '||l_START_DATE);
10913                               msc_sch_wb.atp_debug('ATP_Check: ' || 'From Org : '|| l_atp_rec.organization_id);
10914                               msc_sch_wb.atp_debug('ATP_Check: ' || 'To Org : '|| p_atp_record.organization_id);
10915                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Lead Time : '|| l_atp_rec.delivery_lead_time);
10916                            END IF;
10917                            IF l_PTF_DATE < MSC_ATP_PVT.G_PTF_DATE  THEN
10918                               MSC_ATP_PVT.G_PTF_DATE := MSC_CALENDAR.DATE_OFFSET (l_atp_rec.intransit_cal_code,
10919                                                                    p_atp_record.instance_id,
10920                                                                       MSC_ATP_PVT.G_PTF_DATE ,
10921                                                                    -1 * l_atp_rec.delivery_lead_time, -1); -- Bug No: 4592289/4667756
10922                            IF PG_DEBUG in ('Y', 'C') THEN
10923                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after offset  : '||MSC_ATP_PVT.G_PTF_DATE);
10924                            END IF;
10925                            else
10926                               G_PTF_DATE := l_PTF_DATE;
10927                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after assign  : '||MSC_ATP_PVT.G_PTF_DATE );
10928                            END IF;
10929                            MSC_ATP_PVT.G_PTF_DATE := MSC_CALENDAR.NEXT_WORK_DAY(
10930                                                                 l_atp_rec.shipping_cal_code,
10931                                                                 p_atp_record.instance_id,
10932                                                                 MSC_ATP_PVT.G_PTF_DATE );
10933                            IF PG_DEBUG in ('Y', 'C') THEN
10934                               msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE after offset(Shipping cal)  : '||MSC_ATP_PVT.G_PTF_DATE);
10935                            END IF;
10936 
10937                            -- Bug No  4592289/4667756/5053993 Ends
10938 
10939                            IF PG_DEBUG in ('Y', 'C') THEN
10940                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting DLT using VIC : '||l_start_date);
10941                            END IF;
10942 
10943                            l_atp_rec.requested_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
10944                                                 l_atp_rec.shipping_cal_code,
10945                                                 l_atp_rec.instance_id,
10946                                                 l_start_date);
10947                            /* populating new_ship_date for transfer orders also, this is to support requirement of supporting
10948                               org modeled as supplier which might come in future*/
10949                            l_new_ship_date := l_atp_rec.requested_ship_date;
10950                            l_sugg_start_date := l_new_ship_date;        -- Bug 3241766
10951                            l_start_date := l_new_ship_date; -- Bug 3412143 -- Setting the variable that is compared with PTF date
10952                                                             -- This should be source org's ship date.
10953 
10954                            IF PG_DEBUG in ('Y', 'C') THEN
10955                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC : '||l_atp_rec.requested_ship_date);
10956                            END IF;
10957 
10958                            l_order_date := MSC_CALENDAR.DATE_OFFSET (
10959                                                 p_atp_record.manufacturing_cal_code,
10960                                                 p_atp_record.instance_id,
10961                                                 l_atp_rec.requested_ship_date,
10962                                                 -1 * MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, -1);
10963 
10964                            IF PG_DEBUG in ('Y', 'C') THEN
10965                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
10966                               msc_sch_wb.atp_debug('ATP_Check: ' || '____________________End Offset____________________');
10967                            END IF;
10968                            -- ship_rec_cal changes end
10969 
10970                            IF PG_DEBUG in ('Y', 'C') THEN
10971                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date '||l_atp_rec.requested_ship_date);
10972                            END IF;
10973 
10974                            IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 or p_search = forward_scheduling) and
10975                                   -- ship_rec_cal
10976                                   -- (l_dock_date < l_ptf_date or l_order_date < sysdate) THEN
10977                                   -- Bug 3412143 - Ship date in source org should be greater than receiving org's PTF
10978                                   -- Also use l_trunc_sysdate instead of accessing sysdate directly.
10979                                   (l_start_date < l_ptf_date or l_order_date < l_trunc_sysdate) THEN
10980 
10981                               /* ship_rec_cal changes begin*/
10982                               IF PG_DEBUG in ('Y', 'C') THEN
10983                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc PTF Due Date___________________');
10984                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date: '||l_ptf_date);
10985                               END IF;
10986 
10987                               /* Bug 3412143 - Earlier ptf_due_date was calculated as
10988                               ptf_due_date := ptf_date + pre-processing lead time + delivery lead time + post-processing lead time
10989                               it should actually be
10990                               ptf_due_date := ptf_date + delivery lead time + post-processing lead time
10991                               l_ptf_due_date := MSC_CALENDAR.DATE_OFFSET (
10992                                                 p_atp_record.manufacturing_cal_code,
10993                                                 p_atp_record.instance_id,
10994                                                 l_ptf_date,
10995                                                 MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, 1);
10996 
10997                               IF PG_DEBUG in ('Y', 'C') THEN
10998                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding pre PLT using OMC: '||l_ptf_due_date);
10999                               END IF;
11000                               */
11001 
11002                               l_ptf_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11003                         			l_ptf_date, l_atp_rec.shipping_cal_code, +1,
11004                         			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
11005                         			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
11006 
11007                               IF PG_DEBUG in ('Y', 'C') THEN
11008                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC, adding intransit LT using VIC and ');
11009                                   msc_sch_wb.atp_debug('ATP_Check: ' || '    validating on ORC: '||l_ptf_due_date);
11010                               END IF;
11011 
11012                               l_ptf_due_date :=  MSC_CALENDAR.DATE_OFFSET (
11013                                                 p_atp_record.manufacturing_cal_code, -- using OMC destination
11014                                                 p_atp_record.instance_id,
11015                                                 l_ptf_due_date,
11016                                                 l_post_pro_lt, 1);
11017 
11018                               IF PG_DEBUG in ('Y', 'C') THEN
11019                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ptf_due_date);
11020                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc PTF Due Date___________________');
11021                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' ');
11022                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc LT Due Date___________________');
11023                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate: '||sysdate);
11024                               END IF;
11025 
11026                               l_lt_due_date := MSC_CALENDAR.DATE_OFFSET (
11027                                                 p_atp_record.manufacturing_cal_code,
11028                                                 p_atp_record.instance_id,
11029                                                 -- sysdate, - Bug 3512996 - Removed unnecessary reference to sysdate
11030                                                 l_trunc_sysdate,
11031                                                 MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, 1);
11032 
11033                               -- Bug 3512996 - Set this date as PTF date for the source org so that the call to ATP_Check
11034                               --               for source org does not return a date earlier than this date, thereby enforcing
11035                               --               pre-PLT for transfer cases. Also this date doesn't need to be validated on OSC
11036                               --               the output of ATP_Check will anyway be validated on OSC.
11037                               MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_lt_due_date, MSC_ATP_PVT.G_PTF_DATE);
11038 
11039                               IF PG_DEBUG in ('Y', 'C') THEN
11040                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding pre PLT using OMC: '||l_lt_due_date);
11041                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting PTF for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
11042                               END IF;
11043 
11044                               l_lt_due_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11045                         			l_lt_due_date, l_atp_rec.shipping_cal_code, +1,
11046                         			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
11047                         			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
11048 
11049                               IF PG_DEBUG in ('Y', 'C') THEN
11050                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC, adding intransit LT using VIC and ');
11051                                   msc_sch_wb.atp_debug('ATP_Check: ' || '    validating on ORC: '||l_lt_due_date);
11052                               END IF;
11053 
11054                               l_lt_due_date :=  MSC_CALENDAR.DATE_OFFSET (
11055                                                 p_atp_record.manufacturing_cal_code, -- using OMC destination
11056                                                 p_atp_record.instance_id,
11057                                                 l_lt_due_date,
11058                                                 l_post_pro_lt, 1);
11059 
11060                               IF PG_DEBUG in ('Y', 'C') THEN
11061                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_lt_due_date);
11062                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc LT Due Date___________________');
11063                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' ');
11064                               END IF;
11065                               /* ship_rec_cal end */
11066 
11067                               IF PG_DEBUG in ('Y', 'C') THEN
11068                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
11069                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_due_date := ' || l_ptf_due_date);
11070                               END IF;
11071 
11072 
11073                               -- Bug 3525582 - Constraint message should be added only if constraint actually exists
11074                               IF l_lt_due_date > l_requested_ship_date OR
11075                                  l_ptf_due_date > l_requested_ship_date THEN
11076 
11077                                  -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
11078                                  IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
11079                                     --transfer lead time constraint
11080                                     l_pegging_rec.constraint_type := 5;
11081                                     l_pegging_rec.constraint_date := l_lt_due_date;
11082                                     l_constraint_flag := 1;
11083                                  ELSE
11084                                     ---PTF lead time constraint
11085                                     l_pegging_rec.constraint_type := 2;
11086                                     l_pegging_rec.constraint_date := l_ptf_due_date;
11087                                     l_constraint_flag := 1;
11088                                  END IF;
11089 
11090                               END IF;
11091 
11092                            END IF; --- IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
11093 
11094                         ELSE
11095                            --s_cto_rearch
11096                              l_atp_rec.base_model_id := C_ITEM_INFO_REC.base_item_id;
11097                              l_atp_rec.bom_item_type := C_ITEM_INFO_REC.bom_item_type;
11098                              l_atp_rec.rep_ord_flag :=  C_ITEM_INFO_REC.replenish_to_ord_flag;
11099                            --e_cto_rearch
11100 
11101                             /* Enforce Pur LT - Moved code to get last cap date from Get_Supplier_ATP_Info. At this point it
11102                                         is sufficient to know whether capacity is defined or not. However since this table
11103                                         is being queried, we are getting last cap date as well.*/
11104                             SELECT max(trunc(NVL(to_date,G_PLAN_INFO_REC.plan_cutoff_date)))  --4055719
11105                             INTO   l_atp_rec.last_cap_date
11106                             FROM   msc_supplier_capacities s
11107                             WHERE  s.inventory_item_id = NVL(C_ITEM_INFO_REC.base_item_id,
11108                                                          C_ITEM_INFO_REC.dest_inv_item_id)
11109                             AND    s.sr_instance_id = l_sources.instance_id(j)
11110                             AND    s.plan_id = p_plan_id
11111                             AND    s.organization_id = p_atp_record.organization_id
11112                             AND    s.supplier_id = l_sources.supplier_id(j)
11113                             AND    NVL(s.supplier_site_id, -1) = NVL(l_sources.supplier_site_id(j), -1);
11114 
11115                             IF PG_DEBUG in ('Y', 'C') THEN
11116                               msc_sch_wb.atp_debug('ATP_Check: ' || 'last_cap_date: '||l_atp_rec.last_cap_date);
11117                             END IF;
11118                             /* Enforce Pur LT - changes end */
11119 
11120 
11121                             /* ship_rec_cal changes begin */
11122                             l_start_date := MSC_CALENDAR.DATE_OFFSET(
11123                                                  p_atp_record.manufacturing_cal_code,
11124                                                  p_atp_record.instance_id,
11125                                                  l_requested_ship_date,
11126                                                  -1 * l_post_pro_lt, -1);
11127 
11128                             IF PG_DEBUG in ('Y', 'C') THEN
11129                               msc_sch_wb.atp_debug('ATP_Check: ' || 'enforce_pur_lead_time: ' || G_PLAN_INFO_REC.enforce_pur_lead_time);
11130                               msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
11131                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PPLT using OMC: '||l_start_date);
11132                             END IF;
11133 
11134                             /* Enforce Pur LT - check if date is past due. dock date can be past due only if capacity is not defined. */
11135                             IF l_start_date < l_trunc_sysdate AND G_PLAN_INFO_REC.enforce_pur_lead_time = 2
11136                                 AND l_atp_rec.last_cap_date IS NULL THEN
11137                                 l_trunc_started := 1;
11138                                 l_start_date := l_trunc_sysdate;
11139                                 IF PG_DEBUG in ('Y', 'C') THEN
11140                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11141                                 END IF;
11142                             END IF;
11143 
11144                             /* Enforce Pur LT - No need to calculate dates if trunc has started */
11145                             IF l_trunc_started = 2 THEN
11146                                 -- Actually dock date
11147                                 l_start_date := MSC_CALENDAR.PREV_WORK_DAY(
11148                                                 l_atp_rec.receiving_cal_code,
11149                                                 p_atp_record.instance_id,
11150                                                 l_start_date);
11151                                 IF PG_DEBUG in ('Y', 'C') THEN
11152                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_start_date);
11153                                 END IF;
11154                                 /* Enforce Pur LT - check if date is past due. dock date can be past due only
11155                                         if capacity is not defined. */
11156                                 IF l_start_date < l_trunc_sysdate AND G_PLAN_INFO_REC.enforce_pur_lead_time = 2
11157                                     AND l_atp_rec.last_cap_date IS NULL THEN
11158                                     l_trunc_started := 1;
11159                                     l_start_date := l_trunc_sysdate;
11160                                     IF PG_DEBUG in ('Y', 'C') THEN
11161                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11162                                     END IF;
11163                                 END IF;
11164                             END IF;
11165 
11166                            /* populating new_dock_date */
11167                            l_new_dock_date := l_start_date;
11168 
11169                            IF PG_DEBUG in ('Y', 'C') THEN
11170                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Dock date : '||l_new_dock_date);
11171                               msc_sch_wb.atp_debug('ATP_Check: ' || '  ');
11172                            END IF;
11173                            /* ship_rec_cal changes end */
11174 
11175                            --diag_atp: we calculate the supplier lead time here.
11176                            -- Supplier Capacity and Lead Time (SCLT) changes
11177 
11178                            BEGIN
11179                              -- l_process_lt := l_fixed_lt + l_variable_lt * l_net_demand;
11180                              -- Fixed + Variable is discontinued, Instead item's processing lead time
11181                              -- essentially full_lead_time is used.
11182 
11183                              l_process_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.processing_lt;
11184                              l_pre_pro_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
11185                              IF PG_DEBUG in ('Y', 'C') THEN
11186                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Process_lead_time = '|| l_process_lt);
11187                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pre_pro_lt  = ' || l_pre_pro_lt);
11188                              END IF;
11189 
11190                              IF (l_sources.Supplier_Id(j) <> -99) THEN
11191                                 -- Query changed to include lead_time_date
11192                                 select   nvl(processing_lead_time, l_process_lt), supplier_lead_time_date
11193                                 into     l_process_lt, g_sup_cap_cum_date
11194                                 from     msc_item_suppliers
11195                                 where    plan_id = p_plan_id
11196                                 and      sr_instance_id =  p_atp_record.instance_id
11197                                 and      organization_id = p_atp_record.organization_id
11198                                  --bug 3373598: Use model's inv id for config item.
11199                                 -- base_item_id will be populated only for ATO items
11200                                 and      inventory_item_id = NVL(C_ITEM_INFO_REC.base_item_id, l_inv_item_id)
11201                                 and      supplier_id =  l_sources.Supplier_Id(j)
11202                                 and      supplier_site_id = l_sources.Supplier_site_Id(j);
11203                              ELSE
11204                                 g_sup_cap_cum_date := null;
11205                              END IF;
11206 
11207                            Exception
11208                                WHEN others THEN
11209                                  g_sup_cap_cum_date := null;
11210                            END;
11211                            IF PG_DEBUG in ('Y', 'C') THEN
11212                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Id = ' || p_plan_id);
11213                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Instance Id = ' || p_atp_record.instance_id);
11214                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Organization Id = ' || p_atp_record.organization_id);
11215                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Item Id = ' || l_inv_item_id);
11216                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Id = ' || l_sources.Supplier_Id(j));
11217                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Site Id = ' || l_sources.Supplier_site_Id(j));
11218 
11219                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Process lead time after ASL query = '
11220                                                                                             || l_process_lt);
11221                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Lead Time Date = '
11222                                                                                        || g_sup_cap_cum_date);
11223                            END IF;
11224                            -- Bug 2803195
11225                            IF (g_sup_cap_cum_date IS NOT NULL) THEN
11226                                -- Get the next_working day based on plan owning org's calendar.
11227                                -- to account for MBP's -ve 1 day offset.
11228                                /* ship_rec_cal
11229                                g_sup_cap_cum_date :=  MSC_CALENDAR.next_work_day
11230                                                      (G_PLAN_INFO_REC.organization_id,
11231                                                       G_PLAN_INFO_REC.sr_instance_id,
11232                                                       1,
11233                                                       g_sup_cap_cum_date + 1);*/
11234                                g_sup_cap_cum_date :=  MSC_CALENDAR.next_work_day(
11235                                                       l_atp_rec.manufacturing_cal_code,
11236                                                       G_PLAN_INFO_REC.sr_instance_id,
11237                                                       g_sup_cap_cum_date + 1);
11238                                IF PG_DEBUG in ('Y', 'C') THEN
11239                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Lead Time Date Next Day offset = '
11240                                                                                        || g_sup_cap_cum_date);
11241                                END IF;
11242                            END IF;
11243                            -- End Bug 2803195
11244                            -- End Supplier Capacity and Lead Time changes
11245                            l_net_processing_lt := CEIL(l_process_lt + l_pre_pro_lt);
11246 
11247                            -- Supplier Capacity and Lead Time (SCLT) changes
11248                               -- l_atp_rec.atp_lead_time := l_net_processing_lt;
11249                            -- Only the processing lead time will be passed back into ATP_Check
11250                            l_atp_rec.atp_lead_time := l_process_lt;
11251                            -- End Supplier Capacity and Lead Time changes
11252                            IF PG_DEBUG in ('Y', 'C') THEN
11253                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
11254                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
11255                            END IF;
11256 
11257                             -- Bug 3782472 - calculate ptf date for lower level
11258                             l_ptf_date_for_child := MSC_CALENDAR.DATE_OFFSET (
11259                                                 p_atp_record.manufacturing_cal_code,
11260                                                 p_atp_record.instance_id,
11261                                                 l_parent_ptf_date,
11262                                                 -1 * l_post_pro_lt, -1);
11263 
11264                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child, l_ptf_date);
11265 
11266                             /* ship_rec_cal changes begin */
11267                             -- intransit lead time offset in case of capacity to ship
11268                             IF l_sources.sup_cap_type(j) = 1 THEN
11269                                     IF PG_DEBUG in ('Y', 'C') THEN
11270                                         msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
11271                                     END IF;
11272 
11273                                     -- Bug 3782472 - calculate ptf date for lower level
11274                                     l_ptf_date_for_child := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
11275         					l_ptf_date_for_child, l_atp_rec.receiving_cal_code, -1,
11276         					l_atp_rec.intransit_cal_code, -1 * l_atp_rec.delivery_lead_time, -1,
11277         					l_atp_rec.shipping_cal_code, -1, p_atp_record.instance_id);
11278 
11279                                     /* Enforce Pur LT - Check if date needs to be calculated */
11280                                     IF l_trunc_started = 1 THEN
11281                                         l_supplier_start_date := l_trunc_sysdate;
11282                                     ELSE
11283                                         l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
11284                                                         l_atp_rec.intransit_cal_code,
11285                                                         l_atp_rec.instance_id,
11286                                                         l_new_dock_date,
11287                                                         (-1 * l_atp_rec.delivery_lead_time), -1);
11288                                         IF PG_DEBUG in ('Y', 'C') THEN
11289                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_supplier_start_date);
11290                                         END IF;
11291                                         /* Enforce Pur LT - check if date is past due. for capacity to ship cases ship date
11292                                                 can be past due only if capacity is not defined. */
11293                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_atp_rec.last_cap_date IS NULL
11294                                             AND l_supplier_start_date < l_trunc_sysdate THEN
11295                                             l_supplier_start_date := l_trunc_sysdate;
11296                                             l_trunc_started := 1;
11297                                             IF PG_DEBUG in ('Y', 'C') THEN
11298                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11299                                             END IF;
11300                                         END IF;
11301                                     END IF;
11302 
11303 
11304                                     /* Enforce Pur LT - Check if date needs to be calculated */
11305                                     IF l_trunc_started = 2 THEN
11306                                         l_supplier_start_date := MSC_CALENDAR.PREV_WORK_DAY(
11307                                                 l_atp_rec.shipping_cal_code,
11308                                                 l_atp_rec.instance_id,
11309                                                 l_supplier_start_date);
11310                                         IF PG_DEBUG in ('Y', 'C') THEN
11311                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC : '||l_supplier_start_date);
11312                                         END IF;
11313                                         /* Enforce Pur LT - check if date is past due. for capacity to ship cases ship date
11314                                                 can be past due only if capacity is not defined. */
11315                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_atp_rec.last_cap_date IS NULL
11316                                             AND l_supplier_start_date < l_trunc_sysdate THEN
11317                                             l_supplier_start_date := l_trunc_sysdate;
11318                                             l_trunc_started := 1;
11319                                             IF PG_DEBUG in ('Y', 'C') THEN
11320                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11321                                             END IF;
11322                                         END IF;
11323                                     END IF;
11324 
11325                                    /* populating new_ship_date */
11326                                    l_new_ship_date := l_supplier_start_date;
11327 
11328                                    l_atp_rec.requested_ship_date := l_new_ship_date;
11329 
11330                                    IF PG_DEBUG in ('Y', 'C') THEN
11331                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship date :'||l_new_ship_date);
11332                                       msc_sch_wb.atp_debug('ATP_Check: ' || '---------------------------');
11333                                    END IF;
11334                             ELSE
11335                                     l_supplier_start_date := l_new_dock_date;
11336                                     -- supplier intransit LT
11337 
11338                                     l_atp_rec.requested_ship_date := l_new_dock_date;
11339 
11340                                     -- Bug 3782472 - calculate ptf date for lower level
11341                                     l_ptf_date_for_child := MSC_CALENDAR.PREV_WORK_DAY(
11342                                                 l_atp_rec.receiving_cal_code,
11343                                                 p_atp_record.instance_id,
11344                                                 l_ptf_date_for_child);
11345 
11346                                     IF PG_DEBUG in ('Y', 'C') THEN
11347                                         msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
11348                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier Start Date : '||l_supplier_start_date);
11349                                     END IF;
11350 
11351                                     /* populating new_ship_date in Capacity to Dock scenarios also */
11352                                     /* Enforce Pur LT - Check if date needs to be calculated */
11353                                     IF l_trunc_started = 1 THEN
11354                                         l_new_ship_date := l_trunc_sysdate;
11355                                     ELSE
11356                                         l_new_ship_date := MSC_CALENDAR.DATE_OFFSET(
11357                                                         l_atp_rec.intransit_cal_code,
11358                                                         l_atp_rec.instance_id,
11359                                                         l_start_date,
11360                                                         (-1 * l_atp_rec.delivery_lead_time), -1);
11361 
11362                                         IF PG_DEBUG in ('Y', 'C') THEN
11363                                             msc_sch_wb.atp_debug('ATP_Check: ' || '--- Calculate ship date ---');
11364                                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_new_ship_date);
11365                                         END IF;
11366 
11367                                         /* Enforce Pur LT - check if date is past due.*/
11368                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date < l_trunc_sysdate THEN
11369                                             l_new_ship_date := l_trunc_sysdate;
11370                                             -- Not setting the flag l_trunc_started as dock date may still be more than sysdate
11371                                             -- and in capacity to dock cases further calculation of dates (start date, order date)
11372                                             -- is done from dock date.
11373                                             IF PG_DEBUG in ('Y', 'C') THEN
11374                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11375                                             END IF;
11376                                         ELSE
11377                                             l_new_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
11378                                                         l_atp_rec.shipping_cal_code,
11379                                                         l_atp_rec.instance_id,
11380                                                         l_new_ship_date);
11381 
11382                                             IF PG_DEBUG in ('Y', 'C') THEN
11383                                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC :'||l_new_ship_date);
11384                                             END IF;
11385 
11386                                             /* Enforce Pur LT - check if date is past due.*/
11387                                             IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date < l_trunc_sysdate THEN
11388                                                 l_new_ship_date := l_trunc_sysdate;
11389                                                 IF PG_DEBUG in ('Y', 'C') THEN
11390                                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11391                                                 END IF;
11392                                             END IF;
11393                                         END IF;
11394 
11395                                     END IF;
11396 
11397                                    IF PG_DEBUG in ('Y', 'C') THEN
11398                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship date :'||l_new_ship_date);
11399                                       msc_sch_wb.atp_debug('ATP_Check: ' || '---------------------------');
11400                                    END IF;
11401 
11402                             END IF;
11403 
11404                             /* Enforce Pur LT - Check if date needs to be calculated */
11405                             IF l_trunc_started = 1 THEN
11406                                 l_supplier_start_date := l_trunc_sysdate;
11407                             ELSIF l_process_lt > 0 THEN
11408                                 l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
11409                                                                 l_atp_rec.manufacturing_cal_code,
11410                                                                 G_PLAN_INFO_REC.sr_instance_id,
11411                                                                 l_supplier_start_date,
11412                                                                 (-1 * l_process_lt), -1);
11413                                 IF PG_DEBUG in ('Y', 'C') THEN
11414                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after offsetting processing LT on SMC :'||l_supplier_start_date);
11415                                 END IF;
11416                                 /* Enforce Pur LT - check if date is past due.*/
11417                                 IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_supplier_start_date < l_trunc_sysdate THEN
11418                                     l_supplier_start_date := l_trunc_sysdate;
11419                                     l_trunc_started := 1;
11420                                     IF PG_DEBUG in ('Y', 'C') THEN
11421                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11422                                     END IF;
11423                                 END IF;
11424                             END IF;
11425 
11426                             l_sugg_start_date := l_supplier_start_date;          -- Bug 3241766
11427 
11428                             /* Enforce Pur LT - Check if date needs to be calculated */
11429                             IF l_trunc_started = 2 THEN
11430                                 l_supplier_start_date := MSC_CALENDAR.DATE_OFFSET(
11431                                                         p_atp_record.manufacturing_cal_code, -- use OMC
11432                                                         l_atp_rec.instance_id,
11433                                                         l_supplier_start_date,
11434                                                         (-1 * l_pre_pro_lt), -1);
11435                                 IF PG_DEBUG in ('Y', 'C') THEN
11436                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_supplier_start_date);
11437                                 END IF;
11438                                 /* Enforce Pur LT - check if date is past due.*/
11439                                 IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_supplier_start_date < l_trunc_sysdate THEN
11440                                     l_supplier_start_date := l_trunc_sysdate;
11441                                     l_trunc_started := 1;
11442                                     IF PG_DEBUG in ('Y', 'C') THEN
11443                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Past due date truncated to: '||l_trunc_sysdate);
11444                                     END IF;
11445                                 END IF;
11446                             END IF;
11447 
11448                             l_order_date := l_supplier_start_date;               -- Bug 3241766
11449 
11450                             -- Bug 3782472 - set ptf date for lower level
11451                             MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date_for_child, l_ptf_date);
11452 
11453                            IF PG_DEBUG in ('Y', 'C') THEN
11454                               msc_sch_wb.atp_debug('ATP_Check: ' || '____________________End Offset____________________');
11455                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
11456                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_supplier_start_date := ' || l_supplier_start_date);
11457                               msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child := ' || l_ptf_date_for_child);
11458                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
11459                            END IF;
11460                            /* ship_rec_cal changes end */
11461 
11462                            IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 or p_search = FORWARD_SCHEDULING) and
11463                               (l_start_date < l_ptf_date or
11464                                  l_supplier_start_date < l_trunc_sysdate) THEN -- Enforce Pur LT - change sysdate to avoid
11465                                  -- l_supplier_start_date < sysdate) THEN      -- extraneous call to sysdate
11466 
11467                               /* ship_rec_cal changes begin */
11468                               IF PG_DEBUG in ('Y', 'C') THEN
11469                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc PTF Due Date___________________');
11470                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date: '||l_ptf_date);
11471                               END IF;
11472 
11473                               IF l_start_date < l_ptf_date and l_post_pro_lt > 0 THEN
11474                                  l_ptf_due_date := MSC_CALENDAR.DATE_OFFSET(
11475                                                          p_atp_record.manufacturing_cal_code,
11476                                                          p_atp_record.instance_id,
11477                                                          l_ptf_date,
11478                                                          l_post_pro_lt, 1);
11479 
11480                                  IF PG_DEBUG in ('Y', 'C') THEN
11481                                      msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ptf_due_date);
11482                                  END IF;
11483                               ELSE
11484                                  --- bug 2771784: earliest due date should be PTF date. Not start date
11485                                  --l_ptf_due_date := l_start_date;
11486                                  -- ship_rec_cal i dont need to move l_ptf_date to next_work_day on mfg cal
11487                                  l_ptf_due_date :=  l_ptf_date;
11488                               END IF;
11489 
11490                               /* 3316028 (Enforce Pur LT) - PTF and sysdate validations were done here as well as after
11491                                         the call to ATP_Check for supplier. In forward pass doing all the calculations here
11492                                         itself and passing request ship date as the actual date on which capacity should
11493                                         be checked. l_supplier_request_date stores this date. */
11494                               /* Bug 3828494 (done with 3828469) - Don't change request date as that shows up in the pegging.
11495                                  This constraint will get enforced via the G_PTF_DATE which has already been set to a date
11496                                  greater than or equal to l_ptf_date
11497                               IF p_search = FORWARD_SCHEDULING THEN
11498                                   l_supplier_request_date := GREATEST(l_start_date, l_ptf_date);
11499                                   l_supplier_request_date := MSC_CALENDAR.PREV_WORK_DAY(
11500                                                             l_atp_rec.receiving_cal_code,
11501                                                             l_atp_rec.instance_id,
11502                                                             l_supplier_request_date);
11503 
11504                                   IF PG_DEBUG in ('Y', 'C') THEN
11505                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC : '||l_supplier_request_date);
11506                                   END IF;
11507 
11508                                   IF l_sources.sup_cap_type(j) = 1 THEN
11509                                       -- capacity to be checked on ship date
11510                                       l_supplier_request_date := MSC_CALENDAR.DATE_OFFSET(
11511                                                             l_atp_rec.intransit_cal_code,
11512                                                             l_atp_rec.instance_id,
11513                                                             l_supplier_request_date,
11514                                                             (-1 * l_atp_rec.delivery_lead_time), -1);
11515                                       IF PG_DEBUG in ('Y', 'C') THEN
11516                                           msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting intransit LT using VIC : '||l_supplier_request_date);
11517                                       END IF;
11518                                       l_supplier_request_date := MSC_CALENDAR.PREV_WORK_DAY(
11519                                                     l_atp_rec.shipping_cal_code,
11520                                                     l_atp_rec.instance_id,
11521                                                     l_supplier_request_date);
11522                                   END IF;
11523                                   -- IF PTF is more constraining then this would be the date at which capacity would be checked
11524                                   l_atp_rec.requested_ship_date := l_supplier_request_date;
11525                               END IF;
11526                               */
11527 
11528                               IF PG_DEBUG in ('Y', 'C') THEN
11529                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc PTF Due Date___________________');
11530                                   msc_sch_wb.atp_debug('ATP_Check: ' || ' l_ptf_due_date := ' || l_ptf_due_date);
11531                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate: '||l_trunc_sysdate);
11532                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Calc LT Due Date___________________');
11533                               END IF;
11534                               -- IF l_supplier_start_date < l_sysdate and (l_post_pro_lt + l_net_processing_lt) >0 THEN
11535                               IF l_supplier_start_date < l_trunc_sysdate
11536                                  and ((l_post_pro_lt + l_net_processing_lt)>0
11537                                       or (l_sources.sup_cap_type(j) = 1 and l_atp_rec.delivery_lead_time>0))THEN
11538                                    -- Enforce Pur LT - change sysdate to avoid extraneous call to sysdate
11539                                    -- Bug 3316028 - also included transit time for capacity to ship cases
11540 
11541                                    --Bug 3821358
11542                                    IF l_trunc_sysdate < l_sys_next_date THEN
11543                                       l_store_order_date  := l_sys_next_date - 1;
11544                                    ELSE
11545                                       l_store_order_date  := l_trunc_sysdate;
11546                                    END IF;
11547 
11548                                    l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
11549                                                                 p_atp_record.manufacturing_cal_code,
11550                                                                 l_atp_rec.instance_id,
11551                                                                 l_trunc_sysdate,
11552                                                                 l_pre_pro_lt, 1);
11553                                    --Bug 3821358
11554                                    l_store_start_date := l_lt_due_date;
11555 
11556                                    IF PG_DEBUG in ('Y', 'C') THEN
11557                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding Pre PLT using OMC: '||l_lt_due_date);
11558                                    END IF;
11559 
11560                                    IF l_process_lt > 0 THEN
11561                                            l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
11562                                                                         l_atp_rec.manufacturing_cal_code,
11563                                                                         G_PLAN_INFO_REC.sr_instance_id,
11564                                                                         l_lt_due_date,
11565                                                                         l_process_lt, 1);
11566                                            IF PG_DEBUG in ('Y', 'C') THEN
11567                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PLT using SMC: '||l_lt_due_date);
11568                                            END IF;
11569                                    END IF;
11570 
11571                                    -- intransit lead time offset in case of capacity to ship
11572                                    IF l_sources.sup_cap_type(j) = 1 THEN
11573                                            l_lt_due_date:= MSC_CALENDAR.NEXT_WORK_DAY(
11574                                                                 l_atp_rec.shipping_cal_code,
11575                                                                 p_atp_record.instance_id,
11576                                                                 l_lt_due_date);
11577                                            --Bug 3821358
11578                                            l_store_ship_date := l_lt_due_date;
11579 
11580                                            /* Bug 3828494 (done with 3828469)
11581                                            IF p_search = FORWARD_SCHEDULING THEN
11582                                                l_supplier_request_date := l_lt_due_date;
11583                                            END IF;
11584                                            */
11585                                            l_ptf_date_for_child := l_lt_due_date;       -- Bug 3828494 (done with 3828469)
11586 
11587                                            IF PG_DEBUG in ('Y', 'C') THEN
11588                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
11589                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_lt_due_date);
11590                                            END IF;
11591 
11592                                            l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
11593                                                                 l_atp_rec.intransit_cal_code,
11594                                                                 G_PLAN_INFO_REC.sr_instance_id,
11595                                                                 l_lt_due_date,
11596                                                                 l_atp_rec.delivery_lead_time, 1);
11597 
11598                                            l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
11599                                                                 l_atp_rec.receiving_cal_code,
11600                                                                 p_atp_record.instance_id,
11601                                                                 l_lt_due_date);
11602 
11603                                            --Bug 3821358
11604                                            l_store_dock_date := l_lt_due_date ;
11605 
11606                                            IF PG_DEBUG in ('Y', 'C') THEN
11607                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_lt_due_date);
11608                                            END IF;
11609 
11610                                    ELSE
11611                                            IF PG_DEBUG in ('Y', 'C') THEN
11612                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
11613                                            END IF;
11614 
11615                                            --Bug 3821358
11616                                            l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
11617                                                                l_atp_rec.receiving_cal_code,
11618                                                                p_atp_record.instance_id,
11619                                                                l_lt_due_date);
11620 
11621                                            l_store_dock_date :=  l_lt_due_date;
11622 
11623                                            l_store_ship_date := MSC_CALENDAR.DATE_OFFSET(
11624                                                                 l_atp_rec.intransit_cal_code,
11625                                                                 G_PLAN_INFO_REC.sr_instance_id,
11626                                                                 l_lt_due_date,
11627                                                                 (-1 * l_atp_rec.delivery_lead_time), -1);
11628 
11629                                            l_store_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
11630                                                                 l_atp_rec.shipping_cal_code,
11631                                                                 p_atp_record.instance_id,
11632                                                                 l_store_ship_date);
11633                                    END IF;
11634 
11635                                    /*l_lt_due_date := MSC_CALENDAR.NEXT_WORK_DAY(
11636                                                         l_atp_rec.receiving_cal_code,
11637                                                         p_atp_record.instance_id,
11638                                                         l_lt_due_date);*/
11639 
11640                                    /* Bug 3828494 (done with 3828469)
11641                                    IF p_search = FORWARD_SCHEDULING AND l_sources.sup_cap_type(j) = 2 THEN
11642                                        l_supplier_request_date := l_lt_due_date;
11643                                    END IF;
11644                                    */
11645                                    IF l_sources.sup_cap_type(j) = 2 THEN
11646                                        l_ptf_date_for_child := l_lt_due_date;   -- Bug 3828494 (done with 3828469)
11647                                    END IF;
11648 
11649                                    IF PG_DEBUG in ('Y', 'C') THEN
11650                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_lt_due_date);
11651                                    END IF;
11652 
11653                                    l_lt_due_date := MSC_CALENDAR.DATE_OFFSET(
11654                                                          p_atp_record.manufacturing_cal_code,
11655                                                          p_atp_record.instance_id,
11656                                                          l_lt_due_date,
11657                                                          l_post_pro_lt, 1);
11658 
11659                                    l_store_due_date :=  l_lt_due_date;
11660 
11661                                    IF PG_DEBUG in ('Y', 'C') THEN
11662                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_lt_due_date);
11663                                    END IF;
11664 
11665                                    --Bug 3821358
11666                                    l_store_g_ptf_date := l_ptf_date_for_child;
11667 
11668                               ELSE
11669                                  l_lt_due_date := l_supplier_start_date;
11670                                  /* Bug 3828494 (done with 3828469)
11671                                  IF p_search = FORWARD_SCHEDULING THEN
11672                                      l_supplier_request_date := GREATEST(l_supplier_start_date,l_trunc_sysdate);
11673                                  END IF;
11674                                  */
11675                                  l_ptf_date_for_child := GREATEST(l_supplier_start_date,l_trunc_sysdate);
11676                               END IF;
11677 
11678                               /* Bug 3828494 (done with 3828469) - Don't change request date as that shows up in the pegging.
11679                                  Instead pass this constraint as G_PTF_DATE
11680                               l_atp_rec.requested_ship_date := GREATEST(l_atp_rec.requested_ship_date,l_supplier_request_date);
11681                               */
11682 
11683                               MSC_ATP_PVT.G_PTF_DATE := GREATEST(MSC_ATP_PVT.G_PTF_DATE, l_ptf_date_for_child);
11684 
11685                               -- PTF for lower level = greatest(sysdate+prePLT, current org's PTF, parent org's PTF)
11686                               -- after validating on all applicable calendars.
11687 
11688                               IF PG_DEBUG in ('Y', 'C') THEN
11689                                   msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Calc LT Due Date___________________');
11690                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
11691                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date_for_child: '||l_ptf_date_for_child);
11692                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Setting ptf_date for lower level: '||MSC_ATP_PVT.G_PTF_DATE);
11693                               END IF;
11694                               /* ship_rec_cal changes end */
11695 
11696                               -- Bug 3525582 - Constraint message should be added only if constraint actually exists
11697                               IF l_lt_due_date > l_requested_ship_date OR
11698                                  l_ptf_due_date > l_requested_ship_date THEN
11699 
11700                                  -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
11701                                  IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
11702                                     --purchasing lead time constraint
11703                                     l_pegging_rec.constraint_type := 4;
11704                                     l_pegging_rec.constraint_date := l_lt_due_date;
11705                                     l_constraint_flag := 1;
11706 
11707                                  ELSE
11708                                     --PTF lead time constraint
11709                                     l_pegging_rec.constraint_type := 2;
11710                                     l_pegging_rec.constraint_date := l_ptf_due_date;
11711                                     l_constraint_flag := 1;
11712 
11713                                 END IF;
11714                              END IF;
11715                            END IF;
11716 
11717                         END IF;
11718 
11719                         IF PG_DEBUG in ('Y', 'C') THEN
11720                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
11721                            msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_date := ' || l_atp_rec.requested_ship_date);
11722                            msc_sch_wb.atp_debug('ATP_Check: ' || 'arrival_date := ' || l_atp_rec.requested_arrival_date);
11723                         END IF;
11724 
11725                         l_atp_rec.latest_acceptable_date := null;
11726                         l_atp_rec.freight_carrier := null;
11727                         -- 1665483
11728                         --- bug 1870820 This bug was occuring because following line was commented out
11729                         --- As a result demand class was not getting passed to -get_supplier_atp_info proc
11730                         --- the line was commented out for bug 1665483. Uncommenting the line after discussing
11731                         --- with christine. She has commented this line out so that demand class info is not
11732                         --- passed to the org where we are transferring the item from and demand class
11733                         --- for second org gets enforced
11734                         l_atp_rec.demand_class := p_atp_record.demand_class;
11735                         l_atp_rec.override_flag := null;
11736                         l_atp_rec.ship_date := null;
11737                         l_atp_rec.action := p_atp_record.action;
11738                         l_atp_rec.insert_flag := p_atp_record.insert_flag;
11739                         l_atp_rec.to_location_id :=
11740  				MSC_ATP_FUNC.get_location_id(p_atp_record.instance_id,
11741                                 p_atp_record.organization_id,
11742                                 null,
11743                                 null,
11744                                 null,
11745                                 null);
11746                         l_atp_rec.to_organization_id :=
11747                             p_atp_record.organization_id;
11748 			l_atp_rec.to_instance_id := p_atp_record.instance_id;
11749 
11750                         l_atp_rec.order_number := p_atp_record.order_number;
11751 
11752                         IF PG_DEBUG in ('Y', 'C') THEN
11753                            msc_sch_wb.atp_debug('ATP_Check: ' || 'ORDER_NUMBER is : ' || l_atp_rec.order_number);
11754                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Demand Source Type is : ' || l_atp_rec.demand_source_type);--cmro
11755                            msc_sch_wb.atp_debug('ATP_Check: ' || 'net_demand is : ' || l_net_demand);
11756                         END IF;
11757                         --- End Enhance CTO Phase 1 Req #16 and #17
11758 
11759 
11760                         -- insert a plan order for the l_net_demand
11761                          ---- Change the flag temprorily as new plan's summary flag might be
11762                          ---- different from last plan's summary flag
11763                          ----
11764                         MSC_ATP_PVT.G_SUMMARY_FLAG := l_old_summary_flag;
11765                         /* rajjain 02/19/2003 Bug 2788302 Begin
11766                          * set org to null for Supplier's case. */
11767                         IF NVL(l_atp_rec.supplier_id,-1) <> -1 THEN
11768                            msc_sch_wb.atp_debug('Set Org id null in Supplier case');
11769                            l_src_org_id := NULL;
11770                         ELSE
11771                            l_src_org_id := l_atp_rec.organization_id;
11772                         END IF;
11773                         -- rajjain 02/19/2003 Bug 2788302 End
11774 
11775                         /* Modularize Item and Org Info */
11776                         -- Changed call to remove embedded function call.
11777 
11778 			-- dsting only add planned_order if it's not a constraint node
11779 			l_transaction_id := NULL;
11780                         --bug 2792702: Remove this condition as we need to create planned order
11781                         --during forward scheduling
11782 			--IF l_pegging_rec.constraint_type IS NULL THEN
11783                         -- Begin ATP4drp Create Planned Arrivals for DRP plans
11784                         IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type,1) = 5) AND (l_src_org_id is NOT NULL) THEN
11785                              l_supply_rec.instance_id            := p_atp_record.instance_id;
11786                              l_supply_rec.plan_id                := p_plan_id;
11787                              l_supply_rec.inventory_item_id      :=
11788                                                   C_ITEM_INFO_REC.dest_inv_item_id;
11789                              l_supply_rec.organization_id        := p_atp_record.organization_id;
11790                              l_supply_rec.schedule_date          := l_planned_order_date;
11791                              l_supply_rec.order_quantity         := l_PO_qty;
11792                              l_supply_rec.demand_class           := p_atp_record.demand_class;
11793                              -- l_supply_rec.source_organization_id
11794                              -- l_supply_rec.source_sr_instance_id
11795                              -- Source fields will be updated as a part of
11796                              -- Update_Planned_Order process.
11797                              l_supply_rec.refresh_number         := p_refresh_number;
11798                              l_supply_rec.shipping_cal_code      := l_atp_rec.shipping_cal_code;
11799                              l_supply_rec.receiving_cal_code     := l_atp_rec.receiving_cal_code;
11800                              l_supply_rec.intransit_cal_code     := l_atp_rec.intransit_cal_code;
11801                              l_supply_rec.new_ship_date          := l_new_ship_date;
11802                              l_supply_rec.new_dock_date          := l_new_dock_date;
11803 
11804                              --BUG 4488336: Populate correct start date
11805                              --l_supply_rec.start_date             := l_new_dock_date;
11806                              l_supply_rec.start_date             := l_start_date;
11807                              l_supply_rec.order_date             := l_order_date;
11808                              l_supply_rec.ship_method            := l_atp_rec.ship_method;
11809 
11810                              --l_supply_rec.return_status
11811 
11812                              l_supply_rec.firm_planned_type := 2;
11813                              l_supply_rec.disposition_status_type := 1;
11814                              l_supply_rec.record_source := 2; -- ATP created
11815                              l_supply_rec.supply_type := 51; --- planned arrival
11816                              l_supply_rec.intransit_lead_time := l_atp_rec.delivery_lead_time; --4127630
11817 
11818                              MSC_ATP_DB_UTILS.ADD_Supplies(l_supply_rec);
11819                              -- Asssign the output to local variables.
11820                              l_transaction_id := l_supply_rec.transaction_id;
11821                              l_return_status  := l_supply_rec.return_status;
11822                              IF PG_DEBUG in ('Y', 'C') THEN
11823                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Supply Id l_transaction_id: ' || l_transaction_id);
11824                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Supply Type l_supply_rec.supply_type: '
11825                                                                                        || l_supply_rec.supply_type);
11826                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Return Status l_return_status: ' || l_return_status);
11827                              END IF;
11828                         ELSE -- Create Planned Order otherwise.
11829 
11830                              MSC_ATP_DB_UTILS.Add_Planned_Order(
11831                                     p_atp_record.instance_id,
11832                                     p_plan_id,
11833                                     PF_ITEM_INFO_REC.dest_inv_item_id,
11834                                     p_atp_record.organization_id,
11835                                     l_planned_order_date, -- For ship_rec_cal
11836                                     l_PO_qty, -- 2754446
11837                                     --l_net_demand,
11838                                     null, -- l_atp_rec.supplier_id,
11839                                     null, -- l_atp_rec.supplier_site_id,
11840                                     p_atp_record.demand_class,
11841                                     -- rajjain 02/19/2003 Bug 2788302 Begin
11842                                     -- Add Sourcing details.
11843                                     l_src_org_id,
11844                                     l_atp_rec.instance_id,
11845                                     null, --process seq id (transfer case)
11846                                     -- rajjain 02/19/2003 Bug 2788302 End
11847                                     p_refresh_number,    -- For summary enhancenment
11848                                     -- ship_rec_cal changes begin
11849                                     l_atp_rec.shipping_cal_code,
11850                                     l_atp_rec.receiving_cal_code,
11851                                     l_atp_rec.intransit_cal_code,
11852                                     l_new_ship_date,
11853                                     l_new_dock_date,
11854                                     l_sugg_start_date,  -- Bug 3241766
11855                                     l_order_date,       -- Bug 3241766
11856                                     l_atp_rec.ship_method,
11857                                     -- ship_rec_cal changes end
11858                                     l_transaction_id,
11859                                     l_return_status,
11860                                     -- time_phased_atp
11861                                     l_atp_rec.delivery_lead_time, --4127630
11862                                     C_ITEM_INFO_REC.dest_inv_item_id,
11863                                     p_atp_record.atf_date
11864                                 );
11865                         END IF;
11866                         -- End ATP4drp
11867 			--END IF;
11868 
11869                         -- populate insert rec to pegging tree for this supply
11870 
11871                         IF PG_DEBUG in ('Y', 'C') THEN
11872                            msc_sch_wb.atp_debug('ATP_Check: ' || 'here the the parent_pegging_id inside the buy/transfer :'||
11873 				l_pegging_id);
11874                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_parent_pegging_id := ' || l_parent_pegging_id);
11875                         END IF;
11876                         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
11877                         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
11878                         l_pegging_rec.parent_pegging_id:= l_parent_pegging_id;
11879                         l_pegging_rec.atp_level:= p_level+1;
11880 
11881 
11882                         IF l_sources.source_type(j) = TRANSFER or l_sources.source_type(j) = MAKE THEN --5222584
11883                           l_pegging_rec.organization_id :=
11884                               l_sources.organization_id(j);
11885                           /* Modularize Item and Org Info */
11886                           IF (G_ORG_INFO_REC.organization_id =
11887                                          l_pegging_rec.organization_id) AND
11888                              (G_ORG_INFO_REC.instance_id =
11889                                             p_atp_record.instance_id) THEN
11890                             -- If org has not changed then re-use.
11891                             l_pegging_rec.organization_code :=
11892                                                    G_ORG_INFO_REC.org_code;
11893                             ---we need this organization for pegging creation
11894                             -- Org id contains source org. We need to create atp simplified peg
11895                             --- in rec org. Therefore, we remember it
11896                           ELSE
11897                              /* Modularize Item and Org Info */
11898                              MSC_ATP_PROC.get_global_org_info(p_atp_record.instance_id,
11899                                                          l_pegging_rec.organization_id);
11900                              l_pegging_rec.organization_code := MSC_ATP_PVT.G_ORG_INFO_REC.org_code;
11901                              /* l_pegging_rec.organization_code :=
11902                                MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
11903                                            l_pegging_rec.organization_id);
11904                              Modularize Item and Org Info */
11905                           END IF;
11906                           /* Modularize Item and Org Info */
11907                           l_pegging_rec.supplier_id := NULL;
11908                           l_pegging_rec.supplier_name := NULL;
11909                           l_pegging_rec.supplier_site_id := NULL;
11910                           l_pegging_rec.supplier_site_name := NULL;
11911                           l_pegging_rec.receiving_organization_id :=  p_atp_record.organization_id;
11912                         ELSE
11913                           --- for CTO rearch: add this field so that its read for creating ATP pegging
11914                           --l_pegging_rec.organization_id := NULL;
11915                           l_pegging_rec.organization_id := p_atp_record.organization_id;
11916                           l_pegging_rec.organization_code := NULL;
11917 			  --- EFTC if l_buy_make_cd = 3 then it means that no sources are defined
11918                           --- Therefore source info is not available
11919 			  --- we set the supplier info to null
11920 			  IF (l_make_buy_cd = 3) THEN --- no sources defined
11921  				l_pegging_rec.supplier_id := NULL;
11922                           	l_pegging_rec.supplier_name := NULL;
11923                           	l_pegging_rec.supplier_site_id := NULL;
11924                           	l_pegging_rec.supplier_site_name := NULL;
11925 			  ELSE
11926                        		l_pegging_rec.supplier_id := l_sources.supplier_id(j);
11927                        		l_pegging_rec.supplier_name :=
11928                               		MSC_ATP_FUNC.get_supplier_name(p_atp_record.instance_id,
11929                                				               l_pegging_rec.supplier_id);
11930                                 l_pegging_rec.supplier_site_id :=
11931                               		l_sources.supplier_site_id(j);
11932                           	l_pegging_rec.supplier_site_name :=
11933                               		MSC_ATP_FUNC.get_supplier_site_name(p_atp_record.instance_id,
11934                                        			                    l_pegging_rec.supplier_site_id);
11935                     	  END IF;
11936                         END IF;
11937 
11938                         l_pegging_rec.identifier1:= p_atp_record.instance_id;
11939                         l_pegging_rec.identifier2:= p_plan_id;
11940                         l_pegging_rec.identifier3 := l_transaction_id;
11941 
11942                         -- time_phased_atp changes begin
11943                         IF l_pf_atp = 'Y' THEN
11944                            l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
11945                            l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
11946                         ELSE
11947                            l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
11948                            l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
11949                         END IF;
11950                         l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
11951                         -- time_phased_atp changes end
11952 
11953                         l_pegging_rec.resource_id := NULL;
11954                         l_pegging_rec.resource_code := NULL;
11955                         l_pegging_rec.department_id := NULL;
11956                         l_pegging_rec.department_code := NULL;
11957                         l_pegging_rec.scenario_id:= p_scenario_id;
11958                         l_pegging_rec.supply_demand_source_type:=
11959                             l_sources.source_type(j);
11960 
11961 
11962                         l_pegging_rec.supply_demand_type:= 2;
11963                         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
11964                         l_pegging_rec.source_type := l_sources.source_type(j);
11965 
11966                         -- for demo:1153192
11967                         l_pegging_rec.constraint_flag := 'N';
11968 			l_pegging_rec.component_identifier :=
11969 
11970                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
11971                         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
11972 
11973                         --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
11974                         --- cto looks at pegging tree to place their demands. Since CTO expects to find
11975                         --  id for the requested item, we add the following column. CTO will now read from this column
11976                         l_pegging_rec.request_item_id := p_atp_record.request_item_id;
11977 
11978                         IF l_start_date < l_ptf_date then
11979                             IF PG_DEBUG in ('Y', 'C') THEN
11980                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Add PTF date to pegging');
11981                             END IF;
11982                             l_pegging_rec.ptf_date := l_ptf_date;
11983                         END IF;
11984 
11985                         --diag_atp:
11986                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and p_search = BACKWARD_SCHEDULING
11987                            AND l_constraint_flag = 1 THEN
11988                            l_pegging_rec.supply_demand_quantity := 0;
11989                         ELSE
11990                            l_pegging_rec.supply_demand_quantity :=l_atp_rec.quantity_ordered;
11991                         END IF;
11992                         l_pegging_rec.ptf_date := l_ptf_date;
11993                         IF l_sources.source_type(j) = TRANSFER THEN
11994                             l_pegging_rec.intransit_lead_time := l_sources.Lead_Time(j);
11995                             l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
11996                             l_pegging_rec.pegging_type := MSC_ATP_PVT.TRANSFER_SUPPLY;
11997                             l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
11998                             -- Bug 3826234
11999                             l_pegging_rec.shipping_cal_code      :=  l_atp_rec.shipping_cal_code;
12000                             l_pegging_rec.receiving_cal_code     :=  l_atp_rec.receiving_cal_code;
12001                             l_pegging_rec.intransit_cal_code     :=  l_atp_rec.intransit_cal_code;
12002                             l_pegging_rec.manufacturing_cal_code :=  NULL;
12003 
12004                         ELSE
12005                             l_pegging_rec.intransit_lead_time := l_atp_rec.delivery_lead_time; --supplier intransit LT
12006                             l_pegging_rec.postprocessing_lead_time := l_post_pro_lt;
12007                             l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
12008                             l_pegging_rec.processing_lead_time := l_process_lt;
12009                             l_pegging_rec.pegging_type := MSC_ATP_PVT.BUY_SUPPLY;
12010                              -- Bug 3826234
12011                             l_pegging_rec.shipping_cal_code      :=  l_atp_rec.shipping_cal_code;
12012                             l_pegging_rec.receiving_cal_code     :=  l_atp_rec.receiving_cal_code;
12013                             l_pegging_rec.intransit_cal_code     :=  l_atp_rec.intransit_cal_code;
12014                             l_pegging_rec.manufacturing_cal_code :=  l_atp_rec.manufacturing_cal_code;
12015                         END IF;
12016 
12017                         /* ship_rec_cal
12018                            populate ship_method */
12019                         IF  l_atp_rec.ship_method <> '@@@' THEN
12020                            -- if no ship method is defined then ATP_sources retuns @@@ which
12021                            --we don't want to display
12022                            l_pegging_rec.ship_method := l_atp_rec.ship_method;
12023                         ELSE
12024                            l_pegging_rec.ship_method := null;
12025                         END IF;
12026 
12027                         l_pegging_rec.plan_name := p_atp_record.plan_name;
12028                         l_pegging_rec.required_quantity := l_net_demand;
12029                         l_pegging_rec.required_date := l_requested_ship_date;
12030                         --bug 3328421
12031                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
12032 
12033 			-- dsting diag_atp
12034 			IF l_sources.source_type(j) = MSC_ATP_PVT.BUY
12035 			   AND l_pegging_rec.constraint_type IS NULL
12036 			THEN
12037 				l_last_good_pegging_id := l_pegging_id;
12038 				IF PG_DEBUG in ('Y', 'C') THEN
12039 				   msc_sch_wb.atp_debug('buy last good pegging: ' || l_pegging_id);
12040 				END IF;
12041 			END IF;
12042 
12043                         --s_cto_rearch
12044                             --parent is model entity
12045                         --bug 3373075: Mark plan order to be model entity only for
12046                         -- config, model or option class
12047                         /* IF (p_atp_record.parent_bom_item_type in (1, 4) and
12048                                                   p_atp_record.parent_repl_order_flag = 'Y')
12049                            --item itself is model entity
12050                            OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and
12051                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
12052                         */
12053                         IF (C_ITEM_INFO_REC.bom_item_type in (1, 2, 4) and
12054                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
12055                             l_pegging_rec.model_sd_flag := 1;
12056                         END IF;
12057 
12058                         l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
12059 
12060                         --bug 3356779: populate atp rule and ITF on PO
12061                         IF l_atp_flag = 'Y' THEN
12062                            l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
12063                            l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
12064                         ELSE
12065                            --bug 4394904
12066                            --l_pegging_rec.infinite_time_fence := l_atp_date_this_level;
12067                            l_pegging_rec.infinite_time_fence := l_infinite_time_fence;
12068                            l_pegging_rec.atp_rule_name := l_atp_rule_name;
12069                         END IF;
12070                         --e_Cto_rearch
12071 
12072 			-- dsting diag_atp
12073 			IF PG_DEBUG in ('Y', 'C') THEN
12074 				msc_sch_wb.atp_debug('MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT ' || MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT);
12075 			END IF;
12076                         -- dsting 2819716. we always add planned orders now
12077                         -- so we should always add pegging.
12078 /*
12079 			IF l_constraint_flag <> 1
12080 			   OR NVL(MSC_ATP_PVT.G_SUBSTITUTION_FLAG, 'N') <> 'Y'
12081 			   OR MSC_ATP_PVT.G_HAVE_MAKE_BUY_PARENT = 1
12082 			THEN
12083 */                              l_pegging_rec.demand_class :=  p_atp_record.demand_class;
12084 
12085                                 --4570421
12086                                 l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
12087                                 l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
12088                                 l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
12089                                 l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
12090                                 l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
12091                                 l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
12092                                 l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
12093 
12094 	                        MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
12095 --			END IF;
12096 
12097                         --- now chnage back the summary flag for new plan
12098                         MSC_ATP_PVT.G_SUMMARY_FLAG := l_new_summary_flag;
12099                         -- 2178544:
12100                         MSC_ATP_PVT.G_PTF_DATE := GREATEST(l_ptf_date, MSC_ATP_PVT.G_PTF_DATE);
12101                         IF PG_DEBUG in ('Y', 'C') THEN
12102                            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
12103                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_date := ' || l_ptf_date);
12104                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
12105                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside buy/transfer');
12106                         END IF;
12107 
12108                         ---subst: Since we go down so we do not remain in top org. So we set the flags again
12109                         l_atp_rec.original_item_flag := p_atp_record.original_item_flag;
12110                         l_atp_rec.top_tier_org_flag := 2;
12111 
12112                         IF p_search = BACKWARD_SCHEDULING and
12113                            --diag_atp
12114                            /* Enforce Pur LT - For BUY cases PTF date check needs to be done only if capacity is defined
12115                            (l_start_date < l_ptf_date or
12116                                       (l_sources.source_type(j) = TRANSFER AND
12117                                             l_atp_rec.requested_ship_date < sysdate) OR
12118                                       (l_sources.source_type(j) = BUY AND
12119                                             l_supplier_start_date < sysdate))  THEN
12120                            */
12121                            ((l_sources.source_type(j) = TRANSFER AND
12122                                 -- (l_atp_rec.requested_ship_date < sysdate OR l_start_date < l_ptf_date)) OR
12123                                 -- Bug 3412143 - use l_trunc_sysdate instead of accessing sysdate directly.
12124                                 (l_atp_rec.requested_ship_date < l_trunc_sysdate OR l_start_date < l_ptf_date)) OR
12125                             (l_sources.source_type(j) = BUY AND
12126                                 (l_supplier_start_date < l_trunc_sysdate OR
12127                                         (l_atp_rec.last_cap_date IS NOT NULL AND l_start_date < l_ptf_date))))  THEN
12128 
12129                            l_atp_rec.requested_date_quantity := 0;
12130                            -- dsting 2754446
12131                            l_prim_uom_req_date_qty := 0;
12132 
12133                            IF PG_DEBUG in ('Y', 'C') THEN
12134                               msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF Date check failed in case of transfer');
12135                            END IF;
12136                         ELSE
12137 			   l_atp_rec.children_type := NO_MAKE_BUY_CHILDREN;
12138                            IF PG_DEBUG in ('Y', 'C') THEN
12139                               msc_sch_wb.atp_debug('NO_MAKE_BUY_CHILDREN: pre atp_check');
12140                            END IF;
12141 
12142                            MSC_ATP_PVT.ATP_Check(l_atp_rec,
12143                                   l_plan_id,
12144                                   p_level + 2,
12145                                   p_scenario_id,
12146                                   p_search,
12147                                   p_refresh_number,
12148                                   l_pegging_id,
12149 			          l_assign_set_id,
12150                                   l_tb_atp_period,
12151                                   l_tb_atp_supply_demand,
12152                                   l_return_status);
12153                                   -- krajan: 2400614
12154 
12155 
12156                            IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
12157 
12158                                RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
12159 
12160                            ELSIF l_return_status = MSC_ATP_PVT.G_ATO_SRC_MISMATCH THEN
12161                                  IF PG_DEBUG in ('Y', 'C') THEN
12162                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12163                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12164                                  END IF;
12165                                  RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
12166                               -- dsting 2764213
12167                            ELSIF l_return_status = MSC_ATP_PVT.G_NO_PLAN_FOUND THEN
12168                                     IF PG_DEBUG in ('Y', 'C') THEN
12169                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12170                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12171                                     END IF;
12172                                     RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
12173                               -- krajan 2752705
12174                            ELSIF l_return_status = MSC_ATP_PVT.G_ATO_UNCOLL_ITEM THEN
12175                                     IF PG_DEBUG in ('Y', 'C') THEN
12176                                        msc_sch_wb.atp_debug('Error in ATP_CHECK');
12177                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Error in lower level component check');
12178                                     END IF;
12179                                     RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
12180                            END IF;
12181 
12182                            -- Bug 3633591 - Reset G_ITEM_INFO_REC as it may have changed.
12183                            G_ITEM_INFO_REC := C_ITEM_INFO_REC;
12184 
12185                            -- dsting 2754446
12186                            l_prim_uom_avail_qty := nvl(l_atp_rec.available_quantity, 0);
12187                            l_prim_uom_req_date_qty := nvl(l_atp_rec.requested_date_quantity, 0);
12188                            IF nvl(l_conversion_rate, 0) <> 0 THEN
12189                               l_prim_uom_avail_qty := l_prim_uom_avail_qty / l_conversion_rate;
12190                               l_prim_uom_req_date_qty := l_prim_uom_req_date_qty / l_conversion_rate;
12191                            END IF;
12192 
12193                            -- round to avoid any floating point errors.
12194                            IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
12195 --                              l_atp_rec.available_quantity := FLOOR(l_atp_rec.available_quantity);
12196                               l_prim_uom_avail_qty := FLOOR(l_prim_uom_avail_qty);   --5598066
12197                               l_prim_uom_req_date_qty := FLOOR(l_prim_uom_req_date_qty); --5598066
12198 			   ELSE								--5598066
12199                               l_prim_uom_avail_qty := trunc(l_prim_uom_avail_qty, 6);   --5598066
12200                               l_prim_uom_req_date_qty := trunc(l_prim_uom_req_date_qty, 6); --5598066
12201                            END IF;
12202 
12203   			   IF PG_DEBUG IN ('Y','C') THEN
12204                                 msc_sch_wb.atp_debug('prim_uom_avail_qty: ' || l_prim_uom_avail_qty);
12205                                 msc_sch_wb.atp_debug('prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
12206                                 msc_sch_wb.atp_debug('l_conversion_rate: ' || l_conversion_rate);
12207 				msc_sch_wb.atp_debug('resetting make_buy_parent : ' || G_HAVE_MAKE_BUY_PARENT || ' -> ' || l_have_make_buy_parent);
12208                                 msc_sch_wb.atp_debug('req date qty := ' || l_atp_rec.requested_date_quantity);
12209 			   END IF;
12210 
12211 			   G_HAVE_MAKE_BUY_PARENT := l_have_make_buy_parent;
12212 			   IF (l_atp_rec.children_type = MAKE_BUY_CHILDREN ) THEN
12213 				p_atp_record.children_type := l_atp_rec.children_type;
12214 			   END IF;
12215                         END IF;
12216                         -- check the return status
12217                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12218                             RAISE FND_API.G_EXC_ERROR;
12219                         END IF;
12220 
12221                         IF p_search = BACKWARD_SCHEDULING THEN
12222                           -- rajjain 01/29/03 Bug 2774827
12223                           -- 2754446
12224                           l_net_demand := l_net_demand -
12225                               greatest(nvl(l_prim_uom_req_date_qty, 0), 0);
12226                             -- greatest(nvl(l_atp_rec.requested_date_quantity,0), 0);
12227                         END IF;
12228                         IF PG_DEBUG in ('Y', 'C') THEN
12229                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Are we here, p_search ='||p_search);
12230                            msc_sch_wb.atp_debug('ATP_Check: l_net_demand := ' || l_net_demand);
12231                         END IF;
12232                         IF (p_search = FORWARD_SCHEDULING) THEN
12233 
12234                             -- bug 1303196, ship date need to consider
12235                             -- workday if it is from supplier
12236 
12237                             IF l_sources.source_type(j) = TRANSFER THEN
12238 
12239                                 -- 2463556 : krajan : Added comment
12240                                 IF PG_DEBUG in ('Y', 'C') THEN
12241                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Forward Scheduling ');
12242                                 END IF;
12243 
12244           			/* ship_rec_cal changes begin */
12245 		                -- BEGIN -- removed exception handling from here so that it can be handled in the end
12246 		                        /* Bug 3241766 : Broke the single call to get intermediate dates
12247                                         l_ship_date := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
12248                                 			l_atp_rec.ship_date, l_atp_rec.shipping_cal_code, +1,
12249                                 			l_atp_rec.intransit_cal_code, nvl(l_atp_rec.delivery_lead_time, 0), +1,
12250                                 			l_atp_rec.receiving_cal_code, +1, p_atp_record.instance_id);
12251                                 	*/
12252                                 	l_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
12253                                 	                l_atp_rec.shipping_cal_code,
12254                                 	                p_atp_record.instance_id,
12255                                 	                l_atp_rec.ship_date);
12256                                 	l_new_ship_date := l_ship_date;
12257 
12258                                         IF PG_DEBUG in ('Y', 'C') THEN
12259                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date: ' || l_atp_rec.ship_date);
12260                                            msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
12261                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on OSC: '||l_ship_date);
12262                                         END IF;
12263 
12264                                         l_ship_date := MSC_CALENDAR.DATE_OFFSET(
12265                         				l_atp_rec.intransit_cal_code,
12266                         				p_atp_record.instance_id,
12267                         				l_ship_date,
12268                         				nvl(l_atp_rec.delivery_lead_time, 0),
12269                         				+1);
12270 
12271                                         IF PG_DEBUG in ('Y', 'C') THEN
12272                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_ship_date);
12273                                         END IF;
12274 
12275                                         l_ship_date := MSC_CALENDAR.NEXT_WORK_DAY(
12276                         				l_atp_rec.receiving_cal_code,
12277                         				p_atp_record.instance_id,
12278                         				l_ship_date);
12279                                         l_new_dock_date := l_ship_date;
12280 
12281                                         IF PG_DEBUG in ('Y', 'C') THEN
12282                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_ship_date);
12283                                         END IF;
12284 
12285                                         -- Bug 3241766 : Back calculate start date and order date
12286                                         l_sugg_start_date := l_new_ship_date;
12287 
12288                                         l_order_date := MSC_CALENDAR.DATE_OFFSET (
12289                                                         p_atp_record.manufacturing_cal_code,
12290                                                         p_atp_record.instance_id,
12291                                                         l_sugg_start_date,
12292                                                         -- Bug 3633591 - Dont use global variable here : -1 * MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt, -1);
12293                                                         -1 * l_pre_pro_lt, -1);
12294                                         --bug3821358  Moving Order date to non past due non working date on a day closer to the first working day
12295                                         IF l_order_date < l_trunc_sysdate THEN
12296                                            IF PG_DEBUG in ('Y', 'C') THEN
12297                                               msc_sch_wb.atp_debug('ATP_Check: ' || 'Back Calculated Order date is less then sysdate: '||l_order_date);
12298                                               --bug 4188385 removed this debug was giving error during concatenation
12299                                               --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.
12300                                            END IF;
12301                                            l_order_date  := l_sys_next_date - 1;
12302                                            --bug 4188385 printed the order date
12303                                            IF PG_DEBUG in ('Y', 'C') THEN
12304                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Back Calculated Order date is less then sysdate: '||l_order_date);
12305                                            END IF;
12306                                         END IF;
12307                                         --bug3821358
12308                                         IF PG_DEBUG in ('Y', 'C') THEN
12309                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
12310                                         END IF;
12311 
12312                                         l_ship_date := MSC_CALENDAR.DATE_OFFSET (
12313                                                         p_atp_record.manufacturing_cal_code,
12314                                                         p_atp_record.instance_id,
12315                                                         l_ship_date,
12316                                                         l_post_pro_lt, 1);
12317 
12318                                         IF PG_DEBUG in ('Y', 'C') THEN
12319                                            msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ship_date);
12320                                            msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Offset___________________');
12321                                         END IF;
12322           			/*
12323           			EXCEPTION
12324                                         WHEN others THEN
12325                                                 IF PG_DEBUG in ('Y', 'C') THEN
12326                                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sysdate not found in Calendar');
12327                                                 END IF;
12328                                         null;
12329           			END;
12330           			*/
12331 
12332                             ELSE
12333                                 -- buy
12334                                 /* ship_rec_cal changes begin */
12335 
12336                                 IF PG_DEBUG in ('Y', 'C') THEN
12337                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Ship Date: ' || l_atp_rec.ship_date);
12338                                    msc_sch_wb.atp_debug('ATP_Check: ' || '___________________Start Offset___________________');
12339                                 END IF;
12340                                 -- intransit lead time offset in case of capacity to ship
12341                                 --Bug 3821358
12342                                 IF (l_atp_rec.ship_date = l_store_g_ptf_date )  THEN
12343                                    IF PG_DEBUG in ('Y', 'C') THEN
12344                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_rec.ship_date' || l_atp_rec.ship_date);
12345                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_g_ptf_date' || l_store_g_ptf_date);
12346                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_dock_date' || l_store_dock_date);
12347                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_ship_date' || l_store_ship_date);
12348                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_start_date' || l_store_start_date);
12349                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_order_date' || l_store_order_date);
12350                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_store_due_date' || l_store_due_date);
12351                                    END IF;
12352                                    -- assign the values in l_store* variables..
12353                                    l_ship_date := l_store_due_date;
12354                                    l_new_dock_date := l_store_dock_date;
12355                                    l_new_ship_date := l_store_ship_date;
12356                                    l_sugg_start_date := l_store_start_date;
12357                                    l_order_date := l_store_order_date;
12358 
12359                                 ELSE
12360 
12361                                    IF l_sources.sup_cap_type(j) = 1 THEN
12362 
12363                                            l_ship_date:= MSC_CALENDAR.NEXT_WORK_DAY(
12364                                                                 l_atp_rec.shipping_cal_code,
12365                                                                 l_atp_rec.instance_id,
12366                                                                 l_atp_rec.ship_date);
12367 
12368                                            IF PG_DEBUG in ('Y', 'C') THEN
12369                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Ship ---');
12370                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_ship_date);
12371                                            END IF;
12372 
12373                                            l_new_ship_date := l_ship_date;      -- Bug 3241766
12374 
12375                                            l_ship_date := MSC_CALENDAR.DATE_OFFSET(
12376                                                                 l_atp_rec.intransit_cal_code,
12377                                                                 l_atp_rec.instance_id,
12378                                                                 l_ship_date,
12379                                                                 l_atp_rec.delivery_lead_time, 1);
12380 
12381                                            IF PG_DEBUG in ('Y', 'C') THEN
12382                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_ship_date);
12383                                            END IF;
12384                                    ELSE
12385 
12386                                            l_ship_date := l_atp_rec.ship_date;
12387 
12388                                            IF PG_DEBUG in ('Y', 'C') THEN
12389                                                msc_sch_wb.atp_debug('ATP_Check: ' || '--- Capacity to Dock ---');
12390                                            END IF;
12391 
12392                                            -- Bug 3241766 : back calculate the ship date
12393                                            l_new_ship_date := MSC_CALENDAR.DATE_OFFSET(
12394                                                                 l_atp_rec.intransit_cal_code,
12395                                                                 l_atp_rec.instance_id,
12396                                                                 l_ship_date,
12397                                                                 -1 * l_atp_rec.delivery_lead_time, -1);
12398 
12399                                            IF PG_DEBUG in ('Y', 'C') THEN
12400                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding intransit LT using VIC: '||l_new_ship_date);
12401                                            END IF;
12402 
12403                                            l_new_ship_date := MSC_CALENDAR.PREV_WORK_DAY(
12404                                                                 l_atp_rec.shipping_cal_code,
12405                                                                 l_atp_rec.instance_id,
12406                                                                 l_new_ship_date);
12407                                                                 --Bug 3821358
12408                                                                 --l_atp_rec.ship_date);
12409 
12410                                            IF PG_DEBUG in ('Y', 'C') THEN
12411                                                msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on SSC: '||l_new_ship_date);
12412                                            END IF;
12413                                    END IF;
12414 
12415                                    l_ship_date  := MSC_CALENDAR.NEXT_WORK_DAY(
12416                                                         l_atp_rec.receiving_cal_code,
12417                                                         l_atp_rec.instance_id,
12418                                                         l_ship_date );
12419 
12420                                    l_new_dock_date := l_ship_date;      -- Bug 3241766
12421 
12422                                    IF PG_DEBUG in ('Y', 'C') THEN
12423                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after validating on ORC: '||l_ship_date);
12424                                    END IF;
12425 
12426                                    -- Bug 3241766 : Backcalculate start date and order date
12427                                    -- Bug 3405708 - Enforce Pur LT: Truncate past due dates
12428                                    IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_new_ship_date <= l_trunc_sysdate THEN
12429                                         l_sugg_start_date := l_trunc_sysdate;
12430                                    ELSE
12431                                         IF (l_sources.sup_cap_type(j) = 1) THEN --Bug 3821358
12432                                            l_sugg_start_date := MSC_CALENDAR.DATE_OFFSET(
12433                                                                 l_atp_rec.manufacturing_cal_code,
12434                                                                 l_atp_rec.instance_id,
12435                                                                 l_new_ship_date,
12436                                                                 (-1 * l_process_lt), -1);
12437                                         ELSE
12438                                            l_sugg_start_date := MSC_CALENDAR.DATE_OFFSET(
12439                                                                 l_atp_rec.manufacturing_cal_code,
12440                                                                 l_atp_rec.instance_id,
12441                                                                 l_new_dock_date,
12442                                                                 (-1 * l_process_lt), -1);
12443                                         END IF; --Bug 3821358
12444                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_sugg_start_date <= l_trunc_sysdate THEN
12445                                             l_sugg_start_date := l_trunc_sysdate;
12446                                         END IF;
12447                                    END IF;
12448 
12449                                    IF PG_DEBUG in ('Y', 'C') THEN
12450                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting PLT using SMC : '||l_sugg_start_date);
12451                                    END IF;
12452 
12453                                    IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_sugg_start_date <= l_trunc_sysdate THEN
12454                                         l_order_date := l_trunc_sysdate;
12455                                    ELSE
12456                                         l_order_date := MSC_CALENDAR.DATE_OFFSET(
12457                                                         p_atp_record.manufacturing_cal_code, -- use OMC
12458                                                         l_atp_rec.instance_id,
12459                                                         l_sugg_start_date ,
12460                                                         (-1 * l_pre_pro_lt), -1);
12461                                         IF G_PLAN_INFO_REC.enforce_pur_lead_time = 2 AND l_order_date <= l_trunc_sysdate THEN
12462                                             l_order_date := l_trunc_sysdate;
12463                                         END IF;
12464                                    END IF;
12465                                    -- Bug 3405708 - changes end
12466 
12467                                    IF PG_DEBUG in ('Y', 'C') THEN
12468                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after subtracting pre PLT using OMC : '||l_order_date);
12469                                    END IF;
12470 
12471                                    l_ship_date := MSC_CALENDAR.DATE_OFFSET(
12472                                                          p_atp_record.manufacturing_cal_code,
12473                                                          l_atp_rec.instance_id,
12474                                                          l_ship_date,
12475                                                          l_post_pro_lt, 1);
12476 
12477                                    IF PG_DEBUG in ('Y', 'C') THEN
12478                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Date after adding PPLT using OMC: '||l_ship_date);
12479                                        msc_sch_wb.atp_debug('ATP_Check: ' || '___________________End Offset___________________');
12480                                    END IF;
12481                                    -- ship_rec_cal changes end
12482                                 END IF; --Bug 3821358
12483                             END IF;
12484                             IF PG_DEBUG in ('Y', 'C') THEN
12485                                msc_sch_wb.atp_debug ('ATP_Check: ' || 'ship date is '||l_ship_date);
12486                                msc_sch_wb.atp_debug ('ATP_Check: ' || 'original is '|| p_atp_record.ship_date );
12487                             END IF;
12488 
12489                              ---bug 2798667: The p_atp_record.ship_date for option item contains ship date for
12490                             --- model from earlier source + atp lead time. We should compare with ship date +
12491                             --- atp lead time from this source and not just ship date
12492                             /* s_cto_rearch: Do not honot ATP lead time
12493                             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
12494                                   l_atp_lt_offset_ship_date :=
12495                                      MSC_CALENDAR.DATE_OFFSET(
12496                                      p_atp_record.organization_id,
12497                                      p_atp_record.instance_id,
12498                                      1,
12499                                      l_ship_date,
12500                                      NVL(p_atp_record.atp_lead_time, 0));
12501                             ELSE
12502                                  l_atp_lt_offset_ship_date := l_ship_date;
12503                             END IF;
12504                             */
12505                             l_atp_lt_offset_ship_date := l_ship_date;
12506 
12507                             IF PG_DEBUG in ('Y', 'C') THEN
12508                                msc_sch_wb.atp_debug ('l_atp_lt_offset_ship_date := ' || l_atp_lt_offset_ship_date);
12509                             END IF;
12510 
12511 
12512 			    -- Bug 1566260, If p_atp_record.ship_date and l_ship_date is null, we assume
12513 			    -- that going down is better. This is being done for preserving exploded
12514                             -- pegging tree for use by CTO for storing component demands.
12515                             IF PG_DEBUG in ('Y', 'C') THEN
12516                                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date := ' || p_atp_record.ship_date);
12517                             END IF;
12518                             --bug 2798667: Compare with l_atp_lt_offset_ship_date
12519                             /*IF ((p_atp_record.ship_date IS NULL and l_ship_date IS NULL) OR
12520                                (p_atp_record.ship_date IS NOT NULL and (p_atp_record.ship_date <=
12521                                NVL(l_ship_date, p_atp_record.ship_date )))) THEN
12522                             */
12523                             IF ((p_atp_record.ship_date IS NULL and l_atp_lt_offset_ship_date IS NULL) OR
12524                                (p_atp_record.ship_date IS NOT NULL and (p_atp_record.ship_date <=
12525                                NVL(l_atp_lt_offset_ship_date, p_atp_record.ship_date )))) THEN
12526 
12527 
12528                                 -- single level result is better than
12529                                 -- going down to next level.
12530 
12531                                 IF PG_DEBUG in ('Y', 'C') THEN
12532                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'single level results is better than multi-level');
12533                                 END IF;
12534 
12535 				-- dsting ATO 2465370
12536 --				IF NVL(p_atp_record.override_flag, 'N') = 'N'
12537 			        IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
12538 				   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
12539 				   --AND p_parent_pegging_id is not null)
12540 				THEN
12541                                         --bug 3328421: Update actual_supply_demand_date column
12542 					--update mrp_atp_details_temp set required_date =
12543 					update mrp_atp_details_temp set actual_supply_demand_date =
12544 						TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY
12545 					 where session_id = MSC_ATP_PVT.G_SESSION_ID
12546 					   and pegging_id = l_parent_pegging_id
12547 					   and record_type = 3;
12548 
12549 					IF PG_DEBUG in ('Y', 'C') THEN
12550 						msc_sch_wb.atp_debug('ATO update details_temp date single lev: ' || p_atp_record.ship_date);
12551 						msc_sch_wb.atp_debug('pegging id: ' || l_parent_pegging_id);
12552 					END IF;
12553 				END IF;
12554 
12555                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
12556                                                  p_plan_id,
12557                                                  p_atp_record.ship_date,
12558                                                  0,
12559                                                  l_atp_rec.supplier_id,
12560                                                  l_atp_rec.supplier_site_id,
12561                                                  null,
12562                                                  null,  -- Bug 3241766
12563                                                  null,  -- Bug 3241766
12564                                                  null,  -- Bug 3241766
12565                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
12566                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
12567 
12568 				IF PG_DEBUG in ('Y', 'C') THEN
12569 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'after copying the data');
12570                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'x_atp_supply_demand.level.count = '||
12571 						x_atp_supply_demand.level.count);
12572                                 END IF;
12573 
12574                             ELSE
12575  				-- going down is better than the results
12576                                 -- in this level
12577                                 IF PG_DEBUG in ('Y', 'C') THEN
12578                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'go down is better');
12579                                 END IF;
12580 
12581 				-- dsting ATO 2465370
12582 --				IF NVL(p_atp_record.override_flag, 'N') = 'N'
12583 			        IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
12584 				   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
12585 				   --AND p_parent_pegging_id is not null)
12586 				THEN
12587                                         --bug 3328421: update actual suply demand date
12588 					--update mrp_atp_details_temp set required_date =
12589 					update mrp_atp_details_temp set actual_supply_demand_date =
12590 						TRUNC(l_ship_date) + MSC_ATP_PVT.G_END_OF_DAY
12591 					where session_id = MSC_ATP_PVT.G_SESSION_ID
12592 					   and pegging_id = l_parent_pegging_id
12593 					   and record_type = 3;
12594 
12595 					IF PG_DEBUG in ('Y', 'C') THEN
12596 						msc_sch_wb.atp_debug('ATO update details_temp date go down: ' || l_ship_date);
12597 						msc_sch_wb.atp_debug('pegging id: ' || l_parent_pegging_id);
12598 					END IF;
12599 				END IF;
12600 
12601                                 -- remove what's bad
12602                                 --subst: we remember this pegging id so that when we go back in
12603                                 -- ATP_CHECK_SUBST we can remember to remove this pegging if need to be
12604                                 MSC_ATP_PVT.G_FUTURE_PEGGING_ID := l_pegging_id;
12605                                 -- remove what's bad
12606 
12607                                 IF PG_DEBUG in ('Y', 'C') THEN
12608                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : ' || l_atp_pegging_tab.COUNT);
12609                                 END IF;
12610 
12611 				IF l_atp_pegging_tab.COUNT > 0 THEN
12612 
12613 				   -- Planned Pegging based AATP, l_atp_pegging_tab populated in
12614 				   -- MSC_AATP_REQ.Get_Forward_Material_Atp
12615 
12616 				   MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
12617 				   l_atp_pegging_tab := MRP_ATP_PUB.Number_Arr(); --bug3581151
12618 
12619 				ELSE 		-- IF l_atp_pegging_tab.COUNT > 0 THEN
12620                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_future_pegging_id,
12621                                                  p_plan_id,
12622                                                  p_atp_record.ship_date,
12623                                                  0,
12624                                                  l_atp_rec.supplier_id,
12625                                                  l_atp_rec.supplier_site_id,
12626                                                  null,
12627                                                  null,  -- Bug 3241766
12628                                                  null,  -- Bug 3241766
12629                                                  null,  -- Bug 3241766
12630                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
12631                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
12632 				END IF;		-- IF l_atp_pegging_tab.COUNT > 0 THEN
12633 
12634                                 --bug 2798667: we have already calculated the final date
12635                                 /*p_atp_record.ship_date :=
12636                                      MSC_CALENDAR.DATE_OFFSET(
12637                                      p_atp_record.organization_id,
12638                                      p_atp_record.instance_id,
12639                                      1,
12640                                      l_ship_date,
12641                                      NVL(p_atp_record.atp_lead_time, 0));
12642                                 */
12643                                 p_atp_record.ship_date := l_atp_lt_offset_ship_date;
12644 
12645 
12646                                 -- we will say the available quantity is
12647                                 -- exact the quantity we want to order.
12648                                 -- although this is not correct (
12649                                 -- it should be the quantity we get from next
12650                                 -- level plus what we can have on the parent
12651                                 -- level.  But now we don't want to search
12652 
12653                                 p_atp_record.available_quantity := p_atp_record.quantity_ordered;
12654 
12655                                 IF p_atp_record.ship_date <= p_atp_record.atf_date THEN
12656                                     p_atp_record.atf_date_quantity := p_atp_record.quantity_ordered;
12657                                 ELSE
12658                                     p_atp_record.atf_date_quantity := 0;
12659                                 END IF;
12660 
12661 				IF PG_DEBUG in ('Y', 'C') THEN
12662 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: l_atp_rec.quantity_ordered = '||
12663 					l_atp_rec.quantity_ordered);
12664 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: l_atp_rec.available_quantity = '||
12665 					l_atp_rec.available_quantity);
12666 				   msc_sch_wb.atp_debug('ATP_Check: ' || 'question: p_atp_record.available_quantity = '||
12667 					p_atp_record.available_quantity);
12668 			           msc_sch_wb.atp_debug('ATP_Check: ' || 'question: p_atp_record.used_available_quantity = '||
12669 					p_atp_record.used_available_quantity); --bug3409973
12670 
12671                                    -- 2754446
12672                                    msc_sch_wb.atp_debug('l_prim_uom_avail_qty: ' ||l_prim_uom_avail_qty);
12673                                    msc_sch_wb.atp_debug('l_PO_qty: ' ||l_PO_qty);
12674                                    msc_sch_wb.atp_debug('l_conversion_rate:' || l_conversion_rate);
12675 				END IF;
12676 
12677                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
12678                                             p_plan_id,
12679                                             --bug 2798667: The ATO components are sourced on l_ship_date
12680                                             --p_atp_record.ship_date,
12681                                             l_ship_date,
12682                                             -- dsting 2754446
12683 --                                            least(l_atp_rec.available_quantity, l_atp_rec.quantity_ordered),
12684                                             least(l_prim_uom_avail_qty, l_PO_qty),
12685                                             l_atp_rec.supplier_id,
12686                                             l_atp_rec.supplier_site_id,
12687                                             l_new_dock_date,    -- Bug 3241766
12688                                             l_new_ship_date,    -- Bug 3241766
12689                                             l_sugg_start_date,  -- Bug 3241766
12690                                             l_order_date,       -- Bug 3241766
12691                                             C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
12692                                             PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
12693                                             MSC_ATP_PVT.UNDO, -- dsting 2754446
12694                                             l_conversion_rate -- dsting 2754446
12695                                 );  -- dock date
12696 
12697                                 -- move the supply/demand, period atp
12698                                 -- of the transfer/buy to the output record of
12699                                 -- tables.
12700 
12701                                 l_future_pegging_id := l_pegging_id;
12702                                 l_future_atp_period := l_tb_atp_period;
12703                                 l_future_atp_supply_demand := l_tb_atp_supply_demand;
12704 
12705                             END IF;  -- which level is better
12706 
12707                         ELSE
12708 
12709                              -- we are in backward part
12710                              IF PG_DEBUG in ('Y', 'C') THEN
12711                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Are we here? we should adjust the planned order');
12712                              END IF;
12713 
12714                              -- cchen: bug 1121227
12715                              -- the quantity we want to adjust should not
12716                              -- less than 0.  add greatest to
12717                              -- l_atp_rec.requested_date_quantity
12718 
12719 			     IF( l_atp_rec.children_type <> MAKE_BUY_CHILDREN and
12720 				 l_have_make_buy_parent = 0 AND
12721 				 MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and
12722 				 MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' )
12723 			     THEN
12724 				l_update_PO_mode := MSC_ATP_PVT.INVALID;
12725 			     ELSE
12726 				l_update_PO_mode := MSC_ATP_PVT.UNDO;
12727 			     END IF;
12728 			     IF PG_DEBUG in ('Y', 'C') THEN
12729 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'set l_update_PO_mode ' || l_update_PO_mode);
12730 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'make_buy_children ' || l_atp_rec.children_type);
12731 			        msc_sch_wb.atp_debug('ATP_Check: ' || 'make_buy_parent ' || l_have_make_buy_parent);
12732 				msc_sch_wb.atp_debug('last good pegging ' || l_last_good_pegging_id);
12733 				msc_sch_wb.atp_debug('constraint_flag: ' || l_constraint_flag );
12734 			     END IF;
12735 
12736 			     -- dsting diag_atp if we still have sources or diagnostic atp is off
12737 			     -- then we update the planned order. If l_update_PO_mode is INVALID
12738 			     -- then we remove the subtree. Otherwise, diagnostic atp is on and
12739 			     -- this is the last source, so we leave the planned order as is to
12740 			     -- balance the parent demand and only update the pegging
12741                              -- dsting 2819716. we always add planned orders now
12742                              -- so we should always update them too.
12743 --			     IF l_constraint_flag <> 1 THEN
12744 			        IF l_update_PO_mode = MSC_ATP_PVT.INVALID OR
12745 				   l_sources.source_type.next(j) is not NULL OR
12746 				   MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1
12747 			        THEN
12748 
12749                                    IF PG_DEBUG in ('Y', 'C') THEN
12750                                       msc_sch_wb.atp_debug('l_PO_qty: ' || l_PO_qty);
12751                                       msc_sch_wb.atp_debug('l_prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
12752                                    END IF;
12753 
12754                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
12755 	                             p_plan_id,
12756 	                             null,
12757                                      -- dsting 2754446
12758 --	                             least(greatest(l_atp_rec.requested_date_quantity, 0),
12759 --	                             l_atp_rec.quantity_ordered),
12760                                      least(greatest(l_prim_uom_req_date_qty, 0),
12761                                            l_PO_qty),
12762 	                             l_atp_rec.supplier_id,
12763 	                             l_atp_rec.supplier_site_id,
12764 	                             null, -- l_atp_rec.requested_ship_date, no need to update -- Bug 3241766
12765 	                             null, -- Bug 3241766
12766 	                             null, -- Bug 3241766
12767 	                             null, -- Bug 3241766
12768                                      C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
12769                                      PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
12770 				     l_update_PO_mode,
12771                                      l_conversion_rate
12772                                   );  -- dock date
12773 
12774 				IF l_update_PO_mode = MSC_ATP_PVT.UNDO THEN
12775 				     l_last_good_pegging_id := l_pegging_id;
12776 				END IF;
12777 
12778 				IF l_requested_ship_date <= p_atp_record.atf_date THEN
12779 				    p_atp_record.atf_date_quantity := p_atp_record.atf_date_quantity +
12780 				                                            least(greatest(l_prim_uom_req_date_qty, 0),
12781 				                                                  l_PO_qty);
12782                                 END IF;
12783 /*			     ELSE
12784 
12785 				l_last_good_pegging_id := l_pegging_id;
12786 				MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null,
12787 					least(greatest(l_atp_rec.requested_date_quantity, 0)));
12788 			     END IF;
12789 */
12790                            --5230396, updating planned order for diag ATP too in Buy case.
12791                            ELSIF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
12792                                  IF PG_DEBUG in ('Y', 'C') THEN
12793                                       msc_sch_wb.atp_debug('l_PO_qty: ' || l_PO_qty);
12794                                       msc_sch_wb.atp_debug('l_prim_uom_req_date_qty: ' || l_prim_uom_req_date_qty);
12795                                    END IF;
12796 
12797                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
12798 	                             p_plan_id,
12799 	                             null,
12800                                      -- dsting 2754446
12801 --	                             least(greatest(l_atp_rec.requested_date_quantity, 0),
12802 --	                             l_atp_rec.quantity_ordered),
12803                                      --least(greatest(l_prim_uom_req_date_qty, 0),
12804                                      --     l_PO_qty),
12805                                      --NVL(l_PO_qty,0),
12806                                      NULL,
12807 	                             l_atp_rec.supplier_id,
12808 	                             l_atp_rec.supplier_site_id,
12809 	                             null, -- l_atp_rec.requested_ship_date, no need to update -- Bug 3241766
12810 	                             null, -- Bug 3241766
12811 	                             null, -- Bug 3241766
12812 	                             null, -- Bug 3241766
12813                                      C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
12814                                      PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
12815 				     l_update_PO_mode,
12816                                      l_conversion_rate
12817                                   );  -- dock date
12818                         END IF;
12819 
12820 /*
12821                              -- 1228607: since we add this planned order,
12822                              -- we need to work on the demand record for this too.
12823                              update msc_demands
12824                              set    USING_REQUIREMENT_QUANTITY =
12825                                    (USING_REQUIREMENT_QUANTITY -
12826                                    LEAST(l_net_demand, 0))
12827                              where  demand_id = l_demand_id
12828                              and    plan_id = p_plan_id;
12829 */
12830                              -- cchen :1064647
12831                              -- 2754446
12832                              IF least(l_prim_uom_req_date_qty, l_PO_qty) > 0 THEN
12833                              -- IF least(l_atp_rec.requested_date_quantity,
12834                              --   l_atp_rec.quantity_ordered) > 0 THEN
12835                                 MSC_ATP_PROC.Details_Output(l_tb_atp_period,
12836                                 l_tb_atp_supply_demand,
12837                                 x_atp_period,
12838                                 x_atp_supply_demand,
12839                                 l_return_status);
12840                              END IF;
12841 
12842                         END IF; -- forward scheduling
12843 
12844                         IF l_net_demand <= 0 THEN
12845                            l_net_demand := 0;
12846                            EXIT;
12847                         END IF;
12848 
12849                         END IF; -- for the plan
12850                     ELSE  -- it is a make
12851 
12852                         --s_cto_rearch
12853                         IF PG_DEBUG in ('Y', 'C') THEN
12854                            msc_sch_wb.atp_debug('It is a make case ');
12855                            msc_sch_wb.atp_debug('G_MODEL_BOM_IS_COLLECTED := ' || MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED);
12856                            msc_sch_wb.atp_debug('Bom Item Flag := ' || PF_ITEM_INFO_REC.bom_item_type);
12857                         END IF;
12858                         l_get_comp_info_rec.line_id := p_atp_record.demand_source_line;
12859                         l_get_comp_info_rec.bom_item_type := PF_ITEM_INFO_REC.Bom_item_type;
12860                         l_get_comp_info_rec.atp_flag := l_atp_flag;
12861                         l_get_comp_info_rec.atp_comp_flag := l_atp_comp_flag;
12862                         l_get_comp_info_rec.fixed_lt  := PF_ITEM_INFO_REC.fixed_lt;
12863                         l_get_comp_info_rec.variable_lt := PF_ITEM_INFO_REC.variable_lt;
12864                         l_get_comp_info_rec.replenish_to_order_flag  := PF_ITEM_INFO_REC.replenish_to_ord_flag;
12865                         l_get_comp_info_rec.check_model_capacity_flag := l_check_model_capacity;
12866                         l_get_comp_info_rec.model_sr_inv_item_id := l_sr_inv_item_id;
12867                         --bug 3059305: Pass ship date from last source as the cutoff date
12868                         l_get_comp_info_rec.ship_date_this_level := p_atp_record.ship_date;
12869 
12870                         --4501944, call get_pf_plan_info in make cases too.
12871                         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
12872                         MSC_ATP_PF.Get_PF_Plan_Info(
12873                                        p_atp_record.instance_id,
12874                                        p_atp_record.request_item_id,
12875                                        p_atp_record.inventory_item_id,
12876                                        p_atp_record.organization_id,
12877                                        p_atp_record.demand_class,
12878                                        p_atp_record.atf_date,
12879                                        p_atp_record.error_code,
12880                                        l_return_status,
12881                                        p_plan_id );
12882 
12883                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12884                            IF PG_DEBUG in ('Y', 'C') THEN
12885                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP Downtime');
12886                            END IF;
12887                            x_return_status := FND_API.G_RET_STS_ERROR;
12888                            RAISE FND_API.G_EXC_ERROR;
12889                         END IF;
12890                         --4501944
12891 
12892                         IF PF_ITEM_INFO_REC.bom_item_type =1 and PF_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
12893 
12894                            --pass line info for model as model might be getting sourced from another org
12895                            l_get_comp_info_rec.replenish_to_order_flag  := PF_ITEM_INFO_REC.replenish_to_ord_flag;
12896                            l_get_comp_info_rec.top_model_line_id := p_atp_record.top_model_line_id;
12897                            l_get_comp_info_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
12898                            l_get_comp_info_rec.ato_parent_model_line_id := p_atp_record.ato_parent_model_line_id;
12899                            l_get_comp_info_rec.parent_line_id := p_atp_record.parent_line_id;
12900                            l_get_comp_info_rec.parent_so_quantity := p_atp_record.parent_so_quantity;
12901 
12902                            /* -- we also need to get model bom if it has not been collected
12903                            IF NVL(MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED, 2) = 2 THEN
12904 
12905                                 MSC_ATP_CTO.Populate_CTO_Bom(MSC_ATP_PVT.G_SESSION_ID, null);
12906                                 MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 1;
12907 
12908                            END IF;
12909                            */
12910                         ELSIF PF_ITEM_INFO_REC.bom_item_type = 4 and PF_ITEM_INFO_REC.replenish_to_ord_flag = 'Y' THEN
12911                            -- We are deleting stealing records using ato_model_lien_id
12912                            -- Therefore , we need to pass ato model line id so that
12913                            --stealing records could be adjusted correctly.
12914                            l_get_comp_info_rec.ato_model_line_id := p_atp_record.ato_model_line_id;
12915 
12916                         END IF;
12917                         --e_cto_rearch
12918 
12919                         l_sched_status := TRUE;
12920 
12921                         -- dsting 2754446 adjust l_net_demand according to rounding_control_type
12922                         -- so our planned orders turn out right.
12923                         --bug3397904 start
12924                         IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
12925                            --l_PO_qty := CEIL(l_net_demand);
12926                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
12927                            IF (p_search = FORWARD_SCHEDULING)
12928                                and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
12929                                AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
12930                                AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN  --bug3397904
12931                                l_PO_qty := CEIL(p_atp_record.quantity_ordered);
12932                                IF PG_DEBUG in ('Y', 'C') THEN
12933                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
12934                                END IF;
12935                            ELSE
12936                                l_PO_qty := CEIL(l_net_demand);
12937                                IF PG_DEBUG in ('Y', 'C') THEN
12938                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
12939                                END IF;
12940                            END IF;
12941                         ELSE
12942                            --bug 3904063: Reset the quantity only in case of demand priorit allocated ATP
12943                            IF (p_search = FORWARD_SCHEDULING)
12944                                and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
12945                                AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
12946                                AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1  THEN  --bug3397904
12947 
12948                                l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);   --5598066
12949                                IF PG_DEBUG in ('Y', 'C') THEN
12950                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
12951                                END IF;
12952                            ELSE
12953                                l_PO_qty := trunc(l_net_demand, 6);	--5598066
12954                                IF PG_DEBUG in ('Y', 'C') THEN
12955                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
12956                                END IF;
12957                            END IF;
12958                         END IF;
12959                         --bug3397904 end
12960                         -- dsting 2764213
12961                         l_planned_sources := 1;
12962 
12963                         IF PG_DEBUG in ('Y', 'C') THEN
12964                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make');
12965                         END IF;
12966                         -- insert a plan order for the l_net_demand
12967                         --- bug 1941489
12968                         --- assign correct pegging-id to co-product pegging id
12969                         --- this pegging id will be used to link the coproduct supply to correct
12970                         ---parent
12971                         l_coprod_pegging_id := l_parent_pegging_id;
12972                         IF PG_DEBUG in ('Y', 'C') THEN
12973                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_coprod_pegging_id := ' || l_coprod_pegging_id);
12974                         END IF;
12975                         --- bug 2178544
12976                         ---- get mso_lead_time factor
12977                         l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
12978                         --- Get the lead time for the item
12979                         -- dsting 2754446
12980                         l_net_processing_lt := CEIL((l_fixed_lt +
12981                                                     (l_variable_lt * l_PO_qty))*(1 + l_mso_lead_time_factor)
12982                                                     + l_pre_pro_lt);
12983                         l_process_lt := CEIL((l_fixed_lt +
12984                                              (l_variable_lt * l_PO_qty))*(1 + l_mso_lead_time_factor));
12985 
12986 --                        l_net_processing_lt := CEIL((l_fixed_lt +
12987 --                                                      (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor)
12988 --                                                      + l_pre_pro_lt);
12989 --                        l_process_lt := CEIL((l_fixed_lt +
12990 --                                                  (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor));
12991                         IF PG_DEBUG in ('Y', 'C') THEN
12992                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mso_lead_time_factor := ' || l_mso_lead_time_factor);
12993                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
12994                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
12995                         END IF;
12996                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL := l_ptf_date;
12997                         ---diag_ATP
12998                         IF p_search = BACKWARD_SCHEDULING THEN
12999                             l_constraint_flag := 2;
13000                             --diag_ATP
13001                             IF l_net_processing_lt > 0 THEN
13002                                  IF PG_DEBUG in ('Y', 'C') THEN
13003                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate order date');
13004                                  END IF;
13005                                 /* ship_rec_cal
13006                                 l_order_date := MSC_CALENDAR.DATE_OFFSET
13007                                                                 (p_atp_record.organization_id,
13008                                                                 p_atp_record.instance_id,
13009                                                                 1,
13010                                                                 l_requested_ship_date,
13011                                                                 (-1 * l_net_processing_lt));*/
13012                                 -- since it is a make case only calendar being used is p_atp_record.manufacturing_cal_code
13013                                 l_order_date := MSC_CALENDAR.DATE_OFFSET(
13014                                                                 p_atp_record.manufacturing_cal_code,
13015                                                                 p_atp_record.instance_id,
13016                                                                 l_requested_ship_date,
13017                                                                 (-1 * l_net_processing_lt), -1);
13018 
13019                             ELSE
13020                                  IF PG_DEBUG in ('Y', 'C') THEN
13021                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Net process lt is zero');
13022                                  END IF;
13023                                 l_order_date := l_sysdate;
13024                             END IF;
13025 
13026                             IF PG_DEBUG in ('Y', 'C') THEN
13027                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_order_date := ' || l_order_date);
13028                             END IF;
13029                             -- calculate start date only if PTF_flag is enabled for the plan
13030                             IF (l_process_lt > 0) THEN
13031                                IF PG_DEBUG in ('Y', 'C') THEN
13032                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate start date');
13033                                END IF;
13034                                /* ship_rec_cal
13035                                l_start_date := MSC_CALENDAR.DATE_OFFSET
13036                                                                 (p_atp_record.organization_id,
13037                                                                 p_atp_record.instance_id,
13038                                                                 1,
13039                                                                 l_requested_ship_date,
13040                                                                 (-1 * l_process_lt));*/
13041                                l_start_date := MSC_CALENDAR.DATE_OFFSET(
13042                                                                 p_atp_record.manufacturing_cal_code,
13043                                                                 p_atp_record.instance_id,
13044                                                                 l_requested_ship_date,
13045                                                                 (-1 * l_process_lt), -1);
13046                             ELSE
13047 
13048                                IF PG_DEBUG in ('Y', 'C') THEN
13049                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'PTF flag is off for the plan or process LT is 0');
13050                                END IF;
13051                                l_start_date := l_ptf_date;
13052                             END IF;
13053 
13054                             IF PG_DEBUG in ('Y', 'C') THEN
13055                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
13056                             END IF;
13057                             -- bug 2178544
13058 
13059                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13060                                 IF (l_order_date < l_sysdate OR l_start_date < l_ptf_date)  THEN
13061                                    -- we set the constraint flag so that we know that an constraint has occured
13062                                    l_constraint_flag := 1;
13063 
13064                                    ---calculate earliest due date possible based on PTF date
13065                                    IF l_net_processing_lt > 0 THEN
13066                                         /* ship_rec_cal
13067                                         l_lt_due_date :=
13068                                                     MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13069                                                                                       p_atp_record.instance_id,
13070                                                                                       1,
13071                                                                                       l_sysdate,
13072                                                                                       l_net_processing_lt);*/
13073                                         l_lt_due_date :=
13074                                                     MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13075                                                                                       p_atp_record.instance_id,
13076                                                                                       l_sysdate,
13077                                                                                       l_net_processing_lt, 1);
13078                                    ELSE
13079                                       l_lt_due_date := l_sysdate;
13080                                    END IF;
13081                                    ---calculate earliest due date possible based on PTF date
13082                                    IF  l_process_lt > 0 THEN
13083                                         /* ship_rec_cal
13084                                         l_ptf_due_date :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13085                                                                        p_atp_record.instance_id,
13086                                                                        1,
13087                                                                        l_ptf_date,
13088                                                                        l_process_lt);*/
13089                                         l_ptf_due_date :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13090                                                                        p_atp_record.instance_id,
13091                                                                        l_ptf_date,
13092                                                                        l_process_lt, 1);
13093                                    ELSE
13094                                         l_ptf_due_date := l_ptf_date;
13095                                    END IF;
13096                                    IF PG_DEBUG in ('Y', 'C') THEN
13097                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_ptf_due_date := ' || l_ptf_due_date);
13098                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_lt_due_date := ' || l_lt_due_date);
13099                                    END IF;
13100 
13101                                    ---We marked the most constrained constraint. If both constraint are equally
13102                                    -- constrained then we mark PTF constraint.
13103 
13104                                    -- Bug 3525582 - Constraint message should be added only if constraint actually exists
13105                                    IF l_lt_due_date > l_requested_ship_date OR
13106                                        l_ptf_due_date > l_requested_ship_date THEN
13107 
13108                                        -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
13109                                        IF l_ptf_enabled=2 OR l_lt_due_date > l_ptf_due_date THEN
13110                                            l_pegging_rec.constraint_date := l_lt_due_date;
13111                                            l_pegging_rec.constraint_type := 3;
13112                                        ELSE
13113                                            l_pegging_rec.constraint_date := l_ptf_due_date;
13114                                            l_pegging_rec.constraint_type := 2;
13115                                        END IF;
13116                                    END IF;
13117                                 END IF; -- IF (l_order_date < l_sysdate OR l_start_date < l_ptf_date)  THEN
13118                             END IF; --IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13119                         END IF;
13120                         IF p_search = FORWARD_SCHEDULING THEN
13121 
13122                             -- p_sched_flag = FORWARD_SCHEDULING
13123                             --- The future date must be greater than
13124                             --             1) sysdate + net_process time (pre + fixed + variable )
13125                             --             2) PTF Date + processing LT ( fixed + varibale LT)
13126                             -- Therefore we calcuate these dates
13127                             --- bug 2178544
13128                             IF l_net_processing_lt > 0 THEN
13129                                /* ship_rec_cal
13130                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13131                                                                                       p_atp_record.instance_id,
13132                                                                                       1,
13133                                                                                       l_sysdate,
13134                                                                                       l_net_processing_lt); */
13135                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13136                                                                                       p_atp_record.instance_id,
13137                                                                                       l_sysdate,
13138                                                                                       l_net_processing_lt, 1);
13139                             ELSE
13140                                MSC_ATP_PVT.G_FUTURE_ORDER_DATE := l_sysdate;
13141                             END IF;
13142 
13143                             IF  l_process_lt > 0 THEN
13144                                 /* ship_rec_cal
13145                                 l_start_date :=  MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13146                                                                        p_atp_record.instance_id,
13147                                                                        1,
13148                                                                        l_ptf_date,
13149                                                                        l_process_lt); */
13150                                 l_start_date :=  MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13151                                                                        p_atp_record.instance_id,
13152                                                                        l_ptf_date,
13153                                                                        l_process_lt, 1);
13154                             ELSE
13155                                 l_start_date := l_ptf_date;
13156                             END IF;
13157                             IF PG_DEBUG in ('Y', 'C') THEN
13158                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
13159                             END IF;
13160                             MSC_ATP_PVT.G_FUTURE_START_DATE := GREATEST(l_start_date, MSC_ATP_PVT.G_PTF_DATE);
13161                             ---we make sure that PTF for this stage is greatst of Parent's ptf - lead time,
13162                             -- current level's PTF and (sysdate + pre processing lt). The last part is the minimum
13163                             -- start date. This PTF is start date for the components for the current itme
13164                             IF l_pre_pro_lt >0 THEN
13165                                MSC_ATP_PVT.G_PTF_DATE := GREATEST(
13166                                                                /* ship_rec_cal
13167                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13168                                                                                         p_atp_record.instance_id,
13169                                                                                         1,
13170                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13171                                                                                         -1 * l_process_lt),*/
13172                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13173                                                                                         p_atp_record.instance_id,
13174                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13175                                                                                         -1 * l_process_lt, -1),
13176                                                                l_ptf_date,
13177                                                                /* ship_rec_cal
13178                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13179                                                                                       p_atp_record.instance_id,
13180                                                                                       1,
13181                                                                                       l_sysdate,
13182                                                                                       l_pre_pro_lt));*/
13183                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13184                                                                                       p_atp_record.instance_id,
13185                                                                                       l_sysdate,
13186                                                                                       l_pre_pro_lt, 1));
13187                             ELSE
13188                                MSC_ATP_PVT.G_PTF_DATE := GREATEST(
13189                                                                /* ship_rec_cal
13190                                                                MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13191                                                                                         p_atp_record.instance_id,
13192                                                                                         1,
13193                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13194                                                                                         -1 * l_process_lt),*/
13195                                                                MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13196                                                                                         p_atp_record.instance_id,
13197                                                                                         MSC_ATP_PVT.G_PTF_DATE,
13198                                                                                         -1 * l_process_lt, -1),
13199                                                                 l_ptf_date, l_sysdate);
13200                             END IF;
13201 
13202                             IF PG_DEBUG in ('Y', 'C') THEN
13203                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FUTURE_ORDER_DATE :='|| MSC_ATP_PVT.G_FUTURE_ORDER_DATE);
13204                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_FUTURE_START_DATE :='|| MSC_ATP_PVT.G_FUTURE_START_DATE);
13205                                msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
13206                             END IF;
13207                             --- if requested_ship_date is less than l_ptf_date then we add ptf_date for this item
13208                             -- UI checks this column. If this coulm is populated then UI flags this PO
13209                             --diag_atp
13210                             -- IF l_requested_ship_date < l_start_date  or l_requested_ship_date < sysdate THEN
13211                             -- Bug 3412143 - Use l_sysdate (in this case validated in OMC) instead of sysdate
13212                             IF l_requested_ship_date < l_start_date  or l_requested_ship_date < l_sysdate THEN
13213                                 IF PG_DEBUG in ('Y', 'C') THEN
13214                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Add PTF date to pegging');
13215                                 END IF;
13216                                 -- Bug 3525582 - PTF constraint should be added only if plan is PTF enabled
13217                                 IF l_ptf_enabled=2 OR MSC_ATP_PVT.G_FUTURE_ORDER_DATE > l_start_date THEN
13218                                        l_pegging_rec.constraint_date := MSC_ATP_PVT.G_FUTURE_ORDER_DATE;
13219                                        l_pegging_rec.constraint_type := 3;
13220                                 ELSE
13221                                        l_pegging_rec.constraint_date := l_start_date;
13222                                        l_pegging_rec.constraint_type := 2;
13223                                 END IF;
13224 
13225                             END IF;
13226                         END IF;
13227                         /* Modularize Item and Org Info */
13228                         -- Changed call to remove embedded function call.
13229 
13230                         ---diag_atp
13231                         IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and l_constraint_flag = 1 THEN
13232                            --in diagnostic ATP since we have already failed the lead time test
13233                            --- there is no point in adding PO and removing it later
13234 			   l_transaction_id := NULL;
13235                         ELSE
13236 
13237                            --s_cto_rearch
13238                            --we do not get process effectivity when checking base model's
13239                            -- capacity for config item
13240                            IF l_check_model_capacity = 2 THEN
13241                                /* Make case Get the Process Sequence ID.*/
13242                               -- (ssurendr) Bug 2865389 : Replace call to function MSC_ATP_FUNC.get_process_seq_id
13243                               -- by procedure MSC_ATP_PROC.get_process_effectivity
13244                               --Details. Derive Item's Start Date by Offsetting
13245                               --Due date with LT. Check effectivity on Start Date.
13246                               /* ship_rec_cal
13247                               l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13248                                                            p_atp_record.instance_id,
13249                                                            1,
13250                                                            l_requested_ship_date,
13251                                                            -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13252                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty));*/
13253                               l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13254                                                            p_atp_record.instance_id,
13255                                                            l_requested_ship_date,
13256                                                            -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13257                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty), -1);
13258                               MSC_ATP_PROC.get_process_effectivity(
13259                                    p_plan_id,
13260                                    --PF_ITEM_INFO_REC.dest_inv_item_id,
13261                                    C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
13262                                    p_atp_record.organization_id,
13263                                    p_atp_record.instance_id,
13264                                    l_opm_start_date,
13265                                    l_PO_qty,
13266                                    l_process_seq_id,
13267                                    l_routing_seq_id,
13268                                    l_bill_seq_id,
13269                                    l_op_seq_id, --4570421
13270                                    l_return_status);
13271                               -- check the return status
13272                               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13273                               	   RAISE FND_API.G_EXC_ERROR;
13274 	                      END IF;
13275                            END IF;
13276                            --s_cto_rearch
13277 	                   IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
13278                               MSC_ATP_DB_UTILS.Add_Planned_Order(
13279                                     p_atp_record.instance_id,
13280                                     p_plan_id,
13281                                     PF_ITEM_INFO_REC.dest_inv_item_id,
13282                                     p_atp_record.organization_id,
13283                                     l_planned_order_date,  -- For ship_rec_cal
13284                                     l_PO_qty, -- 2754446
13285                                     -- l_net_demand,
13286                                     null,
13287                                     null,
13288                                     p_atp_record.demand_class,
13289                                     -- rajjain 02/19/2003 Bug 2788302 Begin
13290                                     -- Add Sourcing details
13291                                     l_sources.organization_id(j),
13292                                     l_sources.instance_id(j),
13293                                     l_process_seq_id,
13294                                     -- rajjain 02/19/2003 Bug 2788302 End
13295                                     p_refresh_number,    -- For summary enhancenment
13296                                     -- ship_rec_cal changes begin
13297                                     null,
13298                                     null,
13299                                     null,
13300                                     null,
13301                                     null,
13302                                     --bug 3755704: Add start date and order date
13303                                     l_start_date,
13304                                     l_order_date,
13305                                     --null,       -- Bug 3241766
13306                                     --null,       -- Bug 3241766
13307                                     null,
13308                                     -- ship_rec_cal changes end
13309                                     l_transaction_id,
13310                                     l_return_status,
13311                                     -- time_phased_atp
13312                                     NULL, --4127630 (Make case)
13313                                     C_ITEM_INFO_REC.dest_inv_item_id,
13314                                     p_atp_record.atf_date
13315                               );
13316                            END IF;
13317                         END IF;
13318                         /* Modularize Item and Org Info */
13319 
13320                         -- populate insert rec to pegging tree for this supply
13321 
13322                         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
13323                         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
13324                         --subst
13325                         --l_pegging_rec.parent_pegging_id:= l_pegging_id;
13326                         l_pegging_rec.parent_pegging_id := l_parent_pegging_id;
13327                         l_pegging_rec.atp_level:= p_level+1;
13328                         l_pegging_rec.organization_id :=
13329                             p_atp_record.organization_id;
13330                         --l_pegging_rec.organization_code := l_org_code;
13331                         l_pegging_rec.organization_code :=
13332                                MSC_ATP_FUNC.get_org_code(p_atp_record.instance_id,
13333                                            l_pegging_rec.organization_id);--5222584
13334                         l_pegging_rec.identifier1:= p_atp_record.instance_id;
13335                         l_pegging_rec.identifier2:= p_plan_id;
13336                         l_pegging_rec.identifier3 := l_transaction_id;
13337 
13338                         -- time_phased_atp changes begin
13339                         IF l_pf_atp = 'Y' THEN
13340                            l_pegging_rec.inventory_item_id:= p_atp_record.inventory_item_id;
13341                            l_pegging_rec.inventory_item_name := PF_ITEM_INFO_REC.item_name;
13342                         ELSE
13343                            l_pegging_rec.inventory_item_id:= p_atp_record.request_item_id;
13344                            l_pegging_rec.inventory_item_name := C_ITEM_INFO_REC.item_name;
13345                         END IF;
13346                         l_pegging_rec.aggregate_time_fence_date := p_atp_record.atf_date;
13347                         -- time_phased_atp changes end
13348 
13349                         l_pegging_rec.resource_id := NULL;
13350                         l_pegging_rec.resource_code := NULL;
13351                         l_pegging_rec.department_id := NULL;
13352                         l_pegging_rec.department_code := NULL;
13353                         l_pegging_rec.supplier_id := NULL;
13354                         l_pegging_rec.supplier_name := NULL;
13355                         l_pegging_rec.supplier_site_id := NULL;
13356                         l_pegging_rec.supplier_site_name := NULL;
13357 
13358                         l_pegging_rec.scenario_id:= p_scenario_id;
13359                         l_pegging_rec.supply_demand_source_type:= 1;
13360 
13361                         --diag_atp -- bug 3903205: set qty to zero only for diagnostic ATP
13362                         IF l_constraint_flag = 1 and NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP, 2) = 1 THEN
13363                            l_pegging_rec.supply_demand_quantity :=0;
13364                         ELSE
13365                            l_pegging_rec.supply_demand_quantity :=l_PO_qty; -- 2754446
13366                            -- l_pegging_rec.supply_demand_quantity :=l_net_demand;
13367                         END IF;
13368                         l_pegging_rec.supply_demand_type:= 2;
13369                         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
13370                         l_pegging_rec.source_type := 2;
13371 
13372                         IF PG_DEBUG in ('Y', 'C') THEN
13373                            msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 1');
13374                         END IF;
13375 
13376                         -- for demo:1153192
13377                         l_pegging_rec.constraint_flag := 'N';
13378 			l_pegging_rec.component_identifier :=
13379                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
13380 
13381                         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
13382 
13383                         --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
13384                         --- cto looks at pegging tree to place their demands. Since CTO expects to find
13385                         --  id for the requested item, we add the following column. CTO will now read from this column
13386                         l_pegging_rec.request_item_id := p_atp_record.request_item_id;
13387 
13388                         --diag_atp
13389                         l_pegging_rec.ptf_date := l_ptf_date;
13390                         l_pegging_rec.preprocessing_lead_time := l_pre_pro_lt;
13391                         l_pegging_rec.processing_lead_time := l_process_lt;
13392 
13393                         --bug 4409618: Show variable and fixed lt in pegging properties
13394                         l_pegging_rec.fixed_lead_time := l_fixed_lt;
13395                         l_pegging_rec.VARIABLE_LEAD_TIME := l_variable_lt;
13396 
13397                         l_pegging_rec.pegging_type := MSC_ATP_PVT.MAKE_SUPPLY; -- it is a make planned order
13398                         l_pegging_rec.required_date := l_requested_ship_date;
13399                         --bug 3328421
13400                         l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
13401                         l_pegging_rec.required_quantity := l_net_demand;
13402                         l_pegging_rec.plan_name := p_atp_record.plan_name;
13403                         l_pegging_rec.dest_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
13404 
13405                         --bug 3373075: Mark plan order to be model entity only for
13406                         -- config, model or option class
13407                         --parent is model entity
13408                         /* IF (p_atp_record.parent_bom_item_type in (1, 4) and
13409                                                   p_atp_record.parent_repl_order_flag = 'Y')
13410                            --item itself is model entity
13411                            OR (C_ITEM_INFO_REC.bom_item_type in (1, 4) and
13412                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
13413                         */
13414                         --item itself is model entity
13415                         IF (C_ITEM_INFO_REC.bom_item_type in (1,2, 4) and
13416                                                   C_ITEM_INFO_REC.replenish_to_ord_flag = 'Y') THEN
13417                            l_pegging_rec.model_sd_flag := 1;
13418                         END IF;
13419 
13420 
13421                         --bug 3356779: populate atp rule and ITF on PO
13422                         IF l_atp_flag = 'Y' THEN
13423                            l_pegging_rec.infinite_time_fence := l_get_mat_out_rec.infinite_time_fence_date;
13424                            l_pegging_rec.atp_rule_name := l_get_mat_out_rec.atp_rule_name;
13425                         ELSE
13426                            --4394904
13427                            --l_pegging_rec.infinite_time_fence := l_atp_date_this_level;
13428                            l_pegging_rec.infinite_time_fence := l_infinite_time_fence;
13429                            l_pegging_rec.atp_rule_name := l_atp_rule_name;
13430                         END IF;
13431 
13432                         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
13433                          -- Bug 3826234
13434                         l_pegging_rec.manufacturing_cal_code :=  l_atp_rec.manufacturing_cal_code;
13435                         l_pegging_rec.shipping_cal_code      :=  NULL;
13436                         l_pegging_rec.receiving_cal_code     :=  NULL;
13437                         l_pegging_rec.intransit_cal_code     :=  NULL;
13438 
13439                         --4570421
13440                         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
13441                         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
13442                         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
13443                         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
13444                         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
13445                         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
13446                         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
13447 
13448                         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
13449 
13450 			-- dsting diag_atp last good peggingfor make
13451 			IF l_pegging_rec.constraint_type IS NULL THEN
13452 				IF PG_DEBUG in ('Y', 'C') THEN
13453 				   msc_sch_wb.atp_debug('make last good pegging id ' || l_pegging_id);
13454 				END IF;
13455 				l_last_good_pegging_id := l_pegging_id;
13456 			END IF;
13457 
13458                         /*--- bug 2178544
13459                         ---- get mso_lead_time factor
13460                         l_mso_lead_time_factor := MSC_ATP_PVT.G_MSO_LEAD_TIME_FACTOR;
13461                         --- Get the lead time for the item
13462                         l_net_processing_lt := CEIL((l_fixed_lt +
13463                                                       (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor)
13464                                                       + l_pre_pro_lt);
13465                         l_process_lt := CEIL((l_fixed_lt +
13466                                                   (l_variable_lt * l_net_demand))*(1 + l_mso_lead_time_factor));
13467                         IF PG_DEBUG in ('Y', 'C') THEN
13468                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_mso_lead_time_factor := ' || l_mso_lead_time_factor);
13469                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_net_processing_lt := ' || l_net_processing_lt);
13470                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_process_lt := ' || l_process_lt);
13471                         END IF;
13472                         MSC_ATP_PVT.G_PTF_DATE_THIS_LEVEL := l_ptf_date; */
13473 
13474                         IF PG_DEBUG in ('Y', 'C') THEN
13475                            msc_sch_wb.atp_debug('ATP_Check: ' || 'p_search' || p_search);
13476                         END IF;
13477                         --diag_atp
13478                         IF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 and l_constraint_flag <> 1) OR
13479                             (p_search = BACKWARD_SCHEDULING and MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1) THEN
13480 
13481                             l_avail_assembly_qty := l_net_demand;
13482                             --2178544
13483                             ---- if lead time > 0 then offset the date to see if the offseted
13484                             --- date falls before sysdate or not
13485                             ---- If ofsetted date falls before sys date then we can't make the item
13486                             ---- and requested_date quantity is set to Zero
13487                             --- If Offseted date is > sysdate then we  continue as ususal
13488 
13489                             IF PG_DEBUG in ('Y', 'C') THEN
13490                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
13491                             END IF;
13492                             -- bug 2178544
13493 
13494                             IF (l_order_date < l_sysdate OR l_start_date < l_ptf_date)  THEN
13495                                 IF PG_DEBUG in ('Y', 'C') THEN
13496                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'req date - lead time is less than sys or ptf date');
13497                                 END IF;
13498                                 l_avail_assembly_qty := 0;
13499                             ELSE
13500 
13501                                IF PG_DEBUG in ('Y', 'C') THEN
13502                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 2');
13503                                END IF;
13504                                IF ((l_atp_comp_flag = 'R') OR
13505                                	   (l_atp_comp_flag = 'C')) THEN
13506                                	   IF PG_DEBUG in ('Y', 'C') THEN
13507                                	      msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 3');
13508                                	   END IF;
13509 
13510                                	   -- get resource requirements first
13511             		       	   MSC_ATP_REQ.Get_Res_Requirements(
13512                                	     p_atp_record.instance_id,
13513 			       	     p_plan_id,
13514                                	     p_level+2,
13515                                	     p_scenario_id,
13516                                	     --p_atp_record.inventory_item_id,
13517                                	     l_request_item_id, -- Pass member item's id to make sure member item's id is
13518                                	                        -- inserted in assembly_item_id column in msc_resource_requirements
13519                                	     p_atp_record.organization_id,
13520                               	     l_pegging_id,
13521                                      l_PO_qty, -- 2754446
13522                                	     --l_net_demand,
13523                                	     l_requested_ship_date,
13524                                	     p_refresh_number,
13525 			       	     p_atp_record.insert_flag,
13526                                	     p_search,
13527                                	     p_atp_record.demand_class,
13528                                      --(ssurendr) Bug 2865389 Added Routing Sequence id
13529                                      --and Bill sequence id for OPM fix.
13530                                      l_routing_seq_id,
13531                                      l_bill_seq_id,
13532                                      p_atp_record.ship_date, -- Bug 2814872
13533                                      p_atp_record.demand_source_line,   -- CTO ODR
13534                                	     l_avail_assembly_qty,
13535                                	     l_res_atp_date,
13536                                	     l_res_atp_period,
13537                                	     l_res_atp_supply_demand,
13538 	                             l_return_status );
13539 
13540                                	   -- check the return status
13541                                	   IF l_return_status <>
13542                                	       FND_API.G_RET_STS_SUCCESS THEN
13543                               	       RAISE FND_API.G_EXC_ERROR;
13544 	                           END IF;
13545 
13546                                END IF;
13547 
13548                                ---diag_ATP
13549                                IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13550                                    ---In diagnostic mode we look for complete qty. Thats why looks
13551                                    -- for component as if we got complete resource qty.
13552                                    l_res_avail_qty := l_avail_assembly_qty;
13553                                    l_avail_assembly_qty := l_net_demand;
13554                                END IF;
13555 
13556 
13557                                IF ((l_atp_comp_flag = 'C') OR
13558                                 (l_atp_comp_flag = 'Y')) AND
13559                                LEAST(l_net_demand, l_avail_assembly_qty) >0 THEN
13560                              	   -- get component requirements first
13561 
13562                                	   IF PG_DEBUG in ('Y', 'C') THEN
13563                                	      msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 4');
13564                                	   END IF;
13565 
13566                                    BEGIN
13567                                         IF l_avail_assembly_qty < l_net_demand AND
13568                                             G_ORG_INFO_REC.org_type = 1 THEN
13569                                             /* ship_rec_cal
13570                                             l_opm_start_date := l_requested_ship_date -
13571                                                                 (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13572                                                                 nvl(PF_ITEM_INFO_REC.variable_lt,0) *
13573                                                                 l_avail_assembly_qty);*/
13574                                             l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13575                                                                 p_atp_record.instance_id,
13576                                                                 l_requested_ship_date,
13577                                                                 -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13578                                                                       nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_avail_assembly_qty), -1);
13579                                             MSC_ATP_PROC.get_process_effectivity(
13580                                                 p_plan_id,
13581                                                 --PF_ITEM_INFO_REC.dest_inv_item_id,
13582                                                 C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
13583                                                 p_atp_record.organization_id,
13584                                                 p_atp_record.instance_id,
13585                                                 l_opm_start_date,
13586                                                 l_avail_assembly_qty,
13587                                                 l_process_seq_id,
13588                                                 l_routing_seq_id,
13589                                                 l_bill_seq_id,
13590                                                 l_op_seq_id, --4570421
13591                                                 l_return_status);
13592                                            -- check the return status
13593                                            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13594                                               	RAISE FND_API.G_EXC_ERROR;
13595                 	                   END IF;
13596                                         END IF;
13597 
13598 
13599                                	        MSC_ATP_REQ.Get_Comp_Requirements(
13600                                 	     p_atp_record.instance_id,
13601                                     	     p_plan_id,
13602                                     	     p_level+2,
13603                                     	     p_scenario_id,
13604                                     	     l_request_item_id, -- for time_phased_atp
13605                                     	     p_atp_record.organization_id,
13606                                     	     l_pegging_id,
13607                                     	     p_atp_record.demand_class,
13608                                              LEAST(l_PO_qty, l_avail_assembly_qty),
13609                                     	     --LEAST(l_net_demand, l_avail_assembly_qty),
13610                                     	     l_requested_ship_date,
13611                                     	     p_refresh_number,
13612                                     	     p_atp_record.insert_flag,
13613                                     	     p_search,
13614 				    	     p_assign_set_id,
13615                                              -- (ssurendr) Bug 2865389 Added Routing Sequence id
13616                                              --and Bill sequence id for OPM fix.
13617                                              l_routing_seq_id,
13618                                              l_bill_seq_id,
13619                                              p_atp_record.inventory_item_id,      -- For time_phased_atp
13620                                              p_atp_record.atf_date,               -- For time_phased_atp
13621                                              p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
13622                                     	     l_avail_assembly_qty,
13623                                     	     l_comp_atp_date,
13624                                     	     l_comp_atp_period,
13625                                     	     l_comp_atp_supply_demand,
13626 	                                     l_return_status,
13627                                              --s_cto_rearch
13628                                              l_get_comp_info_rec,
13629                                              -- CTO Phase 1 #16, #17
13630                                              p_atp_record.order_number,
13631                                              l_op_seq_id --4570421
13632                                              );
13633                                              -- Allow creation of Sales Order for CTO comps.
13634                                    -- 2400614: krajan
13635                                    EXCEPTION
13636                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
13637                                              IF PG_DEBUG in ('Y', 'C') THEN
13638                                                 msc_sch_wb.atp_debug('ATP_Check: ' || ' Sourcing Mismatch Error');
13639                                              END IF;
13640                                              p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
13641                                              RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
13642 
13643                                         -- dsting 2764213
13644                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
13645                                              IF PG_DEBUG in ('Y', 'C') THEN
13646                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
13647                                              END IF;
13648                                              --24x7
13649                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
13650                                                 ---2854351
13651                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
13652                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
13653                                              else
13654                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
13655                                              end if;
13656 
13657                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
13658                                         -- krajan 2752705
13659                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
13660                                              IF PG_DEBUG in ('Y', 'C') THEN
13661                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item Error');
13662                                              END IF;
13663                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
13664                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
13665                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
13666                                              IF PG_DEBUG in ('Y', 'C') THEN
13667                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
13668                                              END IF;
13669                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
13670                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
13671                                    END;
13672 
13673                                    IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
13674 
13675                                         RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
13676 
13677                               	   ELSIF l_return_status <>FND_API.G_RET_STS_SUCCESS THEN
13678 	                                RAISE FND_API.G_EXC_ERROR;
13679 	                           END IF;
13680 
13681                                END IF;
13682  			    END IF;
13683                             -- now we have done the capacity check
13684                             -- l_avail_assembly_qty is the quantity that
13685                             -- we think we can build.  Since we may reserve
13686                             -- more during the the check for each comp/res,
13687                             -- we need to undo what we have done so far
13688                             -- and use this l_avail_assembly_qty to reserve
13689                             -- the capacity if we can not build the whole
13690                             -- l_net_demand.  For example,  we have bill like
13691                             -- this
13692                             --    A
13693                             --   / \
13694                             --  B   R
13695                             -- let say we need to build 10 A and we have 7 B
13696                             -- and 4 R.  During atp check we reserve 7B and 4R
13697                             -- already.  Now we need to undo these reservation,
13698                             -- and just reserve 4 B and 4 R instead.
13699                             -- the way we will do is
13700                             -- 1: undo all the inserts we have done for this
13701                             --    make node.
13702                             -- 2: redo this make node with l_avail_assembly_qty.
13703 
13704                             -- 2869830 do not assume lower level returns rounded qty
13705                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13706                                l_avail_assembly_qty := LEAST(l_avail_assembly_qty, l_res_avail_qty);
13707                             END IF;
13708 
13709                             IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
13710                                msc_sch_wb.atp_debug('FLOOR of avail qty for l_avail_PO_qty: '
13711                                                     || l_avail_assembly_qty);
13712                                l_avail_PO_qty := FLOOR(l_avail_assembly_qty);
13713                             ELSE
13714                                msc_sch_wb.atp_debug('l_avail_PO_qty := avail qty: '
13715                                                     || l_avail_assembly_qty);
13716                                l_avail_PO_qty := trunc(l_avail_assembly_qty, 6);	--5598066
13717                             END IF;
13718 
13719                             --diag_atp
13720                             --- we go back and adjust the quantities for non-diagnostic ATP only
13721                             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
13722                                l_avail_assembly_qty := LEAST(l_avail_assembly_qty, l_res_avail_qty);
13723 
13724                                IF PG_DEBUG in ('Y', 'C') THEN
13725                                   msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty := ' || l_avail_assembly_qty);
13726 				  msc_sch_wb.atp_debug('last good pegging: ' || l_pegging_id);
13727 				  msc_sch_wb.atp_debug('qty ' || l_avail_assembly_qty);
13728 				  msc_sch_wb.atp_debug('PO qty ' || l_avail_PO_qty);
13729                                END IF;
13730 
13731 				-- dsting diag_atp
13732 				-- If this is the last source we should only update the pegging and not
13733 				-- the actual PO in case of diagnostic atp
13734                                IF l_avail_PO_qty < l_net_demand THEN -- 2754446
13735                                --IF l_avail_assembly_qty < l_net_demand THEN
13736 				  l_last_good_pegging_id := l_pegging_id;
13737 				  IF l_sources.source_type.next(j) IS NULL
13738 				     AND MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1
13739 				  THEN
13740 					MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null, l_avail_PO_qty); -- 2869830
13741 					-- MSC_ATP_DB_UTILS.Update_Pegging(l_pegging_id, null, l_avail_assembly_qty);
13742 				  ELSE
13743                                         MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13744                                                    p_plan_id,
13745                                                    null,
13746                                                    l_avail_PO_qty, -- 2869830
13747                                                    --l_avail_assembly_qty,
13748                                                    null,
13749                                                    null,
13750                                                    null,
13751                                                    null,  -- Bug 3241766
13752                                                    --bug 3755704: Pass start date and order date
13753                                                    l_start_date,
13754                                                    l_order_date,
13755                                                    --null,  -- Bug 3241766
13756                                                    --null,  -- Bug 3241766
13757                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13758                                                    PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
13759 						   MSC_ATP_PVT.UNDO);
13760 				  END IF;
13761 
13762                                END IF;
13763                                -- move the supply/demand, period atp
13764                                -- of the components to the output record of
13765                                -- tables.
13766                                MSC_ATP_PROC.Details_Output(l_comp_atp_period,
13767                                              l_comp_atp_supply_demand,
13768                                              x_atp_period,
13769                                              x_atp_supply_demand,
13770                                              l_return_status);
13771 
13772                                MSC_ATP_PROC.Details_Output(l_res_atp_period,
13773                                              l_res_atp_supply_demand,
13774                                              x_atp_period,
13775                                              x_atp_supply_demand,
13776                                              l_return_status);
13777 
13778                             ELSIF (l_avail_PO_qty < l_net_demand)  THEN
13779                             --ELSIF (l_avail_assembly_qty < l_net_demand)  THEN
13780                               IF PG_DEBUG in ('Y', 'C') THEN
13781                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty ='||l_avail_assembly_qty);
13782                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id);
13783                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 7');
13784                               END IF;
13785                               IF l_avail_PO_qty > 0 THEN
13786                               --IF l_avail_assembly_qty > 0 THEN
13787 
13788                                 -- bug 1277418: now clean up the data.
13789                                 -- if l_avail_assembly_qty >0, we need to
13790                                 -- to keep the planned order.
13791 
13792                                 MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
13793                                        l_pegging_id ,
13794                                        p_atp_record.instance_id,
13795                                        p_plan_id,
13796                                        INVALID,
13797                                        0,
13798                                        l_return_status);
13799 
13800                                 IF PG_DEBUG in ('Y', 'C') THEN
13801                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside make 6');
13802                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_pegging_id = '||l_pegging_id);
13803                                 END IF;
13804 
13805                                 -- update/delete the planned order record.
13806                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
13807                                                    p_plan_id,
13808                                                    null,
13809                                                    l_avail_PO_qty,
13810                                                    --l_avail_assembly_qty,
13811                                                    null,
13812                                                    null,
13813                                                    null,
13814                                                    null,  -- Bug 3241766
13815                                                    --bug 3755704: pass start date and order date
13816                                                    l_start_date,
13817                                                    l_order_date,
13818                                                    --null,  -- Bug 3241766
13819                                                    --null,  -- Bug 3241766
13820                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
13821                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
13822 
13823                                 -- redo the check using l_avail_assembly_qty
13824 
13825        				-- Bug 1562754, use G_ASSEMBLY_LINE_ID instead of G_COMP_LINE_ID,
13826 				-- to make sure that in case of CTO, we try to get the BOM
13827 				-- correctly from msc_bom_temp_table.-NGOEL 02/01/2001
13828 
13829 				-- Commented on 01/02/2001 by NGOEL as we are now using
13830 				-- G_ASSEMBLY_LINE_ID instead of G_COMP_LINE_ID for BOM.
13831 
13832                                 -- Bug 1532051, set G_COMP_LINE_ID again to G_ORDER_LINE_ID,
13833                                 -- just to make sure that in case of CTO, we try to get the
13834                                 -- BOM correctly from msc_bom_temp_table.
13835 
13836                                 IF PG_DEBUG in ('Y', 'C') THEN
13837                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'setting G_COMP_LINE_ID = '||MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
13838                                 END IF;
13839 
13840                                 MSC_ATP_PVT.G_COMP_LINE_ID := MSC_ATP_PVT.G_ASSEMBLY_LINE_ID;
13841 
13842                                 IF ((l_atp_comp_flag = 'R') OR
13843                                   (l_atp_comp_flag = 'C')) THEN
13844                                   -- get resource requirements first
13845                                   IF PG_DEBUG in ('Y', 'C') THEN
13846                                      msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty := ' || l_avail_assembly_qty);
13847                                   END IF;
13848 
13849 				  -- dsting for nocopy changes
13850                                   l_res_demand := l_avail_PO_qty; -- 2869830
13851 				  --l_res_demand := l_avail_assembly_qty;
13852 
13853                                   --(ssurendr) Bug 2865389. call get_process_effectivity
13854                                   -- again since quantity has changed. Routing
13855                                   -- and bill seq id could change in this case.
13856                                   /* ship_rec_cal
13857                                   l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
13858                                                         p_atp_record.instance_id,
13859                                                         1,
13860                                                         l_requested_ship_date,
13861                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13862                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_res_demand));*/
13863                                   l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
13864                                                         p_atp_record.instance_id,
13865                                                         l_requested_ship_date,
13866                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
13867                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_res_demand), -1);
13868                                   MSC_ATP_PROC.get_process_effectivity(
13869                                           p_plan_id,
13870                                           --PF_ITEM_INFO_REC.dest_inv_item_id,
13871                                           C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
13872                                           p_atp_record.organization_id,
13873                                           p_atp_record.instance_id,
13874                                           l_opm_start_date,
13875                                           l_res_demand,
13876                                           l_process_seq_id,
13877                                           l_routing_seq_id,
13878                                           l_bill_seq_id,
13879                                           l_op_seq_id, --4570421
13880                                           l_return_status);
13881                                      -- check the return status
13882                                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13883                                       RAISE FND_API.G_EXC_ERROR;
13884         	                  END IF;
13885                                   MSC_ATP_REQ.Get_Res_Requirements(
13886                                     p_atp_record.instance_id,
13887                                     p_plan_id,
13888                                     p_level+2,
13889                                     p_scenario_id,
13890                                     --p_atp_record.inventory_item_id,
13891                                	    l_request_item_id, -- Pass member item's id to make sure member item's id is
13892                                	                       -- inserted in assembly_item_id column in msc_resource_requirements
13893                                     p_atp_record.organization_id,
13894                                     l_pegging_id,
13895                                     l_res_demand,
13896                                     l_requested_ship_date,
13897                                     p_refresh_number,
13898                                     p_atp_record.insert_flag,
13899                                     p_search,
13900                                     p_atp_record.demand_class,
13901                                      --(ssurendr) Bug 2865389 Added Routing Sequence id
13902                                      --and Bill sequence id for OPM fix.
13903                                     l_routing_seq_id,
13904                                     l_bill_seq_id,
13905                                     p_atp_record.ship_date, -- Bug 2814872
13906                                     p_atp_record.demand_source_line,   -- CTO ODR
13907                                     l_avail_assembly_qty,
13908 			            l_res_atp_date,
13909                                     l_res_atp_period,
13910                                     l_res_atp_supply_demand,
13911                                     l_return_status );
13912 
13913                                    -- check the return status
13914                                    IF l_return_status <>
13915                                	       FND_API.G_RET_STS_SUCCESS THEN
13916                               	       RAISE FND_API.G_EXC_ERROR;
13917                                    END IF;
13918 
13919                                 END IF;
13920 
13921                                 IF --((l_atp_comp_flag = 'C') OR
13922                                   ((l_atp_comp_flag = 'C' AND nvl(l_avail_assembly_qty,0) <> 0) OR  --5283809
13923                                   (l_atp_comp_flag = 'Y')) THEN
13924                                   -- get component requirements first
13925                                   BEGIN
13926                                         MSC_ATP_REQ.Get_Comp_Requirements(
13927                                                 p_atp_record.instance_id,
13928                                                 p_plan_id,
13929                                                 p_level+2,
13930                                                 p_scenario_id,
13931                                                 l_request_item_id, -- for time_phased_atp
13932                                                 p_atp_record.organization_id,
13933                                                 l_pegging_id,
13934                                                 p_atp_record.demand_class,
13935                                                 LEAST(l_PO_qty, l_avail_assembly_qty), -- 2754446
13936                                                 --LEAST(l_net_demand, l_avail_assembly_qty),
13937                                                 l_requested_ship_date,
13938                                                 p_refresh_number,
13939                                                 p_atp_record.insert_flag,
13940                                                 p_search,
13941                                                 p_assign_set_id,
13942                                                 --(ssurendr) Bug 2865389 Added Routing Sequence id
13943                                                 --and Bill sequence id for OPM fix.
13944                                                 l_routing_seq_id,
13945                                                 l_bill_seq_id,
13946                                                 p_atp_record.inventory_item_id,      -- For time_phased_atp
13947                                                 p_atp_record.atf_date,               -- For time_phased_atp
13948                                                 p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
13949                                                 l_avail_assembly_qty,
13950                                                 l_comp_atp_date,
13951                                                 l_comp_atp_period,
13952                                                 l_comp_atp_supply_demand,
13953                                                 l_return_status,
13954                                                 --s_cto_rearch
13955                                                 l_get_comp_info_rec,
13956                                                 -- CTO Phase 1 #16, #17
13957                                                 p_atp_record.order_number,
13958                                                 l_op_seq_id --4570421
13959                                                 );
13960                                                  -- Allow creation of Sales Order for CTO comps.
13961                                   -- 2400614 : krajan
13962                                   EXCEPTION
13963                                         -- Raising exception handled below
13964                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
13965                                                 IF PG_DEBUG in ('Y', 'C') THEN
13966                                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sourcing Mismatch Error');
13967                                                 END IF;
13968                                                 p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
13969                                                 RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
13970 
13971                                         -- dsting 2764213
13972                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
13973                                              IF PG_DEBUG in ('Y', 'C') THEN
13974                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
13975                                              END IF;
13976                                              --24x7
13977                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
13978                                                 --bug 2854351:
13979                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
13980                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
13981                                              else
13982                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
13983                                              end if;
13984                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
13985                                         -- krajan : 2752705
13986                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
13987                                              IF PG_DEBUG in ('Y', 'C') THEN
13988                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item Found');
13989                                              END IF;
13990                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
13991                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
13992                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
13993                                              IF PG_DEBUG in ('Y', 'C') THEN
13994                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
13995                                              END IF;
13996                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
13997                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
13998                                   END;
13999 
14000                                   IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
14001 
14002                                         RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
14003 
14004                                   ELSIF l_return_status <>
14005                                     FND_API.G_RET_STS_SUCCESS THEN
14006                                     RAISE FND_API.G_EXC_ERROR;
14007                                   END IF;
14008 
14009                                 END IF;
14010 
14011                                 -- move the supply/demand, period atp
14012                                 -- of the resources to the output record of
14013                                 -- tables.
14014 
14015                                 -- bug 1277418
14016                                 -- we need to make sure the l_avail_assembly_qty
14017                                 -- in this second round because it is possible
14018                                 -- that due to lead time we check different
14019                                 -- comp/res that may not have enough capacities.
14020 
14021                                 IF l_avail_assembly_qty > 0 THEN
14022                                   MSC_ATP_PROC.Details_Output(l_comp_atp_period,
14023                                                l_comp_atp_supply_demand,
14024                                                x_atp_period,
14025                                                x_atp_supply_demand,
14026                                                l_return_status);
14027 
14028                                   MSC_ATP_PROC.Details_Output(l_res_atp_period,
14029                                                l_res_atp_supply_demand,
14030                                                x_atp_period,
14031                                                x_atp_supply_demand,
14032                                                l_return_status);
14033                                 ELSE
14034 
14035                                   MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14036                                                    p_plan_id,
14037                                                    null,
14038                                                    0,
14039                                                    null,
14040                                                    null,
14041                                                    null,
14042                                                    null,  -- Bug 3241766
14043                                                    --3755704: Pass start date and order date
14044                                                    l_start_date,
14045                                                    l_order_date,
14046                                                    --null,  -- Bug 3241766
14047                                                    --null,  -- Bug 3241766
14048                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14049                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14050                                 END IF;
14051                               ELSE
14052                                 -- bug 1277418: now clean up the data.
14053                                 -- we remove the whole planned order
14054                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14055                                                    p_plan_id,
14056                                                    null,
14057                                                    0,
14058                                                    null,
14059                                                    null,
14060                                                    null,
14061                                                    null,  -- Bug 3241766
14062                                                    --bug 3755704: Pass start date and order date
14063                                                    l_start_date,
14064                                                    l_order_date,
14065                                                    --null,  -- Bug 3241766
14066                                                    --null,  -- Bug 3241766
14067                                                    C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14068                                                    PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14069                                  MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14070                               END IF; -- end if l_avail_assembly_qty > 0
14071                             ELSE
14072                               -- move the supply/demand, period atp
14073                               -- of the components to the output record of
14074                               -- tables.
14075                               MSC_ATP_PROC.Details_Output(l_comp_atp_period,
14076                                              l_comp_atp_supply_demand,
14077                                              x_atp_period,
14078                                              x_atp_supply_demand,
14079                                              l_return_status);
14080 
14081                               MSC_ATP_PROC.Details_Output(l_res_atp_period,
14082                                              l_res_atp_supply_demand,
14083                                              x_atp_period,
14084                                              x_atp_supply_demand,
14085                                              l_return_status);
14086 
14087 
14088                             END IF;  -- end if the undo stuff
14089                             IF PG_DEBUG in ('Y', 'C') THEN
14090                                msc_sch_wb.atp_debug('ATP_Check: ' || 'OSFM Coproducts Backward');
14091                             END IF;
14092 			    --- OSFM Changes: update coproduct supply
14093 			    l_coproducts_flag := FND_Profile.value('MSC_PLAN_COPRODUCTS');
14094                             IF PG_DEBUG in ('Y', 'C') THEN
14095                                msc_sch_wb.atp_debug('ATP_Check: ' || 'msc_plan_coproducts = ' || l_coproducts_flag);
14096                             END IF;
14097                             --- set the sumary flag back to summary flag for p_plan_id
14098                             MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14099                             IF ((l_atp_comp_flag = 'C') OR
14100                                (l_atp_comp_flag = 'Y')) AND
14101                                l_coproducts_flag = 'Y'  AND
14102                                l_avail_assembly_qty >0 THEN
14103 
14104  				IF PG_DEBUG in ('Y', 'C') THEN
14105  				   msc_sch_wb.atp_debug('ATP_Check: ' || 'Add Coproducts for item '||p_atp_record.inventory_item_id);
14106  				END IF;
14107 			 	MSC_ATP_PROC.Add_Coproducts(
14108                                     p_plan_id,
14109                                     p_atp_record.instance_id,
14110                                     p_atp_record.organization_id,
14111                                     --bug 3766179: pass request item as inventory_item_id contains
14112                                     --PF's id in case of PF based ATP
14113                                     --p_atp_record.inventory_item_id,
14114                                     l_request_item_id,
14115                                     l_requested_ship_date,
14116                                     p_atp_record.demand_class,
14117                                     l_avail_assembly_qty,
14118                                     l_coprod_pegging_id,
14119                                     C_ITEM_INFO_REC.rounding_control_type, -- 2869830
14120                                     p_refresh_number,    -- For summary enhancement
14121                                     l_transaction_id --3766179
14122                                 );
14123 	              		IF PG_DEBUG in ('Y', 'C') THEN
14124 	              		   msc_sch_wb.atp_debug('ATP_Check: ' || 'After adding coproducts Backward');
14125 	              		END IF;
14126 
14127         		    END IF;
14128                             l_net_demand := l_net_demand-l_avail_assembly_qty;
14129 
14130                             IF l_requested_ship_date <= p_atp_record.atf_date THEN
14131                                 p_atp_record.atf_date_quantity := p_atp_record.atf_date_quantity + l_avail_assembly_qty;
14132                             END IF;
14133 
14134                             IF l_net_demand <= 0 THEN
14135                                 l_net_demand := 0;
14136                                 EXIT;
14137                             END IF;
14138                         ---diag_atp
14139                         ELSIF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
14140                             IF PG_DEBUG in ('Y', 'C') THEN
14141                                msc_sch_wb.atp_debug('ATP_Check: ' || 'inside forward_scheduling');
14142                             END IF;
14143                             -- p_sched_flag = FORWARD_SCHEDULING
14144 
14145                             -- initialize the atp date
14146                             -- although we set to sysdate now, each individual
14147                             -- routine will give the right date (or null).
14148 
14149                             l_res_atp_date := l_requested_ship_date;
14150                             l_comp_atp_date := l_requested_ship_date;
14151 
14152 				-- Bug 1554159, donot change G_COMP_LINE_ID here
14153                                 -- Bug 1532051, set G_COMP_LINE_ID again to G_ORDER_LINE_ID,
14154                                 -- just to make sure that in case of CTO, we try to get the
14155                                 -- BOM correctly from msc_bom_temp_table.
14156 
14157                                 --msc_sch_wb.atp_debug('setting G_COMP_LINE_ID = '||MSC_ATP_PVT.G_ORDER_LINE_ID);
14158 
14159                                 --MSC_ATP_PVT.G_COMP_LINE_ID := MSC_ATP_PVT.G_ORDER_LINE_ID;
14160 
14161                             -- dsting 2754446 adjust l_net_demand according to rounding_control_type
14162                             -- so our planned orders turn out right for the future case.
14163                             IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
14164                               --l_PO_qty := CEIL(l_net_demand);
14165                               --bug3397904 start
14166                               --bug 3904063: Reset the quantity only in case of demand priority allocated ATP
14167                               IF (p_search = FORWARD_SCHEDULING)
14168                                  AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
14169                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
14170                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
14171                                l_PO_qty := CEIL(p_atp_record.quantity_ordered);
14172                                IF PG_DEBUG in ('Y', 'C') THEN
14173                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
14174                                END IF;
14175                               ELSE
14176                                l_PO_qty := CEIL(l_net_demand);
14177                                IF PG_DEBUG in ('Y', 'C') THEN
14178                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
14179                                END IF;
14180                               END IF;
14181                             ELSE
14182                             --bug 3904063: Reset the quantity only in case of demand priority allocated ATP
14183                               IF (p_search = FORWARD_SCHEDULING)
14184                                  AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
14185                                  AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1
14186                                  AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
14187                                l_PO_qty := trunc(p_atp_record.quantity_ordered, 6);	--5598066
14188                                IF PG_DEBUG in ('Y', 'C') THEN
14189                                 msc_sch_wb.atp_debug('If l_PO_qty: ' || l_PO_qty);
14190                                END IF;
14191                               ELSE
14192                                l_PO_qty := trunc(l_net_demand, 6);	--5598066
14193                                IF PG_DEBUG in ('Y', 'C') THEN
14194                                 msc_sch_wb.atp_debug('Else l_PO_qty: ' || l_PO_qty);
14195                                END IF;
14196                               END IF;
14197                             END IF;
14198                             --bug3397904 end
14199                             IF ((l_atp_comp_flag = 'R') OR
14200                                 (l_atp_comp_flag = 'C')) THEN
14201                                  -- get resource requirements first
14202                                 IF PG_DEBUG in ('Y', 'C') THEN
14203                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside atp_comp_flag R or C');
14204                                 END IF;
14205 
14206                                 --(ssurendr) Bug 2865389 Call procedure get_process_effectivity
14207                                 --Details. Derive Item's Start Date by Offsetting
14208                                 --Due date with LT. Check effectivity on Start Date.
14209                                 /* ship_rec_cal
14210                                 l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.organization_id,
14211                                                         p_atp_record.instance_id,
14212                                                         1,
14213                                                         l_requested_ship_date,
14214                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14215                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty));*/
14216                                 l_opm_start_date := MSC_CALENDAR.date_offset(p_atp_record.manufacturing_cal_code,
14217                                                         p_atp_record.instance_id,
14218                                                         l_requested_ship_date,
14219                                                         -1 * (nvl(PF_ITEM_INFO_REC.fixed_lt,0) +
14220                                                               nvl(PF_ITEM_INFO_REC.variable_lt,0) * l_PO_qty), -1);
14221                                 MSC_ATP_PROC.get_process_effectivity(
14222                                         p_plan_id,
14223                                         --PF_ITEM_INFO_REC.dest_inv_item_id,
14224                                         C_ITEM_INFO_REC.dest_inv_item_id, -- For time_phased_atp
14225                                         p_atp_record.organization_id,
14226                                         p_atp_record.instance_id,
14227                                         l_opm_start_date,
14228                                         l_PO_qty,
14229                                         l_process_seq_id,
14230                                         l_routing_seq_id,
14231                                         l_bill_seq_id,
14232                                         l_op_seq_id, --4570421
14233                                         l_return_status);
14234                                 -- check the return status
14235                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
14236                                     RAISE FND_API.G_EXC_ERROR;
14237         	                END IF;
14238                                 MSC_ATP_REQ.Get_Res_Requirements(
14239                                     p_atp_record.instance_id,
14240                                     p_plan_id,
14241                                     p_level+2,
14242                                     p_scenario_id,
14243                                     --p_atp_record.inventory_item_id,
14244                                	    l_request_item_id, -- Pass member item's id to make sure member item's id is
14245                                	                       -- inserted in assembly_item_id column in msc_resource_requirements
14246                                     p_atp_record.organization_id,
14247                                     l_pegging_id,
14248                                     l_PO_qty, -- 2754446
14249                                     --l_net_demand,
14250                                     l_requested_ship_date,
14251                                     p_refresh_number,
14252                                     p_atp_record.insert_flag,
14253                                     p_search,
14254                                     p_atp_record.demand_class,
14255                                     --(ssurendr) Bug 2865389 Added Routing Sequence id
14256                                     --and Bill sequence id for OPM fix.
14257                                     l_routing_seq_id,
14258                                     l_bill_seq_id,
14259                                     p_atp_record.ship_date, -- Bug 2814872
14260                                     p_atp_record.demand_source_line,   -- CTO ODR
14261                                     l_avail_assembly_qty,
14262                                     l_res_atp_date,
14263                                     l_res_atp_period,
14264                                     l_res_atp_supply_demand,
14265                                     l_return_status );
14266 
14267                                 -- check the return status
14268                                 -- Error Handling Changes krajan
14269                                  IF l_return_status <>
14270                                	       FND_API.G_RET_STS_SUCCESS THEN
14271                               	       RAISE FND_API.G_EXC_ERROR;
14272                                  END IF;
14273 
14274                                  IF PG_DEBUG in ('Y', 'C') THEN
14275                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty = '||l_avail_assembly_qty);
14276                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_res_atp_date = '||l_res_atp_date);
14277                                  END IF;
14278 
14279 				 -- Bug 1608755, in case l_res_atp_date is null, set l_comp_atp_date
14280 				 -- to NULL so that we return single level better than going down.
14281                             	 IF l_comp_atp_date is NOT NULL and l_res_atp_date is NULL THEN
14282                                     l_comp_atp_date := NULL;
14283                                  END IF;
14284 
14285                             END IF;
14286 
14287                             -- Do comp check if res_atp_date is less than parent's infinite time fence.
14288                             -- Bug 2814872
14289                             IF ((l_atp_comp_flag = 'C' AND l_res_atp_date is not null AND
14290                                 --l_res_atp_date < p_atp_record.ship_date ) OR
14291                                 l_res_atp_date < p_atp_record.ship_date AND nvl(l_avail_assembly_qty,0) <> 0 ) OR --5283809
14292                                 (l_atp_comp_flag = 'Y')) AND
14293                                 l_res_atp_date is not null THEN
14294                                  -- we addd the  l_res_atp_date is not null
14295                                  -- condition to avoid checking components
14296                                  -- if we don't have resources availability.
14297 
14298                                  -- get component requirements first
14299 
14300                                 IF PG_DEBUG in ('Y', 'C') THEN
14301                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'inside atp_comp_flag Y or C');
14302                                 END IF;
14303                                 BEGIN
14304                                     MSC_ATP_REQ.Get_Comp_Requirements(
14305                                         p_atp_record.instance_id,
14306                                         p_plan_id,
14307                                         p_level+2,
14308                                         p_scenario_id,
14309                                         l_request_item_id, -- for time_phased_atp
14310                                         p_atp_record.organization_id,
14311                                         l_pegging_id,
14312                                         p_atp_record.demand_class,
14313                                         l_PO_qty, -- 2754446
14314                                         --l_net_demand,
14315                                         l_requested_ship_date,
14316   				        p_refresh_number,
14317                                         p_atp_record.insert_flag,
14318                                         p_search,
14319                                         p_assign_set_id,
14320                                         --(ssurendr) Bug 2865389 Added Routing Sequence id
14321                                         --and Bill sequence id for OPM fix.
14322                                         l_routing_seq_id,
14323                                         l_bill_seq_id,
14324                                         p_atp_record.inventory_item_id,      -- For time_phased_atp
14325                                         p_atp_record.atf_date,               -- For time_phased_atp
14326                                         p_atp_record.manufacturing_cal_code, -- For ship_rec_cal
14327                                         l_avail_assembly_qty,
14328                                         l_comp_atp_date,
14329                                         l_comp_atp_period,
14330                                         l_comp_atp_supply_demand,
14331                                         l_return_status,
14332                                         --s_cto_rearch
14333                                         l_get_comp_info_rec,
14334                                         -- CTO Phase 1 #16, #17
14335                                         p_atp_record.order_number,
14336                                         l_op_seq_id --4570421
14337                                         );
14338                                         -- Allow creation of Sales Order for CTO comps.
14339                                  -- 2400614 : krajan
14340                                  EXCEPTION
14341                                         WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
14342                                              p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
14343                               	             IF PG_DEBUG in ('Y', 'C') THEN
14344                               	                msc_sch_wb.atp_debug('ATP_Check: ' || 'Source Mismatch Error');
14345                               	             END IF;
14346 	                                     RAISE MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH;
14347 
14348                                         -- dsting 2764213
14349                                         WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
14350                                              IF PG_DEBUG in ('Y', 'C') THEN
14351                                                 msc_sch_wb.atp_debug('ATP_Check: Plan not found Error');
14352                                              END IF;
14353                                              --24x7
14354                                              if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
14355                				        --bug 2854351
14356                                                 --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
14357                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
14358                                              else
14359                                                 p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
14360                                              end if;
14361                                              RAISE MSC_ATP_PVT.EXC_NO_PLAN_FOUND;
14362 
14363                                         -- krajan : 2752705
14364                                         WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
14365                                              IF PG_DEBUG in ('Y', 'C') THEN
14366                                                 msc_sch_wb.atp_debug('ATP_Check: Uncollected Item');
14367                                              END IF;
14368                                              p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
14369                                              RAISE MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM;
14370 
14371                                         WHEN MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL THEN --bug3583705
14372                                              IF PG_DEBUG in ('Y', 'C') THEN
14373                                                 msc_sch_wb.atp_debug ('ATP_Check:NO_MATCHING_DATE_IN_CAL');
14374                                              END IF;
14375                                              p_atp_record.error_code := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
14376                                              RAISE MSC_ATP_PVT.NO_MATCHING_DATE_IN_CAL;
14377                                  END;
14378 
14379                                  IF PG_DEBUG in ('Y', 'C') THEN
14380                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_avail_assembly_qty = '||l_avail_assembly_qty);
14381                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_comp_atp_date = '||l_comp_atp_date);
14382                                  END IF;
14383 
14384 				 -- Bug 1608755, in case l_comp_atp_date is null, set l_res_atp_date
14385 				 -- to NULL so that we return single level better than going down.
14386                             	 IF l_comp_atp_date is NULL and l_res_atp_date is NOT NULL THEN
14387                                     l_res_atp_date := NULL;
14388                                  END IF;
14389 
14390                             END IF;
14391 
14392                             -- check the return status
14393                             IF l_return_status = MSC_ATP_PVT.CTO_OSS_ERROR THEN
14394 
14395                                  RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
14396                             ELSIF l_return_status <>
14397                                    FND_API.G_RET_STS_SUCCESS THEN
14398                                 RAISE FND_API.G_EXC_ERROR;
14399                             END IF;
14400 
14401                             IF PG_DEBUG in ('Y', 'C') THEN
14402                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level ='||l_atp_date_this_level);
14403                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_comp_atp_date ='||l_comp_atp_date);
14404                                msc_sch_wb.atp_debug('ATP_Check: ' || 'l_res_atp_date ='||l_res_atp_date);
14405                                msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date ='||p_atp_record.ship_date);
14406                             END IF;
14407 
14408                             -- Bug 1449555
14409                             IF l_comp_atp_date is NOT NULL and
14410                                 l_res_atp_date is NOT NULL THEN
14411                                 l_temp_date := GREATEST(l_comp_atp_date, l_res_atp_date);
14412                             ELSIF l_comp_atp_date is NOT NULL and
14413                                 l_res_atp_date is NULL THEN
14414                                 l_temp_date := l_comp_atp_date;
14415                             ELSIF l_comp_atp_date is NULL and
14416                                 l_res_atp_date is NOT NULL THEN
14417                                 l_temp_date := l_res_atp_date;
14418                             ELSE
14419                                 l_temp_date := p_atp_record.ship_date;
14420                             END IF;
14421 
14422                             -- 2859130 move to next work date for forward pass
14423                             l_temp_date := MSC_CALENDAR.NEXT_WORK_DAY(
14424                                p_atp_record.organization_id,
14425                                p_atp_record.instance_id,
14426                                1,
14427                                l_temp_date);
14428 
14429                             ---bug 2798667: The p_atp_record.ship_date for option item contains ship date for
14430                             --- model from earlier source + atp lead time. We should compare with ship date +
14431                             --- atp lead time from this source and not just ship date
14432                             /* s_cto_rearch: do not honor ATP lead time
14433                             IF NVL(p_atp_record.atp_lead_time, 0) > 0 THEN
14434                               l_atp_lt_offset_ship_date :=
14435                                   MSC_CALENDAR.DATE_OFFSET(
14436                                   p_atp_record.organization_id,
14437                                   p_atp_record.instance_id,
14438                                   1,
14439                                   --GREATEST(l_comp_atp_date, l_res_atp_date),
14440                                   l_temp_date,
14441                                   NVL(p_atp_record.atp_lead_time, 0));
14442                             ELSE
14443                                l_atp_lt_offset_ship_date := l_temp_date;
14444                             END IF;
14445                             */
14446                             l_atp_lt_offset_ship_date := l_temp_date;
14447 
14448                             IF PG_DEBUG in ('Y', 'C') THEN
14449                                msc_sch_wb.atp_debug('l_atp_lt_offset_ship_date := ' || l_atp_lt_offset_ship_date);
14450                             END IF;
14451 
14452 
14453                             ---bug 2798667: should be comparing with l_atp_lt_offset_ship_date instead of l_temp_date
14454                              /*IF (p_atp_record.ship_date IS NULL AND l_temp_date IS NULL) OR
14455                                 (p_atp_record.ship_date <= l_temp_date) THEN
14456                             */
14457                             IF (p_atp_record.ship_date IS NULL AND l_atp_lt_offset_ship_date IS NULL) OR
14458                                 (p_atp_record.ship_date <= l_atp_lt_offset_ship_date) THEN
14459                 /*
14460                             IF p_atp_record.ship_date <=
14461                                 NVL(GREATEST(l_comp_atp_date, l_res_atp_date),
14462                                 p_atp_record.ship_date) THEN
14463                 */
14464 
14465                                 -- single level result is better than
14466                                 -- going down to next level.
14467 
14468                                 IF PG_DEBUG in ('Y', 'C') THEN
14469                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'single level results is better than multi-level');
14470                                 END IF;
14471 
14472 
14473                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14474                                                  p_plan_id,
14475                                                  p_atp_record.ship_date,
14476                                                  0,
14477                                                  null,
14478                                                  null,
14479                                                  null,
14480                                                  null,  -- Bug 3241766
14481                                                  --bug 3755704: Pass start date and order date
14482                                                  l_start_date,
14483                                                  l_order_date,
14484                                                  --null,  -- Bug 3241766
14485                                                  --null,  -- Bug 3241766
14486                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14487                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14488                                 IF PG_DEBUG in ('Y', 'C') THEN
14489                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'x_atp_supply_demand.level.count = '||
14490 						x_atp_supply_demand.level.count);
14491                                 END IF;
14492 
14493                             ELSE
14494  				-- going down is better than the results
14495                                 -- in this level
14496 
14497                                 -- remove what's bad
14498                                 --- subst: remember this pegging as we might need to undo
14499                                 --- this pegging in ATP_CHECK_SUBST
14500                                 MSC_ATP_PVT.G_FUTURE_PEGGING_ID := l_pegging_id;
14501 
14502                                 IF PG_DEBUG in ('Y', 'C') THEN
14503                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_pegging_tab : ' || l_atp_pegging_tab.COUNT);
14504                                 END IF;
14505 
14506                                 IF l_atp_pegging_tab.COUNT > 0 THEN
14507 
14508                                    -- Planned Pegging based AATP, l_atp_pegging_tab populated in
14509                                    -- MSC_AATP_REQ.Get_Forward_Material_Atp
14510 
14511                                    MSC_ATP_DB_UTILS.Remove_Invalid_Future_SD(l_atp_pegging_tab);
14512                                    l_atp_pegging_tab := MRP_ATP_PUB.Number_Arr(); --bug3581151
14513 
14514                                 ELSE            -- IF l_atp_pegging_tab.COUNT > 0 THEN
14515 
14516                                    MSC_ATP_DB_UTILS.Update_Planned_Order(l_future_pegging_id,
14517                                                  p_plan_id,
14518                                                  p_atp_record.ship_date,
14519                                                  0,
14520                                                  null,
14521                                                  null,
14522                                                  null,
14523                                                  null,  -- Bug 3241766
14524                                                  --bug 3755704: Pass start date and order date
14525                                                  l_start_date,
14526                                                  l_order_date,
14527                                                  --null,  -- Bug 3241766
14528                                                  --null,  -- Bug 3241766
14529                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14530                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14531                                 END IF;            -- IF l_atp_pegging_tab.COUNT > 0 THEN
14532 
14533                                 --bug 2798667: This value is already in l_atp_lt_offset_ship_date
14534                                 /* p_atp_record.ship_date :=
14535                                   MSC_CALENDAR.DATE_OFFSET(
14536                                   p_atp_record.organization_id,
14537                                   p_atp_record.instance_id,
14538                                   1,
14539                                   --GREATEST(l_comp_atp_date, l_res_atp_date),
14540                                   l_temp_date,
14541                                   NVL(p_atp_record.atp_lead_time, 0));
14542                                 */
14543                                 p_atp_record.ship_date := l_atp_lt_offset_ship_date;
14544 
14545 
14546                                 IF PG_DEBUG in ('Y', 'C') THEN
14547                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.ship_date ='||p_atp_record.ship_date);
14548                                 END IF;
14549                                 -- we will say the available quantity is
14550                                 -- exact the quantity we want to order.
14551                                 -- although this is not correct (
14552                                 -- it should be the quantity we get from next
14553                                 -- level plus what we can have on the parent
14554                                 -- level.  But now we don't want to search
14555 
14556                                 p_atp_record.available_quantity :=
14557                                   p_atp_record.quantity_ordered;
14558 
14559                                 IF p_atp_record.ship_date <= p_atp_record.atf_date THEN
14560                                     p_atp_record.atf_date_quantity := p_atp_record.quantity_ordered;
14561                                 ELSE
14562                                     p_atp_record.atf_date_quantity := 0;
14563                                 END IF;
14564 
14565                                 -- bug 1247433: we don't need to update
14566                                 -- the quantity
14567 
14568                                 --bug 3755704: Calculate order date and start date based on new date
14569 
14570                                 IF l_net_processing_lt > 0 THEN
14571                                    IF PG_DEBUG in ('Y', 'C') THEN
14572                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate order date');
14573                                    END IF;
14574                                    -- since it is a make case only calendar being used is
14575                                    --p_atp_record.manufacturing_cal_code
14576                                    l_order_date := MSC_CALENDAR.DATE_OFFSET(
14577                                                                 p_atp_record.manufacturing_cal_code,
14578                                                                 p_atp_record.instance_id,
14579                                                                 l_temp_date,
14580                                                                 (-1 * l_net_processing_lt), -1);
14581 
14582                                 ELSE
14583                                    IF PG_DEBUG in ('Y', 'C') THEN
14584                                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Net process lt is zero');
14585                                    END IF;
14586                                    l_order_date := l_temp_date;
14587                                 END IF;
14588 
14589                                 IF PG_DEBUG in ('Y', 'C') THEN
14590                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_order_date := ' || l_order_date);
14591                                 END IF;
14592                                 -- calculate start date only if PTF_flag is enabled for the plan
14593                                 IF (l_process_lt > 0) THEN
14594                                    IF PG_DEBUG in ('Y', 'C') THEN
14595                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'Calculate start date');
14596                                    END IF;
14597                                    l_start_date := MSC_CALENDAR.DATE_OFFSET(
14598                                                                 p_atp_record.manufacturing_cal_code,
14599                                                                 p_atp_record.instance_id,
14600                                                                 l_temp_date,
14601                                                                 (-1 * l_process_lt), -1);
14602                                 ELSE
14603 
14604                                    IF PG_DEBUG in ('Y', 'C') THEN
14605                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'process LT is 0');
14606                                    END IF;
14607                                    l_start_date := l_temp_date;
14608                                 END IF;
14609 
14610                                 IF PG_DEBUG in ('Y', 'C') THEN
14611                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'l_start_date := ' || l_start_date);
14612                                 END IF;
14613                                 --  end bug 3755704
14614 
14615                                 MSC_ATP_DB_UTILS.Update_Planned_Order(l_pegging_id,
14616                                                  p_plan_id,
14617                                                  --bug 2798667: this should be l_temp_date
14618                                                  --p_atp_record.ship_date,
14619                                                  l_temp_date,
14620                                                  null,
14621                                                  null,
14622                                                  null,
14623                                                  null,
14624                                                  null,  -- Bug 3241766
14625                                                  --bug 3755704: populate start date and order date
14626                                                  l_start_date,
14627                                                  l_order_date,
14628                                                  --null,  -- Bug 3241766
14629                                                  --null,  -- Bug 3241766
14630                                                  C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14631                                                  PF_ITEM_INFO_REC.dest_inv_item_id); -- Bug 3293163
14632 
14633                                 -- bug 1214740
14634                                 l_future_atp_period := l_comp_atp_period;
14635                                 l_future_atp_supply_demand :=
14636                                                  l_comp_atp_supply_demand;
14637                                 l_future_pegging_id := l_pegging_id;
14638 
14639                                 MSC_ATP_PROC.Details_Output(l_res_atp_period,
14640                                                l_res_atp_supply_demand,
14641                                                l_future_atp_period,
14642                                                l_future_atp_supply_demand,
14643                                                l_return_status);
14644  			        IF PG_DEBUG in ('Y', 'C') THEN
14645  			           msc_sch_wb.atp_debug('ATP_Check: ' || 'OSFM Coproducts Forward ');
14646  			        END IF;
14647                                 --- reset the sumamry flag back to summary flag corresponding to p_plan_id
14648                                 MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14649                             	  --- OSFM Changes: update coproduct supply
14650                                 l_coproducts_flag := FND_Profile.value('MSC_PLAN_COPRODUCTS');
14651                                 IF PG_DEBUG in ('Y', 'C') THEN
14652                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'msc_plan_coproducts = ' || l_coproducts_flag);
14653                                 END IF;
14654                                 IF ((l_atp_comp_flag = 'C') OR
14655                                 	(l_atp_comp_flag = 'Y')) AND
14656                                	l_coproducts_flag = 'Y' THEN
14657 
14658                               	  IF PG_DEBUG in ('Y', 'C') THEN
14659                               	     msc_sch_wb.atp_debug('ATP_Check: ' || 'Add Coproducts for item '||p_atp_record.inventory_item_id);
14660                               	  END IF;
14661                               	  MSC_ATP_PROC.Add_Coproducts(
14662                                                 p_plan_id,
14663                                                 p_atp_record.instance_id,
14664                                                 p_atp_record.organization_id,
14665                                                 --p_atp_record.inventory_item_id,
14666                                                 l_request_item_id, -- for time_phased_atp
14667                                                 l_temp_date,
14668                                                 p_atp_record.demand_class,
14669                                                 p_atp_record.quantity_ordered,
14670                                                 l_coprod_pegging_id,
14671                                                 C_ITEM_INFO_REC.rounding_control_type, -- 2869830
14672                                                 p_refresh_number,    -- For summary enhancement
14673                                                 l_transaction_id --3766179
14674                                    );
14675 
14676                                    IF PG_DEBUG in ('Y', 'C') THEN
14677                                       msc_sch_wb.atp_debug('ATP_Check: ' || 'After adding coproducts Forward');
14678                                    END IF;
14679                             	  END IF;
14680 
14681                              END IF;
14682 
14683                         END IF; -- (p_sched_flag = BACKWARD_SCHEDULING)
14684 
14685                     END IF;  -- it is a make or buy or transfer
14686                     j := l_sources.source_type.NEXT(j);
14687                 END LOOP;  -- end the loop of the l_sources
14688                 --4902658/5216528 Fix
14689                                 /*
14690                  Update the pegging with the ATP+CTP Quantity of the pegging.
14691                  l_temp_net_demand -  l_net_demand; gives us the CTP quantity
14692                  */
14693                 IF  l_substitutes_rec.inventory_item_id.Count  > 1 THEN
14694                    l_atp_ctp_qty := l_temp_net_demand -  l_net_demand +  l_substitutes_rec.sub_atp_qty(l_index);
14695 
14696                    IF ( ((l_temp_net_demand - l_net_demand) <> 0)
14697                       AND (l_substitutes_rec.sub_atp_qty(l_index) = 0)
14698                       AND (l_index >= 2) ) THEN
14699 
14700                    --5283809 , adding the demand in case ATP qty for substitute is 0 but CTP not zero.
14701                    IF PG_DEBUG in ('Y', 'C') THEN
14702                       msc_sch_wb.atp_debug('ATP_Check: ' || 'adding the demand in case ATP qty for substitute is 0 but CTP not zero');
14703                       msc_sch_wb.atp_debug('ATP_Check: ' || 'ATP QTY : ' || l_substitutes_rec.sub_atp_qty(l_index) );
14704                       msc_sch_wb.atp_debug('ATP_Check: ' || 'CTP QTY : ' || l_atp_ctp_qty );
14705                    END IF;
14706 
14707 
14708                    IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
14709                       MSC_ATP_DB_UTILS.Update_Pegging( l_substitutes_rec.pegging_id(l_index),
14710                                                        null,
14711                                                        l_atp_ctp_qty );
14712                    END IF;
14713 
14714                    l_atp_insert_rec.instance_id := p_atp_record.instance_id;
14715                    l_atp_insert_rec.inventory_item_id := l_substitutes_rec.pf_item_id(l_index);
14716                    l_atp_insert_rec.request_item_id := l_substitutes_rec.inventory_item_id(l_index);
14717                    l_atp_insert_rec.atf_date_quantity := l_substitutes_rec.atf_date_quantity(l_index);
14718                    l_atp_insert_rec.organization_id := p_atp_record.organization_id;
14719                    l_atp_insert_rec.identifier := p_atp_record.identifier;
14720                    l_atp_insert_rec.demand_source_type:=
14721                           nvl(p_atp_record.demand_source_type, 2);
14722                    l_atp_insert_rec.demand_source_header_id :=
14723                             nvl(p_atp_record.demand_source_header_id, -1);
14724                    l_atp_insert_rec.demand_source_delivery :=
14725                             p_atp_record.demand_source_delivery;
14726                    l_atp_insert_rec.quantity_ordered:= LEAST(
14727                                                             l_atp_ctp_qty,
14728                                                             l_substitutes_rec.quantity_ordered(l_index));
14729                    l_atp_insert_rec.requested_ship_date := p_atp_record.requested_ship_date;
14730                    l_atp_insert_rec.demand_class := p_atp_record.demand_class;
14731                    l_atp_insert_rec.refresh_number := p_refresh_number;
14732                    l_atp_insert_rec.order_number := p_atp_record.order_number;
14733                    l_atp_insert_rec.origination_type := 1;
14734 
14735                    MSC_ATP_DB_UTILS.Add_Mat_Demand(l_atp_insert_rec,
14736                               l_plan_id,
14737                               0,
14738                               l_demand_id);
14739                    IF PG_DEBUG in ('Y', 'C') THEN
14740                       msc_sch_wb.atp_debug('ATP_Check : demand id     :' || l_demand_id );
14741                    END IF;
14742 
14743                    --5553953
14744                    --in order to update the demand id for the pegging line, otherwise demand will remain in the system
14745                    UPDATE mrp_atp_details_temp
14746                    SET   identifier3 = l_demand_id
14747                    WHERE pegging_id = l_substitutes_rec.pegging_id(l_index)
14748                    AND   session_id = MSC_ATP_PVT.G_SESSION_ID
14749                    AND   record_type = 3;
14750 
14751                  ELSE
14752                      IF NVL(MSC_ATP_PVT.G_DIAGNOSTIC_ATP,2) <> 1 THEN
14753                    	  MSC_ATP_DB_UTILS.Update_Pegging( l_substitutes_rec.pegging_id(l_index),
14754                                   null,
14755                                   l_atp_ctp_qty );
14756                      END IF;
14757                      update msc_demands
14758                         set    USING_REQUIREMENT_QUANTITY = MSC_ATP_UTILS.Truncate_Demand(l_atp_ctp_qty)  -- 5598066
14759                       where  demand_id = l_substitutes_rec.demand_id(l_index)
14760                         and    plan_id = p_plan_id
14761                                returning inventory_item_id
14762                        into l_inventory_item_id;
14763                    IF PG_DEBUG in ('Y', 'C') THEN
14764                       msc_sch_wb.atp_debug('ATP_Check : l_atp_ctp_qty :' || l_atp_ctp_qty);
14765                       msc_sch_wb.atp_debug('ATP_Check : l_tem_net_dem :' || l_temp_net_demand);
14766                       msc_sch_wb.atp_debug('ATP_Check : l_net_demand  :' || l_net_demand);
14767                       msc_sch_wb.atp_debug('ATP_Check : l_sub_atp_qty :' || l_substitutes_rec.sub_atp_qty(l_index));
14768                       msc_sch_wb.atp_debug('ATP_Check : pegging_id    :' || l_substitutes_rec.pegging_id(l_index));
14769                       msc_sch_wb.atp_debug('ATP_Check : demand id     :' || l_substitutes_rec.demand_id(l_index));
14770                    END IF;
14771                    IF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
14772                         AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
14773                         update msc_alloc_demands
14774                            set allocated_quantity = l_atp_ctp_qty
14775                          where parent_demand_id = l_substitutes_rec.demand_id(l_index) --l_demand_id
14776                                --where parent_demand_id = l_demand_id
14777                            and plan_id = p_plan_id;
14778                    END IF;
14779                    IF PG_DEBUG in ('Y', 'C') THEN
14780                       msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
14781                       msc_sch_wb.atp_debug('ATP_Check: ' || ' MSC_ATP_PVT.G_SUMMARY_FLAG := ' ||  MSC_ATP_PVT.G_SUMMARY_FLAG);
14782                    END IF;
14783                    -- update summary records removed in ODS case - for summary enhancement
14784                    IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
14785                       MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
14786                                                          p_plan_id,
14787                                                          p_atp_record.organization_id,
14788                                                          l_inventory_item_id,
14789                                                          l_requested_ship_date,
14790                                                          null,
14791                                                          null,
14792                                                          null,
14793                                                          null,
14794                                                          null,
14795                                                          null,
14796                                                          2);
14797                         update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
14798                            set sd_qty = sd_qty + l_atp_ctp_qty
14799                          where plan_id = p_plan_id
14800                            and sr_instance_id  = p_atp_record.instance_id
14801                            and inventory_item_id = l_inventory_item_id
14802                            and organization_id = p_atp_record.organization_id
14803                            and sd_date = trunc(l_requested_ship_date);
14804                       commit;
14805                    END IF;
14806                    IF PG_DEBUG in ('Y', 'C') THEN
14807                       msc_sch_wb.atp_debug('Substitute Loop Ends with l_index := ' || l_index);
14808                    END IF;
14809                 END IF;
14810                END IF;
14811              END LOOP; -- for subs array
14812 
14813                   p_atp_record.request_item_id :=     l_substitutes_rec.inventory_item_id(1);
14814                   l_parent_pegging_id          :=     l_tmp_parent_pegging_id;
14815                   l_pegging_id                 :=     l_substitutes_rec.inventory_item_id(1);
14816                   -- Re initialize the C_item_info_rec G_item_info_rec
14817                   MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
14818                                                     p_plan_id,
14819                                                     p_atp_record.request_item_id,
14820                                                     p_atp_record.organization_id,
14821                                                     l_item_info_rec );
14822                 		/* Make a copy */
14823                   C_ITEM_INFO_REC := G_ITEM_INFO_REC;
14824 
14825                   if l_substitutes_rec.inventory_item_id.count > 1 then
14826                      PF_ITEM_INFO_REC := G_ITEM_INFO_REC;
14827                   end if;
14828                   -- 4902658/5216528 ends update
14829 		-- dsting diag_atp update the last good pegging record to make supply/demand match
14830 		IF PG_DEBUG in ('Y', 'C') THEN
14831 			msc_sch_wb.atp_debug('diag_atp l_pegging_id ' || l_pegging_id);
14832 			msc_sch_wb.atp_debug('last good pegging ' || l_last_good_pegging_id);
14833 			msc_sch_wb.atp_debug('net_demand ' || l_net_demand);
14834 			msc_sch_wb.atp_debug('subst last good pegging ' || MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING);
14835 		END IF;
14836 
14837 		IF MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y'
14838 		   AND l_last_good_pegging_id is NULL
14839 		   AND MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING is not null
14840 		   AND p_atp_record.top_tier_org_flag = 1
14841 		THEN
14842 		   l_last_good_pegging_id := MSC_ATP_SUBST.G_TOP_LAST_PO_PEGGING;
14843 		   l_subst_existing_PO_qty := MSC_ATP_SUBST.G_TOP_LAST_PO_QTY;
14844 		ELSE
14845 		   l_subst_existing_PO_qty := 0;
14846 		END IF;
14847 
14848 		IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 AND
14849 		   l_net_demand > 0 AND
14850 		   l_pegging_id <> l_last_good_pegging_id
14851 		THEN
14852 		   -- bug 5598066   added the following to honour rounding_control_type
14853 		      IF NVL(C_ITEM_INFO_REC.rounding_control_type, 2) = 1 THEN
14854                          l_net_demand_qty := FLOOR(l_net_demand_qty);
14855 			 l_subst_existing_PO_qty := FLOOR(l_subst_existing_PO_qty);
14856 
14857 			 IF PG_DEBUG in ('Y', 'C') THEN
14858 			 msc_sch_wb.atp_debug('Floor of l_net_demand_qty '||l_net_demand_qty);
14859 			 msc_sch_wb.atp_debug('Floor of l_subst_existing_PO_qty '||l_subst_existing_PO_qty);
14860                          END IF;
14861 		       ELSE
14862                            l_net_demand_qty := trunc(l_net_demand_qty, 6);
14863 			   l_subst_existing_PO_qty := trunc(l_subst_existing_PO_qty, 6);
14864 
14865 			   IF PG_DEBUG in ('Y', 'C') THEN
14866 			   msc_sch_wb.atp_debug('trunc of l_net_demand_qty '||l_net_demand_qty);
14867 			   msc_sch_wb.atp_debug('trunc of l_subst_existing_PO_qty '||l_subst_existing_PO_qty);
14868                            END IF;
14869 			END IF;
14870 
14871 			MSC_ATP_DB_UTILS.Update_Planned_Order(l_last_good_pegging_id,
14872 					p_plan_id,
14873 					null,
14874 					l_net_demand + l_subst_existing_PO_qty,
14875 					null,
14876 					null,
14877 					null,
14878                                         null,  -- Bug 3241766
14879                                         null,  -- Bug 3241766
14880                                         null,  -- Bug 3241766
14881                                         C_ITEM_INFO_REC.dest_inv_item_id,   -- Bug 3293163
14882                                         PF_ITEM_INFO_REC.dest_inv_item_id,  -- Bug 3293163
14883 					3);
14884 		END IF;
14885 
14886                 MSC_ATP_PVT.G_SUMMARY_FLAG := l_summary_flag;
14887                 IF PG_DEBUG in ('Y', 'C') THEN
14888                    msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
14889                 END IF;
14890 
14891                 --- adjust the demand here
14892                 IF (l_net_demand > 0) and (p_search =1) and (p_parent_pegging_id is null) THEN
14893 
14894 		  -- dsting diag_atp. do not adjust the demands for diagnostic ATP
14895 
14896 		  IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
14897                     update msc_demands
14898                     set    USING_REQUIREMENT_QUANTITY =
14899                            MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-l_net_demand))  -- 5598066
14900                     where demand_id = l_demand_id
14901                     and plan_id = p_plan_id
14902                     returning  inventory_item_id into l_inventory_item_id;
14903 
14904                     /* time_phased_atp*/
14905                     IF l_time_phased_atp = 'Y' THEN
14906                          --using the same insert rec we prepared earlier
14907                          l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered - l_net_demand;
14908                          l_atp_insert_rec.requested_date_quantity := l_atp_insert_rec.quantity_ordered;
14909                          l_atp_insert_rec.atf_date_quantity := GREATEST(NVL(p_atp_record.atf_date_quantity,
14910                                                                                 p_atp_record.requested_date_quantity), 0);
14911                          MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
14912                                 l_atp_insert_rec,
14913                                 p_plan_id,
14914                                 l_demand_id,
14915                                 l_return_status
14916                          );
14917                          IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
14918                            IF PG_DEBUG in ('Y', 'C') THEN
14919                                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
14920                            END IF;
14921                            RAISE FND_API.G_EXC_ERROR;
14922                          END IF;
14923                     ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
14924                          AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
14925 
14926                          update msc_alloc_demands
14927                          set allocated_quantity  = allocated_quantity -l_net_demand
14928                          where parent_demand_id = l_demand_id
14929                          and   plan_id = p_plan_id;
14930                     END IF;
14931 
14932                     IF PG_DEBUG in ('Y', 'C') THEN
14933                        msc_sch_wb.atp_debug('ATP_Check: ' || 'Check This scenario in backward case');
14934                        msc_sch_wb.atp_debug('ATP_Check: ' || 'MSC_ATP_PVT.G_SUMMARY_FLAG' || MSC_ATP_PVT.G_SUMMARY_FLAG);
14935                     END IF;
14936 
14937                     -- update summary records removed in ODS cases - for summary enhancement
14938                     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
14939                         IF PG_DEBUG in ('Y', 'C') THEN
14940                            msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
14941                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
14942                            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
14943                         END IF;
14944 
14945                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
14946                                                                 p_plan_id,
14947                                                                 p_atp_record.organization_id,
14948                                                                 l_inventory_item_id,
14949                                                                 l_requested_ship_date,
14950                                                                 null,
14951                                                                 null,
14952                                                                 null,
14953                                                                 null,
14954 							        null,
14955  							        null,
14956                                                                 2);
14957 
14958                         update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
14959                         set sd_qty = sd_qty + l_net_demand
14960                         where plan_id = p_plan_id
14961                         and  inventory_item_id = l_inventory_item_id
14962                         and  sr_instance_id = p_atp_record.instance_id
14963                         and  organization_id = p_atp_record.organization_id
14964                         and  sd_date = trunc(l_requested_ship_date);
14965                         commit;
14966                     END IF;
14967 
14968    		  END IF; -- not DIAGNOSTIC ATP
14969 
14970                 END IF;
14971 
14972                 -- now we have the request date quantity from next level
14973 
14974                 -- cchen : 1238941
14975                 -- p_atp_record.requested_date_quantity:=
14976                 --     p_atp_record.quantity_ordered-l_net_demand;
14977 
14978                 p_atp_record.combined_requested_date_qty:=
14979                     p_atp_record.quantity_ordered-l_net_demand;
14980 
14981                 p_atp_record.requested_date_quantity:=
14982                     p_atp_record.quantity_ordered-l_net_demand-
14983                     greatest(l_sub_requested_date_quantity,0);
14984 
14985                /* rajjain changes for re-review comments
14986                   removed check for rule based AATP as this should be set
14987                   for all scenarios
14988                IF MSC_ATP_PVT.G_ALLOCATION_METHOD = 2 THEN
14989                    p_atp_record.used_available_quantity :=
14990                      l_used_available_quantity;
14991                END IF; --bug3409973*/
14992                 p_atp_record.used_available_quantity :=
14993                     l_used_available_quantity;
14994 
14995                 IF PG_DEBUG in ('Y', 'C') THEN
14996                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.requested_date_quantity:'||
14997                     	p_atp_record.requested_date_quantity);
14998                    msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.combined_requested_date_qty:'||
14999 			p_atp_record.combined_requested_date_qty);
15000 	           msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.atp_date_quantity_this_level:'||
15001 			l_used_available_quantity);
15002                 END IF;
15003 
15004                 -- now we want to find the future date and quantity
15005                 -- if we still have net demand, otherwise the atp date
15006                 -- and quantity will be the same as the request date and
15007                 -- request date quantity.
15008 
15009 
15010                 IF (l_net_demand = 0) THEN
15011                     /* s_cto_rearch: Do not honor atp lead time
15012                     p_atp_record.ship_date :=  MSC_CALENDAR.DATE_OFFSET
15013                                  (p_atp_record.organization_id,
15014                                   p_atp_record.instance_id,
15015                                   1,
15016                                   l_requested_ship_date,
15017                                   NVL(p_atp_record.atp_lead_time, 0));
15018                     */
15019                     p_atp_record.ship_date := l_requested_ship_date;
15020                     -- cchen : 1238941
15021                     -- p_atp_record.available_quantity :=
15022                     --    p_atp_record.requested_date_quantity;
15023 
15024                     p_atp_record.available_quantity :=
15025                         p_atp_record.combined_requested_date_qty;
15026 
15027                     -- Update bucketed demands
15028                     IF l_time_phased_atp = 'Y' THEN
15029                             l_atp_insert_rec.quantity_ordered :=  p_atp_record.quantity_ordered;
15030                             l_atp_insert_rec.requested_date_quantity := p_atp_record.combined_requested_date_qty;
15031                             l_atp_insert_rec.atf_date_quantity := GREATEST(NVL(p_atp_record.atf_date_quantity,
15032                                                                                    p_atp_record.combined_requested_date_qty), 0);
15033                             MSC_ATP_PF.Increment_Bucketed_Demands_Qty(
15034                                    l_atp_insert_rec,
15035                                    p_plan_id,
15036                                    l_demand_id,
15037                                    l_return_status
15038                             );
15039                             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15040                               IF PG_DEBUG in ('Y', 'C') THEN
15041                                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Increment_Bucketed_Demands_Qty');
15042                               END IF;
15043                               RAISE FND_API.G_EXC_ERROR;
15044                             END IF;
15045                     END IF;
15046                 ELSE
15047                     -- if this is the top node, we need to turn on a flag so
15048                     -- that we can find the future date.
15049 
15050                     IF p_search = 1 AND p_parent_pegging_id is null THEN
15051                       IF PG_DEBUG in ('Y', 'C') THEN
15052                          msc_sch_wb.atp_debug('ATP_Check: ' || 'not enough, set G_FIND_FUTURE_DATE=Y');
15053                       END IF;
15054 
15055                       -- 1275751
15056                       p_atp_record.error_code := ATP_REQ_DATE_FAIL;
15057 
15058                       -- 1228607
15059                       --- This code has been moved before stealing
15060                       ---  so that demand record is correct before stealing takes place
15061                       /*update msc_demands
15062                       set    USING_REQUIREMENT_QUANTITY =
15063                              (USING_REQUIREMENT_QUANTITY-l_net_demand)
15064                       where demand_id = l_demand_id
15065                       and plan_id = p_plan_id;*/
15066 
15067                       MSC_ATP_PVT.G_FIND_FUTURE_DATE := 'Y';
15068                       MSC_ATP_PVT.G_ASSEMBLY_LINE_ID := p_atp_record.Identifier;
15069                       MSC_ATP_PVT.G_COMP_LINE_ID := p_atp_record.Identifier;
15070                     ELSIF p_search = 1 AND
15071                       --subst: In case of substitution we do not want to adjust the planned order for top org
15072                       NOT (NVL(p_atp_record.original_item_flag, 2) = 1 AND
15073                                     MSC_ATP_PVT.G_SUBSTITUTION_FLAG = 'Y' AND
15074                                     p_atp_record.top_tier_org_flag = 1) THEN
15075                       --diag_atp: we do not adjust the demand in case of diagnostic ATP, because we place the
15076                       -- same demand as we need.
15077                       IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
15078                          -- adjust the demand record
15079                          IF PG_DEBUG in ('Y', 'C') THEN
15080                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this scenario, bacward case 2');
15081                             msc_sch_wb.atp_debug('ATP_Check: ' || 'adjust demand record, l_parent_pegging_id = '||l_parent_pegging_id);
15082                          END IF;
15083                          /* bug 1235225
15084                          update mrp_atp_details_temp
15085                          set    supply_demand_quantity =
15086                                 (supply_demand_quantity - l_net_demand)
15087                          where  session_id = MSC_ATP_PVT.G_SESSION_ID
15088                          and    pegging_id = l_parent_pegging_id
15089                          and    record_type = 3;
15090                          */
15091 
15092                          SELECT supply_demand_quantity
15093                          INTO   l_peg_qty
15094                          FROM   mrp_atp_details_temp
15095                          WHERE  session_id = MSC_ATP_PVT.G_SESSION_ID
15096                          AND    pegging_id = l_parent_pegging_id
15097                          AND    record_type = 3;
15098 
15099                          MSC_ATP_DB_UTILS.Update_Pegging(l_parent_pegging_id,
15100                                         null,
15101                                         (l_peg_qty - l_net_demand));
15102 
15103                          IF PG_DEBUG in ('Y', 'C') THEN
15104                             msc_sch_wb.atp_debug('ATP_Check: ' || 'adjust demand quantity, l_demand_id = '||l_demand_id);
15105                          END IF;
15106 
15107 			-- dsting diag_atp do not adjust the demands for diagnostic atp
15108 			IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
15109 
15110                          update msc_demands
15111                          set    USING_REQUIREMENT_QUANTITY =
15112                                 MSC_ATP_UTILS.Truncate_Demand((USING_REQUIREMENT_QUANTITY-l_net_demand)) -- 5598066
15113                          where demand_id = l_demand_id
15114                          and plan_id = p_plan_id
15115                          returning inventory_item_id into l_inventory_item_id;
15116 
15117                         /* time_phased_atp
15118                            Update PF Bucketed Demands to qty we could find in backward case*/
15119                         IF l_time_phased_atp = 'Y' THEN
15120                                 MSC_ATP_PF.Update_PF_Bucketed_Demands(
15121                                         p_plan_id,
15122                                         l_demand_id,
15123                                         l_requested_ship_date,
15124                                         p_atp_record.atf_date,
15125                                         p_atp_record.quantity_ordered,
15126                                         p_atp_record.combined_requested_date_qty,
15127                                         l_return_status
15128                                 );
15129                                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15130                                    IF PG_DEBUG in ('Y', 'C') THEN
15131                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Update_PF_Bucketed_Demands');
15132                                    END IF;
15133                                    RAISE FND_API.G_EXC_ERROR;
15134                                 END IF;
15135                          --alloc_pp
15136                          ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15137                              AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15138 
15139                               update msc_alloc_demands
15140                               set allocated_quantity = allocated_quantity - l_net_demand
15141                               where parent_demand_id = l_demand_id
15142                               and   plan_id = p_plan_id;
15143                          END IF;
15144 
15145                          IF PG_DEBUG in ('Y', 'C') THEN
15146                             msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this Flag MSC_ATP_PVT.G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
15147                          END IF;
15148 
15149                         -- update summary records removed in ODS cases - for summary enhancement
15150                          IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
15151                               IF PG_DEBUG in ('Y', 'C') THEN
15152                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'update sd table for component');
15153                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'in sumary mode, update msc-demands');
15154                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
15155                                  msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
15156                               END IF;
15157 
15158                               MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15159                                                                    p_plan_id,
15160                                                                    p_atp_record.organization_id,
15161                                                                    l_inventory_item_id,
15162                                                                    l_requested_ship_date,
15163                                                                    null,
15164                                                                    null,
15165                                                                    null,
15166                                                                    null,
15167 								   null,
15168 								   null,
15169                                                                    2);
15170 
15171                               update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
15172                               set sd_qty = sd_qty + l_net_demand
15173                               where plan_id = p_plan_id
15174                               and  inventory_item_id = l_inventory_item_id
15175                               and  sr_instance_id = p_atp_record.instance_id
15176                               and  organization_id = p_atp_record.organization_id
15177                               and  sd_date = trunc(l_requested_ship_date);
15178                               commit;
15179                          END IF;
15180 
15181 			END IF; -- not DIAGNOSTIC ATP
15182                       END IF;
15183                     ---subst
15184                     --ELSE
15185                     ELSIF p_search = 2 THEN
15186 
15187 
15188                       IF PG_DEBUG in ('Y', 'C') THEN
15189                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward l_net_demand = '||l_net_demand);
15190                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward p_atp_record.ship_date = '||p_atp_record.ship_date);
15191                          msc_sch_wb.atp_debug('ATP_Check: ' || 'forward p_atp_record.quantity_ordered = '||
15192 						p_atp_record.quantity_ordered);
15193                          msc_sch_wb.atp_debug('ATP_Check: ' || 'Check this scenario, forward case');
15194                       END IF;
15195                       -- future case
15196 
15197                       -- bug 1302394: we should adjust the date
15198                       -- of the demand which causes this planned order
15199                       -- as well so that they can cancel each other.
15200 
15201                       -- bug 1531429 : if p_atp_record.ship_date is null, update
15202 		      -- USING_ASSEMBLY_DEMAND_DATE with same value
15203 
15204                       -- 1421413: don't update the quantity
15205 
15206                      IF p_atp_record.ship_date IS NOT NULL THEN
15207                          -- Update only if p_atp_record.ship_date is NOT NULL
15208 
15209 			-- dsting ATO 2465370
15210 			-- for override case for CTO, do not adjust demand date in msc_demands
15211                         --bug 3564511: always update the date on PO demands
15212 		        --IF NVL(G_LINE_OVERRIDE_FLAG, 'N') = 'N'
15213 			   --OR (NVL(C_ITEM_INFO_REC.cto_source_org_id, -1) <> p_atp_record.organization_id
15214 			   --AND p_parent_pegging_id is not null)
15215 			--THEN
15216                                 --bug 3328421: update actual date
15217 				--update mrp_atp_details_temp set required_date =
15218 				update mrp_atp_details_temp set actual_supply_demand_date =
15219 					TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY
15220 				where session_id = MSC_ATP_PVT.G_SESSION_ID
15221 				and pegging_id = l_parent_pegging_id
15222 				and record_type = 3;
15223 
15224 				IF PG_DEBUG in ('Y','C') THEN
15225 					msc_sch_wb.atp_debug('ATO update details_temp 12: ' || l_parent_pegging_id || ' date: ' || p_atp_record.ship_date);
15226 				END IF;
15227 
15228                 	        UPDATE msc_demands
15229         	                --SET  USING_ASSEMBLY_DEMAND_DATE = NVL(p_atp_record.ship_date,
15230 			        --					USING_ASSEMBLY_DEMAND_DATE)
15231                         	-- For bug 2259824, move the demand to the end of day
15232                         	----start changes for plan by request date
15233                         	SET    USING_ASSEMBLY_DEMAND_DATE =
15234                         	           DECODE(ORIGINATION_TYPE,
15235                         	                  6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
15236                         	                             MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
15237                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15238                         	                             MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
15239                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15240                         	                             MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
15241                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15242                         	                             MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
15243                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15244                         	                             MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
15245                         	                                 l_original_req_ship_date,
15246                         	                             MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
15247                         	                                 l_original_req_ship_date,
15248                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
15249                         	                  30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
15250                         	                             MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
15251                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15252                         	                             MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
15253                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15254                         	                             MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
15255                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15256                         	                             MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
15257                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15258                         	                             MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
15259                         	                                 l_original_req_ship_date,
15260                         	                             MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
15261                         	                                 l_original_req_ship_date,
15262                         	                                 TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
15263                         	                  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
15264                         	       schedule_ship_date =
15265                         	           DECODE(ORIGINATION_TYPE,
15266                         	                  6,  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15267                         	                  30, TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15268                         	                  NULL),
15269                         	       promise_ship_date =
15270                         	           DECODE(ORIGINATION_TYPE,
15271                         	                  6,  TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15272                         	                  30, TRUNC(p_atp_record.ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
15273                         	                  NULL)
15274                 		--end changes for plan by request date
15275                 		WHERE  demand_id = l_demand_id
15276 	                        AND    plan_id = p_plan_id
15277                         	returning inventory_item_id into l_inventory_item_id;
15278                 	        IF PG_DEBUG in ('Y', 'C') THEN
15279                                    msc_sch_wb.atp_debug('demand_id: ' || l_demand_id);
15280         	                   msc_sch_wb.atp_debug('ATP_Check: ' || 'number of rows updated := ' || SQL%ROWCOUNT);
15281 	                        END IF;
15282 
15283                                 /* time_phased_atp*/
15284                                 IF l_time_phased_atp = 'Y' THEN
15285                                         MSC_ATP_PF.Move_PF_Bucketed_Demands(
15286                                                 p_plan_id,
15287                                                 l_demand_id,
15288                                                 p_atp_record.requested_ship_date,
15289                                                 p_atp_record.ship_date,
15290                                                 NULL,
15291                                                 NULL,
15292                                                 p_atp_record.atf_date,
15293                                                 NULL, -- pass atf_date_qty as null
15294                                                 l_return_status,
15295                                                 NULL --bug3397904
15296                                         );
15297                                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
15298                                                 IF PG_DEBUG in ('Y', 'C') THEN
15299                                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'Error occured in procedure Move_PF_Bucketed_Demands');
15300                                                 END IF;
15301                                                 RAISE FND_API.G_EXC_ERROR;
15302                                         END IF;
15303                          	--alloc_pp
15304                          	ELSIF MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y'
15305                             	   AND MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 AND MSC_ATP_PVT.G_ALLOCATION_METHOD = 1 THEN
15306                                    update msc_alloc_demands
15307                                    set demand_date  = p_atp_record.ship_date
15308                                    where parent_demand_id = l_demand_id
15309                                    and   plan_id = p_plan_id;
15310                          	END IF;
15311 
15312                          	IF PG_DEBUG in ('Y', 'C') THEN
15313                             	   msc_sch_wb.atp_debug('ATP_Check: ' || 'inventory_item_id := ' || l_inventory_item_id);
15314                          	END IF;
15315                             -- update summary records removed in ODS cases - for summary enhancement
15316                             IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND (  SQL%ROWCOUNT > 1) AND p_plan_id = -1 THEN
15317 
15318                                 IF PG_DEBUG in ('Y', 'C') THEN
15319                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'old and new dates are  not same');
15320                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary for old date');
15321                                 END IF;
15322                                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15323                                                                         p_plan_id,
15324                                                                         p_atp_record.organization_id,
15325                                                                         l_inventory_item_id,
15326                                                                         l_requested_ship_date,
15327                                                                         null,
15328                                                                         null,
15329                                                                         null,
15330                                                                         null,
15331                                                                         null,
15332                                                                         null,
15333                                                                         2);
15334 
15335 
15336                                 UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ MSC_ATP_SUMMARY_SD
15337                                 set sd_qty = (sd_qty + p_atp_record.quantity_ordered)
15338                                 where sr_instance_id = p_atp_record.instance_id and
15339                                 inventory_item_id = l_inventory_item_id and
15340                                 organization_id = p_atp_record.organization_id and
15341                                 sd_date = trunc(l_requested_ship_date) and
15342                                 plan_id = p_plan_id;
15343 
15344                                 commit;
15345 
15346                                 --- if record exists then update
15347                                 IF PG_DEBUG in ('Y', 'C') THEN
15348                                     msc_sch_wb.atp_debug('ATP_Check: ' || 'update the qty on new date ');
15349                                 END IF;
15350                                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15351                                                                         p_plan_id,
15352                                                                         p_atp_record.organization_id,
15353                                                                         l_inventory_item_id,
15354                                                                         p_atp_record.ship_date,
15355                                                                         null,
15356                                                                         null,
15357                                                                         null,
15358                                                                         null,
15359                                                                         null,
15360                                                                         null,
15361                                                                         2);
15362 
15363                                 update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
15364                                 set sd_qty = sd_qty - p_atp_record.quantity_ordered
15365                                 where plan_id = p_plan_id and
15366                                 sr_instance_id = p_atp_record.instance_id and
15367                                 inventory_item_id = l_inventory_item_id and
15368                                 organization_id = p_atp_record.organization_id and
15369                                 sd_date = trunc(p_atp_record.ship_date);
15370 
15371                                 IF SQL%NOTFOUND THEN
15372                                     --  record doesn't exists. insert the record
15373                                     IF PG_DEBUG in ('Y', 'C') THEN
15374                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'insert qty on new date');
15375                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'plan_id := ' || p_plan_id);
15376                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
15377                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'organization_id := ' || p_atp_record.organization_id);
15378                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'instance_id := ' || p_atp_record.instance_id);
15379                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'ship_date := ' || p_atp_record.ship_date);
15380                                         msc_sch_wb.atp_debug('ATP_Check: ' || 'quantity_ordered := ' || p_atp_record.quantity_ordered);
15381                                     END IF;
15382                                     BEGIN
15383                                         MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
15384                                         p_atp_record.instance_id,
15385                                         p_atp_record.organization_id,
15386                                         l_inventory_item_id,
15387                                         p_atp_record.ship_date,
15388                                         -1 * p_atp_record.quantity_ordered,
15389                                         '@@@');
15390 
15391                                     END;
15392                                 END IF; --- if sql%notfound
15393                                 commit;
15394                             END IF;
15395 
15396 			--END IF; -- end changes for cto override
15397 		     END IF;
15398 
15399 		      MSC_ATP_PROC.Details_Output(l_future_atp_period,
15400                      	             l_future_atp_supply_demand,
15401                      	             x_atp_period,
15402                      	             x_atp_supply_demand,
15403                      	             l_return_status);
15404 
15405                     END IF;
15406                 END IF;
15407               END IF; -- end if p_search = BACKWARD_SCHEDULING and
15408                       -- l_requested_ship_date < sysdate.  bug 1124538
15409 
15410             END IF;  -- end if net_demand
15411 
15412         END IF;  -- end if atp_flag = 'Y'
15413 
15414     ELSE
15415         -- doing atp check in an vendor site
15416 
15417         IF PG_DEBUG in ('Y', 'C') THEN
15418            msc_sch_wb.atp_debug('ATP_Check: ' || 'do atp check at a supplier site');
15419         END IF;
15420 
15421         l_requested_ship_date := p_atp_record.requested_ship_date;
15422         IF PG_DEBUG in ('Y', 'C') THEN
15423            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
15424         END IF;
15425         -- get the following information
15426         -- requested_date_quantity
15427         -- atp_date_this_level
15428         -- atp_date_quantity_this_level
15429 
15430         /* Modularize Item and Org Info */
15431         -- Re-Use info instead of making unnecessary call.
15432         ---bug 2374548: C_ITEM_INFO_rec is set only in org part and not in suppplier part
15433         --l_inv_item_id := C_ITEM_INFO_REC.dest_inv_item_id;
15434         IF NVL(G_ITEM_INFO_REC.sr_inv_item_id, 0) = p_atp_record.inventory_item_id THEN
15435             l_inv_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
15436         ELSE
15437             MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
15438                                             --bug 3917625: read attributes from planned data
15439                                             -- -1,
15440                                             p_plan_id,
15441                                             p_atp_record.inventory_item_id,
15442                                             p_atp_record.organization_id,
15443                                             l_item_info_rec  );
15444             l_inv_item_id := G_ITEM_INFO_REC.dest_inv_item_id;
15445 
15446         END IF;
15447 
15448         IF PG_DEBUG in ('Y', 'C') THEN
15449            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
15450         END IF;
15451 
15452         /* Modularize Item and Org Info */
15453 
15454         IF (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 2) THEN
15455                IF PG_DEBUG in ('Y', 'C') THEN
15456                   msc_sch_wb.atp_debug('ATP_Check: ' || 'G_HIERARCHY_PROFILE = 2');
15457                END IF;
15458               p_atp_record.demand_class :=
15459                   MSC_AATP_FUNC.Get_Hierarchy_Demand_Class(MSC_ATP_PVT.G_PARTNER_ID,
15460                            MSC_ATP_PVT.G_PARTNER_SITE_ID,
15461                            l_inv_item_id,
15462                            p_atp_record.organization_id,
15463                            p_atp_record.instance_id,
15464                            l_requested_ship_date,
15465                            NULL, -- level_id
15466                            p_atp_record.Demand_Class);
15467               IF PG_DEBUG in ('Y', 'C') THEN
15468                  msc_sch_wb.atp_debug('ATP_Check: ' || 'after getting the dummy demand class');
15469                  msc_sch_wb.atp_debug('ATP_Check: ' || 'p_atp_record.demand_class = '||p_atp_record.demand_class);
15470               END IF;
15471 
15472               --diag_atp
15473               l_allocation_rule_name := MSC_ATP_PVT.G_ALLOCATION_RULE_NAME;
15474         END IF;
15475 
15476         -- Bug 1410591, we need to consider processing lead time also similar
15477         -- to planning.
15478         ---- EFTC changes add preprocessing lead time to the lead time
15479         l_sysdate := sysdate;
15480 
15481         l_atp_request_date := l_requested_ship_date;
15482         IF PG_DEBUG in ('Y', 'C') THEN
15483            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
15484         END IF;
15485         -- Supplier Capacity and Lead Time (SCLT) changes
15486         -- Fixed + Variable is discontinued, Instead item's processing lead time
15487         -- essentially full_lead_time is used. Initialize local variables.
15488 
15489         l_process_lt := p_atp_record.atp_lead_time;
15490         l_pre_pro_lt := MSC_ATP_PVT.G_ITEM_INFO_REC.pre_pro_lt;
15491         -- End Supplier Capacity and Lead Time changes
15492         /* 3316028 (Enforce Pur LT) - No need to to PTF/sysdate checks here as these have already been done
15493                 before calling this */
15494         l_atp_ship_date := l_requested_ship_date;
15495         /*
15496         IF p_search = BACKWARD_SCHEDULING THEN
15497            l_atp_ship_date := l_requested_ship_date;
15498 
15499         ELSIF p_search = FORWARD_SCHEDULING THEN
15500            --diag_atp, Supplier Capacity and Lead Time (SCLT) changes
15501            IF p_atp_record.atp_lead_time > 0 THEN
15502                /* we know the req date but we dont know whether (req date - (pre + process lead time)) < sysdate or not.
15503                   IF (req date - (pre + process lead time)) < sysdate then earlierst we can ship is
15504                   (sysdate + lead time) */
15505              --  Supplier Capacity and Lead Time (SCLT) changes
15506              -- First do the Pre-Processing Lead Time Offset in Forward Case.
15507         /*     IF l_pre_pro_lt > 0 THEN
15508                 /* ship_rec_cal
15509                 l_dock_date := MSC_CALENDAR.DATE_OFFSET(p_atp_record.organization_id,
15510                                                            p_atp_record.instance_id, 1,
15511                                                            l_sysdate,
15512                                                            l_pre_pro_lt);*/
15513         /*        l_dock_date := MSC_CALENDAR.DATE_OFFSET(
15514                                         p_atp_record.manufacturing_cal_code,
15515                                         p_atp_record.instance_id,
15516                                         l_sysdate,
15517                                         l_pre_pro_lt, 1);
15518 
15519                 IF PG_DEBUG in ('Y', 'C') THEN
15520                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After pre-process Lead Time Offset ' );
15521                    msc_sch_wb.atp_debug('ATP_Check: ' || 'After atp dock date = '||
15522                     to_char(l_dock_date, 'DD-MON-YYYY'));
15523                 END IF;
15524               ELSE
15525                 l_dock_date := l_sysdate;
15526               END IF;
15527               -- Next do the Processing lead time offset in Forward Case using Plan Owning Org's calendar.
15528               IF p_atp_record.atp_lead_time > 0 THEN
15529                   /* ship_rec_cal
15530                   l_dock_date := MSC_CALENDAR.DATE_OFFSET(G_PLAN_INFO_REC.organization_id,
15531                                                                   G_PLAN_INFO_REC.sr_instance_id, 1,
15532                                                                   l_dock_date,
15533                                                                   p_atp_record.atp_lead_time);*/
15534         /*          l_dock_date := MSC_CALENDAR.DATE_OFFSET(
15535                                         p_atp_record.manufacturing_cal_code,
15536                                         G_PLAN_INFO_REC.sr_instance_id,
15537                                         l_dock_date,
15538                                         p_atp_record.atp_lead_time, 1);
15539               END IF;
15540               IF PG_DEBUG in ('Y', 'C') THEN
15541                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After Process Lead Time Offset ' );
15542                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After atp dock date = '||
15543                        to_char(l_dock_date, 'DD-MON-YYYY'));
15544               END IF;
15545               -- End  Supplier Capacity and Lead Time changes
15546               l_atp_ship_date := GREATEST(l_dock_date, l_requested_ship_date, MSC_ATP_PVT.G_PTF_DATE);
15547            ELSE
15548                l_atp_ship_date := GREATEST(l_sysdate,  MSC_ATP_PVT.G_PTF_DATE, l_requested_ship_date);
15549            END IF;
15550         END IF;
15551         3316028 (Enforce Pur LT) - changes end */
15552         IF PG_DEBUG in ('Y', 'C') THEN
15553            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_request_date := ' || l_atp_request_date);
15554            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_ship_date := ' || l_atp_ship_date);
15555            msc_sch_wb.atp_debug('ATP_Check: ' || 'G_PTF_DATE := ' || MSC_ATP_PVT.G_PTF_DATE);
15556         END IF;
15557 
15558         --diag_atp
15559         /* IF p_search = BACKWARD_SCHEDULING AND
15560            ((l_atp_request_date < l_sysdate) OR (l_requested_ship_date < MSC_ATP_PVT.G_PTF_DATE)) THEN
15561                 l_requested_date_quantity := 0;
15562                 IF PG_DEBUG in ('Y', 'C') THEN
15563                    msc_sch_wb.atp_debug('ATP_Check: ' || 'Sys date, ptf check failed for supplier');
15564                 END IF;
15565         ELSE
15566         */
15567            --  Supplier Capacity and Lead Time (SCLT) changes
15568            IF (g_sup_cap_cum_date IS NULL) THEN
15569                 g_sup_cap_cum_date := MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date;
15570            END IF;
15571            IF PG_DEBUG in ('Y', 'C') THEN
15572                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Id := ' || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id);
15573                 msc_sch_wb.atp_debug('ATP_Check: ' || 'Plan Start Date := ' ||
15574                                            MSC_ATP_PVT.G_PLAN_INFO_REC.plan_start_date);
15575                 msc_sch_wb.atp_debug('ATP_Check: ' || 'g_sup_cap_cum_date := ' || g_sup_cap_cum_date);
15576            END IF;
15577 
15578 
15579            -- ship_rec_cal changes begin
15580            l_sup_atp_info_rec.instance_id                       := p_atp_record.instance_id;
15581            l_sup_atp_info_rec.plan_id                           := p_plan_id;
15582            l_sup_atp_info_rec.level                             := p_level + 1;
15583            l_sup_atp_info_rec.identifier                        := p_atp_record.identifier;
15584            l_sup_atp_info_rec.scenario_id                       := p_scenario_id;
15585            l_sup_atp_info_rec.inventory_item_id                 := l_inv_item_id;
15586            l_sup_atp_info_rec.organization_id                   := p_atp_record.organization_id;
15587            l_sup_atp_info_rec.supplier_id                       := p_atp_record.supplier_id;
15588            l_sup_atp_info_rec.supplier_site_id                  := p_atp_record.supplier_site_id;
15589            l_sup_atp_info_rec.requested_date                    := l_atp_ship_date;
15590            l_sup_atp_info_rec.quantity_ordered                  := p_atp_record.quantity_ordered;
15591            l_sup_atp_info_rec.demand_class                      := p_atp_record.demand_class;
15592            l_sup_atp_info_rec.insert_flag                       := p_atp_record.insert_flag;
15593            l_sup_atp_info_rec.requested_date_quantity           := null;
15594            l_sup_atp_info_rec.atp_date_this_level               := null;
15595            l_sup_atp_info_rec.atp_date_quantity_this_level      := null;
15596            l_sup_atp_info_rec.shipping_cal_code                 := p_atp_record.shipping_cal_code;
15597            l_sup_atp_info_rec.receiving_cal_code                := p_atp_record.receiving_cal_code;
15598            l_sup_atp_info_rec.manufacturing_cal_code            := p_atp_record.manufacturing_cal_code;
15599            l_sup_atp_info_rec.intransit_cal_code                := p_atp_record.intransit_cal_code;
15600            l_sup_atp_info_rec.sup_cap_cum_date                  := g_sup_cap_cum_date;
15601            l_sup_atp_info_rec.sysdate_seq_num                   := l_sysdate_seq_num;
15602            l_sup_atp_info_rec.sup_cap_type                      := l_sup_cap_type;
15603            l_sup_atp_info_rec.refresh_number                    := p_refresh_number;
15604            -- For CTO rearch
15605            l_sup_atp_info_rec.base_item_id                      := p_atp_record.base_model_id;
15606            l_sup_atp_info_rec.bom_item_type                     := p_atp_record.bom_item_type;
15607            l_sup_atp_info_rec.rep_ord_flag                      := p_atp_record.rep_ord_flag;
15608            -- Enforce Pur LT - pass last cap date as it has been passed to the ATP_check for supplier case
15609            l_sup_atp_info_rec.last_cap_date                     := p_atp_record.last_cap_date;
15610 
15611            MSC_ATP_REQ.Get_Supplier_Atp_Info(
15612                         l_sup_atp_info_rec,
15613                         x_atp_period,
15614                         x_atp_supply_demand,
15615                         l_return_status);
15616 
15617            l_requested_date_quantity           := l_sup_atp_info_rec.requested_date_quantity;
15618            l_atp_date_this_level               := l_sup_atp_info_rec.atp_date_this_level;
15619            l_atp_date_quantity_this_level      := l_sup_atp_info_rec.atp_date_quantity_this_level;
15620            -- ship_rec_cal changes end
15621 
15622         -- bug 1512366
15623         p_atp_record.requested_ship_date:=l_requested_ship_date;
15624 
15625         IF PG_DEBUG in ('Y', 'C') THEN
15626            msc_sch_wb.atp_debug('ATP_Check: ' || 'after call supplier, x_atp_supply_demand'||x_atp_supply_demand.supplier_id.COUNT);
15627            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
15628            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_date_quantity := ' || l_requested_date_quantity);
15629            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_quantity_this_level := ' || l_atp_date_quantity_this_level);
15630         END IF;
15631         IF l_requested_date_quantity >= p_atp_record.quantity_ordered THEN
15632 
15633             -- we meet the requirements!!!
15634             p_atp_record.requested_date_quantity:=l_requested_date_quantity;
15635             p_atp_record.ship_date := l_atp_date_this_level;
15636             p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
15637 
15638         ELSE
15639 
15640             -- we cannot meet the requirements, so we provide
15641             -- the information at this level
15642             p_atp_record.requested_date_quantity:=l_requested_date_quantity;
15643             p_atp_record.ship_date := l_atp_date_this_level;
15644             p_atp_record.available_quantity:=l_atp_date_quantity_this_level;
15645             p_atp_record.error_code := ATP_REQ_QTY_FAIL;
15646 
15647         END IF;
15648 
15649         -- add pegging info for this demand
15650 
15651         -- for performance reason, we call these function here and
15652         -- then populate the pegging tree with the values
15653 
15654         /* Modularize Item and Org Info */
15655         -- Re-Use info instead of making unnecessary call.
15656         ---bug 2374548: C_ITEM_INFO_rec is set only in org part and not in suppplier part
15657         --l_inv_item_name := C_ITEM_INFO_REC.item_name;
15658         IF NVL(G_ITEM_INFO_REC.sr_inv_item_id, 0) = p_atp_record.inventory_item_id THEN
15659             l_inv_item_name := G_ITEM_INFO_REC.item_name;
15660         ELSE
15661             MSC_ATP_PROC.get_global_item_info(p_atp_record.instance_id,
15662                                             --3917625: Read data from plan output
15663                                             -- -1,
15664                                             p_plan_id,
15665                                             p_atp_record.inventory_item_id,
15666                                             p_atp_record.organization_id,
15667                                             l_item_info_rec  );
15668             l_inv_item_name := G_ITEM_INFO_REC.item_name;
15669         END IF;
15670 
15671         IF PG_DEBUG in ('Y', 'C') THEN
15672            msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_name := ' || l_inv_item_name);
15673         END IF;
15674 
15675         /*l_inv_item_name := MSC_ATP_FUNC.get_inv_item_name(p_atp_record.instance_id,
15676                                       p_atp_record.inventory_item_id,
15677                                       p_atp_record.organization_id);
15678          Modularize Item and Org Info */
15679 
15680         l_supplier_name := MSC_ATP_FUNC.get_supplier_name(p_atp_record.instance_id,
15681                                              p_atp_record.supplier_id);
15682         l_supplier_site_name :=
15683             MSC_ATP_FUNC.get_supplier_site_name(p_atp_record.instance_id,
15684                                    p_atp_record.supplier_site_id);
15685 
15686         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
15687         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
15688         l_pegging_rec.parent_pegging_id:= p_parent_pegging_id;
15689         l_pegging_rec.atp_level:= p_level;
15690         l_pegging_rec.inventory_item_id := p_atp_record.inventory_item_id;
15691         l_pegging_rec.inventory_item_name := l_inv_item_name;
15692         l_pegging_rec.supplier_id := p_atp_record.supplier_id;
15693         l_pegging_rec.supplier_name := l_supplier_name;
15694         l_pegging_rec.supplier_site_id := p_atp_record.supplier_site_id;
15695         l_pegging_rec.supplier_site_name := l_supplier_site_name;
15696         l_pegging_rec.resource_id := NULL;
15697         l_pegging_rec.resource_code := NULL;
15698         l_pegging_rec.department_id := NULL;
15699         l_pegging_rec.department_code := NULL;
15700         l_pegging_rec.identifier1:= p_atp_record.instance_id;
15701         l_pegging_rec.identifier2:= p_plan_id;
15702         l_pegging_rec.identifier3 := -1;
15703         l_pegging_rec.scenario_id:= p_scenario_id;
15704         l_pegging_rec.supply_demand_source_type:= 1;
15705         ---- summary
15706 
15707         IF (p_search = 1) THEN
15708            l_pegging_rec.supplier_atp_date :=  l_requested_ship_date;
15709         ELSE
15710            l_pegging_rec.supplier_atp_date := l_atp_date_this_level;
15711         END IF;
15712         l_pegging_rec.dest_inv_item_id := l_inv_item_id;
15713 
15714         -- 1408056
15715         IF (p_search = 1) THEN
15716           --diag_atp : we place the demand on complete quantity in case of diagnostic ATP
15717           IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
15718               l_pegging_rec.supply_demand_quantity:=  p_atp_record.quantity_ordered;
15719           ELSE
15720 
15721              l_pegging_rec.supply_demand_quantity:=
15722                 LEAST(l_requested_date_quantity, p_atp_record.quantity_ordered);
15723           END IF;
15724         ELSE
15725           l_pegging_rec.supply_demand_quantity:= p_atp_record.quantity_ordered;
15726 
15727         END IF;
15728         -- l_pegging_rec.supply_demand_quantity:=p_atp_record.quantity_ordered;
15729 
15730         l_pegging_rec.supply_demand_type:= 1;
15731         l_pegging_rec.supply_demand_date:= l_requested_ship_date;
15732 
15733 	-- dsting ATO 2465370
15734 	IF PG_DEBUG in ('Y','C') THEN
15735 		msc_sch_wb.atp_debug('set required_date supplier demand');
15736 	END IF;
15737         IF (p_search = BACKWARD_SCHEDULING) THEN
15738           -- Commented for bug 2748730. G_END_OF_DAY not required here.
15739 	  -- We will do that inside the procedure Add_Pegging
15740 	  -- l_pegging_rec.required_date:= TRUNC(l_requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY;
15741           --bug 3328421
15742 	  l_pegging_rec.required_date:= l_requested_ship_date ;
15743           l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
15744         ELSE
15745 	  -- Commented for bug 2748730. G_END_OF_DAY not required here.
15746 	  -- We will do that inside the procedure Add_Pegging
15747           -- l_pegging_rec.required_date:= TRUNC(l_atp_date_this_level) + MSC_ATP_PVT.G_END_OF_DAY;
15748 
15749 	  l_pegging_rec.required_date:= l_requested_ship_date ;
15750           l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
15751         END IF;
15752 
15753         -- for demo:1153192
15754         l_pegging_rec.constraint_flag := 'N';
15755 	l_pegging_rec.component_identifier :=
15756                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
15757 
15758         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
15759 
15760         --diag_atp
15761         l_pegging_rec.pegging_type := MSC_ATP_PVT.SUPPLIER_DEMAND;
15762         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
15763 
15764         --4570421
15765         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
15766         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
15767         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
15768         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
15769         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
15770         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
15771         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
15772 
15773         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
15774 
15775         -- add pegging info for the supply
15776 
15777         l_pegging_rec.session_id:= MSC_ATP_PVT.G_SESSION_ID;
15778         l_pegging_rec.order_line_id:= MSC_ATP_PVT.G_ORDER_LINE_ID;
15779         l_pegging_rec.parent_pegging_id:= l_pegging_id;
15780         l_pegging_rec.atp_level:= p_level+1;
15781 
15782         l_pegging_rec.inventory_item_id := p_atp_record.inventory_item_id;
15783         l_pegging_rec.inventory_item_name := l_inv_item_name;
15784         l_pegging_rec.supplier_id := p_atp_record.supplier_id;
15785         l_pegging_rec.supplier_name := l_supplier_name;
15786         l_pegging_rec.supplier_site_id := p_atp_record.supplier_site_id;
15787         l_pegging_rec.supplier_site_name := l_supplier_site_name;
15788         l_pegging_rec.resource_id := NULL;
15789         l_pegging_rec.resource_code := NULL;
15790         l_pegging_rec.department_id := NULL;
15791         l_pegging_rec.department_code := NULL;
15792 
15793         l_pegging_rec.identifier1:= p_atp_record.instance_id;
15794         l_pegging_rec.identifier2:= p_plan_id;
15795         l_pegging_rec.identifier3 := -1;
15796         l_pegging_rec.scenario_id:= p_scenario_id;
15797         l_pegging_rec.supply_demand_source_type:= ATP;
15798         l_pegging_rec.supply_demand_type:= 2;
15799         l_pegging_rec.source_type := 0;
15800 	l_pegging_rec.component_identifier :=
15801                          NVL(p_atp_record.component_identifier, MSC_ATP_PVT.G_COMP_LINE_ID);
15802 
15803         IF (p_search = 1) THEN
15804           l_pegging_rec.supply_demand_date:= l_requested_ship_date;
15805           l_pegging_rec.supply_demand_quantity:= l_requested_date_quantity;
15806           --bug 3328421
15807           l_pegging_rec.actual_supply_demand_date := l_requested_ship_date;
15808 
15809           -- for demo:1153192
15810           IF p_atp_record.quantity_ordered >= l_requested_date_quantity THEN
15811               l_pegging_rec.constraint_flag := 'Y';
15812           ELSE
15813               l_pegging_rec.constraint_flag := 'N';
15814           END IF;
15815         ELSE
15816           l_pegging_rec.supply_demand_date:= l_atp_date_this_level;
15817           l_pegging_rec.supply_demand_quantity:= l_atp_date_quantity_this_level;
15818           --bug 3328421
15819           l_pegging_rec.actual_supply_demand_date := l_atp_date_this_level;
15820         END IF;
15821         l_pegging_rec.summary_flag := MSC_ATP_PVT.G_SUMMARY_FLAG;
15822 
15823         --diag_atp
15824         l_pegging_rec.pegging_type := MSC_ATP_PVT.ATP_SUPPLIER;
15825         -- Bug 3828469 - Move the quantity check out so that constraint is added only if it exists
15826         IF p_atp_record.quantity_ordered > l_requested_date_quantity THEN
15827             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 1 THEN
15828                 l_pegging_rec.constraint_type := 1;
15829             ELSIF (MSC_ATP_PVT.G_DIAGNOSTIC_ATP = 2 and p_search = 2) THEN
15830 
15831                 --s_cto_rearch
15832                 IF l_atp_date_this_level > l_requested_ship_date THEN
15833                     l_pegging_rec.constraint_type := 1;
15834                 END IF;
15835                 --e_cto_rearch
15836             END IF;
15837         END IF;
15838         l_pegging_rec.dest_inv_item_id := l_inv_item_id;
15839 
15840         l_pegging_rec.required_quantity := p_atp_record.quantity_ordered;
15841         --bug 3328421: required date always contain the original requested date
15842         --l_pegging_rec.required_date := l_atp_ship_date;
15843         l_pegging_rec.required_date := l_requested_ship_date;
15844         l_pegging_rec.allocation_rule := l_allocation_rule_name;
15845         l_pegging_rec.plan_name := p_atp_record.plan_name;
15846         l_pegging_rec.demand_class :=  p_atp_record.demand_class;
15847         -- Bug 3826234
15848         l_pegging_rec.shipping_cal_code      :=  p_atp_record.shipping_cal_code;
15849         l_pegging_rec.receiving_cal_code     :=  p_atp_record.receiving_cal_code;
15850         l_pegging_rec.intransit_cal_code     :=  p_atp_record.intransit_cal_code;
15851         l_pegging_rec.manufacturing_cal_code :=  p_atp_record.manufacturing_cal_code;
15852 
15853         --4570421
15854         l_pegging_rec.scaling_type                      := p_atp_record.scaling_type;
15855         l_pegging_rec.scale_multiple                    := p_atp_record.scale_multiple;
15856         l_pegging_rec.scale_rounding_variance           := p_atp_record.scale_rounding_variance;
15857         l_pegging_rec.rounding_direction                := p_atp_record.rounding_direction;
15858         l_pegging_rec.component_yield_factor            := p_atp_record.component_yield_factor; --4570421
15859         l_pegging_rec.usage                             := p_atp_record.usage_qty; --4775920
15860         l_pegging_rec.organization_type                 := p_atp_record.organization_type; --4775920
15861 
15862         MSC_ATP_DB_UTILS.Add_Pegging(l_pegging_rec, l_pegging_id);
15863 
15864         ---- summary
15865         --- Create record into summary table for this supplier
15866         -- Code to update summary records removed for summary enhancement
15867         /** code commented for time being. Will be removed after code review
15868         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
15869             IF (p_search = 1) THEN
15870                 IF PG_DEBUG in ('Y', 'C') THEN
15871                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary table , backward case');
15872                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_requested_ship_date := ' || l_requested_ship_date);
15873                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier_id := ' ||  p_atp_record.supplier_id);
15874                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
15875                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Quantity := ' || least(l_requested_date_quantity, p_atp_record.quantity_ordered));
15876                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
15877                 END IF;
15878                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15879                                                     p_plan_id,
15880                                                     null,
15881                                                     l_inv_item_id,
15882                                                     l_requested_ship_date,
15883                                                     null,
15884                                                     null,
15885                                                     p_atp_record.supplier_id,
15886                                                     p_atp_record.supplier_site_id,
15887                                                     null,
15888                                                     null,
15889                                                     4);
15890 
15891                 update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_sup_U1) *//* msc_atp_summary_sup
15892                 set sd_qty = sd_qty - least(l_requested_date_quantity, p_atp_record.quantity_ordered)
15893                 where plan_id = p_plan_id
15894                 and   sr_instance_id = p_atp_record.instance_id
15895                 and   inventory_item_id = l_inv_item_id
15896                 and   supplier_id = p_atp_record.supplier_id
15897                 and   supplier_site_id = p_atp_record.supplier_site_id
15898                 and   sd_date = trunc(l_requested_ship_date);
15899                 IF PG_DEBUG in ('Y', 'C') THEN
15900                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After update of msc_atp_summary_sup');
15901                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Number of rows updated := ' || SQL%ROWCOUNT);
15902                 END IF;
15903 
15904                 IF SQL%NOTFOUND THEN
15905                     IF PG_DEBUG in ('Y', 'C') THEN
15906                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update failed, insert a row');
15907                     END IF;
15908                     BEGIN
15909                     insert into /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
15910                          (plan_id,
15911                           inventory_item_id,
15912                           supplier_id,
15913                           supplier_site_id,
15914                           sr_instance_id,
15915                           sd_date,
15916                           sd_qty,
15917                           LAST_UPDATE_DATE,
15918                           LAST_UPDATED_BY,
15919                           CREATION_DATE,
15920                           CREATED_BY
15921                           )
15922                           VALUES
15923                           (p_plan_id,
15924                            l_inv_item_id,
15925                            p_atp_record.supplier_id,
15926                            p_atp_record.supplier_site_id,
15927                            p_atp_record.instance_id,
15928                            trunc(l_requested_ship_date),
15929                            -1 * least(l_requested_date_quantity, p_atp_record.quantity_ordered),
15930                            sysdate,
15931                            FND_GLOBAL.USER_ID,
15932                            sysdate,
15933                            FND_GLOBAL.USER_ID);
15934                     EXCEPTION
15935                         WHEN DUP_VAL_ON_INDEX THEN
15936                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15937                                                                 p_plan_id,
15938                                                                 null,
15939                                                                 l_inv_item_id,
15940                                                                 l_requested_ship_date,
15941                                                                 null,
15942                                                                 null,
15943                                                                 p_atp_record.supplier_id,
15944                                                                 p_atp_record.supplier_site_id,
15945                                                                 null,
15946                                                                 null,
15947                                                                 4);
15948 
15949                             update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
15950                             set sd_qty = sd_qty - least(l_requested_date_quantity, p_atp_record.quantity_ordered)
15951                             where plan_id = p_plan_id
15952                             and   sr_instance_id = p_atp_record.instance_id
15953                             and   inventory_item_id = l_inv_item_id
15954                             and   supplier_id = p_atp_record.supplier_id
15955                             and   supplier_site_id = p_atp_record.supplier_site_id
15956                             and   sd_date = trunc(l_requested_ship_date);
15957                     END;
15958                 END IF;
15959                 commit;
15960             ELSIF l_atp_date_this_level IS NOT NULL THEN
15961                 IF PG_DEBUG in ('Y', 'C') THEN
15962                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Update summary table, forward case');
15963                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
15964                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_atp_date_this_level := ' || l_atp_date_this_level);
15965                     msc_sch_wb.atp_debug('ATP_Check: ' || 'Supplier_id := ' ||  p_atp_record.supplier_id);
15966                     msc_sch_wb.atp_debug('ATP_Check: ' || 'l_inv_item_id := ' || l_inv_item_id);
15967                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Quantity := ' ||  p_atp_record.quantity_ordered);
15968                     msc_sch_wb.atp_debug('ATP_Check: ' || 'First try to update');
15969                 END IF;
15970                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
15971                                                     p_plan_id,
15972                                                     p_atp_record.organization_id,
15973                                                     l_inv_item_id,
15974                                                     l_atp_date_this_level,
15975                                                     null,
15976                                                     null,
15977                                                     p_atp_record.supplier_id,
15978                                                     p_atp_record.supplier_site_id,
15979                                                     null,
15980                                                     null,
15981                                                     4);
15982 
15983                 update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
15984                 set sd_qty = sd_qty -  p_atp_record.quantity_ordered
15985                 where plan_id = p_plan_id
15986                 and   sr_instance_id = p_atp_record.instance_id
15987                 and   inventory_item_id = l_inv_item_id
15988                 and   supplier_id = p_atp_record.supplier_id
15989                 and   supplier_site_id = p_atp_record.supplier_site_id
15990                 and   sd_date = trunc(l_atp_date_this_level);
15991                 IF PG_DEBUG in ('Y', 'C') THEN
15992                     msc_sch_wb.atp_debug('ATP_Check: ' || 'After update of msc_atp_summary_sup');
15993                     msc_sch_wb.atp_debug('ATP_Check: ' || ' Number of rows updated := ' || SQL%ROWCOUNT);
15994                 END IF;
15995 
15996                 IF SQL%NOTFOUND THEN
15997                     IF PG_DEBUG in ('Y', 'C') THEN
15998                         msc_sch_wb.atp_debug('ATP_Check: ' || 'update failed, insert a row');
15999                     END IF;
16000                     BEGIN
16001                         insert /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* into msc_atp_summary_sup
16002                                 (plan_id,
16003                                 inventory_item_id,
16004                                 supplier_id,
16005                                 supplier_site_id,
16006                                 sr_instance_id,
16007                                 sd_date,
16008                                 sd_qty,
16009                                 LAST_UPDATE_DATE,
16010                                 LAST_UPDATED_BY,
16011                                 CREATION_DATE,
16012                                 CREATED_BY
16013                                 )
16014                         VALUES
16015                                 (p_plan_id,
16016                                 l_inv_item_id,
16017                                 p_atp_record.supplier_id,
16018                                 p_atp_record.supplier_site_id,
16019                                 p_atp_record.instance_id,
16020                                 trunc(l_atp_date_this_level),
16021                                 -1 * p_atp_record.quantity_ordered,
16022                                 sysdate,
16023                                 FND_GLOBAL.USER_ID,
16024                                 sysdate,
16025                                 FND_GLOBAL.USER_ID);
16026                     EXCEPTION
16027                         WHEN DUP_VAL_ON_INDEX THEN
16028                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_record.instance_id,
16029                                                                 p_plan_id,
16030                                                                 null,
16031                                                                 l_inv_item_id,
16032                                                                 l_atp_date_this_level,
16033                                                                 null,
16034                                                                 null,
16035                                                                 p_atp_record.supplier_id,
16036                                                                 p_atp_record.supplier_site_id,
16037                                                                 null,
16038                                                                 null,
16039                                                                 4);
16040                             update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
16041                             set sd_qty = sd_qty -  p_atp_record.quantity_ordered
16042                             where plan_id = p_plan_id
16043                             and   sr_instance_id = p_atp_record.instance_id
16044                             and   inventory_item_id = l_inv_item_id
16045                             and   supplier_id = p_atp_record.supplier_id
16046                             and   supplier_site_id = p_atp_record.supplier_site_id
16047                             and   sd_date = trunc(l_atp_date_this_level) ;
16048                     END;
16049 
16050                 END IF;
16051                 commit;
16052             END IF; -- if p_search = 1
16053             commit;
16054         END IF; -- IF summary_flag = 'y'
16055         **/
16056 
16057         FOR i in 1..x_atp_period.Level.COUNT LOOP
16058             x_atp_period.Pegging_Id(i) := l_pegging_id;
16059             x_atp_period.End_Pegging_Id(i) := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
16060 
16061         END LOOP;
16062 
16063         IF PG_DEBUG in ('Y', 'C') THEN
16064            msc_sch_wb.atp_debug('ATP_Check: ' || 'in get_res_requirements we are here 2');
16065         END IF;
16066 
16067 	IF p_atp_record.insert_flag <> 0 THEN
16068 	         MSC_ATP_DB_UTILS.move_SD_temp_into_mrp_details(l_pegging_id,
16069 			MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
16070    	END IF;
16071 
16072     END IF;
16073 
16074 IF PG_DEBUG in ('Y', 'C') THEN
16075        msc_sch_wb.atp_debug('ATP_Check: ' || 'children_type: ' || p_atp_record.children_type);
16076        msc_sch_wb.atp_debug('***** End ATP_Check Procedure *****');
16077     END IF;
16078 
16079 
16080 EXCEPTION
16081 
16082     WHEN MSC_ATP_PVT.INVALID_OSS_SOURCE THEN
16083 
16084         IF PG_DEBUG in ('Y', 'C') THEN
16085            msc_sch_wb.atp_debug('ATP_Check: ' || 'Invalid OSS setup detected');
16086         END IF;
16087         p_atp_record.requested_date_quantity := 0;
16088         p_atp_record.available_quantity := 0;
16089         p_atp_record.ship_date := null;
16090         p_atp_record.error_code := MSC_ATP_PVT.OSS_SOURCING_ERROR;
16091         x_return_status := MSC_ATP_PVT.CTO_OSS_ERROR;
16092         --RAISE MSC_ATP_PVT.INVALID_OSS_SOURCE;
16093 
16094     -- 2400614 : krajan: For detecting assignment set mismatch
16095     WHEN MSC_ATP_PVT.G_ATO_SOURCING_MISMATCH THEN
16096         IF PG_DEBUG in ('Y', 'C') THEN
16097            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0');
16098            msc_sch_wb.atp_debug('ATP_Check: ' || 'Source Mismatch detected');
16099         END IF;
16100         p_atp_record.error_code := MSC_ATP_PVT.ASSIGN_SET_NOT_IN_SYNC;
16101         x_return_status := MSC_ATP_PVT.G_ATO_SRC_MISMATCH ;   --- krajan : indicates mismatch
16102                                   --- Primarily for processing in get_comp_requirements
16103     -- dsting 2764213
16104     WHEN MSC_ATP_PVT.EXC_NO_PLAN_FOUND THEN
16105         IF PG_DEBUG in ('Y', 'C') THEN
16106            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0.1');
16107            msc_sch_wb.atp_debug('ATP_Check: Plan not found for the requested item or component');
16108         END IF;
16109         if (NVL(MSC_ATP_PVT.G_DOWNTIME_HIT, 'N') = 'Y') then
16110            --bug 2854351:
16111            --p_atp_record.error_code := MSC_ATP_PVT.TRY_ATP_LATER;
16112            p_atp_record.error_code := MSC_ATP_PVT.PLAN_DOWN_TIME;
16113         else
16114            p_atp_record.error_code := MSC_ATP_PVT.PLAN_NOT_FOUND;
16115         end if;
16116         x_return_status := MSC_ATP_PVT.G_NO_PLAN_FOUND;
16117 
16118     -- krajan 2752705
16119     WHEN MSC_ATP_PVT.G_EXC_UNCOLLECTED_ITEM THEN
16120         IF PG_DEBUG in ('Y', 'C') THEN
16121            msc_sch_wb.atp_debug('something wrong in the ATP_Check 0.2');
16122            msc_sch_wb.atp_debug('ATP_Check: The item is not collected');
16123         END IF;
16124         p_atp_record.error_code := MSC_ATP_PVT.ATP_ITEM_NOT_COLLECTED;
16125         x_return_status := MSC_ATP_PVT.G_ATO_UNCOLL_ITEM;
16126 
16127     WHEN FND_API.G_EXC_ERROR THEN
16128         IF PG_DEBUG in ('Y', 'C') THEN
16129            msc_sch_wb.atp_debug('something wrong in the ATP_Check 1');
16130         END IF;
16131         x_return_status := FND_API.G_RET_STS_ERROR;
16132         IF PG_DEBUG in ('Y', 'C') THEN
16133            msc_sch_wb.atp_debug('ATP_CHECK: IN Error block for G_EXC_ERROR');
16134            msc_sch_wb.atp_debug('ATP_Check: ' || 'Error := ' || SQLerrm);
16135         END IF;
16136 
16137     WHEN NO_MATCHING_DATE_IN_CAL THEN
16138         IF PG_DEBUG in ('Y', 'C') THEN
16139            msc_sch_wb.atp_debug('MAtching cal date not found, in atp_check');
16140         END IF;
16141         p_atp_record.error_code := NO_MATCHING_CAL_DATE;
16142         --x_return_status := FND_API.G_RET_STS_ERROR; --bug3583705
16143         RAISE NO_MATCHING_DATE_IN_CAL;
16144 
16145     /************ Bug 1510853 ATP Rule Check ************/
16146     WHEN  EXC_NO_ATP_RULE THEN
16147         IF PG_DEBUG in ('Y', 'C') THEN
16148            msc_sch_wb.atp_debug('No ATP Rule for Item and Org., in atp_check');
16149         END IF;
16150         p_atp_record.error_code := ATP_BAD_RULE;
16151         x_return_status := FND_API.G_RET_STS_ERROR;
16152 
16153     -- Error Handling Modifications
16154     WHEN MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND THEN
16155         IF PG_DEBUG in ('Y', 'C') THEN
16156            msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
16157            msc_sch_wb.atp_debug('ATP_Check: Invalid Objects found, sqlcode= '||sqlcode);
16158         END IF;
16159 
16160         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16161         IF (p_atp_record.error_code IS NULL) or (p_atp_record.error_code IN (0,61,150)) THEN
16162                 p_atp_record.error_code := ATP_INVALID_OBJECTS;
16163         END IF;
16164         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
16165             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'ATP_Check');
16166         END IF;
16167         RAISE MSC_ATP_PUB.ATP_INVALID_OBJECTS_FOUND;
16168     -- Error Handling Insert End
16169 
16170     -- rajjain 02/20/2003 Bug 2813095
16171     WHEN ALLOC_ATP_INVALID_PROFILE THEN
16172         IF PG_DEBUG in ('Y', 'C') THEN
16173            msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
16174         END IF;
16175         p_atp_record.error_code := INVALID_ALLOC_PROFILE_SETUP;
16176         x_return_status := FND_API.G_RET_STS_ERROR;
16177 
16178     WHEN OTHERS THEN
16179         IF PG_DEBUG in ('Y', 'C') THEN
16180            msc_sch_wb.atp_debug('ATP_CHECK: IN Error block for OTHERS');
16181 	   msc_sch_wb.atp_debug('ATP_Check: ' || 'error := ' || sqlerrm);
16182            msc_sch_wb.atp_debug('something wrong in the ATP_Check 2, sqlcode= '||sqlcode);
16183         END IF;
16184 
16185         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16186 
16187         /* Check if this is actually coming from a calendar routine*/
16188         l_encoded_text := fnd_message.GET_ENCODED;
16189         IF l_encoded_text IS NULL THEN
16190                 l_msg_app := NULL;
16191                 l_msg_name := NULL;
16192         ELSE
16193                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
16194         END IF;
16195 
16196         -- Error Handling Changes
16197         IF (p_atp_record.error_code IS NULL) or (p_atp_record.error_code IN (0,61,150)) THEN
16198                 IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
16199                         p_atp_record.error_code := NO_MATCHING_CAL_DATE;
16200                         IF PG_DEBUG in ('Y', 'C') THEN
16201                            msc_sch_wb.atp_debug('ATP_CHECK: NO_MATCHING_CAL_DATE');
16202                         END IF;
16203                 ELSE
16204                         p_atp_record.error_code := ATP_PROCESSING_ERROR; -- ATP Processing Error
16205                         IF PG_DEBUG in ('Y', 'C') THEN --bug3583705
16206                            msc_sch_wb.atp_debug('ATP_CHECK: ATP_PROCESSING_ERROR');
16207                         END IF;
16208                 END IF;
16209         END IF;
16210 
16211         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
16212             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'ATP_Check');
16213         END IF;
16214 
16215         IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
16216                 RAISE NO_MATCHING_DATE_IN_CAL;
16217         ELSE
16218                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16219         END IF;
16220 END ATP_Check;
16221 
16222 
16223 PROCEDURE Call_Schedule (
16224                p_session_id         IN    NUMBER,
16225                p_atp_table          IN    MRP_ATP_PUB.ATP_Rec_Typ,
16226                p_instance_id        IN    NUMBER,
16227                p_assign_set_id      IN    NUMBER,
16228                p_refresh_number     IN    NUMBER,
16229                x_atp_table          OUT   NoCopy MRP_ATP_PUB.ATP_Rec_Typ,
16230                x_return_status      OUT   NoCopy VARCHAR2,
16231                x_msg_data           OUT   NoCopy VARCHAR2,
16232                x_msg_count          OUT   NoCopy NUMBER,
16233                x_atp_supply_demand  OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
16234                x_atp_period         OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
16235                x_atp_details        OUT   NOCOPY MRP_ATP_PUB.ATP_Details_Typ
16236 ) IS
16237 
16238 i				PLS_INTEGER;
16239 j				PLS_INTEGER;
16240 k                       	PLS_INTEGER;
16241 m                       	PLS_INTEGER;
16242 l_return_status			VARCHAR2(1);
16243 l_atp_input_table 		MRP_ATP_PUB.ATP_Rec_Typ;
16244 l_atp_output_table 		MRP_ATP_PUB.ATP_Rec_Typ;
16245 l_atp_period 			MRP_ATP_PUB.ATP_Period_Typ;
16246 l_atp_supply_demand 		MRP_ATP_PUB.ATP_Supply_Demand_Typ;
16247 l_atp_details 			MRP_ATP_PUB.ATP_Details_Typ;
16248 l_null_atp_table       	     	MRP_ATP_PUB.ATP_Rec_Typ;
16249 l_null_atp_period            	MRP_ATP_PUB.ATP_Period_Typ;
16250 l_null_atp_supply_demand     	MRP_ATP_PUB.ATP_Supply_Demand_Typ;
16251 l_null_atp_details           	MRP_ATP_PUB.ATP_Details_Typ;
16252 l_msg_data 		     	VARCHAR2(2000);
16253 l_msg_text 		     	VARCHAR2(2000);
16254 l_msg_count			NUMBER;
16255 l_msg_number			NUMBER;
16256 plsql_block                     VARCHAR2(10000);
16257 l_dblink                     	VARCHAR2(128);
16258 --For Workflow
16259 l_req_ship_date                 DATE;
16260 l_stealing_qty                  NUMBER;
16261 l_wf_item_key                   VARCHAR2(20);
16262 l_wf_profile			VARCHAR2(1);
16263 l_plan_id                       NUMBER;
16264 -- Override Workflow Variables
16265 l_avail_date                    DATE;
16266 l_wf_start                      VARCHAR2(1);
16267 
16268 /* Bug 2290633 */
16269 l_delivery_lead_time            NUMBER;
16270 l_from_location_id              NUMBER;
16271 l_from_location                 NUMBER;
16272 l_to_location_id                NUMBER;
16273 l_to_location                   NUMBER;
16274 l_ship_method                   VARCHAR2(30);
16275 l_sysdate                       DATE;
16276 L_PREV_WORK_SHIP_DATE           DATE;
16277 l_requested_ship_date           DATE;
16278 /* End Bug 2290633 */
16279 
16280 -- ATP Override rajjain begin 10/03/2002 for generating exceptions
16281 l_exception_rec			MSC_ATP_EXCEPTIONS.ATP_Exception_Rec_Typ;
16282 l_plan_type			NUMBER := 0;
16283 l_generate_exception		VARCHAR2(1);
16284 l_exception_return_status	VARCHAR2(2);
16285 -- ATP Override rajjain end
16286 
16287 -- rajjain 02/06/2003 Bug 2781863
16288 l_inventory_item_id		NUMBER;
16289 -- 24x7
16290 l_instance_id                   NUMBER;
16291 
16292 -- rajjain 02/03/2003 Bug 2766713
16293 l_set_fail_flag			VARCHAR2(1) := 'N';
16294 
16295 --s_cto_rearch
16296 l_included_item_table               MRP_ATP_PUB.ATP_Rec_Typ;
16297 l_included_item_cntr                number;
16298 l_mand_comp_info_rec                MSC_ATP_CTO.mand_comp_info_rec;
16299 l_null_mand_comp_info_rec           MSC_ATP_CTO.mand_comp_info_rec;
16300 l_pto_cntr                          number;
16301 l_count                             number;
16302 l_parent_idx                        number;
16303 l_process_included_item_flag        number := 0;
16304 --e_cto_rearch
16305 
16306 -- ship_rec_cal
16307 l_shipping_cal_code		VARCHAR2(14);
16308 l_receiving_cal_code		VARCHAR2(14);
16309 l_manufacturing_cal_code	VARCHAR2(14);
16310 l_intransit_cal_code		VARCHAR2(14);
16311 
16312 l_quantity_uom		VARCHAR2(3);
16313 l_quantity_ordered	NUMBER;
16314 l_conversion_rate       NUMBER;
16315 --bug3583705
16316 l_encoded_text                  varchar2(4000);
16317 l_msg_app                       varchar2(50);
16318 l_msg_name                      varchar2(30);
16319 
16320 l_global_availability                         varchar2(1); --3720018
16321 l_temp                                        VARCHAR2(1); --3720018
16322 l_null_remove_req_level_rec                   MSC_ATP_PVT.remove_request_level_rec; --3720018
16323 l_old_source_org_id                           NUMBER; --3720018
16324 l_delete_atp_rec                              MSC_ATP_PVT.DELETE_ATP_REC ;--3720018
16325 l                                             NUMBER; --3720018
16326 x_plan_info_rec                               MSC_ATP_PVT.plan_info_rec ;
16327 l_same_org_count                              NUMBER;
16328 l_feeder_count                                NUMBER;
16329 l_consume_so_profile_value                    NUMBER;
16330 
16331 BEGIN
16332 
16333     IF PG_DEBUG in ('Y', 'C') THEN
16334        msc_sch_wb.atp_debug('***** Begin Call_Schedule Procedure *****');
16335     END IF;
16336 
16337     -- Initialize global variables to work in a connection pooling env
16338     G_INV_CTP          := FND_PROFILE.value('INV_CTP') ;
16339     G_SUB_COMP         := NVL(FND_PROFILE.value('MRP_ATP_SUB_COMP'), 'N');
16340     G_ALLOCATED_ATP    := NVL(FND_PROFILE.value('MSC_ALLOCATED_ATP'),'N');
16341     G_HIERARCHY_PROFILE:= NVL(FND_PROFILE.VALUE('MSC_CLASS_HIERARCHY'), 2);
16342     G_INF_SUP_TF_PAD  :=  GREATEST(CEIL(NVL(FND_PROFILE.VALUE('MSC_ATP_INFINITE_TF_PAD'),0)),0);
16343 
16344     --bug 3625348: reset global variable
16345     MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 2;
16346     MSC_ATP_CTO.G_MODEL_IS_PRESENT  := 2;
16347     MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET  := 2;
16348     MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT  := 2;
16349 
16350     -- ngoel 9/27: Reset G_SR_PARTNER_SITE_ID GLOBAL Variable
16351     G_SR_PARTNER_SITE_ID := NULL;
16352 
16353     -- 24x7
16354     G_DOWNTIME_HIT := 'N';
16355 
16356 
16357     MSC_ATP_PVT.G_INSTANCE_ID := p_instance_id;
16358 
16359     G_TIME_PHASED_SET := 'N';   -- For summary enhancement
16360 
16361     -- initialize API return status to success
16362     x_return_status := FND_API.G_RET_STS_SUCCESS;
16363 
16364     -- Bug 2356892
16365     IF PG_DEBUG in ('Y', 'C') THEN
16366        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Check_CTO: G_INV_CTP : '||G_INV_CTP);
16367     END IF;
16368     IF (NVL(G_INV_CTP,-1) not in (4,5)) THEN
16369         G_INV_CTP := 5;
16370         IF PG_DEBUG in ('Y', 'C') THEN
16371            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Check_CTO: Setting G_INV_CTP to 5');
16372         END IF;
16373     END IF;
16374 
16375     --3720018, setting l_global_availability flag based on duplicity of order_line_id.
16376 
16377     BEGIN
16378     l_global_availability := 'Y';
16379     select 'x'
16380        into l_temp
16381        from mrp_atp_schedule_temp
16382        where session_id = p_session_id
16383        group by order_line_id
16384        having count(*) > 1;
16385     EXCEPTION
16386       WHEN no_data_found THEN
16387           l_global_availability := 'N';
16388           IF PG_DEBUG in ('Y', 'C') THEN
16389              --bug4291375 changed debug statements
16390              --msc_sch_wb.atp_debug('Exception := ' || SQLERRM);
16391              msc_sch_wb.atp_debug('Setting l_global_availability as No' );
16392           END IF;
16393       WHEN TOO_MANY_ROWS THEN
16394           l_global_availability := 'Y';
16395           IF PG_DEBUG in ('Y', 'C') THEN
16396              --bug4291375 changed debug statements
16397              --msc_sch_wb.atp_debug('Exception := ' || SQLERRM);
16398              msc_sch_wb.atp_debug('Setting l_global_availability as Yes' );
16399           END IF;
16400     END;
16401 
16402     IF PG_DEBUG in ('Y', 'C') THEN
16403           msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_global_availability = ' || l_global_availability );
16404     END IF; --3720018
16405 
16406     -- initial null collections so that we can use these
16407     -- collections to initialize collections for next iterations
16408     MSC_ATP_PVT.G_SESSION_ID := p_session_id;
16409     IF PG_DEBUG in ('Y', 'C') THEN
16410        msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_SESSION_ID = '||MSC_ATP_PVT.G_SESSION_ID);
16411     END IF;
16412 
16413     l_null_atp_table := x_atp_table;
16414     l_null_atp_period := x_atp_period;
16415     l_null_atp_supply_demand := x_atp_supply_demand;
16416     l_null_atp_details := x_atp_details;
16417 
16418     --alloc_pp
16419     IF MSC_ATP_PVT.G_INV_CTP = 4 AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' AND
16420        MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1 THEN
16421 
16422         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'), 2);
16423         IF PG_DEBUG in ('Y', 'C') THEN
16424            msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
16425         END IF;
16426     END IF;
16427 
16428     --3720018, call in case of ATP inquiry or global availability from scheduled line of Sales Order pad.
16429     IF ((p_atp_table.Action(1) = ATPQUERY) AND
16430         (NVL(p_atp_table.calling_module(1), -99) <> 724)) OR
16431         ((p_atp_table.Action(1) = ATPQUERY) AND
16432         (NVL(p_atp_table.calling_module(1), -99) = -99) AND
16433         (l_global_availability = 'Y'))  THEN
16434 
16435             -- Call_delete_row will take care of calling delete_row for the whole request.
16436             -- The input to this procedure is p_atp_table.
16437             -- l_delete_atp_rec will contain array to be used to call undo_delete_row.
16438             MSC_ATP_DB_UTILS.call_delete_row (
16439                              p_instance_id,
16440                              p_atp_table,
16441                              p_refresh_number,
16442                              l_delete_atp_rec,
16443                              l_return_status);
16444 
16445             IF PG_DEBUG in ('Y', 'C') THEN --3720018
16446                  msc_sch_wb.atp_debug('Call_Schedule: l_return_status: ' || l_return_status);
16447                  msc_sch_wb.atp_debug('Call_Schedule: l_delete_atp_rec.error_code(1): ' || l_delete_atp_rec.error_code(1));
16448             END IF;
16449             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
16450                         IF PG_DEBUG in ('Y', 'C') THEN
16451                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Error in Call_delete_row');
16452                         END IF;
16453                         x_return_status := FND_API.G_RET_STS_ERROR;
16454                         RAISE FND_API.G_EXC_ERROR;
16455             END IF;
16456             IF (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.TRY_ATP_LATER) OR
16457                (l_delete_atp_rec.error_code(1) = MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING) THEN
16458                    x_atp_table := p_atp_table;
16459                    FOR i in 1..x_atp_table.action.count LOOP
16460                       x_atp_table.error_code(i) := l_delete_atp_rec.error_code(1);
16461                    END LOOP;
16462                RETURN;
16463             END IF;
16464     END IF;
16465     -- 3720018
16466 
16467     -- start from the first row of the atp list
16468     -- for each row , arrival set or ship set, call Schedule function
16469     -- we assume that the records are ordered in the way you would
16470     -- like scheduling to consider them.
16471     -- And we also assume that when we have ship set or arrival set,
16472     -- the lines in each set are grouped together.
16473     -- Assume a line is either in a ship set or in an arrival set
16474     -- but not both.
16475 
16476     i := p_atp_table.Action.FIRST;
16477 
16478     IF PG_DEBUG in ('Y', 'C') THEN
16479        msc_sch_wb.atp_debug('Call_Schedule, i='||i);
16480            msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_ALLOCATION_METHOD := ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
16481         END IF;
16482 
16483     LOOP
16484       -- make sure we set the variables to null so that it is clean
16485       -- before we assign new value to them.
16486       l_atp_input_table := l_null_atp_table;
16487       l_atp_output_table := l_null_atp_table;
16488       l_atp_period := l_null_atp_period;
16489       l_atp_supply_demand := l_null_atp_supply_demand;
16490       l_atp_details := l_null_atp_details;
16491        --s_cto_rearch
16492       --l_mand_comp_info_rec := l_null_mand_comp_info_rec;
16493       l_set_fail_flag := 'N';
16494       MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET := 2;
16495       --e_cto_rearch
16496 
16497 
16498       IF PG_DEBUG in ('Y', 'C') THEN
16499          msc_sch_wb.atp_debug('Call_Schedule, i='||i);
16500       END IF;
16501 
16502       EXIT WHEN i is NULL;
16503       /* ---s_cto_rearch
16504       MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
16505                            i,
16506                            l_atp_input_table,
16507                            l_return_status );
16508 
16509       j := p_atp_table.Action.NEXT(i);
16510       e_cto_rearch */
16511 
16512       ---s_cto_rearch
16513 
16514 
16515       IF  NOT (l_mand_comp_info_rec.sr_inventory_item_id.count = 0) AND
16516                       l_included_item_cntr < l_mand_comp_info_rec.sr_inventory_item_id.count THEN
16517           ---process the included item for non SMC PTO model.
16518            IF PG_DEBUG in ('Y', 'C') THEN
16519               msc_sch_wb.atp_debug('Process Included item');
16520               msc_sch_wb.atp_debug('l_included_item_cntr := ' || l_included_item_cntr);
16521            END IF;
16522 
16523           l_included_item_cntr := nvl(l_included_item_cntr, 0) + 1;
16524           MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT := 1;
16525 
16526           MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
16527                                   j,
16528                                   l_atp_input_table,
16529                                   l_return_status );
16530 
16531 
16532           l_atp_input_table.mandatory_item_flag(1) := 1;
16533           l_atp_input_table.quantity_ordered(1) :=
16534                                             l_mand_comp_info_rec.quantity(l_included_item_cntr);
16535 
16536           l_atp_input_table.inventory_item_id(1) :=
16537                                             l_mand_comp_info_rec.sr_inventory_item_id(l_included_item_cntr);
16538 
16539           l_atp_input_table.parent_line_id(1) := p_atp_table.identifier(j);
16540           l_atp_input_table.sequence_number(1) :=  p_atp_table.sequence_number(j) * (1 + (.0001 * l_included_item_cntr));
16541 
16542           l_atp_input_table.Quantity_UOM(1) := l_mand_comp_info_rec.uom_code(l_included_item_cntr);
16543 
16544           IF l_included_item_cntr = l_mand_comp_info_rec.sr_inventory_item_id.count THEN
16545                 l_mand_comp_info_rec := l_null_mand_comp_info_rec;
16546                 j := p_atp_table.Action.NEXT(j);
16547           END IF;
16548 
16549           l_process_included_item_flag := 0;
16550       ELSE
16551          j := i;
16552          LOOP --- loop for finding lines for a set
16553 
16554             EXIT WHEN l_process_included_item_flag = 1;
16555 
16556             IF PG_DEBUG in ('Y', 'C') THEN
16557                 msc_sch_wb.atp_debug('Process ordered item');
16558             END IF;
16559 
16560             ---either  this is first item or PTO included items have already been ATPed. Null out the table
16561             l_mand_comp_info_rec := l_null_mand_comp_info_rec;
16562 
16563             MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
16564                                   j,
16565                                   l_atp_input_table,
16566                                   l_return_status );
16567 
16568             IF p_atp_table.ato_model_line_id(j) is not null THEN --and p_atp_table.bom_item_type(j) = 1 THEN
16569                 --set a flag to tell that a model is present.
16570                 -- this flag will be used in put into temp table in results mode to
16571                 -- selectively update the data for child lines.
16572                 IF PG_DEBUG in ('Y', 'C') THEN
16573                    msc_sch_wb.atp_debug('ato model -line is := ' || p_atp_table.ato_model_line_id(j));
16574                 END IF;
16575                 MSC_ATP_CTO.G_MODEL_IS_PRESENT := 1;
16576                 MSC_ATP_CTO.G_MODEL_IS_PRESENT_IN_SET := 1;
16577 
16578 
16579                 IF NVL(MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED, 2) = 2 and
16580                                    (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'N' ) THEN
16581 
16582                    MSC_ATP_CTO.Populate_CTO_Bom(MSC_ATP_PVT.G_SESSION_ID, p_refresh_number, null);
16583                    MSC_ATP_CTO.G_MODEL_BOM_IS_COLLECTED := 1;
16584 
16585                 END IF;
16586             END IF;
16587 
16588             IF PG_DEBUG in ('Y', 'C') THEN
16589                  msc_sch_wb.atp_debug('attribute_06 := ' ||  NVL(p_atp_table.attribute_06(j), -1));
16590                  msc_sch_wb.atp_debug('Validation Org := ' || NVL(p_atp_table.validation_org(j), -1));
16591                  msc_sch_wb.atp_debug('source_organization_id := ' || NVL(p_atp_table.source_organization_id(j), -1));
16592             END IF;
16593 
16594             --- raise invalid item org combo error only for non-aps instance
16595             IF (p_atp_table.attribute_06(j) is null and NVL(MSC_ATP_PVT.G_CALLING_MODULE, -1) <> 724
16596                 -- if validation org and source_org are null then we cannot validate the item
16597                 -- hence do not raise the error
16598                 and (p_atp_table.validation_org(j) is not null
16599                 or p_atp_table.source_organization_id(j) is not null))  or
16600                p_atp_table.oss_error_code(j) in ( MSC_ATP_PVT.MUTUALLY_EXCLUSIVE_OSS,
16601                                                   MSC_ATP_PVT.OSS_ERROR_AT_LOWER_LEVEL,
16602                                                   MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL) or
16603                l_set_fail_flag = 'Y'  THEN
16604 
16605                l_count := l_atp_input_table.inventory_item_id.count;
16606 
16607                If p_atp_table.attribute_06(j) is null THEN
16608                     IF PG_DEBUG in ('Y', 'C') THEN
16609                         msc_sch_wb.atp_debug('Invalid Item Org Combo');
16610                     END IF;
16611 
16612 
16613                     l_atp_input_table.error_code(l_count) := INVALID_ITEM_ORG_COMBINATION;
16614 
16615                ELSIF p_atp_table.oss_error_code(j) in  (MSC_ATP_PVT.MUTUALLY_EXCLUSIVE_OSS,
16616                                                   MSC_ATP_PVT.OSS_ERROR_AT_LOWER_LEVEL,
16617                                                   MSC_ATP_PVT.OSS_ERROR_AT_THIS_LEVEL) THEN
16618 
16619                     IF PG_DEBUG in ('Y', 'C') THEN
16620                        msc_sch_wb.atp_debug('Inavlid CTO sourcing');
16621                     END IF;
16622                     l_atp_input_table.error_code(l_count) := p_atp_table.oss_error_code(j);
16623 
16624                ELSE
16625                     l_atp_input_table.error_code(l_count) := GROUPEL_ERROR;
16626 
16627                END IF;
16628                l_set_fail_flag := 'Y';
16629 
16630                j := p_atp_table.Action.NEXT(j);
16631 
16632             ELSIF NVL(p_atp_table.included_item_flag(j), 1) = 2 and
16633                 -- calling module must pass 2 else we will assume that
16634                 -- calling module has already exploded the included items
16635                 -- This is to provided backward compatibility
16636                 p_atp_table.pick_components_flag(j) = 'Y' and
16637                 p_atp_table.bom_item_type(j) in (1, 2)  THEN
16638 
16639                 IF PG_DEBUG in ('Y', 'C') THEN
16640                     msc_sch_wb.atp_debug('Explode Included Items');
16641                     msc_sch_wb.atp_debug('First Do UOM Convesrion');
16642                 END IF;
16643 
16644                 MSC_ATP_PROC.inv_primary_uom_conversion(p_instance_id,
16645                                        p_atp_table.validation_org(j),
16646                                        p_atp_table.inventory_item_id(j),
16647                                        p_atp_table.Quantity_UOM(i),
16648                                        l_quantity_uom,
16649                                        l_conversion_rate);
16650 
16651                 IF PG_DEBUG in ('Y', 'C') THEN
16652                     msc_sch_wb.atp_debug('l_conversion_rate');
16653                     msc_sch_wb.atp_debug('QTY Ordered := ' ||  p_atp_table.Quantity_Ordered(j));
16654 
16655                 END IF;
16656 
16657                 l_quantity_ordered := p_atp_table.Quantity_Ordered(j) * l_conversion_rate;
16658                 l_quantity_ordered := ROUND( l_quantity_ordered, 6); --4562646
16659                 IF PG_DEBUG in ('Y', 'C') THEN
16660                     msc_sch_wb.atp_debug('QTY Ordered  after conversion:= ' ||  l_quantity_ordered);
16661                     msc_sch_wb.atp_debug('Now call Get Mand Comp');
16662                 END IF;
16663 
16664                 --- included items are not passed. Explode them
16665                 MSC_ATP_PVT.G_PTF_DATE := sysdate;
16666                 MSC_ATP_CTO.Get_Mandatory_Components( -1,
16667                                                      p_instance_id,
16668                                                      p_atp_table.validation_org(j),
16669                                                      p_atp_table.inventory_item_id(j),
16670                                                      p_atp_table.quantity_ordered(j),
16671                                                      NVL(p_atp_table.Requested_Ship_Date(j),
16672                                                              p_atp_table.Requested_Arrival_Date(j)),
16673                                                      null,
16674                                                      l_mand_comp_info_rec);
16675 
16676                 --- if SMC PTO then add to l_atp_input_table else add to l_included_item_table
16677                 IF PG_DEBUG in ('Y', 'C') THEN
16678                     msc_sch_wb.atp_debug('After getting included items');
16679                     msc_sch_wb.atp_debug('Ship set name := ' ||  NVL(p_atp_table.Ship_Set_Name(j), 'XYZ'));
16680                 END IF;
16681 
16682                 IF p_atp_table.Ship_Set_Name(j) is not null then
16683 
16684                    --its an SMC PTO or KIT, add to the same set
16685 
16686                    IF PG_DEBUG in ('Y', 'C') THEN
16687                        msc_sch_wb.atp_debug('Comp Count := ' || l_mand_comp_info_rec.sr_inventory_item_id.count);
16688                        msc_sch_wb.atp_debug('SMC or PTO Kit');
16689                    END IF;
16690                    FOR l_pto_cntr in 1..l_mand_comp_info_rec.sr_inventory_item_id.count LOOP
16691                         MSC_ATP_CTO.G_INCLUDED_ITEM_IS_PRESENT := 1;
16692                         MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
16693                                   j,
16694                                   l_atp_input_table,
16695                                   l_return_status );
16696                         IF PG_DEBUG in ('Y', 'C') THEN
16697                             msc_sch_wb.atp_debug('l_pto_cntr := ' || l_pto_cntr);
16698                         END IF;
16699                         --cr:ngoel: add seq num as well as UOM code
16700                         l_count := l_atp_input_table.inventory_item_id.count;
16701                         l_atp_input_table.mandatory_item_flag(l_count) := 1;
16702                         l_atp_input_table.quantity_ordered(l_count) :=
16703                                            l_mand_comp_info_rec.quantity(l_pto_cntr);
16704 
16705                         l_atp_input_table.inventory_item_id(l_count) :=
16706                                                           l_mand_comp_info_rec.sr_inventory_item_id(l_pto_cntr);
16707 
16708                         l_atp_input_table.parent_line_id(l_count) := p_atp_table.identifier(j);
16709                         IF PG_DEBUG in ('Y', 'C') THEN
16710                             msc_sch_wb.atp_debug('Assign Seq num');
16711                         END IF;
16712                         ---multiplying by .000001 will make sure that std mand comp are inserted in the
16713                         -- table after the parent
16714                         begin
16715                            l_atp_input_table.sequence_number(l_count) := p_atp_table.sequence_number(j) *
16716                                                                                            (1 + (.0001 * l_pto_cntr));
16717                         EXCEPTION
16718                              when others then
16719                                 IF PG_DEBUG in ('Y', 'C') THEN
16720                                     msc_sch_wb.atp_debug('Error := ' || SQLERRM);
16721                                 END IF;
16722                         END;
16723                         IF PG_DEBUG in ('Y', 'C') THEN
16724                             msc_sch_wb.atp_debug('assign UOM');
16725                             msc_sch_wb.atp_debug('seq count := ' || l_atp_input_table.sequence_number.count);
16726                             msc_sch_wb.atp_debug(' uom cnt := ' || l_atp_input_table.Quantity_UOM.count);
16727                             msc_sch_wb.atp_debug('uom code cnt := ' || l_mand_comp_info_rec.uom_code.count);
16728                         END IF;
16729                         l_atp_input_table.Quantity_UOM(l_count) := l_mand_comp_info_rec.uom_code(l_pto_cntr);
16730                         IF p_atp_table.bom_item_type(j)  = 4 and p_atp_table.Ship_Set_Name(j) is null THEN
16731                            IF PG_DEBUG in ('Y', 'C') THEN
16732                                msc_sch_wb.atp_debug('process KIT');
16733                            END IF;
16734                             -- If its a PTO KIt  and if ship set name is null then we populate
16735                             --shipset name so that whole kit is processed as one entity
16736                             l_atp_input_table.Ship_Set_Name(l_parent_idx) := p_atp_table.identifier(j);
16737                             l_atp_input_table.Ship_Set_Name(l_count) :=  p_atp_table.identifier(j);
16738                         END IF;
16739 
16740                    END LOOP;
16741 
16742                    ---null out mand_comp_table
16743                    l_mand_comp_info_rec := l_null_mand_comp_info_rec;
16744                    j := p_atp_table.Action.NEXT(j);
16745                 ELSE ---  IF p_atp_table.Ship_Set_Name(j) is not null then
16746                    IF PG_DEBUG in ('Y', 'C') THEN
16747                        msc_sch_wb.atp_debug('This is not SMC PTO');
16748                    END IF;
16749                    --non smc PTO
16750                    l_included_item_cntr := 0;
16751                    IF l_mand_comp_info_rec.sr_inventory_item_id.count > 0 THEN
16752                       l_process_included_item_flag := 1;
16753                    ELSE
16754                       j := p_atp_table.Action.NEXT(j);
16755                    END IF;
16756                 END IF;
16757 
16758             ELSE
16759                ---increase the counter only if its a non PTO item or icluded item already exploded
16760                j := p_atp_table.Action.NEXT(j);
16761             END IF;
16762 
16763 
16764             IF (j IS NULL) THEN
16765                EXIT;
16766             END IF;
16767 
16768             /* Fixed as part of ship_rec_cal/sup int LT
16769             Prev condition was not handling arrival sets
16770             EXIT WHEN NVL(p_atp_table.Ship_Set_Name(i),-99) <> NVL(p_atp_table.Ship_Set_Name(j),-100) OR
16771                        NVL(p_atp_table.Arrival_Set_Name(i),-100) <> NVL(p_atp_table.Arrival_Set_Name(j),-100);*/
16772             EXIT WHEN (NVL(p_atp_table.Ship_Set_Name(i),-99) <> NVL(p_atp_table.Ship_Set_Name(j),-100) OR
16773                         -- Bug 3729551, added for support of Global availability for a shipset from OM
16774                         NVL(p_atp_table.source_organization_id(i), -1) <> NVL(p_atp_table.source_organization_id(j), -1)) AND
16775                         NVL(p_atp_table.Arrival_Set_Name(i),-99) <> NVL(p_atp_table.Arrival_Set_Name(j),-100);
16776 
16777          END LOOP;   --- end loop for finding lines for a set
16778       END IF;---IF l_included_item_cntr <= l_m
16779       ---e_cto_reach
16780 
16781       /* s_cto_rearch --- modify the way we do the looping for CTO project
16782       IF (p_atp_table.Ship_Set_Name(i) is not NULL
16783           OR  p_atp_table.Arrival_Set_Name(i) is not NULL)
16784           AND  j is not null  THEN
16785 
16786         LOOP
16787           -- Bug 1868923, Added NVL for ship/ arrival set exit conditions.
16788 
16789           EXIT WHEN NVL(p_atp_table.Ship_Set_Name(i),-99) <> NVL(p_atp_table.Ship_Set_Name(j),-99) OR
16790                     NVL(p_atp_table.Arrival_Set_Name(i),-99) <> NVL(p_atp_table.Arrival_Set_Name(j),-99);
16791 
16792 
16793           IF PG_DEBUG in ('Y', 'C') THEN
16794              msc_sch_wb.atp_debug('Call_Schedule, j='||j);
16795           END IF;
16796 
16797           -- make sure we are in the same ship set before we assign the value.
16798 
16799           MSC_SATP_FUNC.Assign_Atp_Input_Rec(p_atp_table,
16800                                j,
16801                                l_atp_input_table,
16802                                l_return_status );
16803 
16804           j := p_atp_table.Action.NEXT(j);
16805 
16806           IF (j IS NULL) THEN
16807             EXIT;
16808           END IF;
16809 
16810 
16811         END LOOP;
16812 
16813       END IF;
16814 
16815       e_cto_rearch */
16816       -- Added by ngoel on 1/23/2001.
16817       -- In case of PDS, check if this line/set is due to an internal Sales Order.
16818       -- Check OE_Flag='Y', if internal sales order, no ATP is required to be done
16819       -- as the supply has already been reserved for items in this line/set.
16820 
16821       m := l_atp_input_table.Action.FIRST;
16822       IF m is not null THEN
16823 	 IF PG_DEBUG in ('Y', 'C') THEN
16824 	    msc_sch_wb.atp_debug('Call_Schedule: ' || 'OE Flag (Before ISO check) : '||l_atp_input_table.OE_flag(m));
16825 	 END IF;
16826       --Bug 4279623 	4333596
16827       IF l_atp_input_table.oe_flag(m) = 'Y' and MSC_ATP_PVT.G_INV_CTP = 4 THEN
16828          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
16829          --l_consume_so_profile_value :=  NVL(FND_PROFILE.VALUE('MSC_CONSUME_INT_SALES_ORDERS'), NO);
16830          IF PG_DEBUG in ('Y', 'C') THEN
16831             msc_sch_wb.atp_debug('Before get_plan_info_call:l_consume_so_profile_value ' || l_consume_so_profile_value);
16832             msc_sch_wb.atp_debug('Before get_plan_info_call:MSC_CONSUME_INT_SALES_ORDERS ' || l_consume_so_profile_value);
16833             msc_sch_wb.atp_debug('New profile: '||FND_PROFILE.VALUE('MSC_ATP_CHECK_INT_SALES_ORDERS')); --5564082
16834          END IF;
16835          IF  l_consume_so_profile_value  = CHECK_ORG_IN_PLAN THEN
16836              MSC_ATP_PROC.get_plan_info(p_instance_id,
16837                                         l_atp_input_table.inventory_item_id(m),
16838                                         l_atp_input_table.source_organization_id(m),
16839                                         null,
16840                                         x_plan_info_rec);
16841 
16842              IF PG_DEBUG in ('Y', 'C') THEN
16843                 msc_sch_wb.atp_debug('After get_plan_info_call:plan_id : ' || x_plan_info_rec.plan_id );
16844              END IF;
16845 
16846              IF x_plan_info_rec.plan_id > 0 THEN
16847 
16848                 IF PG_DEBUG in ('Y', 'C') THEN
16849                    msc_sch_wb.atp_debug('Plan found for the item in the source org.');
16850                    msc_sch_wb.atp_debug('l_atp_input_table.inventory_item_id(m)' || l_atp_input_table.inventory_item_id(m) );
16851                    msc_sch_wb.atp_debug('l_atp_input_table.internal_org_id(m)' || l_atp_input_table.internal_org_id(m) );
16852                    msc_sch_wb.atp_debug('l_atp_input_table.source_organization_id(m)' || l_atp_input_table.source_organization_id(m) );
16853                    msc_sch_wb.atp_debug('l_atp_input_table.organization_id(m)' || l_atp_input_table.organization_id(m) );
16854                 END IF;
16855 
16856                 select count(*)
16857                 into l_same_org_count
16858                 from msc_system_items i
16859                 where i.plan_id = x_plan_info_rec.plan_id
16860                 and i.organization_id = l_atp_input_table.internal_org_id(m)
16861                 and i.sr_instance_id = p_instance_id
16862                 and i.sr_inventory_item_id = l_atp_input_table.inventory_item_id(m);
16863 
16864                 IF l_same_org_count > 0 THEN
16865                    l_atp_input_table.oe_flag(m) := 'Y';
16866                    IF PG_DEBUG in ('Y', 'C') THEN
16867                       msc_sch_wb.atp_debug('Both the Orgs are in the same plan and the item is planned.');
16868                    END IF;
16869                 ELSE
16870                    IF PG_DEBUG in ('Y', 'C') THEN
16871                       msc_sch_wb.atp_debug('Both the Orgs are in NOT the same plan');
16872                    END IF;
16873                    select count(*) into l_feeder_count
16874                    from msc_system_items
16875                    where plan_id  in ( select plans.plan_id input_plan_id
16876                                            from msc_plan_schedules sched,
16877                                            msc_designators md,
16878                                            msc_plans plans
16879                                            where sched.plan_id = x_plan_info_rec.plan_id
16880                                            and sched.input_type = 1
16881                                            and sched.designator_type in (2,3,4,5)
16882                                            and sched.input_schedule_id = md.designator_id
16883                                            -- we need not consider plans collected from source
16884                                            and md.collected_flag = 2
16885                                            and md.designator = plans.compile_designator
16886                                            and md.sr_instance_id = plans.sr_instance_id
16887                                            and md.organization_id = plans.organization_id
16888                                          )
16889                    and organization_id = l_atp_input_table.internal_org_id(m)
16890                    and sr_instance_id = p_instance_id
16891                    and sr_inventory_item_id =  l_atp_input_table.inventory_item_id(m);
16892 
16893                   IF l_feeder_count > 0 then
16894                      l_atp_input_table.oe_flag(m) := 'Y';
16895                      IF PG_DEBUG in ('Y', 'C') THEN
16896                         msc_sch_wb.atp_debug('There is a feeder plan which has the item palnned. we need not do ATP');
16897                      END IF;
16898                   ELSE
16899                      l_atp_input_table.oe_flag(m) := 'N';
16900                      IF PG_DEBUG in ('Y', 'C') THEN
16901                         msc_sch_wb.atp_debug('There is no feeder plan too. We have to do ATP');
16902 	 END IF;
16903                   END IF; -- l_feeder_count > 0
16904                 END IF; --if l_same_org_count > 0 then
16905              ELSE
16906                l_atp_input_table.oe_flag(m) := 'Y';
16907                IF PG_DEBUG in ('Y', 'C') THEN
16908                   msc_sch_wb.atp_debug('The item is not planned at all. No need to do ATP');
16909                END IF;
16910              END IF; -- x_plan_info_rec.count > 0
16911          ELSIF l_consume_so_profile_value = YES THEN
16912             l_atp_input_table.oe_flag(m) := 'N';
16913             IF PG_DEBUG in ('Y', 'C') THEN
16914                msc_sch_wb.atp_debug('Profile value is set to CHECK ATP. We need to do ATP always.');
16915             END IF;
16916 
16917          END IF; -- nvl(fnd_profile.value('msc_consume_int_sales_orders'), '2') = 3
16918       END IF; -- l_atp_input_table.oe_flag(1) = 'y' and msc_atp_pvt.g_inv_ctp = 4
16919      --Bug 4279623 	4333596
16920         msc_sch_wb.atp_debug('l_atp_input_table.oe_flag(m) = '||l_atp_input_table.oe_flag(m)); --5564082
16921 	 IF l_atp_input_table.OE_flag(m) = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
16922 
16923 	    IF PG_DEBUG in ('Y', 'C') THEN
16924 	       msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Internal Sales order import for OM');
16925 	    END IF;
16926 
16927 	    x_atp_table := l_atp_input_table;
16928            BEGIN --bug3583705
16929             WHILE m IS NOT NULL LOOP
16930                 /* Bug 3449812 - Documenting the logic of plan suggested internal sales orders
16931                    1.   If DLT is not available then obtain it.
16932                    2.   Get all the necessary calendars.
16933                    3.   If date type = ship then
16934                    4.       Obtain ship date by validating RSD on OSC.
16935                    5.   ELse
16936                    6.       Calculate ship date from RAD honoring all calendars.
16937                    7.   End if
16938                    8.   If override then
16939                    9.       If date type = ship then
16940                   10.           ship date = RSD
16941                   11.       Else
16942                   12.           arrival date = RAD
16943                   13.           ship date = Calculate ship date from RAD honoring all calendars.
16944                   14.       End if
16945                   15.   Else
16946                   16.       ship date = greatest(ship date, sysdate)
16947                   17.   End if
16948                   18.   If arrival date is null then calculate it from ship date.
16949                 */
16950 
16951                 /* Bug 2290633 : krajan : 04/05/02 */
16952 
16953                 l_delivery_lead_time := l_atp_input_table.delivery_lead_time(m);
16954 
16955                 -- dsting dlt
16956                 IF (NVL(l_atp_input_table.delivery_lead_time(m), -1) = -1 ) THEN
16957                    IF PG_DEBUG IN ('Y', 'C') THEN
16958                       msc_sch_wb.atp_debug('delivery lead time is not specified');
16959                    END IF;
16960 
16961                    l_ship_method := x_atp_table.ship_method(m);
16962                    -- Bug 3449812 - Base DLT on internal_org_id if available
16963                    -- Bug 3515520, don't use org in case customer/site is populated
16964                    -- IF x_atp_table.internal_org_id(m) IS NOT NULL THEN
16965 
16966                    IF (x_atp_table.internal_org_id(m) IS NOT NULL) OR
16967                       (x_atp_table.organization_id(m) IS NOT NULL AND
16968                        x_atp_table.customer_id(m) IS NULL AND x_atp_table.customer_site_id(m) IS NULL) THEN
16969                        MSC_ATP_PROC.get_delivery_lead_time(
16970                                     x_atp_table.source_organization_id(m),
16971                                     NULL, -- from_loc
16972                                     p_instance_id,
16973                                     -- x_atp_table.internal_org_id(m), -- Bug 3515520
16974                                     NVL(x_atp_table.internal_org_id(m), x_atp_table.organization_id(m)), --Bug 3515520
16975                                     NULL, -- to loc
16976                                     p_instance_id,
16977                                     NULL, -- customer id
16978                                     NULL, -- customer site id
16979                                     NULL, -- supplier_id
16980                                     NULL, -- supplier_site_id
16981                                     MSC_ATP_PVT.G_SESSION_ID,
16982                                     NULL, -- partner site id
16983                                     l_ship_method,
16984                                     l_delivery_lead_time
16985                                     );
16986                    ELSE
16987                        MSC_ATP_PROC.get_delivery_lead_time(
16988                                     x_atp_table.source_organization_id(m),
16989                                     NULL, -- from_loc
16990                                     p_instance_id,
16991                                     -- x_atp_table.organization_id(m), --Bug 3515520
16992                                     NULL, --Bug 3515520
16993                                     NULL, -- to loc
16994                                     p_instance_id,
16995                                     x_atp_table.customer_id(m),
16996                                     x_atp_table.customer_site_id(m),
16997                                     NULL, -- supplier_id
16998                                     NULL, -- supplier_site_id
16999                                     MSC_ATP_PVT.G_SESSION_ID,
17000                                     x_atp_table.customer_site_id(m),
17001                                     l_ship_method,
17002                                     l_delivery_lead_time
17003                                     );
17004                    END IF;
17005 
17006                    x_atp_table.delivery_lead_time(m) := l_delivery_lead_time; --4430710
17007 		   -- dsting removed old delivery lead time code
17008                 END IF; -- end delivery lead time not specified
17009 
17010                 IF PG_DEBUG in ('Y', 'C') THEN
17011                    --msc_sch_wb.atp_debug('Call_Schedule: ' || 'Lead Time '||l_delivery_lead_time);
17012                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Lead Time '||x_atp_table.delivery_lead_time(m)); --4430710
17013                 END IF;
17014 
17015                 -- Bug 3449812 - get receiving cal from ISO's destination org if available
17016                 -- Bug 3515520, don't use org in case customer/site is populated.
17017                 -- IF NVL(x_atp_table.internal_org_id(m),x_atp_table.organization_id(m)) IS NOT NULL THEN
17018 
17019                 -- Bug 3737759 - Moved from below
17020                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17021                                              p_instance_id,
17022                                              null,
17023                                              null,
17024                                              null,
17025                                              null,
17026                                              null,
17027                                              x_atp_table.source_organization_id(m),
17028                                              null,
17029                                              MSC_CALENDAR.OMC);
17030 
17031                 -- Bug 3593394
17032                 IF MSC_ATP_PVT.G_USE_SHIP_REC_CAL='Y' THEN
17033 
17034                     IF (x_atp_table.internal_org_id(m) IS NOT NULL) OR
17035                        (x_atp_table.organization_id(m) IS NOT NULL AND
17036                         x_atp_table.customer_id(m) IS NULL AND x_atp_table.customer_site_id(m) IS NULL) THEN
17037                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17038                                                              p_instance_id,
17039                                                              null,
17040                                                              null,
17041                                                              null,
17042                                                              null,
17043                                                              null,
17044                                                              NVL(x_atp_table.internal_org_id(m),x_atp_table.organization_id(m)),
17045                                                              l_ship_method,
17046                                                              -- Bug 3593394 - x_atp_table.ship_method(m),
17047                                                              MSC_CALENDAR.ORC);
17048                     ELSIF x_atp_table.customer_id(m)  IS NOT NULL THEN
17049                             -- receiving party is customer
17050                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17051                                                              p_instance_id,
17052                                                              null,
17053                                                              null,
17054                                                              MSC_ATP_PVT.G_PARTNER_ID,
17055                                                              MSC_ATP_PVT.G_PARTNER_SITE_ID,
17056                                                              2,
17057                                                              null,
17058                                                              l_ship_method,
17059                                                              -- Bug 3593394 - x_atp_table.ship_method(m),
17060                                                              MSC_CALENDAR.CRC);
17061                     ELSE
17062                             -- neither customer nor org is provided
17063                             l_receiving_cal_code := MSC_CALENDAR.FOC;
17064                     END IF;
17065 
17066                     l_intransit_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17067                                              p_instance_id,
17068                                              null,
17069                                              null,
17070                                              null,
17071                                              null,
17072                                              4,
17073                                              null,
17074                                              l_ship_method,
17075                                              -- Bug 3593394 - x_atp_table.ship_method(m),
17076                                              MSC_CALENDAR.VIC);
17077 
17078                     l_shipping_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17079                                              p_instance_id,
17080                                              null,
17081                                              null,
17082                                              null,
17083                                              null,
17084                                              null,
17085                                              x_atp_table.source_organization_id(m),
17086                                              l_ship_method,
17087                                              -- Bug 3593394 - x_atp_table.ship_method(m),
17088                                              MSC_CALENDAR.OSC);
17089 
17090                 ELSE
17091 
17092                     -- Bug 3593394
17093                     IF PG_DEBUG in ('Y', 'C') THEN
17094                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'G_USE_SHIP_REC_CAL is N');
17095                     END IF;
17096                     -- Bug 3737759 - Use receiving org's OMC
17097                     IF (x_atp_table.internal_org_id(m) IS NOT NULL) OR
17098                        (x_atp_table.organization_id(m) IS NOT NULL AND
17099                         x_atp_table.customer_id(m) IS NULL AND x_atp_table.customer_site_id(m) IS NULL) THEN
17100                             l_receiving_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17101                                                              p_instance_id,
17102                                                              null,
17103                                                              null,
17104                                                              null,
17105                                                              null,
17106                                                              null,
17107                                                              NVL(x_atp_table.internal_org_id(m),x_atp_table.organization_id(m)),
17108                                                              null,
17109                                                              MSC_CALENDAR.OMC);
17110                     ELSE
17111                         l_receiving_cal_code := MSC_CALENDAR.FOC;
17112                     END IF;
17113                     l_intransit_cal_code := MSC_CALENDAR.FOC;
17114                     -- Bug 3737759 - Use source org's OMC - l_shipping_cal_code := MSC_CALENDAR.FOC;
17115                     l_shipping_cal_code := l_manufacturing_cal_code;
17116 
17117                 END IF;
17118 
17119                 /* Bug 3737759 - Moved before IF
17120                 l_manufacturing_cal_code := MSC_CALENDAR.Get_Calendar_Code(
17121                                              p_instance_id,
17122                                              null,
17123                                              null,
17124                                              null,
17125                                              null,
17126                                              null,
17127                                              x_atp_table.source_organization_id(m),
17128                                              null,
17129                                              MSC_CALENDAR.OMC);
17130                 */
17131 
17132                 -- Bug 3449812 - Corrected the logic of date calculation
17133                 IF x_atp_table.requested_ship_date(m) IS NOT NULL THEN
17134                         x_atp_table.ship_date(m) := MSC_CALENDAR.PREV_WORK_DAY(
17135         						l_shipping_cal_code,
17136         						p_instance_id,
17137         						x_atp_table.requested_ship_date(m));
17138                 ELSE
17139                         x_atp_table.ship_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
17140                         				x_atp_table.requested_arrival_date(m), l_receiving_cal_code, -1,
17141                         				l_intransit_cal_code, -1 * nvl( --l_delivery_lead_time 4430710
17142                         				x_atp_table.delivery_lead_time(m), 0), -1,
17143                         				l_shipping_cal_code, -1, p_instance_id);
17144                 END IF;
17145 
17146                 IF PG_DEBUG in ('Y', 'C') THEN
17147                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.ship_date ' || x_atp_table.ship_date(m) );
17148                 END IF;
17149 
17150                 BEGIN  -- Bug2031894. Handle Exception here.
17151 
17152                     l_sysdate := MSC_CALENDAR.NEXT_WORK_DAY(
17153                                         l_shipping_cal_code,    -- Bug 3449812
17154                                         p_instance_id,
17155                                         sysdate);
17156                 EXCEPTION
17157                     WHEN OTHERS THEN
17158                         IF PG_DEBUG in ('Y', 'C') THEN
17159                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'When Others ');
17160                         END IF;
17161                         NULL;
17162                 END;
17163                 IF PG_DEBUG in ('Y', 'C') THEN
17164                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_sysdate '||l_sysdate);
17165                 END IF;
17166                 IF l_sysdate IS NULL THEN
17167                    IF PG_DEBUG in ('Y', 'C') THEN
17168                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'Sysdate not found in Calendar');
17169                    END IF;
17170                    x_atp_table.error_code(m) := NO_MATCHING_CAL_DATE;
17171                 END IF;
17172 
17173                 IF x_atp_table.ship_date(m) < l_sysdate THEN
17174                         x_atp_table.requested_date_quantity(m) := 0;
17175                 ELSE
17176                         x_atp_table.requested_date_quantity(m) :=
17177                         x_atp_table.quantity_ordered(m);
17178                 END IF;
17179                 /* Bug 3449812 - l_prev_work_ship_date not required as ship date has already been validated
17180                 l_prev_work_ship_date := MSC_ATP_FUNC.prev_work_day
17181                                     (x_atp_table.source_organization_id(m),
17182                                     p_instance_id,
17183                                      x_atp_table.ship_date(m));
17184 
17185                 IF l_prev_work_ship_date IS NULL THEN
17186                    IF PG_DEBUG in ('Y', 'C') THEN
17187                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'prev date for ship date not found in Calendar');
17188                       msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.ship_date ' || x_atp_table.ship_date(m) );
17189                    END IF;
17190                    x_atp_table.error_code(m) := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
17191                 END IF;
17192                 */
17193 
17194                 IF PG_DEBUG in ('Y', 'C') THEN
17195                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.requested_ship_date(m) : ' ||
17196                         to_char(x_atp_table.requested_ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
17197                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.ship_date(m) : ' ||
17198                         to_char(x_atp_table.ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
17199                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.requested_arrival_date(m): ' ||
17200                         to_char(x_atp_table.requested_arrival_date(m),'dd/mm/yyyy hh:mi:ss'));
17201                 END IF;
17202                 -- Bug  Bug 2232555, 2250456
17203 
17204 		/* Old commented code removed*/
17205 
17206                 -- ship_rec_cal changes begin
17207                 -- Bug 3449812 - Code to get calendars moved at the beginning of while loop
17208 
17209                 IF (NVL(x_atp_table.Override_Flag(m), 'N') = 'Y') THEN
17210                    IF (x_atp_table.requested_ship_date(m) IS NOT NULL) THEN
17211                       x_atp_table.ship_date(m) := x_atp_table.requested_ship_date(m);
17212 
17213                       -- Bug 3520301 - dont consider first calendar in override cases
17214                       x_atp_table.arrival_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
17215                                                         x_atp_table.ship_date(m), MSC_CALENDAR.FOC, 1,
17216                                                         l_intransit_cal_code, nvl(x_atp_table.delivery_lead_time(m), 0), 1,
17217                                                         l_receiving_cal_code, 1, p_instance_id);
17218 
17219                    ELSE
17220                       x_atp_table.arrival_date(m) := x_atp_table.requested_arrival_date(m);
17221 
17222                       -- Bug 3520301 - ship date needs to be recalculated because first calendar
17223                       --               is not to be considered in override cases
17224                       x_atp_table.ship_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
17225                 					x_atp_table.requested_arrival_date(m), MSC_CALENDAR.FOC, -1,
17226                 					l_intransit_cal_code, -1 * nvl(x_atp_table.delivery_lead_time(m), 0), -1,
17227                 					l_shipping_cal_code, -1, p_instance_id);
17228 
17229                    END IF;
17230 
17231                 ELSE
17232                     -- Bug 3449812 - Added the non-override case, which, it seems, got removed while ship_rec changes were done
17233                     x_atp_table.ship_date(m) := GREATEST(l_sysdate, x_atp_table.ship_date(m));
17234 
17235                 END IF;
17236                 IF (x_atp_table.ship_date(m) IS NOT NULL)  AND (x_atp_table.arrival_date(m) IS NULL) THEN
17237                     x_atp_table.arrival_date(m) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
17238                                                         x_atp_table.ship_date(m), l_shipping_cal_code, 1,
17239                                                         l_intransit_cal_code, nvl(x_atp_table.delivery_lead_time(m), 0), 1,
17240                                                         l_receiving_cal_code, 1, p_instance_id);
17241 
17242                     /* Bug 3449812 - Arrival date should not be pushed back to non-working day
17243                     IF x_atp_table.requested_arrival_date(m) is not null THEN
17244                        x_atp_table.arrival_date(m) := TRUNC(GREATEST(x_atp_table.requested_arrival_date(m),
17245                                                         x_atp_table.arrival_date(m)));
17246                     END IF;
17247                     */
17248                 END IF;
17249                 -- ship_rec_cal changes end
17250 
17251                 x_atp_table.group_ship_date(m) := x_atp_table.ship_date(m);
17252                 x_atp_table.group_arrival_date(m) := x_atp_table.arrival_date(m);
17253                 x_atp_table.ship_method(m) := l_ship_method;
17254                 x_atp_table.delivery_lead_time(m) := l_delivery_lead_time;
17255                 IF PG_DEBUG in ('Y', 'C') THEN
17256                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'group ship date '|| x_atp_table.group_ship_date(m));
17257                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'group arrival date '|| x_atp_table.group_arrival_date(m));
17258                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'x_atp_table.requested_ship_date(m) after : ' ||
17259                         to_char(x_atp_table.requested_ship_date(m), 'dd/mm/yyyy hh:mi:ss'));
17260                 END IF;
17261 
17262                 -- End Changes
17263 
17264                 x_atp_table.Available_quantity(m) := l_atp_input_table.Quantity_Ordered(m);
17265                 --x_atp_table.requested_date_quantity(m) := l_atp_input_table.Quantity_Ordered(m);
17266                 x_atp_table.Error_Code(m) := 0;
17267 
17268                 m := l_atp_input_table.Action.NEXT(m);
17269                 -- End Bug 2232555, 2250456
17270             END LOOP;
17271           EXCEPTION --bug3583705 start
17272 
17273             WHEN NO_MATCHING_DATE_IN_CAL THEN
17274               FOR i in l_atp_input_table.Action.FIRST ..l_atp_input_table.Action.LAST	 LOOP
17275                 IF (x_atp_table.Error_Code(i) IS NULL) or (x_atp_table.Error_Code(i) IN (0,61,150)) THEN
17276                  x_atp_table.Error_Code(i) := NO_MATCHING_CAL_DATE;
17277                 END IF;
17278               END LOOP;
17279 
17280             WHEN OTHERS THEN
17281               x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17282               /* Check if this is actually coming from a calendar routine*/
17283               l_encoded_text := fnd_message.GET_ENCODED;
17284               IF l_encoded_text IS NULL THEN
17285                 l_msg_app := NULL;
17286                 l_msg_name := NULL;
17287               ELSE
17288                 fnd_message.parse_encoded(l_encoded_text, l_msg_app, l_msg_name);
17289               END IF;
17290 
17291               -- Error Handling Changes
17292               FOR i in l_atp_input_table.Action.FIRST ..l_atp_input_table.Action.LAST LOOP
17293                IF (x_atp_table.Error_Code(i) IS NULL) or (x_atp_table.Error_Code(i) IN (0,61,150)) THEN
17294                 IF l_msg_app='MRP' AND l_msg_name='GEN-DATE OUT OF BOUNDS' THEN
17295                         x_atp_table.Error_Code(i) := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
17296                         IF PG_DEBUG in ('Y', 'C') THEN
17297                            msc_sch_wb.atp_debug('Call_Schedule: NO_MATCHING_CAL_DATE');
17298                         END IF;
17299                 ELSE
17300                         x_atp_table.Error_Code(i) := MSC_ATP_PVT.ATP_PROCESSING_ERROR; -- ATP Processing Error
17301                 END IF;
17302                END IF;
17303               END LOOP;
17304               IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
17305                  FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'MSC_ATP_PVT');
17306               END IF;
17307               IF PG_DEBUG in ('Y', 'C') THEN
17308                  msc_sch_wb.atp_debug('Call_Schedule: ' || 'error := ' || sqlerrm);
17309               END IF;
17310           END;
17311           --bug3583705 end
17312         /* END Bug 2290633 : krajan : 04/05/02 */
17313 
17314             IF PG_DEBUG in ('Y', 'C') THEN
17315                msc_sch_wb.atp_debug('Call_Schedule: ' || 'After Assigning values for l_atp_output_table');
17316             END IF;
17317 
17318          ELSE 	-- IF l_atp_input_table.OE_flag(m) = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
17319             -- now call schedule to schedule this line/set.
17320             -- the reason that we do it this way is that when source org not
17321             -- specified, for each line/set, we may return multiple results
17322             -- to users when we cannot satisfy the request on the preferred
17323             -- warehouse and the action is ATP inquiry.
17324 
17325             IF PG_DEBUG in ('Y', 'C') THEN
17326                msc_sch_wb.atp_debug('Call_Schedule, before Schedule ');
17327             END IF;
17328             /* --s_cto_rearch
17329             -- rajjain 02/03/2003 Bug 2766713 Begin
17330             k := l_atp_input_table.error_code.First;
17331             FOR k in 1.. l_atp_input_table.error_code.COUNT LOOP
17332                 IF l_atp_input_table.error_code(k) = INVALID_ITEM_ORG_COMBINATION THEN
17333                    msc_sch_wb.atp_debug ('Call_Schedule: '||'Invalid Item Org Combination : '||k);
17334                    l_set_fail_flag := 'Y';
17335                    EXIT;
17336                 END IF;
17337             END LOOP;
17338             e_cto_rearch*/
17339 
17340             IF l_set_fail_flag = 'Y' THEN
17341                l_atp_output_table := l_atp_input_table;
17342                /* --s_cto_rearch
17343                k := l_atp_input_table.error_code.First;
17344                IF PG_DEBUG in ('Y','C') THEN
17345                    msc_sch_wb.atp_debug ('Call_Schedule: '
17346                      || 'This set failed as this has atleast one item with invalid item-org combination');
17347                END IF;
17348                FOR k in 1.. l_atp_input_table.error_code.COUNT LOOP
17349                    IF nvl(l_atp_input_table.error_code(k), 0) <> INVALID_ITEM_ORG_COMBINATION THEN
17350                       l_atp_output_table.error_code(k) := GROUPEL_ERROR;
17351                    END IF;
17352                END LOOP;
17353                e_cto_rearch */
17354                -- Reset l_set_fail_flag
17355                l_set_fail_flag := 'N';
17356             ELSE
17357                -- 24x7 Call to schedule. What do we call ?
17358                if (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'Y' ) then
17359                    IF PG_DEBUG in ('Y','C') THEN
17360                        msc_sch_wb.atp_debug ('Calling Schedule with 24x7 related parameters');
17361                    END IF;
17362 
17363                    MSC_ATP_PVT.Schedule(l_atp_input_table,
17364                        l_atp_input_table.instance_id(l_atp_input_table.instance_id.FIRST),
17365                        p_assign_set_id,
17366                        l_atp_input_table.attribute_04(l_atp_input_table.attribute_04.FIRST),
17367                        l_atp_output_table,
17368                        l_return_status,
17369                        l_msg_data,
17370                        l_msg_count,
17371                        l_atp_supply_demand,
17372                        l_atp_period,
17373                        l_atp_details);
17374                  else
17375                    MSC_ATP_PVT.Schedule(l_atp_input_table,
17376                      p_instance_id,
17377                      p_assign_set_id,
17378                      p_refresh_number,
17379                      l_atp_output_table,
17380                      l_return_status,
17381                      l_msg_data,
17382                      l_msg_count,
17383                      l_atp_supply_demand,
17384                      l_atp_period,
17385                      l_atp_details);
17386                end if;
17387 
17388                -- Bug 1531469
17389                -- Check if the Scheduling was successful. In case no, set the error code
17390                -- for all rows where its either success or atp_not_appl or null.
17391 
17392 
17393                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
17394 
17395                   IF PG_DEBUG in ('Y', 'C') THEN
17396                      msc_sch_wb.atp_debug('Call_Schedule: ' || 'inside return status error');
17397                   END IF;
17398                   x_return_status := FND_API.G_RET_STS_ERROR;
17399                   k := l_atp_output_table.Calling_Module.First;
17400                   FOR k in 1.. l_atp_output_table.Calling_Module.COUNT LOOP
17401                       --l_atp_output_table.Group_Ship_Date(k):= l_group_date;
17402                       IF (l_atp_output_table.Error_Code(k) = ALLSUCCESS) OR
17403                          (l_atp_output_table.error_code(k) = ATP_NOT_APPL) OR
17404                          (l_atp_output_table.Error_Code(k) IS NULL) THEN
17405                          l_atp_output_table.Error_Code(k) := GROUPEL_ERROR;
17406                       END IF;
17407                   END LOOP;
17408                   IF PG_DEBUG in ('Y', 'C') THEN
17409                      msc_sch_wb.atp_debug('Call_Schedule: ' || 'after setting group error');
17410                   END IF;
17411                END IF;
17412             END IF; --IF l_set_fail_flag = 'Y' THEN
17413             -- rajjain 02/03/2003 Bug 2766713 End
17414             --4500382 Starts
17415             k := l_atp_output_table.action.FIRST;
17416             IF l_return_status = FND_API.G_RET_STS_SUCCESS  and l_atp_output_table.action(k) <> 100
17417                  and  p_atp_table.part_of_set(k) = 'Y' and
17418                (l_atp_output_table.Error_Code(k) = ALLSUCCESS OR
17419                     l_atp_output_table.error_code(k) = ATP_NOT_APPL OR
17420                     l_atp_output_table.Error_Code(k) is NULL) THEN
17421 
17422                     FOR k in 1.. l_atp_output_table.Calling_Module.COUNT
17423                     LOOP
17424                         IF PG_DEBUG in ('Y', 'C') THEN
17425                            msc_sch_wb.atp_debug('requested ship date = '||
17426                                                 to_char( p_atp_table.requested_ship_date(k), 'DD-MON-YYYY HH24:MI:SS'));
17427                            msc_sch_wb.atp_debug('requested arrival date = '
17428                                                 ||to_char( p_atp_table.requested_arrival_date(k), 'DD-MON-YYYY HH24:MI:SS'));
17429                            msc_sch_wb.atp_debug('group ship date = '
17430                                                 ||to_char( l_atp_output_table.group_Ship_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
17431                            msc_sch_wb.atp_debug('group arrival date = '
17432                                                 ||to_char( l_atp_output_table.arrival_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
17433                            msc_sch_wb.atp_debug('arrival date  = '
17434                                                 ||to_char(l_atp_output_table.arrival_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
17435                            msc_sch_wb.atp_debug('ship date  = '
17436                                                 ||to_char(l_atp_output_table.ship_Date(k), 'DD-MON-YYYY HH24:MI:SS'));
17437                         END IF;
17438                         if((( p_atp_table.requested_ship_date(k)is not null and
17439                             (p_atp_table.requested_ship_date(k) <>
17440                              greatest(nvl( l_atp_output_table.group_Ship_Date(k),
17441                                            l_atp_output_table.Ship_Date(k)),
17442                                       l_atp_output_table.Ship_Date(k )))) or
17443                                          ( p_atp_table.requested_arrival_date(k)is not null and
17444                             ( p_atp_table.requested_arrival_date(k) <>
17445                               greatest(nvl( l_atp_output_table.group_arrival_Date(k),
17446                                             l_atp_output_table.arrival_Date(k)),
17447                                        l_atp_output_table.arrival_Date(k)))))) THEN
17448 
17449                            l_atp_output_table.Error_Code(k) := ATP_ACCEPT_FAIL;
17450 
17451                            MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(l_atp_output_table.end_pegging_id(k),
17452                                                                   l_atp_output_table.instance_id(k),
17453                                                                   l_atp_output_table.plan_id(k),
17454                                                                   MSC_ATP_PVT.UNDO,
17455                                                                   2,--p_dc_flag
17456                                                                   l_return_status);
17457                         END IF;
17458                      END LOOP;
17459                   END IF;
17460                --4500382 ENDS
17461             -- now get the results/details and append it to the 'real' output
17462             MSC_SATP_FUNC.Assign_Atp_Output_Rec(l_atp_output_table,x_atp_table,l_return_status );
17463 
17464             MSC_ATP_PROC.Details_Output(l_atp_period,
17465                                         l_atp_supply_demand,
17466                                         x_atp_period,
17467                                         x_atp_supply_demand,
17468                                         l_return_status);
17469 
17470          END IF; 	--IF l_atp_input_table.OE_flag(m) = 'Y' THEN
17471       END IF;
17472 
17473       -- this is for backlog: we need to send a message back.
17474       IF PG_DEBUG in ('Y', 'C') THEN
17475          msc_sch_wb.atp_debug('Call_Schedule: ' || 'before backlog');
17476       END IF;
17477 
17478       k := l_atp_output_table.Calling_Module.First;
17479       IF k IS NOT NULL THEN
17480 
17481         IF NVL(l_atp_output_table.Calling_Module(k), 0) = -1 THEN -- backlog
17482 
17483           IF PG_DEBUG in ('Y', 'C') THEN
17484              msc_sch_wb.atp_debug('Call_Schedule: ' || 'for backlog');
17485           END IF;
17486           --Bug3765793 adding trim functions to remove spaces from db_link
17487     	  SELECT ltrim(rtrim(M2A_DBLINK))
17488     	  INTO   l_dblink
17489     	  FROM   msc_apps_instances
17490     	  WHERE  instance_id = p_instance_id;
17491           IF PG_DEBUG in ('Y', 'C') THEN
17492              msc_sch_wb.atp_debug('Call_Schedule: ' || 'after l_dblink : '||l_dblink);
17493           END IF;
17494 
17495           FOR k in 1..l_atp_output_table.Calling_Module.COUNT LOOP
17496 
17497            IF PG_DEBUG in ('Y', 'C') THEN
17498               msc_sch_wb.atp_debug('Call_Schedule: ' || 'in side for loop for backlog');
17499            END IF;
17500 /*
17501             msc_sch_wb.pipe_utility(p_session_id,
17502                                     'SEND',
17503                                      l_msg_data,
17504                                      l_msg_count,
17505                                      x_return_status,
17506                                      l_msg_data,
17507                                      l_msg_count);
17508 */         --bug3520746 pass MSC_ATP_PVT.G_DATABASE_LINK insead of l_dblink
17509            --as global variable already has the right dblink for RAC
17510            IF l_dblink IS NOT NULL THEN
17511             plsql_block := 'BEGIN msc_sch_wb.pipe_utility'
17512 				||'@'||MSC_ATP_PVT.G_DATABASE_LINK||'(:p_session_id,
17513                                      :p_command,
17514                                      :l_msg_data,
17515                                      :l_msg_number,
17516                                      :x_return_status,
17517                                      :l_msg_text,
17518                                      :l_msg_count); END;';
17519 
17520            IF PG_DEBUG in ('Y', 'C') THEN
17521               msc_sch_wb.atp_debug('Call_Schedule: ' || 'plsql_block : '|| plsql_block);
17522            END IF;
17523 
17524            BEGIN
17525             EXECUTE IMMEDIATE plsql_block USING
17526                                      p_session_id,
17527                                      'SEND',
17528                                      IN OUT l_msg_data,
17529                                      OUT l_msg_number,
17530                                      OUT x_return_status,
17531                                      OUT l_msg_text,
17532                                      OUT l_msg_count;
17533            IF PG_DEBUG in ('Y', 'C') THEN
17534               msc_sch_wb.atp_debug('Call_Schedule: ' || 'after plsql_block');
17535            END IF;
17536            EXCEPTION
17537 		when others then
17538                       IF PG_DEBUG in ('Y', 'C') THEN
17539                          msc_sch_wb.atp_debug('Call_Schedule: ' || 'inside send exception : '||sqlcode);
17540                       END IF;
17541            END;
17542 
17543            ELSE		--IF l_dblink IS NOT NULL THEN
17544             msc_sch_wb.pipe_utility(p_session_id,
17545                                     'SEND',
17546                                      l_msg_data,
17547                                      l_msg_number,
17548                                      x_return_status,
17549                                      l_msg_count,
17550                                      l_msg_count);
17551            END IF;
17552           END LOOP;
17553         END IF;
17554       END IF;
17555 
17556       -- go to the next row.
17557       -- make sure we jump to the right row since we may process more
17558       -- than one row due to ship set or arrival set.
17559 
17560        --3720018, Changes for global Availability
17561        -- Remove_Invalid_SD_Rec is called per source.
17562        -- When we move to the next source G_REMOVE_REQUEST_LEVEL_REC array is NULLed
17563        -- Undo_delete_row is not called here but at request level.
17564       IF (j is NULL) THEN
17565          l := i;
17566       ELSE
17567          l := j;
17568       END IF;
17569 
17570 
17571       IF ( l_global_availability = 'Y' )
17572 	  AND (p_atp_table.source_organization_id(i) <> p_atp_table.source_organization_id(l)) THEN
17573 
17574 	  IF PG_DEBUG in ('Y', 'C') THEN
17575               msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside global availability check and calling remove_invalid_sd_rec');
17576           END IF;
17577           IF ( x_atp_table.Action(1) = ATPQUERY ) THEN
17578 	    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
17579 		    FOR m in 1..MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.COUNT LOOP
17580 		        MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
17581   			            MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(m),
17582                                     p_instance_id,
17583                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST (m),
17584                                     UNDO,
17585                                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(m),
17586   			            l_return_status); --3720018
17587 	            END LOOP;
17588 	    END IF;
17589           END IF;
17590 
17591 	  --3720018, Initialize global variables to NULL.
17592           MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC := l_null_remove_req_level_rec;
17593 
17594       END IF;
17595 
17596       i := j;
17597 
17598     END LOOP;
17599 
17600     IF PG_DEBUG in ('Y', 'C') THEN
17601          msc_sch_wb.atp_debug(x_atp_table.Action(1));
17602          msc_sch_wb.atp_debug(x_atp_table.calling_module(1));
17603     END IF;
17604 
17605     --3720018, Call to Remove_invalid_SD_rec and Undo_delete_row for the whole_request in case of ATP Inquiry
17606     IF ( x_atp_table.Action(1) = ATPQUERY ) THEN
17607       IF PG_DEBUG in ('Y', 'C') THEN
17608          msc_sch_wb.atp_debug('Call_Schedule: ' || 'Outside global availability check and calling remove_invalid_sdrec');
17609       END IF;
17610 
17611       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
17612         FOR m in 1..MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST.COUNT LOOP
17613 	    MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
17614   	            MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PEGGING_ID_REQUEST(m)  ,
17615                     p_instance_id,
17616                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_PLAN_ID_REQUEST(m),
17617                     UNDO,
17618                     MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC.REMOVE_DC_ATP_FLAG_REQUEST(m),
17619   	            l_return_status);
17620 	END LOOP;
17621       END IF;
17622       IF  (NVL(p_atp_table.calling_module(1), -99) <> 724) OR
17623           ((NVL(p_atp_table.calling_module(1), -99) = -99) AND
17624           (l_global_availability = 'Y')) THEN --3720018
17625             IF (l_delete_atp_rec.del_demand_ids IS NOT NULL AND l_delete_atp_rec.del_demand_ids.Count > 0) OR
17626                (l_delete_atp_rec.del_ods_demand_ids IS NOT NULL AND l_delete_atp_rec.del_ods_demand_ids.Count > 0)  THEN
17627  		    MSC_ATP_DB_UTILS.Undo_Delete_Row(
17628                     l_delete_atp_rec.del_identifiers,
17629                     l_delete_atp_rec.del_plan_ids,
17630                     p_instance_id,
17631                     l_delete_atp_rec.del_demand_ids,
17632                     l_delete_atp_rec.del_inv_item_ids,
17633                     l_delete_atp_rec.del_copy_demand_ids,
17634                     l_delete_atp_rec.del_copy_demand_plan_ids,
17635                     l_delete_atp_rec.time_phased_set,
17636                     l_delete_atp_rec.del_atp_peg_items,
17637                     l_delete_atp_rec.del_atp_peg_demands,
17638                     l_delete_atp_rec.del_atp_peg_supplies,
17639                     l_delete_atp_rec.del_atp_peg_res_reqs,
17640                     l_delete_atp_rec.del_demand_source_type,
17641                     l_delete_atp_rec.atp_peg_demands_plan_ids,
17642                     l_delete_atp_rec.atp_peg_supplies_plan_ids,
17643                     l_delete_atp_rec.atp_peg_res_reqs_plan_ids,
17644                     l_delete_atp_rec.del_ods_demand_ids,         --3720018, added for support of rescheduling in ODS
17645                     l_delete_atp_rec.del_ods_inv_item_ids,       --3720018, added for support of rescheduling in ODS
17646                     l_delete_atp_rec.del_ods_demand_src_type,    --3720018, added for support of rescheduling in ODS
17647                     l_delete_atp_rec.del_ods_cto_demand_ids,     --3720018, added for support of rescheduling in ODS
17648                     l_delete_atp_rec.del_ods_cto_inv_item_ids,   --3720018, added for support of rescheduling in ODS
17649                     l_delete_atp_rec.del_ods_cto_dem_src_type,   --3720018, added for support of rescheduling in ODS
17650                     l_delete_atp_rec.del_ods_atp_refresh_no,     --3720018, added for support of rescheduling in ODS
17651                     l_delete_atp_rec.del_ods_cto_atp_refresh_no  --3720018, added for support of rescheduling in ODS
17652                     );
17653 	    END IF;
17654       END IF;
17655 
17656       MSC_ATP_PVT.G_REMOVE_REQUEST_LEVEL_REC := l_null_remove_req_level_rec;
17657 
17658     END IF;
17659 
17660    --3720018 (end of changes)
17661 
17662     -- Delete records from MSC_REGIONS_TEMP before returning back to calling
17663     -- application so as to clean up the table for another request within
17664     -- same session for Region Level Sourcing Support
17665 
17666     DELETE msc_regions_temp
17667     WHERE session_id = p_session_id;
17668 
17669     IF PG_DEBUG in ('Y', 'C') THEN
17670        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Rows deleted from msc_regions_temp : '||sql%rowcount);
17671     END IF;
17672     --bug3940999 Delete records from msc_atp_src_profile_temp before returning
17673     --back to calling application so as to clean up the table
17674     DELETE msc_atp_src_profile_temp
17675     WHERE session_id = p_session_id;
17676 
17677 
17678 
17679     IF PG_DEBUG in ('Y', 'C') THEN
17680        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Rows deleted from msc_atp_src_profile_temp : '||sql%rowcount);
17681        msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before calling MSC_ATP_PROC.Delete_Copy_Demand');
17682     END IF;
17683 
17684     -- Summary enhancement - Delete the copy demands that were created in this transaction if refresh number has moved.
17685     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN --bug4291375 Delete_Copy_Demand should be called when summary is ON.
17686        MSC_ATP_DB_UTILS.Delete_Copy_Demand(MSC_ATP_PVT.G_COPY_DEMAND_ID,
17687                                            MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID,
17688                                            G_TIME_PHASED_SET,
17689                                            l_return_status);
17690     END IF;
17691     MSC_ATP_PVT.G_COPY_DEMAND_ID        := MRP_ATP_PUB.number_arr();
17692     MSC_ATP_PVT.G_COPY_DEMAND_PLAN_ID   := MRP_ATP_PUB.number_arr();
17693 
17694     --Bug 3629191: Insert Offset Record to MRP_ATP_DETAILS_TEMP with record_type = 3
17695     -- and these will be inserted with NULL in all the three pegging_id columns
17696 
17697      IF PG_DEBUG in ('Y', 'C') THEN
17698        msc_sch_wb.atp_debug('Call_Schedule: G_OFF_DEM_TRANS_ID.count ' || G_OFF_DEM_TRANS_ID.count);
17699        for i in 1..G_OFF_DEM_TRANS_ID.count LOOP
17700          msc_sch_wb.atp_debug('Call_Schedule: ' ||G_OFF_INSTANCE_ID(i) ||',' || G_OFF_PLAN_ID(i)||','||G_OFF_DEM_TRANS_ID(i));
17701        END LOOP;
17702     END IF;
17703 
17704     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y')  THEN
17705     FORALL i in 1..G_OFF_DEM_TRANS_ID.count
17706     INSERT INTO MRP_ATP_DETAILS_TEMP(
17707                                      session_id,
17708                                      identifier1,
17709                                      identifier2,
17710                                      identifier3,
17711                                      record_type,
17712                                      scenario_id,
17713                                      summary_flag
17714                                      )
17715                                VALUES(
17716                                      p_session_id,
17717                                      G_OFF_INSTANCE_ID(i),
17718                                      G_OFF_PLAN_ID(i),
17719                                      G_OFF_DEM_TRANS_ID(i),
17720                                      3,
17721                                      1,
17722                                      'Y'
17723                                      );
17724     END IF;
17725 
17726     /* rajjain 02/27/2003 Bug 2824700 Begin
17727      * Restructured Exception/WF Notification code */
17728     l_wf_profile := NVL(G_ATP_WORKFLOW,
17729 			NVL(fnd_profile.value('MSC_ALLOCATED_ATP_WORKFLOW'), 'N'));
17730     IF PG_DEBUG in ('Y', 'C') THEN
17731        msc_sch_wb.atp_debug('Call_Schedule: ' || 'WF Profile:' || l_wf_profile);
17732     END IF;
17733 
17734     k := x_atp_table.Calling_Module.First;
17735     -- Generate exceptions only if action is not ATP query.
17736     IF x_atp_table.Action(k) <> 100 THEN
17737 
17738        WHILE k IS NOT NULL LOOP
17739                 -- re-initialize these variables
17740                 l_wf_start := NULL;
17741                 l_exception_rec.demand_id := NULL;
17742 
17743                 IF PG_DEBUG in ('Y', 'C') THEN
17744                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'k := '||k);
17745                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'Override_flag : '||x_atp_table.Override_Flag(k));
17746 	        END IF;
17747 
17748                 /* rajjain code specific to workflow generation begin*/
17749                 IF NVL(l_wf_profile, 'N') = 'Y' AND MSC_ATP_PVT.G_INV_CTP = 4 THEN
17750                      -- generate workflow notification only in case of PDS.
17751                      IF PG_DEBUG in ('Y', 'C') THEN
17752                         msc_sch_wb.atp_debug('Call_Schedule: ' || '*** Begin Workflow Notification ***');
17753                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'qty ordered : '||x_atp_table.Quantity_Ordered(k));
17754                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'req_date_qty : '||x_atp_table.Requested_Date_Quantity(k));
17755                      END IF;
17756 
17757                      IF (nvl(x_atp_table.Error_Code(k),-1) = 0) AND (NVL(x_atp_table.Override_Flag(k), 'N') = 'Y') THEN
17758                          BEGIN
17759 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
17760                            SELECT MAX(supply_demand_date)
17761                            INTO   l_avail_date
17762                            FROM   mrp_atp_details_temp
17763                            WHERE  session_id = p_session_id
17764                            AND    order_line_id =  x_atp_table.identifier(k)
17765                            AND    parent_pegging_id = end_pegging_id
17766                            AND    supply_demand_type = 2
17767                            AND    record_type = 3
17768                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
17769                          EXCEPTION
17770                            WHEN no_data_found THEN
17771                                 l_avail_date := NULL;
17772                            WHEN others THEN
17773                                 IF PG_DEBUG in ('Y', 'C') THEN
17774                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting Available Date for WF *****');
17775                                 END IF;
17776                                 l_avail_date := NULL;
17777                          END;
17778 
17779         		 l_wf_start := 'Y';
17780         	     END IF;	--IF x_atp_table.Override_Flag ... THEN
17781 
17782                      IF PG_DEBUG in ('Y', 'C') THEN
17783                         msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_avail_date : '||l_avail_date);
17784                      END IF;
17785                      IF (nvl(x_atp_table.Error_Code(k),-1) IN (0,53)) AND MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
17786 
17787                          IF PG_DEBUG in ('Y', 'C') THEN
17788                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Workflow for Action : '||
17789                                        x_atp_table.Action(k));
17790                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'Inside Workflow for calling Module : '||
17791                                        x_atp_table.Calling_Module(k));
17792                          END IF;
17793 
17794                          BEGIN
17795 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
17796                            SELECT NVL(sum(NVL(allocated_quantity, 0)), 0)
17797                            INTO   l_stealing_qty
17798                            FROM   mrp_atp_details_temp
17799                            WHERE  session_id = p_session_id
17800                            AND    order_line_id =  x_atp_table.identifier(k)
17801                            AND    parent_pegging_id = end_pegging_id
17802                            AND    char1 IS NOT NULL
17803                            AND    record_type = 3
17804                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
17805                          EXCEPTION
17806                            WHEN no_data_found THEN
17807                                 l_stealing_qty := 0;
17808                            WHEN others THEN
17809                                 IF PG_DEBUG in ('Y', 'C') THEN
17810                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting stealing qty for WF *****');
17811                                 END IF;
17812                                 l_stealing_qty := -1;
17813                          END;
17814 
17815                          IF PG_DEBUG in ('Y', 'C') THEN
17816                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_stealing_qty : '||l_stealing_qty);
17817                          END IF;
17818 
17819         		 l_wf_start := 'Y';
17820         	     END IF;	--IF MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y' THEN
17821 
17822         	     IF NVL(l_wf_start, 'N') = 'Y' THEN
17823 
17824         	         -- Get the demand_id from mrp_atp_details_temp
17825                          BEGIN
17826 						   -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
17827                            SELECT p_session_id ||':'||mrp_atp_schedule_temp_s.nextval,
17828                                   DECODE(x_atp_table.Requested_Ship_Date(k),
17829                                              NULL, x_atp_table.Requested_Arrival_Date(k),
17830                                              x_atp_table.Requested_Ship_Date(k)),
17831                                   identifier3,
17832                                   identifier2
17833                            INTO   l_wf_item_key,
17834                                   l_req_ship_date,
17835                                   l_exception_rec.demand_id,
17836                                   l_plan_id
17837                            FROM   mrp_atp_details_temp
17838                            WHERE  session_id = p_session_id
17839                            AND    order_line_id =  x_atp_table.identifier(k)
17840                            AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k))
17841                            AND    record_type = 3
17842                            AND    parent_pegging_id IS NULL;
17843 
17844                          EXCEPTION
17845                            WHEN no_data_found THEN
17846                                 IF PG_DEBUG in ('Y', 'C') THEN
17847                                    msc_sch_wb.atp_debug('Call_Schedule: ' || 'No Data Found getting item key for WF');
17848                                 END IF;
17849                                 l_wf_item_key := NULL;
17850                            WHEN others THEN
17851                                 IF PG_DEBUG in ('Y', 'C') THEN
17852                                    msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting item key for WF *****');
17853                                 END IF;
17854                                 l_wf_item_key := NULL;
17855                          END;
17856 
17857                          IF PG_DEBUG in ('Y', 'C') THEN
17858                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_wf_item_key : '||l_wf_item_key);
17859                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_plan_id : '||l_plan_id);
17860                             msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_exception_rec.demand_id : '||l_exception_rec.demand_id);
17861                          END IF;
17862 
17863                          IF ((nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0)) OR
17864                             (nvl(l_stealing_qty, 0) > 0)) AND (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
17865         		    (l_wf_item_key IS NOT NULL) THEN
17866 
17867                             IF PG_DEBUG in ('Y', 'C') THEN
17868                                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before Allocated ATP Workflow');
17869                             END IF;
17870                             --bug 2463556: wrap workflow call with begin end so that if any exception
17871                             ---occurs in workflow generation then ATP continues as normal
17872 
17873                             -- Start Work Flow
17874                             BEGIN
17875 							   -- Bug 3444096, use x_atp_table.match_item_id and matched_item_name for match cases in CTO
17876                                msc_wf_alloc_atp.start_mscalloc_wf(
17877                                    l_wf_item_key,  NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k)),
17878                                    NVL(x_atp_table.matched_item_name(k), x_atp_table.inventory_item_name(k)), l_plan_id,
17879                                    x_atp_table.source_organization_id(k), x_atp_table.source_organization_code(k),
17880                                    p_instance_id, x_atp_table.demand_class(k),
17881                                    x_atp_table.quantity_ordered(k), l_req_ship_date,
17882                                    x_atp_table.Requested_Date_Quantity(k), x_atp_table.Available_Quantity(k),
17883                                    x_atp_table.ship_date(k), l_stealing_qty, x_atp_table.Customer_Id(k),
17884         			               x_atp_table.Customer_Site_Id(k),x_atp_table.Order_Number(k));
17885                             EXCEPTION
17886                                   WHEN OTHERS THEN
17887                                       IF PG_DEBUG in ('Y', 'C') THEN
17888                                          msc_sch_wb.atp_debug('Call_Schedule: ' || ' Allocated ATP WF notification generation failed');
17889                                       END IF;
17890                             END;
17891 
17892                          END IF;
17893 
17894                          IF ((nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0)) AND
17895                             (nvl(x_atp_table.Override_Flag(k), 'N') = 'Y')) AND
17896         		    (l_wf_item_key IS NOT NULL) THEN
17897 
17898                             IF PG_DEBUG in ('Y', 'C') THEN
17899                                msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before ATP Override Workflow');
17900                             END IF;
17901                             --bug 2463556: wrap workflow call with begin end so that if any exception
17902                             ---occurs in workflow generation then ATP continues as normal
17903 
17904                             -- Start Work Flow
17905                             BEGIN
17906 							  -- Bug 3444096, use x_atp_table.match_item_id and matched_item_name for match cases in CTO
17907                               msc_wf_alloc_atp.start_mscatp_wf(
17908                                    l_wf_item_key, NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k)),
17909                                    NVL(x_atp_table.matched_item_name(k), x_atp_table.inventory_item_name(k)), l_plan_id,
17910                                    x_atp_table.source_organization_id(k), x_atp_table.source_organization_code(k),
17911                                    p_instance_id, x_atp_table.demand_class(k),
17912                                    x_atp_table.quantity_ordered(k), l_req_ship_date,
17913                                    x_atp_table.Requested_Date_Quantity(k), x_atp_table.Available_Quantity(k),
17914                                    l_avail_date, x_atp_table.Customer_Id(k),
17915         			               x_atp_table.Customer_Site_Id(k),x_atp_table.Order_Number(k), x_atp_table.Identifier(k));
17916                             EXCEPTION
17917                                  WHEN OTHERS THEN
17918                                     IF PG_DEBUG in ('Y', 'C') THEN
17919                                        msc_sch_wb.atp_debug('Call_Schedule: ' || 'ATP Override WF Notification Generation Failed');
17920                                     END IF;
17921 
17922                             END;
17923                          END IF;
17924                      END IF;
17925         	END IF;
17926         	/* rajjain code specific to workflow generation end */
17927 
17928                 /* rajjain code specific to ATP Override Exception begin
17929                  * ATP Override rajjain begin 10/11/2002 Generate Exceptions */
17930 	        IF (nvl(x_atp_table.Requested_Date_Quantity(k),0) < nvl(x_atp_table.Quantity_Ordered(k),0))
17931 	            AND (nvl(x_atp_table.Override_Flag(k), 'N') = 'Y')
17932                     -- bug 2795053-reopen (ssurendr): Generate exception only if set is successful
17933 	            AND (nvl(x_atp_table.Error_Code(k),-1) = 0)
17934 	        THEN
17935                   IF PG_DEBUG in ('Y', 'C') THEN
17936                     msc_sch_wb.atp_debug('Call_Schedule: ' || 'Before ATP Override Exception');
17937 	          END IF;
17938 
17939                   -- 24x7
17940                   if (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'Y' ) then
17941                        if (x_atp_table.instance_id.exists(k)) then
17942                            l_instance_id := NVL (x_atp_table.instance_id(k),
17943                                    x_atp_table.instance_id(x_atp_table.instance_id.FIRST));
17944                        else
17945                            l_instance_id := x_atp_table.instance_id(x_atp_table.instance_id.FIRST);
17946                        end if;
17947                   else
17948                        l_instance_id := p_instance_id;
17949                   end if;
17950                   IF PG_DEBUG in ('Y', 'C') THEN
17951                        msc_sch_wb.atp_debug ('Call_Schedule: ' || 'Using Instance ID : ' || l_instance_id);
17952                   END IF;
17953                   -- End 24x7
17954 
17955                   If l_exception_rec.demand_id is null THEN
17956         	        -- Get the demand_id from mrp_atp_details_temp
17957                         BEGIN
17958 						  -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
17959                           SELECT identifier3,
17960                                  identifier2
17961                           INTO   l_exception_rec.demand_id,
17962                                  l_plan_id
17963                           FROM   mrp_atp_details_temp
17964                           WHERE  session_id = p_session_id
17965                           AND    order_line_id =  x_atp_table.identifier(k)
17966                           AND    inventory_item_id = NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k))
17967                           AND    record_type = 3
17968                           AND    parent_pegging_id IS NULL;
17969 
17970                         EXCEPTION
17971                           WHEN no_data_found THEN
17972                                IF PG_DEBUG in ('Y', 'C') THEN
17973                                   msc_sch_wb.atp_debug('Call_Schedule: ' || 'Exception: No data found');
17974                                END IF;
17975                                l_exception_rec.demand_id := NULL;
17976                           WHEN others THEN
17977                                IF PG_DEBUG in ('Y', 'C') THEN
17978                                   msc_sch_wb.atp_debug('Call_Schedule: ' || '***** Error getting demand_id for generating exception *****');
17979                                END IF;
17980                                l_exception_rec.demand_id := NULL;
17981                         END;
17982 
17983                         IF PG_DEBUG in ('Y', 'C') THEN
17984                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_exception_rec.demand_id : '||l_exception_rec.demand_id);
17985                            msc_sch_wb.atp_debug('Call_Schedule: ' || 'l_plan_id : '||l_plan_id);
17986                         END IF;
17987                   END IF;
17988                   IF (l_plan_id is not null) AND
17989                      (l_exception_rec.demand_id is not null) THEN
17990                     -- bug 2795053-reopen (ssurendr): do not generate exception for non-ATPable items.
17991                     --                                In case of non-ATPable there will be no pegging record.
17992                     -- generate exception only in case of PDS. In case of ODS ATP l_plan_id will be null
17993                     IF PG_DEBUG in ('Y', 'C') THEN
17994                        msc_sch_wb.atp_debug('Call_Schedule: ' || '*** Begin ATP Override Exception ***');
17995                     END IF;
17996 
17997                     l_exception_rec.exception_type := 68; -- Overcommitment of Sales Order
17998 		    l_exception_rec.quantity := -1 * x_atp_table.quantity_ordered(k);
17999 
18000                     /* rajjain 02/06/2003 Bug 2781863
18001                        Selecting inventory_item_id from msc_system_items*/
18002                     BEGIN
18003 					  -- Bug 3444096, use x_atp_table.match_item_id for match cases in CTO
18004                       Select inventory_item_id
18005                       into   l_inventory_item_id
18006                       from   msc_system_items
18007                       where  plan_id = l_plan_id
18008                       and    sr_instance_id = l_instance_id
18009                       and    organization_id = x_atp_table.source_organization_id(k)
18010                       and    sr_inventory_item_id =  NVL(x_atp_table.match_item_id(k), x_atp_table.inventory_item_id(k));
18011                     EXCEPTION
18012                       WHEN OTHERS THEN
18013                           IF PG_DEBUG in ('Y', 'C') THEN
18014                              msc_sch_wb.atp_debug('Call_Schedule: ' || 'Error occured while selecting inventory_item_id : ' || sqlerrm);
18015                           END IF;
18016                           RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
18017                     END;
18018 
18019                     IF PG_DEBUG in ('Y', 'C') THEN
18020 				       msc_sch_wb.atp_debug('Call_Schedule: Inventory Item Id : ' || l_inventory_item_id);
18021                     END IF;
18022 	            l_exception_rec.exception_group := 5;
18023 	            l_exception_rec.plan_id := l_plan_id;
18024 	            l_exception_rec.organization_id := x_atp_table.source_organization_id(k);
18025 	            l_exception_rec.inventory_item_id := l_inventory_item_id;
18026 	            -- 24x7 : Using l_instance_id
18027                     -- l_exception_rec.sr_instance_id := p_instance_id;
18028 	            l_exception_rec.sr_instance_id := l_instance_id;
18029 	            l_exception_rec.order_number := x_atp_table.Order_Number(k);
18030 	            l_exception_rec.customer_id := x_atp_table.Customer_Id(k);
18031 	            l_exception_rec.customer_site_id := x_atp_table.Customer_Site_Id(k);
18032 	            l_exception_rec.demand_satisfy_date := x_atp_table.Ship_Date(k); -- bug 2795053-reopen (ssurendr)
18033 	            -- demand_satisfy_date is a misnomer. This actually contains demand date.
18034 	            l_exception_rec.quantity_satisfied := x_atp_table.Requested_Date_Quantity(k);
18035 
18036                     MSC_ATP_EXCEPTIONS.Add_ATP_Exception(p_session_id, l_exception_rec, l_exception_return_status);
18037                     IF PG_DEBUG in ('Y', 'C') THEN
18038                        msc_sch_wb.atp_debug ('Call_Schedule: ' || 'Return Status After adding exception : '||
18039                                           l_exception_return_status);
18040                     END IF;
18041                   END IF;
18042                   msc_sch_wb.atp_debug('Call_Schedule: ' || '*** End ATP Override Exception ***');
18043                 END IF;
18044                 /* rajjain code specific to ATP Override Exception end*/
18045 
18046 	        k := x_atp_table.Calling_Module.Next(k);
18047        END LOOP;
18048     END IF;
18049     /* rajjain 02/27/2003 Bug 2824700 End*/
18050 
18051     IF PG_DEBUG in ('Y', 'C') THEN
18052        msc_sch_wb.atp_debug('***** End Call_Schedule Procedure *****');
18053     END IF;
18054 
18055 END Call_Schedule;
18056 
18057 
18058 PROCEDURE Call_Schedule_Remote (
18059                p_session_id         IN    NUMBER,
18060                p_instance_id        IN    NUMBER,
18061                p_assign_set_id      IN    NUMBER,
18062                p_refresh_number     IN    NUMBER,
18063 --               p_inv_ctp            IN    NUMBER := -1,                      --bug3940999 getting from table
18064                p_def_assign_set_id  IN    NUMBER := -1,
18065                p_atp_debug_flag     IN    VARCHAR2 := NULL,
18066 --               p_atp_workflow       IN    VARCHAR2 := NULL,                  --bug3940999 getting from table
18067                --ATP Debug Workflow
18068                x_session_loc_des    OUT NOCOPY  VARCHAR2,
18069                x_spid_des           OUT NOCOPY  NUMBER,
18070                x_trace_loc_des      OUT NOCOPY  VARCHAR2,
18071                p_node_id            IN    NUMBER DEFAULT null --bug3520746
18072 --               p_use_ship_rec       IN    VARCHAR2 DEFAULT 'N' --bug3593394  --bug3940999 getting from table
18073 ) IS
18074 
18075     i                      PLS_INTEGER;
18076     l_dblink               VARCHAR2(128);
18077     p_atp_table            MRP_ATP_PUB.ATP_Rec_Typ;
18078     x_atp_table            MRP_ATP_PUB.ATP_Rec_Typ;
18079     x_return_status        VARCHAR2(128);
18080     x_msg_data             VARCHAR2(2000);
18081     x_msg_count            NUMBER;
18082     x_atp_supply_demand    MRP_ATP_PUB.ATP_Supply_Demand_Typ;
18083     x_atp_period           MRP_ATP_PUB.ATP_Period_Typ;
18084     x_atp_details          MRP_ATP_PUB.ATP_Details_Typ;
18085     l_atp_bom_rec          MRP_ATP_PUB.ATP_BOM_Rec_Typ;
18086 
18087     -- Bug 2387242 : krajan
18088     -- Variables for SQL trace setup
18089     C                      INTEGER;
18090     STATEMENT              VARCHAR2(255);
18091     ROWS_PROCESSED         INTEGER;
18092     l_summary_flag         PLS_INTEGER;
18093     -- rajjain 12/20/2002
18094     l_spid                          VARCHAR2(12);
18095 
18096     --bug3940999
18097     l_profile_value        MRP_ATP_PUB.char255_arr := MRP_ATP_PUB.char255_arr();   --bug3940999
18098     l_profile_name         MRP_ATP_PUB.char255_arr := MRP_ATP_PUB.char255_arr();   --bug3940999
18099     l_use_ship_rec         VARCHAR2(2);                                          --bug3940999
18100     j                      NUMBER ;                                              --bug3940999
18101 
18102 BEGIN
18103     -- initialize API return status to success
18104 
18105     x_return_status := FND_API.G_RET_STS_SUCCESS;
18106     --bug3940999 setting debug mode explicitly so the messages can be printed
18107     IF p_atp_debug_flag IS NOT NULL THEN
18108 	   FND_PROFILE.put('MSC_ATP_DEBUG', p_atp_debug_flag);
18109 	   PG_DEBUG := p_atp_debug_flag;
18110     END IF;
18111     IF PG_DEBUG in ('Y', 'C') THEN
18112             --ATP Debug Workflow
18113 	   select ltrim(rtrim(substr(value, instr(value,',',-1,1)+1)))
18114            into x_session_loc_des
18115            from v$parameter where name= 'utl_file_dir';
18116     END IF;
18117     msc_sch_wb.set_session_id(p_session_id);
18118     --bug3940999 This code used to be down, moved it up
18119     IF order_sch_wb.mr_debug in ('T','C') THEN
18120          IF PG_DEBUG in ('Y', 'C') THEN
18121             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace being enabled');
18122          END IF;
18123          C := DBMS_SQL.OPEN_CURSOR;
18124          -- STATEMENT := 'ALTER SESSION SET SQL_TRACE=TRUE';
18125          STATEMENT := 'ALTER SESSION SET  events ' || '''' || '10046 trace name context forever, level 12' || '''' ;
18126          DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
18127          ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
18128          DBMS_SQL.CLOSE_CURSOR(C);
18129 
18130          -- rajjain 12/20/2002
18131          SELECT spid
18132          INTO   l_spid
18133          FROM   v$process
18134          WHERE  addr = (SELECT paddr FROM v$session
18135                         WHERE audsid=userenv('SESSIONID'));
18136          x_spid_des := l_spid; --ATP Debug Workflow
18137 
18138          SELECT value
18139          INTO x_trace_loc_des
18140          from v$parameter
18141          WHERE name = 'user_dump_dest'; --ATP Debug Workflow
18142 
18143          IF PG_DEBUG in ('Y', 'C') THEN
18144             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'spid: ' || l_spid);
18145          END IF;
18146 
18147     END IF;
18148 
18149     IF PG_DEBUG in ('Y', 'C') THEN --bug3940999
18150         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SESSION_ID : ' || order_sch_wb.debug_session_id);
18151         msc_sch_wb.atp_debug('***** Begin Call_Schedule_Remote Procedure *****');
18152         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_node_id            : ' || p_node_id           );
18153         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_session_id         : ' || p_session_id        );
18154         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_instance_id        : ' || p_instance_id       );
18155         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_assign_set_id      : ' || p_assign_set_id     );
18156         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_refresh_number     : ' || p_refresh_number    );
18157         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_def_assign_set_id  : ' || p_def_assign_set_id );
18158     END IF;
18159 
18160     --bug3520746 start
18161     l_dblink := null;
18162     IF p_node_id is not null THEN
18163        BEGIN
18164          --Bug3765793 adding trim functions to remove spaces from db_link
18165          SELECT ltrim(rtrim(M2A_DBLINK))
18166          INTO   l_dblink
18167          FROM   msc_apps_instance_nodes
18168          WHERE  instance_id = p_instance_id and
18169                 node_id     = p_node_id;
18170        EXCEPTION
18171         WHEN OTHERS THEN
18172             l_dblink := null;
18173             IF PG_DEBUG in ('Y', 'C') THEN
18174              msc_sch_wb.atp_debug('Did not get records in rac case');
18175             END IF;
18176        END;
18177     ELSE --6697106
18178     ---bug 2301524: read summary flag
18179     --Bug3765793 adding trim functions to remove spaces from db_link
18180     SELECT NVL(l_dblink,ltrim(rtrim(M2A_DBLINK)))
18181     INTO   l_dblink
18182     FROM   msc_apps_instances
18183     WHERE  instance_id = p_instance_id;
18184     END IF;
18185     --bug3520746 end
18186     SELECT summary_flag, apps_ver
18187     INTO   l_summary_flag, MSC_ATP_PVT.G_APPS_VER
18188     FROM   msc_apps_instances
18189     WHERE  instance_id = p_instance_id;
18190 
18191     IF PG_DEBUG in ('Y', 'C') THEN
18192        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get m2a_dblink');
18193        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_dblink = ' || l_dblink);
18194        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_summary_flag := ' || l_summary_flag);
18195     END IF;
18196 
18197     --bug3940999 start  honor profile options passed from the source
18198     -- We have passed atp debug mode profile seperately so that debug mode
18199     -- can be set as we enter Call_Schedule_Remote
18200     IF l_dblink is not null then
18201      MSC_SATP_FUNC.get_src_to_dstn_profiles(l_dblink,
18202                                             p_session_id,
18203                                             x_return_status);
18204     END IF;
18205 
18206     select profile_value,profile_name
18207     bulk collect into l_profile_value,l_profile_name
18208     from MSC_ATP_SRC_PROFILE_TEMP
18209     where session_id = p_session_id;
18210 
18211     FOR j in 1..l_profile_name.count LOOP
18212 
18213      IF PG_DEBUG in ('Y', 'C') THEN
18214            msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || l_profile_name(j) || ' : ' || l_profile_value(j));
18215      END IF;
18216 
18217      IF  l_profile_name(j)   = 'MSC_ALLOCATION_METHOD'      THEN
18218 
18219        MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(l_profile_value(j),  2 );
18220        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j), 2 ));
18221 
18222      ELSIF l_profile_name(j) = 'MSC_ALLOCATED_ATP'          THEN
18223 
18224        MSC_ATP_PVT.G_ALLOCATED_ATP     := NVL(l_profile_value(j), 'N');
18225        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j), 'N'));
18226 
18227      ELSIF l_profile_name(j) = 'MSC_CLASS_HIERARCHY'        THEN
18228 
18229        MSC_ATP_PVT.G_HIERARCHY_PROFILE := NVL(l_profile_value(j),  2 );
18230        FND_PROFILE.put(l_profile_name(j) , NVL(l_profile_value(j),  2 ));
18231 
18232      ELSIF l_profile_name(j) = 'MSC_ALLOCATED_ATP_WORKFLOW' THEN
18233 
18234        G_ATP_WORKFLOW                  := l_profile_value(j);
18235        FND_PROFILE.put(l_profile_name(j) , l_profile_value(j));
18236 
18237      ELSIF l_profile_name(j) = 'INV_CTP'                    THEN
18238 
18239        MSC_ATP_PVT.G_INV_CTP_SOURCE    := NVL(l_profile_value(j),  0 );
18240 
18241      ELSIF l_profile_name(j) = 'MSC_USE_SHIP_REC_CAL'       THEN
18242 
18243        l_use_ship_rec                  := NVL(l_profile_value(j), 'N');
18244 
18245      ELSIF l_profile_name(j) = 'MSC_MOVE_PAST_DUE_TO_SYSDATE'          THEN		--6316476
18246 
18247        MSC_ATP_PVT.G_MOV_PAST_DUE_SYSDATE_PROF := NVL(l_profile_value(j), 'Y');		--6316476
18248 
18249      ELSIF l_profile_name(j) = 'MSC_ZERO_ALLOC_PERC'		THEN		--6359986
18250 
18251      	 MSC_ATP_PVT.G_ZERO_ALLOCATION_PERC := NVL(l_profile_value(j), 'N');		--6359986
18252 
18253      ELSE
18254 
18255         IF PG_DEBUG in ('Y', 'C') THEN
18256             msc_sch_wb.atp_debug('Call_Schedule_Remote: Inside Else No match Got');
18257         END IF;
18258      END IF;
18259 
18260     END LOOP;
18261     --bug3940999 end honor profile options passed from the source
18262 
18263     /* --bug3940999 set above
18264     IF p_atp_debug_flag IS NOT NULL THEN
18265 	   FND_PROFILE.put('MSC_ATP_DEBUG', p_atp_debug_flag);
18266 	   PG_DEBUG := p_atp_debug_flag;
18267     END IF;
18268 
18269     msc_sch_wb.set_session_id(p_session_id);
18270     IF PG_DEBUG in ('Y', 'C') THEN
18271        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SESSION_ID : ' || order_sch_wb.debug_session_id);
18272     END IF;
18273 
18274     -- 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
18275     IF p_atp_workflow IS NOT NULL THEN
18276        FND_PROFILE.put('MSC_ALLOCATED_ATP_WORKFLOW', p_atp_workflow);
18277        G_ATP_WORKFLOW := p_atp_workflow;
18278     END IF;
18279 
18280     -- Bug 2387242 : krajan
18281     -- Set Sql Trace.
18282     IF order_sch_wb.mr_debug in ('T','C') THEN
18283          IF PG_DEBUG in ('Y', 'C') THEN
18284             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace being enabled');
18285          END IF;
18286          C := DBMS_SQL.OPEN_CURSOR;
18287          -- STATEMENT := 'ALTER SESSION SET SQL_TRACE=TRUE';
18288          STATEMENT := 'ALTER SESSION SET  events ' || '''' || '10046 trace name context forever, level 12' || '''' ;
18289          DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
18290          ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
18291          DBMS_SQL.CLOSE_CURSOR(C);
18292 
18293          -- rajjain 12/20/2002
18294          SELECT spid
18295          INTO   l_spid
18296          FROM   v$process
18297          WHERE  addr = (SELECT paddr FROM v$session
18298                         WHERE audsid=userenv('SESSIONID'));
18299          IF PG_DEBUG in ('Y', 'C') THEN
18300             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'spid: ' || l_spid);
18301          END IF;
18302 
18303     END IF;
18304 
18305     -- store p_inv_ctp and assignment_set_id passed from source
18306     -- in global variable for bug 2368426 starts
18307     --MSC_ATP_PVT.G_INV_CTP_SOURCE := NVL(p_inv_ctp, 0); --bug3940999 set above
18308     MSC_ATP_PVT.G_SR_ASSIGN_SET := NVL(p_def_assign_set_id, 0);
18309     -- changes for bug 2368426 ends
18310 
18311     IF PG_DEBUG in ('Y', 'C') THEN
18312         msc_sch_wb.atp_debug('***** Begin Call_Schedule_Remote Procedure *****');
18313         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_node_id      : ' || p_node_id);
18314         msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'p_use_ship_rec : ' || p_use_ship_rec);
18315     END IF;
18316     --bug3520746 start
18317     l_dblink := null;
18318     IF p_node_id is not null THEN
18319        BEGIN
18320          --Bug3765793 adding trim functions to remove spaces from db_link
18321          SELECT ltrim(rtrim(M2A_DBLINK))
18322          INTO   l_dblink
18323          FROM   msc_apps_instance_nodes
18324          WHERE  instance_id = p_instance_id and
18325                 node_id     = p_node_id;
18326        EXCEPTION
18327         WHEN OTHERS THEN
18328             l_dblink := null;
18329             IF PG_DEBUG in ('Y', 'C') THEN
18330              msc_sch_wb.atp_debug('Did not get records in rac case');
18331             END IF;
18332        END;
18333     ELSE --6697106
18334     ---bug 2301524: read summary flag
18335     --Bug3765793 adding trim functions to remove spaces from db_link
18336       SELECT NVL(l_dblink,ltrim(rtrim(M2A_DBLINK)))
18337       INTO   l_dblink
18338       FROM   msc_apps_instances
18339       WHERE  instance_id = p_instance_id;
18340     END IF;
18341     --bug3520746 end
18342     SELECT summary_flag, apps_ver
18343     INTO   l_summary_flag, MSC_ATP_PVT.G_APPS_VER
18344     FROM   msc_apps_instances
18345     WHERE  instance_id = p_instance_id;
18346     IF PG_DEBUG in ('Y', 'C') THEN
18347        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get m2a_dblink');
18348        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_dblink = ' || l_dblink);
18349        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'l_summary_flag := ' || l_summary_flag);
18350     END IF;
18351     */ --bug3940999 end
18352     MSC_ATP_PVT.G_SR_ASSIGN_SET := NVL(p_def_assign_set_id, 0);
18353     MSC_ATP_PVT.G_DATABASE_LINK := l_dblink;
18354 
18355     --s_cto_rearch: 24x7
18356     MSC_ATP_PVT.G_REFRESH_NUMBER := p_refresh_number;
18357     --e_cto_rearch: 24x7
18358 
18359     MSC_ATP_UTILS.Get_From_Temp_Table(
18360                 l_dblink,
18361                 p_session_id,
18362                 p_atp_table,
18363                 x_atp_supply_demand,
18364                 x_atp_period,
18365 		x_atp_details ,
18366 		MSC_ATP_UTILS.REQUEST_MODE,
18367                 x_return_status,
18368                 x_msg_data,
18369                 x_msg_count,
18370                 2);
18371 
18372     IF PG_DEBUG in ('Y', 'C') THEN
18373        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after get_from_temp_table'||p_atp_table.action.count);
18374        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'before Call_Schedule');
18375     END IF;
18376 
18377     --- 2301524: Summary mode is not supported for backlog workbench.
18378     IF PG_DEBUG in ('Y', 'C') THEN
18379        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_SUMMARY_FLAG := ' || MSC_ATP_PVT.G_SUMMARY_FLAG);
18380        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_ALLOCATED_ATP := ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
18381        msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'calling_module := '||  p_atp_table.calling_module(1));
18382     END IF;
18383     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'
18384           and MSC_ATP_PVT.G_ALLOCATED_ATP = 'N' AND MSC_ATP_PVT.G_INV_CTP = 5 THEN  -- INV_CTP check added for summary enhancement
18385           --- if backlog workbench is used then set the summary falg to 200 for ODS cases
18386           IF PG_DEBUG in ('Y', 'C') THEN
18387              msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Site is using backlog workbench, update tables and turnoff ODS summary');
18388           END IF;
18389 
18390           update  msc_apps_instances
18391           set     summary_flag = 200;
18392 
18393           MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
18394     ELSIF l_summary_flag = 200 AND MSC_ATP_PVT.G_INV_CTP = 5 THEN  -- INV_CTP check added for summary enhancement
18395           --site is using backlog workbench, turn off the summary flag for ODS cases;
18396           IF PG_DEBUG in ('Y', 'C') THEN
18397              msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Site is using backlog workbench, turnoff ODS summary');
18398           END IF;
18399           MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
18400     END IF;
18401 
18402 
18403     -- bug3940999 Moved code from Line 579 in Schedule here, not directly due to this bug
18404     -- Also changed the order of IF condition.
18405     IF l_use_ship_rec <> MSC_ATP_PVT.G_USE_SHIP_REC_CAL THEN --bug3593394
18406 
18407         --bug3593394
18408         IF PG_DEBUG in ('Y', 'C') THEN
18409             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'G_USE_SHIP_REC_CAL doesnt match');
18410         END IF;
18411         x_atp_table := p_atp_table;
18412         i := x_atp_table.action.FIRST;
18413         WHILE i IS NOT NULL LOOP
18414             IF (i=1) or
18415                (NVL(x_atp_table.Ship_Set_Name(i),-99) <> NVL(x_atp_table.Ship_Set_Name(i-1),-100) AND
18416                        NVL(x_atp_table.Arrival_Set_Name(i),-99) <> NVL(x_atp_table.Arrival_Set_Name(i-1),-100)) THEN
18417                 -- Independent line or first line of a set. Error should be USE_SHIP_REC_NOT_IN_SYNC
18418                 x_atp_table.error_code(i) := MSC_ATP_PVT.USE_SHIP_REC_NOT_IN_SYNC;
18419             ELSE
18420                 -- Part of a set. Error code should be
18421                 x_atp_table.error_code(i) := MSC_ATP_PVT.GROUPEL_ERROR;
18422             END IF;
18423             IF PG_DEBUG in ('Y', 'C') THEN
18424                 msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'error_code(' || i || ') : ' || x_atp_table.error_code(i));
18425             END IF;
18426             i := p_atp_table.Action.NEXT(i);
18427        END LOOP;
18428 
18429     ELSIF MSC_ATP_PVT.G_INV_CTP_SOURCE <> MSC_ATP_PVT.G_INV_CTP THEN
18430 
18431        IF PG_DEBUG in ('Y', 'C') THEN
18432            msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Source INV_CTP not in sync with destination ');
18433        END IF;
18434        x_atp_table := p_atp_table;
18435         i := x_atp_table.action.FIRST;
18436         WHILE i IS NOT NULL LOOP
18437             IF (i=1) or
18438                (NVL(x_atp_table.Ship_Set_Name(i),-99) <> NVL(x_atp_table.Ship_Set_Name(i-1),-100) AND
18439                        NVL(x_atp_table.Arrival_Set_Name(i),-99) <> NVL(x_atp_table.Arrival_Set_Name(i-1),-100)) THEN
18440                 -- Independent line or first line of a set. Error should be INV_CTP_NOT_IN_SYNC
18441                 x_atp_table.error_code(i) := MSC_ATP_PVT.INV_CTP_NOT_IN_SYNC;
18442             ELSE
18443                 -- Part of a set. Error code should be
18444                 x_atp_table.error_code(i) := MSC_ATP_PVT.GROUPEL_ERROR;
18445             END IF;
18446             IF PG_DEBUG in ('Y', 'C') THEN
18447                 msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'error_code(' || i || ') : ' || x_atp_table.error_code(i));
18448             END IF;
18449             i := p_atp_table.Action.NEXT(i);
18450        END LOOP;
18451 
18452     ELSE
18453 
18454        MSC_ATP_PVT.Call_Schedule(p_session_id,
18455                 p_atp_table,
18456                 p_instance_id,
18457                 p_assign_set_id,
18458                 p_refresh_number,
18459                 x_atp_table,
18460                 x_return_status,
18461                 x_msg_data,
18462                 x_msg_count,
18463                 x_atp_supply_demand,
18464                 x_atp_period,
18465                 x_atp_details);
18466 
18467         IF PG_DEBUG in ('Y', 'C') THEN
18468             msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'after Call_Schedule');
18469         END IF;
18470 
18471     END IF;
18472 
18473     MSC_ATP_UTILS.Put_Into_Temp_Table(
18474                 l_dblink,
18475                 p_session_id,
18476                 x_atp_table,
18477                 x_atp_supply_demand,
18478                 x_atp_period,
18479                 x_atp_details,
18480 		MSC_ATP_UTILS.RESULTS_MODE,
18481                 x_return_status,
18482                 x_msg_data,
18483                 x_msg_count);
18484 
18485 IF PG_DEBUG in ('Y', 'C') THEN
18486    msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'here');
18487 END IF;
18488     IF l_dblink IS NOT NULL THEN
18489 	MSC_ATP_UTILS.Transfer_mrp_atp_details_temp(l_dblink, p_session_id);
18490     END IF;
18491 
18492     -- Bug 2387242 : krajan
18493     -- Set Sql Trace.
18494     IF order_sch_wb.mr_debug in ('T','C') THEN
18495                IF PG_DEBUG in ('Y', 'C') THEN
18496                   msc_sch_wb.atp_debug('Call_Schedule_Remote: ' || 'Database Trace disabled');
18497                END IF;
18498                C := DBMS_SQL.OPEN_CURSOR;
18499                STATEMENT := 'ALTER SESSION SET SQL_TRACE=FALSE';
18500                DBMS_SQL.PARSE(C, STATEMENT, DBMS_SQL.NATIVE);
18501                ROWS_PROCESSED := DBMS_SQL.EXECUTE(C);
18502                DBMS_SQL.CLOSE_CURSOR(C);
18503     END IF;
18504 
18505     IF PG_DEBUG in ('Y', 'C') THEN
18506        msc_sch_wb.atp_debug('***** End Call_Schedule_Remote Procedure *****');
18507     END IF;
18508 
18509 END Call_Schedule_Remote;
18510 PROCEDURE Process_Time_Stamp_Errors (l_atp_table  IN OUT NOCOPY   MRP_ATP_PUB.ATP_Rec_Typ,
18511                                      i NUMBER)
18512 IS
18513    BEGIN
18514 
18515       IF PG_DEBUG in ('Y', 'C') THEN
18516          msc_sch_wb.atp_debug('********** Begin Process_Time_Stamp_Errors **********');
18517           msc_sch_wb.atp_debug('ship_date:(Before) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
18518           msc_sch_wb.atp_debug('Req. ship_date:(Before) ' ||  to_char(l_atp_table.requested_ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
18519           msc_sch_wb.atp_debug('MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP := ' ||  MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP);
18520           msc_sch_wb.atp_debug('MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET := ' ||  MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET);
18521       END IF;
18522        IF MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP = 'Y' and MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET = 'N' THEN
18523           IF l_atp_table.requested_ship_date(i) is not null then
18524              l_atp_table.ship_date(i) := l_atp_table.ship_date(i) +
18525                 (l_atp_table.requested_ship_date(i)
18526                  - TRUNC(l_atp_table.requested_ship_date(i)));
18527              l_atp_table.arrival_date(i) := l_atp_table.arrival_date(i) +  --4967040
18528                 (l_atp_table.requested_ship_date(i)
18529                  - TRUNC(l_atp_table.requested_ship_date(i)));
18530 
18531           ELSE
18532              l_atp_table.arrival_date(i) := l_atp_table.arrival_date(i) +
18533                 (l_atp_table.requested_arrival_date(i)
18534                  - TRUNC(l_atp_table.requested_arrival_date(i)));
18535              l_atp_table.ship_date(i) := l_atp_table.ship_date(i) +        --4967040
18536                 (l_atp_table.requested_arrival_date(i)
18537                  - TRUNC(l_atp_table.requested_arrival_date(i)));
18538           END IF;
18539        ELSE
18540           IF l_atp_table.requested_ship_date(i) is not null then
18541              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
18542              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY; --4967040
18543           ELSE
18544              l_atp_table.arrival_date(i) := trunc(l_atp_table.arrival_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
18545              l_atp_table.ship_date(i) := trunc(l_atp_table.ship_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY; --4967040
18546 
18547           END IF;
18548        END IF;
18549        MSC_ATP_PROC.G_latest_ship_date_set := GREATEST(NVL(MSC_ATP_PROC.G_latest_ship_date_set, l_atp_table.ship_date(i)),
18550                                                        NVL(l_atp_table.ship_date(i), MSC_ATP_PROC.G_latest_ship_date_set));
18551        MSC_ATP_PROC.G_latest_arr_date_set  := GREATEST(NVL(MSC_ATP_PROC.G_latest_arr_date_set, l_atp_table.arrival_date(i)),
18552                                                        NVL(l_atp_table.arrival_date(i), MSC_ATP_PROC.G_latest_arr_date_set));
18553        IF PG_DEBUG in ('Y', 'C') THEN
18554           msc_sch_wb.atp_debug('ship_date:(After) ' ||  to_char(l_atp_table.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
18555           msc_sch_wb.atp_debug('Arrival_date:(After) ' ||  to_char(l_atp_table.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));
18556           msc_sch_wb.atp_debug('Req. ship_date:(After) ' ||  to_char(l_atp_table.requested_ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
18557           msc_sch_wb.atp_debug('********** END Process_Time_Stamp_Errors **********');
18558        END IF;
18559     END Process_Time_Stamp_Errors;
18560 -- Comment
18561 END MSC_ATP_PVT;