DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_PROC

Source


1 PACKAGE BODY MSC_ATP_PROC AS
2 /* $Header: MSCPATPB.pls 120.11 2007/12/12 10:33:45 sbnaik ship $  */
3 G_PKG_NAME 		CONSTANT VARCHAR2(30) := 'MSC_ATP_PROC';
4 
5 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
6 
7 -- dlt dsting. replaced hash with nested table since 8i doesn't support it
8 MAX_DLT_CACHE_SZ        NUMBER := 10;
9 dlt_lookup              MRP_ATP_PUB.char80_arr := MRP_ATP_PUB.char80_arr();
10 dlt_cache               MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
11 ship_method_cache       MRP_ATP_PUB.char30_arr := MRP_ATP_PUB.char30_arr();
12 dlt_idx                 NUMBER := 0;
13 
14 -- 2834932
15 G_NONWORKING_DAY        CONSTANT NUMBER := 1;
16 G_set_nonoverride_flag  VARCHAR2(1);
17 G_set_override_flag     VARCHAR2(1);
18 G_set_status            NUMBER;
19 G_is_ship_set           BOOLEAN;
20 G_override_date         DATE;
21 G_ship_EAD_set          DATE;
22 G_ship_LAD_set          DATE;
23 G_arr_LAD_set           DATE;
24 --G_latest_ship_date_set  DATE; --4460369
25 --G_latest_arr_date_set   DATE; --4460369
26 
27 -- Private procedure Get_Sources_Info added for bug 2585710
28 PROCEDURE Get_Sources_Info(p_session_id             IN  NUMBER,
29                            p_inventory_item_id      IN  NUMBER,
30                            p_customer_id            IN  NUMBER,
31                            p_customer_site_id       IN  NUMBER,
32                            p_assignment_set_id      IN  NUMBER,
33                            p_ship_set_item_count    IN  NUMBER,
34                            x_atp_sources            OUT NOCOPY MRP_ATP_PVT.Atp_Source_Typ,
35                            x_return_status          OUT NOCOPY VARCHAR2,
36                            p_partner_type           IN  NUMBER, --2814895
37 	                   p_party_site_id          IN  NUMBER, --2814895
38 	                   p_order_line_id          IN  NUMBER  --2814895
39                            );
40 
41 PROCEDURE add_inf_time_fence_to_period(
42   p_level			IN  NUMBER,
43   p_identifier                  IN  NUMBER,
44   p_scenario_id                 IN  NUMBER,
45   p_inventory_item_id           IN  NUMBER,
46   p_request_item_id		IN  NUMBER,
47   p_organization_id             IN  NUMBER,
48   p_supplier_id                 IN  NUMBER,
49   p_supplier_site_id            IN  NUMBER,
50   p_infinite_time_fence_date    IN  DATE,
51   x_atp_period                  IN OUT NOCOPY 	MRP_ATP_PUB.ATP_Period_Typ
52 
53 ) IS
54   j			NUMBER;
55   l_return_status 	VARCHAR2(1);
56 BEGIN
57 
58         IF PG_DEBUG in ('Y', 'C') THEN
59            msc_sch_wb.atp_debug('PROCEDURE add_inf_time_fence_to_period');
60         END IF;
61 
62 	IF p_infinite_time_fence_date IS NULL THEN
63 		RETURN;
64 	END IF;
65 
66         -- add one more entry to indicate infinite time fence date
67         -- and quantity.
68         MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, l_return_status);
69         j := x_atp_period.Level.COUNT;
70 
71         IF j > 1 THEN
72           x_atp_period.Period_End_Date(j-1) := p_infinite_time_fence_date - 1;
73           x_atp_period.Identifier1(j) := x_atp_period.Identifier1(j-1);
74           x_atp_period.Identifier2(j) := x_atp_period.Identifier2(j-1);
75         END IF;
76 
77         x_atp_period.Level(j) := p_level;
78         x_atp_period.Identifier(j) := p_identifier;
79         x_atp_period.Scenario_Id(j) := p_scenario_id;
80         x_atp_period.Pegging_Id(j) := NULL;
81         x_atp_period.End_Pegging_Id(j) := NULL;
82         x_atp_period.Inventory_Item_Id(j) := p_inventory_item_id;
83 	-- dsting: changed p_inventory_item_id to p_request_item_id
84         x_atp_period.Request_Item_Id(j) := p_request_item_id;
85         x_atp_period.Organization_id(j) := p_organization_id;
86 	x_atp_period.supplier_id(j) := p_supplier_id;
87 	x_atp_period.supplier_site_id(j) := p_supplier_site_id;
88         x_atp_period.Period_Start_Date(j) := p_infinite_time_fence_date;
89         x_atp_period.Total_Supply_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
90         x_atp_period.Total_Demand_Quantity(j) := 0;
91         x_atp_period.Total_Bucketed_Demand_Quantity(j) := 0; -- for time_phased_atp
92         x_atp_period.Period_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
93         x_atp_period.Cumulative_Quantity(j) := MSC_ATP_PVT.INFINITE_NUMBER;
94 
95 END add_inf_time_fence_to_period;
96 
97 --
98 -- dsting 9/17/2002
99 --
100 -- Populate the period record with data from the temp table
101 -- msc_atp_sd_details_temp
102 --
103 -- NOTE: as part of the pegging enhancement this procedure assumes that
104 -- only 1 item's data is in the session specific temp table
105 --
106 PROCEDURE get_period_data_from_SD_temp(
107   x_atp_period                  OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ
108 ) IS
109   i			NUMBER;
110   j			NUMBER;
111   x_return_status 	NUMBER;
112 BEGIN
113 
114 	IF PG_DEBUG in ('Y', 'C') THEN
115 	   msc_sch_wb.atp_debug('PROCEDURE get_period_data_from_SD_temp');
116 	END IF;
117 
118      SELECT
119 	 ATP_level
120 --	,order_line_id
121 	,scenario_id
122 	,inventory_item_id
123 	,request_item_id
124 	,organization_id
125 	,supplier_id
126 	,supplier_site_id
127 	,department_id
128 	,resource_id
129 	,supply_demand_date
130 	,identifier1
131 	,identifier2
132 	,SUM(DECODE(supply_demand_type, 1, supply_demand_quantity, 0))
133 		total_demand_quantity
134 	,SUM(DECODE(supply_demand_type, 2, supply_demand_quantity, 0))
135 		total_supply_quantity
136 	,SUM(supply_demand_quantity)
137      BULK COLLECT INTO
138         x_atp_period.Level,
139 --        x_atp_period.Identifier,
140         x_atp_period.Scenario_Id,
141         x_atp_period.Inventory_Item_Id,
142         x_atp_period.Request_Item_Id,
143         x_atp_period.Organization_id,
144 	x_atp_period.Supplier_ID,
145 	x_atp_period.Supplier_Site_ID,
146         x_atp_period.Department_id,
147         x_atp_period.Resource_id,
148         x_atp_period.Period_Start_Date,
149         x_atp_period.Identifier1,
150         x_atp_period.Identifier2,
151         x_atp_period.Total_Demand_Quantity,
152         x_atp_period.Total_Supply_Quantity,
153         x_atp_period.Period_Quantity
154      FROM msc_atp_sd_details_temp
155      GROUP BY
156 	supply_demand_date
157 	,ATP_level
158 --	,order_line_id
159 	,scenario_id
160 	,inventory_item_id
161 	,request_item_id
162 	,organization_id
163 	,supplier_id
164 	,supplier_site_id
165 	,department_id
166 	,resource_id
167 	,identifier1
168 	,identifier2
169      ORDER BY supply_demand_date;
170 
171      -- set the period end dates and
172      -- extend the remaining fields to ensure same behaviour as before
173      i := x_atp_period.Period_Start_Date.COUNT;
174 
175      x_atp_period.Identifier.EXTEND(i);
176      x_atp_period.Pegging_Id.EXTEND(i);
177      x_atp_period.End_Pegging_Id.EXTEND(i);
178      x_atp_period.Period_End_Date.EXTEND(i);
179      x_atp_period.From_Location_Id.EXTEND(i);
180      x_atp_period.From_Organization_Id.EXTEND(i);
181      x_atp_period.Ship_Method.EXTEND(i);
182      x_atp_period.To_Location_Id.EXTEND(i);
183      x_atp_period.To_Organization_Id.EXTEND(i);
184      x_atp_period.Uom.EXTEND(i);
185 
186      --pf chnages -vivek
187      x_atp_period.total_bucketed_demand_quantity.extend(i);
188 
189      FOR j IN 1..(i-1) LOOP
190 	x_atp_period.Period_End_Date(j) :=
191 		x_atp_period.Period_Start_Date(j+1) - 1;
192      END LOOP;
193 
194 END get_period_data_from_SD_temp;
195 
196 -- New procedure added as part of time_phased_atp to fix the
197 -- issue of not displaying correct quantities in ATP SD Window when
198 -- user opens ATP SD window from ATP pegging in allocated scenarios
199 PROCEDURE Get_Alloc_Data_From_Sd_Temp(
200   x_atp_period                  OUT NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
201   x_return_status               OUT NOCOPY VARCHAR2
202 ) IS
203   i			NUMBER;
204   j			NUMBER;
205 BEGIN
206 
207 	IF PG_DEBUG in ('Y', 'C') THEN
208 	   msc_sch_wb.atp_debug('PROCEDURE Get_Alloc_Data_From_Sd_Temp');
209 	END IF;
210 
211         -- initialize API return status to success
212         x_return_status := FND_API.G_RET_STS_SUCCESS;
213 
214      SELECT
215 	 ATP_level
216 	,scenario_id
217 	,inventory_item_id
218 	,request_item_id
219 	,organization_id
220 	,supplier_id
221 	,supplier_site_id
222 	,department_id
223 	,resource_id
224 	,supply_demand_date
225 	,identifier1
226 	,identifier2
227 	,SUM(DECODE(supply_demand_type, 1, allocated_quantity, 0))
228 		total_demand_quantity
229 	,SUM(DECODE(supply_demand_type, 2, allocated_quantity, 0))
230 		total_supply_quantity
231 	,SUM(allocated_quantity)
232      BULK COLLECT INTO
233         x_atp_period.Level,
234         x_atp_period.Scenario_Id,
235         x_atp_period.Inventory_Item_Id,
236         x_atp_period.Request_Item_Id,
237         x_atp_period.Organization_id,
238 	x_atp_period.Supplier_ID,
239 	x_atp_period.Supplier_Site_ID,
240         x_atp_period.Department_id,
241         x_atp_period.Resource_id,
242         x_atp_period.Period_Start_Date,
243         x_atp_period.Identifier1,
244         x_atp_period.Identifier2,
245         x_atp_period.Total_Demand_Quantity,
246         x_atp_period.Total_Supply_Quantity,
247         x_atp_period.Period_Quantity
248      FROM msc_atp_sd_details_temp
249      GROUP BY
250 	supply_demand_date
251 	,ATP_level
252 	,scenario_id
253 	,inventory_item_id
254 	,request_item_id
255 	,organization_id
256 	,supplier_id
257 	,supplier_site_id
258 	,department_id
259 	,resource_id
260 	,identifier1
261 	,identifier2
262      ORDER BY supply_demand_date;
263 
264      -- set the period end dates and
265      -- extend the remaining fields to ensure same behaviour as before
266      i := x_atp_period.Period_Start_Date.COUNT;
267 
268      x_atp_period.Identifier.EXTEND(i);
269      x_atp_period.Pegging_Id.EXTEND(i);
270      x_atp_period.End_Pegging_Id.EXTEND(i);
271      x_atp_period.Period_End_Date.EXTEND(i);
272      x_atp_period.From_Location_Id.EXTEND(i);
273      x_atp_period.From_Organization_Id.EXTEND(i);
274      x_atp_period.Ship_Method.EXTEND(i);
275      x_atp_period.To_Location_Id.EXTEND(i);
276      x_atp_period.To_Organization_Id.EXTEND(i);
277      x_atp_period.Uom.EXTEND(i);
278      x_atp_period.total_bucketed_demand_quantity.extend(i);
279 
280      FOR j IN 1..(i-1) LOOP
281 	x_atp_period.Period_End_Date(j) :=
282 		x_atp_period.Period_Start_Date(j+1) - 1;
283      END LOOP;
284 
285 EXCEPTION
286   WHEN OTHERS THEN
287         IF PG_DEBUG in ('Y', 'C') THEN
288            msc_sch_wb.atp_debug('Get_Alloc_Data_From_Sd_Temp: ' || 'Error code:' || to_char(sqlcode));
289         END IF;
290         x_return_status := FND_API.G_RET_STS_ERROR;
291 
292 END Get_Alloc_Data_From_Sd_Temp;
293 
294 PROCEDURE Atp_Sources (p_instance_id               IN   NUMBER,
295                        p_plan_id                   IN   NUMBER,
296                        p_inventory_item_id         IN   NUMBER,
297                        p_organization_id           IN   NUMBER,
298                        p_customer_id               IN   NUMBER,
299                        p_customer_site_id          IN   NUMBER,
300                        p_assign_set_id             IN   NUMBER,
301 		       ---p_ship_set_item	           IN	MRP_ATP_PUB.number_arr,
302                        p_item_sourcing_info_rec    IN   MSC_ATP_CTO.Item_Sourcing_Info_Rec,
303                        p_session_id                IN   NUMBER,
304                        x_atp_sources               OUT  NoCopy MRP_ATP_PVT.Atp_Source_Typ,
305                        x_return_status             OUT  NoCopy VARCHAR2,
306                        p_partner_type              IN   NUMBER, --2814895
307 	               p_party_site_id             IN   NUMBER, --2814895
308 	               p_order_line_id             IN   NUMBER  --2814895
309 ) IS
310 -- Extra unwanted variables removed for bug 2585710
311 i PLS_INTEGER :=1;
312 l_count PLS_INTEGER;
313 l_preferred_rank NUMBER;
314 l_customer_id number;
315 l_ship_to_site_id number;
316 l_inv_item_id number;
317 l_distinct_item PLS_INTEGER;
318 l_organization_id  NUMBER;
319 
320 /* Variables added for Bug 2585710 start */
321 l_dist_sr_ship_set_item_list    MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
322 l_dest_ship_set_item_list       MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
323 l_sysdate                       DATE;
324 l_return_status                 VARCHAR2(100);
325 /* Variables added for Bug 2585710 end */
326 --s_cto_rearch
327 l_model_flag  number := 2;
328 l_line_ids    MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
329 l_source_list MRP_ATP_PVT.Atp_Source_Typ;
330 --e_cto_rearch
331 
332 BEGIN
333 
334     IF PG_DEBUG in ('Y', 'C') THEN
335        msc_sch_wb.atp_debug('***** Begin Atp_Sources *********');
336     END IF;
337     -- initialize API return status to success
338     x_return_status := FND_API.G_RET_STS_SUCCESS;
339 
340     IF PG_DEBUG in ('Y', 'C') THEN
341        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_instance_id = '||p_instance_id);
342        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_plan_id = '||p_plan_id);
343        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_inventory_item_id = '||p_inventory_item_id);
344        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_organization_id = '||p_organization_id);
345        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_customer_id = '||p_customer_id);
346        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_customer_site_id = '||p_customer_site_id);
347        msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_assign_set_id = '||p_assign_set_id);
348     END IF;
349 
350     -- For bug 2585710. Store sysdate in local variable once and for all.
351     -- Replace trunc(sysdate) everwhere.
352     -- SELECT trunc(sysdate) INTO l_sysdate FROM dual;
353     -- Modified to change explicit SELECT to direct assignment.
354     l_sysdate := TRUNC(sysdate);
355 
356     IF (p_customer_id is not null) and (p_customer_site_id is not null) THEN
357       BEGIN
358     	SELECT TP_ID
359     	INTO   l_customer_id
360     	FROM   msc_tp_id_lid tp
361     	WHERE  tp.SR_TP_ID = p_customer_id
362     	AND    tp.SR_INSTANCE_ID = p_instance_id
363     	AND    tp.PARTNER_TYPE = 2;
364 
365     	IF PG_DEBUG in ('Y', 'C') THEN
366     	   msc_sch_wb.atp_debug('Atp_Sources: ' || 'l_customer_id '||l_customer_id);
367     	END IF;
368 
369     	SELECT TP_SITE_ID
370     	INTO   l_ship_to_site_id
371     	FROM   msc_tp_site_id_lid tpsite
372     	WHERE  tpsite.SR_TP_SITE_ID = p_customer_site_id
373     	AND    tpsite.SR_INSTANCE_ID =  p_instance_id
374     	AND    tpsite.PARTNER_TYPE = 2;
375         IF PG_DEBUG in ('Y', 'C') THEN
376            msc_sch_wb.atp_debug('Atp_Sources: ' || 'l_ship_to_site_id '||l_ship_to_site_id);
377         END IF;
378       EXCEPTION
379 	WHEN NO_DATA_FOUND THEN
380              IF PG_DEBUG in ('Y', 'C') THEN
381                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Customer id : '|| p_customer_id ||' not collected as yet');
382              END IF;
383 	     l_customer_id := NULL;
384 	     l_ship_to_site_id := NULL;
385       END;
386     END IF;
387 
388     --s_cto_rearch
389     --IF p_ship_set_item.count > 1 THEN
390     IF p_item_sourcing_info_rec.sr_inventory_item_id.count > 1 THEN
391 
392        -- this is for ship set
393         IF PG_DEBUG in ('Y', 'C') THEN
394                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Request is for ship set not a single line item');
395                 FOR i in 1.. p_item_sourcing_info_rec.sr_inventory_item_id.COUNT LOOP
396                         msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_ship_set_item(i) = '
397                                                 ||  p_item_sourcing_info_rec.sr_inventory_item_id(i));
398                 END LOOP;
399         END IF;
400 
401        /* For bug 2585710. A single for loop removes the duplicate items in ship set and
402        also compute destination inventory item id */
403 
404         --FOR i in 1..p_ship_set_item.COUNT LOOP
405         FOR i in 1..p_item_sourcing_info_rec.sr_inventory_item_id.COUNT LOOP
406 
407            IF (i = 1) THEN
408 
409                 -- This is first item. Just store it in l_dist_sr_ship_set_item_list
410                 l_dist_sr_ship_set_item_list.EXTEND;
411 
412                 --s_cto_reach
413                 l_line_ids.extend;
414                 l_line_ids(l_line_ids.count) := p_item_sourcing_info_rec.line_id(i);
415                 ---if model is invloved then set the model flag on
416                 IF p_item_sourcing_info_rec.ato_line_id(i) = p_item_sourcing_info_rec.line_id(i) THEN
417                   l_model_flag := 1;
418                 END IF;
419 
420                 l_dist_sr_ship_set_item_list(l_dist_sr_ship_set_item_list.COUNT)
421                                              :=  p_item_sourcing_info_rec.sr_inventory_item_id(i);
422                 --l_dist_sr_ship_set_item_list(l_dist_sr_ship_set_item_list.COUNT) := p_ship_set_item(i);
423 
424                 -- Get the dest inv item id and store that it in l_dest_ship_set_item_list
425                 l_dest_ship_set_item_list.EXTEND;
426                 l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) :=
427                                         MSC_ATP_FUNC.get_inv_item_id(p_instance_id,
428                                           p_item_sourcing_info_rec.sr_inventory_item_id(i),
429                                           p_item_sourcing_info_rec.match_item_id(i),
430                                           --p_ship_set_item(i),
431                                           p_organization_id);
432 
433                 -- 4091487 added elsif condition to raise try atp later error in case msc_item_id_lid contains no records
434                 -- If dest inv item id is null return from procedure means this item not collected
435                 IF l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) IS NULL THEN
436                     --MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := p_ship_set_item(i);
437                     MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID :=  p_item_sourcing_info_rec.sr_inventory_item_id(i);
438                     --x_return_status := FND_API.G_RET_STS_ERROR;
439                     --return;
440                     x_return_status := MSC_ATP_PVT.G_ITEM_NOT_COLL;
441 
442                     IF PG_DEBUG in ('Y', 'C') THEN
443                         msc_sch_wb.atp_debug('Atp_Sources: ' || 'Inv Item Id not found for : ' ||
444                                                                 p_item_sourcing_info_rec.sr_inventory_item_id(i));
445                         msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
446                     END IF;
447                     RAISE NO_DATA_FOUND;
448                 -- If dest inv item id is -1 return from procedure means no items not collected
449                 ELSIF  l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) = -1 THEN
450                     x_return_status := MSC_ATP_PVT.G_ITEM_ID_NULL;
451                     IF PG_DEBUG in ('Y', 'C') THEN
452                         msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
453                     END IF;
454                     RAISE NO_DATA_FOUND;
455                 END IF;
456 
457 
458            ELSE
459                 -- This is not the first item. First check this in distinct source item ship set.
460 
461                 l_distinct_item := 1; -- assume at first this is unique
462 
463                 IF p_item_sourcing_info_rec.ato_line_id(i) is not null and
464                    p_item_sourcing_info_rec.ato_line_id(i) <>  p_item_sourcing_info_rec.line_id(i) THEN
465                    ---we do not look for sourcing for components of ato model
466                    l_distinct_item := 0;
467 
468                    IF PG_DEBUG in ('Y', 'C') THEN
469                         msc_sch_wb.atp_debug('Model component, ignore for sourcing');
470                    END IF;
471 
472                 ELSE
473 
474                    FOR j in 1..l_dist_sr_ship_set_item_list.COUNT LOOP
475                               --IF (p_ship_set_item(i) = l_dist_sr_ship_set_item_list(j)) THEN
476                               IF ( p_item_sourcing_info_rec.sr_inventory_item_id(i) = l_dist_sr_ship_set_item_list(j)) THEN
477                                       l_distinct_item := 0;
478                                       EXIT;
479                               END IF;
480                    END LOOP;
481                 END IF;
482                 IF (l_distinct_item = 1) THEN
483 
484                         -- This is a distinct item. Do the same as for i = 1
485                         l_dist_sr_ship_set_item_list.EXTEND;
486 
487                         --s_cto_reach
488                         l_line_ids.extend;
489                         l_line_ids(l_line_ids.count) := p_item_sourcing_info_rec.line_id(i);
490                         ---if model is invloved then set the model flag on
491                         IF p_item_sourcing_info_rec.ato_line_id(i) = p_item_sourcing_info_rec.line_id(i) THEN
492                           l_model_flag := 1;
493                         END IF;
494 
495                         --l_dist_sr_ship_set_item_list(l_dist_sr_ship_set_item_list.COUNT) := p_ship_set_item(i);
496                         l_dist_sr_ship_set_item_list(l_dist_sr_ship_set_item_list.COUNT) :=
497                                                        p_item_sourcing_info_rec.sr_inventory_item_id(i);
498 
499                         -- Get the dest inv item id and store that it in l_dest_ship_set_item_list
500                         l_dest_ship_set_item_list.EXTEND;
501                         l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) :=
502                                                 MSC_ATP_FUNC.get_inv_item_id(p_instance_id,
503                                                   --p_ship_set_item(i),
504                                                    p_item_sourcing_info_rec.sr_inventory_item_id(i),
505                                                    p_item_sourcing_info_rec.match_item_id(i),
506                                                   p_organization_id);
507                  -- 4091487 added elsif condition to raise try atp later error in case msc_item_id_lid contains no records
508                  -- If dest inv item id is null return from procedure means this item not collected
509                  IF l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) IS NULL THEN
510                     --MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := p_ship_set_item(i);
511                     MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID :=  p_item_sourcing_info_rec.sr_inventory_item_id(i);
512                     --x_return_status := FND_API.G_RET_STS_ERROR;
513                     --return;
514                     x_return_status := MSC_ATP_PVT.G_ITEM_NOT_COLL;
515                     IF PG_DEBUG in ('Y', 'C') THEN
516                        msc_sch_wb.atp_debug('Atp_Sources: ' || 'Inv Item Id not found for : ' ||
517                                                                p_item_sourcing_info_rec.sr_inventory_item_id(i));
518                        msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
519                     END IF;
520                     RAISE NO_DATA_FOUND;
521                     -- If dest inv item id is -1 return from procedure means no items not collected
522                 ELSIF  l_dest_ship_set_item_list(l_dest_ship_set_item_list.COUNT) = -1 THEN
523                     x_return_status := MSC_ATP_PVT.G_ITEM_ID_NULL;
524                     IF PG_DEBUG in ('Y', 'C') THEN
525                         msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
526                     END IF;
527                     RAISE NO_DATA_FOUND;
528                 END IF;
529 
530 	       END IF;
531 
532 	  END IF;
533 
534        END LOOP;
535 
536        msc_sch_wb.atp_debug('Atp_Sources: ' || 'l_count := ' || l_count);
537        l_count := l_dest_ship_set_item_list.COUNT;
538         IF PG_DEBUG in ('Y', 'C') THEN
539            msc_sch_wb.atp_debug('Atp_Sources: ' || 'No of distinct item in the ship set := '||l_count);
540            FOR j in 1..l_count LOOP
541               msc_sch_wb.atp_debug('Atp_Sources: ' || 'l_dest_ship_set_item_list(j) = '||l_dest_ship_set_item_list(j));
542            END LOOP;
543         END IF;
544 
545 
546        -- Bug 2585710. Store ship set item inventory numbers into 8i temp table.
547 
548        DELETE MSC_SHIP_SET_TEMP;
549 
550        FORALL j IN 1..l_count
551        --s_cto_rearch
552        INSERT INTO MSC_SHIP_SET_TEMP(INVENTORY_ITEM_ID, VISITED_FLAG, MIN_REGION_VALUE, line_id)
553        VALUES (l_dest_ship_set_item_list(j), 0, 0, l_line_ids(j));
554 
555     ELSE
556 
557        IF PG_DEBUG in ('Y', 'C') THEN
558           msc_sch_wb.atp_debug('Atp_Sources: ' || 'Request is for single line item and not ship set');
559        END IF;
560 
561        l_inv_item_id :=  MSC_ATP_FUNC.get_inv_item_id (p_instance_id,
562                                        --p_inventory_item_id,
563                                        --s_cto_rearch
564                                        p_item_sourcing_info_rec.sr_inventory_item_id(1),
565                                        p_item_sourcing_info_rec.match_item_id(1),
566                                        --e_cto_rearch
567                                        p_organization_id);
568        IF PG_DEBUG in ('Y', 'C') THEN
569           msc_sch_wb.atp_debug('Atp_Sources: ' || 'Inv item Id := ' || l_inv_item_id);
570        END IF;
571        -- 4091487 added elsif condition to raise try atp later error in case msc_item_id_lid contains no records
572         ---bug 1905037
573         -- If dest inv item id is -1 return from procedure means this item is not collected
574        IF (l_inv_item_id IS NULL) THEN
575             --MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := p_inventory_item_id;
576             MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := p_item_sourcing_info_rec.sr_inventory_item_id(1);
577             --x_return_status := FND_API.G_RET_STS_ERROR;
578             --return;
579            x_return_status := MSC_ATP_PVT.G_ITEM_NOT_COLL;
580            IF PG_DEBUG in ('Y', 'C') THEN
581               msc_sch_wb.atp_debug('Atp_Sources: ' || 'Inv Item Id not found for : ' ||
582                                                                p_item_sourcing_info_rec.sr_inventory_item_id(1));
583               msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
584            END IF;
585            RAISE NO_DATA_FOUND;
586            -- If dest inv item id is -1 return from procedure means no items not collected
587        ELSIF  l_inv_item_id = -1 THEN
588            x_return_status := MSC_ATP_PVT.G_ITEM_ID_NULL;
589            IF PG_DEBUG in ('Y', 'C') THEN
590               msc_sch_wb.atp_debug('return status assigned = ' || x_return_status);
591            END IF;
592            RAISE NO_DATA_FOUND;
593        END IF;
594        IF p_item_sourcing_info_rec.line_id(1) = p_item_sourcing_info_rec.ato_line_id(1) THEN
595            IF PG_DEBUG in ('Y', 'C') THEN
596               msc_sch_wb.atp_debug('Atp_Sources: ' || 'Model Line, Add line id');
597                msc_sch_wb.atp_debug('Atp_Sources: line_id := ' ||  p_item_sourcing_info_rec.line_id(1));
598            END IF;
599            l_model_flag := 1;
600            l_line_ids.extend;
601            l_line_ids(1) := p_item_sourcing_info_rec.line_id(1);
602            IF PG_DEBUG in ('Y', 'C') THEN
603                msc_sch_wb.atp_debug('After Adding line id');
604            END IF;
605 
606        END IF;
607     END IF;
608     IF (p_organization_id IS NOT NULL) OR  (p_customer_id is NOT NULL) THEN
609        --s_cto_rearch
610        --IF p_ship_set_item.COUNT > 1  THEN
611        IF p_item_sourcing_info_rec.sr_inventory_item_id.count >1 THEN
612 
613 
614           --IF NVL(p_organization_id, -1) = -1 THEN
615           IF p_customer_site_id IS NOT NULL THEN
616           -- Bug 3515520, don't use org in case customer/site is populated.
617 
618 		          IF PG_DEBUG in ('Y', 'C') THEN
619                         msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in ship set and receiving party is customer');
620                   END IF;
621 
622                    -- rmehra Replaced views by procedure for bug 2585710
623                    MSC_ATP_PROC.Get_Sources_Info(order_sch_wb.debug_session_id, null, l_customer_id,
624                    p_customer_site_id, p_assign_set_id, l_count, x_atp_sources, l_return_status,
625                    p_partner_type, p_party_site_id, p_order_line_id ); --2814895
626 
627                    IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
628                         IF PG_DEBUG in ('Y', 'C') THEN
629                                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Error occured in procedure Get_Sources_Info');
630                         END IF;
631                         DELETE MSC_SHIP_SET_TEMP;
632                         x_return_status := FND_API.G_RET_STS_ERROR;
633                         return;
634                    END IF;
635 
636           --ELSE
637           ELSIF p_organization_id IS NOT NULL AND (p_customer_id is NULL AND p_customer_site_id IS NULL) THEN
638           -- Bug 3515520, don't use org in case customer/site is populated.
639 
640             IF PG_DEBUG in ('Y', 'C') THEN
641                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in ship set and receiving party is an inv org');
642             END IF;
643 
644 	   -- the cursor is different if it is ods or pds
645            --bug 3495773: Add nvl condition
646            IF nvl(p_plan_id, -1) <> -1 THEN
647             -- pds , use msc_item_sourcing table
648             IF PG_DEBUG in ('Y', 'C') THEN
649                msc_sch_wb.atp_debug('Atp_Sources: ' || 'inside ship set, p_plan_id <> -1');
650             END IF;
651 
652 	    -- Fix for bug 1454524, no need to join based on assignment set as only
653             -- 1 assignment set is supported for a plan.
654             -- Bug 2585710. Rewriting the dynamic SQL in static form. Using msc_ship_set_temp
655                 SELECT
656                         nvl(s.source_organization_id, -1),
657                         -- nvl(s.sr_instance_id2,-1),
658                         decode(nvl(min(s.source_type),
659                                    decode(min(s.source_organization_id),
660                                           to_number(null), 3,
661                                                            1)),
662                                3, p_instance_id,
663                                   nvl(s.sr_instance_id2,-1)),
664                         -- Bug 3270842 : For buy cases always select the passed instance id as source's
665                         --               instance id for buy sources
666                         -- Bug 3517529: For Buy cases if supplier_id and supplier_site_id are NULL, then
667                         --              they are changed to -99 to identify the Buy case, otherwise it will
668                         --              be identified as a Transfer case and ATP_Check will be called recursively.
669                         nvl(supplier_id,decode(source_type,MSC_ATP_PVT.BUY,-99,-1)),
670                         nvl(supplier_site_id,decode(source_type,MSC_ATP_PVT.BUY,-99,-1)),
671                         sum(nvl(s.rank, 0) + 1 - nvl(s.allocation_percent,0)/1000), --2910418
672                         nvl(min(s.source_type),
673                                 decode(min(s.source_organization_id),to_number(null), 3, 1)),
674                         0,
675                         NVL(MAX(s.avg_transit_lead_time), 0), -- dsting 2614883
676                         NVL(s.ship_method, '@@@'), -- For ship_rec_cal
677                         DECODE(mtps.shipping_control,'BUYER',1,2) -- For supplier intransit LT project - 1:Ship Cap, 2:Dock Cap
678                 BULK COLLECT INTO
679                          x_atp_sources.Organization_Id,
680                          x_atp_sources.Instance_Id,
681                          x_atp_sources.Supplier_Id,
682                          x_atp_sources.Supplier_Site_Id,
683                          x_atp_sources.Rank,
684                          x_atp_sources.Source_Type,
685                          x_atp_sources.Preferred,
686                          x_atp_sources.Lead_Time,
687                          x_atp_sources.Ship_Method,
688                          x_atp_sources.Sup_Cap_Type  -- For supplier intransit LT project
689                 FROM
690                         msc_item_sourcing s,
691                         msc_ship_set_temp msst,
692                         msc_trading_partner_sites mtps -- For supplier intransit LT project
693                 WHERE
694                         s.inventory_item_id = msst.inventory_item_id
695                         AND     s.organization_id = p_organization_id
696                         AND     s.sr_instance_id =  p_instance_id
697                         AND     s.plan_id =  p_plan_id
698                         AND     s.supplier_site_id = mtps.partner_site_id (+) -- For supplier intransit LT project
699                         --bug 3373166: Use assignmnet set for plan sourcing
700                         AND    NVL(s.assignment_set_type, 1) = 1
701                         -- Bug 3787821: Putting the Date check if recieving party is org
702                         AND     TRUNC(NVL(s.DISABLE_DATE,l_sysdate)) >= l_sysdate
703                         AND     TRUNC(s.EFFECTIVE_DATE) <= l_sysdate
704                         -- ATP4drp Circular sources applicable for DRP plans not supported by ATP.
705                         AND     NVL(s.circular_src, 2) <> 1
706 
707 
708                 GROUP BY
709                         s.source_organization_id,
710                         s.sr_instance_id2,
711                         s.supplier_id,
712                         s.supplier_site_id,
713                         DECODE(mtps.shipping_control,'BUYER',1,2) -- For supplier intransit LT project
714                 HAVING  count(*) = l_count
715                 ORDER BY 5;
716 
717            ELSE
718             -- ODS, use msc_sources_v
719             IF PG_DEBUG in ('Y', 'C') THEN
720                msc_sch_wb.atp_debug('Atp_Sources: ' || 'We are in ODS, using msc_sources_v for sources.');
721             END IF;
722 
723             -- fix 1460753: make at for global sourcing rule
724            -- Bug 2585710. Rewriting the dynamic SQL into static form.
725 
726             SELECT
727                     nvl(s.source_organization_id, p_organization_id),
728                     nvl(s.source_org_instance_id, -1),
729                     nvl(s.vendor_id, -1),
730                     nvl(s.vendor_site_id, -1),
731                     sum(nvl(s.rank, 0) + 1 - nvl(s.allocation_percent,0)/1000), --2910418
732                     nvl(min(s.source_type),
733                       decode(s.source_organization_id, to_number(null), 3, 1)),
734                     0,
735                     NVL(MAX(s.avg_transit_lead_time), 0), -- dsting 2614883
736                     '@@@',
737                     NULL -- For supplier intransit LT project
738            BULK COLLECT INTO
739                     x_atp_sources.Organization_Id,
740                     x_atp_sources.Instance_Id,
741                     x_atp_sources.Supplier_Id,
742                     x_atp_sources.Supplier_Site_Id,
743                     x_atp_sources.Rank,
744                     x_atp_sources.Source_Type,
745                     x_atp_sources.Preferred,
746                     x_atp_sources.Lead_Time,
747                     x_atp_sources.Ship_Method,
748                     x_atp_sources.Sup_Cap_Type  -- For supplier intransit LT project
749             FROM    msc_sources_v s,
750                     msc_ship_set_temp msst
751             WHERE   s.inventory_item_id = msst.inventory_item_id
752             AND     s.organization_id = p_organization_id
753             AND     s.sr_instance_id = p_instance_id
754             AND     s.assignment_set_id = p_assign_set_id
755             -- Bug 3787821: Putting the Date check if recieving party is org
756             AND     TRUNC(NVL(s.DISABLE_DATE,l_sysdate)) >= l_sysdate
757             AND     TRUNC(s.EFFECTIVE_DATE) <= l_sysdate
758             AND     NVL(s.source_organization_id,
759                         decode(s.source_type, 2, p_organization_id, -1)) <> -1
760             GROUP BY s.source_organization_id,
761                      s.source_org_instance_id,
762                      s.vendor_id,
763                      s.vendor_site_id
764             HAVING count(*) = l_count
765             ORDER BY 5;
766 
767            END IF; -- end if the ods/pds
768 
769           END IF;
770 
771           IF x_atp_sources.Rank.COUNT > 0 THEN
772             l_preferred_rank := x_atp_sources.Rank(1);
773             i := 1;
774             WHILE i IS NOT NULL LOOP
775               IF x_atp_sources.Rank(i) = l_preferred_rank THEN
776                   x_atp_sources.Preferred(i) := 1;
777               ELSE
778                   EXIT;
779               END IF;
780 
781               i:= x_atp_sources.Rank.NEXT(i);
782             END LOOP;
783           END IF;
784 
785        --ELSIF NVL(p_organization_id, -1) = -1 THEN
786        ELSIF p_customer_site_id IS NOT NULL THEN
787        -- Bug 3515520, don't use org in case customer/site is populated.
788 
789           -- we are in single line, receiving party is customer
790           IF PG_DEBUG in ('Y', 'C') THEN
791                   msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in single line, receiving party is customer');
792                   msc_sch_wb.atp_debug('Atp_Sources: ' || 'Customer Specific Sourcing');
793           END IF;
794 
795           -- savirine, Sep 11, 2001: added region_id and source_organization_id in the where clause and
796           -- added aliases in the select statement.
797 
798            -- ngoel 9/26/2001, since view msc_scatp_sources_v resolves the hierarchy,
799 	   -- no need to have either union all or separate clause for customer site and region
800 
801 	  -- bug 2585710. Replaced views by procedure
802            MSC_ATP_PROC.Get_Sources_Info(order_sch_wb.debug_session_id, l_inv_item_id, l_customer_id,
803            p_customer_site_id, p_assign_set_id, null, x_atp_sources, l_return_status,
804            p_partner_type, p_party_site_id, p_order_line_id ); --2814895
805 
806            IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
807                 IF PG_DEBUG in ('Y', 'C') THEN
808                    msc_sch_wb.atp_debug('Atp_Sources: ' || 'Procedure Get_Sources_Info could not find any sources');
809                 END IF;
810                 x_return_status := FND_API.G_RET_STS_ERROR;
811                 return;
812             END IF;
813 
814 
815           IF x_atp_sources.Rank.COUNT > 0 THEN
816             IF PG_DEBUG in ('Y', 'C') THEN
817                msc_sch_wb.atp_debug('Atp_Sources: ' || 'get some records');
818             END IF;
819             l_preferred_rank := x_atp_sources.Rank(1);
820             i := 1;
821             WHILE i IS NOT NULL LOOP
822               IF x_atp_sources.Rank(i) = l_preferred_rank THEN
823                   x_atp_sources.Preferred(i) := 1;
824               ELSE
825                   EXIT;
826               END IF;
827 
828               i:= x_atp_sources.Rank.NEXT(i);
829             END LOOP;
830           END IF;
831 
832        --ELSE
833        ELSIF p_organization_id IS NOT NULL AND (p_customer_id is NULL AND p_customer_site_id IS NULL) THEN
834        -- Bug 3515520, don't use org in case customer/site is populated.
835 
836           -- we are in single line and receiving party is an inv org.
837 
838           IF PG_DEBUG in ('Y', 'C') THEN
839             msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in single line and receiving party is an inv org.');
840 
841              msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_assign_set_id'||p_assign_set_id);
842              msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_inventory_item_id'||p_inventory_item_id);
843              msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_organization_id'||p_organization_id);
844              msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_instance_id'||p_instance_id);
845              msc_sch_wb.atp_debug('Atp_Sources: ' || 'p_plan_id'||p_plan_id);
846           END IF;
847 
848           -- Bug 1542439, since planning is now carrying the sourcing for models
849           -- removing the wrokaround and look into the PDS data rather than ODS
850           -- data when the models are sourced.
851 
852           -- Modified on 11/06/2000 by ngoel. In case of multi-level, multi-org CTO
853           -- we need to pick the sources from msc_sources_v instead of msc_item_sourcing
854           -- since planning doesn't honor souring rules for models.
855 
856           -- Bug 1562754, use G_ASSEMBLY_LINE_ID instead of G_COMP_LINE_ID, to make sure that
857           -- in case of CTO, we try to get the BOM correctly from msc_bom_temp_table.-NGOEL 02/01/2001
858 
859    --       IF p_plan_id <> -1 AND l_cto_bom = 0 THEN
860            --bug 3495773: Add nvl condition
861           IF nvl(p_plan_id, -1) <> -1 THEN
862             IF PG_DEBUG in ('Y', 'C') THEN
863                msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in pds, using msc_item_sourcing');
864             END IF;
865             -- we are in pds, using msc_item_sourcing
866 
867             -- Fix for bug 1454524, no need to join based on assignment set as only 1 assignment
868             -- set is supported for a plan.
869             SELECT  nvl(s.source_organization_id, -1),
870                     decode(decode(s.organization_id,
871                                   s.source_organization_id, 2,
872                                   nvl(s.source_type,
873                                       decode(source_organization_id,
874                                              to_number(null), 3, 1))),
875                            3, p_instance_id,
876                               nvl(sr_instance_id2, -1)),
877                     -- Bug 3270842 : For buy cases always select the passed instance id as source's
878                     --               instance id for buy sources
879                     -- Bug 3517529: For Buy cases if supplier_id and supplier_site_id are NULL, then
880                     --              they are changed to -99 to identify the Buy case, otherwise it will
881                     --              be identified as a Transfer case and ATP_Check will be called recursively.
882                     nvl(supplier_id,decode(source_type,MSC_ATP_PVT.BUY,-99,-1)),
883                     nvl(supplier_site_id,decode(source_type,MSC_ATP_PVT.BUY,-99,-1)),
884                     nvl(s.rank, -1),
885                     -- 2936920. treat as a make if org/src org are the same
886                     decode(s.organization_id, s.source_organization_id, 2,
887                     nvl(s.source_type,
888                     decode(source_organization_id, to_number(null), 3, 1))),
889                     0,
890                     NVL(s.avg_transit_lead_time, 0), -- dsting 2614883
891                     NVL(s.ship_method, '@@@'),
892                     DECODE(mtps.shipping_control,'BUYER',1,2) -- For supplier intransit LT project - 1:Ship Cap, 2:Dock Cap
893             BULK COLLECT INTO
894                     x_atp_sources.Organization_Id,
895                     x_atp_sources.Instance_Id,
896                     x_atp_sources.Supplier_Id,
897                     x_atp_sources.Supplier_Site_Id,
898                     x_atp_sources.Rank,
899                     x_atp_sources.Source_Type,
900                     x_atp_sources.Preferred,
901                     x_atp_sources.Lead_Time,
902                     x_atp_sources.Ship_Method,
903                     x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
904             FROM    msc_item_sourcing s,
905                     msc_trading_partner_sites mtps -- For supplier intransit LT project
906             WHERE   s.inventory_item_id = l_inv_item_id
907             AND     s.organization_id = p_organization_id
908             AND     s.sr_instance_id = p_instance_id
909             /*AND     s.assignment_set_id = p_assign_set_id*/
910             AND     s.plan_id =  p_plan_id
911             AND     s.supplier_site_id = mtps.partner_site_id (+) -- For supplier intransit LT project
912             --bug 3373166: Use assignmnet set for plan sourcing
913             AND    NVL(s.assignment_set_type, 1) = 1
914             -- Bug 3787821: Putting the Date check if recieving party is org
915             AND     TRUNC(NVL(s.DISABLE_DATE,l_sysdate)) >= l_sysdate
916             AND     TRUNC(s.EFFECTIVE_DATE) <= l_sysdate
917                      -- ATP4drp Circular sources applicable for DRP plans not supported by ATP.
918             AND     NVL(s.circular_src, 2) <> 1
919             ORDER BY rank asc, allocation_percent desc;
920           ELSE
921 
922           IF PG_DEBUG in ('Y', 'C') THEN
923                msc_sch_wb.atp_debug('Atp_Sources: ' || 'we are in ods, using msc_sources_v');
924             END IF;
925             -- we are in ods
926             -- we are in ods or this is for multi-level, multi-org CTO, using msc_sources_v
927 
928             SELECT  nvl(s.source_organization_id, p_organization_id), -- 1460753
929                     nvl(s.source_org_instance_id, -1),
930                     nvl(s.vendor_id, -1),
931                     nvl(s.vendor_site_id, -1),
932                     nvl(s.rank, -1),
933                     nvl(s.source_type,
934                       decode(source_organization_id, to_number(null), 3, 1)),
935                     0,
936                     NVL(s.avg_transit_lead_time, -1),
937                     NVL(s.ship_method, '@@@'),
938                     NULL -- For supplier intransit LT project
939             BULK COLLECT INTO
940                     x_atp_sources.Organization_Id,
941                     x_atp_sources.Instance_Id,
942                     x_atp_sources.Supplier_Id,
943                     x_atp_sources.Supplier_Site_Id,
944                     x_atp_sources.Rank,
945                     x_atp_sources.Source_Type,
946                     x_atp_sources.Preferred,
947                     x_atp_sources.Lead_Time,
948                     x_atp_sources.Ship_Method,
949                     x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
950             FROM    msc_sources_v s
951             WHERE   s.inventory_item_id = l_inv_item_id
952             AND     s.organization_id = p_organization_id
953             AND     s.sr_instance_id = p_instance_id
954             AND     s.assignment_set_id = p_assign_set_id
955             -- Bug 3787821: Putting the Date check if recieving party is org
956             AND     TRUNC(NVL(s.DISABLE_DATE,l_sysdate)) >= l_sysdate
957             AND     TRUNC(s.EFFECTIVE_DATE) <= l_sysdate
958                     -- bug 1460753
959             AND     NVL(s.source_organization_id,
960                     decode(s.source_type,MSC_ATP_PVT.MAKE,p_organization_id,-1)) <> -1
961             ORDER BY rank asc, allocation_percent desc;
962           END IF;
963 
964           IF x_atp_sources.Rank.COUNT > 0 THEN
965             l_preferred_rank := x_atp_sources.Rank(1);
966             i := 1;
967             WHILE i IS NOT NULL LOOP
968               IF x_atp_sources.Rank(i) = l_preferred_rank THEN
969                   x_atp_sources.Preferred(i) := 1;
970               ELSE
971                   EXIT;
972               END IF;
973 
974               i:= x_atp_sources.Rank.NEXT(i);
975             END LOOP;
976           END IF;
977 
978        END IF;
979     END IF;  ---  IF (p_organization_id IS NULL) AND (p_customer_id is NULL)
980 
981     IF PG_DEBUG in ('Y', 'C') THEN
982        msc_sch_wb.atp_debug('Atp_Sources: ' || 'Sources count = '|| x_atp_sources.organization_id.count);
983     END IF;
984       --bug 3495773: Add nvl condition
985     IF (x_atp_sources.organization_id.count = 0) and (nvl(p_plan_id, -1) = -1) then
986        IF PG_DEBUG in ('Y', 'C') THEN
987           msc_sch_wb.atp_debug('Atp_Sources: ' || 'No sources found so far');
988        END IF;
989        --s_cto_rearch
990        --IF  p_ship_set_item.COUNT > 1 THEN
991        IF p_item_sourcing_info_rec.sr_inventory_item_id.count > 1 THEN
992        --e_cto_rearch
993 
994           IF PG_DEBUG in ('Y', 'C') THEN
995              msc_sch_wb.atp_debug('Atp_Sources: ' || 'ship_count >1 ');
996           END IF;
997           -- BUG 2283260 Join to msc_system_items to only obtain sources
998           -- which are valid for the item(s).
999 
1000 	  -- Rewriting the dynamic SQL into static one for bug 2585710.
1001 
1002         SELECT
1003                 NVL(SOURCEORG.SOURCE_ORGANIZATION_ID, -1),
1004                 NVL(SOURCEORG.SOURCE_ORG_INSTANCE_ID,-1),
1005                 NVL(SOURCEORG.SOURCE_PARTNER_ID, -1),
1006                 NVL(SOURCEORG.SOURCE_PARTNER_SITE_ID, -1),
1007                 SUM(NVL(SOURCEORG.RANK, 0)),
1008                 nvl(min(sourceorg.source_type),
1009                 decode(sourceorg.source_organization_id,
1010                        to_number(null), 3, 1)),
1011                 0,
1012                 -1,
1013                 '@@@',
1014                 NULL -- For supplier intransit LT project
1015         BULK COLLECT INTO
1016                 x_atp_sources.Organization_Id,
1017                 x_atp_sources.Instance_Id,
1018                 x_atp_sources.Supplier_Id,
1019                 x_atp_sources.Supplier_Site_Id,
1020                 x_atp_sources.Rank,
1021                 x_atp_sources.Source_Type,
1022                 x_atp_sources.Preferred,
1023                 x_atp_sources.Lead_Time,
1024                 x_atp_sources.Ship_Method,
1025                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1026         FROM
1027                 msc_sourcing_rules msr,
1028                 msc_sr_receipt_org receiptorg,
1029                 msc_sr_source_org sourceorg,
1030                 msc_sr_assignments msa,
1031                 msc_system_items msi,
1032                 msc_ship_set_temp msst
1033         WHERE
1034                 msa.assignment_type = 3
1035                 AND    msa.assignment_set_id = p_assign_set_id
1036                 AND    msa.inventory_item_id = msst.inventory_item_id
1037                 AND    msa.sourcing_rule_id = msr.sourcing_rule_id
1038                 AND    msr.status = 1
1039                 AND    msr.sourcing_rule_type = 1
1040                 AND    msr.sourcing_rule_id = receiptorg.sourcing_rule_id
1041                 -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1042                 AND    TRUNC(NVL(receiptorg.disable_date, l_sysdate)) >=  l_sysdate
1043                 AND    TRUNC(receiptorg.effective_date) <= l_sysdate
1044                 AND    receiptorg.sr_receipt_id = sourceorg.sr_receipt_id
1045                 AND    sourceorg.sr_instance_id = msi.sr_instance_id
1046                 AND    sourceorg.source_organization_id = msi.organization_id
1047                 -- ATP4drp Circular sources not supported by ATP.
1048                 --AND    NVL(sourceorg.circular_src, 2) <> 1
1049                 --Bug4567833
1050                 AND     NVL(sourceorg.circular_src, 'N') <> 'Y'
1051                 AND    msa.inventory_item_id = msi.inventory_item_id
1052                 AND    msi.plan_id = -1
1053         GROUP  BY
1054                 SOURCEORG.SOURCE_ORGANIZATION_ID,
1055                 SOURCEORG.SOURCE_ORG_INSTANCE_ID,
1056                 SOURCEORG.SOURCE_PARTNER_ID,
1057                 SOURCEORG.SOURCE_PARTNER_SITE_ID
1058                 HAVING count(*) = l_count
1059         ORDER  BY 5;
1060 
1061 
1062 	  IF (x_atp_sources.organization_id.count = 0) THEN
1063              IF PG_DEBUG in ('Y', 'C') THEN
1064 		msc_sch_wb.atp_debug('Atp_Sources: ' || 'Lookup for item level assignment failed.');
1065 		msc_sch_wb.atp_debug('Atp_Sources: ' || 'Check on item_category level.');
1066 	      END IF;
1067 	      -- Rewriting the dynamic SQL into a static SQL. Refer to bug 2585710 for details.
1068                 SELECT
1069                         NVL(SOURCEORG.SOURCE_ORGANIZATION_ID, -1),
1070                         NVL(SOURCEORG.SOURCE_ORG_INSTANCE_ID,-1),
1071                         NVL(SOURCEORG.SOURCE_PARTNER_ID, -1),
1072                         NVL(SOURCEORG.SOURCE_PARTNER_SITE_ID, -1),
1073                         SUM(NVL(SOURCEORG.RANK, 0)),
1074                         nvl(min(sourceorg.source_type),
1075                               decode(sourceorg.source_organization_id,
1076                                       to_number(null), 3, 1)),
1077                         0,
1078                         -1,
1079                         '@@@',
1080                         NULL -- For supplier intransit LT project
1081                 BULK COLLECT INTO
1082                         x_atp_sources.Organization_Id,
1083                         x_atp_sources.Instance_Id,
1084                         x_atp_sources.Supplier_Id,
1085                         x_atp_sources.Supplier_Site_Id,
1086                         x_atp_sources.Rank,
1087                         x_atp_sources.Source_Type,
1088                         x_atp_sources.Preferred,
1089                         x_atp_sources.Lead_Time,
1090                         x_atp_sources.Ship_Method,
1091                         x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1092                  FROM   msc_sourcing_rules msr,
1093                         msc_sr_receipt_org receiptorg,
1094                         msc_sr_source_org sourceorg,
1095                         msc_sr_assignments msa,
1096                         msc_item_categories cat,
1097                         msc_ship_set_temp msst
1098                  WHERE  msa.assignment_type = 2 and
1099                         msa.assignment_set_id = p_assign_set_id and
1100                         msa.inventory_item_id = msst.inventory_item_id
1101                  AND    msa.sourcing_rule_id = msr.sourcing_rule_id
1102                  AND    msr.status = 1
1103                  AND    msr.sourcing_rule_type = 1
1104                  AND    msr.sourcing_rule_id = receiptorg.sourcing_rule_id
1105                  -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1106                  AND    TRUNC(NVL(receiptorg.disable_date, l_sysdate)) >= l_sysdate
1107                  AND    TRUNC(receiptorg.effective_date) <= l_sysdate
1108                  AND    receiptorg.sr_receipt_id = sourceorg.sr_receipt_id
1109                  AND    msa.category_name = cat.category_name
1110                  AND    msa.category_set_id = cat.category_set_id
1111                  AND    msa.inventory_item_id = cat.inventory_item_id
1112                  AND    sourceorg.source_organization_id = cat.organization_id
1113                  AND    sourceorg.sr_instance_id = cat.sr_instance_id
1114                  -- ATP4drp Circular sources not supported by ATP.
1115                  --AND    NVL(sourceorg.circular_src, 2) <> 1
1116                  --Bug4567833
1117                  AND     NVL(sourceorg.circular_src, 'N') <> 'Y'
1118                  GROUP  BY SOURCEORG.SOURCE_ORGANIZATION_ID,
1119                            SOURCEORG.SOURCE_ORG_INSTANCE_ID,
1120                            SOURCEORG.SOURCE_PARTNER_ID,
1121                            SOURCEORG.SOURCE_PARTNER_SITE_ID
1122                  HAVING count(*) = l_count
1123                  ORDER  BY 5;
1124 
1125           END IF;
1126 
1127 	  IF (x_atp_sources.organization_id.count = 0) AND (p_organization_id is not null) AND
1128          (p_customer_id is NULL AND p_customer_site_id IS NULL) THEN
1129       -- Bug 3515520, don't use org in case customer/site is populated.
1130              IF PG_DEBUG in ('Y', 'C') THEN
1131                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Lookup for item-cat assignment failed.');
1132                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Check on global-BOD level.');
1133              END IF;
1134              -- BUG 2283260 Join to msc_system_items to only obtain sources
1135              -- which are valid for the item(s).
1136              l_organization_id := p_organization_id; -- local var for testing
1137              IF PG_DEBUG in ('Y', 'C') THEN
1138                 msc_sch_wb.atp_debug('Atp_Sources: ' || ' Org Id := ' || l_organization_id);
1139              END IF;
1140              -- Rewriting the SQL for bug 2585710.
1141                 SELECT
1142                         NVL(SOURCEORG.SOURCE_ORGANIZATION_ID, -1),
1143                         NVL(SOURCEORG.SOURCE_ORG_INSTANCE_ID,-1),
1144                         NVL(SOURCEORG.SOURCE_PARTNER_ID, -1),
1145                         NVL(SOURCEORG.SOURCE_PARTNER_SITE_ID, -1),
1146                         NVL(SOURCEORG.RANK, 0),
1147                         nvl(sourceorg.source_type,
1148                          decode(sourceorg.source_organization_id,
1149                          to_number(null), 3, 1)),
1150                         0,
1151                         -1,
1152                         '@@@',
1153                         NULL -- For supplier intransit LT project
1154                 BULK COLLECT INTO
1155                         x_atp_sources.Organization_Id,
1156                         x_atp_sources.Instance_Id,
1157                         x_atp_sources.Supplier_Id,
1158                         x_atp_sources.Supplier_Site_Id,
1159                         x_atp_sources.Rank,
1160                         x_atp_sources.Source_Type,
1161                         x_atp_sources.Preferred,
1162                         x_atp_sources.Lead_Time,
1163                         x_atp_sources.Ship_Method,
1164                         x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1165                 FROM
1166                         msc_sourcing_rules msr,
1167                         msc_sr_receipt_org receiptorg,
1168                         msc_sr_source_org sourceorg,
1169                         msc_sr_assignments msa,
1170                         msc_system_items msi,
1171                         msc_ship_set_temp msst
1172                 WHERE
1173                         msa.assignment_type = 1
1174                         AND    msa.assignment_set_id = p_assign_set_id
1175                         AND    msa.sourcing_rule_id = msr.sourcing_rule_id
1176                         AND    msr.status = 1
1177                         AND    msr.sourcing_rule_type = 2
1178                         AND    msr.sourcing_rule_id = receiptorg.sourcing_rule_id
1179                         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1180                         AND    TRUNC(NVL(receiptorg.disable_date, l_sysdate)) >= l_sysdate
1181                         AND    TRUNC(receiptorg.effective_date) <= l_sysdate
1182                         AND    receiptorg.sr_receipt_org = l_organization_id
1183                         AND    receiptorg.receipt_org_instance_id = p_instance_id
1184                         AND    receiptorg.sr_receipt_id = sourceorg.sr_receipt_id
1185                         AND    sourceorg.source_organization_id = msi.ORGANIZATION_ID
1186                         AND    sourceorg.sr_instance_id = msi.sr_instance_id
1187                         -- ATP4drp Circular sources not supported by ATP.
1188                         --AND    NVL(sourceorg.circular_src, 2) <> 1
1189                         --Bug4567833
1190                         AND     NVL(sourceorg.circular_src, 'N') <> 'Y'
1191                         AND    msi.inventory_item_id = msst.inventory_item_id
1192                         AND    msi.plan_id = -1
1193                 ORDER  BY rank asc, allocation_percent desc;
1194 
1195           END IF;
1196           IF (x_atp_sources.organization_id.count = 0) THEN
1197              IF PG_DEBUG in ('Y', 'C') THEN
1198                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'Lookup for GLOBAL-BOD assignment failed.');
1199 		msc_sch_wb.atp_debug('Atp_Sources: ' || 'Check on global level');
1200              END IF;
1201              -- BUG 2283260 Join to msc_system_items to only obtain sources
1202              -- which are valid for the item(s).
1203 
1204                 -- Rewriting the SQL into a static one for bug 2585710.
1205                 SELECT
1206                         NVL(SOURCEORG.SOURCE_ORGANIZATION_ID, -1),
1207                         NVL(SOURCEORG.SOURCE_ORG_INSTANCE_ID,-1),
1208                         NVL(SOURCEORG.SOURCE_PARTNER_ID, -1),
1209                         NVL(SOURCEORG.SOURCE_PARTNER_SITE_ID, -1),
1210                         NVL(SOURCEORG.RANK, 0),
1211                         nvl(sourceorg.source_type,
1212                           decode(sourceorg.source_organization_id,
1213                                                to_number(null), 3, 1)),
1214                         0,
1215                         -1,
1216                         '@@@',
1217                         NULL -- For supplier intransit LT project
1218                 BULK COLLECT INTO
1219                         x_atp_sources.Organization_Id,
1220                         x_atp_sources.Instance_Id,
1221                         x_atp_sources.Supplier_Id,
1222                         x_atp_sources.Supplier_Site_Id,
1223                         x_atp_sources.Rank,
1224                         x_atp_sources.Source_Type,
1225                         x_atp_sources.Preferred,
1226                         x_atp_sources.Lead_Time,
1227                         x_atp_sources.Ship_Method,
1228                         x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1229                 FROM
1230                         msc_sourcing_rules msr,
1231                         msc_sr_receipt_org receiptorg,
1232                         msc_sr_source_org sourceorg,
1233                         msc_sr_assignments msa,
1234                         msc_system_items msi,
1235                         msc_ship_set_temp msst
1236                 WHERE
1237                         msa.assignment_type = 1
1238                         AND    msa.assignment_set_id = p_assign_set_id
1239                         AND    msa.sourcing_rule_id = msr.sourcing_rule_id
1240                         AND    msr.status = 1
1241                         AND    msr.sourcing_rule_type = 1
1242                         AND    msr.sourcing_rule_id = receiptorg.sourcing_rule_id
1243                         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1244                         AND    TRUNC(NVL(receiptorg.disable_date, l_sysdate)) >= l_sysdate
1245                         AND    TRUNC(receiptorg.effective_date) <= l_sysdate
1246                         AND    receiptorg.sr_receipt_id = sourceorg.sr_receipt_id
1247                         AND    sourceorg.source_organization_id = msi.ORGANIZATION_ID
1248                         AND    sourceorg.sr_instance_id = msi.sr_instance_id
1249                         -- ATP4drp Circular sources not supported by ATP.
1250                         --AND    NVL(sourceorg.circular_src, 2) <> 1
1251                         --Bug4567833
1252                         AND     NVL(sourceorg.circular_src, 'N') <> 'Y'
1253                         AND    msi.inventory_item_id = msst.inventory_item_id
1254                         AND    msi.plan_id = -1
1255                 ORDER  BY rank asc, allocation_percent desc;
1256           END IF;
1257 
1258        ELSE
1259           IF PG_DEBUG in ('Y', 'C') THEN
1260              msc_sch_wb.atp_debug('Atp_Sources: ' || 'at item level');
1261           msc_sch_wb.atp_debug('Atp_Sources: ' || 'ITEM Inv item Id := ' || l_inv_item_id);
1262        END IF;
1263              -- BUG 2283260 Join to msc_system_items to only obtain sources
1264              -- which are valid for the item(s).
1265           SELECT NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
1266                  NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,-1),
1267        		 NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
1268        	       	 NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
1269                  NVL(SOURCE_ORG.RANK, 0),
1270                  nvl(source_org.source_type,
1271                    decode(source_org.source_organization_id, to_number(null), 3, 1)),
1272                  0,
1273                  -1,
1274                  'XYZ',
1275                  NULL -- For supplier intransit LT project
1276           BULK COLLECT INTO
1277                  x_atp_sources.Organization_Id,
1278                  x_atp_sources.Instance_Id,
1279                  x_atp_sources.Supplier_Id,
1280                  x_atp_sources.Supplier_Site_Id,
1281                  x_atp_sources.Rank,
1282                  x_atp_sources.Source_Type,
1283                  x_atp_sources.Preferred,
1284                  x_atp_sources.Lead_Time,
1285                  x_atp_sources.Ship_Method,
1286                  x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1287 
1288           FROM    msc_sourcing_rules msr,
1289                  msc_sr_receipt_org receipt_org,
1290                  msc_sr_source_org source_org,
1291                  msc_sr_assignments msa,
1292                  msc_system_items msi
1293           WHERE   msa.assignment_type = 3 and
1294                  msa.assignment_set_id = p_assign_set_id and
1295                  msa.inventory_item_id = l_inv_item_id and
1296                  msa.sourcing_rule_id = msr.sourcing_rule_id and
1297                  msr.status = 1 and
1298                  msr.sourcing_rule_type = 1 and
1299                  msr.sourcing_rule_id = receipt_org.sourcing_rule_id and
1300                  -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1301                  TRUNC(NVL(receipt_org.disable_date, l_sysdate )) >= l_sysdate and
1302                  TRUNC(receipt_org.effective_date) <= l_sysdate and
1303                  receipt_org.sr_receipt_id = source_org.sr_receipt_id and
1304                  source_org.sr_instance_id = msi.sr_instance_id and
1305                  source_org.source_organization_id = msi.ORGANIZATION_ID and
1306                  -- ATP4drp Circular sources not supported by ATP.
1307                  --NVL(source_org.circular_src, 2) <> 1  AND
1308                  --Bug4567833
1309                  NVL(source_org.circular_src, 'N') <> 'Y' AND
1310                  msa.inventory_item_id = msi.inventory_item_id and
1311                  msi.plan_id = -1
1312                  ORDER BY rank asc, allocation_percent desc;
1313           -- BUG 2283260 Note that join happens between msc_system_items(msi)
1314           -- and msc_sr_source_org (src) for org, instance as in certain cases
1315           -- join with msc_sr_assignments (msa) will bring no rows.
1316           -- Filter on item in msc_system_items.
1317 
1318           IF PG_DEBUG in ('Y', 'C') THEN
1319              msc_sch_wb.atp_debug('Count after item level = '||x_atp_sources.organization_id.count);
1320           END IF;
1321           IF (x_atp_sources.organization_id.count = 0) then
1322              IF PG_DEBUG in ('Y', 'C') THEN
1323                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'no sources on item level. look into item-cat level');
1324           msc_sch_wb.atp_debug('Atp_Sources: ' || ' ITEM CAT Inv item Id := ' || l_inv_item_id);
1325        END IF;
1326              SELECT NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
1327                     NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,-1),
1328                     NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
1329                     NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
1330                     NVL(SOURCE_ORG.RANK, 0),
1331                     nvl(source_org.source_type,
1332                       decode(source_org.source_organization_id, to_number(null), 3, 1)),
1333                     0,
1334                     -1,
1335                     'XYZ',
1336                     NULL -- For supplier intransit LT project
1337              BULK COLLECT INTO
1338                     x_atp_sources.Organization_Id,
1339                     x_atp_sources.Instance_Id,
1340                     x_atp_sources.Supplier_Id,
1341                     x_atp_sources.Supplier_Site_Id,
1342                     x_atp_sources.Rank,
1343                     x_atp_sources.Source_Type,
1344                     x_atp_sources.Preferred,
1345                     x_atp_sources.Lead_Time,
1346                     x_atp_sources.Ship_Method,
1347                     x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1348             FROM    msc_sourcing_rules msr,
1349                     msc_sr_receipt_org receipt_org,
1350                     msc_sr_source_org source_org,
1351                     msc_sr_assignments msa,
1352                     msc_item_categories  cat
1353             WHERE   msa.assignment_type = 2 and
1354                     msa.assignment_set_id = p_assign_set_id and
1355                     msa.inventory_item_id = l_inv_item_id and
1356                     msa.sourcing_rule_id = msr.sourcing_rule_id and
1357                     msr.status = 1 and
1358                     msr.sourcing_rule_type = 1 and
1359                     msr.sourcing_rule_id = receipt_org.sourcing_rule_id and
1360                     -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1361                     TRUNC(NVL(receipt_org.disable_date, l_sysdate)) >= l_sysdate and
1362                     TRUNC(receipt_org.effective_date) <= l_sysdate and
1363                     receipt_org.sr_receipt_id = source_org.sr_receipt_id and
1364                     msa.category_name = cat.category_name and
1365                     msa.category_set_id = cat.category_set_id and
1366                     msa.inventory_item_id = cat.inventory_item_id and
1367                     source_org.source_organization_id = cat.organization_id and
1368                     source_org.sr_instance_id = cat.sr_instance_id and
1369                     -- ATP4drp Circular sources not supported by ATP.
1370             --AND     NVL(source_org.circular_src, 2) <> 1
1371                     --Bug4567833
1372                     NVL(source_org.circular_src, 'N') <> 'Y'
1373                     ORDER BY rank asc, allocation_percent desc;
1374           -- BUG 2283260 Additional join between msc_sr_source_org snd
1375           -- msc_item_categories to ensure that the source is valid for item.
1376           END IF;
1377 
1378           IF (x_atp_sources.organization_id.count = 0) and (p_organization_id is not null) then
1379              IF PG_DEBUG in ('Y', 'C') THEN
1380                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'no sources on item-cat level. look at global-BOD level');
1381              END IF;
1382              l_organization_id := p_organization_id; -- local var for testing
1383              IF PG_DEBUG in ('Y', 'C') THEN
1384                 msc_sch_wb.atp_debug('Atp_Sources: ' || ' Org Id := ' || l_organization_id);
1385              END IF;
1386 
1387              -- BUG 2283260 Join to msc_system_items to only obtain sources
1388              -- which are valid for the item(s).
1389              SELECT NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
1390                     NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,-1),
1391                     NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
1392                     NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
1393                     NVL(SOURCE_ORG.RANK, 0),
1394                     nvl(source_org.source_type,
1395                       decode(source_org.source_organization_id, to_number(null), 3, 1)),
1396                     0,
1397                     -1,
1398                     'XYZ',
1399                     NULL -- For supplier intransit LT project
1400              BULK COLLECT INTO
1401                     x_atp_sources.Organization_Id,
1402                     x_atp_sources.Instance_Id,
1403                     x_atp_sources.Supplier_Id,
1404                     x_atp_sources.Supplier_Site_Id,
1405                     x_atp_sources.Rank,
1406                     x_atp_sources.Source_Type,
1407                     x_atp_sources.Preferred,
1408                     x_atp_sources.Lead_Time,
1409                     x_atp_sources.Ship_Method,
1410                     x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1411             FROM    msc_sourcing_rules msr,
1412                     msc_sr_receipt_org receipt_org,
1413                     msc_sr_source_org source_org,
1414                     msc_sr_assignments msa,
1415                     msc_system_items msi
1416             WHERE   msa.assignment_type = 1 and
1417                     msa.assignment_set_id = p_assign_set_id and
1418                     ---msa.inventory_item_id = l_inv_item_id and
1419                     msa.sourcing_rule_id = msr.sourcing_rule_id and
1420                     msr.status = 1 and
1421                     msr.sourcing_rule_type = 2 and
1422                     msr.sourcing_rule_id = receipt_org.sourcing_rule_id and
1423                     -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1424                     TRUNC(NVL(receipt_org.disable_date, l_sysdate)) >= l_sysdate and
1425                     TRUNC(receipt_org.effective_date) <= l_sysdate and
1426                     receipt_org.SR_RECEIPT_ORG = l_organization_id and
1427                     receipt_org.RECEIPT_ORG_INSTANCE_ID = p_instance_id and
1428                     receipt_org.sr_receipt_id = source_org.sr_receipt_id and
1429                     source_org.source_organization_id = msi.ORGANIZATION_ID and
1430                     source_org.sr_instance_id = msi.sr_instance_id and
1431                     -- ATP4drp Circular sources not supported by ATP.
1432                     --NVL(source_org.circular_src, 2) <> 1 AND
1433                     --Bug4567833
1434                     NVL(source_org.circular_src, 'N') <> 'Y' AND
1435                     msi.inventory_item_id = l_inv_item_id and
1436                     msi.plan_id = -1
1437                     ORDER BY rank asc, allocation_percent desc;
1438           -- BUG 2283260 Note that join happens between msc_system_items(msi)
1439           -- and msc_sr_source_org (src) for org, instance as in certain cases
1440           -- join with msc_sr_assignments (msa) will bring no rows.
1441           -- Filter on item in msc_system_items.
1442           END IF;
1443 
1444           IF (x_atp_sources.organization_id.count = 0) then
1445              IF PG_DEBUG in ('Y', 'C') THEN
1446                 msc_sch_wb.atp_debug('Atp_Sources: ' || 'no sources on global_bod level. look at global level');
1447              END IF;
1448 
1449              -- BUG 2283260 Join to msc_system_items to only obtain sources
1450              -- which are valid for the item(s).
1451              SELECT NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
1452                     NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,-1),
1453                     NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
1454                     NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
1455                     NVL(SOURCE_ORG.RANK, 0),
1456                     nvl(source_org.source_type,
1457                       decode(source_org.source_organization_id, to_number(null), 3, 1)),
1458                     0,
1459                     -1,
1460                     'XYZ',
1461                     NULL -- For supplier intransit LT project
1462              BULK COLLECT INTO
1463                     x_atp_sources.Organization_Id,
1464                     x_atp_sources.Instance_Id,
1465                     x_atp_sources.Supplier_Id,
1466                     x_atp_sources.Supplier_Site_Id,
1467                     x_atp_sources.Rank,
1468                     x_atp_sources.Source_Type,
1469                     x_atp_sources.Preferred,
1470                     x_atp_sources.Lead_Time,
1471                     x_atp_sources.Ship_Method,
1472                     x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
1473             FROM    msc_sourcing_rules msr,
1474                     msc_sr_receipt_org receipt_org,
1475                     msc_sr_source_org source_org,
1476                     msc_sr_assignments msa,
1477                     msc_system_items msi
1478             WHERE   msa.assignment_type = 1 and
1479                     msa.assignment_set_id = p_assign_set_id and
1480                     ---msa.inventory_item_id = l_inv_item_id and
1481                     msa.sourcing_rule_id = msr.sourcing_rule_id and
1482                     msr.status = 1 and
1483                     msr.sourcing_rule_type = 1 and
1484                     msr.sourcing_rule_id = receipt_org.sourcing_rule_id and
1485                     -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
1486                     TRUNC(NVL(receipt_org.disable_date, l_sysdate)) >= l_sysdate and
1487                     TRUNC(receipt_org.effective_date) <= l_sysdate and
1488                     receipt_org.sr_receipt_id = source_org.sr_receipt_id and
1489                     source_org.source_organization_id = msi.ORGANIZATION_ID and
1490                     source_org.sr_instance_id = msi.sr_instance_id and
1491                     -- ATP4drp Circular sources not supported by ATP.
1492                     --NVL(source_org.circular_src, 2) <> 1 AND
1493                     --Bug4567833
1494                     NVL(source_org.circular_src, 'N') <> 'Y' AND
1495                     msi.inventory_item_id = l_inv_item_id and
1496                     msi.plan_id = -1
1497                     ORDER BY rank asc, allocation_percent desc;
1498           -- BUG 2283260 Note that join happens between msc_system_items(msi)
1499           -- and msc_sr_source_org (src) for org, instance as in certain cases
1500           -- join with msc_sr_assignments (msa) will bring no rows.
1501           -- Filter on item in msc_system_items.
1502           END IF;
1503 
1504        END IF;
1505        FOR i in 1..x_atp_sources.Organization_Id.count Loop
1506             IF PG_DEBUG in ('Y', 'C') THEN
1507         msc_sch_wb.atp_debug( 'Atp_Sources: ' || 'x_atp_sources.Organization_Id ='|| x_atp_sources.Organization_Id(i));
1508         msc_sch_wb.atp_debug( 'Atp_Sources: ' || 'x_atp_sources.Supplier_Id ='|| x_atp_sources.Supplier_Id(i));
1509             END IF;
1510        END Loop;
1511     END IF;
1512         FOR i in 1..x_atp_sources.Organization_Id.count Loop
1513         IF PG_DEBUG in ('Y', 'C') THEN
1514            msc_sch_wb.atp_debug('Atp_Sources: ' || 'x_atp_sources.ship_method' || i ||' ' ||  x_atp_sources.ship_method(i));
1515         END IF;
1516      END LOOP;
1517 
1518      IF l_model_flag = 1
1519                          and x_atp_sources.instance_id.count > 0 THEN
1520         --- we found an ato item or a model. we need to connect to  msc_cto_sourcing to make sure
1521         -- that we have valid source
1522         l_source_list :=  x_atp_sources;
1523         MSC_ATP_CTO.Validate_CTO_Sources(l_source_list,
1524                                          l_line_ids,
1525                                          NVL(MSC_ATP_PVT.G_INSTANCE_ID, p_instance_id),
1526                                          p_session_id,
1527                                          x_return_status);
1528         x_atp_sources := l_source_list;
1529 
1530      END IF;
1531 
1532     IF PG_DEBUG in ('Y', 'C') THEN
1533        msc_sch_wb.atp_debug('***** End Atp_Sources *****');
1534     END IF;
1535 
1536 Exception
1537 
1538     WHEN NO_DATA_FOUND THEN
1539         IF PG_DEBUG in ('Y', 'C') THEN
1540            msc_sch_wb.atp_debug('Atp_Sources: ' || 'sqlcode : ' || sqlcode);
1541            msc_sch_wb.atp_debug('Atp_Sources: ' || 'sqlerrm : ' || sqlerrm);
1542         END IF;
1543         --x_return_status := FND_API.G_RET_STS_ERROR;
1544         x_return_status := NVL(x_return_status, FND_API.G_RET_STS_ERROR);  -- 4091487
1545     WHEN others THEN
1546         IF PG_DEBUG in ('Y', 'C') THEN
1547            msc_sch_wb.atp_debug('Atp_Sources: ' || 'sqlcode : ' || sqlcode);
1548            msc_sch_wb.atp_debug('Atp_Sources: ' || 'sqlerrm : ' || sqlerrm);
1549         END IF;
1550         x_return_status := FND_API.G_RET_STS_ERROR;
1551 
1552 
1553 END Atp_Sources;
1554 
1555 --
1556 -- neat trick to make atp_consume a THETA(n) algo
1557 -- assume p_start_idx = 1, p_end_idx = n
1558 --
1559 -- Denote S(i) as the sum of the qtys on days 1..i
1560 -- S(i..j) as the sum of the qtys on days i..j, j >= i
1561 -- Q(i) as the qty on day i
1562 -- A(i) as the correct accumulated qty on day i
1563 --
1564 -- We can break down the problem into 2 cases using these 2 facts
1565 --
1566 -- A(n) >= 0 iff all demands are satisfied.
1567 -- A(n) = S(n).
1568 --
1569 -- For the case where S(n) >= 0, there are 4 main ideas.
1570 --
1571 -- 1) If Q(n) >= 0 and S(n-1) >= 0 then
1572 --
1573 -- for the array with qtys defined by
1574 -- Q'(i) = Q(i) for i=1..n-1
1575 -- the accumulated qtys A'(i) = A(i) for i = 1..n-1
1576 --
1577 -- Also, A(n) = A(n-1) + Q(n) => A(n-1) = A(n) - Q(n)
1578 --
1579 -- i.e. since S(n-1) >= 0 and Q(n) >=0, all netted demands are
1580 -- satisfied by day n-1.
1581 -- Hence after backward and forward consumption the net qty on
1582 -- day n is still Q(n), and the net qtys on other days are the
1583 -- same as if Q(n) were not there.
1584 --
1585 -- -----------------
1586 --
1587 -- 2) If S(i) < 0 then A(j) = 0 for j = 1..i
1588 -- i.e. since the demands on days 1..i outweigh the supplies,
1589 -- all the supplies during that time must be consumed. Also, we
1590 -- already know all demands are satisfied.
1591 --
1592 -- -----------------
1593 --
1594 -- 3) If Q(n) < 0 and S(n-1) >=0 then
1595 --
1596 -- for the array with qtys defined by
1597 -- Q'(n-1) = Q(n-1) + Q(n)
1598 -- for i=1..n-2, Q'(i) = Q(i)
1599 -- will result in the accumulated qtys A'(i) = A(i) for i=1..n-2.
1600 --
1601 -- Also, A(n-1) = A(n)
1602 --
1603 -- i.e. the last qty must be satisfied by backward consumption.
1604 -- hence may simply push it back 1 day.
1605 --
1606 -- -----------------
1607 --
1608 -- 4) Since (1) and (3) reduce our problem size by 1 each time
1609 -- and 2 is a terminating condition, we can use induction to
1610 -- get a linear time algorithm.
1611 --
1612 -- In translating this to code, we use forward_acc to basically
1613 -- handle (3).
1614 --
1615 -- ---------------------------------------------------
1616 -- For the case when S(n) < 0, we simply use the fact that the old
1617 -- atp_consume prioritizes demands so that earlier demands get first dibs on
1618 -- the supply. So we calculate the total supply and give it to the
1619 -- earliest demands
1620 --
1621 --
1622 
1623 PROCEDURE atp_consume_range (
1624         p_atp_qty         IN OUT  NoCopy MRP_ATP_PUB.number_arr,
1625         p_start_idx       IN      NUMBER,
1626         p_end_idx         IN      NUMBER)
1627 IS
1628 j NUMBER;
1629 acc NUMBER := 0;
1630 real_dmd NUMBER := 0;
1631 forward_acc NUMBER := 0;
1632 BEGIN
1633         IF p_start_idx is null or p_end_idx is null or
1634            p_start_idx >= p_end_idx
1635         THEN
1636            IF PG_DEBUG in ('Y', 'C') THEN
1637               msc_sch_wb.atp_debug('atp_consume_range, bad indices: ' ||
1638                  p_start_idx || ':' || p_end_idx);
1639            END IF;
1640            return;
1641         END IF;
1642 
1643         IF PG_DEBUG in ('Y', 'C') THEN
1644            msc_sch_wb.atp_debug('atp_consume_range: arr size: ' || p_atp_qty.count());
1645            msc_sch_wb.atp_debug('                   start: ' || p_start_idx ||
1646                                 'end: ' || p_end_idx);
1647         END IF;
1648 
1649 	-- calculate S(n) and total supply (real_dmd)
1650 	FOR i IN p_start_idx..p_end_idx LOOP
1651 		acc := acc + p_atp_qty(i);
1652 		IF p_atp_qty(i) > 0 THEN
1653 			real_dmd := real_dmd + p_atp_qty(i);
1654 		END IF;
1655 	END LOOP;
1656 
1657 	j := 0;
1658 	IF acc >= 0 THEN
1659 		forward_acc := p_atp_qty(p_end_idx);
1660 		p_atp_qty(p_end_idx) := acc;
1661 
1662 		FOR i in REVERSE p_start_idx..(p_end_idx-1) LOOP
1663 
1664 			-- idea 1
1665 			IF forward_acc > 0 THEN
1666 				acc := acc - forward_acc;
1667 				forward_acc := 0;
1668 			END IF;
1669 
1670 			-- idea 2
1671 			IF acc < 0 THEN
1672 				j := i;
1673 				EXIT;
1674 			END IF;
1675 
1676 			-- idea 3
1677 			forward_acc := forward_acc + p_atp_qty(i);
1678 			p_atp_qty(i) := acc;
1679 		END LOOP;
1680 
1681 		-- idea 2
1682 		FOR i in p_start_idx..j LOOP
1683 			p_atp_qty(i) := 0;
1684 		END LOOP;
1685 
1686 	ELSE
1687 
1688 		-- Distribute the supplies to the earliest demands
1689 		j := p_end_idx;
1690 		FOR i in p_start_idx..p_end_idx LOOP
1691 			IF p_atp_qty(i) < 0 THEN
1692 				real_dmd := real_dmd + p_atp_qty(i);
1693 			END IF;
1694 
1695 			IF real_dmd >= 0 THEN
1696 				p_atp_qty(i) := 0;
1697 			ELSE
1698 				p_atp_qty(i) := real_dmd;
1699 				j := i+1;
1700 				EXIT;
1701 			END IF;
1702 		END LOOP;
1703 
1704 		FOR i in j..p_end_idx LOOP
1705 			IF p_atp_qty(i) < 0 THEN
1706 				real_dmd := real_dmd + p_atp_qty(i);
1707 			END IF;
1708 			p_atp_qty(i) := real_dmd;
1709 		END LOOP;
1710 	END IF;
1711 
1712 END atp_consume_range;
1713 
1714 PROCEDURE atp_consume (
1715         p_atp_qty         IN OUT  NoCopy MRP_ATP_PUB.number_arr,
1716         p_counter         IN      NUMBER)
1717 IS
1718 BEGIN
1719         atp_consume_range(p_atp_qty, 1, p_counter);
1720 END atp_consume;
1721 
1722 PROCEDURE Details_Output (
1723   p_atp_period          IN       MRP_ATP_PUB.ATP_Period_Typ,
1724   p_atp_supply_demand   IN       MRP_ATP_PUB.ATP_Supply_Demand_Typ,
1725   x_atp_period          IN OUT   NOCOPY  MRP_ATP_PUB.ATP_Period_Typ,
1726   x_atp_supply_demand   IN OUT   NOCOPY  MRP_ATP_PUB.ATP_Supply_Demand_Typ,
1727   x_return_status       OUT      NoCopy VARCHAR2
1728 ) IS
1729 
1730 l_period_count          PLS_INTEGER;
1731 l_sd_count              PLS_INTEGER;
1732 l_count                 PLS_INTEGER;
1733 
1734 Begin
1735 
1736     IF PG_DEBUG in ('Y', 'C') THEN
1737        msc_sch_wb.atp_debug('***** Begin Details_Output Procedure *****');
1738     END IF;
1739 
1740     x_return_status := FND_API.G_RET_STS_SUCCESS;
1741 
1742     -- dsting 10/1/02 supply/demand performance enh
1743     -- insert period data into mrp_atp_details_temp to transfer later
1744     -- since bulk binds across dblink are not supported.
1745 
1746     MSC_ATP_UTILS.Put_Period_Data(p_atp_period, NULL, MSC_ATP_PVT.G_SESSION_ID);
1747     IF PG_DEBUG in ('Y', 'C') THEN
1748        msc_sch_wb.atp_debug('Details_Output: ' || ' dsting expect 0 sd recs: ' || x_atp_supply_demand.level.count);
1749     END IF;
1750     RETURN;
1751 
1752     IF p_atp_period.level.COUNT > 0 THEN
1753 
1754         l_count := x_atp_period.level.COUNT;
1755         FOR l_period_count in 1..p_atp_period.level.COUNT LOOP
1756                     MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
1757 
1758                     x_atp_period.Level(l_count + l_period_count) :=
1759                         p_atp_period.Level(l_period_count);
1760                     x_atp_period.Inventory_Item_Id(l_count + l_period_count) :=
1761                         p_atp_period.Inventory_Item_Id(l_period_count);
1762                     x_atp_period.Request_Item_Id(l_count + l_period_count) :=
1763                         p_atp_period.Request_Item_Id(l_period_count);
1764                     x_atp_period.Organization_Id(l_count + l_period_count) :=
1765                         p_atp_period.Organization_Id(l_period_count);
1766                     x_atp_period.Department_Id(l_count + l_period_count) :=
1767                         p_atp_period.Department_Id(l_period_count);
1768                     x_atp_period.Resource_Id(l_count + l_period_count) :=
1769                         p_atp_period.Resource_Id(l_period_count);
1770                     x_atp_period.Supplier_Id(l_count + l_period_count) :=
1771                         p_atp_period.Supplier_Id(l_period_count);
1772                     x_atp_period.Supplier_Site_Id(l_count + l_period_count) :=
1773                         p_atp_period.Supplier_Site_Id(l_period_count);
1774                     x_atp_period.From_Organization_Id(l_count + l_period_count)
1775                         := p_atp_period.From_Organization_Id(l_period_count);
1776                     x_atp_period.From_Location_Id(l_count + l_period_count) :=
1777                         p_atp_period.From_Location_Id(l_period_count);
1778                     x_atp_period.To_Organization_Id(l_count + l_period_count) :=
1779                         p_atp_period.To_Organization_Id(l_period_count);
1780                     x_atp_period.To_Location_Id(l_count + l_period_count) :=
1781                         p_atp_period.To_Location_Id(l_period_count);
1782                     x_atp_period.Ship_Method(l_count + l_period_count) :=
1783                         p_atp_period.Ship_Method(l_period_count);
1784                     x_atp_period.Uom(l_count + l_period_count) :=
1785                         p_atp_period.Uom(l_period_count);
1786                     x_atp_period.Total_Supply_Quantity(l_count + l_period_count)
1787                         := p_atp_period.Total_Supply_Quantity(l_period_count);
1788                     x_atp_period.Total_Demand_Quantity(l_count + l_period_count)
1789                         := p_atp_period.Total_Demand_Quantity(l_period_count);
1790                     x_atp_period.Period_Start_Date(l_count + l_period_count):=
1791                         p_atp_period.Period_Start_Date(l_period_count);
1792                     x_atp_period.Period_End_Date(l_count + l_period_count):=
1793                         p_atp_period.Period_End_Date(l_period_count);
1794                     x_atp_period.Period_Quantity(l_count + l_period_count):=
1795                         p_atp_period.Period_Quantity(l_period_count);
1796                     x_atp_period.Cumulative_Quantity(l_count + l_period_count):=
1797                         p_atp_period.Cumulative_Quantity(l_period_count);
1798                     x_atp_period.Identifier1(l_count + l_period_count):=
1799                         p_atp_period.Identifier1(l_period_count);
1800                     x_atp_period.Identifier2(l_count + l_period_count):=
1801                         p_atp_period.Identifier2(l_period_count);
1802                     x_atp_period.Identifier(l_count + l_period_count):=
1803                         p_atp_period.Identifier(l_period_count);
1804                     x_atp_period.scenario_Id(l_count + l_period_count) :=
1805                         p_atp_period.scenario_Id(l_period_count);
1806                     x_atp_period.pegging_id(l_count + l_period_count) :=
1807                         p_atp_period.pegging_id(l_period_count);
1808                     x_atp_period.end_pegging_id(l_count + l_period_count) :=
1809                         p_atp_period.end_pegging_id(l_period_count);
1810 
1811 
1812         END LOOP;
1813     END IF;
1814 
1815     IF p_atp_supply_demand.level.COUNT > 0 THEN
1816         l_count := x_atp_supply_demand.level.COUNT;
1817 
1818         FOR l_sd_count in 1..p_atp_supply_demand.level.COUNT LOOP
1819                     MSC_SATP_FUNC.Extend_Atp_Supply_Demand(x_atp_supply_demand,
1820                                              x_return_status);
1821                     x_atp_supply_demand.Level(l_count + l_sd_count):=
1822                         p_atp_supply_demand.Level(l_sd_count);
1823                     x_atp_supply_demand.Inventory_Item_Id(l_count+l_sd_count):=
1824                         p_atp_supply_demand.Inventory_Item_Id(l_sd_count);
1825                     x_atp_supply_demand.Request_Item_Id(l_count+l_sd_count):=
1826                         p_atp_supply_demand.Request_Item_Id(l_sd_count);
1827                     x_atp_supply_demand.Organization_Id(l_count+l_sd_count):=
1828                         p_atp_supply_demand.Organization_Id(l_sd_count);
1829                     x_atp_supply_demand.Department_Id(l_count+l_sd_count):=
1830                         p_atp_supply_demand.Department_Id(l_sd_count);
1831                     x_atp_supply_demand.Resource_Id(l_count+l_sd_count):=
1832                         p_atp_supply_demand.Resource_Id(l_sd_count);
1833                     x_atp_supply_demand.Supplier_Id(l_count+l_sd_count):=
1834                         p_atp_supply_demand.Supplier_Id(l_sd_count);
1835                     x_atp_supply_demand.Supplier_Site_Id(l_count+l_sd_count):=
1836                         p_atp_supply_demand.Supplier_Site_Id(l_sd_count);
1837                     x_atp_supply_demand.From_Organization_Id(l_count+l_sd_count)
1838                         := p_atp_supply_demand.From_Organization_Id(l_sd_count);
1839                     x_atp_supply_demand.From_Location_Id(l_count+l_sd_count):=
1840                         p_atp_supply_demand.From_Location_Id(l_sd_count);
1841                     x_atp_supply_demand.To_Organization_Id(l_count+l_sd_count):=
1842                         p_atp_supply_demand.To_Organization_Id(l_sd_count);
1843                     x_atp_supply_demand.To_Location_Id(l_count+l_sd_count):=
1844                         p_atp_supply_demand.To_Location_Id(l_sd_count);
1845                     x_atp_supply_demand.Ship_Method(l_count+l_sd_count):=
1846                         p_atp_supply_demand.Ship_Method(l_sd_count);
1847                     x_atp_supply_demand.Uom(l_count+l_sd_count):=
1848                         p_atp_supply_demand.Uom(l_sd_count);
1849                     x_atp_supply_demand.Identifier1(l_count+l_sd_count):=
1850                         p_atp_supply_demand.Identifier1(l_sd_count);
1851                     x_atp_supply_demand.Identifier2(l_count+l_sd_count):=
1852                         p_atp_supply_demand.Identifier2(l_sd_count);
1853                     x_atp_supply_demand.Identifier3(l_count+l_sd_count):=
1854                         p_atp_supply_demand.Identifier3(l_sd_count);
1855                     x_atp_supply_demand.Identifier4(l_count+l_sd_count):=
1856                         p_atp_supply_demand.Identifier4(l_sd_count);
1857                     x_atp_supply_demand.Supply_Demand_Type(l_count+l_sd_count):=
1858                         p_atp_supply_demand.Supply_Demand_Type(l_sd_count);
1859                     x_atp_supply_demand.Supply_Demand_Source_Type(l_count+ l_sd_count)
1860                         := p_atp_supply_demand.Supply_Demand_Source_Type(l_sd_count);
1861                     x_atp_supply_demand.Supply_Demand_Source_Type_Name(l_count+l_sd_count):=
1862                         p_atp_supply_demand.Supply_Demand_Source_Type_Name(l_sd_count);
1863                     x_atp_supply_demand.Supply_Demand_Date(l_count+l_sd_count):=
1864                         p_atp_supply_demand.Supply_Demand_Date(l_sd_count);
1865                     x_atp_supply_demand.Supply_Demand_Quantity(l_count+l_sd_count) :=
1866                         p_atp_supply_demand.Supply_Demand_Quantity(l_sd_count);
1867                     x_atp_supply_demand.Identifier(l_count + l_sd_count):=
1868                         p_atp_supply_demand.Identifier(l_sd_count);
1869                     x_atp_supply_demand.scenario_Id(l_count+l_sd_count):=
1870                         p_atp_supply_demand.scenario_Id(l_sd_count);
1871                     x_atp_supply_demand.Disposition_Type(l_count+l_sd_count):=
1872                         p_atp_supply_demand.Disposition_Type(l_sd_count);
1873                     x_atp_supply_demand.Disposition_Name(l_count+l_sd_count):=
1874                         p_atp_supply_demand.Disposition_Name(l_sd_count);
1875                     x_atp_supply_demand.Pegging_Id(l_count+l_sd_count):=
1876                         p_atp_supply_demand.Pegging_Id(l_sd_count);
1877                     x_atp_supply_demand.End_Pegging_Id(l_count+l_sd_count):=
1878                         p_atp_supply_demand.End_Pegging_Id(l_sd_count);
1879 
1880 	IF PG_DEBUG in ('Y', 'C') THEN
1881 	   msc_sch_wb.atp_debug('Details_Output: item '||
1882 		p_atp_supply_demand.inventory_item_id(l_sd_count)||
1883 		' : org '|| p_atp_supply_demand.organization_id(l_sd_count) ||
1884 		' : qty '|| p_atp_supply_demand.supply_demand_quantity(l_sd_count) ||
1885 		' : peg '|| p_atp_supply_demand.pegging_id(l_sd_count) ||
1886                 ' : end peg ' || p_atp_supply_demand.end_pegging_id(l_sd_count));
1887 	END IF;
1888 
1889 
1890         END LOOP;
1891     END IF;
1892 
1893     IF PG_DEBUG in ('Y', 'C') THEN
1894        msc_sch_wb.atp_debug('***** End Details_Output Procedure *****');
1895     END IF;
1896 
1897 END Details_Output;
1898 
1899 
1900 PROCEDURE get_dept_res_code (p_instance_id            IN NUMBER,
1901                              p_department_id          IN NUMBER,
1902                              p_resource_id            IN NUMBER,
1903                              p_organization_id        IN NUMBER,
1904                              x_department_code        OUT NoCopy VARCHAR2,
1905                              x_resource_code          OUT NoCopy VARCHAR2)
1906 IS
1907 
1908 BEGIN
1909 
1910   SELECT department_code,
1911          resource_code
1912   INTO   x_department_code,
1913          x_resource_code
1914   FROM   msc_department_resources
1915   WHERE  sr_instance_id = p_instance_id
1916   AND    organization_id = p_organization_id
1917   AND    plan_id = -1
1918   AND    department_id = p_department_id
1919   AND    resource_id = p_resource_id;
1920 
1921 EXCEPTION WHEN NO_DATA_FOUND THEN
1922   x_department_code := null;
1923   x_resource_code := null;
1924 
1925 END get_dept_res_code;
1926 
1927 
1928 PROCEDURE Get_SD_Period_Rec(
1929   p_atp_period          IN       MRP_ATP_PUB.ATP_Period_Typ,
1930   p_atp_supply_demand   IN       MRP_ATP_PUB.ATP_Supply_Demand_Typ,
1931   p_identifier          IN       NUMBER,
1932   p_scenario_id         IN       NUMBER,
1933   p_new_scenario_id     IN       NUMBER,
1934   x_atp_period          IN OUT   NOCOPY MRP_ATP_PUB.ATP_Period_Typ,
1935   x_atp_supply_demand   IN OUT   NOCOPY MRP_ATP_PUB.ATP_Supply_Demand_Typ,
1936   x_return_status       OUT      NoCopy VARCHAR2
1937 ) IS
1938 
1939 l_period_count  PLS_INTEGER;
1940 l_sd_count      PLS_INTEGER;
1941 l_count         PLS_INTEGER;
1942 
1943 BEGIN
1944 
1945     IF PG_DEBUG in ('Y', 'C') THEN
1946        msc_sch_wb.atp_debug('***** Begin Get_SD_Period_Rec Procedure *****');
1947     END IF;
1948 
1949     FOR l_period_count in 1..p_atp_period.Level.COUNT LOOP
1950 
1951         IF (p_atp_period.identifier(l_period_count) = p_identifier) AND
1952            (p_atp_period.Scenario_Id(l_period_count) = p_scenario_id) THEN
1953                     l_count := x_atp_period.level.COUNT + 1;
1954                     MSC_SATP_FUNC.Extend_Atp_Period(x_atp_period, x_return_status);
1955                     x_atp_period.Level(l_count) :=
1956                         p_atp_period.Level(l_period_count);
1957                     x_atp_period.Inventory_Item_Id(l_count) :=
1958                         p_atp_period.Inventory_Item_Id(l_period_count);
1959                     x_atp_period.Request_Item_Id(l_count) :=
1960                         p_atp_period.Request_Item_Id(l_period_count);
1961                     x_atp_period.Organization_Id(l_count) :=
1962                         p_atp_period.Organization_Id(l_period_count);
1963                     x_atp_period.Department_Id(l_count) :=
1964                         p_atp_period.Department_Id(l_period_count);
1965                     x_atp_period.Resource_Id(l_count) :=
1966                         p_atp_period.Resource_Id(l_period_count);
1967                     x_atp_period.Supplier_Id(l_count) :=
1968                         p_atp_period.Supplier_Id(l_period_count);
1969                     x_atp_period.Supplier_Site_Id(l_count) :=
1970                         p_atp_period.Supplier_Site_Id(l_period_count);
1971                     x_atp_period.From_Organization_Id(l_count)
1972                         := p_atp_period.From_Organization_Id(l_period_count);
1973                     x_atp_period.From_Location_Id(l_count) :=
1974                         p_atp_period.From_Location_Id(l_period_count);
1975                     x_atp_period.To_Organization_Id(l_count) :=
1976                         p_atp_period.To_Organization_Id(l_period_count);
1977                     x_atp_period.To_Location_Id(l_count) :=
1978                         p_atp_period.To_Location_Id(l_period_count);
1979                     x_atp_period.Ship_Method(l_count) :=
1980                         p_atp_period.Ship_Method(l_period_count);
1981                     x_atp_period.Uom(l_count) :=
1982                         p_atp_period.Uom(l_period_count);
1983                     x_atp_period.Total_Supply_Quantity(l_count)
1984                         := p_atp_period.Total_Supply_Quantity(l_period_count);
1985                     x_atp_period.Total_Demand_Quantity(l_count)
1986                         := p_atp_period.Total_Demand_Quantity(l_period_count);
1987                     x_atp_period.Period_Start_Date(l_count):=
1988                         p_atp_period.Period_Start_Date(l_period_count);
1989                     x_atp_period.Period_End_Date(l_count):=
1990                         p_atp_period.Period_End_Date(l_period_count);
1991                     x_atp_period.Period_Quantity(l_count):=
1992                         p_atp_period.Period_Quantity(l_period_count);
1993                     x_atp_period.Cumulative_Quantity(l_count):=
1994                         p_atp_period.Cumulative_Quantity(l_period_count);
1995                     x_atp_period.Identifier1(l_count):=
1996                         p_atp_period.Identifier1(l_period_count);
1997                     x_atp_period.Identifier2(l_count):=
1998                         p_atp_period.Identifier2(l_period_count);
1999                     x_atp_period.Identifier(l_count):=
2000                         p_atp_period.Identifier(l_period_count);
2001                     x_atp_period.scenario_Id(l_count) := p_new_scenario_id;
2002                     x_atp_period.Pegging_Id(l_count):=
2003                         p_atp_period.Pegging_Id(l_period_count);
2004                     x_atp_period.End_Pegging_Id(l_count):=
2005                         p_atp_period.End_Pegging_Id(l_period_count);
2006 
2007         END IF;
2008     END LOOP;
2009 
2010     FOR l_sd_count in 1..p_atp_supply_demand.Level.COUNT LOOP
2011         IF (p_atp_supply_demand.identifier(l_sd_count) = p_identifier) AND
2012            (p_atp_supply_demand.Scenario_Id(l_sd_count)= p_scenario_id) THEN
2013                     l_count := x_atp_supply_demand.level.COUNT + 1;
2014                     MSC_SATP_FUNC.Extend_Atp_Supply_Demand(x_atp_supply_demand,
2015                                              x_return_status);
2016                     x_atp_supply_demand.Level(l_count):=
2017                         p_atp_supply_demand.Level(l_sd_count);
2018                     x_atp_supply_demand.Inventory_Item_Id(l_count):=
2019                         p_atp_supply_demand.Inventory_Item_Id(l_sd_count);
2020                     x_atp_supply_demand.Request_Item_Id(l_count):=
2021                         p_atp_supply_demand.Request_Item_Id(l_sd_count);
2022                     x_atp_supply_demand.Organization_Id(l_count):=
2023                         p_atp_supply_demand.Organization_Id(l_sd_count);
2024                     x_atp_supply_demand.Department_Id(l_count):=
2025                         p_atp_supply_demand.Department_Id(l_sd_count);
2026                     x_atp_supply_demand.Resource_Id(l_count):=
2027                         p_atp_supply_demand.Resource_Id(l_sd_count);
2028                     x_atp_supply_demand.Supplier_Id(l_count):=
2029                         p_atp_supply_demand.Supplier_Id(l_sd_count);
2030                     x_atp_supply_demand.Supplier_Site_Id(l_count):=
2031                         p_atp_supply_demand.Supplier_Site_Id(l_sd_count);
2032                     x_atp_supply_demand.From_Organization_Id(l_count)
2033                         := p_atp_supply_demand.From_Organization_Id(l_sd_count);
2034                     x_atp_supply_demand.From_Location_Id(l_count):=
2035                         p_atp_supply_demand.From_Location_Id(l_sd_count);
2036                     x_atp_supply_demand.To_Organization_Id(l_count):=
2037                         p_atp_supply_demand.To_Organization_Id(l_sd_count);
2038                     x_atp_supply_demand.To_Location_Id(l_count):=
2039                         p_atp_supply_demand.To_Location_Id(l_sd_count);
2040                     x_atp_supply_demand.Ship_Method(l_count):=
2041                         p_atp_supply_demand.Ship_Method(l_sd_count);
2042                     x_atp_supply_demand.Uom(l_count):=
2043                         p_atp_supply_demand.Uom(l_sd_count);
2044                     x_atp_supply_demand.Identifier1(l_count):=
2045                         p_atp_supply_demand.Identifier1(l_sd_count);
2046                     x_atp_supply_demand.Identifier2(l_count):=
2047                         p_atp_supply_demand.Identifier2(l_sd_count);
2048                     x_atp_supply_demand.Identifier3(l_count):=
2049                         p_atp_supply_demand.Identifier3(l_sd_count);
2050                     x_atp_supply_demand.Identifier4(l_count):=
2051                         p_atp_supply_demand.Identifier4(l_sd_count);
2052                     x_atp_supply_demand.Supply_Demand_Type(l_count):=
2053                         p_atp_supply_demand.Supply_Demand_Type(l_sd_count);
2054                     x_atp_supply_demand.Supply_Demand_Source_Type(l_count)
2055                         := p_atp_supply_demand.Supply_Demand_Source_Type(l_sd_count);
2056                     x_atp_supply_demand.Supply_Demand_Source_Type_Name(l_count)
2057                         := p_atp_supply_demand.Supply_Demand_Source_Type_Name(l_sd_count);
2058                     x_atp_supply_demand.Supply_Demand_Date(l_count):=
2059                         p_atp_supply_demand.Supply_Demand_Date(l_sd_count);
2060                     x_atp_supply_demand.Supply_Demand_Quantity(l_count) :=
2061                         p_atp_supply_demand.Supply_Demand_Quantity(l_sd_count);
2062                     x_atp_supply_demand.Identifier(l_count):=
2063                         p_atp_supply_demand.Identifier(l_sd_count);
2064                     x_atp_supply_demand.scenario_Id(l_count):=p_new_scenario_id;
2065                     x_atp_supply_demand.Pegging_Id(l_count):=
2066                         p_atp_supply_demand.Pegging_Id(l_sd_count);
2067                     x_atp_supply_demand.Disposition_Type(l_count):=
2068                         p_atp_supply_demand.Disposition_Type(l_sd_count);
2069                     x_atp_supply_demand.Disposition_Name(l_count):=
2070                         p_atp_supply_demand.Disposition_Name(l_sd_count);
2071                     x_atp_supply_demand.End_Pegging_Id(l_count):=
2072                         p_atp_supply_demand.End_Pegging_Id(l_sd_count);
2073 
2074 IF PG_DEBUG in ('Y', 'C') THEN
2075    msc_sch_wb.atp_debug('Get_SD_Period_Rec: ' || 'Details_Output: item '|| p_atp_supply_demand.inventory_item_id(l_sd_count)
2076 	|| ': org '|| p_atp_supply_demand.organization_id(l_sd_count)
2077 	|| ': quantity '|| p_atp_supply_demand.supply_demand_quantity(l_sd_count)
2078 	|| ': peg '|| p_atp_supply_demand.pegging_id(l_sd_count));
2079 END IF;
2080 
2081         END IF;
2082     END LOOP;
2083 
2084     IF PG_DEBUG in ('Y', 'C') THEN
2085        msc_sch_wb.atp_debug('***** End Get_SD_Period_Rec Procedure *****');
2086     END IF;
2087 
2088 END Get_SD_Period_Rec;
2089 
2090 
2091 PROCEDURE get_org_default_info (
2092 	p_instance_id            	IN 	NUMBER,
2093 	p_organization_id        	IN 	NUMBER,
2094 	x_default_atp_rule_id       	OUT 	NoCopy NUMBER,
2095 	x_calendar_code             	OUT 	NoCopy VARCHAR2,
2096 	x_calendar_exception_set_id 	OUT 	NoCopy NUMBER,
2097 	x_default_demand_class      	OUT 	NoCopy VARCHAR2,
2098 	x_org_code			OUT	NoCopy VARCHAR2)
2099 IS
2100 
2101 BEGIN
2102 
2103     SELECT default_atp_rule_id,
2104            calendar_code,
2105            calendar_exception_set_id,
2106            default_demand_class,
2107 	   organization_code
2108     INTO   x_default_atp_rule_id,
2109            x_calendar_code,
2110            x_calendar_exception_set_id,
2111            x_default_demand_class,
2112 	   x_org_code
2113     FROM   msc_trading_partners
2114     WHERE  sr_tp_id = p_organization_id
2115     AND    sr_instance_id = p_instance_id
2116     AND    partner_type = 3;
2117 
2118 EXCEPTION WHEN others THEN
2119   x_default_atp_rule_id := null;
2120   x_calendar_code := null;
2121   x_calendar_exception_set_id := null;
2122   x_default_demand_class := null;
2123   x_org_code := null;
2124 END get_org_default_info;
2125 
2126 PROCEDURE inv_primary_uom_conversion (p_instance_id        IN  NUMBER,
2127                                       p_organization_id    IN  NUMBER,
2128                                       p_inventory_item_id  IN  NUMBER,
2129                                       p_uom_code           IN  VARCHAR2,
2130                                       x_primary_uom_code   OUT NoCopy VARCHAR2,
2131                                       x_conversion_rate    OUT NoCopy NUMBER
2132                                       )
2133 IS
2134 
2135 BEGIN
2136 
2137    IF PG_DEBUG in ('Y', 'C') THEN --bug3110023
2138         msc_sch_wb.atp_debug('**************** inv_primary_uom_conversion Begin ***************');
2139         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'p_instance_id       - ' || p_instance_id);
2140         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'p_inventory_item_id - ' || MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id);
2141         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'p_inventory_item_id - ' || p_inventory_item_id);
2142         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'p_organization_id   - ' || p_organization_id);
2143         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'p_uom_code   - ' || p_uom_code);
2144         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'primary_uom_code   - ' || MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code);
2145    END IF;
2146    /* Tuned for performance bug 2484964 */
2147     /* 4192057: Use the item info available in G_item_ifo_rec
2148        instead of connecting to msc_system_items table
2149     SELECT items.uom_code,
2150            conversion_rate
2151     INTO   x_primary_uom_code,
2152            x_conversion_rate
2153     FROM   msc_uom_conversions_view mucv,
2154            msc_system_items items
2155     WHERE  items.sr_inventory_item_id = p_inventory_item_id
2156     AND    items.organization_id = p_organization_id
2157     AND    items.plan_id = -1
2158     AND    items.sr_instance_id = p_instance_id
2159     AND    mucv.uom_code = p_uom_code
2160     AND    mucv.primary_uom_code  = items.uom_code
2161     AND    mucv.inventory_item_id = items.inventory_item_id
2162     AND    mucv.organization_id = items.organization_id
2163     AND    mucv.sr_instance_id = items.sr_instance_id;
2164     --AND    mucv.organization_id = p_organization_id
2165     --AND    mucv.sr_instance_id = p_instance_id;
2166     */
2167   IF p_uom_code <> MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code THEN --bug3110023
2168    SELECT primary_uom_code,
2169           conversion_rate
2170    INTO   x_primary_uom_code,
2171           x_conversion_rate
2172    FROM   msc_uom_conversions_view mucv
2173    WHERE  mucv.uom_code = p_uom_code
2174    AND    mucv.primary_uom_code  = MSC_ATP_PVT.G_ITEM_INFO_REC.uom_code
2175    AND    mucv.inventory_item_id = MSC_ATP_PVT.G_ITEM_INFO_REC.dest_inv_item_id
2176    AND    mucv.organization_id = p_organization_id
2177    AND    mucv.sr_instance_id = p_instance_id;
2178   ELSE
2179    x_primary_uom_code := p_uom_code;
2180    x_conversion_rate := 1;
2181   END IF;
2182 
2183   IF PG_DEBUG in ('Y', 'C') THEN --bug3110023
2184       msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'x_primary_uom_code   - ' || x_primary_uom_code);
2185       msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'x_conversion_rate   - ' || x_conversion_rate);
2186   END IF;
2187 EXCEPTION WHEN NO_DATA_FOUND THEN
2188   x_primary_uom_code := p_uom_code;
2189   x_conversion_rate := 1;
2190   IF PG_DEBUG in ('Y', 'C') THEN --bug3110023
2191         msc_sch_wb.atp_debug('inv_primary_uom_conversion : Inside NO_DATA_FOUND  ');
2192         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'x_primary_uom_code   - ' || x_primary_uom_code);
2193         msc_sch_wb.atp_debug('inv_primary_uom_conversion : ' || 'x_conversion_rate   - ' || x_conversion_rate);
2194   END IF;
2195 END inv_primary_uom_conversion;
2196 
2197 
2198 PROCEDURE Extend_Atp_Sources (
2199   p_atp_sources         IN OUT NOCOPY  MRP_ATP_PVT.Atp_Source_Typ,
2200   x_return_status       OUT      NoCopy VARCHAR2)
2201 IS
2202 
2203 Begin
2204 
2205     x_return_status := FND_API.G_RET_STS_SUCCESS;
2206     p_atp_sources.Organization_Id.EXTEND;
2207     p_atp_sources.Supplier_Id.EXTEND;
2208     p_atp_sources.Supplier_Site_Id.EXTEND;
2209     p_atp_sources.Rank.EXTEND;
2210     p_atp_sources.Source_Type.EXTEND;
2211     p_atp_sources.Instance_Id.EXTEND;
2212     p_atp_sources.Preferred.EXTEND;
2213     p_atp_sources.Lead_Time.EXTEND;
2214     p_atp_sources.Ship_Method.EXTEND;
2215 
2216 END;
2217 
2218 
2219 -- 24x7 ATP
2220 --  Function logic changed to support 24x7 ATP.
2221 Procedure Get_Plan_Info(
2222     p_instance_id        IN NUMBER,
2223     p_inventory_item_id  IN NUMBER,
2224     p_organization_id    IN NUMBER,
2225     p_demand_class       IN VARCHAR2,
2226     -- x_plan_id         OUT NoCopy NUMBER,  commented for bug 2392456
2227     -- x_assign_set_id   OUT NoCopy NUMBER   comented for bug 2392456
2228     x_plan_info_rec      OUT NoCopy MSC_ATP_PVT.plan_info_rec,   -- added for bug 2392456
2229     p_parent_plan_id     IN NUMBER DEFAULT NULL, --bug3510475
2230     p_time_phased_atp    IN VARCHAR2 := 'N' -- time_phased_atp
2231 )
2232 IS
2233 l_dc_atp_flag       NUMBER := 2;
2234 l_summary_flag      NUMBER;
2235 
2236 --24x7
2237 l_using_new_plan    number;
2238 l_plan_info_rec     MSC_ATP_PVT.plan_info_rec;
2239 
2240 --bug 2854351
2241 l_plan_ids          MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
2242 i                   number;
2243 l_generic_plan      number;
2244 
2245 --ATP4drp local variable for time phased ATP.
2246 l_time_phased_atp   VARCHAR2(2);
2247 -- END ATP4drp
2248 
2249 BEGIN
2250     -- ngoel, modified to check for ATP rule for demand class ATP
2251 
2252     -- initialize x_plan_info_rec for bug 2392456 starts
2253     x_plan_info_rec.plan_id                 := null;
2254     x_plan_info_rec.plan_name               := null;
2255     x_plan_info_rec.assignment_set_id       := null;
2256     x_plan_info_rec.plan_start_date         := null;
2257     x_plan_info_rec.plan_cutoff_date        := null;
2258     -- changes for bug 2392456 ends.
2259 
2260     x_plan_info_rec.summary_flag            := null; -- 24x7
2261     x_plan_info_rec.copy_plan_id            := null; -- 24x7
2262     x_plan_info_rec.subst_flag              := null; -- 24x7
2263 
2264     -- Additional Fields for Supplier Capacity and Lead Time (SCLT) Project.
2265     x_plan_info_rec.sr_instance_id          := null;
2266     x_plan_info_rec.organization_id         := null;
2267     x_plan_info_rec.curr_cutoff_date        := null;
2268     --add for plan by request date
2269     x_plan_info_rec.schedule_by_date_type   := null;
2270 
2271     -- For ship_rec_cal project.
2272     x_plan_info_rec.enforce_pur_lead_time   := null;
2273     x_plan_info_rec.enforce_sup_capacity    := null;
2274 
2275     -- For ATP4drp project
2276     x_plan_info_rec.plan_type               := null;
2277 
2278     IF PG_DEBUG in ('Y', 'C') THEN
2279         msc_sch_wb.atp_debug('**************** Get_Plan_Info Begin ***************');
2280         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'p_instance_id       - ' || p_instance_id);
2281         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'p_inventory_item_id - ' || p_inventory_item_id);
2282         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'p_organization_id   - ' || p_organization_id);
2283         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'p_demand_class      - ' || p_demand_class);
2284         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'p_time_phased_atp   - ' || p_time_phased_atp);
2285     END IF;
2286 
2287     --  bug3510475. Check if the plan for Parent ORG /ITEM
2288     --already exists. if the parent plan exists, verify if the current
2289     --Item / Org is also planned in parent plan. Do not select from
2290     --msc_atp_plan_sn in this case.
2291     IF p_parent_plan_id is not null then
2292         BEGIN
2293           SELECT  plan_id
2294           INTO    x_plan_info_rec.plan_id
2295           FROM    msc_system_items
2296           WHERE   sr_instance_id = p_instance_id
2297           AND     organization_id = p_organization_id
2298           AND     sr_inventory_item_id = p_inventory_item_id
2299           AND     plan_id = p_parent_plan_id;
2300         EXCEPTION
2301           WHEN NO_DATA_FOUND THEN
2302                  x_plan_info_rec.plan_id := null;
2303         END;
2304     END IF;
2305     --bug3510475 add the x_plan_info_rec.plan_id condition
2306     IF MSC_ATP_PVT.G_ALLOCATED_ATP = 'N' and
2307          p_demand_class IS NOT NULL        and
2308          x_plan_info_rec.plan_id is NULL THEN
2309 
2310         -- Check if the demand class ATP is needed
2311         -- select item level and org level atp rules
2312         IF PG_DEBUG in ('Y', 'C') THEN
2313            msc_sch_wb.atp_debug('Item / Org is not planned in parent plan');
2314         END IF;
2315         BEGIN
2316 
2317             -- Bug 1757259, modified to replace = to IN for subquery. Also added distinct
2318             SELECT  demand_class_atp_flag
2319             INTO    l_dc_atp_flag
2320             FROM    msc_atp_rules
2321             WHERE   sr_instance_id = p_instance_id
2322             AND     rule_id IN (
2323                     SELECT  distinct NVL(mi.atp_rule_id, tp.default_atp_rule_id)
2324                     FROM    msc_system_items mi,
2325                             msc_trading_partners tp
2326                     WHERE   mi.organization_id = tp.sr_tp_id
2327                     AND     mi.sr_instance_id = tp.sr_instance_id
2328                     AND     tp.partner_type = 3
2329                     AND     mi.plan_id = -1
2330                     AND     mi.sr_instance_id = p_instance_id
2331                     AND     mi.organization_id = p_organization_id
2332                     AND     mi.sr_inventory_item_id = p_inventory_item_id);
2333         EXCEPTION
2334             WHEN no_data_found THEN
2335                 l_dc_atp_flag := 2;
2336         END;
2337         IF PG_DEBUG in ('Y', 'C') THEN
2338             msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'l_dc_atp_flag : ' ||l_dc_atp_flag);
2339         END IF;
2340     END IF;
2341 
2342     BEGIN
2343 
2344         --bug3510475 add the x_plan_info_rec.plan_id condition
2345         IF ((NVL(l_dc_atp_flag, 2) = 1) AND
2346             x_plan_info_rec.plan_id IS NULL) THEN
2347 
2348 
2349             -- select the plan_id based on the demand class
2350             IF PG_DEBUG in ('Y', 'C') THEN
2351                 msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'Inside Demand Class ATP');
2352             END IF;
2353 
2354 
2355             /*SELECT  plan_id
2356                     -- INTO   x_plan_id  commented for bug 2392456
2357             INTO    x_plan_info_rec.plan_id   -- changed for bug 2392456
2358             FROM    msc_atp_plan_sn
2359 
2360             WHERE   demand_class =  p_demand_class
2361             AND     sr_instance_id = p_instance_id
2362             AND     organization_id = p_organization_id
2363             AND     sr_inventory_item_id = p_inventory_item_id; */
2364 
2365             --CHANGES MADE FOR HUBnSPOKE
2366 	   SELECT  plan_id
2367            INTO    x_plan_info_rec.plan_id
2368 	   FROM
2369 	   	(SELECT  plan_id,Rank,completion_date
2370                	FROM    msc_atp_plan_sn
2371                	WHERE   demand_class = p_demand_class
2372                 AND     sr_instance_id = p_instance_id
2373                 AND     organization_id = p_organization_id
2374                 AND     sr_inventory_item_id = p_inventory_item_id
2375                 ORDER  BY Rank asc,completion_date desc,plan_id asc)
2376                 WHERE ROWNUM=1;
2377 
2378 
2379 
2380         END IF;
2381             EXCEPTION
2382                 WHEN no_data_found THEN
2383                     -- x_plan_id := NULL;  commented for bug 2392456
2384                     x_plan_info_rec.plan_id  := NULL;  -- changed for bug 2392456
2385 
2386         END;
2387 
2388         -- IF x_plan_id IS NULL THEN   commented for bug 2392456
2389         IF x_plan_info_rec.plan_id IS NULL THEN  -- changed for bug 2392456
2390 
2391             -- AATP: if we are doing allocated atp or no demand_class atp in atp_rule,
2392             -- we don't want to pick a plan with demand class on that plan.
2393             -- For AATP we want to pick a generic plan and then do allocation on it.
2394             -- Also, in case of demand class ATP, if we dont get a plan, we will select
2395             -- generic plan_id
2396 
2397             IF PG_DEBUG in ('Y', 'C') THEN
2398                 msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'Inside Generic Plan');
2399             END IF;
2400 
2401             -- 1873918: PDS-ODS fix
2402             -- add error handling
2403             BEGIN
2404                 --2854351
2405                 /*SELECT plan_id
2406                 -- INTO   x_plan_id  commented for bug 2392456
2407                 INTO   x_plan_info_rec.plan_id   -- changed for bug 2392456
2408                 FROM   msc_atp_plan_sn
2409                 WHERE  demand_class IS NULL
2410                 AND    sr_instance_id = p_instance_id
2411                 AND    organization_id = p_organization_id
2412                 AND    sr_inventory_item_id = p_inventory_item_id;
2413                 */
2414                 -- Bug 3086444, 3086366 : UNION ALL and rownum = 1 added
2415 
2416                 select  plan_id
2417                 bulk collect into l_plan_ids
2418                 from
2419                     (
2420                         SELECT  plan_id,Rank,completion_date
2421                         FROM    msc_atp_plan_sn
2422                         WHERE   demand_class IS NULL
2423                         AND     sr_instance_id = p_instance_id
2424                         AND     organization_id = p_organization_id
2425                         AND     sr_inventory_item_id = p_inventory_item_id
2426 
2427                         UNION ALL
2428 
2429                         SELECT  -200 PLAN_ID, 20, to_date(null)
2430                         from    msc_atp_plan_sn
2431                         WHERE   rownum = 1
2432                         ORDER  BY Rank asc,completion_date desc,plan_id asc
2433                     );
2434 
2435                 l_generic_plan := 1;
2436 
2437             EXCEPTION
2438                 WHEN no_data_found THEN
2439                     msc_sch_wb.atp_debug('No data Found exception');
2440                     -- x_plan_id := NULL;
2441                     x_plan_info_rec.plan_id := NULL;
2442                     l_generic_plan := 1;
2443 
2444             END;
2445 
2446         END IF;
2447 
2448         IF PG_DEBUG in ('Y', 'C') THEN
2449             msc_sch_wb.atp_debug('Total l_plan_ids count := ' || l_plan_ids.count);
2450         END IF;
2451 
2452         IF NVL(l_generic_plan, -1) = 1 THEN
2453             IF l_plan_ids.count = 0 THEN
2454             --- this is planned down time.
2455             -- The snapshot is being refreshed.
2456             -- Bug 2919892
2457             x_plan_info_rec.plan_id := -300;
2458             RETURN;
2459         ELSIF l_plan_ids.count = 1 THEN
2460             --ods to pds switch
2461             x_plan_info_rec.plan_id := NULL;
2462         ELSE
2463             FOR i in 1..l_plan_ids.count LOOP
2464                 IF l_plan_ids(i) <> -200 THEN
2465                     x_plan_info_rec.plan_id := l_plan_ids(i);
2466                     EXIT;
2467                 END IF;
2468             END LOOP;
2469          END IF; -- IF l_plan_ids.count = 0 THEN
2470     END IF; --- IF NVL(l_generic_plan, -1) THEN
2471 
2472     IF PG_DEBUG in ('Y', 'C') THEN
2473         msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'selected plan_id : '||x_plan_info_rec.plan_id);
2474     END IF;  -- changed for bug 2392456
2475     -- msc_sch_wb.atp_debug('selected plan_id : '||x_plan_id);  commented for bug 2392456
2476 
2477     -- 1873918 PDS-ODS fix
2478     -- IF x_plan_id is null, then we switch to ODS, x_plan_id = -1
2479     -- IF x_plan_id is not null, we check the completion date,
2480     --     if no completion date, we should raise error later. set
2481     --                 x_plan_id to null
2482     --     if we have completion date, then keep the x_plan_id
2483 
2484     -- IF x_plan_id IS NULL THEN    commented for bug 2392456
2485     IF x_plan_info_rec.plan_id IS NULL THEN  -- changed for bug 2392456
2486         -- bug 2119013
2487         -- do not switch for multiorg/multilevel ato line.
2488         -- only doing the switch if this line is not MLATO.
2489 
2490         IF PG_DEBUG in ('Y', 'C') THEN
2491             msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'MSC_ATP_PVT.G_CTO_LINE='||MSC_ATP_PVT.G_CTO_LINE);
2492         END IF;
2493 
2494         IF (NVL(MSC_ATP_PVT.G_CTO_LINE, 'N') = 'N') THEN
2495             IF PG_DEBUG in ('Y', 'C') THEN
2496                 msc_sch_wb.atp_debug('Get_Plan_Info: ' || 'x_plan_id is null, set to -1');
2497             END IF;
2498             -- x_plan_id := -1;  commented for bug 2392456
2499             x_plan_info_rec.plan_id := -1;  -- changed for  bug 2392456
2500         END IF;
2501 
2502     ELSE
2503 
2504         -- x_plan_id has some value
2505         BEGIN
2506 
2507             l_using_new_plan := 0;
2508             -- select designator type and assignment set
2509             -- bug 1384242, add the check to see if the plan has complete.
2510             -- we cannot do atp if the plan is running (not complete)
2511             -- this is because we only refresh snapshot when the plan finishes
2512             -- or inventory atp flag has been changed.  We don't refresh the
2513             -- snapshot when a plan is running.
2514             -- also select compile_designator for bug 2392456
2515 
2516             -- 24x7 : Removed join with msc_designators
2517 
2518             SELECT  plans.curr_assignment_set_id,
2519                     plans.compile_designator,
2520                     x_plan_info_rec.plan_id,
2521                     NVL(plans.summary_flag, 1),
2522                     NVL(plans.use_end_item_substitutions, 2),
2523                     NVL(plans.copy_plan_id,-1),
2524                     -- second plan for 24x7
2525                     plans2.curr_assignment_set_id,
2526                     plans2.compile_designator,
2527                     plans2.plan_id,
2528                     NVL(plans2.summary_flag, 1),
2529                     NVL(plans2.use_end_item_substitutions, 2),
2530                     NVL(plans2.copy_plan_id,-1),
2531                     -- Supplier Capacity and Lead Time (SCLT) Proj.
2532                     plans.sr_instance_id,
2533                     plans.organization_id,
2534                     trunc(plans.plan_start_date),
2535                     trunc(plans.cutoff_date),
2536                     plans.curr_cutoff_date,
2537                     DECODE(plans.plan_type, 4, 2,
2538                         DECODE(plans.daily_material_constraints, 1, 1,
2539                             DECODE(plans.daily_resource_constraints, 1, 1,
2540                                 DECODE(plans.weekly_material_constraints, 1, 1,
2541                                     DECODE(plans.weekly_resource_constraints, 1, 1,
2542                                         DECODE(plans.period_material_constraints, 1, 1,
2543                                             DECODE(plans.period_resource_constraints, 1, 1, 2)
2544                                               )
2545                                           )
2546                                       )
2547                                   )
2548                               )
2549                           ), -- 2859130
2550                     plans2.sr_instance_id,
2551                     plans2.organization_id,
2552                     trunc(plans2.plan_start_date),
2553                     trunc(plans2.cutoff_date),
2554                     plans2.curr_cutoff_date,
2555                     DECODE(plans2.plan_type, 4, 2,
2556                         DECODE(plans2.daily_material_constraints, 1, 1,
2557                             DECODE(plans2.daily_resource_constraints, 1, 1,
2558                                 DECODE(plans2.weekly_material_constraints, 1, 1,
2559                                     DECODE(plans2.weekly_resource_constraints, 1, 1,
2560                                         DECODE(plans2.period_material_constraints, 1, 1,
2561                                             DECODE(plans2.period_resource_constraints, 1, 1, 2)
2562                                               )
2563                                           )
2564                                       )
2565                                   )
2566                               )
2567                           ), -- 2859130      ,
2568                     plans.schedule_by,
2569                     plans2.schedule_by,
2570                     -- ship_rec_cal changes begin
2571                     NVL(plans.daily_material_constraints, 2),
2572                     --bug 4100346: For unconstrained plan always enforce purchasing lead time
2573                     DECODE(plans.plan_type, 4, 1,
2574                         DECODE(plans.daily_material_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2),
2575                             DECODE(plans.daily_resource_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2),
2576                                 DECODE(plans.weekly_material_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2),
2577                                     DECODE(plans.weekly_resource_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2),
2578                                         DECODE(plans.period_material_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2),
2579                                             DECODE(plans.period_resource_constraints, 1, NVL(plans.enforce_pur_lt_constraints, 2), 1)
2580                                               )
2581                                           )
2582                                       )
2583                                   )
2584                               )
2585                           ),
2586                     --NVL(plans.enforce_pur_lt_constraints, 2),
2587                     NVL(plans2.daily_material_constraints, 2),
2588                     --bug 4100346: For unconstrained plan always enforce purchasing lead time
2589                     --NVL(plans2.enforce_pur_lt_constraints, 2),
2590                     DECODE(plans2.plan_type, 4, 1,
2591                         DECODE(plans2.daily_material_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2),
2592                             DECODE(plans2.daily_resource_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2),
2593                                 DECODE(plans2.weekly_material_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2),
2594                                     DECODE(plans2.weekly_resource_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2),
2595                                         DECODE(plans2.period_material_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2),
2596                                             DECODE(plans2.period_resource_constraints, 1, NVL(plans2.enforce_pur_lt_constraints, 2), 1)
2597                                               )
2598                                           )
2599                                       )
2600                                   )
2601                               )
2602                           ),
2603                     -- ship_rec_cal changes end
2604                     -- ATP4drp changes begin
2605                     NVL(plans.plan_type, 1), -- Default is MRP plan
2606                     NVL(plans2.plan_type, 1),
2607                     plans.itf_horiz_days,   -- Obtain the ITF_HORIZ_DAYS
2608                     plans2.itf_horiz_days
2609                     -- ATP4drp changes end
2610             INTO    x_plan_info_rec.assignment_set_id,
2611                     x_plan_info_rec.plan_name,
2612                     x_plan_info_rec.plan_id,
2613                     --l_summary_flag,
2614                     x_plan_info_rec.summary_flag,
2615                     --MSC_ATP_PVT.G_PLAN_SUBST_FLAG,
2616                     x_plan_info_rec.subst_flag,
2617                     x_plan_info_rec.copy_plan_id,
2618                     l_plan_info_rec.assignment_set_id,
2619                     l_plan_info_rec.plan_name,
2620                     l_plan_info_rec.plan_id,
2621                     l_plan_info_rec.summary_flag,
2622                     l_plan_info_rec.subst_flag,
2623                     l_plan_info_rec.copy_plan_id,
2624                     -- Supplier Capacity and Lead Time (SCLT) Proj.
2625                     x_plan_info_rec.sr_instance_id,
2626                     x_plan_info_rec.organization_id,
2627                     x_plan_info_rec.plan_start_date,
2628                     x_plan_info_rec.plan_cutoff_date,
2629                     x_plan_info_rec.curr_cutoff_date,
2630                     x_plan_info_rec.optimized_plan, -- 2859130
2631                     l_plan_info_rec.sr_instance_id,
2632                     l_plan_info_rec.organization_id,
2633                     l_plan_info_rec.plan_start_date,
2634                     l_plan_info_rec.plan_cutoff_date,
2635                     l_plan_info_rec.curr_cutoff_date,
2636                     l_plan_info_rec.optimized_plan,-- 2859130
2637                     --plan by request date changes begin
2638                     x_plan_info_rec.schedule_by_date_type,
2639                     l_plan_info_rec.schedule_by_date_type,
2640                     --plan by request date changes end
2641                     -- ship_rec_cal changes begin
2642                     x_plan_info_rec.enforce_sup_capacity,
2643                     x_plan_info_rec.enforce_pur_lead_time,
2644                     l_plan_info_rec.enforce_sup_capacity,
2645                     l_plan_info_rec.enforce_pur_lead_time,
2646                     -- ship_rec_cal changes end
2647                     -- ATP4drp changes begin
2648                     x_plan_info_rec.plan_type,
2649                     l_plan_info_rec.plan_type,
2650                     x_plan_info_rec.itf_horiz_days,
2651                     l_plan_info_rec.itf_horiz_days
2652                     -- ATP4drp changes end
2653             FROM    msc_plans plans,
2654                     msc_plans plans2
2655             WHERE   plans.plan_id = x_plan_info_rec.plan_id
2656             AND     plans.plan_completion_date is not null
2657             AND     plans.data_completion_date is not null
2658             and     plans.plan_id = plans2.copy_plan_id (+);
2659 
2660 
2661             IF PG_DEBUG in ('Y', 'C') THEN
2662                 msc_sch_wb.atp_debug('G_SYNC_ATP_CHECK  := '|| MSC_ATP_PVT.G_SYNC_ATP_CHECK);
2663                 msc_sch_wb.atp_debug (' ---- ');
2664                 msc_sch_wb.atp_debug ('Query Output : Old Plan ID : ' || x_plan_info_rec.plan_id);
2665                 msc_sch_wb.atp_debug (' plan name:     : ' || x_plan_info_rec.plan_name) ;
2666                 msc_sch_wb.atp_debug (' assign_set_id  : ' || x_plan_info_rec.assignment_set_id) ;
2667                 msc_sch_wb.atp_debug (' summary_flag   : ' || x_plan_info_rec.summary_flag);
2668                 msc_sch_wb.atp_debug (' substitition   : ' || x_plan_info_rec.subst_flag);
2669                 msc_sch_wb.atp_debug (' copy_plan_id   : ' || x_plan_info_rec.copy_plan_id);
2670                 msc_sch_wb.atp_debug (' start date     : ' || x_plan_info_rec.plan_start_date);
2671                 msc_sch_wb.atp_debug (' cutoff date    : ' || x_plan_info_rec.plan_cutoff_date);
2672                 msc_sch_wb.atp_debug (' curr_cutoff dt : ' || x_plan_info_rec.curr_cutoff_date);
2673                 msc_sch_wb.atp_debug (' sr_instance_id : ' || x_plan_info_rec.sr_instance_id);
2674                 msc_sch_wb.atp_debug (' org_id         : ' || x_plan_info_rec.organization_id);
2675                 -- ship_rec_cal changes begin
2676                 msc_sch_wb.atp_debug (' enforce_sup_capacity    : ' || x_plan_info_rec.enforce_sup_capacity);
2677                 msc_sch_wb.atp_debug (' enforce_pur_lead_time   : ' || x_plan_info_rec.enforce_pur_lead_time);
2678                 -- ship_rec_cal changes end
2679                 -- ATP4drp changes begin
2680                 msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2681                 msc_sch_wb.atp_debug (' optimized_plan   : ' || x_plan_info_rec.optimized_plan);
2682                 msc_sch_wb.atp_debug (' plan_type        : ' || x_plan_info_rec.plan_type);
2683                 msc_sch_wb.atp_debug (' ITF_HORIZ_DAYS   : ' || x_plan_info_rec.itf_horiz_days);
2684                 -- ATP4drp changes end
2685                 msc_sch_wb.atp_debug (' ---- ');
2686                 msc_sch_wb.atp_debug ('Query Output : New Plan ID : ' || l_plan_info_rec.plan_id);
2687                 msc_sch_wb.atp_debug (' plan name:     : ' || l_plan_info_rec.plan_name) ;
2688                 msc_sch_wb.atp_debug (' assign_set_id  : ' || l_plan_info_rec.assignment_set_id) ;
2689                 msc_sch_wb.atp_debug (' summary_flag   : ' || l_plan_info_rec.summary_flag);
2690                 msc_sch_wb.atp_debug (' substitition   : ' || l_plan_info_rec.subst_flag);
2691                 msc_sch_wb.atp_debug (' copy_plan_id   : ' || l_plan_info_rec.copy_plan_id);
2692                 msc_sch_wb.atp_debug (' start date     : ' || l_plan_info_rec.plan_start_date);
2693                 msc_sch_wb.atp_debug (' cutoff date    : ' || l_plan_info_rec.plan_cutoff_date);
2694                 msc_sch_wb.atp_debug (' curr_cutoff dt : ' || l_plan_info_rec.curr_cutoff_date);
2695                 msc_sch_wb.atp_debug (' sr_instance_id : ' || l_plan_info_rec.sr_instance_id);
2696                 msc_sch_wb.atp_debug (' org_id         : ' || l_plan_info_rec.organization_id);
2697                 -- ship_rec_cal changes begin
2698                 msc_sch_wb.atp_debug (' enforce_sup_capacity    : ' || l_plan_info_rec.enforce_sup_capacity);
2699                 msc_sch_wb.atp_debug (' enforce_pur_lead_time   : ' || l_plan_info_rec.enforce_pur_lead_time);
2700                 msc_sch_wb.atp_debug (' ---- ');
2701                 -- ship_rec_cal changes end
2702                 -- ATP4drp changes begin
2703                 msc_sch_wb.atp_debug (' optimized_plan   : ' || l_plan_info_rec.optimized_plan);
2704                 msc_sch_wb.atp_debug (' plan_type        : ' || l_plan_info_rec.plan_type);
2705                 msc_sch_wb.atp_debug (' ITF_HORIZ_DAYS   : ' || l_plan_info_rec.itf_horiz_days);
2706                 msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2707                 -- ATP4drp changes end
2708                 msc_sch_wb.atp_debug (' ---- ');
2709             END IF;
2710 
2711             if (NVL(MSC_ATP_PVT.G_SYNC_ATP_CHECK, 'N') = 'Y') then
2712                 -- this is a sync call
2713                 l_using_new_plan := 1;
2714                 if PG_DEBUG in ('Y','C') then
2715                     msc_sch_wb.atp_debug ('Sync process. Switching plan to '|| l_plan_info_rec.plan_id);
2716                 end if;
2717                 if (l_plan_info_rec.plan_id IS  NULL) then
2718                     if PG_DEBUG in ('Y','C') then
2719                         msc_sch_wb.atp_debug ('Cannot find new plan during SYNC ATP check call');
2720                         msc_sch_wb.atp_debug ('Going by old plan to account for extended sync');
2721                     end if;
2722                     l_plan_info_rec := x_plan_info_rec;
2723                 end if;
2724 
2725                 -- copy new plan data to the old plan
2726                 x_plan_info_rec := l_plan_info_rec;
2727             end if;
2728 
2729         EXCEPTION
2730             WHEN NO_DATA_FOUND THEN
2731                 x_plan_info_rec.plan_id             := NULL;
2732                 x_plan_info_rec.plan_name           := NULL;
2733                 x_plan_info_rec.assignment_set_id   := NULL;
2734                 -- Additional Fields for Supplier Capacity and Lead Time (SCLT) Project.
2735                 x_plan_info_rec.plan_start_date     := null;
2736                 x_plan_info_rec.plan_cutoff_date    := null;
2737                 x_plan_info_rec.sr_instance_id      := null;
2738                 x_plan_info_rec.organization_id     := null;
2739                 x_plan_info_rec.curr_cutoff_date    := null;
2740                 x_plan_info_rec.optimized_plan      := 2; -- 2859130
2741                 -- ATP4drp plan_type is meaningless when plan_id is NULL
2742                 x_plan_info_rec.plan_type           := NULL;
2743         END;
2744     END IF;
2745 
2746     -- Assign global and local variables
2747     l_summary_flag := x_plan_info_rec.summary_flag;
2748     MSC_ATP_PVT.G_PLAN_SUBST_FLAG := x_plan_info_rec.subst_flag;
2749 
2750     if PG_DEBUG in ('Y','C') then
2751         msc_sch_wb.atp_debug ('Plan_ID after processing switches is : ' || x_plan_info_rec.plan_id);
2752     end if;
2753 
2754     -- ATP4drp
2755     IF NVL(x_plan_info_rec.plan_type, 1) = 5 THEN -- DRP plan then re-set variables
2756         MSC_ATP_PVT.G_ALLOCATED_ATP := 'N';
2757         l_time_phased_atp := 'N';
2758         IF PG_DEBUG in ('Y','C') then
2759             msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2760             msc_sch_wb.atp_debug('DRP Plan, re-setting variables');
2761         END IF;
2762     ELSE
2763         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
2764         l_time_phased_atp := p_time_phased_atp;
2765     END IF;
2766     IF PG_DEBUG in ('Y','C') then
2767         msc_sch_wb.atp_debug('Value of MSC_ATP_PVT.G_ALLOCATED_ATP :' || MSC_ATP_PVT.G_ALLOCATED_ATP);
2768         msc_sch_wb.atp_debug('Value of l_time_phased_atp :' || l_time_phased_atp);
2769         msc_sch_wb.atp_debug('Value of p_time_phased_atp :' || p_time_phased_atp);
2770     END IF;
2771     -- End ATP4drp
2772     IF x_plan_info_rec.plan_id > 0  THEN
2773 
2774         IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
2775             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2776             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
2777             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
2778 
2779             IF l_summary_flag = MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN THEN       -- Summary / pre-allocation was never run.
2780                 x_plan_info_rec.plan_id := -200;
2781                 if PG_DEBUG in ('Y','C') then
2782                     msc_sch_wb.atp_debug('Pre-allocation process needs to be run');
2783                 end if;
2784             ELSIF l_summary_flag = MSC_POST_PRO.G_SF_PREALLOC_RUNNING THEN   -- Pre-allocation is running.
2785                 x_plan_info_rec.plan_id := -100;
2786                 if PG_DEBUG in ('Y','C') then
2787                     msc_sch_wb.atp_debug('Pre-allocation and/or PF bucketting process is running');
2788                 end if;
2789             END IF;
2790         -- Summary enhancement: Do not reset summary flag so that copy SOs can be created
2791         -- this check will be made again just before building period data to decide whether summary should be used
2792         /*
2793         ELSIF NVL(MSC_ATP_PVT.G_SUMMARY_FLAG, 'N') = 'Y' THEN
2794             IF l_summary_flag = 1 THEN
2795                 -- set the summary flag
2796                 MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
2797                 if PG_DEBUG in ('Y','C') then
2798                 msc_sch_wb.atp_debug('Switch from summary to details tables');
2799                 end if;
2800             ELSIF l_summary_flag = 2 THEN*/
2801         ELSIF l_summary_flag IN (MSC_POST_PRO.G_SF_SUMMARY_NOT_RUN, MSC_POST_PRO.G_SF_PREALLOC_RUNNING) THEN
2802             -- This means that PF bucketting has not started or is still in progress
2803             IF nvl(l_time_phased_atp,'N') = 'Y' THEN -- ATP4drp Use local variable instead of parameter
2804                 x_plan_info_rec.plan_id := -100;  --changed for 2392456
2805                 if PG_DEBUG in ('Y','C') then
2806                     msc_sch_wb.atp_debug('PF bucketting process is running or was never run');
2807                 end if;
2808             END IF;
2809         END IF;
2810 
2811 
2812         -- 24x7 Specific Checks
2813 
2814         if (l_using_new_plan = 0) and (l_plan_info_rec.plan_id is not NULL)
2815                                    and (l_plan_info_rec.plan_id > 0) then
2816             -- checks against new plan when apt against old
2817             if l_plan_info_rec.summary_flag = MSC_ATP_24x7.G_SF_SYNC_DOWNTIME then
2818                 -- downtime during ATP sync process
2819                 x_plan_info_rec.plan_id := -300;
2820                 if PG_DEBUG in ('Y','C') then
2821                     msc_sch_wb.atp_debug ('ATP Downtime based on new plan');
2822                 end if;
2823             end if;
2824         end if;
2825     END IF;
2826 
2827     IF PG_DEBUG in ('Y', 'C') THEN
2828         msc_sch_wb.atp_debug('Get_Plan_Info : ' || 'x_plan_info_rec.plan_id - ' || x_plan_info_rec.plan_id);
2829         msc_sch_wb.atp_debug('**************** Get_Plan_Info End ***************');
2830     END IF;
2831 
2832 END Get_Plan_Info;
2833 
2834 PROCEDURE Atp_Backward_Consume(
2835         p_atp_qty         IN OUT  NoCopy MRP_ATP_PUB.number_arr
2836 )
2837 IS
2838 i NUMBER;
2839 j NUMBER;
2840 
2841 BEGIN
2842 
2843     IF PG_DEBUG in ('Y', 'C') THEN
2844        msc_sch_wb.atp_debug('*******Begin Atp_Backward_Consume Procedure******');
2845     END IF;
2846 
2847     -- this for loop will do backward consumption
2848     FOR i in 2..p_atp_qty.COUNT LOOP
2849 
2850         -- backward consumption when neg atp quantity occurs
2851         IF (p_atp_qty(i) < 0 ) THEN
2852             j := i - 1;
2853             WHILE ((j>0) and (p_atp_qty(j)>=0))  LOOP
2854                 IF (p_atp_qty(j) = 0) THEN
2855                     --  backward one more period
2856                     j := j-1 ;
2857                 ELSE
2858                     IF (p_atp_qty(j) + p_atp_qty(i) < 0) THEN
2859                         -- not enough to cover the shortage
2860                         p_atp_qty(i) := p_atp_qty(i) + p_atp_qty(j);
2861                         p_atp_qty(j) := 0;
2862                         j := j-1;
2863                     ELSE
2864                         -- enough to cover the shortage
2865                         p_atp_qty(j) := p_atp_qty(j) + p_atp_qty(i);
2866                         p_atp_qty(i) := 0;
2867                         j := -1;
2868                     END IF;
2869                 END IF;
2870             END LOOP;
2871         END IF;
2872     END LOOP;
2873 
2874     IF PG_DEBUG in ('Y', 'C') THEN
2875        msc_sch_wb.atp_debug('*******End Atp_Backward_Consume Procedure******');
2876     END IF;
2877 
2878 END Atp_Backward_Consume;
2879 
2880 
2881 PROCEDURE Atp_Accumulate(
2882         p_atp_qty         IN OUT  NoCopy MRP_ATP_PUB.number_arr
2883 )
2884 IS
2885 i NUMBER;
2886 
2887 BEGIN
2888 
2889     IF PG_DEBUG in ('Y', 'C') THEN
2890        msc_sch_wb.atp_debug('*******Begin Atp_Accumulate Procedure***********');
2891     END IF;
2892     -- this for loop will do the acculumation
2893     FOR i in 2..p_atp_qty.COUNT LOOP
2894         -- accumulation (only the surplus)
2895 
2896         -- 1956037: do accumulation for neg quantity as well
2897         -- IF (p_atp_qty(i-1) > 0) THEN
2898           p_atp_qty(i) := p_atp_qty(i) + p_atp_qty(i-1);
2899         -- END IF;
2900     END LOOP;
2901 
2902     IF PG_DEBUG in ('Y', 'C') THEN
2903        msc_sch_wb.atp_debug('**********End Atp_Accumulate Procedure***********');
2904     END IF;
2905 END Atp_Accumulate;
2906 
2907 PROCEDURE Add_Coproducts(
2908     p_plan_id           IN NUMBER,
2909     p_instance_id       IN NUMBER,
2910     p_org_id            IN NUMBER,
2911     p_inv_item_id       IN NUMBER,
2912     p_request_date      IN DATE,
2913     p_demand_class      IN VARCHAR2,
2914     p_assembly_qty      IN NUMBER,
2915     p_parent_pegging_id IN NUMBER,
2916     -- 2869830
2917     p_rounding_flag     IN NUMBER,
2918     p_refresh_number    IN NUMBER,  -- For summary enhancement
2919     p_disposition_id    IN NUMBER -- bug 3766179
2920 )
2921 IS
2922 
2923 TYPE ITEM_COPRODUCTS is RECORD (
2924                Inventory_item_id MRP_ATP_PUB.number_Arr,
2925                Quantity     MRP_ATP_PUB.number_arr);
2926 l_coproducts_rec  ITEM_COPRODUCTS;
2927 --l_supply_usage  number;
2928 --l_coproducts_flag varchar(4);
2929 l_transaction_id NUMBER;
2930 l_return_status VARCHAR2(10);
2931 l_pegging_id number;
2932 l_process_seq_id NUMBER; -- rajjain 02/19/2003 Bug 2788302
2933 
2934 -- 2869830
2935 l_coprod_qty    number;
2936 
2937 --3766179
2938 l_supply_rec_type   MSC_ATP_DB_UTILS.Supply_Rec_typ;
2939 BEGIN
2940         IF PG_DEBUG in ('Y', 'C') THEN
2941            msc_sch_wb.atp_debug('Add_Coproducts: ' || '******* Start ADD_COPRODUCT ********');
2942            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_plan_id := ' || p_plan_id);
2943            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_instance_id := ' || p_instance_id);
2944            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_org_id := ' || p_org_id);
2945            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_inv_item_id := ' || p_inv_item_id);
2946            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_request_date := ' || p_request_date);
2947            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_demand_class := ' || p_demand_class);
2948            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_assembly_qty := ' || p_assembly_qty);
2949            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_parent_pegging_id := ' || p_parent_pegging_id);
2950            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_rounding_flag := ' || p_rounding_flag);
2951            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_refresh_number := ' || p_refresh_number);
2952            msc_sch_wb.atp_debug('Add_Coproducts: ' || ' p_disposition_id := ' || p_disposition_id);
2953            msc_sch_wb.atp_debug('Add_Coproducts: ' || 'p_parent_pegging_id := ' || p_parent_pegging_id);
2954         END IF;
2955 	------ determine the coproducts and their quantities
2956         -- Tuned the query for performance bug 2484964
2957 	SELECT inventory_item_id,quantity
2958 	BULK COLLECT INTO l_coproducts_rec.inventory_item_id,
2959 							l_coproducts_rec.quantity
2960 	FROM (SELECT MBC.inventory_item_id,
2961 	            ABS(NVL(MBC.usage_quantity,1) * p_assembly_qty/
2962                 NVL(MB.assembly_quantity,1)) quantity
2963   			FROM  MSC_SYSTEM_ITEMS I,
2964            		MSC_BOMS MB,
2965               	MSC_BOM_COMPONENTS MBC,
2966               	MSC_CALENDAR_DATES C,
2967               	MSC_TRADING_PARTNERS TP
2968         	WHERE I.plan_id = p_plan_id and
2969                I.sr_instance_id =  p_instance_id and
2970                I.organization_id = p_org_id and
2971                I.sr_inventory_item_id = p_inv_item_id and
2972                --MB.plan_id = p_plan_id and
2973                MB.plan_id = I.plan_id and
2974                --MB.assembly_item_id = I.inventory_item_id and
2975                MB.assembly_item_id = I.inventory_item_id and
2976                --MB.organization_id  = p_org_id and
2977                MB.organization_id  = I.organization_id and
2978                --MB.sr_instance_id =  p_instance_id and
2979                MB.sr_instance_id =  I.sr_instance_id and
2980                MB.bill_sequence_id = MBC.bill_sequence_id and
2981                MBC.plan_id = MB.plan_id and
2982                --MBC.organization_id  = p_org_id and
2983                --MBC.sr_instance_id =  p_instance_id and
2984                MBC.organization_id  = MB.organization_id and
2985                MBC.sr_instance_id =  MB.sr_instance_id and
2986                MBC.usage_quantity < 0 and
2987                TRUNC(NVL(MBC.disable_date , C.calendar_date + 1)) >
2988                	TRUNC(C.Calendar_date) and
2989                TRUNC(MBC.effectivity_date)<=
2990                  	TRUNC(GREATEST(sysdate, C.calendar_date)) and
2991                C.calendar_date = trunc(p_request_date) and
2992                --C.sr_instance_id = p_instance_id and
2993                C.sr_instance_id = MBC.sr_instance_id and
2994                C.calendar_code = TP.calendar_code and
2995                C.exception_set_id = TP.calendar_exception_set_id and
2996                --TP.sr_instance_id = p_instance_id and
2997                --TP.sr_tp_id =  p_org_id and
2998                TP.sr_instance_id = MBC.sr_instance_id and
2999                TP.sr_tp_id =  MBC.organization_id and
3000                TP.partner_type = 3 );
3001     IF PG_DEBUG in ('Y', 'C') THEN
3002        msc_sch_wb.atp_debug('Add_Coproducts: ' || 'No of Coproducts = ' || l_coproducts_rec.inventory_item_id.count);
3003     END IF;
3004 	FOR rec_count in 1..l_coproducts_rec.inventory_item_id.count LOOP
3005             IF PG_DEBUG in ('Y', 'C') THEN
3006                msc_sch_wb.atp_debug('Add_Coproducts: ' || 'coproduct id = ' ||l_coproducts_rec.inventory_item_id(rec_count));
3007                msc_sch_wb.atp_debug('Add_Coproducts: ' || 'Coproduct quantity ='||  l_coproducts_rec.quantity(rec_count));
3008                msc_sch_wb.atp_debug('Add_Coproducts: ' || 'coproduct date = ' || p_request_date);
3009             END IF;
3010             /* rajjain 02/19/2003 Bug 2788302 Begin
3011              * get Process Sequence ID */
3012             l_process_seq_id := MSC_ATP_FUNC.get_process_seq_id(
3013                                   p_plan_id,
3014                                   l_coproducts_rec.inventory_item_id(rec_count),
3015                                   p_org_id,
3016                                   p_instance_id,
3017                                   p_request_date
3018                                   );
3019             -- rajjain 02/19/2003 Bug 2788302 End
3020 
3021             IF nvl(p_rounding_flag, 2) = 1 THEN
3022                l_coprod_qty := FLOOR(l_coproducts_rec.quantity(rec_count));
3023             ELSE
3024                l_coprod_qty := l_coproducts_rec.quantity(rec_count);
3025             END IF;
3026 
3027             --bug 3766179: call new procedure add_supplies instead
3028             /*
3029    	    MSC_ATP_DB_UTILS.Add_Planned_Order(
3030                                 p_instance_id,
3031                                 p_plan_id,
3032                                 l_coproducts_rec.inventory_item_id(rec_count),
3033                                 p_org_id,
3034                                 p_request_date,
3035                                 l_coprod_qty, -- 2869830
3036                                 --l_coproducts_rec.quantity(rec_count),
3037                                 null,
3038                                 null,
3039                                 p_demand_class,
3040                                 -- rajjain 02/19/2003 Bug 2788302 Begin
3041                                 p_org_id,
3042                                 p_instance_id,
3043                                 l_process_seq_id,
3044                                 -- rajjain 02/19/2003 Bug 2788302 End
3045                                 p_refresh_number,   -- For summary enhancement
3046                                 -- ship_rec_cal changes begin
3047                                 null,
3048                                 null,
3049                                 null,
3050                                 null,
3051                                 null,
3052                                 null,       -- Bug 3241766
3053                                 null,       -- Bug 3241766
3054                                 null,
3055                                 -- ship_rec_cal changes end
3056                                 l_transaction_id,
3057                                 l_return_status
3058                              	);
3059 
3060             */
3061 
3062             l_supply_rec_type.instance_id := p_instance_id;
3063             l_supply_rec_type.plan_id := p_plan_id;
3064             l_supply_rec_type.inventory_item_id := l_coproducts_rec.inventory_item_id(rec_count);
3065             l_supply_rec_type.organization_id := p_org_id;
3066             l_supply_rec_type.schedule_date :=p_request_date;
3067             l_supply_rec_type.order_quantity := l_coprod_qty;
3068             l_supply_rec_type.supplier_id  := null;
3069             l_supply_rec_type.supplier_site_id := null;
3070             l_supply_rec_type.demand_class := p_demand_class;
3071             l_supply_rec_type.source_organization_id := p_org_id;
3072             l_supply_rec_type.source_sr_instance_id := p_instance_id;
3073             l_supply_rec_type.process_seq_id :=  l_process_seq_id;
3074             l_supply_rec_type.refresh_number := p_refresh_number;
3075             l_supply_rec_type.shipping_cal_code := null;
3076             l_supply_rec_type.receiving_cal_code := null;
3077             l_supply_rec_type.intransit_cal_code:= null;
3078             l_supply_rec_type.new_ship_date := null;
3079             l_supply_rec_type.new_dock_date := null;
3080             l_supply_rec_type.start_date := null;
3081             l_supply_rec_type.order_date := null;
3082             l_supply_rec_type.ship_method := null;
3083             l_supply_rec_type.request_item_id  := null;
3084             l_supply_rec_type.atf_date := null;
3085 
3086             l_supply_rec_type.firm_planned_type := 2;
3087             l_supply_rec_type.disposition_status_type := 1;
3088             l_supply_rec_type.record_source := 2;
3089             l_supply_rec_type.supply_type := 17; --planned order coproduct
3090             l_supply_rec_type.disposition_id := p_disposition_id;
3091 
3092             MSC_ATP_DB_UTILS.ADD_SUPPLIES(l_supply_rec_type);
3093 
3094             l_transaction_id := l_supply_rec_type.transaction_id;
3095             l_return_status :=  l_supply_rec_type.return_status;
3096 
3097             IF PG_DEBUG in ('Y', 'C') THEN
3098                msc_sch_wb.atp_debug('Add_Coproducts: ' || 'l_transaction_id := ' || l_transaction_id);
3099                msc_Sch_wb.atp_debug('Add_Coproducts: ' || 'Add pegging for coproducts where pegging id = 4');
3100             END IF;
3101 
3102             INSERT into mrp_atp_details_temp
3103                  (session_id,
3104                   order_line_id,
3105 	          pegging_id,
3106                   parent_pegging_id,
3107                   atp_level,
3108                   record_type,
3109                   organization_id,
3110                   organization_code,
3111                   identifier1,
3112                   identifier2,
3113                   identifier3,
3114 		  inventory_item_id,
3115                   inventory_item_name,
3116                   resource_id,
3117                   resource_code,
3118                   department_id,
3119                   department_code,
3120                   supplier_id,
3121                   supplier_name,
3122  		  supplier_site_id,
3123                   supplier_site_name,
3124 	          scenario_id,
3125 		  source_type,
3126 		  supply_demand_source_type,
3127                   supply_demand_quantity,
3128 		  supply_demand_type,
3129 		  supply_demand_date,
3130                   end_pegging_id,
3131                   constraint_flag,
3132                   allocated_quantity, -- 1527660
3133                   number1,
3134                   char1,
3135 		  component_identifier,
3136                   -- resource batching
3137                   batchable_flag,
3138                   summary_flag
3139                 -- dsting 2535568 purge temp table fix
3140                 , creation_date
3141                 , created_by
3142                 , last_update_date
3143                 , last_updated_by
3144                 , last_update_login
3145 		)
3146 	    VALUES
3147                  (MSC_ATP_PVT.G_SESSION_ID,
3148                   MSC_ATP_PVT.G_ORDER_LINE_ID,
3149                   msc_full_pegging_s.nextval,
3150                   p_parent_pegging_id,
3151                   1,
3152                   4,
3153                   p_org_id,
3154                   null,
3155                   p_instance_id,
3156                   p_plan_id,
3157                   l_transaction_id,
3158                   l_coproducts_rec.inventory_item_id(rec_count),
3159                   null,
3160                   null,
3161                   null,
3162                   null,
3163                   null,
3164                   null,
3165                   null,
3166                   null,
3167                   null,
3168                   1,
3169 		  2,
3170                   2,
3171                   l_coproducts_rec.quantity(rec_count),
3172                   2,
3173                   p_request_date,
3174                   NVL(MSC_ATP_PVT.G_DEMAND_PEGGING_ID, msc_full_pegging_s.currval),
3175                   null,
3176                   null, -- 1527660
3177                   null,
3178                   null,
3179 		  null,
3180                   null,
3181                   MSC_ATP_PVT.G_SUMMARY_FLAG
3182 		  -- dsting  2535568 purge temp table fix
3183 		  , sysdate 		-- creation_date
3184 		  , FND_GLOBAL.USER_ID  -- created_by
3185 		  , sysdate 		-- last_update_date
3186 		  , FND_GLOBAL.USER_ID  -- update_by
3187 		  , FND_GLOBAL.USER_ID	-- login_by
3188 		)
3189               RETURNING pegging_id INTO l_pegging_id;
3190               IF PG_DEBUG in ('Y', 'C') THEN
3191                  msc_sch_wb.atp_debug('Add_Coproducts: ' || ' rec_count : = ' || rec_count);
3192                  msc_sch_wb.atp_debug('Add_Coproducts: ' || ' l_pegging_id : = ' || l_pegging_id);
3193               END IF;
3194 	END LOOP;
3195 	IF PG_DEBUG in ('Y', 'C') THEN
3196 	   msc_sch_wb.atp_debug('**********End ADD_Coproducts Procedure***********');
3197 	END IF;
3198 
3199 END ADD_COPRODUCTS;
3200 
3201 
3202 -- ========================================================================
3203 -- This procedure inserts information source org information for the request
3204 -- items  into mrp_atp_schedule_temp for Supply Chain ATP.
3205 -- ========================================================================
3206 
3207 PROCEDURE get_Supply_Sources(
3208 			     x_session_id         IN      NUMBER,
3209 			     x_sr_instance_id     IN      NUMBER,
3210 			     x_assignment_set_id  IN      NUMBER,
3211 			     x_plan_id            IN      NUMBER,
3212 			     x_calling_inst       IN      VARCHAR2,
3213 			     x_ret_status         OUT     NoCopy VARCHAR2,
3214 			     x_error_mesg         OUT     NoCopy VARCHAR2,
3215                              p_node_id            IN    NUMBER DEFAULT null --bug3610706
3216 			     )
3217   IS
3218      l_return_status       VARCHAR2(100);
3219      l_request_item_id     NUMBER := NULL;
3220      l_sources             mrp_atp_pvt.atp_source_typ;
3221      l_item_arr            mrp_atp_pub.number_arr := mrp_atp_pub.number_arr(1);
3222      l_item_sourcing_rec   MSC_ATP_CTO.Item_Sourcing_Info_Rec;
3223      l_other_cols          order_sch_wb.other_cols_typ;
3224      l_item_id             NUMBER;
3225      l_sr_instance_id      NUMBER;
3226      l_organization_id     NUMBER;
3227      l_customer_id         NUMBER;
3228      l_customer_site_id    NUMBER;
3229      l_dblink              VARCHAR2(128);   -- m2a link
3230      l_dynstring           VARCHAR2(129) := NULL;
3231      l_intransit_time	   NUMBER;
3232      l_ship_method	   VARCHAR2(30);
3233      l_ship_method_text	   VARCHAR2(80);
3234      sql_stmt              VARCHAR2(32000);
3235      j                     NUMBER;
3236      l_to_location_id      NUMBER;
3237      l_default_flag        NUMBER;
3238      l_cursor              integer;
3239      rows_processed  NUMBER;
3240      l_from_location_id    NUMBER;
3241      l_region_level          NUMBER;
3242      l_region_id             NUMBER;
3243      l_om_source_org         NUMBER;
3244 
3245 CURSOR  SH_METHODS(p_from_location_id NUMBER,
3246                           p_source_instance_id NUMBER,
3247                           p_to_location_id NUMBER,
3248                           p_instance_id NUMBER) IS
3249       SELECT msim.intransit_time,
3250              msim.ship_method,
3251              msim.default_flag
3252       FROM  msc_interorg_ship_methods  msim
3253       WHERE  msim.plan_id = -1
3254       AND msim.from_location_id = p_from_location_id
3255       AND msim.sr_instance_id = p_source_instance_id
3256       AND msim.to_location_id = p_to_location_id
3257       AND msim.sr_instance_id2 = p_instance_id
3258       AND msim.to_region_id is null;
3259 
3260 -- cnazarma  c_region_level cursor is needed to get
3261 -- the most  specific region_level
3262 
3263 CURSOR  c_region_level (p_from_location_id NUMBER,
3264                      p_from_instance_id NUMBER,
3265                      p_to_instance_id NUMBER,
3266                      p_session_id NUMBER,
3267                      p_partner_site_id NUMBER)   IS
3268 SELECT  ( (10 * (10 - mrt.region_type)) +
3269          DECODE(mrt.zone_flag, 'Y', 1, 0)) region_level, mrt.region_id
3270 FROM    msc_interorg_ship_methods mism,
3271         msc_regions_temp mrt
3272 WHERE   mism.plan_id = -1
3273 AND     mism.from_location_id = p_from_location_id
3274 AND     mism.sr_instance_id = p_from_instance_id
3275 AND     mism.sr_instance_id2 = p_to_instance_id
3276 AND     mism.to_region_id = mrt.region_id
3277 AND     mrt.session_id = p_session_id
3278 AND     mrt.partner_site_id = p_partner_site_id
3279 ORDER BY 1;
3280 
3281      TYPE mastcurtyp IS REF CURSOR;
3282      mast_cursor mastcurtyp;
3283 BEGIN
3284 
3285 /*IF PG_DEBUG in ('Y', 'C') THEN
3286    msc_sch_wb.atp_debug('get_Supply_Sources: ' || 'Inside of atp_proc ');
3287    msc_sch_wb.atp_debug('get_Supply_Sources: ' || 'p_node_id ' || p_node_id);
3288 END IF;  */
3289 /*----------------------------------------------------------------------------
3290   -- This procedure will be on the ATP server and will be called thru
3291    -- the atp link from the client.
3292 ----------------------------------------------------------------------------*/
3293 msc_sch_wb.set_session_id(x_session_id); -- Set session ID for debug file.
3294   --bug3610706 start
3295     IF PG_DEBUG in ('Y', 'C') THEN
3296      msc_sch_wb.atp_debug('get_Supply_Sources: ' || 'p_node_id ' || p_node_id || ':' || x_session_id);
3297      msc_sch_wb.atp_debug('get_Supply_Sources: ' || 'x_sr_instance_id ' || x_sr_instance_id);
3298     END IF;
3299     l_dblink := null;
3300     IF p_node_id is not null THEN
3301        BEGIN
3302        --Bug3765793 adding trim functions to remove spaces from db_link
3303          SELECT ltrim(rtrim(M2A_DBLINK))
3304          INTO   l_dblink
3305          FROM   msc_apps_instance_nodes
3306          WHERE  instance_id = x_sr_instance_id and
3307                 node_id     = p_node_id;
3308          IF PG_DEBUG in ('Y', 'C') THEN
3309              msc_sch_wb.atp_debug('RAC instance');
3310          END IF;
3311        EXCEPTION
3312         WHEN OTHERS THEN
3313             l_dblink := null;
3314             IF PG_DEBUG in ('Y', 'C') THEN
3315              msc_sch_wb.atp_debug('Did not get records in rac case');
3316             END IF;
3317        END;
3318     END IF;
3319   /* Bug 2085071 Fix */
3320     IF l_dblink is null THEN
3321      BEGIN
3322      --Bug3765793 adding trim functions to remove spaces from db_link
3323       SELECT  ltrim(rtrim(M2A_DBLINK))
3324 	INTO   l_dblink
3325 	FROM   msc_apps_instances
3326 	WHERE  instance_id = x_sr_instance_id;
3327      EXCEPTION
3328       WHEN no_data_found THEN
3329 	 NULL;
3330      END;
3331     END IF;
3332   --bug3610706 end
3333    IF l_dblink IS NOT NULL AND x_calling_inst = 'APPS' THEN
3334       l_dynstring := '@'||l_dblink;
3335    END IF;
3336 
3337    IF PG_DEBUG in ('Y', 'C') THEN
3338       msc_sch_wb.atp_debug('get_Supply_Sources: ' || ' l_dblink '||l_dblink);
3339    END IF;
3340 
3341 /*--------------------------------------------------
3342 selecting ship method and delivery lead time passed
3343 by OM in addition to instance_id
3344 org_id ,cust_id and cust_site_id
3345 
3346 Pachset J changes:
3347 Removed DISTINCT from select and added
3348 order_line_id = nvl(ato_model_line_id, order_line_id)
3349 and rownum = 1
3350 
3351 In case of Ship set :
3352 we should be selecting only first line
3353 
3354 In case of Model/PTO only parent line will
3355 have ship method, intransit time info
3356 ----------------------------------------------*/
3357      sql_stmt :=
3358             ' SELECT '||
3359             ' mast.sr_instance_id,mast.source_organization_id, '||
3360             ' mast.organization_id, '||
3361             ' mast.customer_id,mast.customer_site_id, '||
3362             ' mast.ship_method,mast.delivery_lead_time,mast.ship_method_text '||
3363             ' FROM mrp_atp_schedule_temp'||l_dynstring||' mast '||
3364             ' WHERE mast.session_id = :x_session_id '||
3365             ' AND status_flag = 4 and '||
3366             ' order_line_id = nvl(ato_model_line_id, order_line_id)'||
3367             ' AND  rownum = 1';
3368 
3369 IF PG_DEBUG in ('Y', 'C') THEN
3370    msc_sch_wb.atp_debug('get_Supply_Sources: ' || 'sql_stmt : ' || sql_stmt);
3371 END IF;
3372 
3373    EXECUTE IMMEDIATE sql_stmt INTO l_sr_instance_id,l_om_source_org,
3374                 l_organization_id,
3375 		l_customer_id,l_customer_site_id,
3376 		g_ship_method_rec.ship_method,g_ship_method_rec.intransit_time,
3377 		g_ship_method_rec.ship_method_text
3378 		using x_session_id;
3379 IF PG_DEBUG in ('Y', 'C') THEN
3380       msc_sch_wb.atp_debug('l_organization_id (destin org ) ' || l_organization_id);
3381       msc_sch_wb.atp_debug('l_om_source_org is ' ||l_om_source_org);
3382       msc_sch_wb.atp_debug('g_ship_method_rec.ship_method is ' ||
3383                             g_ship_method_rec.ship_method);
3384       msc_sch_wb.atp_debug('g_ship_method_rec.intransit_time is ' ||
3385                             g_ship_method_rec.intransit_time);
3386       msc_sch_wb.atp_debug('l_customer_id is ' || l_customer_id);
3387       msc_sch_wb.atp_debug('l_customer_site_id is ' || l_customer_site_id);
3388 END IF;
3389 
3390    sql_stmt :=
3391      '    SELECT inventory_item_id ,order_line_id, ato_model_line_id,'||
3392      '           match_item_id' ||
3393      '    FROM mrp_atp_schedule_temp'||l_dynstring||
3394      '    WHERE session_id = :x_session_id '||
3395      '    AND status_flag = 4';
3396 
3397    OPEN mast_cursor FOR sql_stmt using x_session_id;
3398 
3399    j := 1;
3400    item_sources_extend(l_item_sourcing_rec);
3401 
3402    LOOP
3403       FETCH mast_cursor INTO l_item_sourcing_rec.sr_inventory_item_id(j),
3404                              l_item_sourcing_rec.line_id(j),
3405                              l_item_sourcing_rec.ato_line_id(j),
3406                              l_item_sourcing_rec.match_item_id(j);
3407       EXIT WHEN mast_cursor%notfound;
3408       j := j + 1;
3409       item_sources_extend(l_item_sourcing_rec);
3410    END LOOP;
3411       l_item_sourcing_rec.sr_inventory_item_id.trim(1);
3412       l_item_sourcing_rec.line_id.trim(1);
3413       l_item_sourcing_rec.ato_line_id.trim(1);
3414       l_item_sourcing_rec.match_item_id.trim(1);
3415 
3416 
3417 IF PG_DEBUG in ('Y', 'C') THEN
3418      msc_sch_wb.atp_debug(' count is ' ||
3419                          l_item_sourcing_rec.sr_inventory_item_id. count);
3420      msc_sch_wb.atp_debug('get_Supply_Sources: ' || ' Plan_id : '||
3421                          x_plan_id||' assgn_id : '||x_assignment_set_id||
3422                          ' inst_id : '||l_sr_instance_id);
3423 END IF;
3424 
3425     --bug3610706 Insert into destination regions table from Source
3426     --bug 4507141: l_dynstring indicates whether we are truely in distributed mode or not
3427     --IF l_dblink is not null THEN
3428     IF l_dynstring is not null THEN
3429              sql_stmt :=
3430                'INSERT INTO MSC_REGIONS_TEMP(
3431                 session_id,
3432                 partner_site_id,
3433                 region_id,
3434                 region_type,
3435                 zone_flag,
3436                 partner_type
3437                 )
3438                 (SELECT
3439                  session_id,
3440                  partner_site_id,
3441                  region_id,
3442                  region_type,
3443                  zone_flag,
3444                  partner_type
3445                  FROM msc_regions_temp' || l_dynstring || '
3446                  WHERE session_id = :x_session_id)';
3447               EXECUTE IMMEDIATE sql_stmt USING x_session_id;
3448 
3449       IF PG_DEBUG in ('Y', 'C') THEN
3450          msc_sch_wb.atp_debug('Rows inserted in msc_regions_temp:'|| sql%rowcount);
3451       END IF;
3452     END IF;
3453     --bug3610706 End Changes
3454 
3455     IF l_item_sourcing_rec.sr_inventory_item_id.COUNT > 0 THEN
3456 
3457       IF PG_DEBUG in ('Y', 'C') THEN
3458           msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3459                               'l_item_sourcing_rec.sr_inventory_item_id(1) : '
3460                                || l_item_sourcing_rec.sr_inventory_item_id(1));
3461           msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3462                                 'l_item_sourcing_rec.line_id(1) : '
3463                                || l_item_sourcing_rec.line_id(1));
3464           msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3465                                 'l_item_sourcing_rec.ato_line_id(1) : '
3466                                || l_item_sourcing_rec.ato_line_id(1));
3467           msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3468                                 'l_item_sourcing_rec.match_item_id(1) : '
3469                                || l_item_sourcing_rec.match_item_id(1));
3470       END IF;
3471 
3472 	 MSC_ATP_PROC.Atp_Sources(l_sr_instance_id,
3473 				  x_plan_id,
3474 				  NULL,
3475 				  l_Organization_Id,
3476 				  l_Customer_Id,
3477 				  l_Customer_Site_Id,
3478 				  x_assignment_set_id,
3479 				  l_item_sourcing_rec,
3480                                   x_session_id,
3481 				  l_sources,
3482 				  l_return_status);
3483 
3484      IF l_return_status <> 'S' THEN
3485 	x_error_mesg := l_return_status;
3486 	IF PG_DEBUG in ('Y', 'C') THEN
3487 	   msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3488                                 ' Error in call to msc_atp_pvt.atp_sources ');
3489 	END IF;
3490 	RETURN ;
3491      ELSE
3492 	IF PG_DEBUG in ('Y', 'C') THEN
3493 	   msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3494                                 ' Success in call to atp_sources : ' ||
3495                                   l_sources.organization_id.COUNT);
3496 	END IF;
3497      END IF;
3498 
3499      IF l_sources.organization_id.COUNT > 0 THEN
3500         MSC_SCH_WB.extend_other_cols(l_other_cols,
3501                                      l_sources.organization_id.COUNT);
3502 
3503 	FOR j IN 1..l_sources.organization_id.COUNT LOOP
3504 	    -- cchen's api returns these values.
3505 	    IF l_sources.ship_method(j) = '@@@' THEN
3506 	       l_sources.ship_method(j) := NULL;
3507 	    END IF;
3508 	    IF l_sources.lead_time(j) = '-1' THEN
3509 	       l_sources.lead_time(j) := NULL;
3510 	    END IF;
3511 
3512 	    IF l_sources.organization_id(j) <> -1 THEN
3513 	        BEGIN
3514 		   SELECT organization_code
3515 		     INTO l_other_cols.org_code(j)
3516 		     FROM msc_trading_partners
3517 		     WHERE sr_tp_id = l_sources.organization_id(j)
3518 		     AND sr_instance_id = l_sources.instance_id(j)
3519 		     AND partner_type = 3;
3520 		EXCEPTION
3521 		   WHEN no_data_found THEN
3522 IF PG_DEBUG in ('Y', 'C') THEN
3523           msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3524                                ' Org Code Not found for : ' ||
3525                                 l_sources.organization_id(j));
3526 END IF;
3527      x_error_mesg := 'Org Code Not found for : ' ||
3528                        l_sources.organization_id(j);
3529       RETURN;
3530       END;
3531 	    ELSE
3532  	          BEGIN
3533 		      SELECT mtil.sr_tp_id, mtp.partner_name
3534 			INTO l_other_cols.sr_supplier_id(j),
3535 			l_other_cols.vendor_name(j)
3536 			FROM msc_tp_id_lid mtil,
3537 			msc_trading_partners mtp
3538 			WHERE mtil.tp_id = mtp.partner_id
3539 			AND mtil.sr_instance_id = l_sources.instance_id(j)
3540 			AND mtil.partner_type = 1
3541 			AND mtp.partner_id = l_sources.supplier_id(j);
3542 
3543 		      SELECT mtsil.sr_tp_site_id, mtps.tp_site_code
3544 			INTO l_other_cols.sr_supplier_site_id(j),
3545 			l_other_cols.vendor_site_name(j)
3546 			FROM msc_tp_site_id_lid mtsil,
3547 			msc_trading_partner_sites mtps
3548 			WHERE mtsil.tp_site_id = mtps.partner_site_id
3549 			AND mtsil.sr_instance_id = l_sources.instance_id(j)
3550 			AND mtsil.partner_type = 1
3551 			AND mtps.partner_site_id =
3552 			l_sources.supplier_site_id(j);
3553 
3554         	  EXCEPTION
3555 		     WHEN no_data_found THEN
3556 			IF PG_DEBUG in ('Y', 'C') THEN
3557 			   msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3558                                        'Vendor name or site name Not found for '
3559 				        ||l_sources.organization_id(j));
3560 			END IF;
3561 			x_error_mesg :='Vendor name or site name Not found for'
3562 			                ||l_sources.organization_id(j);
3563 			RETURN;
3564 		  END;
3565 	      END IF; -- IF l_sources.organization_id(j) <> -1
3566 	   END LOOP;
3567 /*------------------------------------------------------------------------------
3568  Due to bug # 2428750 changing region level sourcing logic
3569 The new way to get shipping methods is the following:
3570 1)  Find specific shipping methods  from location to location.
3571 2)    If found, no need to deal with region level  ( see below condition:
3572     IF l_ship_method is NULL , only then go and proceed with region level)
3573 3) IF not found, then use cursor c_region_level to get the most
3574 specific region for this customer's location and its region_id
3575 4) Once we know region_id , we need to get all  defined ship methods ( default and not default ) for a user to pick.
3576 
3577 ------------------------------------------------------------------------------*/
3578    FOR counter IN 1..l_sources.organization_id.COUNT LOOP
3579       MSC_ATP_PROC.msc_calculate_source_attrib
3580       	                           ( l_customer_id,
3581                                      l_customer_site_id,
3582 	                             l_organization_id,
3583                                      l_sr_instance_id,
3584 		                     counter,
3585                                      l_sources,
3586                                      l_other_cols);
3587 
3588            l_to_location_id :=
3589               msc_atp_func.get_location_id(l_sr_instance_id,
3590                                            l_organization_id,
3591                                            l_customer_id,
3592                                            l_customer_site_id,
3593                                            l_sources.supplier_id(counter),                                                 l_sources.supplier_site_id(counter));
3594 
3595       IF PG_DEBUG in ('Y', 'C') THEN
3596         msc_sch_wb.atp_debug('get_Supply_Sources: '
3597                               || ' l_to_location_id '
3598                               ||l_to_location_id);
3599       END IF;
3600 
3601 -- cnazarma need to have l_from_location_id since org_id
3602 --might be diff from its location_id Bug #2422940
3603 
3604  l_from_location_id :=
3605   msc_atp_func.get_location_id (p_instance_id => l_sources.instance_id(counter),
3606                       p_organization_id => l_sources.organization_id(counter),
3607                       p_customer_id => NULL,
3608                       p_customer_site_id => NULL,
3609                       p_supplier_id => NULL,
3610                       p_supplier_site_id => NULL);
3611 
3612 -- here we are getting shipping methods for specific from location to location
3613 
3614     IF PG_DEBUG in ('Y', 'C') THEN
3615        msc_sch_wb.atp_debug('get_Supply_Sources: '
3616                              || ' l_from_location_id '
3617                              ||l_from_location_id);
3618     END IF;
3619 
3620 -- if l_to_location_id is null, that means we need to work with redions.
3621 
3622 
3623   IF l_to_location_id is NOT NULL THEN
3624        OPEN SH_METHODS( l_from_location_id,
3625                       l_sources.instance_id(counter),
3626                       l_to_location_id,
3627                       l_sr_instance_id);
3628        LOOP
3629        FETCH SH_METHODS INTO l_intransit_time,
3630                          l_ship_method,
3631                        --  l_ship_method_text,
3632                          l_default_flag;
3633        EXIT WHEN SH_METHODS%NOTFOUND;
3634 
3635               sql_stmt :=
3636                ' INSERT INTO '||
3637                ' MRP_ATP_SCHEDULE_TEMP'||l_dynstring||' '||
3638                ' ( SESSION_ID,'||
3639                ' DELIVERY_LEAD_TIME,SHIP_METHOD, '||
3640                ' STATUS_FLAG, '||
3641                ' ship_method_text, '||
3642                ' inventory_item_id,scenario_id,source_organization_id' ||
3643 		-- dsting 2535568 purge temp table fix
3644 	       ',creation_date
3645 	        ,created_by
3646 	        ,last_update_date
3647 		,last_updated_by
3648 		,last_update_login) '||
3649                ' VALUES ( ' ||
3650                ' :x_session_id,' ||
3651                ' :l_instansit_time,' ||
3652                ' :l_ship_method,' ||
3653                ' -99,  '||
3654                ':l_ship_method_text, ' ||
3655                ' -1101, ' ||
3656                ' :l_default_flag,' ||
3657                ' :l_source_org_id'	||
3658 
3659 		-- dsting 2535568 purge temp table fix
3660 	       ',sysdate'         ||
3661 	       ',:created_by'     ||
3662 	       ',sysdate'         ||
3663 	       ',:last_update_by' ||
3664 	       ',:last_update_login )';
3665 
3666          BEGIN
3667                 EXECUTE immediate sql_stmt
3668 		using x_session_id,
3669                 l_intransit_time,
3670                 l_ship_method,
3671                 l_ship_method_text,
3672                 nvl(l_default_flag,1),
3673                 l_sources.organization_id(counter),
3674                 FND_GLOBAL.USER_ID,	-- dsting 2535568 purge temp table fix
3675                 FND_GLOBAL.USER_ID,
3676                 FND_GLOBAL.USER_ID;
3677 
3678        IF PG_DEBUG in ('Y', 'C') THEN
3679             msc_sch_wb.atp_debug('get_Supply_Sources: '
3680                                  || ' after exec of above sql ');
3681        END IF;
3682 
3683       END;
3684 
3685   END LOOP;  -- finish looping through all available shipping methods
3686   CLOSE SH_METHODS;
3687 
3688   IF PG_DEBUG in ('Y', 'C') THEN
3689     msc_sch_wb.atp_debug('get_Supply_Sources: '
3690                           || 'l_ship_method AFTER FIRST SQL '
3691                           || l_ship_method);
3692   END IF;
3693 
3694 /*-----------------------------------------------------------------------------
3695 In case if there are no specific shipping methods found (l_ship_method is NULL ) THEN get the most specific region level and its region_id
3696 -----------------------------------------------------------------------------*/
3697   ELSE
3698         OPEN c_region_level (l_from_location_id,
3699                              l_sources.instance_id(counter),
3700                              l_sources.instance_id(counter),
3701                              x_session_id,
3702                              l_customer_site_id);
3703         FETCH c_region_level INTO l_region_level, l_region_id;
3704         CLOSE c_region_level;
3705 
3706         IF PG_DEBUG in ('Y', 'C') THEN
3707             msc_sch_wb.atp_debug('get_Supply_Sources: '
3708                                   || 'region level is '
3709                                   || l_region_level || ' and region_id '
3710                                   || l_region_id);
3711         END IF;
3712 
3713     sql_stmt :=
3714                ' INSERT INTO '||
3715                ' MRP_ATP_SCHEDULE_TEMP'||l_dynstring||' '||
3716                ' ( SESSION_ID,'||
3717                ' DELIVERY_LEAD_TIME,SHIP_METHOD, '||
3718                ' STATUS_FLAG, '||
3719                ' SHIP_METHOD_TEXT, '||
3720                ' inventory_item_id,scenario_id,source_organization_id' ||
3721 	       -- dsting 2535568 purge temp table fix
3722 	       ' , creation_date
3723 		 , created_by
3724 		 , last_update_date
3725 		 , last_updated_by
3726 		 , last_update_login) '||
3727            ' SELECT distinct'||
3728                ' mrt.session_id , '||
3729                ' msim.intransit_time,  '||
3730                ' msim.ship_method,  '||
3731                ' -99,  '||
3732                ' msim.ship_method_text, ' ||
3733                ' -1101,msim.default_flag,:source_org_id'||
3734 
3735 		-- dsting 2535568 purge temp table fix
3736 	       ',sysdate'             ||
3737 	       ',:created_by'         ||
3738 	       ',sysdate'             ||
3739 	       ',:last_update_by'     ||
3740 	       ',:last_update_login ' ||
3741                ' FROM msc_interorg_ship_methods  msim '||
3742                ' , msc_regions_temp mrt '||
3743 --               ' , fnd_common_lookups fnd '||
3744                ' WHERE mrt.session_id = :x_session_id '||
3745                ' AND msim.plan_id = -1 '||
3746                ' AND msim.from_location_id = :l_from_location_id '||
3747                ' AND msim.sr_instance_id = :source_instance_id '||
3748                ' AND msim.to_region_id = mrt.region_id '||
3749                ' AND mrt.partner_site_id = :customer_site_id '||
3750                ' AND msim.sr_instance_id2 = :instance_id '||
3751                ' AND mrt.region_id = :l_region_id ';
3752 --               ' AND FND.LOOKUP_TYPE = ''SHIP_METHOD'''||
3753 --               ' AND FND.APPLICATION_ID = 401 '||
3754 --               ' AND FND.LOOKUP_CODE = msim.ship_method';
3755 
3756                 execute immediate sql_stmt
3757                 using l_sources.organization_id(counter),
3758 		FND_GLOBAL.USER_ID,	-- dsting 2535568 purge temp table fix
3759 		FND_GLOBAL.USER_ID,
3760 		FND_GLOBAL.USER_ID,
3761                 x_session_id,
3762                 l_from_location_id,
3763                 l_sources.instance_id(counter),
3764                 l_customer_site_id,
3765                 l_sr_instance_id,
3766                 l_region_id;
3767 
3768         END IF; -- if l_to_location_id_
3769 
3770           begin
3771           sql_stmt :=
3772   	   ' SELECT '||
3773   	   ' distinct mast.ship_method '||
3774   	   ' FROM mrp_atp_schedule_temp'||l_dynstring||' mast '||
3775   	   ' WHERE mast.session_id = :x_session_id '||
3776   	   ' AND status_flag = -99'||
3777   	   ' AND mast.source_organization_id=:source_org_id '||
3778   	   ' AND mast.ship_method = :ship_method ';
3779 
3780 	   l_ship_method:=null;
3781 
3782   	  EXECUTE IMMEDIATE sql_stmt
3783           INTO l_ship_method
3784   	  USING x_session_id,
3785                 l_sources.organization_id(counter),
3786                 g_ship_method_rec.ship_method;
3787 	  EXCEPTION
3788 	  when no_data_found then null;
3789 	  end;
3790 
3791 
3792       IF l_sources.organization_id(counter) = l_om_source_org THEN
3793           IF l_ship_method = g_ship_method_rec.ship_method THEN
3794            l_sources.ship_method(counter):=g_ship_method_rec.ship_method;
3795            l_sources.lead_time(counter):=g_ship_method_rec.intransit_time;
3796 
3797         IF g_ship_method_rec.intransit_time is NULL THEN
3798 
3799         BEGIN
3800           sql_stmt :=
3801              '  SELECT DISTINCT '||
3802              ' delivery_lead_time '||
3803              ' FROM mrp_atp_schedule_temp'||l_dynstring||
3804              ' WHERE session_id = :x_session_id '||
3805              ' AND status_flag = -99'||
3806              ' AND ship_method =:g_ship_method'||
3807              ' AND source_organization_id = :l_om_org';
3808 
3809                EXECUTE IMMEDIATE sql_stmt
3810                INTO l_sources.lead_time(counter)
3811                USING  x_session_id,
3812                       g_ship_method_rec.ship_method,
3813                       l_om_source_org ;
3814 
3815                  EXCEPTION
3816                  when no_data_found THEN
3817                  NULL;
3818                  END;
3819          END IF;
3820         END IF;
3821 
3822    	   IF l_sources.SHIP_METHOD(counter) IS NOT null THEN
3823             BEGIN
3824   	     select fnd.meaning
3825 	     into  l_other_cols.ship_method_text(counter)
3826 	     FROM FND_common_lookups fnd
3827 	     where    FND.LOOKUP_CODE = l_sources.SHIP_METHOD(counter)
3828 	     AND      FND.LOOKUP_TYPE = 'SHIP_METHOD'
3829 	     AND      fND.APPLICATION_ID = 401;
3830             EXCEPTION
3831 	     WHEN no_data_found THEN
3832 	     l_other_cols.ship_method_text(counter)
3833 	      := l_sources.ship_method(counter);
3834 	     IF PG_DEBUG in ('Y', 'C') THEN
3835 	        msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3836                                      'no desc for ship method ');
3837 	     END IF;
3838             END;
3839        	   END IF;
3840        	   END IF;
3841 
3842 	   END LOOP;
3843 
3844            -- m2a link to put back the records into the apps table.
3845 	   MSC_ATP_PROC.insert_atp_sources(x_session_id,
3846                                            l_dblink,
3847                                            x_calling_inst,
3848 			                   l_sources,l_other_cols);
3849 	 ELSE
3850 	   IF PG_DEBUG in ('Y', 'C') THEN
3851 	      msc_sch_wb.atp_debug('get_Supply_Sources: ' ||
3852                                    ' There are no sources to be inserted ');
3853 	   END IF;
3854 	   x_ret_status := 'E';
3855 	   x_error_mesg := 'MRP_ATP_NO_SOURCES';
3856 	END IF; -- IF l_sources.organization_id.COUNT > 0
3857      END IF; -- IF l_item_arr.COUNT > 0 THEN
3858 
3859     -- Delete records from MSC_REGIONS_TEMP before returning back to calling
3860     -- application so as to clean up the table for another request within
3861     -- same session for Region Level Sourcing Support
3862 
3863     DELETE msc_regions_temp
3864     WHERE session_id = x_session_id;
3865 
3866     IF PG_DEBUG in ('Y', 'C') THEN
3867        msc_sch_wb.atp_debug('get_Supply_Sources: '
3868                             || 'Rows deleted from msc_regions_temp : '
3869                             ||sql%rowcount);
3870     END IF;
3871    --ubadrina bug 2265012 begin
3872    EXCEPTION
3873    when no_data_found then null;
3874    --ubadrina bug 2265012 end
3875 
3876 END get_supply_sources;
3877 
3878 PROCEDURE item_sources_extend(p_item_sourcing_rec
3879                           IN OUT NOCOPY MSC_ATP_CTO.Item_Sourcing_Info_Rec) IS
3880 BEGIN
3881 p_item_sourcing_rec.sr_inventory_item_id.extend;
3882 p_item_sourcing_rec.line_id.extend;
3883 p_item_sourcing_rec.ato_line_id.extend;
3884 p_item_sourcing_rec.match_item_id.extend;
3885 END item_sources_extend;
3886 
3887 
3888 PROCEDURE msc_calculate_source_attrib
3889   ( l_customer_id                NUMBER,
3890     l_ship_to_site_use_id        NUMBER,
3891     l_dest_org_id                NUMBER,
3892     l_dest_instance_id           NUMBER,
3893     counter                      NUMBER,
3894     x_atp_sources                IN OUT NoCopy mrp_atp_pvt.atp_source_typ,
3895     x_other_cols                 IN OUT NoCopy order_sch_wb.other_cols_typ) IS
3896 
3897     l_from_location_id  NUMBER;
3898     l_to_location_id    NUMBER;
3899 BEGIN
3900 msc_sch_wb.atp_debug('!!!!!msc_calculate_attrib !!!!!!');
3901 msc_sch_wb.atp_debug(' x_atp_sources.lead_time(counter) is '|| x_atp_sources.lead_time(counter));
3902 msc_sch_wb.atp_debug('x_atp_sources.ship_method(counter) is '|| x_atp_sources.ship_method(counter));
3903 
3904    IF x_atp_sources.lead_time(counter) IS NULL THEN
3905       -- find the ship method and intransit time
3906       -- find the from_location_id
3907       IF PG_DEBUG in ('Y', 'C') THEN
3908          msc_sch_wb.atp_debug('msc_calculate_source_attrib: '
3909                                || ' find the ship_method and intransit LT');
3910       END IF;
3911 
3912       l_from_location_id :=
3913 	msc_atp_func.get_location_id (p_instance_id => l_dest_instance_id,
3914 		    p_organization_id => x_atp_sources.organization_id(counter),
3915 				     p_customer_id => NULL,
3916 				     p_customer_site_id => NULL,
3917 				     p_supplier_id => NULL,
3918 				     p_supplier_site_id => NULL);
3919 
3920       IF PG_DEBUG in ('Y', 'C') THEN
3921          msc_sch_wb.atp_debug('msc_calculate_source_attrib: '
3922                                || ' from location_id = '||l_from_location_id);
3923       END IF;
3924 
3925       -- find the to_location_id
3926       l_to_location_id :=
3927 	msc_atp_func.get_location_id(p_instance_id => l_dest_instance_id,
3928 				    p_organization_id => l_dest_org_id,
3929 				    p_customer_id => l_customer_id,
3930 				    p_customer_site_id => l_ship_to_site_use_id,
3931 				    p_supplier_id => NULL,
3932 				    p_supplier_site_id => NULL);
3933 
3934       IF PG_DEBUG in ('Y', 'C') THEN
3935          msc_sch_wb.atp_debug('msc_calculate_source_attrib: '
3936                                     || ' from to_loc_id = '||l_to_location_id);
3937       END IF;
3938 
3939       -- find the ship method and intransit time first by
3940       -- from_location_id , to_location_id and default_flag
3941       x_atp_sources.ship_method(counter) :=
3942 	NVL(MSC_SCATP_PUB.get_default_ship_method(l_from_location_id,
3943 					  x_atp_sources.instance_id(counter),
3944 					  l_to_location_id,
3945 					  l_dest_instance_id,
3946 					  order_sch_wb.debug_session_id,
3947 					  l_ship_to_site_use_id),
3948 	  MSC_SCATP_PUB.get_ship_method(x_atp_sources.organization_id(counter),
3949 					  x_atp_sources.instance_id(counter),
3950 					  l_dest_org_id,
3951 					  l_dest_instance_id,
3952 					  NULL,
3953 					  NULL));
3954       x_atp_sources.lead_time(counter) :=
3955 	NVL(MSC_SCATP_PUB.get_default_intransit_time(
3956 					     l_from_location_id,
3957 					     x_atp_sources.instance_id(counter),
3958 					     l_to_location_id,
3959 					     l_dest_instance_id,
3960 					     order_sch_wb.debug_session_id,
3961 					     l_ship_to_site_use_id),
3962 	   NVL(MSC_SCATP_PUB.get_intransit_time(
3963 				 x_atp_sources.organization_id(counter),
3964 				 x_atp_sources.instance_id(counter),
3965 				 l_dest_org_id,
3966 				 l_dest_instance_id,
3967 				 NULL,
3968 				 NULL), 0));
3969    END IF;
3970 
3971    BEGIN
3972       IF PG_DEBUG in ('Y', 'C') THEN
3973          msc_sch_wb.atp_debug('msc_calculate_source_attrib: ' || 'get vendor_id and vendor_site_id ');
3974       END IF;
3975 
3976       select ORG_INFORMATION3,  ORG_INFORMATION4
3977 	into x_atp_sources.supplier_id(counter), x_atp_sources.supplier_site_id(counter)
3978 	from hr_organization_information
3979 	where organization_id = x_atp_sources.organization_id(counter)
3980 	and  ORG_INFORMATION_CONTEXT = 'Customer/Supplier Association';
3981    exception
3982       when no_data_found THEN
3983 	 IF PG_DEBUG in ('Y', 'C') THEN
3984 	    msc_sch_wb.atp_debug('msc_calculate_source_attrib: ' || 'No data found in-get vendor_id and vendor_site_id');
3985 	 END IF;
3986 
3987 	 x_atp_sources.supplier_id(counter) := 0;
3988 	 x_atp_sources.supplier_site_id(counter) :=0;
3989    END;
3990 
3991    IF x_atp_sources.SHIP_METHOD(counter) is not null THEN
3992       IF PG_DEBUG in ('Y', 'C') THEN
3993          msc_sch_wb.atp_debug('msc_calculate_source_attrib: '
3994                                || 'ship method is not null '
3995                                ||x_atp_sources.SHIP_METHOD(counter));
3996       END IF;
3997    BEGIN
3998            select fnd.meaning
3999 	   into  x_other_cols.ship_method_text(counter)
4000 	   fROM FND_common_lookups FND
4001 	   where    FND.LOOKUP_CODE = x_atp_sources.SHIP_METHOD(counter)
4002 	   AND      FND.LOOKUP_TYPE = 'SHIP_METHOD'
4003 	   AND      fND.APPLICATION_ID = 401;
4004       EXCEPTION
4005 	 WHEN no_data_found THEN
4006 	    x_other_cols.ship_method_text(counter)
4007 	      := x_atp_sources.ship_method(counter);
4008 	    IF PG_DEBUG in ('Y', 'C') THEN
4009 	       msc_sch_wb.atp_debug('msc_calculate_source_attrib: '
4010                                     || 'no desc for ship method ');
4011 	    END IF;
4012       END;
4013 
4014    END IF;
4015 
4016 END msc_calculate_source_attrib;
4017 
4018 
4019 PROCEDURE insert_atp_sources(x_session_id     NUMBER,
4020 			     x_dblink         VARCHAR2,
4021 			     x_calling_inst   VARCHAR2,
4022 			     x_atp_sources    mrp_atp_pvt.atp_source_typ,
4023 			     x_other_cols     order_sch_wb.other_cols_typ)
4024   IS
4025      sql_stmt   VARCHAR2(32000);
4026      l_dynstring VARCHAR2(129);
4027 
4028 BEGIN
4029 
4030    IF x_dblink IS NOT NULL AND x_calling_inst = 'APPS' THEN
4031       l_dynstring := '@'||x_dblink;
4032       IF PG_DEBUG in ('Y', 'C') THEN
4033          msc_sch_wb.atp_debug('insert_atp_sources: ' || 'inserting sources link '||l_dynstring);
4034       END IF;
4035    END IF;
4036 
4037    order_sch_wb.debug_session_id := x_session_id;
4038    IF PG_DEBUG in ('Y', 'C') THEN
4039       msc_sch_wb.atp_debug('insert_atp_sources: ' || 'inserting sources count =  '||x_atp_sources.organization_id.count);
4040    END IF;
4041 
4042      sql_stmt :=
4043        ' INSERT INTO '||
4044        ' MRP_ATP_SCHEDULE_TEMP'||l_dynstring||' '||
4045        ' (ACTION, CALLING_MODULE, SESSION_ID, ORDER_HEADER_ID, ORDER_LINE_ID, '||
4046        ' INVENTORY_ITEM_ID, ORGANIZATION_ID, SR_INSTANCE_ID, ORGANIZATION_CODE, '||
4047        ' SOURCE_ORGANIZATION_ID, '||
4048        ' order_number,  '||
4049        ' CUSTOMER_ID, CUSTOMER_SITE_ID, DESTINATION_TIME_ZONE, '||
4050        ' QUANTITY_ORDERED, UOM_CODE, REQUESTED_SHIP_DATE, REQUESTED_ARRIVAL_DATE, '||
4051        ' LATEST_ACCEPTABLE_DATE, DELIVERY_LEAD_TIME, FREIGHT_CARRIER, SHIP_METHOD, '||
4052        ' DEMAND_CLASS, SHIP_SET_NAME, ARRIVAL_SET_NAME, OVERRIDE_FLAG, '||
4053        ' VENDOR_ID, VENDOR_SITE_ID, INSERT_FLAG, '||
4054        ' ERROR_CODE, ERROR_MESSAGE, SEQUENCE_NUMBER, FIRM_FLAG, INVENTORY_ITEM_NAME, '||
4055        ' SOURCE_ORGANIZATION_CODE, '||
4056        ' INSTANCE_ID1, ORDER_LINE_NUMBER, PROMISE_DATE, '||
4057        ' CUSTOMER_NAME, CUSTOMER_LOCATION, '||
4058        ' Top_Model_line_id, ' ||
4059        ' ATO_Model_Line_Id, '||
4060        ' Parent_line_id, ' ||
4061        ' Config_item_line_id, ' ||
4062        ' Validation_Org, '||
4063        ' Component_Sequence_ID, '||
4064        ' Component_Code, ' ||
4065        ' line_number, '||
4066        ' included_item_flag, '||
4067        ' SCENARIO_ID, VENDOR_NAME, VENDOR_SITE_NAME, '||
4068        ' STATUS_FLAG, MDI_ROWID, DEMAND_SOURCE_TYPE, '||
4069        ' DEMAND_SOURCE_DELIVERY, ATP_LEAD_TIME, OE_FLAG, ITEM_DESC,  '||
4070        ' ship_method_text, shipment_number, option_number, '||
4071        ' project_number, task_number,old_source_organization_id,old_demand_class, '||
4072        ' ship_set_id, arrival_set_id' ||
4073        -- dsting 2535568 purge temp table fix
4074        ' ,creation_date'    ||
4075        ' ,created_by'       ||
4076        ' ,last_update_date' ||
4077        ' ,last_updated_by'  ||
4078        ' ,last_update_login ) ' ||
4079        ' SELECT  '||
4080        ' ACTION, CALLING_MODULE, SESSION_ID, ORDER_HEADER_ID, ORDER_LINE_ID, '||
4081        ' INVENTORY_ITEM_ID, ORGANIZATION_ID, SR_INSTANCE_ID, ORGANIZATION_CODE, '||
4082        ' :source_org_id, '||
4083        ' order_number, '||
4084        ' CUSTOMER_ID, CUSTOMER_SITE_ID, DESTINATION_TIME_ZONE, '||
4085        ' QUANTITY_ORDERED, UOM_CODE, REQUESTED_SHIP_DATE, REQUESTED_ARRIVAL_DATE, '||
4086        ' LATEST_ACCEPTABLE_DATE, '||
4087        ' :lead_time,  '||
4088        ' FREIGHT_CARRIER, '||
4089        ' :ship_method,  '||
4090        ' DEMAND_CLASS, '||
4091        ' ship_set_name, '||
4092        ' ARRIVAL_SET_NAME, OVERRIDE_FLAG, '||
4093        ' :supplier_id,  '||
4094        ' :supplier_site_id,  '||
4095        ' INSERT_FLAG, '||
4096        ' ERROR_CODE, ERROR_MESSAGE, SEQUENCE_NUMBER, FIRM_FLAG, INVENTORY_ITEM_NAME, '||
4097        ' :org_code, '||
4098        ' INSTANCE_ID1, ORDER_LINE_NUMBER, PROMISE_DATE, '||
4099        ' CUSTOMER_NAME, CUSTOMER_LOCATION, '||
4100        ' Top_Model_line_id, ' ||
4101        ' ATO_Model_Line_Id, '||
4102        ' Parent_line_id, ' ||
4103        ' Config_item_line_id, ' ||
4104        ' Validation_Org, '||
4105        ' Component_Sequence_ID, '||
4106        ' Component_Code, ' ||
4107        ' line_number, '||
4108        ' included_item_flag, '||
4109        ' SCENARIO_ID, '||
4110        ' :vendor_name,  '||
4111        ' :vendor_site_name, '||
4112        ' 22,  '||
4113        -- 22 is used here so that it does not get selected in lines block.
4114        -- cannot use 2 since it would be a valid one for backlog mode lines block.
4115        ' MDI_ROWID, DEMAND_SOURCE_TYPE, '||
4116        ' DEMAND_SOURCE_DELIVERY, ATP_LEAD_TIME, OE_FLAG, ITEM_DESC, '||
4117        ' :ship_method_text, '||
4118        ' shipment_number, option_number, project_number, task_number,old_source_organization_id, '||
4119        ' old_demand_class, ship_set_id, arrival_set_id  '||
4120 	  ' ,sysdate '           ||  -- dsting 2535568 purge temp table fix
4121 	  ' ,:created_by '       ||
4122 	  ' ,sysdate '           ||
4123 	  ' ,:last_update_by'    ||
4124 	  ' ,:last_update_login' ||
4125        ' FROM mrp_atp_schedule_temp'||l_dynstring||' '||
4126        ' WHERE session_id = :x_session_id '||
4127        ' AND status_flag = 4';
4128      --msc_sch_wb.atp_debug(' sql stmt '||sql_stmt);
4129 
4130      FOR j IN 1..x_atp_sources.organization_id.COUNT LOOP
4131      -- x_atp_sources.organization_id.first..x_atp_sources.organization_id.last
4132 	execute immediate sql_stmt
4133 	  using x_atp_sources.organization_id(j),x_atp_sources.lead_time(j),
4134 	  x_atp_sources.ship_method(j),
4135 	  --x_other_cols.row_index(j), -- created initially for "forall" stmt
4136 	  x_other_cols.sr_supplier_id(j),
4137 	  x_other_cols.sr_supplier_site_id(j),x_other_cols.org_code(j),
4138 	  x_other_cols.vendor_name(j),x_other_cols.vendor_site_name(j),
4139 	  x_other_cols.ship_method_text(j),
4140 	  FND_GLOBAL.USER_ID,		-- dsting 2535568 purge temp table fix
4141 	  FND_GLOBAL.USER_ID,
4142 	  FND_GLOBAL.USER_ID,
4143 	  x_session_id;
4144 IF PG_DEBUG in ('Y', 'C') THEN
4145    msc_sch_wb.atp_debug('insert_atp_sources: ' ||  ' In atp_insert_sources   x_atp_sources.ship_method(j) is ' || x_atp_sources.ship_method(j));
4146 	   msc_sch_wb.atp_debug('insert_atp_sources: ' || ' inserted rows '||SQL%ROWCOUNT);
4147 	END IF;
4148      END LOOP;
4149 END insert_atp_sources;
4150 
4151 PROCEDURE SHOW_SUMMARY_QUANTITY(p_instance_id          IN NUMBER,
4152                                 p_plan_id              IN NUMBER,
4153                                 p_organization_id      IN NUMBER,
4154                                 p_inventory_item_id    IN NUMBER,
4155                                 p_sd_date              IN DATE,
4156                                 p_resource_id          IN NUMBER,
4157                                 p_department_id        IN NUMBER,
4158                                 p_supplier_id          IN NUMBER,
4159                                 p_supplier_site_id     IN NUMBER,
4160                                 p_dc_flag              IN NUMBER,
4161                                 p_demand_class         IN VARCHAR2,
4162                                 p_mode                 IN NUMBER
4163                                 )
4164 IS
4165 temp_sd_qty  NUMBER;
4166 BEGIN
4167         /* p_mode tells from which table to select
4168            1- summary_so    2- Summary_sd  3- summary_res  4-Summary_sup */
4169 
4170         IF order_sch_wb.mr_debug = 'Y' THEN -- if debug mode is on
4171            IF PG_DEBUG in ('Y', 'C') THEN
4172               msc_sch_wb.atp_debug('**** In SHOW_SUMMARY_QUANTITY Debug mode is on ****');
4173               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_instance_id := ' || p_instance_id);
4174               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_plan_id     := ' || p_plan_id);
4175               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_organization_id := ' || p_organization_id);
4176               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_inventory_item_id := ' || p_inventory_item_id);
4177               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_sd_date := ' || p_sd_date);
4178               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_resource_id := ' || p_resource_id);
4179               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_department_id := ' || p_department_id);
4180               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_supplier_id := ' || p_supplier_id);
4181               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_supplier_site_id := ' || p_supplier_site_id);
4182               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_mode := ' || p_mode);
4183               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_demand_class = ' || p_demand_class);
4184               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'p_dc_flag = ' || p_dc_flag);
4185            END IF;
4186 
4187     	   IF p_mode = 1 THEN
4188 	      BEGIN
4189                     select /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ sd_qty
4190                     into temp_sd_qty
4191                     from MSC_ATP_SUMMARY_SO
4192                     where inventory_item_id = p_inventory_item_id and
4193                     organization_id = p_organization_id and
4194                     sr_instance_id = p_instance_id and
4195                     sd_date = trunc(p_sd_date) and
4196                     demand_class = Decode(p_dc_flag, 1, NVL(p_demand_class, '@@@'),'@@@');
4197               EXCEPTION
4198                        WHEN NO_DATA_FOUND THEN
4199                            temp_sd_qty := -8888;
4200                        WHEN OTHERS THEn
4201                            temp_sd_qty := -9999;
4202 	      END;
4203            ELSIF p_mode = 2 THEN
4204               BEGIN
4205                     select /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ sd_qty
4206                     into temp_sd_qty
4207                     from MSC_ATP_SUMMARY_SD
4208                     where sr_instance_id = p_instance_id
4209                     and   inventory_item_id = p_inventory_item_id
4210                     and   organization_id = p_organization_id
4211                     and   sd_date = trunc(p_sd_date)
4212                     and   plan_id = p_plan_id;
4213               EXCEPTION
4214                        when NO_DATA_FOUND THEN
4215                            temp_sd_qty := -8888;
4216                        when others then
4217                            temp_sd_qty := -9999;
4218               END;
4219 
4220            ELSIF p_mode = 3 THEN
4221               BEGIN
4222                     select /*+ INDEX(msc_atp_summary_res MSC_ATP_SUMMARY_RES_U1) */ sd_qty
4223                     into temp_sd_qty
4224                     from msc_atp_summary_res
4225                     where plan_id = p_plan_id
4226                     and   sr_instance_id = p_instance_id
4227                     and   organization_id = p_organization_id
4228                     and   resource_id = p_resource_id
4229                     and   department_id = p_department_id
4230                     and   sd_date = trunc(p_sd_date);
4231               EXCEPTION
4232                        when NO_DATA_FOUND THEN
4233                            temp_sd_qty := -8888;
4234                        when others then
4235                            temp_sd_qty := -9999;
4236               END;
4237 
4238            ELSIF p_mode = 4 THEN
4239               BEGIN
4240                     select /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) */ sd_qty
4241                     into temp_sd_qty
4242                     from msc_atp_summary_sup
4243                     where plan_id = p_plan_id
4244                     and   sr_instance_id = p_instance_id
4245                     and inventory_item_id = p_inventory_item_id
4246                     and supplier_id = p_supplier_id
4247                     and supplier_site_id = p_supplier_site_id
4248                     and sd_date = trunc(p_sd_date);
4249               EXCEPTION
4250                        when NO_DATA_FOUND THEN
4251                            temp_sd_qty := -8888;
4252                        when others then
4253                            temp_sd_qty := -9999;
4254               END;
4255 
4256            END IF; -- IF p_mode= 1
4257            IF PG_DEBUG in ('Y', 'C') THEN
4258               msc_sch_wb.atp_debug('SHOW_SUMMARY_QUANTITY: ' || 'temp_sd_qty := ' || temp_sd_qty);
4259               msc_sch_wb.atp_debug('**** END SHOW_SUMMARY_QUANTITY  ****');
4260            END IF;
4261         END IF; -- IF order_sch_wb.mr_debug = 'Y
4262 
4263 END SHOW_SUMMARY_QUANTITY;
4264 
4265 PROCEDURE GET_ITEM_ATTRIBUTES (p_instance_id            IN  NUMBER,
4266                                p_plan_id                IN  NUMBER,
4267                                p_inventory_item_id      IN  NUMBER,
4268                                p_organization_id        IN  NUMBER,
4269                                p_item_attribute_rec    IN OUT NoCopy MSC_ATP_PVT.item_attribute_rec)
4270 AS
4271 
4272 l_atp_flag           VARCHAR2(1);
4273 l_bom_item_type      NUMBER;
4274 l_atp_check          NUMBER;
4275 l_atp_comp_flag      VARCHAR2(1);
4276 l_pick_comp_flag     VARCHAR2(1);
4277 l_replenish_flag     VARCHAR2(1);
4278 l_cto_bom            NUMBER := 0;
4279 l_source_org_id      NUMBER ;
4280 
4281 
4282 BEGIN
4283    IF PG_DEBUG in ('Y', 'C') THEN
4284       msc_sch_wb.atp_debug('inside get_item_attributes');
4285       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'sr_inventory_item_id = '||p_inventory_item_id);
4286       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'organization_id = '||p_organization_id);
4287       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'plan_id = '||p_plan_id);
4288       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'sr_instance_id = '||p_instance_id);
4289       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'G_ORDER_LINE_ID = '||MSC_ATP_PVT.G_ORDER_LINE_ID);
4290       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'G_ASSEMBLY_LINE_ID = '||MSC_ATP_PVT.G_ASSEMBLY_LINE_ID);
4291       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'G_COMP_LINE_ID = '||MSC_ATP_PVT.G_COMP_LINE_ID);
4292       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'G_INV_CTP = '||MSC_ATP_PVT.G_INV_CTP);
4293       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'parent_repl_ord_flag := ' ||
4294                                                           p_item_attribute_rec.parent_repl_ord_flag);
4295       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'parent_bom_item_type := ' ||
4296                                                           p_item_attribute_rec.parent_bom_item_type);
4297       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'parent_comp_flag := ' ||
4298                                                           p_item_attribute_rec.parent_comp_flag);
4299       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'parent_atp_flag := ' ||
4300                                                           p_item_attribute_rec.parent_atp_flag);
4301       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'parent_pegging_id := ' ||
4302                                                           p_item_attribute_rec.parent_pegging_id);
4303    END IF;
4304 
4305    -- ATP Rule added to accomodate Bug 1510853
4306    --s_cto_rearch
4307    SELECT i.atp_flag, i.bom_item_type,
4308           i.atp_components_flag, i.bom_item_type,
4309           i.pick_components_flag, i.replenish_to_order_flag, -- atp_comp_flag
4310           NVL(i.fixed_lead_time, 0), NVL(i.variable_lead_time, 0),
4311           --bug3609031 adding ceil
4312           NVL(ceil(i.preprocessing_lead_time), 0), NVL(ceil(i.postprocessing_lead_time),0),   --lead times,
4313           NVL(i.substitution_window,0), NVL(i.create_supply_flag, 1), i.inventory_item_id,
4314           SUBSTR(i.item_name, 1,40), i.atp_rule_id, NVL(i.rounding_control_type, 2),
4315           --diag_atp
4316           i.unit_volume, i.unit_weight, i.volume_uom, i.weight_uom,
4317           i.uom_code, i.inventory_item_id, --rajjain AATP forward consumption
4318           --bug3609031 adding ceil
4319           NVL(ceil(i.full_lead_time),0)  -- SCLT (Supplier Capacity Lead Time)
4320           , i.base_item_id
4321           --bug5222635/5248167
4322           ,decode(i.atp_flag,'N',i.inventory_item_id,Decode(i.product_family_id,NULL,i.inventory_item_id,-23453,i.inventory_item_id,i.product_family_id))
4323           --, nvl(i.product_family_id, i.inventory_item_id) -- For time_phased_atp
4324           ---3917625: Store plan_id
4325           ,i.plan_id
4326           , lowest_level_src -- ATP4drp obtain flag applicable to DRP plan items.
4327    INTO   l_atp_flag, l_bom_item_type,
4328           l_atp_comp_flag, l_bom_item_type, l_pick_comp_flag, l_replenish_flag,
4329           p_item_attribute_rec.fixed_lt, p_item_attribute_rec.variable_lt,
4330           p_item_attribute_rec.pre_pro_lt, p_item_attribute_rec.post_pro_lt,
4331           p_item_attribute_rec.substitution_window, p_item_attribute_rec.create_supply_flag,
4332           p_item_attribute_rec.dest_inv_item_id,
4333           p_item_attribute_rec.item_name,
4334           p_item_attribute_rec.atp_rule_id, p_item_attribute_rec.rounding_control_type,
4335           --diag_atp
4336           p_item_attribute_rec.unit_volume, p_item_attribute_rec.unit_weight,
4337           p_item_attribute_rec.volume_uom, p_item_attribute_rec.weight_uom,
4338           p_item_attribute_rec.uom_code, p_item_attribute_rec.inventory_item_id,
4339           p_item_attribute_rec.processing_lt -- SCLT (Supplier Capacity Lead Time)
4340           , p_item_attribute_rec.base_item_id
4341           , p_item_attribute_rec.product_family_id -- time_phased_atp
4342           ---bug 3917625
4343           ,p_item_attribute_rec.plan_id
4344           , p_item_attribute_rec.lowest_level_src -- ATP4drp obtain flag applicable to DRP plan items.
4345    FROM   msc_system_items i
4346    WHERE  i.sr_inventory_item_id = p_inventory_item_id
4347    AND    i.organization_id = p_organization_id
4348    --- bug 3917625: Read item attribute from planned data
4349    --AND    i.plan_id = -1
4350    AND    i.plan_id = p_plan_id
4351    AND    i.sr_instance_id = p_instance_id;
4352 
4353    --e_cto_rearch
4354 
4355    IF PG_DEBUG in ('Y', 'C') THEN
4356       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_bom_item_type = '||l_bom_item_type);
4357       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_atp_check     = '||l_atp_check);
4358       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_replenish_flag = ' || l_replenish_flag);
4359       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_atp_comp_flag = ' || l_atp_comp_flag);
4360       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'UOM CODE = ' || p_item_attribute_rec.uom_code); --bug3110023
4361       msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
4362       msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'Lowest Level Source = ' || p_item_attribute_rec.lowest_level_src);
4363    END IF;
4364 
4365    -- ATP4drp re-set component flag for DRP plans
4366    IF NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
4367       IF l_atp_comp_flag = 'C' THEN
4368          l_atp_comp_flag := 'Y';
4369       ELSIF l_atp_comp_flag = 'R' THEN
4370          l_atp_comp_flag := 'N';
4371       END IF;
4372       IF PG_DEBUG in ('Y', 'C') THEN
4373          msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: Before Reset Product Family = ' || p_item_attribute_rec.product_family_id);
4374       END IF;
4375       p_item_attribute_rec.product_family_id := p_item_attribute_rec.dest_inv_item_id;
4376       -- Bug 4052808 DRP Plans only support Fixed Lead Times.
4377       p_item_attribute_rec.variable_lt := 0;
4378       IF PG_DEBUG in ('Y', 'C') THEN
4379          msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_atp_comp_flag = ' || l_atp_comp_flag);
4380          msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'PF not applicable for DRP plans');
4381          msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'Reset Product Family = ' || p_item_attribute_rec.product_family_id);
4382          msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
4383       END IF;
4384    END IF;
4385    -- END ATP4drp
4386 
4387    /* s_cto_rearch
4388    IF l_bom_item_type = 4 AND NVL(l_atp_check, -1) = 2 THEN
4389        l_atp_flag := 'N';
4390    END IF;
4391     e_cto_rearch */
4392 
4393     -- Fix for Bug 1413039 9/22/00 - NGOEL
4394     -- Since we don't support multi-level ATP for ODS, set
4395     -- return ATP component flag as N in case of ODS.
4396   IF l_bom_item_type in (1,2) and l_pick_comp_flag = 'Y' THEN
4397      --We treat PTO option class and model as non-atpable
4398      l_atp_flag := 'N';
4399      l_atp_comp_flag := 'N';
4400 
4401   ELSIF MSC_ATP_PVT.G_INV_CTP = 5 THEN
4402     IF ((l_bom_item_type = 1 or l_bom_item_type = 4) and l_replenish_flag = 'Y') AND
4403           p_item_attribute_rec.parent_pegging_id is null THEN
4404        --If ATO model or ATO item then we may need to explode it.
4405        If l_atp_comp_flag = 'C' THEN
4406 
4407          l_atp_comp_flag := 'Y';
4408 
4409        ELSIF l_atp_comp_flag = 'R' THEN
4410           l_atp_comp_flag := 'N';
4411        END IF;
4412 
4413     ELSE
4414        l_atp_comp_flag := 'N';
4415     END IF;
4416 
4417     IF PG_DEBUG in ('Y', 'C') THEN
4418         msc_sch_wb.atp_debug('l_bom_item_type := ' || l_bom_item_type);
4419         msc_sch_wb.atp_debug('parent_item_id := ' || p_item_attribute_rec.parent_item_id);
4420 
4421     END IF;
4422     IF NVL(p_item_attribute_rec.parent_item_id, -1) = p_inventory_item_id THEN
4423         --atp flag for this model = 'Y'. We are coming here for second time
4424         -- set the atp comp flag = 'N' so that model is not exploded again.
4425         l_atp_comp_flag := 'N';
4426         IF PG_DEBUG in ('Y', 'C') THEN
4427            msc_sch_wb.atp_debug('Turn off atp comp flag for model');
4428         END IF;
4429     END IF;
4430   ELSE
4431     IF PG_DEBUG in ('Y', 'C') THEN
4432        msc_sch_wb.atp_debug('PDS ATP');
4433     END IF;
4434     IF NVL(p_item_attribute_rec.parent_repl_ord_flag, 'N') = 'Y' and
4435                     NVL(p_item_attribute_rec.parent_bom_item_type, 4) = 4 THEN
4436         --parent is config item
4437         IF PG_DEBUG in ('Y', 'C') THEN
4438             msc_sch_wb.atp_debug('Parent is ato item');
4439         END IF;
4440         IF l_bom_item_type in (1,2) THEN
4441             --here parent is config and thisi tem is model or option class
4442             -- we turn off the atp comp flag, honor atpp flga as it is
4443             l_atp_comp_flag := 'N'; --- we turn off components flags
4444             IF PG_DEBUG in ('Y', 'C') THEN
4445                msc_sch_wb.atp_debug('Model or option class, turn off components flag');
4446             END IF;
4447         END IF;
4448     ELSIF NVL(p_item_attribute_rec.parent_bom_item_type, 4) = 1 and
4449                       NVL(p_item_attribute_rec.parent_repl_ord_flag, 'N') = 'Y'  And l_bom_item_type = 2 THEN
4450          ---parent is a model and this item is option class
4451          ---atp flag is honored as such
4452          ---components resource part is copied over from model's components flag
4453          IF PG_DEBUG in ('Y', 'C') THEN
4454                msc_sch_wb.atp_debug(' parent is model, This is option class');
4455          END IF;
4456 
4457          IF NVL(p_item_attribute_rec.parent_comp_flag, 'N') in ('R', 'C') THEN
4458               IF PG_DEBUG in ('Y', 'C') THEN
4459                    msc_sch_wb.atp_debug(' Resource check on model is on, set the resource flag on option class');
4460               END IF;
4461                l_atp_comp_flag := 'R';
4462          ELSE
4463               l_atp_comp_flag := 'N';
4464          END IF;
4465 
4466     ELSIF l_bom_item_type = 1 THEN
4467         IF PG_DEBUG in ('Y', 'C') THEN
4468            msc_sch_wb.atp_debug('l_bom_item_type := ' || l_bom_item_type);
4469            msc_sch_wb.atp_debug('parent_item_id := ' || p_item_attribute_rec.parent_item_id);
4470 
4471         END IF;
4472         IF NVL(p_item_attribute_rec.parent_item_id, -1) = p_inventory_item_id THEN
4473            --atp flag for this model = 'Y'. We are coming here for second time
4474            -- set the atp comp flag = 'N' so that model is not exploded again.
4475            l_atp_comp_flag := 'N';
4476            IF PG_DEBUG in ('Y', 'C') THEN
4477               msc_sch_wb.atp_debug('Turn off atp comp flag for model');
4478            END IF;
4479         END IF;
4480 
4481     END IF;
4482 
4483 
4484   END IF;
4485   MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := NULL;
4486   p_item_attribute_rec.atp_flag := l_atp_flag;
4487   p_item_attribute_rec.atp_comp_flag := l_atp_comp_flag;
4488 
4489   --s_cto_rearch
4490   p_item_attribute_rec.bom_item_type := l_bom_item_type;
4491   p_item_attribute_rec.replenish_to_ord_flag := l_replenish_flag;
4492   --e_cto_rearch
4493 
4494   IF PG_DEBUG in ('Y', 'C') THEN
4495      msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'l_source_org_id = '||l_source_org_id);
4496   END IF;
4497   p_item_attribute_rec.cto_source_org_id := l_source_org_id;
4498 
4499   IF PG_DEBUG in ('Y', 'C') THEN
4500      msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'get_item_attr ATP flag is '||l_atp_flag);
4501      msc_sch_wb.atp_debug('GET_ITEM_ATTRIBUTES: ' || 'get_item_attr ATP components flag is '||l_atp_comp_flag);
4502   END IF;
4503 
4504   p_item_attribute_rec.instance_id := p_instance_id;
4505   p_item_attribute_rec.organization_id := p_organization_id;
4506   p_item_attribute_rec.sr_inv_item_id := p_inventory_item_id;
4507 EXCEPTION
4508    WHEN  NO_DATA_FOUND THEN
4509        p_item_attribute_rec.instance_id := null;
4510        p_item_attribute_rec.organization_id := null;
4511        p_item_attribute_rec.sr_inv_item_id := null;
4512        MSC_ATP_PVT.G_SR_INVENTORY_ITEM_ID := p_inventory_item_id;
4513        p_item_attribute_rec.atp_flag := 'N';
4514        p_item_attribute_rec.atp_comp_flag := 'N';
4515        p_item_attribute_rec.pre_pro_lt := 0;
4516        p_item_attribute_rec.fixed_lt := 0;
4517        p_item_attribute_rec.variable_lt := 0;
4518        p_item_attribute_rec.post_pro_lt := 0;
4519        p_item_attribute_rec.substitution_window := 0;
4520        p_item_attribute_rec.create_supply_flag := 2;
4521        p_item_attribute_rec.atp_rule_id := NULL;
4522        p_item_attribute_rec.cto_source_org_id := l_source_org_id;
4523        p_item_attribute_rec.uom_code := NULL; --rajjain 12/10/2002
4524        p_item_attribute_rec.inventory_item_id := NULL; --rajjain 12/10/2002
4525        p_item_attribute_rec.processing_lt := 0;   -- SCLT (Supplier Capacity Lead Time)
4526        p_item_attribute_rec.lowest_level_src := 0;   -- ATP4drp
4527 END GET_ITEM_ATTRIBUTES;
4528 
4529 PROCEDURE GET_ORG_ATTRIBUTES (
4530         p_instance_id                   IN      NUMBER,
4531         p_organization_id               IN      NUMBER,
4532         x_org_attribute_rec             OUT     NoCopy MSC_ATP_PVT.org_attribute_rec)
4533 IS
4534 BEGIN
4535 
4536    IF PG_DEBUG in ('Y', 'C') THEN
4537       msc_sch_wb.atp_debug('inside get_org_attributes');
4538       msc_sch_wb.atp_debug('GET_ORG_ATTRIBUTES: ' || 'sr_instance_id = '||p_instance_id);
4539       msc_sch_wb.atp_debug('GET_ORG_ATTRIBUTES: ' || 'organization_id = '||p_organization_id);
4540    END IF;
4541 
4542    SELECT  tp.default_atp_rule_id,
4543            tp.calendar_code,
4544            tp.calendar_exception_set_id,
4545            tp.default_demand_class,
4546            tp.organization_code,
4547            tp.organization_type, --(ssurendr) Bug 2865389
4548            NVl(mp.network_scheduling_method,1), --bug3601223
4549            NVL(tp.use_phantom_routings, 2) --4570421
4550 
4551     INTO   x_org_attribute_rec.default_atp_rule_id,
4552            x_org_attribute_rec.cal_code,
4553            x_org_attribute_rec.cal_exception_set_id,
4554            x_org_attribute_rec.default_demand_class,
4555            x_org_attribute_rec.org_code,
4556            x_org_attribute_rec.org_type, --(ssurendr) Bug 2865389
4557            x_org_attribute_rec.network_scheduling_method, --bug3601223
4558            x_org_attribute_rec.use_phantom_routings --4570421
4559 
4560     FROM   msc_trading_partners tp, msc_parameters mp
4561     WHERE  tp.sr_tp_id = p_organization_id
4562     AND    tp.sr_instance_id = p_instance_id
4563     AND    tp.partner_type = 3
4564     AND    mp.ORGANIZATION_ID(+) = tp.sr_tp_id
4565     AND    mp.SR_INSTANCE_ID(+) = tp.sr_instance_id;
4566 
4567     x_org_attribute_rec.instance_id          := p_instance_id;
4568     x_org_attribute_rec.organization_id      := p_organization_id;
4569 
4570    IF PG_DEBUG in ('Y', 'C') THEN
4571       msc_sch_wb.atp_debug('GET_ORG_ATTRIBUTES: network_scheduling_method := ' || x_org_attribute_rec.network_scheduling_method);
4572    END IF;
4573 
4574 EXCEPTION WHEN others THEN
4575   x_org_attribute_rec.instance_id          := null;
4576   x_org_attribute_rec.organization_id      := null;
4577   x_org_attribute_rec.default_atp_rule_id  := null;
4578   x_org_attribute_rec.cal_code             := null;
4579   x_org_attribute_rec.cal_exception_set_id := null;
4580   x_org_attribute_rec.default_demand_class := null;
4581   x_org_attribute_rec.org_code             := null;
4582   x_org_attribute_rec.org_type             := null; --(ssurendr) Bug 2865389
4583   x_org_attribute_rec.network_scheduling_method := 1; --bug3601223
4584 
4585 
4586 END GET_ORG_ATTRIBUTES;
4587 
4588 PROCEDURE get_global_org_info (
4589         p_instance_id                   IN      NUMBER,
4590         p_organization_id               IN      NUMBER) IS
4591 
4592 x_org_attribute_rec           MSC_ATP_PVT.org_attribute_rec;
4593 
4594 BEGIN
4595 
4596    IF PG_DEBUG in ('Y', 'C') THEN
4597       msc_sch_wb.atp_debug('In get_global_org_info core');
4598    END IF;
4599 
4600    IF (MSC_ATP_PVT.G_ORG_INFO_REC.instance_id IS NULL) OR
4601       (MSC_ATP_PVT.G_ORG_INFO_REC.organization_id IS NULL) OR
4602       (MSC_ATP_PVT.G_ORG_INFO_REC.instance_id <> p_instance_id) OR
4603       (MSC_ATP_PVT.G_ORG_INFO_REC.organization_id <> p_organization_id) THEN
4604 
4605      GET_ORG_ATTRIBUTES(p_instance_id,  p_organization_id, x_org_attribute_rec);
4606 
4607      MSC_ATP_PVT.G_ORG_INFO_REC := x_org_attribute_rec;
4608 
4609    END IF;
4610 
4611 END get_global_org_info;
4612 
4613 PROCEDURE get_global_item_info (p_instance_id             IN  NUMBER,
4614                                p_plan_id                IN  NUMBER,
4615                                p_inventory_item_id      IN  NUMBER,
4616                                p_organization_id        IN  NUMBER,
4617                                p_item_attribute_rec     IN  MSC_ATP_PVT.item_attribute_rec )  IS
4618 
4619  l_item_attribute_rec         MSC_ATP_PVT.item_attribute_rec;
4620 
4621 BEGIN
4622 
4623    IF PG_DEBUG in ('Y', 'C') THEN
4624       msc_sch_wb.atp_debug('Inside Get Global item attributes');
4625       msc_sch_wb.atp_debug('parent_repl_ord_flag := ' || p_item_attribute_rec.parent_repl_ord_flag);
4626       msc_sch_wb.atp_debug('parent_bom_item_type := ' || p_item_attribute_rec.parent_bom_item_type);
4627       msc_sch_wb.atp_debug('parent_comp_flag := ' || p_item_attribute_rec.parent_comp_flag);
4628       msc_sch_wb.atp_debug('parent_atp_flag := ' || p_item_attribute_rec.parent_atp_flag);
4629       msc_sch_wb.atp_debug('parent_pegging_id := ' || p_item_attribute_rec.parent_pegging_id);
4630       msc_sch_wb.atp_debug('p_instance_id := ' || p_instance_id);
4631       msc_sch_wb.atp_debug('p_plan_id := ' || p_plan_id);
4632       msc_sch_wb.atp_debug('p_inventory_item_id := ' || p_inventory_item_id);
4633       msc_sch_wb.atp_debug('p_organization_id := ' || p_organization_id);
4634    END IF;
4635    IF (MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id IS NULL) OR
4636       (MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id IS NULL) OR
4637       (MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id IS NULL) OR
4638       (MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id <> p_instance_id) OR
4639       (MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id <> p_organization_id) OR
4640       (MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id <> p_inventory_item_id)
4641       ---3917625: read the item attributes if plan chnages
4642       OR (NVL(MSC_ATP_PVT.G_ITEM_INFO_REC.plan_id, -12345) <> p_plan_id) THEN
4643 
4644 
4645      msc_sch_wb.atp_debug('Item/Org/instance/plan info has changed. Recalculate item attributes');
4646      l_item_attribute_rec.parent_repl_ord_flag := p_item_attribute_rec.parent_repl_ord_flag;
4647      l_item_attribute_rec.parent_bom_item_type := p_item_attribute_rec.parent_bom_item_type;
4648      l_item_attribute_rec.parent_comp_flag := p_item_attribute_rec.parent_comp_flag;
4649      l_item_attribute_rec.parent_atp_flag := p_item_attribute_rec.parent_atp_flag;
4650      l_item_attribute_rec.parent_pegging_id := p_item_attribute_rec.parent_pegging_id;
4651      l_item_attribute_rec.parent_item_id := p_item_attribute_rec.parent_item_id;
4652 
4653      GET_ITEM_ATTRIBUTES(p_instance_id, p_plan_id, p_inventory_item_id,
4654                                 p_organization_id, l_item_attribute_rec);
4655 
4656      MSC_ATP_PVT.G_ITEM_INFO_REC := l_item_attribute_rec;
4657 
4658 
4659    END IF;
4660 
4661    IF PG_DEBUG in ('Y', 'C') THEN
4662       msc_sch_wb.atp_debug('get_global_item_info ATP flag is '||
4663                                 MSC_ATP_PVT.G_ITEM_INFO_REC.atp_flag);
4664    END IF;
4665 
4666 END get_global_item_info;
4667 
4668 -- New Procedure Supplier Capacity and Lead Time (SCLT) Project.
4669 PROCEDURE get_global_plan_info (p_instance_id        IN NUMBER,
4670                                 p_inventory_item_id  IN NUMBER,
4671                                 p_organization_id    IN NUMBER,
4672                                 p_demand_class       IN VARCHAR2,
4673                                 p_parent_plan_id     IN NUMBER DEFAULT NULL, --bug3510475
4674                                 p_time_phased_atp    IN VARCHAR2 := 'N' ) IS -- time_phased_atp
4675 
4676 p_plan_info_rec                 MSC_ATP_PVT.plan_info_rec;
4677 
4678 BEGIN
4679    IF PG_DEBUG in ('Y', 'C') THEN
4680       msc_sch_wb.atp_debug('In get_global_plan_info core');
4681       msc_sch_wb.atp_debug('g__inv_id := ' || MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id);
4682       msc_sch_wb.atp_debug('p_inv_id := ' || p_inventory_item_id);
4683       msc_sch_wb.atp_debug('g_instance_id := ' || MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id);
4684       msc_sch_wb.atp_debug('p_instance_id := ' || p_instance_id);
4685       msc_sch_wb.atp_debug('g_org_id := ' || MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id);
4686       msc_sch_wb.atp_debug('p_org_id := ' || p_organization_id);
4687       msc_sch_wb.atp_debug('g_plan_id := ' || MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id);
4688    END IF;
4689 
4690    IF (MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id IS NULL) OR
4691       (MSC_ATP_PVT.G_ITEM_INFO_REC.instance_id <> p_instance_id) OR
4692       (MSC_ATP_PVT.G_ITEM_INFO_REC.organization_id <> p_organization_id) OR
4693       (MSC_ATP_PVT.G_ITEM_INFO_REC.sr_inv_item_id <> p_inventory_item_id) OR
4694       -- If Item information does not match
4695       (MSC_ATP_PVT.G_PLAN_INFO_REC.plan_id IS NULL) OR
4696       (MSC_ATP_PVT.G_PLAN_INFO_REC.sr_instance_id <> p_instance_id) OR
4697       (MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id <> p_organization_id) THEN
4698       -- Or If the Plan Information does not match then obtain the plan info.
4699 
4700       --bug3510475
4701       Get_Plan_Info(p_instance_id, p_inventory_item_id,
4702                     p_organization_id, p_demand_class, p_plan_info_rec,p_parent_plan_id,p_time_phased_atp); -- time_phased_atp
4703       MSC_ATP_PVT.G_PLAN_INFO_REC := p_plan_info_rec;
4704 
4705    END IF;
4706 
4707    IF PG_DEBUG in ('Y', 'C') THEN
4708       msc_sch_wb.atp_debug('get_global_plan_info Plan name is '||
4709                                 MSC_ATP_PVT.G_PLAN_INFO_REC.plan_name);
4710       msc_sch_wb.atp_debug('get_global_plan_info Argument Organization ID is '||
4711                                                                p_organization_id);
4712       msc_sch_wb.atp_debug('get_global_plan_info Plan owning organization is '||
4713                                      MSC_ATP_PVT.G_PLAN_INFO_REC.organization_id);
4714    END IF;
4715 
4716 END get_global_plan_info;
4717 
4718 --diag_atp
4719 Procedure get_infinite_time_fence_date (p_instance_id             IN NUMBER,
4720                                        p_inventory_item_id        IN NUMBER,
4721                                        p_organization_id          IN NUMBER,
4722                                        p_plan_id                  IN NUMBER,
4723                                        x_infinite_time_fence_date OUT NoCopy DATE,
4724                                        x_atp_rule_name            OUT NoCopy VARCHAR2,
4725                                        -- Bug 3036513 Add additional parameters with
4726                                        -- defaults in spec for resource infinite time fence.
4727                                        p_resource_id              IN NUMBER,
4728                                        p_department_id            IN NUMBER)
4729 IS
4730 l_infinite_time_fence_date      DATE;
4731 l_item_type                     NUMBER;
4732 
4733 BEGIN
4734 
4735   -- Bug 3036513 Print out the parameter values passed in to the procedure.
4736   IF PG_DEBUG in ('Y', 'C') THEN
4737       msc_sch_wb.atp_debug('****inside get_infinite_time_fence_date****');
4738       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: sr_instance_id = '||p_instance_id);
4739       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: ' ||
4740                               'sr_inventory_item_id = '||p_inventory_item_id);
4741       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: ' ||
4742                               'organization_id = '||p_organization_id);
4743       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: ' || 'plan_id = '||p_plan_id);
4744       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: resource_id = '||p_resource_id);
4745       msc_sch_wb.atp_debug('GET_INFINITE_TIME_FENCE_DATE: department_id = '||p_department_id);
4746   END IF;
4747   -- End Bug 3036513.
4748 
4749   -- Bug 1566260, in case of modle or option class for PDS ATP, return null.
4750   -- This way, we always will work with multi-level results rather than single level
4751   -- This was done so that pegging tree is always available for Mulit-org CTO and
4752   -- demand entries could be stored for planning purposes.
4753   /* s_CTO rearch : we start honoring infinite time fence on model
4754   IF p_plan_id <> -1  THEN
4755     IF PG_DEBUG in ('Y', 'C') THEN
4756        msc_sch_wb.atp_debug('get_infinite_time_fence_date: ' || 'selecting item type for PDS');
4757     END IF;
4758     SELECT i.bom_item_type
4759     INTO   l_item_type
4760     FROM   msc_system_items i
4761     WHERE  i.plan_id = p_plan_id
4762     AND    i.sr_instance_id = p_instance_id
4763     AND    i.organization_id = p_organization_id
4764     AND    i.sr_inventory_item_id = p_inventory_item_id;
4765   END IF;
4766 
4767   IF nvl(l_item_type, -1) IN (1,2)  THEN
4768     IF PG_DEBUG in ('Y', 'C') THEN
4769        msc_sch_wb.atp_debug('get_infinite_time_fence_date: ' || 'PDS item type is model(1) or option class(2) : '||l_item_type);
4770     END IF;
4771     l_infinite_time_fence_date := null;
4772   ELSE
4773 
4774   --e_CTO_rearch */
4775 
4776     -- Bug 2877340, 2746213
4777     -- Read the Profile option to pad the user defined days
4778     -- to infinite Supply fence.
4779     IF PG_DEBUG in ('Y', 'C') THEN
4780        msc_sch_wb.atp_debug('get_infinite_time_fence_date: Profile value for Infinite Supply Pad');
4781        msc_sch_wb.atp_debug('get_infinite_time_fence_date: MSC_ATP_PVT.G_INF_SUP_TF_PAD ' || MSC_ATP_PVT.G_INF_SUP_TF_PAD);
4782     END IF;
4783     -- Bug 3036513 If resource_id is NULL get the infinite_time_fence_date
4784     -- for just the item.
4785     IF (p_resource_id IS NULL) THEN
4786 
4787        -- Obtain the ITF the normal way.
4788        SELECT c2.calendar_date, r.rule_name
4789        INTO   l_infinite_time_fence_date, x_atp_rule_name
4790        FROM   msc_calendar_dates c2,
4791               msc_calendar_dates c1,
4792               msc_atp_rules r,
4793               msc_trading_partners tp,
4794               msc_system_items i
4795        WHERE  i.sr_inventory_item_id = p_inventory_item_id
4796        AND    i.organization_id = p_organization_id
4797        --AND    i.plan_id = p_plan_id
4798        AND    i.plan_id = -1   -- for 1478110
4799        AND    i.sr_instance_id = p_instance_id
4800        AND    tp.sr_tp_id = i.organization_id
4801        AND    tp.sr_instance_id = i.sr_instance_id
4802        AND    tp.partner_type = 3
4803        AND    r.rule_id = NVL(i.atp_rule_id, NVL(tp.default_atp_rule_id,0))
4804        AND    r.sr_instance_id = p_instance_id
4805        AND    c1.sr_instance_id = p_instance_id
4806        AND    c1.calendar_date = TRUNC(sysdate)
4807        AND    c1.calendar_code = tp.calendar_code
4808        AND    c1.exception_set_id = -1
4809        AND    c2.sr_instance_id = p_instance_id
4810        -- Bug 2877340, 2746213
4811        -- Add Infinite Supply Time Fence PAD
4812        --bug3609031 adding ceil
4813        AND    c2.seq_num = c1.next_seq_num +
4814                    DECODE(r.infinite_supply_fence_code,
4815                    1, ceil(i.cumulative_total_lead_time) + MSC_ATP_PVT.G_INF_SUP_TF_PAD,
4816                    2, ceil(i.cum_manufacturing_lead_time) + MSC_ATP_PVT.G_INF_SUP_TF_PAD,
4817                    3, DECODE(NVL(ceil(i.preprocessing_lead_time),-1)+
4818                              NVL(ceil(i.full_lead_time),-1)+
4819                              NVL(ceil(i.postprocessing_lead_time),-1),-3,
4820                              NULL,              -- All are NULL so return NULL.
4821                              NVL(ceil(i.preprocessing_lead_time),0)+   -- Otherwise
4822                              NVL(ceil(i.full_lead_time),0) +           -- evaluate to
4823                              NVL(ceil(i.postprocessing_lead_time),0) -- NON NULL
4824                              + MSC_ATP_PVT.G_INF_SUP_TF_PAD),
4825                                                 -- Bugs 1986353, 2004479.
4826                    4, r.infinite_supply_time_fence)
4827        -- End Bug 2877340, 2746213
4828        AND    c2.calendar_code = c1.calendar_code
4829        AND    c2.exception_set_id = -1;
4830 
4831     ELSE -- Bug 3036513 obtain infinite_time_fence_date for resource.
4832        SELECT c2.calendar_date, r.rule_name
4833        INTO   l_infinite_time_fence_date, x_atp_rule_name
4834        FROM   msc_calendar_dates c2,
4835               msc_calendar_dates c1,
4836               msc_atp_rules r,
4837               msc_trading_partners tp,
4838               msc_department_resources dep_res
4839        WHERE  dep_res.resource_id = p_resource_id
4840        AND    dep_res.department_id = p_department_id
4841        AND    dep_res.organization_id = p_organization_id
4842        AND    dep_res.plan_id = p_plan_id
4843        AND    dep_res.sr_instance_id = p_instance_id
4844        AND    tp.sr_tp_id = dep_res.organization_id
4845        AND    tp.sr_instance_id = dep_res.sr_instance_id
4846        AND    tp.partner_type = 3
4847        AND    r.rule_id = NVL(dep_res.atp_rule_id, NVL(tp.default_atp_rule_id,0))
4848        AND    r.sr_instance_id = dep_res.sr_instance_id
4849        AND    c1.sr_instance_id = dep_res.sr_instance_id
4850        AND    c1.calendar_date = TRUNC(sysdate)
4851        AND    c1.calendar_code = tp.calendar_code
4852        AND    c1.exception_set_id = -1
4853        AND    c2.sr_instance_id = dep_res.sr_instance_id
4854        AND    c2.seq_num = c1.next_seq_num + r.infinite_supply_time_fence
4855        AND    c2.calendar_code = c1.calendar_code
4856        AND    c2.exception_set_id = c1.exception_set_id;
4857     END IF;
4858     -- End Bug 3036513 p_resource_id is NULL
4859 
4860   --s_cto_rearch
4861   --END IF;
4862 
4863   IF PG_DEBUG in ('Y', 'C') THEN
4864      msc_sch_wb.atp_debug('get_infinite_time_fence_date: ' || 'l_infinite_time_fence_date'||l_infinite_time_fence_date);
4865       msc_sch_wb.atp_debug('get_infinite_time_fence_date: ' || 'ATP Rule := ' || x_atp_rule_name);
4866    END IF;
4867   x_infinite_time_fence_date := l_infinite_time_fence_date;
4868 
4869 EXCEPTION
4870     WHEN NO_DATA_FOUND THEN
4871        -- for 1478110.  please refer to the BUG
4872        --IF p_plan_id = -1 THEN
4873 
4874        -- ngoel 2/15/2002, modified to avoid no_data_found in case call is made from
4875        -- View_Allocation
4876 
4877        IF p_plan_id IN (-1, -200) THEN
4878          x_infinite_time_fence_date := null;
4879        ELSE
4880 
4881          -- since this is pds, use planning's cutoff date as the infinite
4882          -- time fence date if no rule at item/org level, and no rule
4883          -- at org default.
4884 
4885          SELECT curr_cutoff_date
4886          INTO   l_infinite_time_fence_date
4887          FROM   msc_plans
4888          WHERE  plan_id = p_plan_id;
4889 
4890          IF PG_DEBUG in ('Y', 'C') THEN
4891             msc_sch_wb.atp_debug('get_infinite_time_fence_date: ' || 'l_infinite_time_fence_date'||l_infinite_time_fence_date);
4892          END IF;
4893          x_infinite_time_fence_date := l_infinite_time_fence_date;
4894        END IF;
4895 
4896 END get_infinite_time_fence_date;
4897 
4898 -- krajan
4899 -- New API to get all shipping methods for :
4900 --     * ORG - ORG
4901 --     * ORG - Customer Site
4902 --
4903 -- Can be called from both source and destnation
4904 --
4905 -- See design document for Bug 2359231
4906 -- Checked- in as part of pegging enhancement I project
4907 --
4908 
4909 PROCEDURE Get_Shipping_Methods (
4910         p_from_organization_id            IN      number,
4911         p_to_organization_id              IN      number,
4912         p_to_customer_id                  IN      number,
4913         p_to_customer_site_id             IN      number,
4914         p_from_instance_id                IN      number,
4915         p_to_instance_id                  IN      number,
4916         p_session_id                      IN      number,
4917         p_calling_module                  IN      number,
4918         x_return_status                   OUT NOCOPY    varchar2
4919 )
4920 IS
4921 
4922 ---------------    CURSORS
4923 
4924 -- For Source Region Level Data
4925 CURSOR  c_lead_time (c_from_loc_id number, c_partner_site_id number, c_session_id number)
4926 IS
4927 SELECT  mism.ship_method, mism.intransit_time,
4928         ((10 * (10 - mrt.region_type)) + DECODE(mrt.zone_flag, 'Y', 1, 0)) region_level
4929 FROM    mtl_interorg_ship_methods mism,
4930         msc_regions_temp mrt
4931 WHERE   mism.from_location_id = c_from_loc_id
4932 AND     mism.to_region_id = mrt.region_id
4933 AND     mrt.session_id = c_session_id
4934 AND     mrt.partner_site_id = c_partner_site_id
4935 ORDER BY 3;
4936 
4937 
4938 -- For Destination Region Level Data
4939 CURSOR  c_lead_time_dest (c_from_location_id number , c_from_instance_id number,
4940                           c_partner_site_id number, c_to_instance_id number,
4941                           c_session_id number
4942                          )
4943 IS
4944 SELECT  mism.ship_method, mism.intransit_time,
4945         ((10 * (10 - mrt.region_type)) + DECODE(mrt.zone_flag, 'Y', 1, 0)) region_level
4946 FROM    msc_interorg_ship_methods mism,
4947         msc_regions_temp mrt
4948 WHERE   mism.plan_id = -1
4949 AND     mism.from_location_id = c_from_location_id
4950 AND     mism.sr_instance_id = c_from_instance_id
4951 AND     mism.sr_instance_id2 = c_to_instance_id
4952 AND     mism.to_region_id = mrt.region_id
4953 AND     mrt.session_id = c_session_id
4954 AND     mrt.partner_site_id = c_partner_site_id
4955 ORDER BY 3;
4956 
4957 
4958 ----------------      Local Variables
4959 
4960 l_ship_method_arr       MRP_ATP_PUB.char30_arr := MRP_ATP_PUB.char30_arr();
4961 l_lead_time_arr         MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4962 
4963 l_ship_method_arr_null  MRP_ATP_PUB.char30_arr := MRP_ATP_PUB.char30_arr();
4964 l_lead_time_arr_null    MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4965 
4966 l_counter               number;
4967 l_counter2              number;
4968 l_level_tmp             number;
4969 l_level_orig            number;
4970 l_intransit_time_tmp    number;
4971 l_ship_method_tmp       varchar2(30);
4972 l_from_location_id      number;
4973 l_to_location_id        number;
4974 
4975 BEGIN
4976 
4977   IF PG_DEBUG in ('Y', 'C') THEN
4978     msc_sch_wb.atp_debug ('------------Get Shipping Methods----------');
4979     msc_sch_wb.atp_debug ('From Organization        : ' || p_from_organization_id );
4980     msc_sch_wb.atp_debug ('To Organization          : ' || p_to_organization_id);
4981     msc_sch_wb.atp_debug ('To Customer              : ' || p_to_customer_id );
4982     msc_sch_wb.atp_debug ('To Customer Site         : ' || p_to_customer_site_id );
4983     msc_sch_wb.atp_debug ('From Instance            : ' || p_from_instance_id );
4984     msc_sch_wb.atp_debug ('To Instance              : ' || p_to_instance_id );
4985     msc_sch_wb.atp_debug ('Session ID               : ' || p_session_id);
4986     msc_sch_wb.atp_debug ('Calling Module           : ' || p_calling_module);
4987   END IF;
4988     x_return_status := FND_API.G_RET_STS_SUCCESS;
4989 
4990     -- Call Get Regions to pipulate Region level data
4991 
4992     MSC_SATP_FUNC.Get_Regions (
4993         p_to_customer_site_id,
4994         p_calling_module,
4995         p_to_instance_id,
4996         p_session_id,
4997         NULL, -- dblink
4998         x_return_status
4999     );
5000 
5001     if (p_calling_module = 724) then
5002         -- On Destination
5003         if (p_to_organization_ID is not NULL) and
5004            (p_to_customer_id is NULL and p_to_customer_site_id is NULL) then
5005 			-- Bug 3515520, don't use org in case customer/site is populated.
5006             -- Org - Org Intransit Lead Time
5007             -- bug 2958287
5008             BEGIN
5009                 insert into mrp_atp_schedule_temp
5010                 (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5011                 (
5012                     select  p_session_id,
5013                             -1,
5014                             -1,
5015                             intransit_time,
5016                             ship_method,
5017                             100
5018                       from  msc_interorg_ship_methods
5019                      where  from_organization_id = p_from_organization_id
5020                        and  to_organization_id = p_to_organization_id
5021                        and  sr_instance_id = p_from_instance_id
5022                        and  sr_instance_id2 = p_to_instance_id
5023                        and  to_region_id is null
5024                        and  plan_id = -1
5025                 );
5026             EXCEPTION
5027                 when NO_DATA_FOUND then
5028                     null;
5029             END;
5030         -----------------------------------------------------------
5031         --else -- p_to_org_id is not NULL
5032         elsif p_to_customer_site_id is NOT NULL then
5033 			-- Bug 3515520, verify that customer site is populated.
5034             l_from_location_id := MSC_ATP_FUNC.get_location_id ( p_from_instance_id,
5035                                                             p_from_organization_id,
5036                                                             NULL,
5037                                                             NULL,
5038                                                             NULL,
5039                                                             NULL
5040                                                             );
5041 
5042             l_to_location_id := MSC_ATP_FUNC.get_location_id ( p_to_instance_id,
5043                                                             NULL,
5044                                                             p_to_customer_id,
5045                                                             p_to_customer_site_id,
5046                                                             NULL,
5047                                                             NULL
5048                                                             );
5049 
5050             if (l_from_location_id is NULL) then
5051                 x_return_status := FND_API.G_RET_STS_ERROR;
5052                 IF PG_DEBUG in ('Y', 'C') THEN
5053                 msc_sch_wb.atp_debug ('Cannot map data to locations ');
5054                 END IF;
5055                 return;
5056             end if;
5057 
5058             -- Now try region mapping.
5059             BEGIN
5060                -- bug 2958287
5061                insert into mrp_atp_schedule_temp
5062                 (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5063                 (
5064                     select  p_session_id,
5065                             -1,
5066                             -1,
5067                             intransit_time,
5068                             ship_method,
5069                             100
5070                       from  msc_interorg_ship_methods
5071                      where  plan_id = -1
5072                        and  from_location_id = l_from_location_id
5073                        and  sr_instance_id = p_from_instance_id
5074                        and  to_location_id = l_to_location_id
5075                        and  sr_instance_id2 = p_to_instance_id
5076                        and  to_region_id is NULL
5077                 );
5078             EXCEPTION
5079                 when DUP_VAL_ON_INDEX then
5080                    IF PG_DEBUG in ('Y', 'C') THEN
5081                     msc_sch_wb.atp_debug ('Data already present');
5082                     END IF;
5083             END;
5084 
5085             if (sql%rowcount = 0) then
5086                IF PG_DEBUG in ('Y', 'C') THEN
5087                 msc_sch_wb.atp_debug ('Shifting to Regions and Zones data');
5088                END IF;
5089 
5090                 l_lead_time_arr := l_lead_time_arr_null;
5091                 l_ship_method_arr := l_ship_method_arr_null;
5092                 l_level_orig := NULL;
5093 
5094                 l_counter := 0;
5095                 OPEN c_lead_time_dest (l_from_location_id, p_from_instance_id,
5096                                        p_to_customer_site_id, p_to_instance_id,
5097                                        p_session_id
5098                                       );
5099 
5100                 LOOP
5101                     FETCH c_lead_time_dest INTO  l_ship_method_tmp, l_intransit_time_tmp, l_level_tmp;
5102                     EXIT WHEN c_lead_time_dest%NOTFOUND;
5103 
5104                     if (l_level_orig is NULL) then
5105                         l_level_orig := l_level_tmp;
5106                     end if;
5107 
5108                     if (l_level_orig <> l_level_tmp) then
5109                         EXIT;
5110                     else
5111                         l_lead_time_arr.EXTEND;
5112                         l_ship_method_arr.EXTEND;
5113                         l_counter := l_counter + 1;
5114 
5115                         l_lead_time_arr(l_counter) := l_intransit_time_tmp;
5116                         l_ship_method_arr(l_counter) := l_ship_method_tmp;
5117 
5118                         IF PG_DEBUG in ('Y', 'C') THEN
5119                         msc_sch_wb.atp_debug ('Method : ' || l_ship_method_tmp  || '  -  Time : ' || l_intransit_time_tmp);
5120                         END IF;
5121                     end if;
5122 
5123                 END LOOP;
5124 
5125                 CLOSE c_lead_time_dest;
5126 
5127                 if (l_counter = 0) then
5128                    IF PG_DEBUG in ('Y', 'C') THEN
5129                     msc_sch_wb.atp_debug ('No data from regions table');
5130                    END IF;
5131                 else
5132                     BEGIN
5133                         forall l_counter2 in 1..l_counter
5134                          insert into mrp_atp_schedule_temp
5135                          (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5136                          values
5137                          (
5138                             p_session_id,
5139                             -1,
5140                             -1,
5141                             l_lead_time_arr (l_counter2),
5142                             l_ship_method_arr (l_counter2),
5143                             100
5144                          );
5145                     EXCEPTION
5146                         when others then
5147                            IF PG_DEBUG in ('Y', 'C') THEN
5148                             msc_sch_wb.atp_debug ('Unable to insert data');
5149                             END IF;
5150                     END;
5151                 end if; -- l_counter = 0
5152             end if;
5153         end if;
5154 ----------------------------------------------------------------
5155     else -- case where calling_module <> 724
5156         -- On Source
5157         if (p_to_organization_ID is not NULL) and
5158            (p_to_customer_id is NULL and p_to_customer_site_id is NULL) then
5159             -- Bug 3515520, don't use org in case customer/site is populated.
5160             -- Org - Org Intransit Lead Time
5161             BEGIN
5162                IF PG_DEBUG in ('Y', 'C') THEN
5163                 msc_sch_wb.atp_debug ('To Organization Specified ');
5164                 END IF;
5165                 insert into mrp_atp_schedule_temp
5166                 (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5167                 (
5168                     select  p_session_id,
5169                             -1,
5170                             -1,
5171                             intransit_time,
5172                             ship_method,
5173                             100
5174                       from  mtl_interorg_ship_methods
5175                      where  from_organization_id = p_from_organization_id
5176                        and  to_organization_id = p_to_organization_id
5177                 );
5178                IF PG_DEBUG in ('Y', 'C') THEN
5179                 msc_sch_wb.atp_debug ('Records inserted into table : ' || sql%rowcount);
5180                 END IF;
5181             EXCEPTION
5182                 when OTHERS then
5183                     null;
5184             END;
5185         -----------------------------------------------------------
5186         --else -- p_to_organization_ID is not NULL
5187         elsif p_to_customer_site_id is NOT NULL then
5188         -- Bug 3515520, verify that customer site is populated.
5189             -- Org to Customer intransit Lead Time Calculation
5190 
5191             IF PG_DEBUG in ('Y', 'C') THEN
5192             msc_sch_wb.atp_debug ('Customer ID specified ');
5193             END IF;
5194             -- First get the location corresponding to the customer
5195             BEGIN
5196                 l_from_location_id := MSC_SATP_FUNC.src_location_id (p_from_organization_id,
5197                                                                  NULL,
5198                                                                  NULL);
5199                 l_to_location_id := MSC_SATP_FUNC.src_location_id (NULL,
5200                                                                p_to_customer_id,
5201                                                                p_to_customer_site_id);
5202             EXCEPTION
5203                 when NO_DATA_FOUND then
5204                    IF PG_DEBUG in ('Y', 'C') THEN
5205                     msc_sch_wb.atp_debug ('Unable to map organization of customer to location');
5206                    END IF;
5207             END;
5208 
5209             if (l_from_location_id is NULL) then
5210                 IF PG_DEBUG in ('Y', 'C') THEN
5211                 msc_sch_wb.atp_debug ('From location is NULL');
5212                 END IF;
5213                 x_return_status := FND_API.G_RET_STS_ERROR;
5214                 return;
5215             end if;
5216 
5217            IF PG_DEBUG in ('Y', 'C') THEN
5218             msc_sch_wb.atp_debug ('From Location ID : ' || l_from_location_id);
5219             msc_sch_wb.atp_debug ('To Location ID   : ' || l_to_location_id );
5220            END IF;
5221 
5222             BEGIN
5223                insert into mrp_atp_schedule_temp
5224                 (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5225                 (
5226                     select  p_session_id,
5227                             -1,
5228                             -1,
5229                             intransit_time,
5230                             ship_method,
5231                             100
5232                       from  mtl_interorg_ship_methods
5233                      where  from_location_id = l_from_location_id
5234                        and  to_location_id = l_to_location_id
5235                 );
5236             EXCEPTION
5237                 when DUP_VAL_ON_INDEX then
5238                    IF PG_DEBUG in ('Y', 'C') THEN
5239                     msc_sch_wb.atp_debug ('Data already present');
5240                    END IF;
5241 
5242                 when NO_DATA_FOUND then
5243                     null;
5244             END;
5245 
5246             if (sql%rowcount = 0) then
5247                 IF PG_DEBUG in ('Y', 'C') THEN
5248                 msc_sch_wb.atp_debug ('Shifting to Regions and Zones data');
5249                 END IF;
5250 
5251                 l_level_orig := NULL;
5252                 l_lead_time_arr := l_lead_time_arr_null;
5253                 l_ship_method_arr := l_ship_method_arr_null;
5254 
5255                 l_counter := 0;
5256                 OPEN c_lead_time (l_from_location_id, p_to_customer_site_id, p_session_id);
5257 
5258                 LOOP
5259                     FETCH c_lead_time INTO  l_ship_method_tmp, l_intransit_time_tmp, l_level_tmp;
5260                     EXIT WHEN c_lead_time%NOTFOUND;
5261 
5262                     if (l_level_orig is NULL) then
5263                         l_level_orig := l_level_tmp;
5264                     end if;
5265 
5266                     if (l_level_orig <> l_level_tmp) then
5267                         EXIT;
5268                     else
5269 
5270                         l_lead_time_arr.EXTEND;
5271                         l_ship_method_arr.EXTEND;
5272                         l_counter := l_counter + 1;
5273 
5274                         l_lead_time_arr(l_counter) := l_intransit_time_tmp;
5275                         l_ship_method_arr(l_counter) := l_ship_method_tmp;
5276 
5277                        IF PG_DEBUG in ('Y', 'C') THEN
5278                         msc_sch_wb.atp_debug ('Method : ' || l_ship_method_tmp || '  -  Time : ' || l_intransit_time_tmp);
5279                        END IF;
5280 
5281                     end if;
5282                 END LOOP;
5283 
5284                 CLOSE c_lead_time;
5285 
5286                 if (l_counter = 0) then
5287                    IF PG_DEBUG in ('Y', 'C') THEN
5288                     msc_sch_wb.atp_debug ('No data from regions table');
5289                     END IF;
5290                 else
5291                     BEGIN
5292                         forall l_counter2 in 1..l_counter
5293                          insert into mrp_atp_schedule_temp
5294                          (session_id, inventory_item_id, scenario_id, delivery_lead_time, ship_method, status_flag)
5295                          values
5296                          (
5297                             p_session_id,
5298                             -1,
5299                             -1,
5300                             l_lead_time_arr (l_counter2),
5301                             l_ship_method_arr (l_counter2),
5302                             100
5303                          );
5304                     EXCEPTION
5305                         when others then
5306                             IF PG_DEBUG in ('Y', 'C') THEN
5307                             msc_sch_wb.atp_debug ('Unable to insert data');
5308                             END IF;
5309                     END;
5310                 end if; -- l_counter = 0
5311             end if; --sql rowcount = 0
5312         end if; -- p_org_id is not nULL
5313      end if; -- calling module check
5314 
5315      -- Delete data from MSC_REGIONS_TEMP
5316      DELETE MSC_REGIONS_TEMP
5317      where  session_id = p_session_id;
5318 
5319      IF PG_DEBUG in ('Y', 'C') THEN
5320          msc_sch_wb.atp_debug (sql%rowcount || ' rows deleted from regions temp table.');
5321      END IF;
5322 
5323      COMMIT;
5324 
5325 END Get_shipping_methods;
5326 
5327 
5328 
5329 -- dsting
5330  --*
5331  --* search for transit times in this order
5332  --*
5333  --* 1) loc to loc
5334  --* 2) region to region
5335  --* 3) org to org
5336  --*
5337  --* if a ship method is specified then 1,2,3 with ship method specified
5338  --* 	then 1,2,3 with default times
5339  --*
5340  --*
5341  --
5342 -- krajan: Part of 2359231 changes
5343 PROCEDURE ATP_Shipping_Lead_Time (
5344   p_from_loc_id             IN NUMBER,
5345   p_to_customer_site_id     IN NUMBER,
5346   p_session_id              IN NUMBER,
5347   x_ship_method             IN OUT NOCOPY VARCHAR2,
5348   x_intransit_time          OUT NOCOPY NUMBER,
5349   x_return_status           OUT NOCOPY VARCHAR2
5350 ) IS
5351 l_to_location_id        number;
5352 l_sql_stmt      VARCHAR2(1000);
5353 BEGIN
5354 	-- We will return an error if shipping gives us something
5355     -- that doesn't meet our assumptions
5356     IF PG_DEBUG in ('Y','C') then
5357     msc_sch_wb.atp_debug ('---- ATP_Shipping_Lead_time--- ');
5358     msc_sch_wb.atp_debug ('From Loc ID        : ' ||p_from_loc_id);
5359     msc_sch_wb.atp_debug ('To Customer Site   : ' ||p_to_customer_site_id);
5360     msc_sch_wb.atp_debug ('Session ID         : ' || p_session_id );
5361     msc_sch_wb.atp_debug ('Ship Method        : ' || x_ship_method);
5362     end if;
5363 
5364     x_return_status := FND_API.G_RET_STS_SUCCESS;
5365 
5366     l_to_location_id := -1;
5367 
5368     -- Map customer site ID to location ID using PO (HR) data.
5369     BEGIN
5370         l_sql_stmt := 'select location_id
5371                 	 from PO_LOCATION_ASSOCIATIONS
5372                 	 where SITE_USE_ID = :p_customer_site_id';
5373         execute immediate l_sql_stmt into l_to_location_id
5374                 using p_to_customer_site_id;
5375     EXCEPTION
5376         WHEN NO_DATA_FOUND THEN
5377 
5378         IF PG_DEBUG in ('Y','C') then
5379             msc_sch_wb.atp_debug ('Cannot map customer site ID to location ID');
5380         end if;
5381     END;
5382 
5383     IF PG_DEBUG in ('Y','C') then
5384         msc_sch_wb.atp_debug ('Location ID : ' || l_to_location_id);
5385     end if;
5386 
5387 	MSC_SATP_FUNC.Get_Regions_Shipping(
5388 		p_to_customer_site_id,   -- customer_site_id
5389 		-1,   -- not destination(724)
5390 		NULL, -- no instance id
5391 		p_session_id,
5392 		NULL, -- dblink
5393 		x_return_status
5394 	);
5395 
5396 	MSC_SATP_FUNC.get_src_transit_time(
5397 		NULL, -- from org
5398 		p_from_loc_id,
5399 		NULL, -- to org
5400 		l_to_location_id,
5401 		p_session_id,
5402 		p_to_customer_site_id,   -- fake customer/partner_site_id
5403 		x_ship_method,
5404 		x_intransit_time
5405 	);
5406 
5407 	DELETE MSC_REGIONS_TEMP
5408 	WHERE session_id = p_session_id
5409         AND partner_site_id = p_to_customer_site_id;
5410 
5411         if (PG_DEBUG in ('Y','C')) then
5412                 msc_sch_wb.atp_debug ('Returning ....');
5413                 msc_sch_wb.atp_debug ('  Ship Method  : ' || x_ship_method);
5414                 msc_sch_wb.atp_debug ('  Lead Time    : ' || x_intransit_time);
5415         end if;
5416 
5417 	IF x_intransit_time < 0 THEN
5418 		x_return_status := FND_API.G_RET_STS_ERROR;
5419 	END IF;
5420 
5421 END ATP_Shipping_Lead_Time;
5422 
5423 -- Procedure added for bug 2585710
5424 PROCEDURE Get_Sources_Info(p_session_id             IN  NUMBER,
5425                            p_inventory_item_id      IN  NUMBER,
5426                            p_customer_id            IN  NUMBER,
5427                            p_customer_site_id       IN  NUMBER,
5428                            p_assignment_set_id      IN  NUMBER,
5429                            p_ship_set_item_count    IN  NUMBER,
5430                            x_atp_sources            OUT NOCOPY MRP_ATP_PVT.Atp_Source_Typ,
5431                            x_return_status          OUT NOCOPY VARCHAR2,
5432                            p_partner_type           IN  NUMBER, --2814895
5433 	                   p_party_site_id          IN  NUMBER, --2814895
5434 	                   p_order_line_id          IN  NUMBER)  --2814895
5435 IS
5436         l_dist_level_type	MRP_ATP_PUB.number_arr;
5437         l_counter               PLS_INTEGER := 0;
5438         l_sysdate               DATE;
5439         l_inserted_rows         PLS_INTEGER := 0;
5440         l_updated_rows          PLS_INTEGER := 0;
5441 	l_min_region_value	PLS_INTEGER := 0;
5442 	l_inventory_item_id_arr	MRP_ATP_PUB.number_arr;
5443 	l_min_region_value_arr	MRP_ATP_PUB.number_arr;
5444 	l_items_visited		PLS_INTEGER := 0;
5445 	i			PLS_INTEGER := 0;
5446 
5447 BEGIN
5448 
5449 /*
5450 We make use of fall-through approach in searching the sources.
5451 We first find whether our search is at all required or not.
5452 If yes, then which all levels have to searched. This is done
5453 by selecting distinct assignment_type along with their level_id
5454 by a preliminary SQL
5455 
5456 We have 2 cases here:
5457 Case1. Finding the sources for single item.
5458 p_ship_set_item_count is null, p_inventory_item_id is not null
5459 Case2. Finding the sources for ship set.
5460 p_ship_set_item_count is not null, p_inventory_item_id is null.
5461 */
5462 IF PG_DEBUG in ('Y', 'C') THEN
5463 
5464 msc_sch_wb.atp_debug('Inside Get_Sources Info procedure');
5465 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'p_inventory_item_id ' || p_inventory_item_id);
5466 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'p_customer_id ' || p_customer_id);
5467 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'p_customer_site_id ' || p_customer_site_id);
5468 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'p_assignment_set_id ' || p_assignment_set_id);
5469 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'p_ship_set_item_count ' || p_ship_set_item_count);
5470 END IF;
5471 
5472 -- Initialize return status and sysdate.
5473 x_return_status := FND_API.G_RET_STS_SUCCESS;
5474 SELECT TRUNC(sysdate) INTO l_sysdate FROM dual;
5475 
5476 SELECT
5477 	DECODE(MSRA.ASSIGNMENT_TYPE,
5478 		1, 9,
5479 		2, 6,
5480 		3, 4,
5481 		4, 7,
5482 		5, 3,
5483 		6, 1,
5484 		7, 8,
5485 		8, 5,
5486 		9, 2)		Level_id
5487 BULK COLLECT INTO
5488         l_dist_level_type
5489 FROM
5490         MSC_SOURCING_RULES      MSR,
5491         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
5492         MSC_SR_ASSIGNMENTS      MSRA,
5493         MSC_SR_SOURCE_ORG       SOURCE_ORG
5494 WHERE
5495         MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5496         AND     MSRA.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
5497         AND     MSR.STATUS = 1
5498         AND     MSR.SOURCING_RULE_TYPE = 1
5499         AND     MSR.SOURCING_RULE_ID = RECEIPT_ORG.SOURCING_RULE_ID
5500         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5501         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
5502         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
5503         AND     RECEIPT_ORG.SR_RECEIPT_ID = SOURCE_ORG.SR_RECEIPT_ID
5504         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
5505 GROUP BY MSRA.ASSIGNMENT_TYPE
5506 ORDER BY Level_id;
5507 
5508 IF (l_dist_level_type.COUNT = 0) THEN
5509     IF PG_DEBUG in ('Y', 'C') THEN
5510 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Checking assignment set id indicates no sources can be found.');
5511 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'So, not searching in any level. Returning from here itself');
5512      END IF;
5513      x_return_status := FND_API.G_RET_STS_ERROR;
5514      return;
5515 ELSE
5516     IF PG_DEBUG in ('Y', 'C') THEN
5517 	FOR l_counter in 1..l_dist_level_type.COUNT LOOP
5518 	   msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level: ' || l_dist_level_type(l_counter));
5519 	END LOOP;
5520      END IF;
5521 END IF;
5522 
5523 
5524 -- case1
5525 IF (p_ship_set_item_count IS NULL) THEN
5526 
5527 IF PG_DEBUG in ('Y', 'C') THEN
5528         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Starting search for a single line item sources');
5529 END IF;
5530 
5531 
5532 FOR l_counter IN l_dist_level_type.FIRST..l_dist_level_type.LAST LOOP
5533 
5534 IF (l_dist_level_type(l_counter) = 1) THEN
5535 
5536 -- LEVEL 1, ASSIGNMENT TYPE 6: ITEM-ORG
5537 IF PG_DEBUG in ('Y', 'C') THEN
5538         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 1 (item-org)');
5539 END IF;
5540 
5541 SELECT
5542         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
5543         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
5544         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
5545         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
5546         NVL(SOURCE_ORG.RANK, -1)                        RANK,
5547         NVL(SOURCE_ORG.SOURCE_TYPE,
5548                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
5549                 to_number(null), 3, 1))                 SOURCE_TYPE,
5550         0                                               PREFERRED,
5551         -1                                              LEAD_TIME,
5552         '@@@'                                           SHIP_METHOD,
5553         NULL -- For supplier intransit LT project
5554 BULK COLLECT INTO
5555          x_atp_sources.Organization_Id,
5556          x_atp_sources.Instance_Id,
5557          x_atp_sources.Supplier_Id,
5558          x_atp_sources.Supplier_Site_Id,
5559          x_atp_sources.Rank,
5560          x_atp_sources.Source_Type,
5561          x_atp_sources.Preferred,
5562          x_atp_sources.Lead_Time,
5563          x_atp_sources.Ship_Method,
5564          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
5565 FROM
5566         MSC_SYSTEM_ITEMS                        ITEM,
5567         MSC_SR_SOURCE_ORG                       SOURCE_ORG,
5568         MSC_SR_RECEIPT_ORG                      RECEIPT_ORG,
5569         MSC_SOURCING_RULES                      MSR,
5570         MSC_SR_ASSIGNMENTS                      MSRA,
5571         MSC_TP_SITE_ID_LID                      MTSIL
5572 WHERE
5573         MSRA.ASSIGNMENT_TYPE = 6 /* ITEM-ORG */
5574         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5575         AND     MSRA.PARTNER_ID IS NOT NULL
5576         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
5577         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
5578         AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
5579         AND     MTSIL.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
5580         AND     ITEM.INVENTORY_ITEM_ID = MSRA.INVENTORY_ITEM_ID
5581         AND     ITEM.PLAN_ID = -1
5582         AND     ITEM.INVENTORY_ITEM_ID = p_inventory_item_id
5583         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
5584         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
5585         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
5586 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
5587 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5588         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
5589         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
5590         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
5591         AND     MSR.STATUS = 1
5592         AND     MSR.SOURCING_RULE_TYPE = 1
5593         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
5594 ORDER BY
5595         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
5596 
5597 -- Stopping check
5598 IF (x_atp_sources.Rank.COUNT > 0) THEN
5599     IF PG_DEBUG in ('Y', 'C') THEN
5600 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 1. Search over.');
5601     END IF;
5602     return;
5603 ELSE
5604     IF l_counter = l_dist_level_type.LAST THEN
5605 	IF PG_DEBUG in ('Y', 'C') THEN
5606 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
5607 	END IF;
5608 	x_return_status := FND_API.G_RET_STS_ERROR;
5609 	return;
5610     ELSE
5611 	IF PG_DEBUG in ('Y', 'C') THEN
5612 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 1. Continuing search..');
5613 	END IF;
5614     END IF;
5615 END IF;
5616 
5617 ELSIF (l_dist_level_type(l_counter) = 2) THEN
5618 
5619 -- LEVEL 2, ASSIGNMENT TYPE 9: ITEM-REGION
5620 IF PG_DEBUG in ('Y', 'C') THEN
5621         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 2 (item-reg)');
5622 END IF;
5623 
5624 -- Search for sources for which following expression is minimum
5625 SELECT	NVL(MIN(2000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 10) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0)), 0)
5626 INTO	l_min_region_value
5627 FROM
5628 	MSC_SYSTEM_ITEMS        ITEM_INNER,
5629 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
5630 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
5631 	MSC_SOURCING_RULES      MSR_INNER,
5632 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
5633 	MSC_REGIONS_TEMP        MRT_INNER
5634 WHERE
5635 	MSRA_INNER.ASSIGNMENT_TYPE = 9
5636 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
5637 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
5638 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
5639 	AND     MRT_INNER.SESSION_ID = p_session_id
5640 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id
5641 	AND     MRT_INNER.PARTNER_SITE_ID = decode( NVL(p_partner_type, 2), 2, p_customer_site_id , 3 , p_party_site_id, 4, p_order_line_id)  --2814895
5642 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
5643 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
5644 	AND     MSRA_INNER.INVENTORY_ITEM_ID = ITEM_INNER.INVENTORY_ITEM_ID
5645 	AND     ITEM_INNER.PLAN_ID = -1
5646 	AND     ITEM_INNER.INVENTORY_ITEM_ID = p_inventory_item_id
5647 	AND     ITEM_INNER.ORGANIZATION_ID = SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID
5648 	AND     ITEM_INNER.SR_INSTANCE_ID = SOURCE_ORG_INNER.SR_INSTANCE_ID
5649 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
5650 	AND     SOURCE_ORG_INNER.SR_RECEIPT_ID = RECEIPT_ORG_INNER.SR_RECEIPT_ID
5651 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5652 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate)) >= l_sysdate
5653 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
5654 	AND     RECEIPT_ORG_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
5655 	AND     MSR_INNER.STATUS = 1
5656 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
5657 	AND     MSR_INNER.SOURCING_RULE_ID = MSRA_INNER.SOURCING_RULE_ID;
5658 
5659 IF (l_min_region_value <> 0) THEN
5660 -- Sources found.
5661 IF PG_DEBUG in ('Y', 'C') THEN
5662 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All sources found at level 2.');
5663 END IF;
5664 -- Collect the found sources and return
5665 SELECT
5666         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
5667         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
5668         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
5669         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
5670         NVL(SOURCE_ORG.RANK, -1)                        RANK,
5671         NVL(SOURCE_ORG.SOURCE_TYPE,
5672                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
5673                 to_number(null), 3, 1))                 SOURCE_TYPE,
5674         0                                               PREFERRED,
5675         -1                                              LEAD_TIME,
5676         '@@@'                                           SHIP_METHOD,
5677         NULL -- For supplier intransit LT project
5678 BULK COLLECT INTO
5679          x_atp_sources.Organization_Id,
5680          x_atp_sources.Instance_Id,
5681          x_atp_sources.Supplier_Id,
5682          x_atp_sources.Supplier_Site_Id,
5683          x_atp_sources.Rank,
5684          x_atp_sources.Source_Type,
5685          x_atp_sources.Preferred,
5686          x_atp_sources.Lead_Time,
5687          x_atp_sources.Ship_Method,
5688          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
5689 FROM
5690 	MSC_SYSTEM_ITEMS        ITEM,
5691 	MSC_SR_SOURCE_ORG       SOURCE_ORG,
5692 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
5693 	MSC_SOURCING_RULES      MSR,
5694 	MSC_SR_ASSIGNMENTS      MSRA,
5695 	MSC_REGIONS_TEMP        MRT
5696 WHERE
5697 	MSRA.ASSIGNMENT_TYPE = 9
5698 	AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5699 	AND     MSRA.REGION_ID = MRT.REGION_ID
5700 	AND     MRT.SESSION_ID = p_session_id
5701 	AND     MRT.PARTNER_SITE_ID IS NOT NULL
5702 	--AND     MRT.PARTNER_SITE_ID = p_customer_site_id
5703 	AND     MRT.PARTNER_SITE_ID = decode( NVL(p_partner_type, 2), 2, p_customer_site_id , 3 ,  p_party_site_id, 4, p_order_line_id)  --2814895
5704 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
5705 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
5706 	AND     (2000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 10) +
5707 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = l_min_region_value
5708 	AND     MSRA.INVENTORY_ITEM_ID = ITEM.INVENTORY_ITEM_ID
5709 	AND     ITEM.PLAN_ID = -1
5710 	AND     ITEM.INVENTORY_ITEM_ID = p_inventory_item_id
5711 	AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
5712 	AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
5713 	AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
5714 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
5715 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5716 	AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
5717 	AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
5718 	AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
5719 	AND     MSR.STATUS = 1
5720 	AND     MSR.SOURCING_RULE_TYPE = 1
5721 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
5722 ORDER BY
5723         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
5724 	return;
5725 ELSE
5726 -- Stopping check.
5727    IF l_counter = l_dist_level_type.LAST THEN
5728         IF PG_DEBUG in ('Y', 'C') THEN
5729             msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
5730         END IF;
5731         x_return_status := FND_API.G_RET_STS_ERROR;
5732         return;
5733    ELSE
5734 	IF PG_DEBUG in ('Y', 'C') THEN
5735 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 2. Continuing search..');
5736 	END IF;
5737    END IF;
5738 END IF;
5739 
5740 
5741 ELSIF (l_dist_level_type(l_counter) = 3) THEN
5742 
5743 -- LEVEL3, ASSIGNMENT_TYPE 5: CATEGORY-ORG
5744 IF PG_DEBUG in ('Y', 'C') THEN
5745         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 3 (cat-org)');
5746 END IF;
5747 
5748 SELECT
5749         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
5750         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
5751         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
5752         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
5753         NVL(SOURCE_ORG.RANK, -1)                        RANK,
5754         NVL(SOURCE_ORG.SOURCE_TYPE,
5755                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
5756                 to_number(null), 3, 1))                 SOURCE_TYPE,
5757         0                                               PREFERRED,
5758         -1                                              LEAD_TIME,
5759         '@@@'                                           SHIP_METHOD,
5760         NULL -- For supplier intransit LT project
5761 BULK COLLECT INTO
5762          x_atp_sources.Organization_Id,
5763          x_atp_sources.Instance_Id,
5764          x_atp_sources.Supplier_Id,
5765          x_atp_sources.Supplier_Site_Id,
5766          x_atp_sources.Rank,
5767          x_atp_sources.Source_Type,
5768          x_atp_sources.Preferred,
5769          x_atp_sources.Lead_Time,
5770          x_atp_sources.Ship_Method,
5771          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
5772 FROM
5773         MSC_ITEM_CATEGORIES     CAT,
5774         MSC_SR_SOURCE_ORG       SOURCE_ORG,
5775         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
5776         MSC_SOURCING_RULES      MSR,
5777         MSC_SR_ASSIGNMENTS      MSRA,
5778         MSC_TP_SITE_ID_LID      MTSIL
5779 WHERE
5780         MSRA.ASSIGNMENT_TYPE = 5 /* CATEGORY-ORG */
5781         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5782         AND     MSRA.PARTNER_ID IS NOT NULL
5783         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
5784         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
5785 	AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
5786 	AND     MTSIL.SR_INSTANCE_ID = CAT.SR_INSTANCE_ID
5787 	AND     CAT.INVENTORY_ITEM_ID = p_inventory_item_id
5788         AND     CAT.CATEGORY_SET_ID = MSRA.CATEGORY_SET_ID
5789         AND     CAT.CATEGORY_NAME = MSRA.CATEGORY_NAME
5790         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
5791         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
5792         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
5793 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
5794 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5795         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
5796         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
5797         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
5798         AND     MSR.STATUS = 1
5799         AND     MSR.SOURCING_RULE_TYPE = 1
5800         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
5801 ORDER BY
5802         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
5803 
5804 --Stopping check
5805 IF (x_atp_sources.Rank.COUNT > 0) THEN
5806     IF PG_DEBUG in ('Y', 'C') THEN
5807 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 3. Search over.');
5808     END IF;
5809     return;
5810 ELSE
5811     IF l_counter = l_dist_level_type.LAST THEN
5812 	IF PG_DEBUG in ('Y', 'C') THEN
5813 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
5814 	END IF;
5815 	x_return_status := FND_API.G_RET_STS_ERROR;
5816 	return;
5817     ELSE
5818 	IF PG_DEBUG in ('Y', 'C') THEN
5819 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 3. Continuing search..');
5820 	END IF;
5821     END IF;
5822 END IF;
5823 
5824 ELSIF (l_dist_level_type(l_counter) = 4) THEN
5825 
5826 -- LEVEL4, ASSIGNMENT TYPE3: ITEM
5827 IF PG_DEBUG in ('Y', 'C') THEN
5828         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 4 (item)');
5829 END IF;
5830 -- Bug 2931266. No need for customer_site_id join at this level
5831 SELECT
5832         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
5833         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
5834         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
5835         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
5836         NVL(SOURCE_ORG.RANK, -1)                        RANK,
5837         NVL(SOURCE_ORG.SOURCE_TYPE,
5838                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
5839                 to_number(null), 3, 1))                 SOURCE_TYPE,
5840         0                                               PREFERRED,
5841         -1                                              LEAD_TIME,
5842         '@@@'                                           SHIP_METHOD,
5843         NULL -- For supplier intransit LT project
5844 BULK COLLECT INTO
5845          x_atp_sources.Organization_Id,
5846          x_atp_sources.Instance_Id,
5847          x_atp_sources.Supplier_Id,
5848          x_atp_sources.Supplier_Site_Id,
5849          x_atp_sources.Rank,
5850          x_atp_sources.Source_Type,
5851          x_atp_sources.Preferred,
5852          x_atp_sources.Lead_Time,
5853          x_atp_sources.Ship_Method,
5854          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
5855 FROM
5856         MSC_SYSTEM_ITEMS                ITEM,
5857         MSC_SR_SOURCE_ORG               SOURCE_ORG,
5858         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
5859         MSC_SOURCING_RULES              MSR,
5860 --        MSC_TRADING_PARTNER_SITES       TP,
5861 --        MSC_TP_SITE_ID_LID              MTSIL,
5862         MSC_SR_ASSIGNMENTS              MSRA
5863 WHERE
5864         MSRA.ASSIGNMENT_TYPE = 3 /* ITEM */
5865         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5866         AND     MSRA.INVENTORY_ITEM_ID = p_inventory_item_id
5867         AND     ITEM.INVENTORY_ITEM_ID = MSRA.INVENTORY_ITEM_ID
5868         AND     ITEM.PLAN_ID = -1
5869 --        AND     ITEM.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
5870 --        AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
5871 --        AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
5872 --        AND     TP.PARTNER_TYPE = 2
5873         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
5874         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
5875         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
5876         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
5877         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5878         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE ,l_sysdate)) >= l_sysdate
5879         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
5880         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
5881 	AND     MSR.STATUS = 1
5882         AND     MSR.SOURCING_RULE_TYPE = 1
5883 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
5884 ORDER BY
5885         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
5886 
5887 -- Check for stopping
5888 IF (x_atp_sources.Rank.COUNT > 0) THEN
5889     IF PG_DEBUG in ('Y', 'C') THEN
5890 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 4. Search over.');
5891     END IF;
5892     return;
5893 ELSE
5894     IF l_counter = l_dist_level_type.LAST THEN
5895 	IF PG_DEBUG in ('Y', 'C') THEN
5896 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
5897 	END IF;
5898 	x_return_status := FND_API.G_RET_STS_ERROR;
5899 	return;
5900     ELSE
5901 	IF PG_DEBUG in ('Y', 'C') THEN
5902 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 4. Continuing search..');
5903 	END IF;
5904     END IF;
5905 END IF;
5906 
5907 ELSIF (l_dist_level_type(l_counter) = 5) THEN
5908 
5909 -- LEVEL 5, ASSIGNMENT TYPE 8: CATEGORY-REGION
5910 IF PG_DEBUG in ('Y', 'C') THEN
5911         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 5 (cat-reg)');
5912 END IF;
5913 -- Searching sources for min value of expression.
5914 SELECT	NVL(MIN(5000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 10) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0)), 0)
5915 INTO	l_min_region_value
5916 FROM
5917 	MSC_ITEM_CATEGORIES     CAT_INNER,
5918 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
5919 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
5920 	MSC_SOURCING_RULES      MSR_INNER,
5921 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
5922 	MSC_REGIONS_TEMP        MRT_INNER
5923 WHERE
5924 	MSRA_INNER.ASSIGNMENT_TYPE = 8 /* CATEGORY-REGION */
5925 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
5926 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
5927 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
5928 	AND     MRT_INNER.SESSION_ID = p_session_id
5929 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id --2814895
5930 	AND     MRT_INNER.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
5931 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
5932 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
5933 	AND     MSRA_INNER.CATEGORY_SET_ID = CAT_INNER.CATEGORY_SET_ID
5934 	AND     MSRA_INNER.CATEGORY_NAME = CAT_INNER.CATEGORY_NAME
5935 	AND     CAT_INNER.INVENTORY_ITEM_ID = p_inventory_item_id
5936 	AND     CAT_INNER.ORGANIZATION_ID = SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID
5937 	AND     CAT_INNER.SR_INSTANCE_ID = SOURCE_ORG_INNER.SR_INSTANCE_ID
5938 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
5939 	AND     SOURCE_ORG_INNER.SR_RECEIPT_ID = RECEIPT_ORG_INNER.SR_RECEIPT_ID
5940 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
5941 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate)) >= l_sysdate
5942 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
5943 	AND     RECEIPT_ORG_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
5944 	AND     MSR_INNER.STATUS = 1
5945 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
5946 	AND     MSR_INNER.SOURCING_RULE_ID = MSRA_INNER.SOURCING_RULE_ID;
5947 
5948 IF (l_min_region_value <> 0) THEN
5949 -- Sources found
5950         IF PG_DEBUG in ('Y', 'C') THEN
5951                 msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All sources found at level 5.');
5952         END IF;
5953 -- Collect the found sources and return.
5954 SELECT
5955         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
5956         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
5957         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
5958         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
5959         NVL(SOURCE_ORG.RANK, -1)                        RANK,
5960         NVL(SOURCE_ORG.SOURCE_TYPE,
5961                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
5962                 to_number(null), 3, 1))                 SOURCE_TYPE,
5963         0                                               PREFERRED,
5964         -1                                              LEAD_TIME,
5965         '@@@'                                           SHIP_METHOD,
5966         NULL -- For supplier intransit LT project
5967 BULK COLLECT INTO
5968          x_atp_sources.Organization_Id,
5969          x_atp_sources.Instance_Id,
5970          x_atp_sources.Supplier_Id,
5971          x_atp_sources.Supplier_Site_Id,
5972          x_atp_sources.Rank,
5973          x_atp_sources.Source_Type,
5974          x_atp_sources.Preferred,
5975          x_atp_sources.Lead_Time,
5976          x_atp_sources.Ship_Method,
5977          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
5978 FROM
5979         MSC_ITEM_CATEGORIES     CAT,
5980         MSC_SR_SOURCE_ORG       SOURCE_ORG,
5981         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
5982         MSC_SOURCING_RULES      MSR,
5983         MSC_SR_ASSIGNMENTS      MSRA,
5984         MSC_REGIONS_TEMP        MRT
5985 WHERE
5986         MSRA.ASSIGNMENT_TYPE = 8 /* CATEGORY-REGION */
5987         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
5988         AND     MSRA.REGION_ID = MRT.REGION_ID
5989         AND     MRT.PARTNER_SITE_ID IS NOT NULL
5990         AND     MRT.SESSION_ID = p_session_id
5991         --AND     MRT.PARTNER_SITE_ID = p_customer_site_id
5992         AND     MRT.PARTNER_SITE_ID = decode( NVL(p_partner_type,2), 2, p_customer_site_id , 4, p_party_site_id, 5, p_order_line_id)  --2814895
5993 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
5994 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
5995         AND     (5000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 10) +
5996 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = l_min_region_value
5997 	AND     MSRA.CATEGORY_SET_ID = CAT.CATEGORY_SET_ID
5998 	AND     MSRA.CATEGORY_NAME = CAT.CATEGORY_NAME
5999 	AND     CAT.INVENTORY_ITEM_ID = p_inventory_item_id
6000         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6001         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
6002         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6003 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6004 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6005 	AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
6006         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6007 	AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6008 	AND     MSR.STATUS = 1
6009         AND     MSR.SOURCING_RULE_TYPE = 1
6010         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6011 ORDER BY
6012         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
6013 	return;
6014 ELSE
6015 -- Sources not found. Check for stopping
6016     IF l_counter = l_dist_level_type.LAST THEN
6017 	IF PG_DEBUG in ('Y', 'C') THEN
6018 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
6019 	END IF;
6020 	x_return_status := FND_API.G_RET_STS_ERROR;
6021 	return;
6022     ELSE
6023 	IF PG_DEBUG in ('Y', 'C') THEN
6024 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 5. Continuing search..');
6025 	END IF;
6026     END IF;
6027 END IF;
6028 
6029 
6030 ELSIF (l_dist_level_type(l_counter) = 6) THEN
6031 
6032 -- LEVEL 6, ASSIGNMENT TYPE 2: CATEGORY
6033 IF PG_DEBUG in ('Y', 'C') THEN
6034         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 6 (cat)');
6035 END IF;
6036 -- Bug 2931266. No need for customer_site_id join at this level
6037 SELECT
6038         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
6039         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
6040         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
6041         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
6042         NVL(SOURCE_ORG.RANK, -1)                        RANK,
6043         NVL(SOURCE_ORG.SOURCE_TYPE,
6044                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6045                 to_number(null), 3, 1))                 SOURCE_TYPE,
6046         0                                               PREFERRED,
6047         -1                                              LEAD_TIME,
6048         '@@@'                                           SHIP_METHOD,
6049         NULL -- For supplier intransit LT project
6050 BULK COLLECT INTO
6051          x_atp_sources.Organization_Id,
6052          x_atp_sources.Instance_Id,
6053          x_atp_sources.Supplier_Id,
6054          x_atp_sources.Supplier_Site_Id,
6055          x_atp_sources.Rank,
6056          x_atp_sources.Source_Type,
6057          x_atp_sources.Preferred,
6058          x_atp_sources.Lead_Time,
6059          x_atp_sources.Ship_Method,
6060          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6061 FROM
6062         MSC_ITEM_CATEGORIES             CAT,
6063 --        MSC_TRADING_PARTNER_SITES       TP,
6064         MSC_SR_SOURCE_ORG               SOURCE_ORG,
6065         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
6066         MSC_SOURCING_RULES              MSR,
6067         MSC_SR_ASSIGNMENTS              MSRA
6068 --        MSC_TP_SITE_ID_LID              MTSIL
6069 WHERE
6070         MSRA.ASSIGNMENT_TYPE = 2 /* CATEGORY */
6071         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6072         AND     MSRA.CATEGORY_NAME = CAT.CATEGORY_NAME
6073         AND     MSRA.CATEGORY_SET_ID = CAT.CATEGORY_SET_ID
6074         AND     CAT.INVENTORY_ITEM_ID = p_inventory_item_id
6075 --        AND     CAT.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
6076 --        AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6077 --        AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
6078 --        AND     TP.PARTNER_TYPE = 2
6079         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6080         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
6081         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6082         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6083         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6084         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
6085         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6086         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6087         AND     MSR.STATUS = 1
6088         AND     MSR.SOURCING_RULE_TYPE = 1
6089 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6090 ORDER BY
6091         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
6092 
6093 -- Stopping check
6094 IF (x_atp_sources.Rank.COUNT > 0) THEN
6095     IF PG_DEBUG in ('Y', 'C') THEN
6096 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 6. Search over.');
6097     END IF;
6098     return;
6099 ELSE
6100     IF l_counter = l_dist_level_type.LAST THEN
6101 	IF PG_DEBUG in ('Y', 'C') THEN
6102 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
6103 	END IF;
6104 	x_return_status := FND_API.G_RET_STS_ERROR;
6105 	return;
6106     ELSE
6107         IF PG_DEBUG in ('Y', 'C') THEN
6108 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 6. Continuing search..');
6109 	END IF;
6110     END IF;
6111 END IF;
6112 
6113 ELSIF (l_dist_level_type(l_counter) = 7) THEN
6114 
6115 -- LEVEL 7, ASSIGNMENT_TYPE 4: ORG
6116 IF PG_DEBUG in ('Y', 'C') THEN
6117         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 7 (org)');
6118 END IF;
6119 
6120 SELECT
6121         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
6122         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
6123         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
6124         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
6125         NVL(SOURCE_ORG.RANK, -1)                        RANK,
6126         NVL(SOURCE_ORG.SOURCE_TYPE,
6127                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6128                 to_number(null), 3, 1))                 SOURCE_TYPE,
6129         0                                               PREFERRED,
6130         -1                                              LEAD_TIME,
6131         '@@@'                                           SHIP_METHOD,
6132         NULL -- For supplier intransit LT project
6133 BULK COLLECT INTO
6134          x_atp_sources.Organization_Id,
6135          x_atp_sources.Instance_Id,
6136          x_atp_sources.Supplier_Id,
6137          x_atp_sources.Supplier_Site_Id,
6138          x_atp_sources.Rank,
6139          x_atp_sources.Source_Type,
6140          x_atp_sources.Preferred,
6141          x_atp_sources.Lead_Time,
6142          x_atp_sources.Ship_Method,
6143          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6144 FROM
6145         MSC_SYSTEM_ITEMS        ITEM,
6146         MSC_SR_SOURCE_ORG       SOURCE_ORG,
6147         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
6148         MSC_SOURCING_RULES      MSR,
6149         MSC_SR_ASSIGNMENTS      MSRA,
6150         MSC_TP_SITE_ID_LID      MTSIL
6151 WHERE
6152         MSRA.ASSIGNMENT_TYPE = 4 /* ORG */
6153         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6154         AND     MSRA.PARTNER_ID IS NOT NULL
6155         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
6156         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
6157         AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6158         AND     MTSIL.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
6159         AND     ITEM.INVENTORY_ITEM_ID = p_inventory_item_id
6160         AND     ITEM.PLAN_ID = -1
6161         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6162         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
6163         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6164         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6165         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6166         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
6167         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6168         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6169         AND     MSR.STATUS = 1
6170         AND     MSR.SOURCING_RULE_TYPE = 1
6171         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6172 ORDER BY
6173         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
6174 
6175 -- Stopping Check
6176 IF (x_atp_sources.Rank.COUNT > 0) THEN
6177     IF PG_DEBUG in ('Y', 'C') THEN
6178 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 7. Search over.');
6179     END IF;
6180     return;
6181 ELSE
6182     IF l_counter = l_dist_level_type.LAST THEN
6183 	IF PG_DEBUG in ('Y', 'C') THEN
6184 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
6185 	END IF;
6186 	x_return_status := FND_API.G_RET_STS_ERROR;
6187 	return;
6188     ELSE
6189 	IF PG_DEBUG in ('Y', 'C') THEN
6190 	   msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 7. Continuing search..');
6191 	END IF;
6192     END IF;
6193 END IF;
6194 
6195 ELSIF (l_dist_level_type(l_counter) = 8) THEN
6196 
6197 -- LEVEL 8, ASSIGNMENT_TYPE 7: REGION
6198 IF PG_DEBUG in ('Y', 'C') THEN
6199         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 8 (reg)');
6200 END IF;
6201 -- Searching sources for min expression value
6202 SELECT	NVL(MIN(8000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 100) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0)), 0)
6203 INTO	l_min_region_value
6204 FROM
6205 	MSC_SYSTEM_ITEMS        ITEM_INNER,
6206 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
6207 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
6208 	MSC_SOURCING_RULES      MSR_INNER,
6209 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
6210 	MSC_REGIONS_TEMP        MRT_INNER
6211 WHERE
6212 	MSRA_INNER.ASSIGNMENT_TYPE = 7 /* REGION */
6213 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
6214 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
6215 	AND     MRT_INNER.SESSION_ID = p_session_id
6216 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id
6217 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
6218 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
6219 	AND     MRT_INNER.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
6220 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
6221 	AND     MSRA_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
6222 	AND     MSR_INNER.STATUS = 1
6223 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
6224 	AND     MSR_INNER.SOURCING_RULE_ID = RECEIPT_ORG_INNER.SOURCING_RULE_ID
6225 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6226 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate )) >= l_sysdate
6227 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
6228 	AND     RECEIPT_ORG_INNER.SR_RECEIPT_ID = SOURCE_ORG_INNER.SR_RECEIPT_ID
6229 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
6230 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID = ITEM_INNER.ORGANIZATION_ID
6231 	AND     SOURCE_ORG_INNER.SR_INSTANCE_ID = ITEM_INNER.SR_INSTANCE_ID
6232 	AND     ITEM_INNER.INVENTORY_ITEM_ID = p_inventory_item_id
6233 	AND     ITEM_INNER.PLAN_ID = -1	;
6234 
6235 IF (l_min_region_value <> 0) THEN
6236 -- Sources found
6237     IF PG_DEBUG in ('Y', 'C') THEN
6238 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All sources found at level 8.');
6239     END IF;
6240 -- Collect the sources and return.
6241 SELECT
6242         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
6243         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
6244         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
6245         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
6246         NVL(SOURCE_ORG.RANK, -1)                        RANK,
6247         NVL(SOURCE_ORG.SOURCE_TYPE,
6248                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6249                 to_number(null), 3, 1))                 SOURCE_TYPE,
6250         0                                               PREFERRED,
6251         -1                                              LEAD_TIME,
6252         '@@@'                                           SHIP_METHOD,
6253         NULL -- For supplier intransit LT project
6254 BULK COLLECT INTO
6255          x_atp_sources.Organization_Id,
6256          x_atp_sources.Instance_Id,
6257          x_atp_sources.Supplier_Id,
6258          x_atp_sources.Supplier_Site_Id,
6259          x_atp_sources.Rank,
6260          x_atp_sources.Source_Type,
6261          x_atp_sources.Preferred,
6262          x_atp_sources.Lead_Time,
6263          x_atp_sources.Ship_Method,
6264          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6265 FROM
6266         MSC_SYSTEM_ITEMS        ITEM,
6267         MSC_SR_SOURCE_ORG       SOURCE_ORG,
6268         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
6269         MSC_SOURCING_RULES      MSR,
6270         MSC_SR_ASSIGNMENTS      MSRA,
6271         MSC_REGIONS_TEMP        MRT
6272 WHERE
6273         MSRA.ASSIGNMENT_TYPE = 7 /* REGION */
6274         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6275 	AND     MSRA.REGION_ID = MRT.REGION_ID
6276         AND     MRT.PARTNER_SITE_ID IS NOT NULL
6277         AND     MRT.SESSION_ID = p_session_id
6278         --AND     MRT.PARTNER_SITE_ID = p_customer_site_id
6279 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
6280 	AND     MRT.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
6281 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
6282         AND     (8000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 100) +
6283 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = l_min_region_value
6284 	AND     MSRA.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6285         AND     MSR.STATUS = 1
6286         AND     MSR.SOURCING_RULE_TYPE = 1
6287         AND     MSR.SOURCING_RULE_ID = RECEIPT_ORG.SOURCING_RULE_ID
6288         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6289         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
6290         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6291         AND     RECEIPT_ORG.SR_RECEIPT_ID = SOURCE_ORG.SR_RECEIPT_ID
6292         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID = ITEM.ORGANIZATION_ID
6293         AND     SOURCE_ORG.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
6294         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6295 	AND     ITEM.INVENTORY_ITEM_ID = p_inventory_item_id
6296 	AND     ITEM.PLAN_ID = -1
6297 ORDER BY
6298         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
6299         return;
6300 ELSE
6301 -- No sources found. Check for stopping
6302     IF l_counter = l_dist_level_type.LAST THEN
6303 	IF PG_DEBUG in ('Y', 'C') THEN
6304 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
6305 	END IF;
6306 	x_return_status := FND_API.G_RET_STS_ERROR;
6307 	return;
6308     ELSE
6309 	IF PG_DEBUG in ('Y', 'C') THEN
6310 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No sources found at level 8. Continuing search..');
6311 	END IF;
6312     END IF;
6313 END IF;
6314 
6315 
6316 ELSIF (l_dist_level_type(l_counter) = 9) THEN
6317 
6318 -- LEVEL 9, ASSIGNMENT_TYPE 1: GLOBAL
6319 IF PG_DEBUG in ('Y', 'C') THEN
6320         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 9 (global)');
6321 END IF;
6322 -- Bug 2931266. No need for customer_site_id join at this level
6323 SELECT
6324         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1)      SOURCE_ORGANIZATION_ID,
6325         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1)      SOURCE_ORG_INSTANCE_ID,
6326         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1)           VENDOR_ID,
6327         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1)      VENDOR_SITE_ID,
6328         NVL(SOURCE_ORG.RANK, -1)                        RANK,
6329         NVL(SOURCE_ORG.SOURCE_TYPE,
6330                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6331                 to_number(null), 3, 1))                 SOURCE_TYPE,
6332         0                                               PREFERRED,
6333         -1                                              LEAD_TIME,
6334         '@@@'                                           SHIP_METHOD,
6335         NULL -- For supplier intransit LT project
6336 BULK COLLECT INTO
6337          x_atp_sources.Organization_Id,
6338          x_atp_sources.Instance_Id,
6339          x_atp_sources.Supplier_Id,
6340          x_atp_sources.Supplier_Site_Id,
6341          x_atp_sources.Rank,
6342          x_atp_sources.Source_Type,
6343          x_atp_sources.Preferred,
6344          x_atp_sources.Lead_Time,
6345          x_atp_sources.Ship_Method,
6346          x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6347 FROM
6348         MSC_SYSTEM_ITEMS                ITEM,
6349 --        MSC_TRADING_PARTNER_SITES       TP,
6350         MSC_SR_SOURCE_ORG               SOURCE_ORG,
6351         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
6352         MSC_SOURCING_RULES              MSR,
6353         MSC_SR_ASSIGNMENTS              MSRA
6354 --        MSC_TP_SITE_ID_LID              MTSIL
6355 WHERE
6356         MSRA.ASSIGNMENT_TYPE = 1 /* GLOBAL */
6357         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6358         AND     MSRA.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6359         AND     MSR.STATUS = 1
6360         AND     MSR.SOURCING_RULE_TYPE = 1
6361         AND     MSR.SOURCING_RULE_ID = RECEIPT_ORG.SOURCING_RULE_ID
6362         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6363         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
6364         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6365         AND     RECEIPT_ORG.SR_RECEIPT_ID = SOURCE_ORG.SR_RECEIPT_ID
6366         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6367 	AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID = ITEM.ORGANIZATION_ID
6368         AND     SOURCE_ORG.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
6369         AND     ITEM.INVENTORY_ITEM_ID = p_inventory_item_id
6370 	AND     ITEM.PLAN_ID = -1
6371 /*
6372 	AND     ITEM.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
6373         AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6374         AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
6375         AND     TP.PARTNER_TYPE = 2
6376 */
6377 ORDER BY
6378         SOURCE_ORG.RANK ASC, SOURCE_ORG.ALLOCATION_PERCENT DESC;
6379 
6380 -- Stopping Check
6381 IF (x_atp_sources.Rank.COUNT > 0) THEN
6382     IF PG_DEBUG in ('Y', 'C') THEN
6383 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 9. Search over.');
6384     END IF;
6385     return;
6386 ELSE
6387     IF l_counter = l_dist_level_type.LAST THEN
6388 	IF PG_DEBUG in ('Y', 'C') THEN
6389 	    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Could not find sources for the item in the entire search');
6390 	END IF;
6391 	x_return_status := FND_API.G_RET_STS_ERROR;
6392 	return;
6393     END IF;
6394 END IF;
6395 
6396 END IF; -- l_dist_level_type
6397 
6398 END LOOP; -- Loop on levels
6399 
6400 ELSE -- of case1
6401 -- case2 begins
6402 
6403 IF PG_DEBUG in ('Y', 'C') THEN
6404         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Starting search for ship set sources');
6405 END IF;
6406 
6407 -- Delete the table MSC_ATP_SOURCES_TEMP
6408 DELETE MSC_ATP_SOURCES_TEMP;
6409 
6410 FOR l_counter IN l_dist_level_type.FIRST..l_dist_level_type.LAST LOOP
6411 
6412 IF (l_dist_level_type(l_counter) = 1) THEN
6413 
6414 -- LEVEL 1, ASSIGNMENT TYPE 6: ITEM-ORG
6415 IF PG_DEBUG in ('Y', 'C') THEN
6416         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 1 (item-org)');
6417 END IF;
6418 
6419 -- At first level, visited_flag in table msc_ship_set_temp is 0 for all rows.
6420 -- Therefore no need for clause 'AND msst.visisble_flag = 0'.
6421 
6422 -- Find and insert the sources in msc_atp_sources_temp
6423 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
6424         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
6425 SELECT
6426         MSST.INVENTORY_ITEM_ID,
6427         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
6428         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
6429         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
6430         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
6431         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
6432         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
6433                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6434                         to_number(null), 3, 1)),
6435         0,
6436         -1,
6437         '@@@'
6438 FROM
6439         MSC_SYSTEM_ITEMS                        ITEM,
6440         MSC_SR_SOURCE_ORG                       SOURCE_ORG,
6441         MSC_SR_RECEIPT_ORG                      RECEIPT_ORG,
6442         MSC_SOURCING_RULES                      MSR,
6443         MSC_SR_ASSIGNMENTS                      MSRA,
6444         MSC_TP_SITE_ID_LID                      MTSIL,
6445         MSC_SHIP_SET_TEMP                       MSST
6446 WHERE
6447         MSRA.ASSIGNMENT_TYPE = 6 /* ITEM-ORG */
6448         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6449         AND     MSRA.PARTNER_ID IS NOT NULL
6450         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
6451         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
6452         AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6453         AND     MTSIL.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
6454         AND     ITEM.INVENTORY_ITEM_ID = MSRA.INVENTORY_ITEM_ID
6455         AND     ITEM.PLAN_ID = -1
6456         AND     ITEM.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
6457         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6458         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
6459         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6460 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6461 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6462         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
6463         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6464         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6465         AND     MSR.STATUS = 1
6466         AND     MSR.SOURCING_RULE_TYPE = 1
6467         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6468 GROUP BY
6469         MSST.INVENTORY_ITEM_ID,
6470         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6471         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
6472         SOURCE_ORG.SOURCE_PARTNER_ID,
6473         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
6474 
6475 l_inserted_rows	:= SQL%ROWCOUNT;
6476 
6477 IF (l_inserted_rows > 0) THEN
6478 
6479     -- Found some sources for some items.
6480     IF PG_DEBUG in ('Y', 'C') THEN
6481 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 1');
6482     END IF;
6483 
6484 
6485     -- Mark all the items for which sources are/have been found.
6486     UPDATE	msc_ship_set_temp
6487     set		visited_flag = 1
6488     where	inventory_item_id in (select inventory_item_id from msc_atp_sources_temp);
6489 
6490     l_updated_rows := SQL%ROWCOUNT;
6491 
6492     -- Check if all the items in ship set are processed.
6493     IF (l_updated_rows = p_ship_set_item_count) THEN
6494 
6495 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
6496 	SELECT  Organization_Id,
6497 		Instance_Id,
6498 		Supplier_Id,
6499 		Supplier_Site_Id,
6500 		sum(Rank),  -- order by cum rank instead of group by rank
6501 		Source_Type,
6502 		0,
6503 		-1,
6504 		'@@@',
6505 		NULL -- For supplier intransit LT project
6506 	BULK COLLECT INTO
6507 		x_atp_sources.Organization_Id,
6508 		x_atp_sources.Instance_Id,
6509 		x_atp_sources.Supplier_Id,
6510 		x_atp_sources.Supplier_Site_Id,
6511 		x_atp_sources.Rank,
6512 		x_atp_sources.Source_Type,
6513 		x_atp_sources.Preferred,
6514 		x_atp_sources.Lead_Time,
6515 		x_atp_sources.Ship_Method,
6516                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6517 	FROM    MSC_ATP_SOURCES_TEMP
6518 	GROUP BY
6519 		Organization_Id,
6520 		Instance_Id,
6521 		Supplier_Id,
6522 		Supplier_Site_Id,
6523 		-- Rank, order by cum rank instead of group by rank
6524 		Source_Type
6525 	HAVING  count(*) = p_ship_set_item_count
6526 	ORDER BY 5; -- order by cum rank instead of group by rank
6527 
6528 	IF (x_atp_sources.Rank.COUNT > 0) THEN
6529 		-- Common sources found
6530 		IF PG_DEBUG in ('Y', 'C') THEN
6531 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 1');
6532 		END IF;
6533 		return;
6534 	ELSE
6535 		-- No common sources found
6536 		IF PG_DEBUG in ('Y', 'C') THEN
6537 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
6538 		END IF;
6539 		x_return_status := FND_API.G_RET_STS_ERROR;
6540 		return;
6541 	END IF; -- common sources
6542 
6543      END IF; -- all items of ship set
6544 END IF; -- some sources found
6545 
6546 -- Check the stopping condition.
6547 IF l_counter = l_dist_level_type.LAST THEN
6548     IF PG_DEBUG in ('Y', 'C') THEN
6549 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
6550     END IF;
6551     x_return_status := FND_API.G_RET_STS_ERROR;
6552      return;
6553 ELSE
6554     IF PG_DEBUG in ('Y', 'C') THEN
6555 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
6556     END IF;
6557 END IF;
6558 
6559 
6560 ELSIF (l_dist_level_type(l_counter) = 2) THEN
6561 
6562 -- LEVEL 2, ASSIGNMENT TYPE 9: ITEM-REGION
6563 IF PG_DEBUG in ('Y', 'C') THEN
6564         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 2 (item-reg)');
6565 END IF;
6566 
6567 -- Find sources for min value of expression.
6568 SELECT
6569 	MSST_INNER.INVENTORY_ITEM_ID,
6570 	MIN(2000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 10) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0))
6571 BULK COLLECT INTO
6572 	l_inventory_item_id_arr,
6573 	l_min_region_value_arr
6574 FROM
6575 	MSC_SYSTEM_ITEMS        ITEM_INNER,
6576 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
6577 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
6578 	MSC_SOURCING_RULES      MSR_INNER,
6579 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
6580 	MSC_REGIONS_TEMP        MRT_INNER,
6581 	MSC_SHIP_SET_TEMP       MSST_INNER
6582 WHERE
6583 	MSRA_INNER.ASSIGNMENT_TYPE = 9
6584 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
6585 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
6586 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
6587 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id
6588 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
6589 	AND     MRT_INNER.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
6590 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
6591 	AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
6592 	AND     MSRA_INNER.INVENTORY_ITEM_ID = ITEM_INNER.INVENTORY_ITEM_ID
6593 	AND     ITEM_INNER.PLAN_ID = -1
6594 	AND     ITEM_INNER.INVENTORY_ITEM_ID = MSST_INNER.INVENTORY_ITEM_ID
6595 	AND	MSST_INNER.VISITED_FLAG = 0
6596 	AND     ITEM_INNER.ORGANIZATION_ID = SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID
6597 	AND     ITEM_INNER.SR_INSTANCE_ID = SOURCE_ORG_INNER.SR_INSTANCE_ID
6598 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
6599 	AND     SOURCE_ORG_INNER.SR_RECEIPT_ID = RECEIPT_ORG_INNER.SR_RECEIPT_ID
6600 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6601 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate)) >= l_sysdate
6602 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
6603 	AND     RECEIPT_ORG_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
6604 	AND     MSR_INNER.STATUS = 1
6605 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
6606 	AND     MSR_INNER.SOURCING_RULE_ID = MSRA_INNER.SOURCING_RULE_ID
6607 GROUP BY
6608 	MSST_INNER.INVENTORY_ITEM_ID;
6609 
6610 IF (l_inventory_item_id_arr.COUNT > 0) THEN
6611 
6612 -- Step1: All the items for which sources are found,
6613 -- update column min_region_value and visited_flag in msc_ship_set_temp.
6614 IF PG_DEBUG in ('Y', 'C') THEN
6615     msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'For following items, the min region value found');
6616     FOR i IN 1..l_inventory_item_id_arr.COUNT LOOP
6617       msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Item: '|| l_inventory_item_id_arr(i)
6618 			|| ' min region value: ' || l_min_region_value_arr(i));
6619     END LOOP;
6620 END IF;
6621 
6622 
6623 FORALL	i IN l_inventory_item_id_arr.FIRST..l_inventory_item_id_arr.LAST
6624 UPDATE	msc_ship_set_temp
6625 SET	min_region_value = l_min_region_value_arr(i),
6626 	visited_flag = 1
6627 WHERE	inventory_item_id = l_inventory_item_id_arr(i);
6628 
6629 -- Step2: For the updated items find all the sources and store them in msc_atp_sources_temp
6630 -- This is done by adding clause AND (expr) = msst.min_region_value
6631 -- and removing the clause AND visited_flag = 0
6632 
6633 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
6634         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
6635 SELECT
6636         MSST.INVENTORY_ITEM_ID,
6637         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
6638         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
6639         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
6640         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
6641         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
6642         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
6643                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6644                         to_number(null), 3, 1)),
6645         0,
6646         -1,
6647         '@@@'
6648 FROM
6649         MSC_SYSTEM_ITEMS        ITEM,
6650         MSC_SR_SOURCE_ORG       SOURCE_ORG,
6651         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
6652         MSC_SOURCING_RULES      MSR,
6653         MSC_SR_ASSIGNMENTS      MSRA,
6654         MSC_REGIONS_TEMP        MRT,
6655         MSC_SHIP_SET_TEMP       MSST
6656 WHERE
6657 	MSRA.ASSIGNMENT_TYPE = 9
6658 	AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6659 	AND     MSRA.REGION_ID = MRT.REGION_ID
6660 	AND     MRT.SESSION_ID = p_session_id
6661 	AND     MRT.PARTNER_SITE_ID IS NOT NULL
6662 	--AND     MRT.PARTNER_SITE_ID = p_customer_site_id
6663 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
6664 	AND     MRT.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
6665 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
6666 	AND     (2000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 10) +
6667 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = MSST.MIN_REGION_VALUE
6668 	AND     MSRA.INVENTORY_ITEM_ID = ITEM.INVENTORY_ITEM_ID
6669 	AND     ITEM.PLAN_ID = -1
6670 	AND     ITEM.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
6671 	AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6672 	AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
6673 	AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6674 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6675 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6676 	AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
6677 	AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6678 	AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6679 	AND     MSR.STATUS = 1
6680 	AND     MSR.SOURCING_RULE_TYPE = 1
6681 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6682 GROUP BY
6683         MSST.INVENTORY_ITEM_ID,
6684         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6685         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
6686         SOURCE_ORG.SOURCE_PARTNER_ID,
6687         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
6688 
6689 -- Step3: Check if I need to stop here.
6690 
6691 SELECT	COUNT(*)
6692 INTO	l_items_visited
6693 FROM	MSC_SHIP_SET_TEMP
6694 WHERE	VISITED_FLAG = 1;
6695 
6696 IF (l_items_visited = p_ship_set_item_count) THEN
6697 -- We have to stop search and return from here. But before returning,
6698 -- We need to check whether the search was fruitful or not
6699 -- If search was fruitful set return_status to SUCCESS else FAILURE
6700 
6701 	SELECT  Organization_Id,
6702 		Instance_Id,
6703 		Supplier_Id,
6704 		Supplier_Site_Id,
6705 		sum(Rank),  -- order by cum rank instead of group by rank
6706 		Source_Type,
6707 		0,
6708 		-1,
6709 		'@@@',
6710 		NULL -- For supplier intransit LT project
6711 	BULK COLLECT INTO
6712 		x_atp_sources.Organization_Id,
6713 		x_atp_sources.Instance_Id,
6714 		x_atp_sources.Supplier_Id,
6715 		x_atp_sources.Supplier_Site_Id,
6716 		x_atp_sources.Rank,
6717 		x_atp_sources.Source_Type,
6718 		x_atp_sources.Preferred,
6719 		x_atp_sources.Lead_Time,
6720 		x_atp_sources.Ship_Method,
6721                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6722 	FROM    MSC_ATP_SOURCES_TEMP
6723 	GROUP BY
6724 		Organization_Id,
6725 		Instance_Id,
6726 		Supplier_Id,
6727 		Supplier_Site_Id,
6728 		-- Rank,  order by cum rank instead of group by rank
6729 		Source_Type
6730 	HAVING  count(*) = p_ship_set_item_count
6731 	ORDER BY 5;  -- Rank;  order by cum rank instead of group by rank
6732 
6733 	IF (x_atp_sources.Rank.COUNT > 0) THEN
6734 		IF PG_DEBUG in ('Y', 'C') THEN
6735 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 2. Search over.');
6736 		END IF;
6737 		return;
6738 	ELSE
6739 		IF PG_DEBUG in ('Y', 'C') THEN
6740 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for the ship set.');
6741 		END IF;
6742 		x_return_status := FND_API.G_RET_STS_ERROR;
6743 		return;
6744 	END IF;
6745 
6746 END IF; -- All items in ship set
6747 
6748 END IF; -- some sources found
6749 
6750 -- Check the stopping condition.
6751 IF l_counter = l_dist_level_type.LAST THEN
6752     IF PG_DEBUG in ('Y', 'C') THEN
6753 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
6754     END IF;
6755     x_return_status := FND_API.G_RET_STS_ERROR;
6756      return;
6757 ELSE
6758     IF PG_DEBUG in ('Y', 'C') THEN
6759 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
6760     END IF;
6761 END IF;
6762 
6763 ELSIF (l_dist_level_type(l_counter) = 3) THEN
6764 
6765 -- LEVEL3, ASSIGNMENT_TYPE 5: CATEGORY-ORG
6766 
6767 IF PG_DEBUG in ('Y', 'C') THEN
6768         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 3 (cat-org)');
6769 END IF;
6770 
6771 -- Find and insert the sources in msc_atp_sources_temp
6772 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
6773         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
6774 SELECT
6775         MSST.INVENTORY_ITEM_ID,
6776         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
6777         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
6778         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
6779         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
6780         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
6781         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
6782                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6783                         to_number(null), 3, 1)),
6784         0,
6785         -1,
6786         '@@@'
6787 FROM
6788         MSC_ITEM_CATEGORIES     CAT,
6789         MSC_SR_SOURCE_ORG       SOURCE_ORG,
6790         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
6791         MSC_SOURCING_RULES      MSR,
6792         MSC_SR_ASSIGNMENTS      MSRA,
6793         MSC_TP_SITE_ID_LID      MTSIL,
6794         MSC_SHIP_SET_TEMP       MSST
6795 WHERE
6796         MSRA.ASSIGNMENT_TYPE = 5 /* CATEGORY-ORG */
6797         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6798         AND     MSRA.PARTNER_ID IS NOT NULL
6799         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
6800         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
6801 	AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6802 	AND     MTSIL.SR_INSTANCE_ID = CAT.SR_INSTANCE_ID
6803 	AND     CAT.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
6804         AND     MSST.VISITED_FLAG = 0
6805         AND     CAT.CATEGORY_SET_ID = MSRA.CATEGORY_SET_ID
6806         AND     CAT.CATEGORY_NAME = MSRA.CATEGORY_NAME
6807         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6808         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
6809         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6810 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6811 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6812         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
6813         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6814         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6815         AND     MSR.STATUS = 1
6816         AND     MSR.SOURCING_RULE_TYPE = 1
6817         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6818 GROUP BY
6819         MSST.INVENTORY_ITEM_ID,
6820         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6821         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
6822         SOURCE_ORG.SOURCE_PARTNER_ID,
6823         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
6824 
6825 l_inserted_rows	:= SQL%ROWCOUNT;
6826 
6827 IF (l_inserted_rows > 0) THEN
6828 
6829     -- Found some sources for some items.
6830     IF PG_DEBUG in ('Y', 'C') THEN
6831 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 3');
6832     END IF;
6833 
6834     -- Mark all the items for which sources are/have been found.
6835     UPDATE	msc_ship_set_temp
6836     set		visited_flag = 1
6837     where	inventory_item_id in (select inventory_item_id from msc_atp_sources_temp);
6838 
6839     l_updated_rows := SQL%ROWCOUNT;
6840 
6841     -- Check if all the items in ship set are processed.
6842     IF (l_updated_rows = p_ship_set_item_count) THEN
6843 
6844 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
6845 	SELECT  Organization_Id,
6846 		Instance_Id,
6847 		Supplier_Id,
6848 		Supplier_Site_Id,
6849 		sum(Rank),  -- order by cum rank instead of group by rank
6850 		Source_Type,
6851 		0,
6852 		-1,
6853 		'@@@',
6854 		NULL -- For supplier intransit LT project
6855 	BULK COLLECT INTO
6856 		x_atp_sources.Organization_Id,
6857 		x_atp_sources.Instance_Id,
6858 		x_atp_sources.Supplier_Id,
6859 		x_atp_sources.Supplier_Site_Id,
6860 		x_atp_sources.Rank,
6861 		x_atp_sources.Source_Type,
6862 		x_atp_sources.Preferred,
6863 		x_atp_sources.Lead_Time,
6864 		x_atp_sources.Ship_Method,
6865                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
6866 	FROM    MSC_ATP_SOURCES_TEMP
6867 	GROUP BY
6868 		Organization_Id,
6869 		Instance_Id,
6870 		Supplier_Id,
6871 		Supplier_Site_Id,
6872 		-- Rank, order by cum rank instead of group by rank
6873 		Source_Type
6874 	HAVING  count(*) = p_ship_set_item_count
6875 	ORDER BY 5; -- Rank; order by cum rank instead of group by rank
6876 
6877 	IF (x_atp_sources.Rank.COUNT > 0) THEN
6878 		-- Common sources found
6879 		IF PG_DEBUG in ('Y', 'C') THEN
6880 		   msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 3');
6881 		END IF;
6882 		return;
6883 	ELSE
6884 		-- No common sources found
6885 		IF PG_DEBUG in ('Y', 'C') THEN
6886 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
6887 		END IF;
6888 		x_return_status := FND_API.G_RET_STS_ERROR;
6889 		return;
6890 	END IF; -- common sources
6891 
6892      END IF; -- all items of ship set
6893 END IF; -- some sources found
6894 
6895 -- Check the stopping condition.
6896 IF l_counter = l_dist_level_type.LAST THEN
6897     IF PG_DEBUG in ('Y', 'C') THEN
6898 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
6899     END IF;
6900     x_return_status := FND_API.G_RET_STS_ERROR;
6901      return;
6902 ELSE
6903     IF PG_DEBUG in ('Y', 'C') THEN
6904 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
6905     END IF;
6906 END IF;
6907 
6908 ELSIF (l_dist_level_type(l_counter) = 4) THEN
6909 
6910 -- LEVEL4, ASSIGNMENT TYPE3: ITEM
6911 IF PG_DEBUG in ('Y', 'C') THEN
6912         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 4 (item)');
6913 END IF;
6914 -- Bug 2931266. No need for customer_site_id join at this level
6915 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
6916         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
6917 SELECT
6918         MSST.INVENTORY_ITEM_ID,
6919         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
6920         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
6921         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
6922         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
6923         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
6924         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
6925                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6926                         to_number(null), 3, 1)),
6927         0,
6928         -1,
6929         '@@@'
6930 FROM
6931         MSC_SYSTEM_ITEMS                ITEM,
6932 --        MSC_TRADING_PARTNER_SITES       TP,
6933         MSC_SR_SOURCE_ORG               SOURCE_ORG,
6934         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
6935         MSC_SOURCING_RULES              MSR,
6936         MSC_SR_ASSIGNMENTS              MSRA,
6937 --        MSC_TP_SITE_ID_LID              MTSIL,
6938         MSC_SHIP_SET_TEMP               MSST
6939 WHERE
6940 
6941         MSRA.ASSIGNMENT_TYPE = 3 /* ITEM */
6942         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
6943         AND     MSRA.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
6944 	AND	MSST.VISITED_FLAG = 0
6945 	AND     ITEM.INVENTORY_ITEM_ID = MSRA.INVENTORY_ITEM_ID
6946 	AND     ITEM.PLAN_ID = -1
6947 --	AND     ITEM.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
6948 --        AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
6949 --        AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
6950 --        AND     TP.PARTNER_TYPE = 2
6951         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
6952         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
6953         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
6954         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
6955         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
6956         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE ,l_sysdate)) >= l_sysdate
6957         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
6958         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
6959 	AND     MSR.STATUS = 1
6960         AND     MSR.SOURCING_RULE_TYPE = 1
6961 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
6962 GROUP BY
6963         MSST.INVENTORY_ITEM_ID,
6964         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
6965         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
6966         SOURCE_ORG.SOURCE_PARTNER_ID,
6967         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
6968 l_inserted_rows	:= SQL%ROWCOUNT;
6969 
6970 IF (l_inserted_rows > 0) THEN
6971 
6972     -- Found some sources for some items.
6973     IF PG_DEBUG in ('Y', 'C') THEN
6974 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 4');
6975     END IF;
6976 
6977     -- Mark all the items for which sources are/have been found.
6978     UPDATE	msc_ship_set_temp
6979     set		visited_flag = 1
6980     where	inventory_item_id in (select distinct(inventory_item_id) from msc_atp_sources_temp);
6981 
6982     l_updated_rows := SQL%ROWCOUNT;
6983 
6984     -- Check if all the items in ship set are processed.
6985     IF (l_updated_rows = p_ship_set_item_count) THEN
6986 
6987 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
6988 	SELECT  Organization_Id,
6989 		Instance_Id,
6990 		Supplier_Id,
6991 		Supplier_Site_Id,
6992 		sum(Rank),  -- order by cum rank instead of group by rank
6993 		Source_Type,
6994 		0,
6995 		-1,
6996 		'@@@',
6997 		NULL -- For supplier intransit LT project
6998 	BULK COLLECT INTO
6999 		x_atp_sources.Organization_Id,
7000 		x_atp_sources.Instance_Id,
7001 		x_atp_sources.Supplier_Id,
7002 		x_atp_sources.Supplier_Site_Id,
7003 		x_atp_sources.Rank,
7004 		x_atp_sources.Source_Type,
7005 		x_atp_sources.Preferred,
7006 		x_atp_sources.Lead_Time,
7007 		x_atp_sources.Ship_Method,
7008                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7009 	FROM    MSC_ATP_SOURCES_TEMP
7010 	GROUP BY
7011 		Organization_Id,
7012 		Instance_Id,
7013 		Supplier_Id,
7014 		Supplier_Site_Id,
7015 		-- Rank,  order by cum rank instead of group by rank
7016 		Source_Type
7017 	HAVING  count(*) = p_ship_set_item_count
7018 	ORDER BY 5; -- Rank; order by cum rank instead of group by rank
7019 
7020 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7021 		-- Common sources found
7022 		IF PG_DEBUG in ('Y', 'C') THEN
7023 		    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 4');
7024 		END IF;
7025 		return;
7026 	ELSE
7027 		-- No common sources found
7028 		IF PG_DEBUG in ('Y', 'C') THEN
7029 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
7030 		END IF;
7031 		x_return_status := FND_API.G_RET_STS_ERROR;
7032 		return;
7033 	END IF; -- common sources
7034 
7035      END IF; -- all items of ship set
7036 END IF; -- some sources found
7037 
7038 -- Check the stopping condition.
7039 IF l_counter = l_dist_level_type.LAST THEN
7040     IF PG_DEBUG in ('Y', 'C') THEN
7041 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
7042     END IF;
7043     x_return_status := FND_API.G_RET_STS_ERROR;
7044      return;
7045 ELSE
7046     IF PG_DEBUG in ('Y', 'C') THEN
7047 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
7048     END IF;
7049 END IF;
7050 
7051 ELSIF (l_dist_level_type(l_counter) = 5) THEN
7052 
7053 -- LEVEL 5, ASSIGNMENT TYPE 8: CATEGORY-REGION
7054 IF PG_DEBUG in ('Y', 'C') THEN
7055         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 5 (cat-reg)');
7056 END IF;
7057 
7058 SELECT
7059 	MSST_INNER.INVENTORY_ITEM_ID,
7060 	MIN(5000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 10) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0))
7061 BULK COLLECT INTO
7062 	l_inventory_item_id_arr,
7063 	l_min_region_value_arr
7064 FROM
7065 	MSC_ITEM_CATEGORIES     CAT_INNER,
7066 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
7067 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
7068 	MSC_SOURCING_RULES      MSR_INNER,
7069 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
7070 	MSC_REGIONS_TEMP        MRT_INNER,
7071 	MSC_SHIP_SET_TEMP       MSST_INNER
7072 WHERE
7073 
7074 	MSRA_INNER.ASSIGNMENT_TYPE = 8 /* CATEGORY-REGION */
7075 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
7076 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
7077 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
7078 	AND     MRT_INNER.SESSION_ID = p_session_id
7079 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id
7080 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
7081 	AND     MRT_INNER.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
7082 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
7083 	AND     MSRA_INNER.CATEGORY_SET_ID = CAT_INNER.CATEGORY_SET_ID
7084 	AND     MSRA_INNER.CATEGORY_NAME = CAT_INNER.CATEGORY_NAME
7085 	AND     CAT_INNER.INVENTORY_ITEM_ID = MSST_INNER.INVENTORY_ITEM_ID
7086 	AND	MSST_INNER.VISITED_FLAG = 0
7087 	AND     CAT_INNER.ORGANIZATION_ID = SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID
7088 	AND     CAT_INNER.SR_INSTANCE_ID = SOURCE_ORG_INNER.SR_INSTANCE_ID
7089 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
7090 	AND     SOURCE_ORG_INNER.SR_RECEIPT_ID = RECEIPT_ORG_INNER.SR_RECEIPT_ID
7091 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7092 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate)) >= l_sysdate
7093 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
7094 	AND     RECEIPT_ORG_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
7095 	AND     MSR_INNER.STATUS = 1
7096 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
7097 	AND     MSR_INNER.SOURCING_RULE_ID = MSRA_INNER.SOURCING_RULE_ID
7098 GROUP BY	MSST_INNER.INVENTORY_ITEM_ID;
7099 
7100 IF (l_inventory_item_id_arr.COUNT > 0) THEN
7101 
7102 -- Step1: For all the items found, update column min_region_value in msc_ship_set_temp.
7103 -- Also update the column visited_flag
7104 IF PG_DEBUG in ('Y', 'C') THEN
7105     msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'For following items, the min region value found');
7106     FOR i IN 1..l_inventory_item_id_arr.COUNT LOOP
7107       msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Item: '|| l_inventory_item_id_arr(i)
7108 			|| ' min region value: ' || l_min_region_value_arr(i));
7109     END LOOP;
7110 END IF;
7111 
7112 FORALL	i IN l_inventory_item_id_arr.FIRST..l_inventory_item_id_arr.LAST
7113 UPDATE	msc_ship_set_temp
7114 SET		min_region_value = l_min_region_value_arr(i),
7115 		visited_flag = 1
7116 WHERE	inventory_item_id = l_inventory_item_id_arr(i);
7117 
7118 -- Step2: For the updated items find all the sources and store them in msc_atp_sources_temp
7119 -- This is done by adding clause AND (expr) = msst.min_region_value
7120 -- and removing the clause AND visited_flag = 0
7121 
7122 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
7123         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
7124 SELECT
7125         MSST.INVENTORY_ITEM_ID,
7126         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
7127         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
7128         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
7129         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
7130         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
7131         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
7132                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7133                         to_number(null), 3, 1)),
7134         0,
7135         -1,
7136         '@@@'
7137 FROM
7138         MSC_ITEM_CATEGORIES     CAT,
7139         MSC_SR_SOURCE_ORG       SOURCE_ORG,
7140         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
7141         MSC_SOURCING_RULES      MSR,
7142         MSC_SR_ASSIGNMENTS      MSRA,
7143         MSC_REGIONS_TEMP        MRT,
7144         MSC_SHIP_SET_TEMP       MSST
7145 WHERE
7146 
7147         MSRA.ASSIGNMENT_TYPE = 8 /* CATEGORY-REGION */
7148         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
7149         AND     MSRA.REGION_ID = MRT.REGION_ID
7150         AND     MRT.PARTNER_SITE_ID IS NOT NULL
7151         AND     MRT.SESSION_ID = p_session_id
7152         --AND     MRT.PARTNER_SITE_ID = p_customer_site_id
7153 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
7154 	AND     MRT.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
7155 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
7156         AND     (5000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 10) +
7157 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = MSST.MIN_REGION_VALUE
7158 	AND     MSRA.CATEGORY_SET_ID = CAT.CATEGORY_SET_ID
7159 	AND     MSRA.CATEGORY_NAME = CAT.CATEGORY_NAME
7160 	AND     CAT.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
7161         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
7162         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
7163         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
7164 	AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
7165 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7166 	AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
7167         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
7168 	AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
7169 	AND     MSR.STATUS = 1
7170         AND     MSR.SOURCING_RULE_TYPE = 1
7171         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
7172 GROUP BY
7173         MSST.INVENTORY_ITEM_ID,
7174         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7175         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
7176         SOURCE_ORG.SOURCE_PARTNER_ID,
7177         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
7178 
7179 -- Step3: Check if I need to stop here.
7180 
7181 SELECT	COUNT(*)
7182 INTO	l_items_visited
7183 FROM	MSC_SHIP_SET_TEMP
7184 WHERE	VISITED_FLAG = 1;
7185 
7186 IF (l_items_visited = p_ship_set_item_count) THEN
7187 	-- We have to stop search and return from here. But before returning,
7188 	-- We need to check whether the search was fruitful or not
7189 	-- If search was fruitful set return_status to SUCCESS else FAILURE
7190 
7191 	SELECT  Organization_Id,
7192 		Instance_Id,
7193 		Supplier_Id,
7194 		Supplier_Site_Id,
7195 		sum(Rank),  -- order by cum rank instead of group by rank
7196 		Source_Type,
7197 		0,
7198 		-1,
7199 		'@@@',
7200 		NULL -- For supplier intransit LT project
7201 	BULK COLLECT INTO
7202 		x_atp_sources.Organization_Id,
7203 		x_atp_sources.Instance_Id,
7204 		x_atp_sources.Supplier_Id,
7205 		x_atp_sources.Supplier_Site_Id,
7206 		x_atp_sources.Rank,
7207 		x_atp_sources.Source_Type,
7208 		x_atp_sources.Preferred,
7209 		x_atp_sources.Lead_Time,
7210 		x_atp_sources.Ship_Method,
7211                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7212 	FROM    MSC_ATP_SOURCES_TEMP
7213 	GROUP BY
7214 		Organization_Id,
7215 		Instance_Id,
7216 		Supplier_Id,
7217 		Supplier_Site_Id,
7218 		-- Rank,  order by cum rank instead of group by rank
7219 		Source_Type
7220 	HAVING  count(*) = p_ship_set_item_count
7221 	ORDER BY 5; -- Rank;order by cum rank instead of group by rank
7222 
7223 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7224 		IF PG_DEBUG in ('Y', 'C') THEN
7225 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 5. Search over.');
7226 		END IF;
7227 		return;
7228 	ELSE
7229 		IF PG_DEBUG in ('Y', 'C') THEN
7230 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for the ship set.');
7231 		END IF;
7232 		x_return_status := FND_API.G_RET_STS_ERROR;
7233 		return;
7234 	END IF;
7235 
7236 END IF; -- All items in ship set
7237 
7238 END IF; -- some sources found
7239 
7240 ELSIF (l_dist_level_type(l_counter) = 6) THEN
7241 
7242 -- LEVEL 6, ASSIGNMENT TYPE 2: CATEGORY
7243 IF PG_DEBUG in ('Y', 'C') THEN
7244         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 6 (cat)');
7245 END IF;
7246 -- Bug 2931266. No need for customer_site_id join at this level
7247 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
7248         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
7249 SELECT
7250         MSST.INVENTORY_ITEM_ID,
7251         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
7252         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
7253         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
7254         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
7255         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
7256         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
7257                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7258                         to_number(null), 3, 1)),
7259         0,
7260         -1,
7261         '@@@'
7262 FROM
7263         MSC_ITEM_CATEGORIES             CAT,
7264 --        MSC_TRADING_PARTNER_SITES       TP,
7265         MSC_SR_SOURCE_ORG               SOURCE_ORG,
7266         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
7267         MSC_SOURCING_RULES              MSR,
7268         MSC_SR_ASSIGNMENTS              MSRA,
7269 --        MSC_TP_SITE_ID_LID              MTSIL,
7270         MSC_SHIP_SET_TEMP               MSST
7271 WHERE
7272 
7273         MSRA.ASSIGNMENT_TYPE = 2 /* CATEGORY */
7274         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
7275         AND     MSRA.CATEGORY_NAME = CAT.CATEGORY_NAME
7276         AND     MSRA.CATEGORY_SET_ID = CAT.CATEGORY_SET_ID
7277         AND     CAT.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
7278 	AND	MSST.VISITED_FLAG = 0
7279 --        AND     CAT.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
7280 --        AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
7281 --        AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
7282 --        AND     TP.PARTNER_TYPE = 2
7283         AND     CAT.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
7284         AND     CAT.SR_INSTANCE_ID = SOURCE_ORG.SR_INSTANCE_ID
7285         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
7286         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
7287         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7288         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
7289         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
7290         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
7291         AND     MSR.STATUS = 1
7292         AND     MSR.SOURCING_RULE_TYPE = 1
7293 	AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
7294 GROUP BY
7295         MSST.INVENTORY_ITEM_ID,
7296         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7297         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
7298         SOURCE_ORG.SOURCE_PARTNER_ID,
7299         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
7300 
7301 l_inserted_rows	:= SQL%ROWCOUNT;
7302 
7303 IF (l_inserted_rows > 0) THEN
7304 
7305     -- Found some sources for some items.
7306     IF PG_DEBUG in ('Y', 'C') THEN
7307 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 6');
7308     END IF;
7309 
7310     -- Mark all the items for which sources are/have been found.
7311     UPDATE	msc_ship_set_temp
7312     set		visited_flag = 1
7313     where	inventory_item_id in (select inventory_item_id from msc_atp_sources_temp);
7314 
7315     l_updated_rows := SQL%ROWCOUNT;
7316 
7317     -- Check if all the items in ship set are processed.
7318     IF (l_updated_rows = p_ship_set_item_count) THEN
7319 
7320 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
7321 	SELECT  Organization_Id,
7322 		Instance_Id,
7323 		Supplier_Id,
7324 		Supplier_Site_Id,
7325 		sum(Rank),  -- order by cum rank instead of group by rank
7326 		Source_Type,
7327 		0,
7328 		-1,
7329 		'@@@',
7330 		NULL -- For supplier intransit LT project
7331 	BULK COLLECT INTO
7332 		x_atp_sources.Organization_Id,
7333 		x_atp_sources.Instance_Id,
7334 		x_atp_sources.Supplier_Id,
7335 		x_atp_sources.Supplier_Site_Id,
7336 		x_atp_sources.Rank,
7337 		x_atp_sources.Source_Type,
7338 		x_atp_sources.Preferred,
7339 		x_atp_sources.Lead_Time,
7340 		x_atp_sources.Ship_Method,
7341                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7342 	FROM    MSC_ATP_SOURCES_TEMP
7343 	GROUP BY
7344 		Organization_Id,
7345 		Instance_Id,
7346 		Supplier_Id,
7347 		Supplier_Site_Id,
7348 		-- Rank,  order by cum rank instead of group by rank
7349 		Source_Type
7350 	HAVING  count(*) = p_ship_set_item_count
7351 	ORDER BY 5; -- Rank;  order by cum rank instead of group by rank
7352 
7353 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7354 		-- Common sources found
7355 		IF PG_DEBUG in ('Y', 'C') THEN
7356    		   msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 6');
7357 		END IF;
7358 		return;
7359 	ELSE
7360 		-- No common sources found
7361 		IF PG_DEBUG in ('Y', 'C') THEN
7362 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
7363 		END IF;
7364 		x_return_status := FND_API.G_RET_STS_ERROR;
7365 		return;
7366 	END IF; -- common sources
7367 
7368      END IF; -- all items of ship set
7369 END IF; -- some sources found
7370 
7371 -- Check the stopping condition.
7372 IF l_counter = l_dist_level_type.LAST THEN
7373     IF PG_DEBUG in ('Y', 'C') THEN
7374 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
7375     END IF;
7376     x_return_status := FND_API.G_RET_STS_ERROR;
7377      return;
7378 ELSE
7379     IF PG_DEBUG in ('Y', 'C') THEN
7380 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
7381     END IF;
7382 END IF;
7383 
7384 ELSIF (l_dist_level_type(l_counter) = 7) THEN
7385 
7386 -- LEVEL 7, ASSIGNMENT_TYPE 4: ORG
7387 IF PG_DEBUG in ('Y', 'C') THEN
7388         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 7 (org)');
7389 END IF;
7390 
7391 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
7392         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
7393 SELECT
7394         MSST.INVENTORY_ITEM_ID,
7395         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
7396         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
7397         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
7398         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
7399         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
7400         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
7401                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7402                         to_number(null), 3, 1)),
7403         0,
7404         -1,
7405         '@@@'
7406 FROM
7407         MSC_SYSTEM_ITEMS        ITEM,
7408         MSC_SR_SOURCE_ORG       SOURCE_ORG,
7409         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
7410         MSC_SOURCING_RULES      MSR,
7411         MSC_SR_ASSIGNMENTS      MSRA,
7412         MSC_TP_SITE_ID_LID      MTSIL,
7413         MSC_SHIP_SET_TEMP       MSST
7414 WHERE
7415         MSRA.ASSIGNMENT_TYPE = 4 /* ORG */
7416         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
7417         AND     MSRA.PARTNER_ID IS NOT NULL
7418         AND     MSRA.SHIP_TO_SITE_ID IS NOT NULL
7419         AND     MSRA.SHIP_TO_SITE_ID = MTSIL.TP_SITE_ID
7420         AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
7421         AND     MTSIL.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
7422         AND     ITEM.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
7423 	AND	MSST.VISITED_FLAG = 0
7424         AND     ITEM.PLAN_ID = -1
7425         AND     ITEM.ORGANIZATION_ID = SOURCE_ORG.SOURCE_ORGANIZATION_ID
7426         AND     ITEM.SR_INSTANCE_ID = SOURCE_ORG.SOURCE_ORG_INSTANCE_ID
7427         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
7428         AND     SOURCE_ORG.SR_RECEIPT_ID = RECEIPT_ORG.SR_RECEIPT_ID
7429         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7430         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate )) >= l_sysdate
7431         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
7432         AND     RECEIPT_ORG.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
7433         AND     MSR.STATUS = 1
7434         AND     MSR.SOURCING_RULE_TYPE = 1
7435         AND     MSR.SOURCING_RULE_ID = MSRA.SOURCING_RULE_ID
7436 GROUP BY
7437         MSST.INVENTORY_ITEM_ID,
7438         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7439         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
7440         SOURCE_ORG.SOURCE_PARTNER_ID,
7441         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
7442 
7443 l_inserted_rows	:= SQL%ROWCOUNT;
7444 
7445 IF (l_inserted_rows > 0) THEN
7446 
7447     -- Found some sources for some items.
7448     IF PG_DEBUG in ('Y', 'C') THEN
7449 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 7');
7450     END IF;
7451 
7452     -- Mark all the items for which sources are/have been found.
7453     UPDATE	msc_ship_set_temp
7454     set		visited_flag = 1
7455     where	inventory_item_id in (select inventory_item_id from msc_atp_sources_temp);
7456 
7457     l_updated_rows := SQL%ROWCOUNT;
7458 
7459     -- Check if all the items in ship set are processed.
7460     IF (l_updated_rows = p_ship_set_item_count) THEN
7461 
7462 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
7463 	SELECT  Organization_Id,
7464 		Instance_Id,
7465 		Supplier_Id,
7466 		Supplier_Site_Id,
7467 		sum(Rank),  -- order by cum rank instead of group by rank
7468 		Source_Type,
7469 		0,
7470 		-1,
7471 		'@@@',
7472 		NULL -- For supplier intransit LT project
7473 	BULK COLLECT INTO
7474 		x_atp_sources.Organization_Id,
7475 		x_atp_sources.Instance_Id,
7476 		x_atp_sources.Supplier_Id,
7477 		x_atp_sources.Supplier_Site_Id,
7478 		x_atp_sources.Rank,
7479 		x_atp_sources.Source_Type,
7480 		x_atp_sources.Preferred,
7481 		x_atp_sources.Lead_Time,
7482 		x_atp_sources.Ship_Method,
7483                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7484 	FROM    MSC_ATP_SOURCES_TEMP
7485 	GROUP BY
7486 		Organization_Id,
7487 		Instance_Id,
7488 		Supplier_Id,
7489 		Supplier_Site_Id,
7490 		-- Rank,  order by cum rank instead of group by rank
7491 		Source_Type
7492 	HAVING  count(*) = p_ship_set_item_count
7493 	ORDER BY 5; -- Rank; order by cum rank instead of group by rank
7494 
7495 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7496 		-- Common sources found
7497 		IF PG_DEBUG in ('Y', 'C') THEN
7498 		    msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 7');
7499 		END IF;
7500 		return;
7501 	ELSE
7502 		-- No common sources found
7503 		IF PG_DEBUG in ('Y', 'C') THEN
7504 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
7505 		END IF;
7506 		x_return_status := FND_API.G_RET_STS_ERROR;
7507 		return;
7508 	END IF; -- common sources
7509 
7510      END IF; -- all items of ship set
7511 END IF; -- some sources found
7512 
7513 -- Check the stopping condition.
7514 IF l_counter = l_dist_level_type.LAST THEN
7515     IF PG_DEBUG in ('Y', 'C') THEN
7516 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
7517     END IF;
7518     x_return_status := FND_API.G_RET_STS_ERROR;
7519      return;
7520 ELSE
7521     IF PG_DEBUG in ('Y', 'C') THEN
7522 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Some more search left. Continuing');
7523     END IF;
7524 END IF;
7525 
7526 ELSIF (l_dist_level_type(l_counter) = 8) THEN
7527 
7528 -- LEVEL 8, ASSIGNMENT_TYPE 7: REGION
7529 IF PG_DEBUG in ('Y', 'C') THEN
7530         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 8 (reg)');
7531 END IF;
7532 
7533 SELECT
7534 	MSST_INNER.INVENTORY_ITEM_ID,
7535 	MIN(8000 + ((10 - NVL(MRT_INNER.REGION_TYPE, 0)) * 100) + DECODE(MRT_INNER.ZONE_FLAG, 'Y', 1, 0))
7536 BULK COLLECT INTO
7537 	l_inventory_item_id_arr,
7538 	l_min_region_value_arr
7539 FROM
7540 	MSC_SYSTEM_ITEMS        ITEM_INNER,
7541 	MSC_SR_SOURCE_ORG       SOURCE_ORG_INNER,
7542 	MSC_SR_RECEIPT_ORG      RECEIPT_ORG_INNER,
7543 	MSC_SOURCING_RULES      MSR_INNER,
7544 	MSC_SR_ASSIGNMENTS      MSRA_INNER,
7545 	MSC_REGIONS_TEMP        MRT_INNER,
7546 	MSC_SHIP_SET_TEMP       MSST_INNER
7547 WHERE
7548 	MSRA_INNER.ASSIGNMENT_TYPE = 7 /* REGION */
7549 	AND     MSRA_INNER.ASSIGNMENT_SET_ID = p_assignment_set_id
7550 	AND     MSRA_INNER.REGION_ID = MRT_INNER.REGION_ID
7551 	AND     MRT_INNER.SESSION_ID = p_session_id
7552 	--AND     MRT_INNER.PARTNER_SITE_ID = p_customer_site_id
7553 	AND     MRT_INNER.PARTNER_SITE_ID IS NOT NULL
7554 	AND     MRT_INNER.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
7555 	AND     MRT_INNER.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
7556 	--AND     MRT_INNER.PARTNER_TYPE = 2 -- For supplier intransit LT project
7557 	AND     MSRA_INNER.SOURCING_RULE_ID = MSR_INNER.SOURCING_RULE_ID
7558 	AND     MSR_INNER.STATUS = 1
7559 	AND     MSR_INNER.SOURCING_RULE_TYPE = 1
7560 	AND     MSR_INNER.SOURCING_RULE_ID = RECEIPT_ORG_INNER.SOURCING_RULE_ID
7561 	-- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7562 	AND     TRUNC(NVL(RECEIPT_ORG_INNER.DISABLE_DATE,l_sysdate)) >= l_sysdate
7563 	AND     TRUNC(RECEIPT_ORG_INNER.EFFECTIVE_DATE) <= l_sysdate
7564 	AND     RECEIPT_ORG_INNER.SR_RECEIPT_ID = SOURCE_ORG_INNER.SR_RECEIPT_ID
7565 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID IS NOT NULL
7566 	AND     SOURCE_ORG_INNER.SOURCE_ORGANIZATION_ID = ITEM_INNER.ORGANIZATION_ID
7567 	AND     SOURCE_ORG_INNER.SR_INSTANCE_ID = ITEM_INNER.SR_INSTANCE_ID
7568 	AND     ITEM_INNER.PLAN_ID = -1
7569 	AND     ITEM_INNER.INVENTORY_ITEM_ID = MSST_INNER.INVENTORY_ITEM_ID
7570 	AND	MSST_INNER.VISITED_FLAG = 0
7571 GROUP BY MSST_INNER.INVENTORY_ITEM_ID;
7572 
7573 IF (l_inventory_item_id_arr.COUNT > 0) THEN
7574 
7575 -- Step1: For all the items found, update column min_region_value in msc_ship_set_temp.
7576 -- Also update the column visited_flag
7577 IF PG_DEBUG in ('Y', 'C') THEN
7578     msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'For following items, the min region value found');
7579     FOR i IN 1..l_inventory_item_id_arr.COUNT LOOP
7580       msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Item: '|| l_inventory_item_id_arr(i)
7581 			|| ' min region value: ' || l_min_region_value_arr(i));
7582     END LOOP;
7583 END IF;
7584 
7585 FORALL	i IN l_inventory_item_id_arr.FIRST..l_inventory_item_id_arr.LAST
7586 UPDATE	msc_ship_set_temp
7587 SET		min_region_value = l_min_region_value_arr(i),
7588 		visited_flag = 1
7589 WHERE	inventory_item_id = l_inventory_item_id_arr(i);
7590 
7591 -- Step2: For the updated items find all the sources and store them in msc_atp_sources_temp
7592 -- This is done by adding clause AND (expr) = msst.min_region_value
7593 -- and removing the clause AND visited_flag = 0
7594 
7595 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
7596         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
7597 SELECT
7598         MSST.INVENTORY_ITEM_ID,
7599         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
7600         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
7601         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
7602         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
7603         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
7604         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
7605                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7606                         to_number(null), 3, 1)),
7607         0,
7608         -1,
7609         '@@@'
7610 FROM
7611         MSC_SYSTEM_ITEMS        ITEM,
7612         MSC_SR_SOURCE_ORG       SOURCE_ORG,
7613         MSC_SR_RECEIPT_ORG      RECEIPT_ORG,
7614         MSC_SOURCING_RULES      MSR,
7615         MSC_SR_ASSIGNMENTS      MSRA,
7616         MSC_REGIONS_TEMP        MRT,
7617         MSC_SHIP_SET_TEMP       MSST
7618 WHERE
7619 
7620         MSRA.ASSIGNMENT_TYPE = 7 /* REGION */
7621         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
7622 	AND     MSRA.REGION_ID = MRT.REGION_ID
7623         AND     MRT.PARTNER_SITE_ID IS NOT NULL
7624         AND     MRT.SESSION_ID = p_session_id
7625         --AND     MRT.PARTNER_SITE_ID = p_customer_site_id
7626 	--AND     MRT.PARTNER_TYPE = 2 -- For supplier intransit LT project
7627 	AND     MRT.PARTNER_SITE_ID = decode(NVL(p_partner_type,2), 2, p_customer_site_id , 4 ,  p_party_site_id, 5, p_order_line_id)  --2814895
7628 	AND     MRT.PARTNER_TYPE  = NVL(p_partner_type,2) --2814895
7629         AND     (8000 + ((10 - NVL(MRT.REGION_TYPE, 0)) * 100) +
7630 		DECODE(MRT.ZONE_FLAG, 'Y', 1, 0)) = MSST.MIN_REGION_VALUE
7631 	AND     MSRA.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
7632         AND     MSR.STATUS = 1
7633         AND     MSR.SOURCING_RULE_TYPE = 1
7634         AND     MSR.SOURCING_RULE_ID = RECEIPT_ORG.SOURCING_RULE_ID
7635         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7636         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
7637         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
7638         AND     RECEIPT_ORG.SR_RECEIPT_ID = SOURCE_ORG.SR_RECEIPT_ID
7639         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID = ITEM.ORGANIZATION_ID
7640         AND     SOURCE_ORG.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
7641         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
7642 	AND     ITEM.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
7643 	AND     ITEM.PLAN_ID = -1
7644 GROUP BY
7645         MSST.INVENTORY_ITEM_ID,
7646         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7647         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
7648         SOURCE_ORG.SOURCE_PARTNER_ID,
7649         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
7650 
7651 -- Step3: Check if I need to stop here.
7652 
7653 SELECT	COUNT(*)
7654 INTO	l_items_visited
7655 FROM	MSC_SHIP_SET_TEMP
7656 WHERE	VISITED_FLAG = 1;
7657 
7658 IF (l_items_visited = p_ship_set_item_count) THEN
7659 	-- We have to stop search and return from here. But before returning,
7660 	-- We need to check whether the search was fruitful or not
7661 	-- If search was fruitful set return_status to SUCCESS else FAILURE
7662 
7663 	SELECT  Organization_Id,
7664 		Instance_Id,
7665 		Supplier_Id,
7666 		Supplier_Site_Id,
7667 		sum(Rank),  -- order by cum rank instead of group by rank
7668 		Source_Type,
7669 		0,
7670 		-1,
7671 		'@@@',
7672 		NULL -- For supplier intransit LT project
7673 	BULK COLLECT INTO
7674 		x_atp_sources.Organization_Id,
7675 		x_atp_sources.Instance_Id,
7676 		x_atp_sources.Supplier_Id,
7677 		x_atp_sources.Supplier_Site_Id,
7678 		x_atp_sources.Rank,
7679 		x_atp_sources.Source_Type,
7680 		x_atp_sources.Preferred,
7681 		x_atp_sources.Lead_Time,
7682 		x_atp_sources.Ship_Method,
7683                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7684 	FROM    MSC_ATP_SOURCES_TEMP
7685 	GROUP BY
7686 		Organization_Id,
7687 		Instance_Id,
7688 		Supplier_Id,
7689 		Supplier_Site_Id,
7690 		-- Rank, order by cum rank instead of group by rank
7691 		Source_Type
7692 	HAVING  count(*) = p_ship_set_item_count
7693 	ORDER BY 5; -- Rank;  order by cum rank instead of group by rank
7694 
7695 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7696 		IF PG_DEBUG in ('Y', 'C') THEN
7697 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Sources found at level 8. Search over.');
7698 		END IF;
7699 		return;
7700 	ELSE
7701 		IF PG_DEBUG in ('Y', 'C') THEN
7702 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for the ship set.');
7703 		END IF;
7704 		x_return_status := FND_API.G_RET_STS_ERROR;
7705 		return;
7706 	END IF;
7707 
7708 END IF; -- All items in ship set
7709 
7710 END IF; -- some sources found
7711 
7712 ELSIF (l_dist_level_type(l_counter) = 9) THEN
7713 
7714 -- LEVEL 9, ASSIGNMENT_TYPE 1: GLOBAL
7715 IF PG_DEBUG in ('Y', 'C') THEN
7716         msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Searching sources at level 9 (global)');
7717 END IF;
7718 -- Bug 2931266. No need for customer_site_id join at this level
7719 INSERT INTO MSC_ATP_SOURCES_TEMP (inventory_item_id, Organization_Id, Instance_Id,
7720         Supplier_Id, Supplier_Site_Id, Rank, Source_Type, Preferred, Lead_Time, Ship_Method)
7721 SELECT
7722         MSST.INVENTORY_ITEM_ID,
7723         NVL(SOURCE_ORG.SOURCE_ORGANIZATION_ID, -1),
7724         NVL(SOURCE_ORG.SOURCE_ORG_INSTANCE_ID, -1),
7725         NVL(SOURCE_ORG.SOURCE_PARTNER_ID, -1),
7726         NVL(SOURCE_ORG.SOURCE_PARTNER_SITE_ID, -1),
7727         SUM(NVL(SOURCE_ORG.RANK, 0) + 1 - SOURCE_ORG.ALLOCATION_PERCENT/1000), --2910418
7728         NVL(MIN(SOURCE_ORG.SOURCE_TYPE),
7729                 DECODE(SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7730                         to_number(null), 3, 1)),
7731         0,
7732         -1,
7733         '@@@'
7734 FROM
7735         MSC_SYSTEM_ITEMS                ITEM,
7736 --        MSC_TRADING_PARTNER_SITES       TP,
7737         MSC_SR_SOURCE_ORG               SOURCE_ORG,
7738         MSC_SR_RECEIPT_ORG              RECEIPT_ORG,
7739         MSC_SOURCING_RULES              MSR,
7740         MSC_SR_ASSIGNMENTS              MSRA,
7741 --        MSC_TP_SITE_ID_LID              MTSIL,
7742         MSC_SHIP_SET_TEMP               MSST
7743 WHERE
7744         MSRA.ASSIGNMENT_TYPE = 1 /* GLOBAL */
7745         AND     MSRA.ASSIGNMENT_SET_ID = p_assignment_set_id
7746         AND     MSRA.SOURCING_RULE_ID = MSR.SOURCING_RULE_ID
7747         AND     MSR.STATUS = 1
7748         AND     MSR.SOURCING_RULE_TYPE = 1
7749         AND     MSR.SOURCING_RULE_ID = RECEIPT_ORG.SOURCING_RULE_ID
7750         -- Bug 3787821: Changes for making the condition vaild if Sysdate = Disable_date
7751         AND     TRUNC(NVL(RECEIPT_ORG.DISABLE_DATE,l_sysdate)) >= l_sysdate
7752         AND     TRUNC(RECEIPT_ORG.EFFECTIVE_DATE) <= l_sysdate
7753         AND     RECEIPT_ORG.SR_RECEIPT_ID = SOURCE_ORG.SR_RECEIPT_ID
7754         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID IS NOT NULL
7755         AND     SOURCE_ORG.SOURCE_ORGANIZATION_ID = ITEM.ORGANIZATION_ID
7756         AND     SOURCE_ORG.SR_INSTANCE_ID = ITEM.SR_INSTANCE_ID
7757 	AND     ITEM.PLAN_ID = -1
7758         AND     ITEM.INVENTORY_ITEM_ID = MSST.INVENTORY_ITEM_ID
7759 	AND	MSST.VISITED_FLAG = 0
7760 --        AND     ITEM.SR_INSTANCE_ID = MTSIL.SR_INSTANCE_ID
7761 --        AND     MTSIL.SR_TP_SITE_ID = p_customer_site_id
7762 --        AND     MTSIL.TP_SITE_ID = TP.PARTNER_SITE_ID
7763 --        AND     TP.PARTNER_TYPE = 2
7764 GROUP BY
7765         MSST.INVENTORY_ITEM_ID,
7766         SOURCE_ORG.SOURCE_ORGANIZATION_ID,
7767         SOURCE_ORG.SOURCE_ORG_INSTANCE_ID,
7768         SOURCE_ORG.SOURCE_PARTNER_ID,
7769         SOURCE_ORG.SOURCE_PARTNER_SITE_ID;
7770 
7771 
7772 l_inserted_rows	:= SQL%ROWCOUNT;
7773 
7774 IF (l_inserted_rows > 0) THEN
7775 
7776     -- Found some sources for some items.
7777     IF PG_DEBUG in ('Y', 'C') THEN
7778 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'Found some sources for some ship set items at level 9');
7779     END IF;
7780 
7781     -- Mark all the items for which sources are/have been found.
7782     UPDATE	msc_ship_set_temp
7783     set		visited_flag = 1
7784     where	inventory_item_id in (select inventory_item_id from msc_atp_sources_temp);
7785 
7786     l_updated_rows := SQL%ROWCOUNT;
7787 
7788     -- Check if all the items in ship set are processed.
7789     IF (l_updated_rows = p_ship_set_item_count) THEN
7790 
7791 	-- Find the common sources from the pool of sources in msc_atp_sources_temp
7792 	SELECT  Organization_Id,
7793 		Instance_Id,
7794 		Supplier_Id,
7795 		Supplier_Site_Id,
7796 		sum(Rank),	-- order by cum rank instead of group by rank
7797 		Source_Type,
7798 		0,
7799 		-1,
7800 		'@@@',
7801 		NULL -- For supplier intransit LT project
7802 	BULK COLLECT INTO
7803 		x_atp_sources.Organization_Id,
7804 		x_atp_sources.Instance_Id,
7805 		x_atp_sources.Supplier_Id,
7806 		x_atp_sources.Supplier_Site_Id,
7807 		x_atp_sources.Rank,
7808 		x_atp_sources.Source_Type,
7809 		x_atp_sources.Preferred,
7810 		x_atp_sources.Lead_Time,
7811 		x_atp_sources.Ship_Method,
7812                 x_atp_sources.Sup_Cap_Type -- For supplier intransit LT project
7813 	FROM    MSC_ATP_SOURCES_TEMP
7814 	GROUP BY
7815 		Organization_Id,
7816 		Instance_Id,
7817 		Supplier_Id,
7818 		Supplier_Site_Id,
7819 		-- Rank,	order by cum rank instead of group by rank
7820 		Source_Type
7821 	HAVING  count(*) = p_ship_set_item_count
7822 	ORDER BY 5; --Rank;	order by cum rank instead of group by rank
7823 
7824 	IF (x_atp_sources.Rank.COUNT > 0) THEN
7825 		-- Common sources found
7826 		IF PG_DEBUG in ('Y', 'C') THEN
7827 		   msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All common sources for ship set found at level 9');
7828 		END IF;
7829 		return;
7830 	ELSE
7831 		-- No common sources found
7832 		IF PG_DEBUG in ('Y', 'C') THEN
7833 			msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'No common sources for all the ship set items.');
7834 		END IF;
7835 		x_return_status := FND_API.G_RET_STS_ERROR;
7836 		return;
7837 	END IF; -- common sources
7838 
7839      END IF; -- all items of ship set
7840 END IF; -- some sources found
7841 
7842 -- Check the stopping condition.
7843 IF l_counter = l_dist_level_type.LAST THEN
7844     IF PG_DEBUG in ('Y', 'C') THEN
7845 	msc_sch_wb.atp_debug('Get_Sources_Info: '|| 'All items in ship set could not be found in the entire search.');
7846     END IF;
7847     x_return_status := FND_API.G_RET_STS_ERROR;
7848      return;
7849 END IF;
7850 
7851 END IF; -- l_dist_level_type
7852 
7853 END LOOP;
7854 
7855 END IF; -- case2
7856 
7857 IF PG_DEBUG in ('Y', 'C') THEN
7858         msc_sch_wb.atp_debug('End of Get_Sources Info procedure');
7859 END IF;
7860 EXCEPTION
7861         WHEN OTHERS THEN
7862                 x_return_status := FND_API.G_RET_STS_ERROR;
7863                 return;
7864 
7865 END Get_Sources_Info;
7866 
7867 --------------------------------------------------
7868 --        Set processing procedures             --
7869 --------------------------------------------------
7870 
7871 PROCEDURE Initialize_Set_Processing (
7872    p_set        IN      MRP_ATP_PUB.ATP_Rec_Typ,
7873    p_start      IN      NUMBER
7874 ) IS
7875 BEGIN
7876    IF PG_DEBUG in ('Y', 'C') THEN
7877       msc_sch_wb.atp_debug('Begin Initialize_Set_Processing');
7878    END IF;
7879    -- 2834932
7880    G_set_nonoverride_flag := 'N';
7881    G_set_override_flag := 'N';
7882    G_set_status := MSC_ATP_PVT.ALLSUCCESS;
7883    G_is_ship_set := p_set.ship_set_name(p_start) is not null;
7884    G_override_date := null;
7885    G_ship_LAD_set  := null;
7886    G_arr_LAD_set   := null;
7887    G_ship_EAD_set  := null;
7888    G_latest_ship_date_set := null;
7889    G_latest_arr_date_set  := null;
7890 END Initialize_Set_Processing;
7891 
7892 PROCEDURE Process_Set_Line(
7893    p_set         IN OUT NOCOPY   MRP_ATP_PUB.ATP_Rec_Typ,
7894    i             IN              NUMBER,
7895    x_line_status OUT NOCOPY      NUMBER
7896 ) IS
7897    l_request_date_ln             DATE;
7898    l_ship_LAD_ln                 DATE;
7899    l_arr_LAD_ln                  DATE;
7900 BEGIN
7901 
7902    IF PG_DEBUG in ('Y', 'C') THEN
7903       msc_sch_wb.atp_debug('********** Begin Process_Set_Line **********');
7904    END IF;
7905 
7906    x_line_status := NVL(p_set.error_code(i), MSC_ATP_PVT.ALLSUCCESS);
7907 
7908    IF p_set.requested_ship_date(i) is NOT null THEN
7909       l_request_date_ln := p_set.requested_ship_date(i);
7910    ELSE
7911       l_request_date_ln := p_set.requested_arrival_date(i);
7912    END IF;
7913 
7914    IF PG_DEBUG in ('Y', 'C') THEN
7915       msc_sch_wb.atp_debug('l_request_date_ln: ' || l_request_date_ln);
7916       msc_sch_wb.atp_debug('Input error code: ' || x_line_status); --bug3439591
7917    END IF;
7918 
7919    IF p_set.override_flag(i) = 'Y' THEN
7920 
7921        IF p_set.requested_ship_date(i) is NOT null THEN
7922           l_ship_LAD_ln := p_set.ship_date(i);
7923        ELSE
7924           l_arr_LAD_ln := p_set.arrival_date(i);
7925        END IF;
7926 
7927        -- Honor the first overridden date
7928        IF G_set_override_flag = 'N' THEN
7929           IF G_is_ship_set THEN
7930               G_override_date := p_set.ship_date(i);
7931           ELSE
7932               G_override_date := p_set.arrival_date(i);
7933           END IF;
7934        END IF;
7935 
7936        IF PG_DEBUG in ('Y', 'C') THEN
7937           msc_sch_wb.atp_debug('Override case. override_date: ' ||
7938                                 to_char(G_override_date, 'DD-MON-YYYY HH24:MI:SS'));
7939        END IF;
7940        G_set_override_flag := 'Y';
7941    ELSE
7942        IF p_set.requested_ship_date(i) is NOT null THEN
7943 
7944           l_ship_LAD_ln := p_set.latest_acceptable_date(i);--bug3439591
7945        ELSE
7946           l_arr_LAD_ln := p_set.latest_acceptable_date(i);--bug3439591
7947        END IF;
7948 
7949        -- xxx dsting hack
7950        G_ship_EAD_set := GREATEST(nvl(p_set.earliest_acceptable_date(i), G_ship_EAD_set),
7951                                   nvl(G_ship_EAD_set, p_set.earliest_acceptable_date(i)));
7952 
7953       /* Bug 3365376: LAD should be considered for Non-atpable items as well.
7954       -- nonatpable items..move LAD back to sysdate of curr LAD is before sysdate
7955        IF p_set.Error_Code(i) = MSC_ATP_PVT.ATP_NOT_APPL THEN
7956           l_ship_LAD_ln := null;
7957           l_arr_LAD_ln := null;
7958        END IF;
7959        */
7960 
7961        IF PG_DEBUG in ('Y', 'C') THEN
7962           msc_sch_wb.atp_debug('Non-Override case. ship_date: ' || to_char(p_set.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
7963           msc_sch_wb.atp_debug('Non-Override case. ship_LAD_ln: ' || to_char(l_ship_LAD_ln, 'DD-MON-YYYY HH24:MI:SS'));
7964           msc_sch_wb.atp_debug('Non-Override case.  arr_LAD_ln: ' || to_char(l_arr_LAD_ln, 'DD-MON-YYYY HH24:MI:SS'));
7965           msc_sch_wb.atp_debug('Non-Override case.  arr_date: ' || to_char(p_set.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));--bug3439591
7966           msc_sch_wb.atp_debug('Non-Override case.  LAD: ' || to_char(p_set.latest_acceptable_date(i), 'DD-MON-YYYY HH24:MI:SS'));--bug3439591
7967        END IF;
7968        -- 2834932
7969        G_set_nonoverride_flag := 'Y';
7970    END IF;
7971 
7972    -- set line status
7973    -- dsting 2797410
7974    IF nvl(p_set.override_flag(i), 'N') = 'Y'
7975       AND p_set.error_code(i) = MSC_ATP_PVT.ATP_REQ_DATE_FAIL
7976    THEN
7977       x_line_status := MSC_ATP_PVT.ALLSUCCESS;
7978 
7979    --bug 3365376: We should check pass/fail status for Non atpbale lines as well
7980    /* ELSIF p_set.error_code(i) = MSC_ATP_PVT.ATP_NOT_APPL
7981       OR p_set.error_code(i) = MSC_ATP_PVT.PDS_TO_ODS_SWITCH
7982    */
7983    ELSIF  p_set.error_code(i) = MSC_ATP_PVT.PDS_TO_ODS_SWITCH
7984    THEN
7985       x_line_status := MSC_ATP_PVT.ALLSUCCESS;
7986    ELSIF p_set.error_code(i) = MSC_ATP_PVT.ATP_REQ_DATE_FAIL
7987       --bug 3365376
7988       OR p_set.error_code(i) = MSC_ATP_PVT.ATP_NOT_APPL
7989       THEN
7990       IF TRUNC(p_set.ship_date(i)) > TRUNC(l_ship_LAD_ln)
7991          or TRUNC(p_set.arrival_date(i)) > TRUNC(l_arr_LAD_ln)
7992       THEN
7993          x_line_status := MSC_ATP_PVT.ATP_ACCEPT_FAIL;
7994       ELSE
7995          x_line_status := MSC_ATP_PVT.ALLSUCCESS;
7996       END IF;
7997    END IF;
7998    IF p_set.Action(i) <> MSC_ATP_PVT.ATPQUERY AND
7999       (p_set.Error_Code(i) = MSC_ATP_PVT.ATP_NOT_APPL OR
8000        p_set.error_code(i) = MSC_ATP_PVT.PDS_TO_ODS_SWITCH)
8001    THEN
8002       p_set.Error_Code(i) := MSC_ATP_PVT.ALLSUCCESS;
8003    END IF;
8004 
8005    IF p_set.requested_ship_date(i) is NOT null THEN
8006       G_ship_LAD_set := LEAST(NVL(G_ship_LAD_set, l_ship_LAD_ln),
8007                               NVL(l_ship_LAD_ln,  G_ship_LAD_set));
8008    ELSE
8009       G_arr_LAD_set := LEAST(NVL(G_arr_LAD_set, l_arr_LAD_ln),
8010                              NVL(l_arr_LAD_ln,  G_arr_LAD_set));
8011    END IF;
8012 
8013    G_latest_ship_date_set := GREATEST(NVL(G_latest_ship_date_set, p_set.ship_date(i)),
8014                                       NVL(p_set.ship_date(i), G_latest_ship_date_set));
8015    G_latest_arr_date_set  := GREATEST(NVL(G_latest_arr_date_set, p_set.arrival_date(i)),
8016                                       NVL(p_set.arrival_date(i), G_latest_arr_date_set));
8017 
8018    IF x_line_status not in (MSC_ATP_PVT.ALLSUCCESS,
8019                             MSC_ATP_PVT.ATP_ACCEPT_FAIL)
8020    THEN
8021       G_set_status := x_line_status;
8022    ELSIF G_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
8023       IF TRUNC(G_latest_ship_date_set) > TRUNC(G_ship_LAD_set)
8024          OR TRUNC(G_latest_arr_date_set) > TRUNC(G_arr_LAD_set)
8025          OR (G_is_ship_set AND TRUNC(G_override_date) < TRUNC(G_ship_EAD_set)) -- xxx dsting hack
8026       THEN
8027         G_set_status := MSC_ATP_PVT.GROUPEL_ERROR;
8028       END IF;
8029    END IF;
8030    IF MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET  = 'N' and p_set.atp_flag(i) <> 'N' or p_set.atp_components_flag(i) <> 'N' THEN
8031       MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET := 'Y';
8032       msc_sch_wb.atp_debug('CHANGED THE ITEM PRESENT FLAG');
8033    END IF;
8034    IF PG_DEBUG in ('Y', 'C') THEN
8035        msc_sch_wb.atp_debug('line_status: '  || x_line_status);
8036        msc_sch_wb.atp_debug('set_status: '  || G_set_status);
8037        msc_sch_wb.atp_debug('ship_LAD_set: ' || G_ship_LAD_set);
8038        msc_sch_wb.atp_debug('arr_LAD_set: '  || G_arr_LAD_set);
8039        msc_sch_wb.atp_debug('latest_ship_date_set: ' || to_char(G_latest_ship_date_set, 'DD-MON-YYYY HH24:MI:SS'));
8040        msc_sch_wb.atp_debug('latest_arr_date_set: ' || to_char(G_latest_arr_date_set, 'DD-MON-YYYY HH24:MI:SS'));
8041    END IF;
8042 
8043 END Process_Set_Line;
8044 
8045 PROCEDURE Process_Set_Dates_Errors(
8046    p_set         IN OUT NOCOPY      MRP_ATP_PUB.ATP_Rec_Typ,
8047    p_src_dest    IN                 VARCHAR2,
8048    x_set_status  OUT NOCOPY         NUMBER,
8049    p_start       IN                 NUMBER DEFAULT NULL,
8050    p_end         IN                 NUMBER DEFAULT NULL
8051 ) IS
8052    l_line_date      DATE;
8053    l_group_date     DATE;
8054    l_start          NUMBER;
8055    l_end            NUMBER;
8056 
8057    -- 2834932
8058    l_status_flag    NUMBER;
8059    l_group_work_date DATE;
8060 
8061 BEGIN
8062 
8063    IF PG_DEBUG in ('Y', 'C') THEN
8064       msc_sch_wb.atp_debug('Begin Process_Set_Dates_Errors');
8065       msc_sch_wb.atp_debug('   set_status: ' || G_set_status);
8066    END IF;
8067 
8068    l_start := nvl(p_start, 1);
8069    l_end   := nvl(p_end, p_set.action.count);
8070 
8071    IF G_is_ship_set THEN
8072       l_group_date := G_latest_ship_date_set;  --4460369
8073    ELSE
8074       l_group_date := G_latest_arr_date_set; --4460369
8075    END IF;
8076 
8077    IF G_set_status = MSC_ATP_PVT.ATP_REQ_QTY_FAIL THEN
8078       l_group_date := null;
8079    END IF;
8080 
8081    -- 2834932 dsting
8082    -- check if the date is on a weekend
8083    /* ship_rec_cal changes begin */
8084    IF TRUNC(G_override_date) = TRUNC(l_group_date) AND
8085       G_set_nonoverride_flag = 'Y'
8086    THEN
8087       IF p_src_dest = 'D' THEN
8088 
8089 	IF G_is_ship_set THEN
8090 	 l_group_work_date := MSC_CALENDAR.next_work_day(
8091               p_set.shipping_cal_code(l_start),
8092               p_set.instance_id(l_start),
8093               l_group_date );
8094 	ELSE
8095 	 l_group_work_date := MSC_CALENDAR.next_work_day(
8096               p_set.receiving_cal_code(l_start),
8097               p_set.instance_id(l_start),
8098               l_group_date );
8099 	END IF;
8100 
8101       ELSIF p_src_dest = 'S' THEN
8102 	IF G_is_ship_set THEN
8103 	 l_group_work_date := MSC_SATP_FUNC.src_next_work_day(
8104               p_set.shipping_cal_code(l_start),
8105               l_group_date);
8106 	ELSE
8107 	 l_group_work_date := MSC_SATP_FUNC.src_next_work_day(
8108               p_set.receiving_cal_code(l_start),
8109               l_group_date);
8110 	END IF;
8111       END IF;
8112       /* ship_rec_cal changes end */
8113 
8114       IF l_group_work_date is null THEN
8115             p_set.error_code(l_start) := MSC_ATP_PVT.NO_MATCHING_CAL_DATE;
8116             raise NO_DATA_FOUND;
8117       END IF;
8118 
8119       IF TRUNC(l_group_date) <> TRUNC(l_group_work_date) THEN
8120          l_status_flag := G_NONWORKING_DAY;
8121          G_set_status := MSC_ATP_PVT.ATP_OVERRIDE_DATE_FAIL;
8122       ELSE
8123          l_status_flag := null;
8124       END IF;
8125       l_group_date := l_group_work_date; --4460369
8126    END IF;
8127 
8128    for i in l_start..l_end loop
8129 
8130       -- Populate the group dates
8131        --added by avjain for populating latestacceptable date for shipset
8132       IF G_ship_LAD_set IS NOT NULL THEN
8133   	p_set.latest_acceptable_date(i) :=G_ship_LAD_set;
8134       ELSE
8135   	p_set.latest_acceptable_date(i) :=G_arr_LAD_set;
8136       END IF;
8137       if G_is_ship_set then
8138          p_set.group_ship_date(i) := l_group_date;
8139 
8140          if l_group_date is null then
8141             p_set.group_arrival_date(i) := null;
8142          elsif p_set.ship_date(i) = l_group_date then
8143             p_set.group_arrival_date(i) := p_set.arrival_date(i);
8144          elsif p_src_dest = 'D' then
8145 
8146          /* ship_rec_cal changes begin */
8147             p_set.group_arrival_date(i) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
8148                                                 l_group_date, null, 0, -- pass null to make sure no validation on ship cal as
8149                                                                        -- l_group_date has already been validated on ship cal
8150                                                 p_set.intransit_cal_code(i), nvl(p_set.delivery_lead_time(i), 0), 1,
8151                                                 p_set.receiving_cal_code(i), 1, p_set.instance_id(i))
8152                                                 + (l_group_date - trunc(l_group_date)); --4460369  --4967040
8153 
8154          else
8155             p_set.group_arrival_date(i) := TRUNC(MSC_SATP_FUNC.SRC_THREE_STEP_CAL_OFFSET_DATE(
8156                                                 l_group_date, null, 0, -- pass null to make sure no validation on ship cal as
8157                                                                        -- l_group_date has already been validated on ship cal
8158                                                 p_set.intransit_cal_code(i), nvl(p_set.delivery_lead_time(i), 0), 1,
8159                                                 p_set.receiving_cal_code(i), 1))
8160                                                 + (l_group_date - trunc(l_group_date)); --4460369  --4967040
8161 
8162          end if;
8163          /* ship_rec_cal changes end */
8164       else
8165          p_set.group_arrival_date(i) := l_group_date;
8166          if l_group_date is null then
8167             p_set.group_arrival_date(i) := null;
8168          elsif p_set.arrival_date(i) = l_group_date then
8169             p_set.group_ship_date(i) := p_set.ship_date(i);
8170          elsif p_src_dest = 'D' then
8171             /* ship_rec_cal changes begin */
8172             p_set.group_ship_date(i) := MSC_CALENDAR.THREE_STEP_CAL_OFFSET_DATE(
8173                 				l_group_date, null, 0, -- pass null to make sure no validation on receiving cal as
8174                                                        -- l_group_date has already been validated on receiving cal
8175                 				p_set.intransit_cal_code(i), -1 * nvl(p_set.delivery_lead_time(i), 0), -1,
8176                 				p_set.shipping_cal_code(i), -1, p_set.instance_id(i))
8177                                                 + (l_group_date - trunc(l_group_date)); --4460369  --4967040
8178 
8179          else
8180             p_set.group_ship_date(i) := TRUNC(MSC_SATP_FUNC.SRC_THREE_STEP_CAL_OFFSET_DATE(
8181                 				l_group_date, null, 0, -- pass null to make sure no validation on receiving cal as
8182                                                        -- l_group_date has already been validated on receiving cal
8183                 				p_set.intransit_cal_code(i), -1 * nvl(p_set.delivery_lead_time(i), 0), -1,
8184                 				p_set.shipping_cal_code(i), -1))
8185                                                 + (l_group_date - trunc(l_group_date)); --4460369  --4967040
8186 
8187          end if;
8188          /* ship_rec_cal changes end */
8189       end if;
8190 
8191       G_latest_ship_date_set := GREATEST(G_latest_ship_date_set, p_set.group_ship_date(i));
8192       G_latest_arr_date_set  := GREATEST(G_latest_arr_date_set, p_set.group_arrival_date(i));
8193 
8194       /*IF MSC_ATP_PVT.G_RETAIN_TIME_NON_ATP = 'N' or MSC_ATP_PVT.G_ATP_ITEM_PRESENT_IN_SET = 'Y'  THEN  --4460369
8195          IF p_set.requested_ship_date(i) is not null then
8196             p_set.group_ship_date(i) := trunc(p_set.group_ship_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
8197          ELSE
8198             p_set.group_arrival_date(i) := trunc(p_set.group_arrival_date(i)) +  MSC_ATP_PVT.G_END_OF_DAY;
8199          END IF;
8200       END IF;  --4460369*/ --4967040
8201 
8202       IF PG_DEBUG in ('Y', 'C') THEN
8203          msc_sch_wb.atp_debug('Ship Date := ' || to_char(p_set.ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
8204          msc_sch_wb.atp_debug('Arrival Date := ' || to_char(p_set.arrival_date(i), 'DD-MON-YYYY HH24:MI:SS'));
8205          msc_sch_wb.atp_debug('Group Ship Date := ' || to_char(p_set.group_ship_date(i), 'DD-MON-YYYY HH24:MI:SS'));
8206         msc_sch_wb.atp_debug('group_ship_date(' || i || '): ' || p_set.group_ship_date(i));
8207         msc_sch_wb.atp_debug('group_arrival_date(' || i || '): ' || p_set.group_arrival_date(i));
8208       END IF;
8209    end loop;
8210    -- 2834932 dsting. split the group date and error code loops
8211 
8212    -- one final check to see if and dates pushed beyond LAD
8213    IF G_set_status = MSC_ATP_PVT.ALLSUCCESS THEN
8214       IF TRUNC(G_latest_ship_date_set) > TRUNC(G_ship_LAD_set)
8215          OR TRUNC(G_latest_arr_date_set) > TRUNC(G_arr_LAD_set)
8216       THEN
8217         G_set_status := MSC_ATP_PVT.GROUPEL_ERROR;
8218       END IF;
8219    END IF;
8220 
8221    --
8222    -- Populate error code if needed. These errors are handled
8223    --   ATP_MULTI_OVERRIDE_DATES
8224    --   ATP_OVERRIDE_DATE_FAIL
8225    --   ATP_ACCEPT_FAIL
8226    --   GROUPEL_ERROR
8227    --
8228    if G_set_status <> MSC_ATP_PVT.ALLSUCCESS then
8229       for i in l_start..l_end loop
8230          if G_is_ship_set then
8231             l_line_date := p_set.ship_date(i);
8232             -- 2834932 dsting
8233             IF l_status_flag = G_NONWORKING_DAY AND
8234                nvl(p_set.override_flag(i), 'N') = 'N'
8235             THEN
8236                IF PG_DEBUG in ('Y', 'C') THEN
8237                   msc_sch_wb.atp_debug('Override date is nonworking day');
8238                END IF;
8239                IF p_set.error_code(i) = MSC_ATP_PVT.ALLSUCCESS THEN
8240                   p_set.error_code(i) := MSC_ATP_PVT.ATP_OVERRIDE_DATE_FAIL;
8241                END IF;
8242             END IF;
8243          else
8244             l_line_date := p_set.arrival_date(i);
8245          end if;
8246 
8247          IF (NVL(p_set.Override_Flag(i), 'N') = 'Y') THEN
8248             IF  TRUNC(G_override_date) <> TRUNC(l_line_date) THEN
8249                p_set.Error_Code(i) := MSC_ATP_PVT.ATP_MULTI_OVERRIDE_DATES;
8250                IF PG_DEBUG in ('Y', 'C') THEN
8251                   msc_sch_wb.atp_debug('stmt18 - Multiple lines overridden to diff dates');
8252                END IF;
8253             END IF;
8254          ELSE
8255             IF TRUNC(G_override_date) < TRUNC(l_line_date) THEN
8256                IF PG_DEBUG in ('Y', 'C') THEN
8257                   msc_sch_wb.atp_debug('stmt19 - Unable to Meet Overridden dates');
8258                END IF;
8259                p_set.Error_Code(i) := MSC_ATP_PVT.ATP_OVERRIDE_DATE_FAIL;
8260             ELSIF TRUNC(p_set.ship_date(i)) > TRUNC(G_ship_LAD_set)
8261                   OR TRUNC(p_set.arrival_date(i)) > TRUNC(G_arr_LAD_set)
8262             THEN
8263                p_set.Error_Code(i) := MSC_ATP_PVT.ATP_ACCEPT_FAIL;
8264                IF PG_DEBUG in ('Y', 'C') THEN
8265                   msc_sch_wb.atp_debug('stmt19 - Unable to Meet LAD');
8266                END IF;
8267             END IF;
8268          END IF;
8269 
8270          IF NVL(p_set.Error_Code(i), MSC_ATP_PVT.ALLSUCCESS)
8271              --bug 3365376: Popluate error also when we have atp not appl or PDS to ODS switch
8272              --= MSC_ATP_PVT.ALLSUCCESS
8273              IN (MSC_ATP_PVT.ALLSUCCESS, MSC_ATP_PVT.ATP_NOT_APPL, MSC_ATP_PVT.PDS_TO_ODS_SWITCH)
8274          THEN
8275             p_set.Error_Code(i) := MSC_ATP_PVT.GROUPEL_ERROR;
8276             IF PG_DEBUG in ('Y', 'C') THEN
8277                msc_sch_wb.atp_debug('GROUPEL_ERROR');
8278             END IF;
8279          END IF;
8280       end loop;
8281    end if;
8282 
8283    IF PG_DEBUG in ('Y', 'C') THEN
8284       msc_sch_wb.atp_debug('END Proces_Set_Dates_Errors');
8285    END IF;
8286 
8287    x_set_status := G_set_status;
8288 END Process_Set_Dates_Errors;
8289 
8290 -- dsting copied and pasted from schedule
8291 PROCEDURE Update_Set_SD_Dates(
8292    p_set        IN OUT NOCOPY     MRP_ATP_PUB.ATP_Rec_Typ,
8293    p_arrival_set IN    		  mrp_atp_pub.date_arr
8294 ) IS
8295    l_plan_info_rec      	MSC_ATP_PVT.plan_info_rec;
8296    l_plan_id            	NUMBER;
8297    l_demand_pegging_id  	NUMBER;
8298    l_sd_date            	DATE;
8299    --ship_rec_cal
8300    l_order_date_type    	NUMBER;
8301    l_ship_arrival_date_rec MSC_ATP_PVT.ship_arrival_date_rec_typ;
8302    l_return_status      	VARCHAR2(1);
8303 BEGIN
8304      IF PG_DEBUG in ('Y', 'C') THEN
8305         msc_sch_wb.atp_debug('Begin Update_Set_SD_Dates');
8306      END IF;
8307 
8308      FOR m in 1.. p_set.action.count LOOP
8309        IF PG_DEBUG in ('Y', 'C') THEN
8310            msc_sch_wb.atp_debug('m = ' || m);
8311        END IF;
8312 
8313        IF (p_set.Action(m)<> MSC_ATP_PVT.ATPQUERY and p_set.quantity_ordered(m) <> 0) THEN -- xxxx
8314          /* --bug4281663 commented and initialized below
8315          IF MSC_ATP_PVT.G_INV_CTP = 4 THEN
8316              -- New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
8317              -- (SCLT)
8318              MSC_ATP_PROC.get_global_plan_info(p_set.instance_id(m),
8319                          p_set.inventory_item_id(m),
8320                          -- 2832497 dsting
8321                          p_set.source_organization_id(m),
8322                          p_set.demand_class(m));
8323 
8324              l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
8325              -- End New procedure for obtaining plan data : Supplier Capacity Lead Time proj.
8326 
8327              l_plan_id          := l_plan_info_rec.plan_id;
8328              IF p_set.attribute_07.Exists(m) THEN
8329                 p_set.attribute_07(m) := l_plan_info_rec.plan_name;
8330              END IF;
8331              -- changes for bug 2392456 ends
8332 
8333          ELSE
8334              l_plan_id := -1;
8335          END IF;
8336          */
8337          --bug4281663 Using the plan_id passed instead of calling get_global_plan_info
8338          l_plan_id := p_set.plan_id(m);
8339          l_demand_pegging_id := MSC_ATP_PVT.G_DEMAND_PEGGING_ID;
8340          IF PG_DEBUG in ('Y', 'C') THEN
8341             msc_sch_wb.atp_debug('  l_demand_pegging_id := ' || l_demand_pegging_id);
8342             msc_sch_wb.atp_debug('  l_plan_id := ' || l_plan_id);
8343          END IF;
8344          -- xxxx
8345          MSC_ATP_PVT.G_DEMAND_PEGGING_ID := p_set.end_pegging_id(m);
8346          IF PG_DEBUG in ('Y', 'C') THEN
8347             msc_sch_wb.atp_debug('  G_DEMAND_PEGGING_ID := ' || MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
8348             msc_sch_wb.atp_debug('  G_DEMAND_CLASS_ATP_FLAG := ' || MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(m));
8349             msc_sch_wb.atp_debug('  G_REQ_ATP_DATE := ' || MSC_ATP_PVT.G_REQ_ATP_DATE(m));
8350             msc_sch_wb.atp_debug('  G_REQ_DATE_QTY := ' || MSC_ATP_PVT.G_REQ_DATE_QTY(m));
8351             --bug4281663 Added more debug statements
8352             msc_sch_wb.atp_debug('  p_set.ship_date(m) := ' || p_set.ship_date(m));
8353             msc_sch_wb.atp_debug('  p_set.group_ship_date(m) := ' || p_set.group_ship_date(m));
8354          END IF;
8355 
8356          -- update_sd_date for setproc
8357          IF p_set.ship_date(m) <> p_set.group_ship_date(m) THEN
8358             IF NVL(p_set.override_flag(m), 'N') = 'Y' THEN
8359                l_sd_date := p_set.ship_date(m);
8360             ELSIF nvl(p_set.atp_lead_time(m), 0) <> 0 THEN
8361                -- For ship_rec_cal, this is fine because date_offset from non-working day or working day
8362 	       -- gives the same result.
8363                l_sd_date := MSC_CALENDAR.DATE_OFFSET(
8364                                    p_set.source_organization_id(m),
8365                                    p_set.instance_id(m),
8366                                    1,
8367                                    p_set.group_ship_date(m),
8368                                    -NVL(p_set.atp_lead_time(m), 0));
8369             ELSE
8370                l_sd_date := p_set.group_ship_date(m);
8371             END IF;
8372 
8373             -- ship_rec_cal
8374             IF p_set.requested_arrival_date(m) is not null THEN
8375                 l_order_date_type := 2;
8376             ELSE
8377                 l_order_date_type := 1;
8378             END IF;
8379 
8380             IF PG_DEBUG in ('Y', 'C') THEN
8381                 msc_sch_wb.atp_debug(' __________________Flush PDS Data__________________');
8382                 msc_sch_wb.atp_debug('Update_Set_SD_Dates : ' || 'sch arrival date: ' || p_set.group_arrival_date(m));
8383                 msc_sch_wb.atp_debug('Update_Set_SD_Dates : ' || 'lat accep date  : ' || p_set.latest_acceptable_date(m));
8384                 msc_sch_wb.atp_debug('Update_Set_SD_Dates : ' || 'order_date_type : ' || l_order_date_type);
8385                 msc_sch_wb.atp_debug('  update_sd_date line: ' || m || ' date: ' || l_sd_date);
8386             END IF;
8387 
8388             MSC_ATP_DB_UTILS.Update_SD_Date(p_set.Identifier(m),
8389              p_set.instance_id(m), l_sd_date, l_plan_id,null, -- dsting setproc
8390              MSC_ATP_PVT.G_DEMAND_CLASS_ATP_FLAG(m),
8391              MSC_ATP_PVT.G_REQ_ATP_DATE(m),
8392              MSC_ATP_PVT.G_REQ_DATE_QTY(m), -- Bug 1501787
8393              l_sd_date, -- Bug 2795053-reopen
8394              null,              -- For time_phased_atp
8395              p_set.atf_date(m), -- For time_phased_atp
8396              null,              -- For time_phased_atp
8397              p_set.group_arrival_date(m),       -- For ship_rec_cal
8398              l_order_date_type,                 -- For ship_rec_cal
8399              p_set.latest_acceptable_date(m),   -- For ship_rec_cal
8400              p_set.ship_set_name(m),
8401              p_set.arrival_set_name(m),
8402              p_set.override_flag(m),
8403              p_arrival_set(m),null    --time_phased_atp --bug3397904
8404              );
8405 
8406              MSC_ATP_PVT.G_DEMAND_PEGGING_ID := l_demand_pegging_id;
8407          ELSE
8408 
8409               /* ship_rec_cal changes begin
8410                  flush sch arrival date, lat acceptable date, order date type in pds*/
8411 
8412                     l_ship_arrival_date_rec.scheduled_arrival_date := p_set.arrival_date(m);
8413                     l_ship_arrival_date_rec.latest_acceptable_date := p_set.latest_acceptable_date(m);
8414                     l_ship_arrival_date_rec.instance_id 	   := p_set.instance_id(m);
8415                     l_ship_arrival_date_rec.plan_id 		   := l_plan_id;
8416                     l_ship_arrival_date_rec.arrival_set_name       := p_set.arrival_set_name(m);
8417                     l_ship_arrival_date_rec.ship_set_name          := p_set.ship_set_name(m);
8418                     l_ship_arrival_date_rec.atp_override_flag      := p_set.override_flag(m);
8419     		    l_ship_arrival_date_rec.request_arrival_date   := p_arrival_set(m);
8420                     MSC_ATP_PVT.G_DEMAND_PEGGING_ID := p_set.end_pegging_id(m);
8421                     /* Read demand_id from madt*/
8422                     BEGIN
8423                         SELECT identifier3
8424                         INTO   l_ship_arrival_date_rec.demand_id
8425                         FROM   mrp_atp_details_temp
8426                         WHERE  pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
8427                         AND    session_id = MSC_ATP_PVT.G_SESSION_ID
8428                         AND    record_type = 3;
8429                     EXCEPTION
8430                         WHEN OTHERS THEN
8431                             l_ship_arrival_date_rec.demand_id := null;
8432                     END;
8433                     IF PG_DEBUG in ('Y', 'C') THEN
8434                             msc_sch_wb.atp_debug('Update_Set_SD_Dates: ' || 'l_ship_arrival_date_rec.demand_id = ' || l_ship_arrival_date_rec.demand_id);
8435                     END IF;
8436 
8437                     /* Determine order date type*/
8438                     IF p_set.requested_arrival_date(m) is not null THEN
8439                         l_ship_arrival_date_rec.order_date_type := 2;
8440                     ELSE
8441                         l_ship_arrival_date_rec.order_date_type := 1;
8442                     END IF;
8443 		    IF l_ship_arrival_date_rec.demand_id is not null THEN
8444                     	    --bug4281663 used plan id to determine ods or pds
8445                     	    /*
8446                     	    IF (MSC_ATP_PVT.G_INV_CTP = 4) THEN
8447 	                    	MSC_ATP_DB_UTILS.Flush_Data_In_Pds(l_ship_arrival_date_rec, l_return_status);
8448 	                    ELSE
8449 	                    	MSC_ATP_DB_UTILS.Flush_Data_In_Ods(l_ship_arrival_date_rec, l_return_status);
8450 	                    END IF;
8451 	                    */
8452 	                    IF NVL(p_set.plan_id(m),-1) = -1 THEN
8453 	                    	MSC_ATP_DB_UTILS.Flush_Data_In_Ods(l_ship_arrival_date_rec, l_return_status);
8454 	                    ELSE
8455 	                    	MSC_ATP_DB_UTILS.Flush_Data_In_Pds(l_ship_arrival_date_rec, l_return_status);
8456 	                    END IF;
8457 		    END IF;
8458 
8459                     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8460                             IF PG_DEBUG in ('Y', 'C') THEN
8461                                     msc_sch_wb.atp_debug('Update_Set_SD_Dates: ' || 'Error in call to Flush_Data_In_Pds procedure ');
8462                             END IF;
8463                             RAISE FND_API.G_EXC_ERROR;
8464                     END IF;
8465 
8466               /* ship_rec_cal changes end */
8467          END IF;
8468        END IF;
8469      END LOOP;
8470 END Update_Set_SD_Dates;
8471 
8472 --------------------------------------------------
8473 --      Delivery lead time Procedures           --
8474 --------------------------------------------------
8475 
8476 -- dsting
8477 --
8478 -- search for transit times in this order
8479 --
8480 -- 1) loc to loc
8481 -- 2) region to region
8482 -- 3) org to org
8483 --
8484 -- if a ship method is specified then 1,2,3 with ship method specified
8485 -- 	then 1,2,3 with default times
8486 
8487 PROCEDURE get_transit_time (
8488 	p_from_loc_id		IN NUMBER,
8489 	p_from_instance_id	IN NUMBER,
8490 	p_to_loc_id		IN NUMBER,
8491 	p_to_instance_id	IN NUMBER,
8492 	p_session_id		IN NUMBER,
8493 	p_partner_site_id	IN NUMBER,
8494 	x_ship_method		IN OUT NoCopy VARCHAR2,
8495 	x_intransit_time	OUT NoCopy NUMBER,
8496 	p_supplier_site_id      IN NUMBER DEFAULT NULL, -- For supplier intransit LT project
8497 	p_partner_type          IN      NUMBER,--2814895
8498 	p_party_site_id         IN      NUMBER, --2814895
8499 	p_order_line_id         IN      NUMBER)--2814895
8500 IS
8501 	l_level			NUMBER;
8502         l_cache_idx             NUMBER;
8503 
8504 CURSOR	c_lead_time
8505 IS
8506 SELECT  intransit_time,
8507 	((10 * (10 - mrt.region_type)) + DECODE(mrt.zone_flag, 'Y', 1, 0)) region_level
8508 FROM    msc_interorg_ship_methods mism,
8509 	msc_regions_temp mrt
8510 WHERE   mism.plan_id = -1
8511 AND     mism.from_location_id = p_from_loc_id
8512 AND     mism.sr_instance_id = p_from_instance_id
8513 AND     mism.sr_instance_id2 = p_to_instance_id
8514 AND     mism.ship_method = x_ship_method
8515 AND     mism.to_region_id = mrt.region_id
8516 AND     mrt.session_id = p_session_id
8517 AND     mrt.partner_site_id = decode(NVL(p_partner_type,2), 2, p_partner_site_id, 4,p_party_site_id, 5, p_order_line_id)  --2814895
8518 AND     mrt.partner_type    = NVL(p_partner_type,2)  --2814895 -- For supplier intransit LT project
8519 ORDER BY 2;
8520 
8521 CURSOR	c_default_lead_time
8522 IS
8523 SELECT  ship_method, intransit_time,
8524 	((10 * (10 - mrt.region_type)) + DECODE(mrt.zone_flag, 'Y', 1, 0)) region_level
8525 FROM    msc_interorg_ship_methods mism,
8526 	   msc_regions_temp mrt
8527 WHERE   mism.plan_id = -1
8528 AND     mism.from_location_id = p_from_loc_id
8529 AND     mism.sr_instance_id = p_from_instance_id
8530 AND     mism.sr_instance_id2 = p_to_instance_id
8531 AND	mism.default_flag = 1
8532 AND     mism.to_region_id = mrt.region_id
8533 AND     mrt.session_id = p_session_id
8534 AND     mrt.partner_site_id = decode( NVL(p_partner_type,2), 2, p_partner_site_id, 4, p_party_site_id, 5, p_order_line_id)  --2814895
8535 AND     mrt.partner_type    = NVL(p_partner_type,2)  --2814895 -- For supplier intransit LT project
8536 ORDER BY 3;
8537 
8538 -- Changes for supplier intransit LT project begin
8539 CURSOR  c_supplier_lead_time
8540 IS
8541 SELECT  intransit_time,
8542         mrt.region_type region_level -- collection has already translated data
8543 FROM    msc_interorg_ship_methods mism,
8544         msc_regions_temp          mrt
8545 WHERE   mism.plan_id            = -1
8546 AND     mism.to_location_id     = p_to_loc_id
8547 AND     mism.sr_instance_id2    = p_to_instance_id
8548 AND     mism.sr_instance_id     = p_from_instance_id
8549 AND     mism.ship_method        = x_ship_method
8550 AND     mism.from_region_id     = mrt.region_id
8551 AND     mrt.session_id          = p_session_id
8552 AND     mrt.partner_site_id     = p_supplier_site_id
8553 AND     mrt.partner_type        = 1 -- For supplier intransit LT project
8554 ORDER BY 2;
8555 
8556 CURSOR  c_supplier_default_lead_time
8557 IS
8558 SELECT  ship_method, intransit_time,
8559         mrt.region_type region_level -- collection has already translated data
8560 FROM    msc_interorg_ship_methods mism,
8561         msc_regions_temp          mrt
8562 WHERE   mism.plan_id            = -1
8563 AND     mism.to_location_id     = p_to_loc_id
8564 AND     mism.sr_instance_id2    = p_to_instance_id
8565 AND     mism.sr_instance_id     = p_from_instance_id
8566 AND     mism.default_flag       = 1
8567 AND     mism.from_region_id     = mrt.region_id
8568 AND     mrt.session_id          = p_session_id
8569 AND     mrt.partner_site_id     = p_supplier_site_id
8570 AND     mrt.partner_type        = 1 -- For supplier intransit LT project
8571 ORDER BY 3;
8572 -- Changes for supplier intransit LT project end
8573 
8574 BEGIN
8575 
8576         IF PG_DEBUG in ('Y', 'C') THEN
8577            msc_sch_wb.atp_debug('********** get_transit_time **********');
8578            msc_sch_wb.atp_debug('loc from: ' || p_from_loc_id ||
8579                                 ' to: '      || p_to_loc_id);
8580            msc_sch_wb.atp_debug('instance from: ' || p_from_instance_id ||
8581                                 ' to: '           || p_to_instance_id);
8582            msc_sch_wb.atp_debug('session_id: '    || p_session_id);
8583            msc_sch_wb.atp_debug('partner_site: '  || p_partner_site_id);
8584            msc_sch_wb.atp_debug('ship method: '   || x_ship_method);
8585         END IF;
8586 
8587 	-- if the receipt org or the ship method is NULL
8588 	-- then get the default time
8589 
8590 	IF p_from_loc_id IS NOT NULL THEN
8591     	 	BEGIN
8592                         IF p_to_loc_id IS NULL THEN
8593                            RAISE NO_DATA_FOUND;
8594                         END IF;
8595 
8596 			IF x_ship_method IS NOT NULL THEN
8597          			SELECT  intransit_time
8598     	    			INTO    x_intransit_time
8599      	    			FROM    msc_interorg_ship_methods
8600     	    			WHERE   plan_id = -1
8601     				AND     from_location_id = p_from_loc_id
8602          			AND     sr_instance_id = p_from_instance_id
8603 	         		AND     to_location_id = p_to_loc_id
8604     		    		AND     sr_instance_id2 = p_to_instance_id
8605     				AND     ship_method = x_ship_method
8606     				AND     rownum = 1;
8607 			ELSE
8608          			SELECT  ship_method, intransit_time
8609     	    			INTO    x_ship_method, x_intransit_time
8610      	    			FROM    msc_interorg_ship_methods
8611     	    			WHERE   plan_id = -1
8612     				AND     from_location_id = p_from_loc_id
8613          			AND     sr_instance_id = p_from_instance_id
8614 	         		AND     to_location_id = p_to_loc_id
8615     		    		AND     sr_instance_id2 = p_to_instance_id
8616 				AND     default_flag = 1
8617     				AND     rownum = 1;
8618 			END IF;
8619 		EXCEPTION WHEN NO_DATA_FOUND THEN
8620 			msc_sch_wb.atp_debug('Using region level transit times');
8621 			IF x_ship_method IS NOT NULL THEN
8622 	     			OPEN c_lead_time;
8623 	     			FETCH c_lead_time INTO x_intransit_time, l_level;
8624 		     		CLOSE c_lead_time;
8625 			ELSE
8626 	     			OPEN c_default_lead_time;
8627 	     			FETCH c_default_lead_time INTO x_ship_method,
8628 							x_intransit_time,
8629 					   		l_level;
8630 			     	CLOSE c_default_lead_time;
8631 			END IF;
8632 		END;
8633 
8634         ELSIF p_supplier_site_id IS NOT NULL THEN -- For supplier intransit LT project
8635 
8636                 IF x_ship_method IS NOT NULL THEN
8637                         OPEN  c_supplier_lead_time;
8638                         FETCH c_supplier_lead_time
8639                         INTO  x_intransit_time, l_level;
8640                         CLOSE c_supplier_lead_time;
8641                 ELSE
8642                         OPEN  c_supplier_default_lead_time;
8643                         FETCH c_supplier_default_lead_time
8644                         INTO  x_ship_method, x_intransit_time, l_level;
8645                         CLOSE c_supplier_default_lead_time;
8646                 END IF;
8647 
8648 	END IF;
8649 
8650 	IF x_intransit_time IS NULL AND x_ship_method IS NOT NULL THEN
8651                 IF PG_DEBUG in ('Y', 'C') THEN
8652                    msc_sch_wb.atp_debug('Using default transit times');
8653                 END IF;
8654 
8655 		-- call myself with null ship method to get defaults
8656 		x_ship_method := NULL;
8657 		get_transit_time(p_from_loc_id,
8658 				 p_from_instance_id,
8659 				 p_to_loc_id,
8660 				 p_to_instance_id,
8661 				 p_session_id,
8662 				 p_partner_site_id,
8663 				 x_ship_method,
8664 				 x_intransit_time,
8665 				 p_supplier_site_id,-- For supplier intransit LT project
8666 				 p_partner_type,--2814895
8667 	                         p_party_site_id,--2814895
8668 	                         p_order_line_id ); --2814895
8669 	END IF;
8670 
8671 
8672         IF PG_DEBUG in ('Y', 'C') THEN
8673            msc_sch_wb.atp_debug('transit_time:' || x_intransit_time);
8674            msc_sch_wb.atp_debug('ship_method:'  || x_ship_method);
8675         END IF;
8676 
8677 END get_transit_time;
8678 
8679 --
8680 -- Calculate the delivery lead time.
8681 --
8682 --
8683 -- Get the to and from locations if needed
8684 --
8685 -- 1) first get the location to location transit time using ship method if given
8686 -- 2) if that fails, then get the org to org transit time using ship method if given
8687 -- 3) get the default location to location transit time if no ship method given,
8688 --	or 1,2 failed
8689 -- 4) if that fails, then get the default org to org transit time
8690 -- 5) if everything fails, just set the delivery lead time to 0
8691 --
8692 -- If we care about the ship method and it's not specified
8693 -- 6) set the ship method to be the default location to location ship method
8694 -- 7) if that fails, set it to be the dafault org to org ship method
8695 --
8696 -- The following functions are deprecated
8697 --
8698 --    get_default_ship_method
8699 --    get_default_intransit_time
8700 --    get_ship_method
8701 --    get_intransit_time
8702 --    get_interloc_transit_time
8703 --    (replaced by get_delivery_lead_time, get_transit_time)
8704 --
8705 --    src_interloc_transit_time
8706 --    src_default_intransit_time
8707 --    src_default_ship_method
8708 --    src_ship_method
8709 --    src_intransit_time
8710 --    (replaced by get_src_transit_time)
8711 
8712 PROCEDURE get_delivery_lead_time(
8713         p_from_org_id		IN	NUMBER,
8714         p_from_loc_id		IN 	NUMBER,
8715         p_instance_id	  	IN	NUMBER,
8716         p_to_org_id	  	IN	NUMBER,
8717         p_to_loc_id		IN 	NUMBER,
8718         p_to_instance_id 	IN	NUMBER,
8719         p_customer_id		IN	NUMBER,
8720         p_customer_site_id	IN	NUMBER,
8721         p_supplier_id           IN      NUMBER,
8722         p_supplier_site_id      IN      NUMBER,
8723         p_session_id	  	IN	NUMBER,
8724         p_partner_site_id	IN	NUMBER,
8725         p_ship_method	  	IN OUT NoCopy	VARCHAR2,
8726         x_delivery_lead_time 	OUT NoCopy	NUMBER,
8727         p_partner_type          IN      NUMBER, --2814895
8728         p_party_site_id         IN      NUMBER , --2814895
8729 	p_order_line_id         IN      NUMBER  --2814895
8730 ) IS
8731         l_from_loc_id		NUMBER;
8732         l_to_loc_id		NUMBER;
8733         l_cache_idx             NUMBER;
8734         l_dlt_key               VARCHAR2(64);
8735 BEGIN
8736 
8737     IF PG_DEBUG IN ('Y', 'C') THEN
8738         msc_sch_wb.atp_debug('PROCEDURE get_delivery_lead_time');
8739         msc_sch_wb.atp_debug('   org from: ' || p_from_org_id ||
8740                              '   to: '       || p_to_org_id);
8741         msc_sch_wb.atp_debug('   loc from: ' || p_from_loc_id ||
8742                              '   to: '       || p_to_loc_id);
8743         msc_sch_wb.atp_debug('   instance from: ' || p_instance_id ||
8744                              '   to: ' 	         || p_to_instance_id);
8745         msc_sch_wb.atp_debug('   customer id: '  || p_customer_id ||
8746                              '   site_id: '      || p_customer_site_id);
8747         msc_sch_wb.atp_debug('   supplier id: '  || p_supplier_id ||
8748                              '   site_id: '      || p_supplier_site_id);
8749         msc_sch_wb.atp_debug('   session_id: '    || p_session_id);
8750         msc_sch_wb.atp_debug('   partner_site: '  || p_partner_site_id);
8751         msc_sch_wb.atp_debug('   ship method: '   || p_ship_method);
8752         msc_sch_wb.atp_debug('   p_partner_type: '|| p_partner_type); --2814895
8753         msc_sch_wb.atp_debug('   p_party_site_id: '|| p_party_site_id);
8754         msc_sch_wb.atp_debug('   p_order_line_id: '|| p_order_line_id);
8755     END IF;
8756 
8757     l_to_loc_id   := p_to_loc_id;
8758 
8759     IF  NOT (p_party_site_id IS NOT NULL AND NVL(p_partner_type, -1) = 3) THEN  --2814895
8760        l_to_loc_id   := p_to_loc_id;
8761     END IF; -- 2814895, to give customer_sire_id preference over party_site_id
8762 
8763     l_from_loc_id := p_from_loc_id;
8764 
8765 
8766     l_dlt_key := p_to_org_id   || ':' || p_to_instance_id   || ':' ||
8767                  p_customer_id || ':' || p_customer_site_id || ':' ||
8768                  p_supplier_id || ':' || p_supplier_site_id || ':' ||
8769                  p_from_org_id || ':' || p_instance_id || ':' ||
8770                  p_ship_method;
8771 
8772     -- check if I've already calculated dlt for these parameters
8773 
8774     IF dlt_cache.count > 0 THEN
8775         for l_cache_idx in reverse dlt_lookup.first..dlt_lookup.count loop
8776             if dlt_lookup(l_cache_idx) = l_dlt_key then
8777                 x_delivery_lead_time := dlt_cache(l_cache_idx);
8778                 p_ship_method := ship_method_cache(l_cache_idx);
8779                 IF PG_DEBUG in ('Y', 'C') THEN
8780                     msc_sch_wb.atp_debug('dlt cache hit! idx: ' || dlt_idx ||
8781                                          ' dlt: ' || x_delivery_lead_time ||
8782                                          ' ship_method: ' || p_ship_method);
8783                 END IF;
8784                 return;
8785             end if;
8786         end loop;
8787     END IF;
8788 
8789 	IF l_to_loc_id IS NULL THEN
8790 		IF p_customer_id IS NOT NULL AND p_customer_site_id IS NOT NULL THEN
8791 	     		l_to_loc_id := MSC_ATP_FUNC.get_location_id(
8792 					p_instance_id,
8793 					NULL,
8794 					p_customer_id,
8795 					p_customer_site_id,
8796 					NULL,
8797 					NULL);
8798 		--ELSE
8799 		ELSIF p_to_org_id IS NOT NULL AND (p_customer_id is NULL AND p_customer_site_id IS NULL) THEN
8800         -- Bug 3515520, don't use org in case customer/site is populated.
8801 	     		l_to_loc_id := MSC_ATP_FUNC.get_location_id(
8802 					p_instance_id,
8803 					p_to_org_id,
8804 					NULL,
8805 					NULL,
8806 					NULL,
8807 					NULL);
8808 		END IF;
8809                 IF PG_DEBUG in ('Y', 'C') THEN
8810                    msc_sch_wb.atp_debug('location to: '      || l_to_loc_id);
8811                 END IF;
8812 	END IF;
8813 
8814 	IF l_to_loc_id IS NULL THEN --2814895
8815 	   l_to_loc_id   := p_to_loc_id;
8816 	END IF;
8817 
8818 	IF l_from_loc_id IS NULL AND p_from_org_id IS NOT NULL THEN
8819                 -- For supplier intransit LT project
8820                 -- call get_location only when from_org is passsed
8821 		l_from_loc_id := MSC_ATP_FUNC.get_location_id(p_instance_id,
8822 					p_from_org_id,
8823 					NULL,
8824 					NULL,
8825 					p_supplier_id,
8826 					p_supplier_site_id);
8827                 IF PG_DEBUG in ('Y', 'C') THEN
8828                    msc_sch_wb.atp_debug('location from: ' || l_from_loc_id);
8829                 END IF;
8830 	END IF;
8831 
8832 	get_transit_time(l_from_loc_id,
8833 			 p_instance_id,
8834 			 l_to_loc_id,
8835 			 p_to_instance_id,
8836 			 p_session_id,
8837 			 p_partner_site_id,
8838 			 p_ship_method,
8839 			 x_delivery_lead_time,
8840 			 p_supplier_site_id, -- For supplier intransit LT project
8841 			 p_partner_type, --2814895
8842 			 p_party_site_id, --2814895
8843 			 p_order_line_id );  --2814895
8844 
8845         IF NVL(x_delivery_lead_time, -1) = -1 THEN
8846            p_ship_method := null;
8847            x_delivery_lead_time := 0;
8848         END IF;
8849 
8850         dlt_idx := dlt_idx+1;
8851         IF dlt_idx = MAX_DLT_CACHE_SZ THEN
8852            dlt_idx := 1;
8853         END IF;
8854 
8855         if dlt_cache.count() < MAX_DLT_CACHE_SZ then
8856            dlt_lookup.extend();
8857            dlt_cache.extend();
8858            ship_method_cache.extend();
8859         end if;
8860 
8861         -- add dlt and ship_method to cache
8862         dlt_lookup(dlt_idx) := l_dlt_key;
8863         dlt_cache(dlt_idx) := x_delivery_lead_time;
8864         ship_method_cache(dlt_idx) := p_ship_method;
8865 
8866         IF PG_DEBUG in ('Y', 'C') THEN
8867            msc_sch_wb.atp_debug('   ship method = ' || p_ship_method);
8868            msc_sch_wb.atp_debug('   delivery_lead_time = ' || x_delivery_lead_time);
8869            msc_sch_wb.atp_debug('END get_delivery_lead_time');
8870         END IF;
8871 
8872 END get_delivery_lead_time;
8873 
8874 PROCEDURE number_arr_cat (
8875         p1      IN OUT NOCOPY   mrp_atp_pub.number_arr,
8876         p2      IN              mrp_atp_pub.number_arr
8877 ) IS
8878         len        number;
8879 BEGIN
8880         len := p1.count();
8881         p1.extend(p2.count);
8882         for i in 1..p2.count() loop
8883                 p1(len + i) := p2(i);
8884         end loop;
8885 END number_arr_cat;
8886 
8887 PROCEDURE date_arr_cat (
8888         p1      IN OUT NOCOPY   mrp_atp_pub.date_arr,
8889         p2      IN              mrp_atp_pub.date_arr
8890 ) IS
8891         len        number;
8892 BEGIN
8893         len := p1.count();
8894         p1.extend(p2.count);
8895         for i in 1..p2.count() loop
8896                p1(len + i) := p2(i);
8897         end loop;
8898 END date_arr_cat;
8899 
8900 PROCEDURE cleanup_set(
8901         p_instance_id   IN      number,
8902         p_plan_id       IN      number,
8903         peg_ids         IN      mrp_atp_pub.number_arr,
8904         dmd_class_flag  IN      mrp_atp_pub.number_arr
8905 ) IS
8906         l_return_sts varchar2(1);
8907 BEGIN
8908         FOR i in 1..peg_ids.count LOOP
8909             MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
8910               peg_ids(i),
8911               p_instance_id,
8912               p_plan_id,
8913               MSC_ATP_PVT.UNDO,
8914               dmd_class_flag(i),
8915               l_return_sts);
8916         end loop;
8917 END cleanup_set;
8918 
8919 --(ssurendr) Bug 2865389 Create a New procedure for Process Effectivity
8920 PROCEDURE Get_Process_Effectivity (
8921                              p_plan_id             IN NUMBER,
8922                              p_item_id             IN NUMBER,
8923                              p_organization_id     IN NUMBER,
8924                              p_sr_instance_id      IN NUMBER,
8925                              p_new_schedule_date   IN DATE,
8926                              p_requested_quantity  IN NUMBER,
8927                              x_process_seq_id      OUT NOCOPY NUMBER,
8928                              x_routing_seq_id      OUT NOCOPY NUMBER,
8929                              x_bill_seq_id         OUT NOCOPY NUMBER,
8930                              x_op_seq_id           OUT NOCOPY NUMBER, --4570421
8931                              x_return_status       OUT NOCOPY VARCHAR2)
8932 
8933 IS
8934 BEGIN
8935     IF PG_DEBUG in ('Y', 'C') THEN
8936         msc_sch_wb.atp_debug('***** Inside Get Process Effectivity *****');
8937         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'Selecting Process Sequence ID');
8938         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'plan_id = '|| p_plan_id);
8939         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'item id = '|| p_item_id);
8940         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'p_organization_id = '|| p_organization_id);
8941         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'p_sr_instance_id = '|| p_sr_instance_id);
8942         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'p_new_schedule_date = '|| p_new_schedule_date);
8943         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'p_requested_quantity = '|| p_requested_quantity);
8944     END IF;
8945 
8946     -- initialize API return status to success
8947     x_return_status := FND_API.G_RET_STS_SUCCESS;
8948 
8949     SELECT  a.process_sequence_id,a.routing_sequence_id, a.bill_sequence_id, a.operation_sequence_id --4570421
8950     INTO x_process_seq_id,x_routing_seq_id,x_bill_seq_id, x_op_seq_id
8951     FROM
8952     (
8953     /*
8954     SELECT process_sequence_id,routing_sequence_id,bill_sequence_id
8955     FROM msc_process_effectivity
8956     WHERE plan_id = p_plan_id
8957     AND   organization_id = p_organization_id
8958     AND   item_id = p_item_id
8959     AND   sr_instance_id = p_sr_instance_id
8960     AND   p_requested_quantity BETWEEN NVL(minimum_quantity,0) AND
8961                DECODE(NVL(maximum_quantity,0),0,99999999,maximum_quantity)
8962     /* rajjain 3008611
8963      * effective date should be greater than or equal to greatest of PTF date, sysdate and start date
8964      * disable date should be less than or equal to greatest of PTF date, sysdate and start date
8965     AND   TRUNC(effectivity_date) <= TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
8966     AND   TRUNC(NVL(disable_date,GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
8967                > TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
8968     ORDER BY preference */
8969     --4570421
8970     SELECT eff.process_sequence_id, eff.routing_sequence_id, eff.bill_sequence_id, op.operation_sequence_id
8971     FROM msc_process_effectivity eff, msc_routing_operations op
8972     WHERE eff.plan_id = p_plan_id
8973     AND   eff.organization_id = p_organization_id
8974     AND   eff.item_id = p_item_id
8975     AND   eff.sr_instance_id = p_sr_instance_id
8976     AND   p_requested_quantity BETWEEN NVL(eff.minimum_quantity,0) AND
8977                DECODE(NVL(eff.maximum_quantity,0),0,99999999,maximum_quantity)
8978     AND   TRUNC(eff.effectivity_date) <= TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
8979     AND   TRUNC(NVL(eff.disable_date,GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
8980                > TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
8981     --4570421
8982     and   eff.plan_id = op.plan_id(+)
8983     AND   eff.sr_instance_id = op.sr_instance_id(+)
8984     and   eff.routing_sequence_id = op.routing_sequence_id(+)
8985     AND   TRUNC(op.effectivity_date(+)) <= TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE))
8986     AND   TRUNC(NVL(op.disable_date(+),GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)+1))
8987                > TRUNC(GREATEST(p_new_schedule_date, sysdate, MSC_ATP_PVT.G_PTF_DATE)) --4570421
8988     ORDER BY eff.preference, op.operation_seq_num
8989     ) a
8990     where rownum = 1;
8991 
8992     IF PG_DEBUG in ('Y', 'C') THEN
8993         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'Routing Id:' || x_routing_seq_id || ' Bill Seq id :' || x_bill_seq_id || ' Procees seq ID:' || x_process_seq_id
8994                               || ' Operation seq ID:' || x_op_seq_id); --4570421
8995         msc_sch_wb.atp_debug('*****  END Get Process Effectivity *****');
8996     END IF;
8997 EXCEPTION
8998     -- 3027711
8999     WHEN NO_DATA_FOUND THEN
9000         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'Could not get process effectivity Values');
9001         x_process_seq_id := null;
9002         x_routing_seq_id := null;
9003         x_bill_seq_id := null;
9004     WHEN OTHERS THEN
9005         msc_sch_wb.atp_debug('Get_Process_Effectivity: ' || 'sqlcode: ' || sqlcode);
9006         x_process_seq_id := null;
9007         x_routing_seq_id := null;
9008         x_bill_seq_id := null;
9009         x_return_status := FND_API.G_RET_STS_ERROR;
9010 END get_process_effectivity;
9011 
9012 -- supplier intransit LT
9013 PROCEDURE Get_Supplier_Regions (p_vendor_site_id    IN  NUMBER,
9014                                 p_calling_module    IN  NUMBER,
9015                                 p_instance_id       IN  NUMBER,
9016                                 x_return_status     OUT NOCOPY VARCHAR2)
9017 IS
9018 l_counter       PLS_INTEGER;
9019 BEGIN
9020         IF PG_DEBUG in ('Y', 'C') THEN
9021            msc_sch_wb.atp_debug('*************** Get_Supplier_Regions Begin *************');
9022            msc_sch_wb.atp_debug('Get_Supplier_Regions :' || 'p_vendor_site_id := ' || p_vendor_site_id);
9023         END IF;
9024 
9025         x_return_status := FND_API.G_RET_STS_SUCCESS;
9026 
9027         FOR l_counter IN 1..G_VENDOR_SITE_ID.COUNT LOOP
9028             IF MSC_ATP_PROC.G_VENDOR_SITE_ID(l_counter) = p_vendor_site_id THEN
9029                 msc_sch_wb.atp_debug('Get_Supplier_Regions :' || 'Data for site ' || p_vendor_site_id ||
9030                         ' already exists.');
9031                 return;
9032             END IF;
9033         END LOOP;
9034 
9035         MSC_SATP_FUNC.Get_Regions(NULL,                     -- p_customer_site_id
9036                                   p_calling_module,         -- p_calling_module
9037                                   p_instance_id,            -- p_instance_id
9038                                   MSC_ATP_PVT.G_SESSION_ID, -- p_session_id
9039                                   NULL,                     -- p_dblink
9040                                   x_return_status,          -- x_return_status
9041                                   NULL,                     -- p_location_id
9042                                   NULL,                     -- p_location_source
9043                                   p_vendor_site_id);        -- p_supplier_site_id
9044 
9045         G_VENDOR_SITE_ID.Extend();
9046         G_VENDOR_SITE_ID(G_VENDOR_SITE_ID.COUNT) := p_vendor_site_id;
9047 
9048         IF PG_DEBUG in ('Y', 'C') THEN
9049            msc_sch_wb.atp_debug('*************** Get_Supplier_Regions End *************');
9050         END IF;
9051 EXCEPTION
9052     WHEN OTHERS THEN
9053         IF PG_DEBUG in ('Y', 'C') THEN
9054                 msc_sch_wb.atp_debug('Get_Supplier_Regions: ' || 'sqlcode: ' || sqlcode);
9055         END IF;
9056         x_return_status := FND_API.G_RET_STS_ERROR;
9057 END Get_Supplier_Regions;
9058 
9059 
9060 -- supplier intransit LT
9061 /*--ATP_Intransit_LT-----------------------------------------------------------
9062 | - Generic API to find intransit lead times to be called mainly by
9063 |   products other than GOP.
9064 | o p_src_dest            - whether being called from source or
9065 |                           destination - 1:Source; 2:Destination
9066 | o p_session_id          - unique number identifying the current session
9067 | o p_from_org_id         - used in org-org and org-cust scenario, should
9068 |                           be null in supp-org case, not required if
9069 |                           p_from_loc_id is provided
9070 | o p_from_loc_id         - used in org-cust scenario, should be null in
9071 |                           org-org and supp-org cases, not required if
9072 |                           p_from_org_id is provided
9073 | o p_from_vendor_site_id - used in supp-org scenario, should be null in
9074 |                           org-org and supp-org cases
9075 | o p_from_instance_id    - from party's instance id, not required when
9076 |                           called from source
9077 | o p_to_org_id           - used in org-org and supp-org scenario, should
9078 |                           be null in org-cust case
9079 | o p_to_loc_id           - used in org-cust scenario, should be null in
9080 |                           org-org and supp-org cases, not required if
9081 |                           p_to_customer_site_id is provided
9082 | o p_to_customer_site_id - used in org-cust scenario, should be null in
9083 |                           org-org and supp-org cases, not required if
9084 |                           p_to_loc_id is provided
9085 | o p_to_instance_id      - to party's instance id, not required when called
9086 |                           from source
9087 | o p_ship_method         - default ship method is used if not passed. if
9088 |                           the passed ship method does not exist in shipping
9089 |                           network then default ship method is returned
9090 | o x_intransit_lead_time - intrasit lead time
9091 | o x_return_status       - return status
9092 +----------------------------------------------------------------------------*/
9093 PROCEDURE ATP_Intransit_LT (p_src_dest              IN  NUMBER,
9094                             p_session_id            IN  NUMBER,
9095                             p_from_org_id           IN  NUMBER,
9096                             p_from_loc_id           IN  NUMBER,
9097                             p_from_vendor_site_id   IN  NUMBER,
9098                             p_from_instance_id      IN  NUMBER,
9099                             p_to_org_id             IN  NUMBER,
9100                             p_to_loc_id             IN  NUMBER,
9101                             p_to_customer_site_id   IN  NUMBER,
9102                             p_to_instance_id        IN  NUMBER,
9103                             p_ship_method           IN OUT  NoCopy VARCHAR2,
9104                             x_intransit_lead_time   OUT     NoCopy NUMBER,
9105                             x_return_status         OUT NOCOPY VARCHAR2
9106 )
9107 IS
9108     l_calling_module    NUMBER;
9109     l_return_status     VARCHAR2(1);
9110 BEGIN
9111     -- initialize API return status to success
9112     x_return_status := FND_API.G_RET_STS_SUCCESS;
9113 
9114     msc_sch_wb.set_session_id(p_session_id);
9115     IF PG_DEBUG in ('Y', 'C') THEN
9116        msc_sch_wb.atp_debug('***************Begin ATP_Intransit_LT *****************');
9117        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_src_dest              : ' || p_src_dest );
9118        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_session_id            : ' || p_session_id );
9119        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_from_org_id           : ' || p_from_org_id );
9120        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_from_loc_id           : ' || p_from_loc_id );
9121        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_from_vendor_site_id   : ' || p_from_vendor_site_id );
9122        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_from_instance_id      : ' || p_from_instance_id );
9123        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_to_org_id             : ' || p_to_org_id );
9124        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_to_loc_id             : ' || p_to_loc_id );
9125        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_to_customer_site_id   : ' || p_to_customer_site_id );
9126        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_to_instance_id        : ' || p_to_instance_id );
9127        msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'p_ship_method           : ' || p_ship_method );
9128     END IF;
9129 
9130     IF p_from_vendor_site_id IS NOT NULL THEN
9131         IF p_src_dest = 1 THEN
9132             IF PG_DEBUG in ('Y', 'C') THEN
9133                 msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'Invalid parameter - supp-org not supported from source');
9134             END IF;
9135             x_return_status := FND_API.G_RET_STS_ERROR;
9136         ELSE
9137             IF PG_DEBUG in ('Y', 'C') THEN
9138                 msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || ': supp-org case');
9139             END IF;
9140             MSC_SATP_FUNC.Get_Regions(NULL,                 -- p_customer_site_id
9141                                       724,                  -- p_calling_module
9142                                       p_from_instance_id,   -- p_instance_id
9143                                       p_session_id,         -- p_session_id
9144                                       NULL,                 -- p_dblink
9145                                       l_return_status,      -- x_return_status
9146                                       NULL,                 -- p_location_id
9147                                       NULL,                 -- p_location_source
9148                                       p_from_vendor_site_id -- p_supplier_site_id
9149                                      );
9150 
9151             Get_Delivery_Lead_Time(NULL,                    -- p_from_org_id
9152                                    NULL,                    -- p_from_loc_id
9153                                    p_from_instance_id,      -- p_instance_id
9154                                    p_to_org_id,             -- p_to_org_id
9155                                    NULL,                    -- p_to_loc_id
9156                                    p_to_instance_id,        -- p_to_instance_id
9157                                    NULL,                    -- p_customer_id
9158                                    NULL,                    -- p_customer_site_id
9159                                    NULL,                    -- p_supplier_id
9160                                    p_from_vendor_site_id,   -- p_supplier_site_id
9161                                    p_session_id,            -- p_session_id
9162                                    NULL,                    -- p_partner_site_id
9163                                    p_ship_method,           -- p_ship_method
9164                                    x_intransit_lead_time    -- x_delivery_lead_time
9165                                   );
9166         END IF;
9167     ELSIF p_to_customer_site_id IS NOT NULL OR p_to_loc_id IS NOT NULL THEN
9168         IF PG_DEBUG in ('Y', 'C') THEN
9169             msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || ': org-cust case');
9170         END IF;
9171         IF p_src_dest = 1 THEN
9172             MSC_SATP_FUNC.Get_Regions(p_to_customer_site_id,    -- p_customer_site_id
9173                                       -1,                       -- p_calling_module (not 724)
9174                                       NULL,                     -- p_instance_id
9175                                       p_session_id,             -- p_session_id
9176                                       NULL,                     -- p_dblink
9177                                       l_return_status,          -- x_return_status
9178                                       p_to_loc_id,              -- p_location_id
9179                                       'HZ',                     -- p_location_source
9180                                       NULL                      -- p_supplier_site_id
9181                                      );
9182 
9183             MSC_SATP_FUNC.get_src_transit_time(p_from_org_id,   -- p_from_org_id
9184                                        p_from_loc_id,           -- p_from_loc_id
9185                                        NULL,                    -- p_to_org_id
9186                                        p_to_loc_id,             -- p_to_loc_id
9187                                        p_session_id,            -- p_session_id
9188                                        p_to_customer_site_id,   -- p_partner_site_id
9189                                        p_ship_method,           -- x_ship_method
9190                                        x_intransit_lead_time    -- x_intransit_time
9191                                       );
9192         ELSE
9193             MSC_SATP_FUNC.Get_Regions(p_to_customer_site_id,    -- p_customer_site_id
9194                                       724,                      -- p_calling_module
9195                                       p_from_instance_id,       -- p_instance_id
9196                                       p_session_id,             -- p_session_id
9197                                       NULL,                     -- p_dblink
9198                                       l_return_status,          -- x_return_status
9199                                       p_to_loc_id,              -- p_location_id
9200                                       'HZ',                     -- p_location_source
9201                                       NULL                      -- p_supplier_site_id
9202                                      );
9203 
9204             Get_Delivery_Lead_Time(p_from_org_id,           -- p_from_org_id
9205                                    p_from_loc_id,           -- p_from_loc_id
9206                                    p_from_instance_id,      -- p_instance_id
9207                                    NULL,                    -- p_to_org_id
9208                                    p_to_loc_id,             -- p_to_loc_id
9209                                    p_to_instance_id,        -- p_to_instance_id
9210                                    NULL,                    -- p_customer_id
9211                                    p_to_customer_site_id,   -- p_customer_site_id
9212                                    NULL,                    -- p_supplier_id
9213                                    p_from_vendor_site_id,   -- p_supplier_site_id
9214                                    p_session_id,            -- p_session_id
9215                                    p_to_customer_site_id,   -- p_partner_site_id
9216                                    p_ship_method,           -- p_ship_method
9217                                    x_intransit_lead_time    -- x_delivery_lead_time
9218                                   );
9219         END IF;
9220     ELSE
9221         IF PG_DEBUG in ('Y', 'C') THEN
9222             msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || ': org-org case');
9223         END IF;
9224         IF p_src_dest = 1 THEN
9225             MSC_SATP_FUNC.get_src_transit_time(p_from_org_id,   -- p_from_org_id
9226                                        p_from_loc_id,           -- p_from_loc_id
9227                                        p_to_org_id,             -- p_to_org_id
9228                                        p_to_loc_id,             -- p_to_loc_id
9229                                        p_session_id,            -- p_session_id
9230                                        NULL,                    -- p_partner_site_id
9231                                        p_ship_method,           -- x_ship_method
9232                                        x_intransit_lead_time    -- x_intransit_time
9233                                       );
9234         ELSE
9235             Get_Delivery_Lead_Time(p_from_org_id,           -- p_from_org_id
9236                                    p_from_loc_id,           -- p_from_loc_id
9237                                    p_from_instance_id,      -- p_instance_id
9238                                    p_to_org_id,             -- p_to_org_id
9239                                    p_to_loc_id,             -- p_to_loc_id
9240                                    p_to_instance_id,        -- p_to_instance_id
9241                                    NULL,                    -- p_customer_id
9242                                    NULL,                    -- p_customer_site_id
9243                                    NULL,                    -- p_supplier_id
9244                                    NULL,                    -- p_supplier_site_id
9245                                    p_session_id,            -- p_session_id
9246                                    NULL,                    -- p_partner_site_id
9247                                    p_ship_method,           -- p_ship_method
9248                                    x_intransit_lead_time    -- x_delivery_lead_time
9249                                   );
9250         END IF;
9251     END IF;
9252 
9253 
9254 EXCEPTION
9255     WHEN OTHERS THEN
9256         IF PG_DEBUG in ('Y', 'C') THEN
9257            msc_sch_wb.atp_debug('ATP_Intransit_LT: ' || 'Error code:' || to_char(sqlcode));
9258         END IF;
9259         x_return_status := FND_API.G_RET_STS_ERROR;
9260 END ATP_Intransit_LT;
9261 
9262 END MSC_ATP_PROC;