DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_PROC

Source


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