DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_ATP_DB_UTILS

Source


1 PACKAGE BODY MSC_ATP_DB_UTILS AS
2 /* $Header: MSCDATPB.pls 120.25 2011/12/23 07:19:51 vjuluri ship $  */
3 
4 G_PKG_NAME              CONSTANT VARCHAR2(30) := 'MSC_ATP_DB_UTILS';
5 
6 -- dsting: 0 if Clear_SD_Details_temp has been called, 0 otherwise.
7 -- used to save 1 delete in sd performance enh
8 PG_CLEAR_SD_DETAILS_TEMP	NUMBER := 0;
9 
10 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('MSC_ATP_DEBUG'), 'N');
11 G_ORIG_INV_CTP NUMBER := FND_PROFILE.value('INV_CTP'); -- Bug 3295831.
12         -- Added to control summary operations.
13 
14 --5357370 private declaration
15 PROCEDURE UNDO_DELETE_SUMMARY_ROW (p_identifier                      IN NUMBER,
16                                    p_instance_id                     IN NUMBER,
17                                    p_demand_source_type              IN NUMBER
18                                    );
19 
20 PROCEDURE Add_Mat_Demand(
21   p_atp_rec          IN		MRP_ATP_PVT.AtpRec ,
22   p_plan_id          IN         NUMBER ,
23   p_dc_flag          IN         NUMBER,
24   x_demand_id        OUT        NoCopy NUMBER
25 )
26 
27 IS
28 l_sqlfound	BOOLEAN := FALSE;
29 my_sqlcode 	NUMBER;
30 l_count         NUMBER;
31 temp_sd_qty     number;
32 l_record_source number := 2; -- for plan order pegging
33 
34 -- time_phased_atp
35 l_time_phased_atp       varchar2(1) := 'N';
36 l_insert_item_id        number;
37 l_return_status         varchar2(1);
38 lv_apps_ver    VARCHAR2(30); --bug#12973673 -- enable the triple checkin for MSCDATPB.pls file
39 l_is_rel_120	BOOLEAN := FALSE;
40 lv_sql_stmt     VARCHAR2(32767);
41 l_stmt          VARCHAR2(32767);
42 
43 BEGIN
44     IF PG_DEBUG in ('Y', 'C') THEN
45         msc_sch_wb.atp_debug('***** Begin Add_Mat_Demand *****');
46         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.quantity_ordered '||p_atp_rec.quantity_ordered);
47         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.requested_ship_date '||p_atp_rec.requested_ship_date);
48         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.origination_type '||p_atp_rec.origination_type);
49         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.inventory_item_id '||p_atp_rec.inventory_item_id);
50         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.organization_id '||p_atp_rec.organization_id);
51         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.demand_source_line '||p_atp_rec.demand_source_line);
52         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.demand_source_type '||p_atp_rec.demand_source_type);--cmro
53         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.instance_id '||p_atp_rec.instance_id);
54         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_plan_id = ' || p_plan_id);
55         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.action '||p_atp_rec.action);
56         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'old_demand_id := ' || p_atp_rec.old_demand_id);
57         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'demand class := ' || p_atp_rec.demand_class);
58         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.request_item_id := ' || p_atp_rec.request_item_id);
59         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.atf_date := ' || p_atp_rec.atf_date);
60         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'ato model line id := ' || p_atp_rec.ato_model_line_id);
61         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.MAND_COMP_FLAG := ' || p_atp_rec.MAND_COMP_FLAG);	 --Bug#6647536
62     END IF;
63 
64     -- time_phased_atp changes begin
65     IF (p_atp_rec.inventory_item_id <> p_atp_rec.request_item_id) and p_atp_rec.atf_date is not null THEN
66         l_time_phased_atp := 'Y';
67         /* In time phased atp scenarios add demand in msc_demands for member item*/
68         l_insert_item_id := p_atp_rec.request_item_id;
69         IF PG_DEBUG in ('Y', 'C') THEN
70             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Time Phased ATP = ' || l_time_phased_atp);
71         END IF;
72     ELSE
73         l_insert_item_id := p_atp_rec.inventory_item_id;
74     END IF;
75     IF PG_DEBUG in ('Y', 'C') THEN
76         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Insert demand in msc_demands for ' || l_insert_item_id);
77     END IF;
78     -- time_phased_atp changes end
79 
80     IF (p_plan_id = -1) THEN  -- ods, put into ods tables
81 
82         IF PG_DEBUG in ('Y', 'C') THEN
83             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'ODS based ATP, Add into MSC_SALES Orders');
84             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'p_atp_rec.original_request_ship_date: ' ||p_atp_rec.original_request_ship_date);
85         END IF;
86 		--bug#12973673 -- enable the triple checkin for MSCDATPB.pls file
87 		/* Design : read the patch_level in fnd_product_installations.
88 		 If it is MSC.A --> We are in MSC120. If it is not null and some other value --> It is other than MSC120.
89 		In case it is null --> Try to read the row_type --> It caught with exception --> We are in MSC120 */
90 
91         BEGIN
92                 SELECT substr(PATCH_LEVEL,1,9)
93                     INTO lv_apps_ver
94                 FROM fnd_product_installations
95                     WHERE APPLICATION_ID = 724;
96         EXCEPTION
97                 WHEN others THEN
98                     lv_apps_ver := null;
99                 IF PG_DEBUG in ('Y', 'C') THEN
100                     msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Could not get the source instance version..');
101                 END IF;
102         END;
103 
104         IF (lv_apps_ver IS NOT NULL AND lv_apps_ver = 'R12.MSC.A') then
105                 l_is_rel_120 := TRUE;
106         ELSIF (lv_apps_ver IS NOT NULL AND ( lv_apps_ver = 'R12.MSC.B' or lv_apps_ver = 'R12.MSC.C')) then
107                 l_is_rel_120 := FALSE;
108         ELSE
109             BEGIN
110                 l_stmt := 'select row_type from msc_sales_orders where 2=1';
111                 EXECUTE IMMEDIATE l_stmt;
112             EXCEPTION
113                     WHEN others THEN
114                         IF SQLCODE IN (-00904) THEN
115                              l_is_rel_120 := true;
116                              IF PG_DEBUG in ('Y', 'C') THEN
117                                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Row_type column doesnot exists in msc_sales_orders. So release is MSC120');
118                              END IF;
119                         END IF;
120             END;
121         END IF;
122 
123         IF PG_DEBUG in ('Y', 'C') THEN
124             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Source Instance Version : lv_apps_ver : ' || lv_apps_ver);
125         END IF;
126 
127         IF (l_is_rel_120)  THEN
128 				INSERT INTO MSC_SALES_ORDERS(
129 						DEMAND_ID,
130 						SR_INSTANCE_ID,
131 						INVENTORY_ITEM_ID,
132 						ORGANIZATION_ID,
133 						DEMAND_SOURCE_TYPE,
134 						DEMAND_SOURCE_HEADER_ID,
135 						DEMAND_SOURCE_LINE,
136 						DEMAND_SOURCE_DELIVERY,
137 						PRIMARY_UOM_QUANTITY,
138 						COMPLETED_QUANTITY,
139 						RESERVATION_TYPE,
140 						REQUIREMENT_DATE,
141 						LAST_UPDATE_DATE,
142 						LAST_UPDATED_BY,
143 						CREATION_DATE,
144 						CREATED_BY,
145 						DEMAND_CLASS,
146 						ATP_REFRESH_NUMBER,
147 						SALES_ORDER_NUMBER,
148 						ato_line_id,
149 						RECORD_SOURCE,       -- bug 2810112
150 						REQUEST_SHIP_DATE,   --plan by request date
151 						SCHEDULE_SHIP_DATE,  --plan by request date
152 						CUSTOMER_ID,         --bug3263368
153 						SHIP_TO_SITE_USE_ID, --bug3263368
154 						--MFG_LEAD_TIME, --bug3263368
155 						--bug3578083 New column Intransit_lead_time was added for bug 3403975
156 						--which now holds the value of Intransit Lead Time
157 						INTRANSIT_LEAD_TIME,
158 						SHIP_SET_NAME, --bug3263368
159 						ARRIVAL_SET_NAME) --bug3263368
160 				VALUES( msc_demands_s.nextval,
161 						p_atp_rec.instance_id,
162 						p_atp_rec.inventory_item_id,
163 						p_atp_rec.organization_id,
164 						p_atp_rec.demand_source_type,
165 						p_atp_rec.demand_source_header_id,
166 						p_atp_rec.demand_source_line,
167 						p_atp_rec.demand_source_delivery,
168 						MSC_ATP_UTILS.Truncate_Demand(p_atp_rec.quantity_ordered),  --5598066
169 						0,
170 						1,
171 						TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY , -- For bug 2259824
172 						sysdate,
173 						FND_GLOBAL.USER_ID,
174 						sysdate,
175 						FND_GLOBAL.USER_ID,
176 						p_atp_rec.demand_class,
177 						p_atp_rec.refresh_number,
178 						p_atp_rec.order_number,
179 						p_atp_rec.ato_model_line_id,
180 						l_record_source, -- bug 2810112
181 						--start changes for plan by request date
182 						p_atp_rec.original_request_ship_date,
183 						p_atp_rec.requested_ship_date + MSC_ATP_PVT.G_END_OF_DAY,
184 						--end changes for plan by request date
185 						MSC_ATP_PVT.G_PARTNER_ID,
186 						MSC_ATP_PVT.G_PARTNER_SITE_ID,
187 						p_atp_rec.delivery_lead_time, --bug3263368
188 						p_atp_rec.ship_set_name, --bug3263368
189 						p_atp_rec.arrival_set_name --bug3263368
190 						)
191 				RETURNING DEMAND_ID INTO x_demand_id;
192         ELSE
193 			lv_sql_stmt := 'INSERT INTO MSC_SALES_ORDERS('
194                     ||' DEMAND_ID,'
195                     ||' SR_INSTANCE_ID,'
196                     ||' INVENTORY_ITEM_ID,'
197                     ||' ORGANIZATION_ID,'
198                     ||' DEMAND_SOURCE_TYPE,'
199                     ||' DEMAND_SOURCE_HEADER_ID,'
200                     ||' DEMAND_SOURCE_LINE,'
201                     ||' DEMAND_SOURCE_DELIVERY,'
202                     ||' PRIMARY_UOM_QUANTITY,'
203                     ||' COMPLETED_QUANTITY,'
204                     ||' RESERVATION_TYPE,'
205                     ||' REQUIREMENT_DATE,'
206                     ||' LAST_UPDATE_DATE,'
207                     ||' LAST_UPDATED_BY,'
208                     ||' CREATION_DATE,'
209                     ||' CREATED_BY,'
210                     ||' DEMAND_CLASS,'
211                     ||' ATP_REFRESH_NUMBER,'
212                     ||' SALES_ORDER_NUMBER,'
213                     ||' ato_line_id,'
214                     ||' RECORD_SOURCE,'       -- bug 2810112
215                     ||' REQUEST_SHIP_DATE,'   --plan by request date
216                     ||' SCHEDULE_SHIP_DATE,'  --plan by request date
217                     ||' CUSTOMER_ID,'         --bug3263368
218                     ||' SHIP_TO_SITE_USE_ID,' --bug3263368
219                      --MFG_LEAD_TIME, --bug3263368
220                      --bug3578083 New column Intransit_lead_time was added for bug 3403975
221                      --which now holds the value of Intransit Lead Time
222                     ||' INTRANSIT_LEAD_TIME,'
223                     ||' SHIP_SET_NAME,'       --bug3263368
224                     ||' ARRIVAL_SET_NAME,'    --bug3263368
225                     ||' SR_DEMAND_ID,'        --bug6647536
226                     ||' ROW_TYPE,'            --bug6647536
227                     ||' SR_SO_LINEID)'        --bug6647536
228                     ||' VALUES( msc_demands_s.nextval,'
229                     ||' :instance_id,'
230                     ||' :inventory_item_id,'
231                     ||' :organization_id,'
232                     ||' :demand_source_type,'
233                     ||' :demand_source_header_id,'
234                     ||' :demand_source_line,'
235                     ||' :demand_source_delivery,'
236                     ||' :quantity_ordered,'
237                     ||' 0,'
238                     ||' 1,'
239                     ||' :requested_ship_date,'
240                     ||' sysdate,'
241                     ||' :user_id,'
242                     ||' sysdate,'
243                     ||' :user_id,'
244                     ||' :demand_class,'
245                     ||' :refresh_number,'
246                     ||' :order_number,'
247                     ||' :ato_model_line_id,'
248                     ||' :record_source,'
249                     ||' :original_request_ship_date,'
250                     ||' :requested_ship_date,'
251                     ||' :partner_id,'
252                     ||' :partner_site_id,'
253                     ||' :delivery_lead_time,'
254                     ||' :ship_set_name,'
255                     ||' :arrival_set_name,'
256                     ||' :sr_demand_id,'
257                     ||' Decode(:row_type,1,3,2),'
258                     ||' :sr_so_lineid)'
259                     ||' RETURNING DEMAND_ID INTO :demand_id';
260 
261             EXECUTE IMMEDIATE lv_sql_stmt USING
262                     p_atp_rec.instance_id,
263                     p_atp_rec.inventory_item_id,
264                     p_atp_rec.organization_id,
265                     p_atp_rec.demand_source_type,
266                     p_atp_rec.demand_source_header_id,
267                     p_atp_rec.demand_source_line,
268                     p_atp_rec.demand_source_delivery,
269                     MSC_ATP_UTILS.Truncate_Demand(p_atp_rec.quantity_ordered),    --5598066
270                     TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY , -- For bug 2259824
271                     FND_GLOBAL.USER_ID,
272                     FND_GLOBAL.USER_ID,
273                     p_atp_rec.demand_class,
274                     p_atp_rec.refresh_number,
275                     p_atp_rec.order_number,
276                     p_atp_rec.ato_model_line_id,
277                     l_record_source, -- bug 2810112
278                     p_atp_rec.original_request_ship_date,
279                     p_atp_rec.requested_ship_date + MSC_ATP_PVT.G_END_OF_DAY,
280                     MSC_ATP_PVT.G_PARTNER_ID,
281                     MSC_ATP_PVT.G_PARTNER_SITE_ID,
282                     p_atp_rec.delivery_lead_time, --bug3263368
283                     p_atp_rec.ship_set_name, --bug3263368
284                     p_atp_rec.arrival_set_name, --bug3263368
285                     p_atp_rec.demand_source_line,--bug6647536
286                     p_atp_rec.MAND_COMP_FLAG, --bug6647536
287                     p_atp_rec.demand_source_line--bug6647536
288                     RETURNING INTO x_demand_id;
289         END IF;
290 
291         IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') THEN
292             IF PG_DEBUG in ('Y', 'C') THEN
293                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Insert into summary table');
294                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || ' update msc_atp_summary_so');
295             END IF;
296             -- use hint to do index search
297             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_rec.instance_id,
298                                                -1,
299                                                p_atp_rec.organization_id,
300                                                p_atp_rec.inventory_item_id,
301                                                p_atp_rec.requested_ship_date,
302                                                null,
303                                                null,
304                                                null,
305                                                null,
306                                                p_dc_flag,
307                                                p_atp_rec.demand_class,
308                                                1);
309             UPDATE  /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
310             set     sd_qty = sd_qty + p_atp_rec.quantity_ordered
311             where   inventory_item_id = p_atp_rec.inventory_item_id
312             and     organization_id = p_atp_rec.organization_id
313             and     sr_instance_id = p_atp_rec.instance_id
314             and     sd_date = trunc(p_atp_rec.requested_ship_date)
315             and     demand_class = Decode(p_dc_flag, 1, NVL(p_atp_rec.demand_class, '@@@'),'@@@');
316 
317             IF SQL%NOTFOUND THEN
318                 IF PG_DEBUG in ('Y', 'C') THEN
319                     msc_sch_wb.atp_debug('Add_Mat_Demand: ' || ' insert into msc_atp_summary_so');
320                 END IF;
321                 BEGIN
322                       --- use hint to do index search
323                     insert /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ into msc_atp_summary_so
324                            (plan_id,
325                             inventory_item_id,
326                             organization_id,
327                             sr_instance_id,
328                             sd_date,
329                             sd_qty,
330                             demand_class,
331                             LAST_UPDATE_DATE,
332                             LAST_UPDATED_BY,
333                             CREATION_DATE,
334                             CREATED_BY
335                            )
336                     VALUES
337                         (-1,
338                         p_atp_rec.inventory_item_id,
339                         p_atp_rec.organization_id,
340                         p_atp_rec.instance_id,
341                         -- 2161453 truncate the date
342                         -- if we dont truncate the date then same days will have different enteries
343                         -- with diiferet time components
344                         trunc(p_atp_rec.requested_ship_date),
345                         p_atp_rec.quantity_ordered,
346                         Decode(p_dc_flag, 1, NVL(p_atp_rec.demand_class, '@@@') ,'@@@'),
347                         sysdate,
348                         FND_GLOBAL.USER_ID,
349                         sysdate,
350                         FND_GLOBAL.USER_ID);
351                 EXCEPTION
352                     WHEN  DUP_VAL_ON_INDEX THEN
353                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_rec.instance_id,
354                                                            -1,
355                                                            p_atp_rec.organization_id,
356                                                            p_atp_rec.inventory_item_id,
357                                                            p_atp_rec.requested_ship_date,
358                                                            null,
359                                                            null,
360                                                            null,
361                                                            null,
362                                                            p_dc_flag,
363                                                            p_atp_rec.demand_class,
364                                                            1);
365 
366                         UPDATE  /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
367                         set     sd_qty = sd_qty + p_atp_rec.quantity_ordered
368                         where   inventory_item_id = p_atp_rec.inventory_item_id
369                         and     organization_id = p_atp_rec.organization_id
370                         and     sr_instance_id = p_atp_rec.instance_id
371                         and     sd_date = trunc(p_atp_rec.requested_ship_date)
372                         and     demand_class = Decode(p_dc_flag, 1, NVL(p_atp_rec.demand_class, '@@@'),'@@@');
373 
374                 END;
375 
376             END IF;
377         END IF;
378     ELSE  -- pds, put into pds tables
379 
380         --as this code is redundent commenting it out avjain
381         /*IF (p_atp_rec.origination_type IN (6, 30)) AND NVL(p_atp_rec.old_demand_id, 0) > 0 THEN
382 
383             IF PG_DEBUG in ('Y', 'C') THEN
384                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'inside update of msc_demands');
385             END IF;
386 
387             -- For bug 2259824, move the demand to the end of day
388             UPDATE  msc_demands
389             SET     using_requirement_quantity = p_atp_rec.quantity_ordered,
390                      USING_ASSEMBLY_DEMAND_DATE=
391                      decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
392                     MSC_ATP_PVT.G_SCHEDULE_DATE_LEGEND,
393                     	TRUNC(NVL(p_atp_rec.requested_ship_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
394                     MSC_ATP_PVT.G_PROMISE_DATE_LEGEND,
395                     	TRUNC(NVL(p_atp_rec.requested_ship_date,PROMISE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
396                     MSC_ATP_PVT.G_REQUEST_DATE_LEGEND,
397                     	p_atp_rec.requested_ship_date,
398                     TRUNC(NVL(p_atp_rec.requested_ship_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY),
399                     schedule_ship_date = TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,--added by avjain
400                     promise_ship_date = TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,--added by avjain
401                     request_ship_date=TRUNC(p_atp_rec.original_request_ship_date),--added by avjain
402                     --request_arrival_date=TRUNC(p_atp_rec.original_request_arrival_date),--added by avjain
403                     demand_type = 1,
404                     origination_type = p_atp_rec.origination_type,
405                     plan_id = p_plan_id,
406                     organization_id = p_atp_rec.organization_id,
407                     last_update_date = sysdate,
408                     last_updated_by = FND_GLOBAL.USER_ID,
409                     demand_class = p_atp_rec.demand_class,
410                     refresh_number = p_atp_rec.refresh_number,
411                     order_number = decode(p_atp_rec.origination_type, 6, p_atp_rec.order_number,
412                                                                      30, p_atp_rec.order_number,
413                                                                          null),
414                     applied = decode(p_atp_rec.origination_type, 6, 2, 30, 2, null),
415                     status = decode(p_atp_rec.origination_type, 6, 0, 30, 0, null),
416                     customer_id = MSC_ATP_PVT.G_PARTNER_ID,
417                     ship_to_site_id = MSC_ATP_PVT.G_PARTNER_SITE_ID,
418                     inventory_item_id = p_atp_rec.inventory_item_id,
419                     -- 24x7
420                     atp_synchronization_flag = 0,
421                     -- bug 2795053-reopen (ssurendr) update the demand_satisfied_date in msc_demands
422                     dmd_satisfied_date = TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY
423             WHERE  sr_instance_id = p_atp_rec.instance_id
424             AND    plan_id = p_plan_id
425             AND    sales_order_line_id = p_atp_rec.demand_source_line
426             AND    demand_id = p_atp_rec.old_demand_id
427             RETURNING DEMAND_ID INTO x_demand_id;
428 
429             l_sqlfound := SQL%NOTFOUND;
430 
431             IF NOT l_sqlfound THEN
432                 IF PG_DEBUG in ('Y', 'C') THEN
433                     msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'found demand and updated demand_id : ' || x_demand_id);
434                 END IF;
435                 -- Allocated ATP Based on Planning Details -- Agilent changes Begin
436 
437                 IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
438                    (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
439                    (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
440                    (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
441 
442                     IF PG_DEBUG in ('Y', 'C') THEN
443 	                    msc_sch_wb.atp_debug('Add_Mat_Demand: before update of msc_alloc_demands');
444 	                END IF;
445 
446                     UPDATE  msc_alloc_demands
447                     SET     old_allocated_quantity = allocated_quantity,
448                             old_demand_date = demand_date,
449                             plan_id = p_plan_id,
450                             organization_id = p_atp_rec.organization_id,
451                             demand_class = p_atp_rec.demand_class,
452                             demand_date = p_atp_rec.requested_ship_date,
453                             parent_demand_id = x_demand_id,
454                             allocated_quantity = p_atp_rec.quantity_ordered,
455                             origination_type = p_atp_rec.origination_type,
456                             inventory_item_id = p_atp_rec.inventory_item_id,
457                             order_number = decode(p_atp_rec.origination_type,
458                             6, p_atp_rec.order_number,
459                             30, p_atp_rec.order_number, null),
460                             last_updated_by = FND_GLOBAL.USER_ID,
461                             last_update_date = sysdate,
462                             refresh_number = p_atp_rec.refresh_number -- For summary enhancement
463                     WHERE   sr_instance_id = p_atp_rec.instance_id
464                     AND     plan_id = p_plan_id
465                     AND     sales_order_line_id = p_atp_rec.demand_source_line
466                     AND     parent_demand_id = p_atp_rec.old_demand_id;
467 
468                     IF PG_DEBUG in ('Y', 'C') THEN
469                         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'No of rows updated := ' || SQL%ROWCOUNT);
470                     END IF;
471 
472                 END IF;
473 
474                 -- Allocated ATP Based on Planning Details -- Agilent changes End
475 
476             END IF;
477         END IF; 		--(p_atp_rec.origination_type IN (6, 30)) THEN
478 	*/
479 
480         IF (SQL%NOTFOUND OR (p_atp_rec.origination_type NOT IN (6, 30)) OR
481                                       NVL(p_atp_rec.old_demand_id,0 ) = 0)   THEN
482 
483             IF PG_DEBUG in ('Y', 'C') THEN
484                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'before insert into msc_demands');
485             END IF;
486 
487             INSERT INTO MSC_DEMANDS(
488                     DEMAND_ID,
489                     USING_REQUIREMENT_QUANTITY,
490                     SCHEDULE_SHIP_DATE,         --plan by request date
491                     USING_ASSEMBLY_DEMAND_DATE,
492                     promise_ship_date,          --plan by request date
493                     request_ship_date,          --plan by request date
494                     DEMAND_TYPE,
495                     DEMAND_SOURCE_TYPE, --cmro
496                     ORIGINATION_TYPE,
497                     USING_ASSEMBLY_ITEM_ID,
498                     PLAN_ID,
499                     ORGANIZATION_ID,
500                     INVENTORY_ITEM_ID,
501                     SALES_ORDER_LINE_ID,
502                     SR_INSTANCE_ID,
503                     LAST_UPDATE_DATE,
504                     LAST_UPDATED_BY,
505                     CREATION_DATE,
506                     CREATED_BY,
507                     DEMAND_CLASS,
508                     REFRESH_NUMBER,
509                     ORDER_NUMBER,
510                     APPLIED,
511                     STATUS,
512                     CUSTOMER_ID,
513                     SHIP_TO_SITE_ID,
514                     RECORD_SOURCE,  -- For plan order pegging
515                     -- 24x7
516                     ATP_SYNCHRONIZATION_FLAG,
517                     -- bug 2795053-reopen (ssurendr) insert the demand_satisfied_date in msc_demands
518                     DMD_SATISFIED_DATE,
519                     -- rajjain bug 2771075 04/25/2003 Populate disposition_id column with the demand_id
520                     DISPOSITION_ID,
521                     --s_cto_rearch
522                     LINK_TO_LINE_ID,
523                     ATO_LINE_ID,
524                     TOP_MODEL_LINE_ID,
525                     parent_model_line_id,
526                     std_mandatory_comp_flag,
527                     wip_supply_type,
528                     --e_cto_rearch
529                     /* time_phased_atp
530                        We no longer require Add_Mat_Demand in MSC_ATP_Subst*/
531                     ORIGINAL_ITEM_ID ,
532                     SHIP_METHOD, -- For ship_rec_cal
533                     atp_session_id,
534                     INTRANSIT_LEAD_TIME, --bug3263368
535                     SHIP_SET_NAME, --bug3263368
536                     ARRIVAL_SET_NAME --bug3263368
537                     )
538             VALUES(
539                     msc_demands_s.nextval,
540                     MSC_ATP_UTILS.Truncate_Demand(p_atp_rec.quantity_ordered),	-- 5598066
541                     -- start changes for plan by request date
542                     decode(p_atp_rec.origination_type,
543                            6,  TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
544                            30, TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
545                                null), --plan by request date
546                     decode(p_atp_rec.origination_type,
547                            6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
548                                       MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
549                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
550                                       MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
551                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
552                                       MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
553                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
554                                       MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
555                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
556                                       MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
557                                           p_atp_rec.original_request_ship_date,
558                                       MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
559                                           p_atp_rec.original_request_ship_date,
560                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
561                            30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
562                                       MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
563                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
564                                       MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
565                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
566                                       MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
567                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
568                                       MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
569                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
570                                       MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
571                                           p_atp_rec.original_request_ship_date,
572                                       MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
573                                           p_atp_rec.original_request_ship_date,
574                                           TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY),
575                            TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY), --plan by request date
576                     decode(p_atp_rec.origination_type,
577                            6,  TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
578                            30, TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
579                                null), -- plan by request date
580                     decode(p_atp_rec.origination_type,
581                            6,  p_atp_rec.original_request_ship_date,
582                            30, p_atp_rec.original_request_ship_date,
583                                null), -- plan by request date
584                     -- end changes for plan by request date
585                     1, -- discrete demand
586                     p_atp_rec.demand_source_type,  --cmro
587                     p_atp_rec.origination_type,
588                     l_insert_item_id, -- for time_phased_atp
589                     p_plan_id,
590                     p_atp_rec.organization_id,
591                     l_insert_item_id, -- for time_phased_atp
592                     p_atp_rec.demand_source_line,
593                     p_atp_rec.instance_id,
594                     sysdate,
595                     FND_GLOBAL.USER_ID,
596                     sysdate,
597                     FND_GLOBAL.USER_ID,
598                     --p_atp_rec.demand_class,
599                     ---bug 2424357: we do not store the converted demand class
600                     -- always store the actual demand class.
601                     DECODE(MSC_ATP_PVT.G_HIERARCHY_PROFILE, 1, MSC_ATP_PVT.G_ATP_DEMAND_CLASS,
602                                                             2, p_atp_rec.demand_class),
603                     p_atp_rec.refresh_number,
604                     /* s_cto_rearch
605                     -- Modified by ngoel on 1/12/2001 for origination_type = 30
606                     decode(p_atp_rec.origination_type, 6, p_atp_rec.order_number,
607                         30, p_atp_rec.order_number,
608                         null),
609                     e_cto_rearch */
610                     --s_cto_rearch
611                     p_atp_rec.order_number,
612                     decode(p_atp_rec.origination_type, 6, 2, 30, 2, null),
613                     decode(p_atp_rec.origination_type, 6, 0, 30, 0, null),
614                     --decode(p_atp_rec.origination_type, 6, MSC_ATP_PVT.G_PARTNER_ID, 30, MSC_ATP_PVT.G_PARTNER_ID, null),
615                     --decode(p_atp_rec.origination_type, 6, MSC_ATP_PVT.G_PARTNER_SITE_ID, 30, MSC_ATP_PVT.G_PARTNER_SITE_ID, null),
616                     MSC_ATP_PVT.G_PARTNER_ID,
617                     MSC_ATP_PVT.G_PARTNER_SITE_ID,
618                     l_record_source, -- For plan order pegging
619                     -- 24x7
620                     0,
621                     -- bug 2795053-reopen (ssurendr) insert the demand_satisfied_date in msc_demands
622                     TRUNC(p_atp_rec.requested_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
623                     msc_demands_s.nextval,
624                     --s_cto_rearch
625                     p_atp_rec.parent_line_id,
626                     p_atp_rec.ato_model_line_id,
627                     p_atp_rec.top_model_line_id,
628                     p_atp_rec.ATO_Parent_Model_Line_Id,
629                     p_atp_rec.mand_comp_flag,
630                     p_atp_rec.wip_supply_type,
631                     --e_cto_rearch)
632                     p_atp_rec.original_item_id, -- time_phased_atp
633                     p_atp_rec.ship_method,      -- For ship_rec_cal
634                     p_atp_rec.session_id,
635                     p_atp_rec.delivery_lead_time, --bug3263368
636                     p_atp_rec.ship_set_name, --bug3263368
637                     p_atp_rec.arrival_set_name --bug3263368
638                 )
639             RETURNING DEMAND_ID INTO x_demand_id;
640 
641             -- time_phased_atp
642             IF l_time_phased_atp = 'Y' THEN
643                 IF PG_DEBUG in ('Y', 'C') THEN
644                         msc_sch_wb.atp_debug('Add_Mat_Demand: Time phased ATP = '|| l_time_phased_atp);
645                 END IF;
646                 MSC_ATP_PF.Add_PF_Bucketed_Demands(
647                         p_atp_rec,
648                         p_plan_id,
649                         x_demand_id,
650                         p_atp_rec.refresh_number,
651                         l_return_status
652                 );
653                 --5158454 Preserve the SO demand id for the first run.
654                 IF MSC_ATP_PVT.G_OPTIONAL_FW is null AND MSC_ATP_PVT.G_FORWARD_ATP = 'Y'
655                    AND p_atp_rec.origination_type in (6,30) THEN
656                    MSC_ATP_PVT.G_DEMAND_ID := x_demand_id;
657                 END IF;
658 
659                 IF PG_DEBUG in ('Y', 'C') THEN
660                         msc_sch_wb.atp_debug('Add_Mat_Demand: MSC_ATP_PVT.G_DEMAND_ID = '|| MSC_ATP_PVT.G_DEMAND_ID);
661                 END IF;
662                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
663                         IF PG_DEBUG in ('Y', 'C') THEN
664                                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Error occured in procedure Add_PF_Bucketed_Demands');
665                         END IF;
666                         RAISE FND_API.G_EXC_ERROR;
667                 END IF;
668 
669             -- Allocated ATP Based on Planning Details -- Agilent changes Begin
670             ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
671                (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
672                (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
673                (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
674 
675                 IF PG_DEBUG in ('Y', 'C') THEN
676                     msc_sch_wb.atp_debug('Add_Mat_Demand: before insert into msc_alloc_demands');
677                 END IF;
678 
679                 INSERT INTO MSC_ALLOC_DEMANDS(
680                         PLAN_ID,
681                         INVENTORY_ITEM_ID,
682                         ORGANIZATION_ID,
683                         SR_INSTANCE_ID,
684                         DEMAND_CLASS,
685                         DEMAND_SOURCE_TYPE, --cmro
686                         DEMAND_DATE,
687                         PARENT_DEMAND_ID,
688                         ALLOCATED_QUANTITY,
689                         ORIGINATION_TYPE,
690                         ORDER_NUMBER,
691                         SALES_ORDER_LINE_ID,
692                         CREATED_BY,
693                         CREATION_DATE,
694                         LAST_UPDATED_BY,
695                         LAST_UPDATE_DATE,
696                         REFRESH_NUMBER, -- For summary enhancement
697                         --bug3684383 added as in Insert_SD_Into_Details_Temp we need these columns populated
698 			-- to show partner name and location.
699                         CUSTOMER_ID,
700                         SHIP_TO_SITE_ID
701                        )
702                 VALUES (
703                         p_plan_id,
704                         l_insert_item_id, -- for time_phased_atp,
705                         p_atp_rec.organization_id,
706                         p_atp_rec.instance_id,
707                         p_atp_rec.demand_class,
708                         p_atp_rec.demand_source_type,--CMRO
709                         p_atp_rec.requested_ship_date, -- QUESTION arrival items ?
710                         x_demand_id,
711                         MSC_ATP_UTILS.Truncate_Demand(p_atp_rec.quantity_ordered),	-- 5598066
712                         p_atp_rec.origination_type,
713                         -- rajjain 04/25/2003 Bug 2770175 populate order_number column with the
714 
715                         --decode(p_atp_rec.origination_type, 1, x_demand_id, p_atp_rec.order_number),
716                         -- s_cto_rearch insert order number
717                         p_atp_rec.order_number,
718 
719                         p_atp_rec.demand_source_line,
720                         FND_GLOBAL.USER_ID,
721                         sysdate,
722                         FND_GLOBAL.USER_ID,
723                         sysdate,
724                         p_atp_rec.refresh_number,
725                         --bug3684383
726                         decode(p_atp_rec.origination_type, 6, MSC_ATP_PVT.G_PARTNER_ID,
727                                                            30, MSC_ATP_PVT.G_PARTNER_ID,
728                                                            null),
729                         decode(p_atp_rec.origination_type, 6, MSC_ATP_PVT.G_PARTNER_SITE_ID,
730                                                            30, MSC_ATP_PVT.G_PARTNER_SITE_ID,
731                                                            null));
732             END IF;
733 
734             -- Allocated ATP Based on Planning Details -- Agilent changes End
735 
736         END IF; 	--(p_atp_rec.origination_type NOT IN (6, 30) OR SQL%ROWCOUNT = 0)
737 
738 
739         -- Update summary records only in ODS - for summary enhancement
740         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1 THEN
741             IF PG_DEBUG in ('Y', 'C') THEN
742                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'add demands to summary tables');
743                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'First try to update');
744             END IF;
745 
746             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_atp_rec.instance_id,
747                                               p_plan_id,
748                                               p_atp_rec.organization_id,
749                                               p_atp_rec.inventory_item_id,
750                                               p_atp_rec.requested_ship_date,
751                                               null,
752                                               null,
753                                               null,
754                                               null,
755                                               null,
756                                               null,
757                                               2);
758 
759             update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
760             set sd_qty = sd_qty - p_atp_rec.quantity_ordered
761             where inventory_item_id =  p_atp_rec.inventory_item_id
762             and   plan_id = p_plan_id
763             and   sr_instance_id = p_atp_rec.instance_id
764             and   organization_id = p_atp_rec.organization_id
765             and   sd_date = trunc(p_atp_rec.requested_ship_date);
766 
767             IF PG_DEBUG in ('Y', 'C') THEN
768                 msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'After update  to summary table');
769             END IF;
770 
771             IF SQL%NOTFOUND THEN
772                 IF PG_DEBUG in ('Y', 'C') THEN
773                     msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Couldnt find a rec in summ table. Insert new rec');
774                 END IF;
775 
776                 MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
777                                                        p_atp_rec.instance_id,
778                                                        p_atp_rec.organization_id,
779                                                        p_atp_rec.inventory_item_id,
780                                                        p_atp_rec.requested_ship_date,
781                                                        -1 * p_atp_rec.quantity_ordered,
782                                                        '@@@');
783             END IF;
784         END IF;
785 
786     END IF;
787     my_sqlcode := SQLCODE;
788     IF PG_DEBUG in ('Y', 'C') THEN
789         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'x_demand_id '||x_demand_id);
790         msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'insert row: sqlcode =  '|| to_char(my_sqlcode));
791     END IF;
792 
793     -- Commit chenges only in PDS - for summary enhancement
794     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND p_plan_id = -1 THEN
795         -- in case of summmary mode we always commit after each change
796         IF PG_DEBUG in ('Y', 'C') THEN
797             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'Commit in summary mode');
798         END IF;
799         commit;
800     END IF;
801 
802     IF PG_DEBUG in ('Y', 'C') THEN
803         msc_sch_wb.atp_debug('***** End Add_Mat_Demand *****');
804     END IF;
805 
806 EXCEPTION
807     WHEN OTHERS THEN
808         my_sqlcode := SQLCODE;
809         IF PG_DEBUG in ('Y', 'C') THEN
810             msc_sch_wb.atp_debug('Add_Mat_Demand: ' || 'error in insert row: sqlcode =  '|| to_char(my_sqlcode));
811         END IF;
812 
813         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
814             FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME , 'Add_Mat_Demand');
815         END IF;
816         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
817 END Add_Mat_Demand;
818 
819 
820 PROCEDURE Add_Pegging(
821     p_pegging_rec          IN         mrp_atp_details_temp%ROWTYPE,
822     x_pegging_id           OUT        NoCopy NUMBER
823 )
824 IS
825 temp number;
826 BEGIN
827     IF PG_DEBUG in ('Y', 'C') THEN
828         msc_sch_wb.atp_debug('***** Begin Add_Pegging Procedure *****');
829         msc_sch_wb.atp_debug('Add_Pegging: ' || 'p_pegging_rec.ptf_date := ' || p_pegging_rec.ptf_date);
830         msc_sch_wb.atp_debug('Add_Pegging: ' || 'pegging component_identifier : '||
831             p_pegging_rec.component_identifier);
832         msc_sch_wb.atp_debug('Add_Pegging: ' || 'Parent peg id := ' || p_pegging_rec.parent_pegging_id);
833         msc_sch_wb.atp_debug('Add_Pegging: ' || 'scaling_type := ' || p_pegging_rec.scaling_type);
834         msc_sch_wb.atp_debug('Add_Pegging: ' || 'scale_multiple := ' || p_pegging_rec.scale_multiple);
835         msc_sch_wb.atp_debug('Add_Pegging: ' || 'scale_rounding_variance := ' || p_pegging_rec.scale_rounding_variance);
836         msc_sch_wb.atp_debug('Add_Pegging: ' || 'rounding_direction := ' || p_pegging_rec.rounding_direction);
837         msc_sch_wb.atp_debug('Add_Pegging: ' || 'component_yield_factor := ' || p_pegging_rec.component_yield_factor);
838 
839     END IF;
840 
841     INSERT into mrp_atp_details_temp
842            (session_id,
843             order_line_id,
844             pegging_id,
845             parent_pegging_id,
846             atp_level,
847             record_type,
848             organization_id,
849             organization_code,
850             identifier1,
851             identifier2,
852             identifier3,
853             inventory_item_id,
854             inventory_item_name,
855             resource_id,
856             resource_code,
857             department_id,
858             department_code,
859             supplier_id,
860             supplier_name,
861             supplier_site_id,
862             supplier_site_name,
863             scenario_id,
864             source_type,
865             supply_demand_source_type,
866             supply_demand_quantity,
867             supply_demand_type,
868             supply_demand_date,
869             end_pegging_id,
870             constraint_flag,
871             allocated_quantity, -- 1527660
872             number1,
873             char1,
874             component_identifier,
875             batchable_flag,
876             supplier_atp_date,
877             dest_inv_item_id,
878             summary_flag,
879             --- bug 2152184: For PF based ATP inventory_item_id field contains id for PF item
880             --- cto looks at pegging tree to place their demands. Since CTO expects to find
881             --  id for the requested item, we add the following column. CTO will now read from this column
882             request_item_id,
883             --- if req-date < ptf date then we update this column with PTF date
884             ptf_date
885             -- dsting
886             , creation_date
887             , created_by
888             , last_update_date
889             , last_updated_by
890             , last_update_login,
891             --diag_atp
892             FIXED_LEAD_TIME,
893             VARIABLE_LEAD_TIME,
894             PREPROCESSING_LEAD_TIME,
895             PROCESSING_LEAD_TIME,
896             POSTPROCESSING_LEAD_TIME,
897             INTRANSIT_LEAD_TIME,
898             ALLOCATION_RULE,
899             INFINITE_TIME_FENCE,
900             SUBSTITUTION_WINDOW,
901             REQUIRED_QUANTITY,
902             ROUNDING_CONTROL,
903             ATP_FLAG,
904             ATP_COMPONENT_FLAG,
905             REQUIRED_DATE,
906             OPERATION_SEQUENCE_ID,
907             SOURCING_RULE_NAME,
908             OFFSET,
909             EFFICIENCY,
910             REVERSE_CUM_YIELD,
911             BASIS_TYPE,
912             USAGE,
913             CONSTRAINT_TYPE,
914             CONSTRAINT_DATE,
915             UTILIZATION,
916             OWNING_DEPARTMENT,
917             ATP_RULE_NAME,
918             PLAN_NAME,
919             weight_capacity,
920             volume_capacity,
921             weight_uom,
922             volume_uom,
923             pegging_type,
924             ship_method,
925             --s_cto_rearch
926             model_sd_flag,
927             error_code,
928             base_model_id,
929             base_model_name,
930             nonatp_flag,
931             demand_class,
932             customer_id,
933             CUSTOMER_SITE_ID,
934             receiving_organization_id,
935             actual_supply_demand_date,
936             --e_cto_rearch
937             aggregate_time_fence_date, -- For time_phased_atp
938             shipping_cal_code,     -- Bug 3826234
939             receiving_cal_code,    -- Bug 3826234
940             intransit_cal_code,    -- Bug 3826234
941             manufacturing_cal_code, -- Bug 3826234
942             --4570421
943             scaling_type,
944             scale_multiple,
945             scale_rounding_variance,
946             rounding_direction,
947             component_yield_factor,
948             organization_type --4775920
949            )
950     VALUES
951            (p_pegging_rec.session_id,
952             p_pegging_rec.order_line_id,
953             msc_full_pegging_s.nextval,
954             p_pegging_rec.parent_pegging_id,
955             p_pegging_rec.atp_level,
956             3,
957             p_pegging_rec.organization_id,
958             p_pegging_rec.organization_code,
959             p_pegging_rec.identifier1,
960             p_pegging_rec.identifier2,
961             p_pegging_rec.identifier3,
962             p_pegging_rec.inventory_item_id,
963             p_pegging_rec.inventory_item_name,
964             p_pegging_rec.resource_id,
965             p_pegging_rec.resource_code,
966             p_pegging_rec.department_id,
967             p_pegging_rec.department_code,
968             p_pegging_rec.supplier_id,
969             p_pegging_rec.supplier_name,
970             p_pegging_rec.supplier_site_id,
971             p_pegging_rec.supplier_site_name,
972             p_pegging_rec.scenario_id,
973             p_pegging_rec.source_type,
974             p_pegging_rec.supply_demand_source_type,
975             p_pegging_rec.supply_demand_quantity,
976             p_pegging_rec.supply_demand_type,
977             p_pegging_rec.supply_demand_date,
978             NVL(MSC_ATP_PVT.G_DEMAND_PEGGING_ID, msc_full_pegging_s.currval),
979             p_pegging_rec.constraint_flag,
980             p_pegging_rec.allocated_quantity, -- 1527660
981             p_pegging_rec.number1,
982             p_pegging_rec.char1,
983             p_pegging_rec.component_identifier,
984             p_pegging_rec.batchable_flag,
985             p_pegging_rec.supplier_atp_date,
986             p_pegging_rec.dest_inv_item_id,
987             p_pegging_rec.summary_flag,
988             p_pegging_rec.request_item_id,
989             p_pegging_rec.ptf_date
990             -- dsting
991             , sysdate 			-- creation_date
992             , FND_GLOBAL.USER_ID	-- created_by
993             , sysdate				-- creation_date
994             , FND_GLOBAL.USER_ID	-- created_by
995             , FND_GLOBAL.USER_ID,	-- last_update_login
996             --diag_atp
997             p_pegging_rec.FIXED_LEAD_TIME,
998             p_pegging_rec.VARIABLE_LEAD_TIME,
999             p_pegging_rec.PREPROCESSING_LEAD_TIME,
1000             p_pegging_rec.PROCESSING_LEAD_TIME,
1001             p_pegging_rec.POSTPROCESSING_LEAD_TIME,
1002             p_pegging_rec.INTRANSIT_LEAD_TIME,
1003             p_pegging_rec.ALLOCATION_RULE,
1004             p_pegging_rec.INFINITE_TIME_FENCE,
1005             p_pegging_rec.SUBSTITUTION_WINDOW,
1006             p_pegging_rec.REQUIRED_QUANTITY,
1007             p_pegging_rec.ROUNDING_CONTROL,
1008             p_pegging_rec.ATP_FLAG,
1009             p_pegging_rec.ATP_COMPONENT_FLAG,
1010             -- p_pegging_rec.REQUIRED_DATE,
1011             -- Bug 2748730. Move the required_date to day end only when the pegging is for demand line
1012             -- This is applicable irrespective of whether the line is overridden or not
1013             DECODE(p_pegging_rec.supply_demand_type,
1014             1, TRUNC(p_pegging_rec.REQUIRED_DATE) + MSC_ATP_PVT.G_END_OF_DAY,
1015             p_pegging_rec.REQUIRED_DATE),
1016             p_pegging_rec.OPERATION_SEQUENCE_ID,
1017             p_pegging_rec.SOURCING_RULE_NAME,
1018             p_pegging_rec.OFFSET,
1019             p_pegging_rec.EFFICIENCY,
1020             p_pegging_rec.REVERSE_CUM_YIELD,
1021             p_pegging_rec.BASIS_TYPE,
1022             p_pegging_rec.USAGE,
1023             p_pegging_rec.CONSTRAINT_TYPE,
1024             p_pegging_rec.CONSTRAINT_DATE,
1025             p_pegging_rec.UTILIZATION,
1026             p_pegging_rec.OWNING_DEPARTMENT,
1027             p_pegging_rec.ATP_RULE_NAME,
1028             p_pegging_rec.PLAN_NAME,
1029             p_pegging_rec.weight_capacity,
1030             p_pegging_rec.volume_capacity,
1031             p_pegging_rec.weight_uom,
1032             p_pegging_rec.volume_uom,
1033             p_pegging_rec.pegging_type,
1034             p_pegging_rec.ship_method,
1035             --s_cto_rearch
1036             p_pegging_rec.model_sd_flag ,
1037             p_pegging_rec.error_code,
1038             p_pegging_rec.base_model_id,
1039             p_pegging_rec.base_model_name,
1040             p_pegging_rec.nonatp_flag,
1041             p_pegging_rec.demand_class,
1042             MSC_ATP_PVT.G_PARTNER_ID,
1043             MSC_ATP_PVT.G_PARTNER_SITE_ID,
1044             p_pegging_rec.receiving_organization_id,
1045             p_pegging_rec.actual_supply_demand_date,
1046             --e_cto_rearch
1047             p_pegging_rec.aggregate_time_fence_date, -- For time_phased_atp
1048             p_pegging_rec.shipping_cal_code,     -- Bug 3826234
1049             p_pegging_rec.receiving_cal_code,    -- Bug 3826234
1050             p_pegging_rec.intransit_cal_code,    -- Bug 3826234
1051             p_pegging_rec.manufacturing_cal_code, -- Bug 3826234
1052             --4570421
1053             p_pegging_rec.scaling_type,
1054             p_pegging_rec.scale_multiple,
1055             p_pegging_rec.scale_rounding_variance,
1056             p_pegging_rec.rounding_direction,
1057             p_pegging_rec.component_yield_factor,
1058             p_pegging_rec.organization_type --4775920
1059            )
1060     RETURNING pegging_id INTO x_pegging_id;
1061 
1062   IF PG_DEBUG in ('Y', 'C') THEN
1063      msc_sch_wb.atp_debug('Add_Pegging: ' || 'x_pegging_id = '||x_pegging_id);
1064      msc_sch_wb.atp_debug('***** End Add_Pegging Procedure *****');
1065   END IF;
1066 
1067 END Add_Pegging;
1068 
1069 
1070 PROCEDURE Add_Planned_Order(
1071         p_instance_id               IN NUMBER,
1072         p_plan_id                   IN NUMBER,
1073         p_inventory_item_id         IN NUMBER,
1074         p_organization_id           IN NUMBER,
1075         p_schedule_date             IN DATE,
1076         p_order_quantity            IN NUMBER,
1077         p_supplier_id               IN NUMBER,
1078         p_supplier_site_id          IN NUMBER,
1079         p_demand_class              IN VARCHAR2,
1080         -- rajjain 02/19/2003 Bug 2788302 Begin
1081         p_source_organization_id    IN NUMBER,
1082         p_source_sr_instance_id     IN NUMBER,
1083         p_process_seq_id            IN NUMBER,
1084         -- rajjain 02/19/2003 Bug 2788302 End
1085         p_refresh_number            IN VARCHAR2, -- for summary enhancement
1086         p_shipping_cal_code         IN VARCHAR2, -- For ship_rec_cal
1087         p_receiving_cal_code        IN VARCHAR2, -- For ship_rec_cal
1088         p_intransit_cal_code        IN VARCHAR2, -- For ship_rec_cal
1089         p_new_ship_date             IN DATE,     -- For ship_rec_cal
1090         p_new_dock_date             IN DATE,     -- For ship_rec_cal
1091         p_start_date                IN DATE,     -- Bug 3241766
1092         p_order_date                IN DATE,     -- Bug 3241766
1093         p_ship_method               IN VARCHAR2, -- For ship_rec_cal
1094         x_transaction_id            OUT NoCopy NUMBER,
1095         x_return_status             OUT NoCopy VARCHAR2,
1096         p_intransit_lead_time       IN      NUMBER, --4127630
1097         p_request_item_id           IN NUMBER := NULL, -- for time_phased_atp
1098         p_atf_date                  IN DATE := NULL -- for time_phased_atp
1099 )
1100 IS
1101     temp_sd_qty number;
1102     l_record_source        number := 2; -- for plan order pegging rmehra
1103 
1104     -- time_phased_atp
1105     l_time_phased_atp      varchar2(1) := 'N';
1106     l_insert_item_id       number;
1107     l_return_status        varchar2(1);
1108 
1109     l_supply_rec  MSC_ATP_DB_UTILS.Supply_Rec_Typ;
1110 
1111 BEGIN
1112     IF PG_DEBUG in ('Y', 'C') THEN
1113         msc_sch_wb.atp_debug('***** Begin Add_Planned_Order Procedure *****');
1114         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_order_quantity := ' || p_order_quantity);
1115         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_inventory_item_id := ' || p_inventory_item_id);
1116         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_schedule_date := ' || p_schedule_date);
1117         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_organization_id := ' || p_organization_id);
1118         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_schedule_date := ' || p_schedule_date);
1119         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_supplier_id := ' || p_supplier_id);
1120         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_supplier_site_id := ' || p_supplier_site_id);
1121         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_refresh_number := ' || p_refresh_number);
1122         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_request_item_id := ' || p_request_item_id);
1123         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_atf_date := ' || p_atf_date);
1124         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_new_dock_date := ' || p_new_dock_date);
1125         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_new_ship_date := ' || p_new_ship_date);
1126         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_start_date := ' || p_start_date);
1127         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_order_date := ' || p_order_date);
1128         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'p_intransit_lead_time := ' || p_intransit_lead_time);
1129     END IF;
1130 
1131     x_return_status := FND_API.G_RET_STS_SUCCESS;
1132 
1133     --bug 3766179: Code to create planned order is moved to add_supplies
1134     l_supply_rec.instance_id := p_instance_id;
1135     l_supply_rec.plan_id := p_plan_id;
1136     l_supply_rec.inventory_item_id := p_inventory_item_id;
1137     l_supply_rec.organization_id := p_organization_id;
1138     l_supply_rec.schedule_date := p_schedule_date;
1139     l_supply_rec.order_quantity := p_order_quantity;
1140     l_supply_rec.supplier_id := p_supplier_id;
1141     l_supply_rec.supplier_site_id := p_supplier_site_id;
1142     l_supply_rec.demand_class := p_demand_class;
1143     l_supply_rec.source_organization_id := p_source_organization_id;
1144     l_supply_rec.source_sr_instance_id := p_source_sr_instance_id;
1145     l_supply_rec.process_seq_id := p_process_seq_id;
1146     l_supply_rec.refresh_number := p_refresh_number;
1147     l_supply_rec.shipping_cal_code  := p_shipping_cal_code;
1148     l_supply_rec.receiving_cal_code := p_receiving_cal_code;
1149     l_supply_rec.intransit_cal_code := p_intransit_cal_code;
1150     l_supply_rec.new_ship_date := p_new_ship_date;
1151     l_supply_rec.new_dock_date := p_new_dock_date;
1152     l_supply_rec.start_date := p_start_date;
1153     l_supply_rec.order_date := p_order_date;
1154     l_supply_rec.ship_method := p_ship_method;
1155     l_supply_rec.request_item_id := p_request_item_id;
1156     l_supply_rec.atf_date        := p_atf_date;
1157 
1158     l_supply_rec.firm_planned_type := 2;
1159     l_supply_rec.disposition_status_type := 1;
1160     l_supply_rec.record_source := 2;
1161     l_supply_rec.supply_type := 5; --- planned order
1162     l_supply_rec.intransit_lead_time := p_intransit_lead_time; --4127630
1163 
1164     MSC_ATP_DB_UTILS.ADD_SUPPLIES(l_supply_rec);
1165 
1166     x_transaction_id := l_supply_rec.transaction_id;
1167     x_return_status := l_supply_rec.return_status;
1168 
1169     IF PG_DEBUG in ('Y', 'C') THEN
1170         msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'x_transaction_id = '||x_transaction_id);
1171         msc_sch_wb.atp_debug('***** End Add_Planned_Order Procedure *****');
1172     END IF;
1173 
1174 END Add_Planned_Order;
1175 
1176 
1177 PROCEDURE Add_Resource_Demand(
1178         p_instance_id           IN  NUMBER,
1179         p_plan_id               IN  NUMBER,
1180         p_supply_id             IN  NUMBER,
1181         p_organization_id       IN  NUMBER,
1182         p_resource_id           IN  NUMBER,
1183         p_department_id         IN  NUMBER,
1184         -- Bug 3348095
1185         -- Now both start and end dates will be stored for
1186         -- ATP created resource requirements.
1187         p_start_date            IN  DATE,
1188         p_end_date              IN  DATE,
1189         -- End Bug 3348095
1190         p_resource_hours        IN  NUMBER,
1191         p_unadj_resource_hours  IN  NUMBER, --5093604
1192         p_touch_time            IN  NUMBER, --5093604
1193         p_std_op_code           IN  VARCHAR2,
1194         p_resource_cap_hrs      IN  NUMBER,
1195         p_item_id               IN  NUMBER,  -- Need to store assembly_item_id CTO ODR
1196         p_basis_type            IN  NUMBER,   -- Need to store basis_type CTO ODR
1197         p_op_seq_num            IN  NUMBER,   -- Need to store op_seq_num CTO ODR
1198         p_refresh_number        IN  VARCHAR2, -- for summary enhancement
1199         x_transaction_id        OUT NoCopy NUMBER,
1200         x_return_status         OUT NoCopy VARCHAR2)
1201 IS
1202     temp_sd_qty number;
1203 BEGIN
1204     IF PG_DEBUG in ('Y', 'C') THEN
1205         msc_sch_wb.atp_debug('***** Begin Add_Resource_Demand Procedure *****');
1206         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_resource_id := ' || p_resource_id);
1207         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_department_id := ' || p_department_id);
1208         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_resource_hours := ' || p_resource_hours);
1209         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_resource_cap_hrs := ' || p_resource_cap_hrs);
1210         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_item_id := ' || p_item_id);
1211         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_start_date := ' || p_start_date);
1212         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_end_date := ' || p_end_date);
1213         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_refresh_number := ' || p_refresh_number);
1214         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_basis_type := ' || p_basis_type);
1215         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_op_seq_num := ' || p_op_seq_num);
1216         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_unadj_resource_hours := ' || p_unadj_resource_hours); --5093604
1217         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'p_touch_time := ' || p_touch_time);                               --5093604
1218     END IF;
1219 
1220     x_return_status := FND_API.G_RET_STS_SUCCESS;
1221 
1222     -- Insert demand into msc_resource_requirements
1223     INSERT into msc_resource_requirements
1224            (plan_id,
1225             supply_id,
1226             transaction_id,
1227             organization_id,
1228             sr_instance_id,
1229             resource_seq_num,
1230             resource_id,
1231             department_id,
1232             assembly_item_id, --  This field was not getting populated before. CTO ODR
1233             basis_type, --  This field was not getting populated before. CTO ODR
1234             operation_seq_num, --  This field was not getting populated before. CTO ODR
1235             start_date,
1236             -- Bug 3348095 Store End Date as well.
1237             end_date,
1238             -- End Bug 3348095
1239             resource_hours,
1240             unadjusted_resource_hours, --5093604
1241             touch_time,                --5093604
1242             load_rate,
1243             assigned_units,
1244             supply_type, -- 1510686
1245             std_op_code, --resource batching
1246             -- parent_id, Bug 3327819 parent_id will be defaulted to NULL.
1247             last_update_date,
1248             last_updated_by,
1249             creation_date,
1250             created_by,
1251             record_source,  -- This field was not getting populated before. added with summary enhancement
1252             refresh_number) -- for summary enhancement
1253     VALUES (p_plan_id,
1254             p_supply_id,
1255             msc_resource_requirements_s.nextval,
1256             p_organization_id,
1257             p_instance_id,
1258             1,
1259             p_resource_id,
1260             p_department_id,
1261             p_item_id,  -- This field was not getting populated before. CTO ODR
1262             p_basis_type,  -- This field was not getting populated before. CTO ODR
1263             p_op_seq_num,  -- This field was not getting populated before. CTO ODR
1264             -- Bug 3348095
1265             -- Now both start and end dates will be stored for
1266             -- ATP created resource requirements.
1267             TRUNC(p_start_date) + MSC_ATP_PVT.G_END_OF_DAY,
1268             TRUNC(p_end_date) + MSC_ATP_PVT.G_END_OF_DAY, -- For bug 2259824
1269             -- End Bug 3348095
1270             p_resource_hours,
1271             p_unadj_resource_hours, --5093604
1272             p_touch_time,                --5093604
1273             decode(p_resource_id,-1,p_resource_hours,to_number(null)),
1274             0,
1275             5,
1276             p_std_op_code,
1277             --MSC_ATP_PVT.G_OPTIMIZED_PLAN, Bug 3327819 Default parent_id to NULL.
1278             sysdate,
1279             FND_GLOBAL.USER_ID,
1280             sysdate,
1281             FND_GLOBAL.USER_ID,
1282             2,               -- This field was not getting populated before. added with summary enhancement
1283             p_refresh_number -- for summary enhancement
1284            )
1285     RETURNING transaction_id INTO x_transaction_id;
1286 
1287     -- Code to make summary updates and commit removed for summary enhancement
1288     /** code commented for time being. Will be removed after code review
1289     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
1290         IF PG_DEBUG in ('Y', 'C') THEN
1291             msc_sch_wb.atp_debug('Add_Resource_Demand: ' || ' In summary mode update resource req');
1292             msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'First update');
1293         END IF;
1294         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
1295                                           p_plan_id,
1296                                           p_organization_id,
1297                                           null,
1298                                           p_start_date,
1299                                           p_resource_id,
1300                                           p_department_id,
1301                                           null,
1302                                           null,
1303                                           null,
1304                                           null,
1305                                           3);
1306 
1307         update /*+ INDEX(msc_atp_summary_res MSC_ATP_SUMMARY_RES_U1) *//* msc_atp_summary_res
1308         set sd_qty = sd_qty - p_resource_cap_hrs
1309         where plan_id = p_plan_id
1310         and   sr_instance_id = p_instance_id
1311         and   organization_id = p_organization_id
1312         and   resource_id = p_resource_id
1313         and   department_id = p_department_id
1314         and   sd_date = trunc(p_start_date);
1315         IF PG_DEBUG in ('Y', 'C') THEN
1316            msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'after update qty := ' || temp_sd_qty);
1317         END IF;
1318 
1319 
1320         IF SQL%NOTFOUND THEN
1321            IF PG_DEBUG in ('Y', 'C') THEN
1322               msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'Couldnt update. Inset the row');
1323            END IF;
1324            BEGIN
1325               INSERT /*+ INDEX(msc_atp_summary_RES MSC_ATP_SUMMARY_SD_RES) *//* INTO MSC_ATP_SUMMARY_RES
1326                          (plan_id,
1327                           organization_id,
1328                           sr_instance_id,
1329                           department_id,
1330                           resource_id,
1331                           sd_date,
1332                           sd_qty,
1333                           LAST_UPDATE_DATE,
1334                           LAST_UPDATED_BY,
1335                           CREATION_DATE,
1336                           CREATED_BY
1337                           )
1338                           VALUES
1339                           (p_plan_id,
1340                            p_organization_id,
1341                            p_instance_id,
1342                            p_department_id,
1343                            p_resource_id,
1344                            trunc(p_start_date),
1345                            -1 * p_resource_cap_hrs,
1346                            sysdate,
1347                            FND_GLOBAL.USER_ID,
1348                            sysdate,
1349                            FND_GLOBAL.USER_ID);
1350            EXCEPTION
1351                WHEN DUP_VAL_ON_INDEX THEN
1352 
1353                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
1354    	                                              p_plan_id,
1355                                           	      p_organization_id,
1356                                           	      null,
1357                                           	      p_start_date,
1358                                           	      p_resource_id,
1359                                           	      p_department_id,
1360                                           	      null,
1361                                           	      null,
1362                                           	      null,
1363                                           	      null,
1364                                           	      3);
1365 
1366                     update /*+ INDEX(msc_atp_summary_RES MSC_ATP_SUMMARY_RES_U1) *//* msc_atp_summary_res
1367                     set sd_qty = sd_qty - p_resource_cap_hrs
1368                     where plan_id = p_plan_id
1369                     and   sr_instance_id = p_instance_id
1370                     and   organization_id = p_organization_id
1371                     and   resource_id = p_resource_id
1372                     and   department_id = p_department_id
1373                     and   sd_date = trunc(p_start_date);
1374            END;
1375         END IF; --- if sql%notfound
1376         IF PG_DEBUG in ('Y', 'C') THEN
1377            msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'Summary mode commit, in resource_req');
1378         END IF;
1379         commit;
1380     END IF; --- if MSC_ATP_PVT.SUMMARY_FLAG = 'Y'
1381     ***/
1382 
1383     IF PG_DEBUG in ('Y', 'C') THEN
1384         msc_sch_wb.atp_debug('Add_Resource_Demand: ' || 'x_transaction_id = '||x_transaction_id);
1385         msc_sch_wb.atp_debug('***** End Add_Resource_Demand Procedure *****');
1386     END IF;
1387 
1388 END Add_Resource_Demand;
1389 
1390 
1391 PROCEDURE Delete_Pegging(
1392    p_pegging_id           IN        number
1393 )
1394 IS
1395 
1396 BEGIN
1397       IF PG_DEBUG in ('Y', 'C') THEN
1398          msc_sch_wb.atp_debug('Begin delete_pegging,p_pegging_id='||p_pegging_id);
1399       END IF;
1400 
1401       DELETE FROM mrp_atp_details_temp
1402       WHERE pegging_id = p_pegging_id
1403       AND   session_id = MSC_ATP_PVT.G_SESSION_ID;
1404 
1405       IF PG_DEBUG in ('Y', 'C') THEN
1406          msc_sch_wb.atp_debug('After delete_pegging,p_pegging_id='||p_pegging_id);
1407       END IF;
1408 
1409 EXCEPTION
1410       WHEN OTHERS THEN
1411           null;
1412 END Delete_Pegging;
1413 
1414 
1415 PROCEDURE Delete_Row(p_identifier         IN   NUMBER,
1416                      p_config_line_id     IN   NUMBER,  -- CTO Re-arch, ATP Simplified Pegging
1417                      p_plan_id            IN   NUMBER,
1418                      p_instance_id        IN   NUMBER,
1419                      p_refresh_number     IN   NUMBER,
1420                                        -- Bug 2831298 Ensure that the refresh_number is updated
1421                      p_order_number       IN   NUMBER, -- Bug 2840734 : krajan :
1422                      p_time_phased_atp    IN   VARCHAR2,                       -- For time_phased_atp
1423                      p_ato_model_line_id  IN      number,
1424                      p_demand_source_type IN    Number,  --cmro
1425                      p_source_organization_Id IN  NUMBER,  --Bug 7118988
1426                      x_demand_id          OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1427                      x_inv_item_id        OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1428                      x_copy_demand_id     OUT  NoCopy MRP_ATP_PUB.Number_Arr,  -- For summary enhancement
1429                      -- CTO ODR and Simplified Pegging
1430                      x_atp_peg_items      OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1431                      x_atp_peg_demands    OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1432                      x_atp_peg_supplies   OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1433                      x_atp_peg_res_reqs   OUT  NoCopy MRP_ATP_PUB.Number_Arr,
1434                      x_demand_instance_id OUT NoCopy MRP_ATP_PUB.Number_Arr, --Bug 3629191
1435                      x_supply_instance_id OUT NoCopy MRP_ATP_PUB.Number_Arr, --Bug 3629191
1436                      x_res_instance_id    OUT NoCopy MRP_ATP_PUB.Number_Arr, --Bug 3629191
1437                      x_ods_cto_demand_ids   OUT NoCopy MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
1438                      x_ods_cto_inv_item_ids OUT NoCopy MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
1439                      x_ods_atp_refresh_no      OUT NoCopy MRP_ATP_PUB.Number_Arr,
1440                      x_ods_cto_atp_refresh_no  OUT NoCopy MRP_ATP_PUB.Number_Arr
1441                      -- End CTO ODR and Simplified Pegging
1442 )
1443 
1444 IS
1445     l_del_rows                  NUMBER;
1446     -- added for bug 2738280
1447     i                           PLS_INTEGER;
1448     l_old_demand_date           MRP_ATP_PUB.date_arr    := MRP_ATP_PUB.date_arr();
1449     l_old_demand_quantity       MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1450     l_organization_id           MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1451     -- added for summary enhancement
1452     l_current_refresh_number    NUMBER;
1453     i_item_id_tab               MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1454     l_refresh_number_tab        MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1455     l_demand_class_tab          MRP_ATP_PUB.char30_arr  := MRP_ATP_PUB.char30_arr();
1456     l_qty_tab                   MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1457     i_ins_item_id_tab           MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1458     l_ins_org_id_tab            MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1459     l_ins_refresh_number_tab    MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1460     l_ins_demand_class_tab      MRP_ATP_PUB.char30_arr  := MRP_ATP_PUB.char30_arr();
1461     l_ins_qty_tab               MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1462     l_ins_date_tab              MRP_ATP_PUB.date_arr    := MRP_ATP_PUB.date_arr();
1463     l_copy_demand_id            MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1464     l_identifier_tab            MRP_ATP_PUB.number_arr  := MRP_ATP_PUB.number_arr();
1465 
1466     -- CTO ODR and Simplified Pegging
1467     l_return_status         VARCHAR2(1);
1468 BEGIN
1469     IF PG_DEBUG in ('Y', 'C') THEN
1470         msc_sch_wb.atp_debug('**********Begin Delete_Row Procedure************');
1471         msc_sch_wb.atp_debug('Delete_Row: ' || 'p_plan_id := ' || p_plan_id);
1472         msc_sch_wb.atp_debug(' identifier := ' || p_identifier);
1473         msc_sch_wb.atp_debug(' p_config_line_id := ' || p_config_line_id);
1474         msc_sch_wb.atp_debug('Ato model line id := ' || p_ato_model_line_id);
1475         msc_sch_wb.atp_debug('p_source_organization id := ' || p_source_organization_Id);
1476     END IF;
1477     IF p_plan_id = -1 THEN
1478 
1479         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND G_ORIG_INV_CTP = 5 THEN
1480                 -- Condition for INV_CTP added for bug 3295831 because summary
1481                 -- is not supported in PDS-ODS switch.
1482             MSC_ATP_DB_UTILS.DELETE_SUMMARY_ROW(p_identifier, p_plan_id, p_instance_id,p_demand_source_type);--cmro
1483         END IF;
1484 
1485         --3720018, commented the deletion of Sales orders.
1486         -- Bug 1723284, implicit conversion of datatype was forcing it
1487         -- not to use index and do FTS.
1488         /*DELETE msc_sales_orders
1489         WHERE  sr_instance_id = p_instance_id
1490         AND    demand_source_line = to_char(p_identifier)
1491         AND     decode(demand_source_type,100,demand_source_type,-1)
1492                             =decode(p_demand_source_type,
1493                                                 100,
1494                                                 p_demand_source_type,
1495                                                 -1); --CMRO*/
1496 
1497         --3720018, msc_sales_order is updated in case of ODS rescheduling
1498         Update msc_sales_orders
1499               set old_primary_uom_quantity = primary_uom_quantity,
1500               old_reservation_quantity = reservation_quantity,
1501               reservation_quantity = 0,
1502               Primary_uom_quantity = 0
1503         WHERE  sr_instance_id = p_instance_id
1504         AND    demand_source_line = to_char(p_identifier)
1505         AND    organization_id = p_source_organization_Id			---7118988
1506         AND     decode(demand_source_type,100,demand_source_type,-1)
1507                             =decode(p_demand_source_type,
1508                                                 100,
1509                                                 p_demand_source_type,
1510                                                 -1)
1511         returning demand_id, inventory_item_id, atp_refresh_number
1512         bulk collect into x_demand_id, x_inv_item_id, x_ods_atp_refresh_no;
1513 
1514         Update msc_sales_orders
1515               set atp_refresh_number = 10000000000 --3720018
1516         WHERE  sr_instance_id = p_instance_id
1517         AND    demand_source_line = to_char(p_identifier)
1518         AND    organization_id = p_source_organization_Id			---7118988
1519         AND     decode(demand_source_type,100,demand_source_type,-1)
1520                             =decode(p_demand_source_type,
1521                                                 100,
1522                                                 p_demand_source_type,
1523                                                 -1);
1524 
1525 
1526         IF PG_DEBUG in ('Y', 'C') THEN
1527              msc_sch_wb.atp_debug('Number of SO lines updated := ' || SQL%ROWCOUNT);
1528              --Bug 4336692, In case above queries donot return anything,
1529              -- following debug statement will cause an error.
1530              --msc_sch_wb.atp_debug( 'x_ods_atp_refresh_no' || x_ods_atp_refresh_no(1));
1531               msc_sch_wb.atp_debug('x_demand_id(1) ' ||x_demand_id.count);
1532         END IF;
1533 
1534         --s_cto_rearch: Now delete rows fpr CTO components
1535         IF p_ato_model_line_id is not null THEN
1536 
1537             --3720018, commented the deletion of Sales orders.
1538             /*DELETE msc_sales_orders
1539             where  sr_instance_id = p_instance_id
1540             and    ato_line_id = p_identifier
1541             AND     decode(demand_source_type,100,demand_source_type,-1)
1542                             =decode(p_demand_source_type,
1543                                                 100,
1544                                                 p_demand_source_type,
1545                                                 -1); --CMRO*/
1546 
1547             --3720018, msc_sales_order is updated in case of ODS rescheduling
1548             Update msc_sales_orders
1549                  set old_primary_uom_quantity = primary_uom_quantity,
1550                  --old_refresh_number = refresh_number,
1551                  Primary_uom_quantity = 0,
1552                  old_reservation_quantity = reservation_quantity,
1553                  reservation_quantity = 0
1554             WHERE  sr_instance_id = p_instance_id
1555             AND    ato_line_id    = p_identifier
1556             AND    demand_source_line <> to_char(p_identifier)
1557             AND    organization_id = p_source_organization_Id			---7118988
1558             AND    decode(demand_source_type,100,demand_source_type,-1)
1559                             =decode(p_demand_source_type,
1560                                                 100,
1561                                                 p_demand_source_type,
1562                                                 -1)
1563             returning demand_id, inventory_item_id, atp_refresh_number
1564             bulk collect into x_ods_cto_demand_ids, x_ods_cto_inv_item_ids, x_ods_cto_atp_refresh_no ;
1565 
1566 
1567             IF PG_DEBUG in ('Y', 'C') THEN
1568                msc_sch_wb.atp_debug('Number of component lines updated := ' || SQL%ROWCOUNT);
1569             END IF;
1570 
1571             Update msc_sales_orders
1572                  set atp_refresh_number = 10000000000 --3720018
1573             WHERE  sr_instance_id = p_instance_id
1574             AND    ato_line_id    = p_identifier
1575             AND    demand_source_line <> to_char(p_identifier)
1576             AND    organization_id = p_source_organization_Id			---7118988
1577             AND    decode(demand_source_type,100,demand_source_type,-1)
1578                             =decode(p_demand_source_type,
1579                                                 100,
1580                                                 p_demand_source_type,
1581                                                 -1);
1582 
1583         END IF;
1584     ELSE
1585         IF PG_DEBUG in ('Y', 'C') THEN
1586             msc_sch_wb.atp_debug('Delete_Row: ' || 'Deleting msc_demands with identifier = '||
1587                 p_identifier ||' : plan id = '||p_plan_id);
1588         END IF;
1589 
1590         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
1591             IF PG_DEBUG in ('Y', 'C') THEN
1592                 msc_sch_wb.atp_debug('Delete_Row: ' || 'Obtain the latest refresh number.');
1593             END IF;
1594 
1595             SELECT latest_refresh_number
1596             INTO   l_current_refresh_number
1597             FROM   MSC_PLANS
1598             WHERE  plan_id = p_plan_id;
1599 
1600             IF PG_DEBUG in ('Y', 'C') THEN
1601                 msc_sch_wb.atp_debug('Delete_Row: ' || 'l_current_refresh_number := ' || l_current_refresh_number);
1602             END IF;
1603         END IF;
1604 
1605         IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'N') OR
1606            (p_time_phased_atp = 'Y') OR
1607            ((MSC_ATP_PVT.G_INV_CTP = 4) AND
1608             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
1609             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
1610             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
1611             -- If condition added for summary enhancement
1612             -- execute the old code non-summary cases and demand priority allocation cases
1613             IF PG_DEBUG in ('Y', 'C') THEN
1614                 msc_sch_wb.atp_debug('Delete_Row: ' || 'Summary , PF or allocated.');
1615             END IF;
1616 
1617             -- Bug # 1868383, do not delete old demand records, rather just update qty = 0
1618             -- Bug 2738280. Club the 2 update SQL's into one.
1619             -- Collect all the entities required for updating summary tables here.
1620             UPDATE  msc_demands
1621             SET     old_demand_quantity = using_requirement_quantity,
1622                     -- bug 2863322 : change the column used to store date
1623                     old_using_assembly_demand_date = using_assembly_demand_date,
1624                     applied = 2,
1625                     status = 0,
1626                     using_requirement_quantity = 0,
1627                     --24x7
1628                     atp_synchronization_flag = 0,
1629                     old_refresh_number = refresh_number, -- For summary enhancement
1630                     refresh_number = p_refresh_number,
1631                     -- Bug 2831298 Ensure that the refresh_number is updated.
1632                     order_number = p_order_number -- 2840734 : krajan : populate order number
1633             WHERE	sr_instance_id = p_instance_id
1634             AND     plan_id = p_plan_id
1635             AND     using_requirement_quantity > 0
1636             -- CTO ODR and Simplified Pegging
1637             AND     decode(demand_source_type,100,demand_source_type,-1)
1638                             =decode(p_demand_source_type,
1639                                                 100,
1640                                                 p_demand_source_type,
1641                                                 -1) --CMRO
1642 
1643             AND     sales_order_line_id in (p_identifier, p_config_line_id)
1644             AND     origination_type in (6,30,-100)
1645             AND     organization_id = p_source_organization_Id			---7118988
1646 
1647             -- rajjain 03/14/2003 Bug 2849749 Begin
1648             /* Comment out code refering msc_bom_temp
1649             AND     sales_order_line_id IN (
1650                     SELECT  component_identifier
1651                     FROM    msc_bom_temp
1652                     START WITH assembly_identifier = p_identifier
1653                     AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1654                     AND     assembly_identifier <> component_identifier
1655                     CONNECT BY PRIOR component_identifier = assembly_identifier
1656                     AND     assembly_identifier <> component_identifier
1657                     AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1658                     UNION ALL
1659                     SELECT p_identifier FROM dual
1660             -- rajjain 03/14/2003 Bug 2849749 End
1661                 )
1662                Comment out code refering msc_bom_temp
1663              */
1664             -- End CTO ODR and Simplified Pegging
1665             -- bug 2863322 : change the column used to store date
1666             returning demand_id, inventory_item_id, sales_order_line_id
1667             bulk collect into x_demand_id, x_inv_item_id, l_identifier_tab;
1668             -- Returning clause changed for summary enhancement
1669 
1670             l_del_rows := SQL%ROWCOUNT;
1671             IF PG_DEBUG in ('Y', 'C') THEN
1672                 msc_sch_wb.atp_debug('Delete_Row: ' || 'No. of demands updated = '|| l_del_rows);
1673                 FOR i IN 1..x_demand_id.COUNT LOOP
1674                     msc_sch_wb.atp_debug('Delete_Row i: ' || i || '; demand_id: '|| x_demand_id(i) || '; inv_item_id: '|| x_inv_item_id(i));
1675                 END LOOP;
1676             END IF;
1677 
1678         ELSE
1679 
1680             IF PG_DEBUG in ('Y', 'C') THEN
1681                 msc_sch_wb.atp_debug('Delete_Row: ' || 'Unallocated summary');
1682             END IF;
1683 
1684             /*SELECT  demand_id,
1685                     inventory_item_id,
1686                     organization_id,
1687                     refresh_number,
1688                     using_requirement_quantity,
1689                     using_assembly_demand_date
1690             BULK COLLECT INTO
1691                     x_demand_id,
1692                     x_inv_item_id,
1693                     l_organization_id,
1694                     l_refresh_number_tab,
1695                     l_qty_tab,
1696                     l_old_demand_date
1697             FROM    MSC_DEMANDS
1698             WHERE   sr_instance_id = p_instance_id
1699             AND     plan_id = p_plan_id
1700             AND     using_requirement_quantity > 0
1701             -- CTO ODR and Simplified Pegging
1702             AND     nvl(decode(demand_source_type,100,demand_source_type,-1),-1)
1703                     =nvl(decode(p_demand_source_type,
1704                                                 100,
1705                                                 p_demand_source_type,
1706                                                 -1),-1) --CMRO
1707             AND     sales_order_line_id in (p_identifier, p_config_line_id)
1708             AND     origination_type in (6,30)
1709              Comment out code refering msc_bom_temp
1710             AND     sales_order_line_id IN (
1711                     SELECT  component_identifier
1712                     FROM    msc_bom_temp
1713                     START WITH assembly_identifier = p_identifier
1714                     AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1715                     AND     assembly_identifier <> component_identifier
1716                     CONNECT BY PRIOR component_identifier = assembly_identifier
1717                     AND     assembly_identifier <> component_identifier
1718                     AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1719                     UNION ALL
1720                     SELECT p_identifier FROM dual
1721             )
1722             Comment out code refering msc_bom_temp
1723 
1724             -- End CTO ODR and Simplified Pegging
1725             ;*/ --commented out as now combining this with update and returning clause
1726 
1727             --IF x_demand_id IS NOT NULL AND x_demand_id.COUNT > 0 THEN
1728               --  IF PG_DEBUG in ('Y', 'C') THEN
1729                 --    msc_sch_wb.atp_debug('Delete_Row: ' || 'x_demand_id.COUNT:' || x_demand_id.COUNT);
1730                 --END IF;
1731 
1732                 --FORALL i in 1..x_demand_id.COUNT  --cmro
1733                UPDATE  msc_demands
1734                SET     old_demand_quantity = using_requirement_quantity,
1735                         old_using_assembly_demand_date = using_assembly_demand_date,
1736                         applied = 2,
1737                         status = 0,
1738                         using_requirement_quantity = 0,
1739                         atp_synchronization_flag = 0,
1740                         old_refresh_number = refresh_number, -- For summary enhancement
1741                         refresh_number = p_refresh_number,
1742                         order_number = p_order_number
1743                WHERE   sr_instance_id = p_instance_id
1744                AND     plan_id = p_plan_id
1745                AND     using_requirement_quantity > 0
1746                AND     decode(demand_source_type,100,demand_source_type,-1)
1747                        =decode(p_demand_source_type,
1748                                                 100,
1749                                                 p_demand_source_type,
1750                                                 -1) --CMRO
1751                AND     sales_order_line_id in (p_identifier, p_config_line_id)
1752                AND     origination_type in (6,30,-100)
1753                AND    organization_id = p_source_organization_Id			---7118988
1754                RETURNING demand_id, inventory_item_id,organization_id,old_refresh_number,old_demand_quantity,old_using_assembly_demand_date
1755                BULK COLLECT INTO x_demand_id,x_inv_item_id,l_organization_id,l_refresh_number_tab,l_qty_tab,l_old_demand_date;--cmro
1756 
1757               IF x_demand_id IS NOT NULL AND x_demand_id.COUNT > 0 THEN
1758                 IF PG_DEBUG in ('Y', 'C') THEN
1759                     msc_sch_wb.atp_debug('Delete_Row: ' || 'x_demand_id.COUNT:' || x_demand_id.COUNT);
1760                 END IF;
1761 
1762                 FOR i IN 1..x_demand_id.COUNT LOOP
1763                     IF l_current_refresh_number IS NULL -- summary has not run
1764                         OR l_refresh_number_tab(i) > l_current_refresh_number THEN -- SO being unscheduled has not been summarized
1765 
1766                             IF PG_DEBUG in ('Y', 'C') THEN
1767                                 msc_sch_wb.atp_debug('Delete_Row: ' || 'Copy SO will be created for demand id ' || x_demand_id(i));
1768                             END IF;
1769 
1770                             i_ins_item_id_tab.Extend;
1771                             l_ins_org_id_tab.Extend;
1772                             l_ins_refresh_number_tab.Extend;
1773                             l_ins_qty_tab.Extend;
1774                             l_ins_date_tab.Extend;
1775                             l_copy_demand_id.Extend;
1776 
1777                             i_ins_item_id_tab(i_ins_item_id_tab.COUNT) := x_inv_item_id(i);
1778                             l_ins_org_id_tab(l_ins_org_id_tab.COUNT) := l_organization_id(i);
1779                             l_ins_refresh_number_tab(l_ins_refresh_number_tab.COUNT) := l_refresh_number_tab(i);
1780                             l_ins_qty_tab(l_ins_qty_tab.COUNT) := l_qty_tab(i);
1781                             l_ins_date_tab(l_ins_date_tab.COUNT) := l_old_demand_date(i);
1782 
1783                             SELECT  msc_demands_s.nextval
1784                             INTO    l_copy_demand_id(l_copy_demand_id.COUNT)
1785                             FROM    dual;
1786                     END IF;
1787                 END LOOP;
1788 
1789                 IF i_ins_item_id_tab IS NOT NULL and i_ins_item_id_tab.COUNT > 0 THEN
1790                     -- Insert copy sales orders
1791                     IF PG_DEBUG in ('Y', 'C') THEN
1792                         msc_sch_wb.atp_debug('Delete_Row: ' || 'i_ins_item_id_tab.COUNT:' || i_ins_item_id_tab.COUNT);
1793                     END IF;
1794 
1795                     FORALL i IN 1..i_ins_item_id_tab.COUNT
1796                     INSERT INTO MSC_DEMANDS(
1797                             DEMAND_ID,
1798                             USING_REQUIREMENT_QUANTITY,
1799                             USING_ASSEMBLY_DEMAND_DATE,
1800                             DEMAND_TYPE,
1801                             ORIGINATION_TYPE,
1802                             USING_ASSEMBLY_ITEM_ID,
1803                             PLAN_ID,
1804                             ORGANIZATION_ID,
1805                             INVENTORY_ITEM_ID,
1806                             SR_INSTANCE_ID,
1807                             LAST_UPDATE_DATE,
1808                             LAST_UPDATED_BY,
1809                             CREATION_DATE,
1810                             CREATED_BY,
1811                             RECORD_SOURCE,
1812                             REFRESH_NUMBER)
1813                     VALUES (l_copy_demand_id(i),
1814                             MSC_ATP_UTILS.Truncate_Demand(l_ins_qty_tab(i)),	-- 5598066
1815                             l_ins_date_tab(i),
1816                             1,      -- Discrete
1817                             52,     -- Copy sales order
1818                             i_ins_item_id_tab(i),
1819                             p_plan_id,
1820                             l_ins_org_id_tab(i),
1821                             i_ins_item_id_tab(i),
1822                             p_instance_id,
1823                             sysdate,
1824                             FND_GLOBAL.USER_ID,
1825                             sysdate,
1826                             FND_GLOBAL.USER_ID,
1827                             2,
1828                             l_ins_refresh_number_tab(i));
1829 
1830                 ELSE
1831                     IF PG_DEBUG in ('Y', 'C') THEN
1832                         msc_sch_wb.atp_debug('Delete_Row: ' || 'No copy Sales Orders');
1833                     END IF;
1834                 END IF;
1835 
1836                 x_copy_demand_id := l_copy_demand_id;
1837 
1838               ELSE -- IF x_demand_id IS NOT NULL AND x_demand_id.COUNT > 0 THEN
1839                 IF PG_DEBUG in ('Y', 'C') THEN
1840                     msc_sch_wb.atp_debug('Delete_Row: ' || 'No demands to be deleted');
1841                 END IF;
1842               END IF;
1843 
1844         END IF;
1845 
1846 
1847         -- Code to update summary records removed for summary enhancement
1848         /** code commented for time being. Will be removed after code review
1849         IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
1850             IF PG_DEBUG in ('Y', 'C') THEN
1851                 msc_sch_wb.atp_debug('Delete_Row: ' || 'update Demands in summary mode');
1852             END IF;
1853             -- Bug 2738280. Change the input parameters of procedure call.
1854             -- MSC_ATP_DB_UTILS.UPDATE_PLAN_SUMMARY_ROW(p_identifier, p_plan_id, p_instance_id);
1855             MSC_ATP_DB_UTILS.UPDATE_PLAN_SUMMARY_ROW(x_inv_item_id, l_old_demand_date, l_old_demand_quantity,
1856                                                      l_organization_id, p_plan_id, p_instance_id);
1857         END IF;
1858         **/
1859 
1860         /* time_phased_atp
1861            Delete bucketed demands and rollup supplies*/
1862         IF ((p_time_phased_atp = 'Y')
1863            OR
1864            ((MSC_ATP_PVT.G_INV_CTP = 4) AND
1865             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
1866             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
1867             --bug 3442528: We need to delete SO for model entities as well.
1868             --(MSC_ATP_PVT.G_ALLOCATION_METHOD = 1))) AND p_ato_model_line_id is null THEN
1869             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1))) THEN
1870 
1871             IF PG_DEBUG in ('Y', 'C') THEN
1872                 msc_sch_wb.atp_debug('Delete_Row: before update of msc_alloc_demands');
1873             END IF;
1874 
1875             IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'N' THEN
1876                 -- If condition added for summary enhancement
1877                 -- For non summary case since no returning is required hence direct bulk update is used
1878 
1879                 IF l_identifier_tab IS NOT NULL and l_identifier_tab.COUNT > 0 THEN
1880                     IF PG_DEBUG in ('Y', 'C') THEN
1881                         msc_sch_wb.atp_debug('Delete_Row: l_identifier_tab.COUNT ' || l_identifier_tab.COUNT);
1882                     END IF;
1883 
1884                     FORALL  i IN 1..l_identifier_tab.COUNT
1885                     UPDATE  msc_alloc_demands
1886                     SET     old_allocated_quantity = allocated_quantity,
1887                             old_demand_date = demand_date,
1888                             allocated_quantity = 0,
1889                             old_refresh_number = refresh_number, -- For summary enhancement
1890                             refresh_number = p_refresh_number
1891                     WHERE   sr_instance_id = p_instance_id
1892                     AND     plan_id = p_plan_id
1893                     AND     allocated_quantity > 0
1894                     AND     sales_order_line_id = l_identifier_tab(i)
1895                     AND     organization_id = p_source_organization_Id			---7118988
1896                     AND     decode(demand_source_type,100,demand_source_type,-1)
1897                             =decode(p_demand_source_type,
1898                                                 100,
1899                                                 p_demand_source_type,
1900                                                 -1); --CMRO
1901 
1902 
1903                     FORALL  i IN 1..l_identifier_tab.COUNT
1904                     UPDATE  msc_alloc_supplies
1905                     SET     old_allocated_quantity = allocated_quantity,
1906                             old_supply_date = supply_date,
1907                             allocated_quantity = 0,
1908                             old_refresh_number = refresh_number, -- For summary enhancement
1909                             refresh_number = p_refresh_number
1910                     WHERE   sr_instance_id = p_instance_id
1911                     AND     plan_id = p_plan_id
1912                     AND     stealing_flag = 1
1913                     AND     allocated_quantity <> 0
1914                     AND     sales_order_line_id = l_identifier_tab(i)
1915                     AND     organization_id = p_source_organization_Id			---7118988
1916                     AND     decode(demand_source_type,100,demand_source_type,-1)
1917                             =decode(p_demand_source_type,
1918                                                 100,
1919                                                 p_demand_source_type,
1920                                                 -1); --CMRO
1921 
1922 
1923                 END IF;
1924 
1925             ELSE    -- IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'N' THEN
1926 
1927                 SELECT  sales_order_line_id,
1928                         inventory_item_id,
1929                         organization_id,
1930                         refresh_number,
1931                         allocated_quantity,
1932                         demand_date,
1933                         demand_class
1934                 BULK COLLECT INTO
1935                         l_identifier_tab,
1936                         i_item_id_tab,
1937                         l_organization_id,
1938                         l_refresh_number_tab,
1939                         l_qty_tab,
1940                         l_old_demand_date,
1941                         l_demand_class_tab
1942                 FROM    msc_alloc_demands
1943                 WHERE   sr_instance_id = p_instance_id
1944                 AND     plan_id = p_plan_id
1945                 AND     allocated_quantity > 0
1946                 -- CTO ODR and Simplified Pegging
1947                 AND     decode(demand_source_type,100,demand_source_type,-1)
1948                             =decode(p_demand_source_type,
1949                                                 100,
1950                                                 p_demand_source_type,
1951                                                 -1) --CMRO
1952                 AND     sales_order_line_id in (p_identifier, p_config_line_id);
1953                 /* Comment out code refering msc_bom_temp
1954                 AND     sales_order_line_id IN (
1955                         SELECT  component_identifier
1956                         FROM    msc_bom_temp
1957                         START WITH assembly_identifier = p_identifier
1958                         AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1959                         AND     assembly_identifier <> component_identifier
1960                         CONNECT BY PRIOR component_identifier = assembly_identifier
1961                         AND     assembly_identifier <> component_identifier
1962                         AND     session_id = MSC_ATP_PVT.G_SESSION_ID
1963                         UNION ALL
1964                         SELECT  p_identifier FROM dual)
1965                Comment out code refering msc_bom_temp
1966                */
1967                -- End CTO ODR and Simplified Pegging
1968 
1969 
1970 
1971                 IF l_identifier_tab IS NOT NULL and l_identifier_tab.COUNT > 0 THEN
1972 
1973                     IF PG_DEBUG in ('Y', 'C') THEN
1974                         msc_sch_wb.atp_debug('Delete_Row: l_identifier_tab.COUNT ' || l_identifier_tab.COUNT);
1975                     END IF;
1976 
1977                     FORALL  i IN 1..l_identifier_tab.COUNT
1978                     UPDATE  msc_alloc_demands
1979                     SET     old_allocated_quantity = allocated_quantity,
1980                             old_demand_date = demand_date,
1981                             allocated_quantity = 0,
1982                             old_refresh_number = refresh_number, -- For summary enhancement
1983                             refresh_number = p_refresh_number -- For summary enhancement
1984                     WHERE   sr_instance_id = p_instance_id
1985                     AND     plan_id = p_plan_id
1986                     AND     allocated_quantity > 0
1987                     AND     sales_order_line_id = l_identifier_tab(i)
1988                     AND     organization_id = p_source_organization_Id			---7118988
1989                     AND     decode(demand_source_type,100,demand_source_type,-1)
1990                             =decode(p_demand_source_type,
1991                                                 100,
1992                                                 p_demand_source_type,
1993                                                 -1); --CMRO
1994 
1995 
1996 
1997 
1998                     FOR i IN 1..i_item_id_tab.COUNT LOOP
1999                         IF l_current_refresh_number IS NULL -- summary has not run
2000                             OR l_refresh_number_tab(i) > l_current_refresh_number THEN
2001                                 IF PG_DEBUG in ('Y', 'C') THEN
2002                                     msc_sch_wb.atp_debug('Delete_Row: ' || 'Copy SO will be created for identifier ' || l_identifier_tab(i));
2003                                 END IF;
2004 
2005                                 i_ins_item_id_tab.Extend;
2006                                 l_ins_org_id_tab.Extend;
2007                                 l_ins_refresh_number_tab.Extend;
2008                                 l_ins_qty_tab.Extend;
2009                                 l_ins_date_tab.Extend;
2010                                 l_copy_demand_id.Extend;
2011                                 l_ins_demand_class_tab.Extend;
2012 
2013                                 i_ins_item_id_tab(i_ins_item_id_tab.COUNT) := i_item_id_tab(i);
2014                                 l_ins_org_id_tab(l_ins_org_id_tab.COUNT) := l_organization_id(i);
2015                                 l_ins_refresh_number_tab(l_ins_refresh_number_tab.COUNT) := l_refresh_number_tab(i);
2016                                 l_ins_qty_tab(l_ins_qty_tab.COUNT) := l_qty_tab(i);
2017                                 l_ins_date_tab(l_ins_date_tab.COUNT) := l_old_demand_date(i);
2018                                 l_ins_demand_class_tab(l_ins_demand_class_tab.COUNT) := l_demand_class_tab(i);
2019 
2020                                 SELECT  msc_demands_s.nextval
2021                                 INTO    l_copy_demand_id(l_copy_demand_id.COUNT)
2022                                 FROM    dual;
2023 
2024                         END IF;
2025                     END LOOP;
2026 
2027                 END IF; -- IF l_identifier_tab IS NOT NULL and l_identifier_tab.COUNT > 0 THEN
2028 
2029                 SELECT  sales_order_line_id,
2030                         inventory_item_id,
2031                         organization_id,
2032                         refresh_number,
2033                         -1 * allocated_quantity, -- multiply by -1 since the copy record will be stored in msc_demands
2034                         supply_date,
2035                         demand_class
2036                 BULK COLLECT INTO
2037                         l_identifier_tab,
2038                         i_item_id_tab,
2039                         l_organization_id,
2040                         l_refresh_number_tab,
2041                         l_qty_tab,
2042                         l_old_demand_date,
2043                         l_demand_class_tab
2044                 FROM    msc_alloc_supplies
2045                 WHERE   sr_instance_id = p_instance_id
2046                 AND     plan_id = p_plan_id
2047                 AND     stealing_flag = 1
2048                 AND     allocated_quantity <> 0
2049                 -- CTO ODR and Simplified Pegging
2050                 AND     decode(demand_source_type,100,demand_source_type,-1)
2051                             =decode(p_demand_source_type,
2052                                                 100,
2053                                                 p_demand_source_type,
2054                                                 -1) --CMRO
2055                 AND     sales_order_line_id in (p_identifier, p_config_line_id);
2056                 /* Comment out code refering msc_bom_temp
2057                 AND     sales_order_line_id IN (
2058                         SELECT  component_identifier
2059                         FROM    msc_bom_temp
2060                         START WITH assembly_identifier = p_identifier
2061                         AND     session_id = MSC_ATP_PVT.G_SESSION_ID
2062                         AND     assembly_identifier <> component_identifier
2063                         CONNECT BY PRIOR component_identifier = assembly_identifier
2064                         AND     assembly_identifier <> component_identifier
2065                         AND     session_id = MSC_ATP_PVT.G_SESSION_ID
2066                         UNION ALL
2067                         SELECT  p_identifier FROM dual)
2068                 Comment out code refering msc_bom_temp
2069                 */
2070                 -- End CTO ODR and Simplified Pegging
2071 
2072 
2073                 IF l_identifier_tab IS NOT NULL and l_identifier_tab.COUNT > 0 THEN
2074 
2075                     IF PG_DEBUG in ('Y', 'C') THEN
2076                         msc_sch_wb.atp_debug('Delete_Row: l_identifier_tab.COUNT ' || l_identifier_tab.COUNT);
2077                     END IF;
2078 
2079                     FORALL  i IN 1..l_identifier_tab.COUNT
2080                     UPDATE  msc_alloc_supplies
2081                     SET     old_allocated_quantity = allocated_quantity,
2082                             old_supply_date = supply_date,
2083                             allocated_quantity = 0,
2084                             old_refresh_number = refresh_number, -- For summary enhancement
2085                             refresh_number = p_refresh_number -- For summary enhancement
2086                     WHERE   sr_instance_id = p_instance_id
2087                     AND     plan_id = p_plan_id
2088                     AND     stealing_flag = 1
2089                     AND     allocated_quantity <> 0
2090                     AND     sales_order_line_id = l_identifier_tab(i)
2091                     AND     organization_id = p_source_organization_Id			---7118988
2092                     AND     decode(demand_source_type,100,demand_source_type,-1)
2093                             =decode(p_demand_source_type,
2094                                                 100,
2095                                                 p_demand_source_type,
2096                                                 -1); --CMRO
2097 
2098 
2099 
2100 
2101                     FOR i IN 1..i_item_id_tab.COUNT LOOP
2102                         IF l_current_refresh_number IS NULL -- summary has not run
2103                             OR l_refresh_number_tab(i) > l_current_refresh_number THEN
2104 
2105                             IF PG_DEBUG in ('Y', 'C') THEN
2106                                 msc_sch_wb.atp_debug('Delete_Row: ' || 'Copy stealing will be created for identifier ' || l_identifier_tab(i));
2107                             END IF;
2108 
2109                             i_ins_item_id_tab.Extend;
2110                             l_ins_org_id_tab.Extend;
2111                             l_ins_refresh_number_tab.Extend;
2112                             l_ins_qty_tab.Extend;
2113                             l_ins_date_tab.Extend;
2114                             l_copy_demand_id.Extend;
2115                             l_ins_demand_class_tab.Extend;
2116 
2117                             i_ins_item_id_tab(i_ins_item_id_tab.COUNT) := i_item_id_tab(i);
2118                             l_ins_org_id_tab(l_ins_org_id_tab.COUNT) := l_organization_id(i);
2119                             l_ins_refresh_number_tab(l_ins_refresh_number_tab.COUNT) := l_refresh_number_tab(i);
2120                             l_ins_qty_tab(l_ins_qty_tab.COUNT) := l_qty_tab(i);
2121                             l_ins_date_tab(l_ins_date_tab.COUNT) := l_old_demand_date(i);
2122                             l_ins_demand_class_tab(l_ins_demand_class_tab.COUNT) := l_demand_class_tab(i);
2123 
2124                             SELECT  msc_demands_s.nextval
2125                             INTO    l_copy_demand_id(l_copy_demand_id.COUNT)
2126                             FROM    dual;
2127                         END IF;
2128                     END LOOP;
2129 
2130                 END IF; --IF l_identifier_tab IS NOT NULL and l_identifier_tab.COUNT > 0 THEN
2131 
2132 
2133                 IF i_ins_item_id_tab IS NOT NULL and i_ins_item_id_tab.COUNT > 0 THEN
2134                     IF PG_DEBUG in ('Y', 'C') THEN
2135                         msc_sch_wb.atp_debug('Delete_Row: i_ins_item_id_tab.COUNT ' || i_ins_item_id_tab.COUNT);
2136                     END IF;
2137 
2138                     FORALL i IN 1..i_ins_item_id_tab.COUNT
2139                     INSERT INTO MSC_ALLOC_DEMANDS(
2140                             PLAN_ID,
2141                             INVENTORY_ITEM_ID,
2142                             ORGANIZATION_ID,
2143                             SR_INSTANCE_ID,
2144                             DEMAND_CLASS,
2145                             DEMAND_DATE,
2146                             PARENT_DEMAND_ID,
2147                             ALLOCATED_QUANTITY,
2148                             ORIGINATION_TYPE,
2149                             CREATED_BY,
2150                             CREATION_DATE,
2151                             LAST_UPDATED_BY,
2152                             LAST_UPDATE_DATE,
2153                             REFRESH_NUMBER)
2154                     VALUES (p_plan_id,
2155                             i_ins_item_id_tab(i),
2156                             l_ins_org_id_tab(i),
2157                             p_instance_id,
2158                             l_ins_demand_class_tab(i),
2159                             l_ins_date_tab(i),
2160                             l_copy_demand_id(i),     -- parent demand id
2161                             MSC_ATP_UTILS.Truncate_Demand(l_ins_qty_tab(i)),	--5598066
2162                             52,     -- Copy sales order
2163                             FND_GLOBAL.USER_ID,
2164                             sysdate,
2165                             FND_GLOBAL.USER_ID,
2166                             sysdate,
2167                             l_ins_refresh_number_tab(i));
2168                 END IF; -- IF i_ins_item_id_tab IS NOT NULL and i_ins_item_id_tab.COUNT > 0 THEN
2169 
2170                 x_copy_demand_id := l_copy_demand_id;
2171 
2172             END IF; -- IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'N' THEN
2173 
2174         END IF; -- IF <demand priority allocation> THEN
2175         -- CTO ODR and Simplified Pegging
2176         MSC_ATP_PEG.Add_Offset_Data(p_identifier ,
2177                                     p_config_line_id,
2178                                     p_plan_id      ,
2179                                     p_refresh_number,
2180                                     p_order_number  ,
2181                                     p_demand_source_type,--cmro
2182                                     x_atp_peg_items  ,
2183                                     x_atp_peg_demands ,
2184                                     x_atp_peg_supplies,
2185                                     x_atp_peg_res_reqs,
2186                                     x_demand_instance_id, --Bug 3629191
2187                                     x_supply_instance_id, --Bug 3629191
2188                                     x_res_instance_id,    --Bug 3629191
2189                                     l_return_status);
2190         -- END CTO ODR and Simplified Pegging
2191 
2192     END IF; -- IF p_plan_id = -1 THEN
2193 
2194     -- Commit removed for PDS cases - summary enhancement
2195     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' AND p_plan_id = -1  AND G_ORIG_INV_CTP = 5 THEN
2196                 -- Condition for INV_CTP added for bug 3295831 because summary
2197                 -- is not supported in PDS-ODS switch.
2198         IF PG_DEBUG in ('Y', 'C') THEN
2199             msc_sch_wb.atp_debug('Summary mode commit, in Delete_Row');
2200         END IF;
2201         commit;
2202     END IF;
2203 
2204     IF PG_DEBUG in ('Y', 'C') THEN
2205         msc_sch_wb.atp_debug('**********End Delete_Row Procedure************');
2206     END IF;
2207 
2208 EXCEPTION
2209     WHEN OTHERS THEN
2210         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2211             FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME , 'Delete_Row');
2212         END IF;
2213 
2214         IF PG_DEBUG in ('Y', 'C') THEN
2215             msc_sch_wb.atp_debug('Delete_Row :' || sqlcode || ': ' || sqlerrm);
2216         END IF;
2217         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2218 
2219 END Delete_Row;
2220 
2221 
2222 PROCEDURE Remove_Invalid_SD_Rec(
2223         p_identifier          IN      NUMBER,
2224         p_instance_id         IN      NUMBER,
2225         p_plan_id             IN      NUMBER, -- not use
2226         p_mode                IN      NUMBER,
2227         p_dc_flag             IN      NUMBER,
2228         x_return_status       OUT     NoCopy VARCHAR2
2229 )
2230 IS
2231 
2232     CURSOR pegging IS
2233         select  pegging_id, identifier3, identifier2, identifier1,
2234                 supply_demand_type, inventory_item_id, char1, organization_id, supply_demand_date, --Bug 1419121
2235                 supply_demand_quantity, department_id, resource_id, order_line_id, supplier_id, supplier_site_id,
2236                 supplier_atp_date, dest_inv_item_id, summary_flag
2237                 -- time_phased_atp
2238                 , aggregate_time_fence_date
2239         from    mrp_atp_details_temp
2240         where   ((pegging_id <> p_identifier and (p_mode = 2 or p_mode = 3)) or
2241                 (p_mode = 1))
2242         and     record_type in (3,4)
2243         and     session_id = MSC_ATP_PVT.G_SESSION_ID
2244         start with pegging_id = p_identifier
2245         and     session_id = MSC_ATP_PVT.G_SESSION_ID
2246         and     record_type = 3
2247         connect by parent_pegging_id = prior pegging_id
2248         AND     session_id = prior session_id
2249         AND     record_type in (3,4);
2250 
2251     c1 pegging%ROWTYPE;
2252     l_inventory_item_id number;
2253     --bug 2465088: increase size of l_demand class from 25 to 30 characters
2254     l_demand_class      varchar2(30);
2255     l_department_id     number;
2256     l_resource_id       number;
2257     l_instance_id       number;
2258     l_sd_qty            number;
2259     l_sd_date           date; -- bug 2120698
2260     l_organization_id   number;
2261     temp_sd_qty         number;
2262     l_supplier_id       number;
2263     l_supplier_site_id  number;
2264     l_start_date        date;
2265 
2266     -- time_phased_atp
2267     l_atf_date   date;
2268 
2269     -- bug 9911034, ALLOC ATP CHANGES, 12973673
2270     l_inv_ctp           number:= MSC_ATP_PVT.G_INV_CTP;
2271     l_allocated_atp     varchar2(1):= MSC_ATP_PVT.G_ALLOCATED_ATP;
2272 
2273 BEGIN
2274     IF PG_DEBUG in ('Y', 'C') THEN
2275         msc_sch_wb.atp_debug('***** Begin Remove_Invalid_SD_Rec Procedure *****');
2276     END IF;
2277 
2278     -- for ods, just need to remove the record from msc_sales_orders
2279     -- initialize API return status to success
2280     x_return_status := FND_API.G_RET_STS_SUCCESS;
2281 
2282     -- rajjain 05/19/2003 bug 2959840
2283     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Reset all the global variables ');
2284     -- Begin ATP4drp Allocation not supported for DRP plans.
2285     IF (NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5) THEN
2286         MSC_ATP_PVT.G_ALLOCATED_ATP :=  'N';
2287         MSC_ATP_PVT.G_ALLOCATION_METHOD := 2;
2288         IF PG_DEBUG in ('Y', 'C') THEN
2289               msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2290               msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'PF and Allocated ATP not applicable for DRP plans');
2291               msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2292         END IF;
2293     ELSE
2294         -- ATP4drp set using the original value
2295         MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
2296         MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'),2);
2297     END IF;
2298     -- End ATP4drp
2299     MSC_ATP_PVT.G_INV_CTP := FND_PROFILE.value('INV_CTP') ;
2300 
2301     IF PG_DEBUG in ('Y', 'C') THEN
2302         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'G_ALLOCATION_METHOD= ' || MSC_ATP_PVT.G_ALLOCATION_METHOD);
2303         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'G_ALLOCATED_ATP= ' || MSC_ATP_PVT.G_ALLOCATED_ATP);
2304         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'INV_CTP= ' || MSC_ATP_PVT.G_INV_CTP);
2305         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'p_mode= ' || p_mode);
2306         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'p_identifier= ' || p_identifier);
2307         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'p_instance_id = '||p_instance_id);
2308     END IF;
2309 
2310 
2311     OPEN pegging;
2312     LOOP
2313 
2314         FETCH pegging INTO c1;
2315         EXIT WHEN pegging%NOTFOUND;
2316         ---set l_inventory_item_id back to null;
2317         l_inventory_item_id := null;
2318         IF PG_DEBUG in ('Y', 'C') THEN
2319             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'pegging_id = '||c1.pegging_id);
2320             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supply_demand_type = '||c1.supply_demand_type);
2321             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'identifier3 = '||c1.identifier3);
2322             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'plan_id (identifier2) = '||c1.identifier2);
2323             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'inventory_item_id := ' || c1.inventory_item_id);
2324             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supply_demand_quantity := ' || c1.supply_demand_quantity);
2325             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'organization_id := ' || c1.organization_id);
2326             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supply_demand_date := ' || c1.supply_demand_date);
2327             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supplier_id := ' || c1.supplier_id);
2328             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supplier_site_id := ' || c1.supplier_site_id);
2329             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supplier_atp_date := ' || c1.supplier_atp_date);
2330             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Destination inv_item id := ' || c1.dest_inv_item_id);
2331         END IF;
2332         l_organization_id := null;
2333 
2334         IF p_mode = MSC_ATP_PVT.INVALID or (p_mode=MSC_ATP_PVT.UNDO and p_instance_id is null)  THEN
2335 
2336             MSC_ATP_DB_UTILS.Delete_Pegging(c1.pegging_id);
2337 
2338             IF PG_DEBUG in ('Y', 'C') THEN
2339                 msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'delete this pegging_id '||c1.pegging_id);
2340             END IF;
2341         END IF;
2342 
2343         IF c1.supply_demand_type = 2 THEN
2344 
2345             IF NVL(c1.inventory_item_id, -1) > 0 THEN
2346 
2347                 -- delete the planned order that we may have enterred.
2348                 DELETE FROM MSC_SUPPLIES
2349                 WHERE transaction_id = c1.identifier3
2350                 AND   plan_id = c1.identifier2
2351                 returning inventory_item_id, sr_instance_id,new_order_quantity, organization_id, supplier_id, supplier_site_id
2352                 into l_inventory_item_id, l_instance_id, l_sd_qty, l_organization_id, l_supplier_id, l_supplier_site_id;
2353 
2354                 IF PG_DEBUG in ('Y', 'C') THEN
2355                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_inventory_item_id := '|| l_inventory_item_id);
2356                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_instance_id := ' || l_instance_id);
2357                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_sd_qty := ' || l_sd_qty);
2358                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_organization_id := ' || l_organization_id);
2359                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_supplier_id := ' || l_supplier_id);
2360                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_supplier_site_id := ' || l_supplier_site_id);
2361                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'summary flag := ' || c1.summary_flag);
2362                 END IF;
2363 
2364                 -- time_phased_atp
2365                 IF (c1.aggregate_time_fence_date is not null)
2366                    OR
2367                    ((MSC_ATP_PVT.G_INV_CTP = 4) AND
2368                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2369                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
2370                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
2371 
2372                     IF PG_DEBUG in ('Y', 'C') THEN
2373                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: before delete of ' ||
2374                             '  msc_alloc_supplies');
2375                     END IF;
2376 
2377                     DELETE FROM MSC_ALLOC_SUPPLIES
2378                     WHERE parent_transaction_id = c1.identifier3
2379                     AND   plan_id = c1.identifier2;
2380 
2381                     IF PG_DEBUG in ('Y', 'C') THEN
2382                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'No. of supply deleted from msc_alloc_supplies = '|| SQL%ROWCOUNT);
2383                     END IF;
2384                 END IF;
2385                 --  Allocated ATP Based on Planning Details -- Agilent changes End
2386 
2387                 -- Code to update summary records and committing removed for Bug 3295831
2388                 -- Supplies are being updated hence plan id would definitely not be -1
2389                 /**
2390                 IF c1.summary_flag = 'Y' and (c1.identifier3 > 0) and (p_plan_id = -1) THEN
2391                     -- Check for Plan_id=-1 added for summary enhancement
2392                     --- update the MSC_ATP_SUMMARY_SD table
2393                     IF PG_DEBUG in ('Y', 'C') THEN
2394                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'update summary table');
2395                     END IF;
2396                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2397                                                        p_plan_id,
2398                                                        l_organization_id,
2399                                                        l_inventory_item_id,
2400                                                        c1.supply_demand_date,
2401                                                        null,
2402                                                        null,
2403                                                        null,
2404                                                        null,
2405                                                        null,
2406                                                        null,
2407                                                        2);
2408 
2409                     UPDATE  /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* MSC_ATP_SUMMARY_SD
2410                     set     sd_qty = (sd_qty - l_sd_qty)
2411                     where   plan_id = p_plan_id and
2412                             sr_instance_id = l_instance_id and
2413                             inventory_item_id = l_inventory_item_id and
2414                             organization_id = l_organization_id and
2415                             sd_date = trunc(c1.supply_demand_date);
2416                     IF PG_DEBUG in ('Y', 'C') THEN
2417                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'no of rows updated in summary mode := ' || SQL%ROWCOUNT);
2418                     END IF;
2419 
2420                     commit;
2421                 END IF;
2422                 **/
2423 
2424                 -- Bug 1419121 Delete demand records which are inserted with supply_demand_type=2
2425                 -- for taking care of demand class consumption. In such cases, the records in
2426                 -- pegging tree are inserted with supply_demand_type = 2 and demand_class as
2427                 -- NOT NULL.
2428 
2429                 IF NVL(c1.char1, '@@@') <> '@@@' THEN
2430 
2431                     IF PG_DEBUG in ('Y', 'C') THEN
2432                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'delete stealing demand from msc_demands, demand_id = '||
2433                             c1.identifier3);
2434                     END IF;
2435 
2436                     DELETE FROM MSC_DEMANDS
2437                     WHERE demand_id = c1.identifier3
2438                     AND   plan_id = c1.identifier2;
2439                 END IF;
2440             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
2441 
2442         ELSE    -- IF c1.supply_demand_type = 2 THEN
2443 
2444             -- delete the demand records we may have entrered.
2445             --4267076: Remove demand only when deamdn id is available
2446             IF NVL(c1.inventory_item_id, -1) > 0 AND c1.identifier3 is not null THEN
2447                 IF c1.identifier2 <> -1 THEN
2448 
2449                     IF PG_DEBUG in ('Y', 'C') THEN
2450                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'delete demand from msc_demand, demand_id = '||
2451                             c1.identifier3);
2452                     END IF;
2453 
2454                     -- Bug 1661545, if scheduling was unsuccessful, old demand record needs to be
2455                     -- preserved back, as it was updated to 0 in the begining in case of reschedule in PDS.
2456 
2457                     DELETE  FROM MSC_DEMANDS
2458                     WHERE   demand_id = c1.identifier3
2459                     AND     plan_id = c1.identifier2
2460                     AND	    old_demand_quantity IS NULL
2461                     -- for bug 2120698, need to get the date and quantity from here
2462                     -- instead of pegging
2463                     returning inventory_item_id, sr_instance_id,
2464                             using_requirement_quantity, organization_id ,
2465                             trunc(using_assembly_demand_date)
2466                     into    l_inventory_item_id, l_instance_id,
2467                             l_sd_qty, l_organization_id,
2468                             l_sd_date;
2469 
2470                     IF PG_DEBUG in ('Y', 'C') THEN
2471                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'No. of demand deleted from msc_demand = '|| SQL%ROWCOUNT);
2472                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
2473                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_instance_id := ' || l_instance_id);
2474                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_sd_qty := ' || l_sd_qty);
2475                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_organization_id := ' || l_organization_id);
2476                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_sd_date :='||l_sd_date);
2477                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'summary_flag := ' || c1.summary_flag);
2478                     END IF;
2479 
2480                     -- time_phased_atp
2481                     IF (c1.aggregate_time_fence_date is not null)
2482                        OR
2483                        ((MSC_ATP_PVT.G_INV_CTP = 4) AND
2484                         (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2485                         (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
2486                         (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
2487 
2488                         IF PG_DEBUG in ('Y', 'C') THEN
2489                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: before delete of' ||
2490                                 ' msc_alloc_demands');
2491                         END IF;
2492 
2493                         DELETE FROM MSC_ALLOC_DEMANDS
2494                         WHERE parent_demand_id = c1.identifier3
2495                         AND	  old_allocated_quantity IS NULL
2496                         AND   plan_id = c1.identifier2;
2497 
2498                         IF PG_DEBUG in ('Y', 'C') THEN
2499                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'No. of demand deleted from msc_alloc_demands = '|| SQL%ROWCOUNT);
2500                         END IF;
2501                     END IF;
2502                     --  Allocated ATP Based on Planning Details -- Agilent changes End
2503 
2504                     -- Code to update summary records and committing removed for Bug 3295831
2505                     -- We are coming here only when c1.identifier2 <> -1
2506                     /**
2507                     IF (c1.summary_flag = 'Y') AND (c1.identifier3 > 0) and (p_plan_id = -1) THEN
2508                     -- Check for Plan_id=-1 added for summary enhancement
2509                         IF PG_DEBUG in ('Y', 'C') THEN
2510                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'summary mode, delete demand');
2511                         END IF;
2512                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2513                                                   p_plan_id,
2514                                                   l_organization_id,
2515                                                   l_inventory_item_id,
2516                                                   l_sd_date,
2517                                                   null,
2518                                                   null,
2519                                                   null,
2520                                                   null,
2521                                                   null,
2522                                                   null,
2523                                                   2);
2524 
2525                         UPDATE  /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* MSC_ATP_SUMMARY_SD
2526                         SET sd_qty = sd_qty + l_sd_qty
2527                         WHERE plan_id = p_plan_id
2528                         AND   sr_instance_id = l_instance_id
2529                         AND   organization_id = l_organization_id
2530                         AND   inventory_item_id = l_inventory_item_id
2531                         AND   sd_date = trunc(l_sd_date);
2532                         IF PG_DEBUG in ('Y', 'C') THEN
2533                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'no of rows updated in summary mode := ' || SQL%ROWCOUNT);
2534                         END IF;
2535 
2536                         commit;
2537                     END IF;
2538                     **/
2539 
2540                     ---- update supplier info if it is a supplier record
2541                     -- Code to update summary records and committing removed for Bug 3295831
2542                     /** code commented for time being. Will be removed after code review
2543                     IF  (c1.summary_flag = 'Y') AND (NVL(c1.supplier_id, -1) <> -1)  THEN
2544                         IF PG_DEBUG in ('Y', 'C') THEN
2545                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'update suppliers info ');
2546                         END IF;
2547                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(c1.identifier1,
2548                                           p_plan_id,
2549                                           null,
2550                                           c1.dest_inv_item_id,
2551                                            c1.supplier_atp_date,
2552                                           null,
2553                                           null,
2554                                           c1.supplier_id,
2555                                           c1.supplier_site_id,
2556                                           null,
2557                                           null,
2558                                           4);
2559 
2560                         update /*+ INDEX(msc_atp_summary_sup MSC_ATP_SUMMARY_SUP_U1) *//* msc_atp_summary_sup
2561                         set sd_qty = sd_qty + c1.supply_demand_quantity
2562                         where plan_id = p_plan_id
2563                         and   sr_instance_id = c1.identifier1
2564                         and inventory_item_id = c1.dest_inv_item_id
2565                         and supplier_id = c1.supplier_id
2566                         and supplier_site_id = c1.supplier_site_id
2567                         and sd_date = trunc(c1.supplier_atp_date);
2568 
2569                         IF PG_DEBUG in ('Y', 'C') THEN
2570                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Number of rows updated in SUMMARY_SUP := ' || SQL%ROWCOUNT);
2571                         END IF;
2572 
2573                         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(c1.identifier1,
2574                                           p_plan_id,
2575                                           null,
2576                                           c1.dest_inv_item_id,
2577                                            c1.supplier_atp_date,
2578                                           null,
2579                                           null,
2580                                           c1.supplier_id,
2581                                           c1.supplier_site_id,
2582                                           null,
2583                                           null,
2584                                           4);
2585 
2586                         commit;
2587                     END IF;
2588                     */
2589 
2590                 ELSE    -- IF c1.identifier2 <> -1 THEN
2591 
2592                     IF PG_DEBUG in ('Y', 'C') THEN
2593                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'delete demand from msc_sales_orders, demand_id = '||
2594                             c1.identifier3);
2595                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'demand_source_line := ' || to_char(c1.order_line_id));
2596                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'sr_instance_id := ' ||  c1.identifier1);
2597                     END IF;
2598 
2599                     DELETE  from msc_sales_orders
2600                     WHERE   demand_id = c1.identifier3
2601                     AND     demand_source_line = to_char(c1.order_line_id)
2602                     AND     sr_instance_id = c1.identifier1
2603                             -- for bug 2120698, need to get the date and quantity from here
2604                             -- instead of pegging
2605                     returning inventory_item_id, demand_class, sr_instance_id,
2606                             primary_uom_quantity, trunc(requirement_date)
2607                     into    l_inventory_item_id, l_demand_class, l_instance_id,
2608                             l_sd_qty, l_sd_date;
2609 
2610                     IF PG_DEBUG in ('Y', 'C') THEN
2611                         msc_sch_wb.atp_debug('Rows deleted := ' || SQL%ROWCOUNT);
2612                     END IF;
2613 
2614                     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND G_ORIG_INV_CTP = 5 THEN
2615                         -- Condition for INV_CTP added for bug 3295831 because summary
2616                         -- is not supported in PDS-ODS switch.
2617                         IF PG_DEBUG in ('Y', 'C') THEN
2618                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Update summary sales order tbale');
2619                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || ' supply_demand_quantity := '||c1.supply_demand_quantity);
2620                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || '  c1.inventory_item_id := ' ||  c1.inventory_item_id);
2621                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || ' instance_id := ' || p_instance_id);
2622                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || ' organization_id := ' || c1.organization_id);
2623                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'supply_demand_date := ' ||  c1.supply_demand_date);
2624                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_instance_id := ' || l_instance_id);
2625                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_sd_qty := ' || l_sd_qty);
2626                             msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_sd_date := ' || l_sd_date);
2627                         END IF;
2628 
2629                         BEGIN
2630                             IF PG_DEBUG in ('Y', 'C') THEN
2631                                 msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'inventory_item id after conversion := ' || l_inventory_item_id);
2632                             END IF;
2633 
2634                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2635                                                                -1,
2636                                                                c1.organization_id,
2637                                                                l_inventory_item_id,
2638                                                                l_sd_date,
2639                                                                null,
2640                                                                null,
2641                                                                null,
2642                                                                null,
2643                                                                p_dc_flag,
2644                                                                l_demand_class,
2645                                                                1);
2646 
2647 
2648                             update  /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
2649                                     -- bug 2120698, use the date and quantity from
2650                                     -- the returned value of msc_sales_orders instead of
2651                                     -- pegging info.  The reason is that for a ship set,
2652                                     -- we could update sd date to later date without
2653                                     -- changing the pegging.
2654                                     -- set sd_qty = (sd_qty - c1.supply_demand_quantity)
2655                             set     sd_qty = (sd_qty - l_sd_qty)
2656                             where   inventory_item_id = l_inventory_item_id
2657                             and     sr_instance_id = l_instance_id
2658                             and     organization_id = c1.organization_id
2659                                     -- bug 2120698: same reason above
2660                                     -- and sd_date = c1.supply_demand_date
2661                             and     sd_date = trunc(l_sd_date)
2662                             and     demand_class =Decode(p_dc_flag, 1, NVL(l_demand_class, '@@@'),'@@@') ;
2663 
2664                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2665                                                                -1,
2666                                                                c1.organization_id,
2667                                                                l_inventory_item_id,
2668                                                                l_sd_date,
2669                                                                null,
2670                                                                null,
2671                                                                null,
2672                                                                null,
2673                                                                p_dc_flag,
2674                                                                l_demand_class,
2675                                                                1);
2676 
2677                         EXCEPTION
2678                             WHEN OTHERS THEN
2679                                 IF PG_DEBUG in ('Y', 'C') THEN
2680                                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || sqlerrm);
2681                                 END IF;
2682                         END;
2683                     END IF;
2684 
2685                 END IF; -- IF c1.identifier2 <> -1 THEN
2686             --bug 4267076: Delete only if demand_id/transaction id is available
2687             ELSIF c1.identifier3 is not null then     -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
2688 
2689                 DELETE  FROM MSC_RESOURCE_REQUIREMENTS
2690                 WHERE   transaction_id = c1.identifier3
2691                 AND     plan_id = c1.identifier2
2692                 AND   sr_instance_id = c1.identifier1 -- Bug 2675487 --3395085: Use correct instance_id
2693                 returning department_id, resource_id, organization_id, start_date, sr_instance_id
2694                 into    l_department_id, l_resource_id, l_organization_id, l_start_date, l_instance_id;
2695 
2696                 IF PG_DEBUG in ('Y', 'C') THEN
2697                     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'summary_flag := ' || c1.summary_flag);
2698                 END IF;
2699 
2700                 -- Code to update summary records and committing removed for summary enhancement
2701                 /** code commented for time being. Will be removed after code review
2702                 IF c1.summary_flag = 'Y' THEN
2703                     IF PG_DEBUG in ('Y', 'C') THEN
2704                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_department_id = ' || l_department_id);
2705                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_resource_id = ' || l_resource_id);
2706                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_organization_id = ' || l_organization_id);
2707                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_start_date = ' || l_start_date);
2708                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'l_instance_id = ' || l_instance_id);
2709                     END IF;
2710 
2711                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2712                                               p_plan_id,
2713                                               l_organization_id,
2714                                               null,
2715                                               l_start_date,
2716                                               l_resource_id,
2717                                               l_department_id,
2718                                               null,
2719                                               null,
2720                                               null,
2721                                               null,
2722                                               3);
2723 
2724                     UPDATE /*+ INDEX(msc_atp_summary_res MSC_ATP_SUMMARY_RES_U1) *//* MSC_ATP_SUMMARY_RES
2725                     set sd_qty = sd_qty + c1.supply_demand_quantity
2726                     where plan_id = p_plan_id
2727                     and   sr_instance_id = l_instance_id
2728                     and   organization_id = l_organization_id
2729                     and   department_id = l_department_id
2730                     and   resource_id = l_resource_id
2731                     and   sd_date =  trunc(l_start_date);
2732 
2733                     IF PG_DEBUG in ('Y', 'C') THEN
2734                         msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Number of row updated in MSC_ATP_SUMMARY_RES := ' || SQL%ROWCOUNT);
2735                     END IF;
2736 
2737                     MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
2738                                               p_plan_id,
2739                                               l_organization_id,
2740                                               null,
2741                                               l_start_date,
2742                                               l_resource_id,
2743                                               l_department_id,
2744                                               null,
2745                                               null,
2746                                               null,
2747                                               null,
2748                                               3);
2749 
2750                     commit;
2751                 END IF;
2752                 **/
2753 
2754             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
2755         END IF; -- IF c1.supply_demand_type = 2 THEN
2756 
2757     END LOOP;
2758     CLOSE pegging;
2759 
2760     -- bug 9911034, ALLOC ATP CHANGES, 12973673
2761     msc_sch_wb.atp_debug('Remove_Invalid_SD_Rec: ' || 'Reset G_INV_CTP and G_ALLOCATED_ATP to runtime changed values, if any');
2762     MSC_ATP_PVT.G_INV_CTP := nvl(l_inv_ctp, MSC_ATP_PVT.G_INV_CTP);
2763     MSC_ATP_PVT.G_ALLOCATED_ATP := nvl(l_allocated_atp, MSC_ATP_PVT.G_ALLOCATED_ATP);
2764 
2765     IF PG_DEBUG in ('Y', 'C') THEN
2766         msc_sch_wb.atp_debug('***** End Remove_Invalid_SD_Rec Procedure *****');
2767     END IF;
2768 
2769 END Remove_Invalid_SD_Rec;
2770 
2771 
2772 PROCEDURE Update_Pegging(
2773   p_pegging_id          IN         NUMBER,
2774   p_date                IN         DATE,
2775   p_quantity            IN         NUMBER
2776 )
2777 IS
2778 BEGIN
2779 
2780       IF PG_DEBUG in ('Y', 'C') THEN
2781          msc_sch_wb.atp_debug('Begin Update_Pegging,p_pegging_id='||p_pegging_id);
2782       END IF;
2783 
2784       UPDATE mrp_atp_details_temp
2785       SET   supply_demand_quantity = NVL(p_quantity, supply_demand_quantity),
2786             supply_demand_date = NVL(p_date, supply_demand_date),
2787             --bug 3328421
2788             --required_date =  NVL(p_date, supply_demand_date)
2789             actual_supply_demand_date = NVL(p_date, supply_demand_date)
2790 			-- dsting
2791 	    , last_update_date = sysdate
2792 	    , last_updated_by = FND_GLOBAL.USER_ID
2793 	    , last_update_login = FND_GLOBAL.USER_ID
2794       WHERE pegging_id = p_pegging_id
2795       AND   session_id = MSC_ATP_PVT.G_SESSION_ID
2796       AND   record_type = 3;
2797 
2798       IF PG_DEBUG in ('Y', 'C') THEN
2799          msc_sch_wb.atp_debug('End Update_Pegging');
2800       END IF;
2801 EXCEPTION
2802       WHEN OTHERS THEN
2803           IF PG_DEBUG in ('Y', 'C') THEN
2804              msc_sch_wb.atp_debug('something wrong : Update_Pegging : ' || sqlcode);
2805           END IF;
2806 END Update_Pegging;
2807 
2808 PROCEDURE Update_Planned_Order(
2809         p_pegging_id          IN         NUMBER,
2810         p_plan_id             IN         NUMBER,
2811         p_date                IN         DATE,
2812         p_quantity            IN         NUMBER,
2813         p_supplier_id         IN         NUMBER,
2814         p_supplier_site_id    IN         NUMBER,
2815         p_dock_date           IN         DATE,
2816         p_ship_date           IN         DATE,     -- Bug 3241766
2817         p_start_date          IN         DATE,     -- Bug 3241766
2818         p_order_date          IN         DATE,     -- Bug 3241766
2819         p_mem_item_id         IN         NUMBER,   -- Bug 3293163
2820         p_pf_item_id          IN         NUMBER,
2821         p_mode                IN         NUMBER := MSC_ATP_PVT.UNDO,
2822         p_uom_conv_rate       IN         NUMBER := NULL
2823 )
2824 IS
2825 
2826     l_transaction_id    number;
2827     l_return_status     varchar2(1);
2828     l_child_pegging_id  number;
2829     l_demand_id         number;
2830     l_plan_id           number;
2831     l_inventory_item_id number;
2832     l_organization_id   number;
2833     l_sd_date           date;
2834     l_sd_qty            number;
2835     l_instance_id       number;
2836     temp_sd_qty         number;
2837     l_supplier_atp_date date;
2838     l_summary_flag      varchar2(1);
2839 
2840     -- dsting 2754446
2841     l_prim_uom_dmd_qty  number;
2842 
2843     -- time_phased_atp
2844     l_atf_date           date;
2845     l_po_qty             number;
2846 
2847     -- ATP4drp Additional Fields to track source and receiving orgs.
2848     l_receive_org_id     number;
2849 
2850 BEGIN
2851 
2852     IF PG_DEBUG in ('Y', 'C') THEN
2853         msc_sch_wb.atp_debug('***** Begin Update_Planned_Order Procedure *****');
2854         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_pegging_id = '||p_pegging_id);
2855         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_plan_id = '||p_plan_id);
2856         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_date = '||p_date);
2857         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_quantity = '||p_quantity);
2858         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_mode = '||p_mode);
2859         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_uom_conv_rate = '||p_uom_conv_rate);
2860         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_dock_date = '||p_dock_date);
2861         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_ship_date = '||p_ship_date);
2862         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_start_date = '||p_start_date);
2863         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_order_date = '||p_order_date);
2864         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_mem_item_id := ' || p_mem_item_id);
2865         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'p_pf_item_id := ' || p_pf_item_id);
2866     END IF;
2867 
2868     SELECT  identifier3, supply_demand_date, supply_demand_quantity, summary_flag
2869             ,aggregate_time_fence_date -- for time_phased_atp
2870             , receiving_organization_id, organization_id, identifier1  -- ATP4drp
2871     INTO    l_transaction_id, l_sd_date, l_po_qty, l_summary_flag
2872             ,l_atf_date -- for time_phased_atp
2873             , l_receive_org_id, l_organization_id, l_instance_id   -- ATP4drp
2874     FROM    mrp_atp_details_temp
2875     WHERE   pegging_id = p_pegging_id
2876     AND     record_type = 3
2877     AND     session_id = MSC_ATP_PVT.G_SESSION_ID ;
2878 
2879     IF PG_DEBUG in ('Y', 'C') THEN
2880         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'after select');
2881         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_sd_date := ' || l_sd_date);
2882         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_po_qty := ' || l_po_qty);
2883         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_summary_flag := ' || l_summary_flag);
2884         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_atf_date := ' || l_atf_date);
2885         -- ATP4drp print out organizations
2886         msc_sch_wb.atp_debug('----- ATP4drp Specific Debug Messages -----');
2887         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_receive_org_id := ' || l_receive_org_id);
2888         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_organization_id := ' || l_organization_id);
2889         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_instance_id := ' || l_instance_id);
2890         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_transaction_id := ' || l_transaction_id);
2891         -- ATP4drp print out organizations
2892     END IF;
2893     --diag_ATP: Do not remove the planned order in diagnostic mode even if the quantity is zero
2894     IF NVL(p_quantity, -1) = 0  AND (MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 or p_mode = MSC_ATP_PVT.INVALID) THEN
2895 
2896         MSC_ATP_DB_UTILS.Remove_Invalid_SD_Rec(
2897             p_pegging_id,
2898             null,
2899             p_plan_id,
2900             MSC_ATP_PVT.UNDO,
2901             0,
2902             l_return_status);
2903 
2904         IF PG_DEBUG in ('Y', 'C') THEN
2905             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'after delete mrp_atp_details_temp by calling remove');
2906         END IF;
2907 
2908         --s_cto_rearch
2909         IF MSC_ATP_PVT.G_INV_CTP = 4  THEN
2910             DELETE from msc_supplies
2911             WHERE  plan_id = p_plan_id
2912             AND    transaction_id = l_transaction_id;
2913         END IF;
2914         --e_cto_rearch
2915 
2916         IF PG_DEBUG in ('Y', 'C') THEN
2917             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'after msc_supplies');
2918         END IF;
2919     ELSE
2920 
2921         -- dsting diag_atp.
2922         IF p_mode <> 3 THEN
2923             MSC_ATP_DB_UTILS.Update_Pegging(p_pegging_id, p_date, p_quantity);
2924         END IF;
2925 
2926         IF PG_DEBUG in ('Y', 'C') THEN
2927             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'after first update to mrp_atp_details_temp');
2928         END IF;
2929 
2930         -- For bug 2259824, move the supply to the end of day
2931         --s_cto_rearch:
2932         IF MSC_ATP_PVT.G_INV_CTP = 4  THEN
2933             UPDATE  msc_supplies
2934             SET     new_schedule_date = TRUNC(NVL(p_date, new_schedule_date)) + MSC_ATP_PVT.G_END_OF_DAY ,
2935                     new_order_quantity = NVL(p_quantity, new_order_quantity),
2936                     -- rajjain 02/19/2003 Bug 2788302 Begin
2937                     supplier_id =      Decode(p_supplier_id,
2938                                               -1,null,
2939                                               p_supplier_id),
2940                     supplier_site_id = Decode(p_supplier_site_id,
2941                                               -1,null,
2942                                               p_supplier_site_id),
2943                     -- rajjain 02/19/2003 Bug 2788302 End
2944                     -- Bug 3821358, Making the dates at the end of the day
2945                     new_dock_date = TRUNC(NVL(p_dock_date, new_dock_date)) + MSC_ATP_PVT.G_END_OF_DAY,
2946                     -- rajjain 02/19/2003 Bug 2788302 Begin
2947                     source_supplier_id = decode(p_supplier_id,
2948                                                 -1,null,
2949                                                 p_supplier_id),
2950                     source_supplier_site_id = decode(p_supplier_site_id,
2951                                                      -1,null,
2952                                                      p_supplier_site_id),
2953                     -- rajjain 02/19/2003 Bug 2788302 End
2954                     -- ATP4drp Ensure that source data is updated
2955                     --4767922, only populating source_organization_id and source_sr_instance_id if p_supplier_id is NULL
2956                     source_organization_id = Decode(NVL(p_supplier_id,-1), -1, l_organization_id, NULL), --4767922
2957                     source_sr_instance_id = DEcode(NVL(p_supplier_id,-1), -1, l_instance_id, NULL), --4767922
2958                     -- End ATP4drp
2959                     -- Bug 3821358, Making the dates at the end of the day
2960                     new_ship_date            = TRUNC(NVL(p_ship_date,new_ship_date)) + MSC_ATP_PVT.G_END_OF_DAY,               -- Bug 3241766
2961                     new_wip_start_date       = TRUNC(NVL(p_start_date,new_wip_start_date)) + MSC_ATP_PVT.G_END_OF_DAY,         -- Bug 3241766
2962                     new_order_placement_date = TRUNC(NVL(p_order_date,new_order_placement_date)) + MSC_ATP_PVT.G_END_OF_DAY    -- Bug 3241766
2963 
2964             WHERE   plan_id = p_plan_id
2965             AND     transaction_id = l_transaction_id
2966             returning inventory_item_id, sr_instance_id, organization_id
2967             into    l_inventory_item_id, l_instance_id, l_organization_id;
2968         END IF;
2969         --e_cto_rearch
2970 
2971         IF PG_DEBUG in ('Y', 'C') THEN
2972             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_organization_id := '|| l_organization_id);
2973             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
2974             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_instance_id := ' || l_instance_id);
2975         END IF;
2976 
2977         -- time_phased_atp
2978         IF (p_mem_item_id <> p_pf_item_id) and (l_atf_date is not null) THEN
2979                 MSC_ATP_PF.Update_PF_Rollup_Supplies(
2980                         p_plan_id,
2981                         l_transaction_id,
2982                         p_mem_item_id,
2983                         p_pf_item_id,
2984                         p_date,
2985                         p_quantity,
2986                         l_atf_date,
2987                         l_return_status
2988                 );
2989                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2990                         IF PG_DEBUG in ('Y', 'C') THEN
2991                                 msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'Error occured in procedure Update_PF_Rollup_Supplies');
2992                         END IF;
2993                         RAISE FND_API.G_EXC_ERROR;
2994                 END IF;
2995 
2996         --  Allocated ATP Based on Planning Details -- Agilent changes Begin
2997         ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
2998             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
2999             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
3000             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
3001 
3002             IF PG_DEBUG in ('Y', 'C') THEN
3003                 msc_sch_wb.atp_debug('Update_Planned_Order: before update of' ||
3004                     ' msc_alloc_supplies');
3005             END IF;
3006 
3007             UPDATE  msc_alloc_supplies
3008             SET     old_supply_date = supply_date,
3009                     old_allocated_quantity = allocated_quantity,
3010                     supply_date = NVL(p_date, supply_date),
3011                     allocated_quantity = NVL(p_quantity, allocated_quantity),
3012                     LAST_UPDATED_BY = FND_GLOBAL.USER_ID,
3013                     LAST_UPDATE_DATE = sysdate
3014             WHERE   plan_id = p_plan_id
3015             AND     parent_transaction_id = l_transaction_id;
3016 
3017             IF PG_DEBUG in ('Y', 'C') THEN
3018                 msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'No of rows updated := ' || SQL%ROWCOUNT);
3019             END IF;
3020         END IF;
3021 
3022         --  Allocated ATP Based on Planning Details -- Agilent changes End
3023 
3024         -- Code to update summary records and committing removed for summary enhancement
3025         /** code commented for time being. Will be removed after code review
3026         IF l_summary_flag = 'Y' THEN
3027             IF PG_DEBUG in ('Y', 'C') THEN
3028                msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'in Summary mode, update planned orders');
3029             END IF;
3030             IF (p_date is NULL) THEN
3031 
3032                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3033                                               p_plan_id,
3034                                               l_organization_id,
3035                                               l_inventory_item_id,
3036                                               l_sd_date,
3037                                               null,
3038                                               null,
3039                                               null,
3040                                               null,
3041                                               null,
3042                                               null,
3043                                               2);
3044 
3045                 --- old and new dates are same
3046                 IF PG_DEBUG in ('Y', 'C') THEN
3047                    msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'old and new dates are same');
3048                 END IF;
3049                 update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* msc_atp_summary_sd
3050                 set sd_qty = sd_qty - l_sd_qty + p_quantity
3051                 where plan_id = p_plan_id
3052                 and   inventory_item_id = l_inventory_item_id
3053                 and   organization_id = l_organization_id
3054                 and   sd_date = trunc(l_sd_date)
3055                 and   sr_instance_id = l_instance_id;
3056 
3057                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3058                                               p_plan_id,
3059                                               l_organization_id,
3060                                               l_inventory_item_id,
3061                                               l_sd_date,
3062                                               null,
3063                                               null,
3064                                               null,
3065                                               null,
3066                                               null,
3067                                               null,
3068                                               2);
3069 
3070             ELSE
3071                IF PG_DEBUG in ('Y', 'C') THEN
3072                   msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'old and new dates are  not same');
3073                   msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'Update summary for old date');
3074                END IF;
3075 
3076                MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3077                                               p_plan_id,
3078                                               l_organization_id,
3079                                               l_inventory_item_id,
3080                                               l_sd_date,
3081                                               null,
3082                                               null,
3083                                               null,
3084                                               null,
3085                                               null,
3086                                               null,
3087                                               2);
3088 
3089                UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* MSC_ATP_SUMMARY_SD
3090                set sd_qty = (sd_qty - l_sd_qty)
3091                where sr_instance_id = l_instance_id and
3092                      inventory_item_id = l_inventory_item_id and
3093                      organization_id = l_organization_id and
3094                      sd_date = trunc(l_sd_date) and
3095                      plan_id = p_plan_id;
3096 
3097                MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3098                                               p_plan_id,
3099                                               l_organization_id,
3100                                               l_inventory_item_id,
3101                                               l_sd_date,
3102                                               null,
3103                                               null,
3104                                               null,
3105                                               null,
3106                                               null,
3107                                               null,
3108                                               2);
3109 
3110 
3111                --- if record exists then update
3112                commit;
3113                IF PG_DEBUG in ('Y', 'C') THEN
3114                   msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'update the qty on new date ');
3115                END IF;
3116 
3117                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3118                                               p_plan_id,
3119                                               l_organization_id,
3120                                               l_inventory_item_id,
3121                                               p_date,
3122                                               null,
3123                                               null,
3124                                               null,
3125                                               null,
3126                                               null,
3127                                               null,
3128                                               2);
3129 
3130                update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* msc_atp_summary_sd
3131                set sd_qty = (sd_qty + nvl(p_quantity, l_sd_qty))
3132                where plan_id = p_plan_id and
3133     		 sr_instance_id = l_instance_id and
3134                      inventory_item_id = l_inventory_item_id and
3135                      organization_id = l_organization_id and
3136                      sd_date = trunc(p_date);
3137 
3138                IF SQL%NOTFOUND THEN
3139                    --  record doesn't exists. insert the record
3140                   IF PG_DEBUG in ('Y', 'C') THEN
3141                      msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'insert qty on new date');
3142                   END IF;
3143 
3144                    MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
3145                                                        l_instance_id,
3146                                                        l_organization_id,
3147                                                        l_inventory_item_id,
3148                                                        p_date,
3149                                                        NVL(p_quantity,l_sd_qty),
3150                                                        '@@@');
3151                END IF; --- if sql%notfound
3152                commit;
3153             END IF; --- if p_date is null;
3154 
3155             commit;
3156         END IF; -- if summary flag
3157         **/
3158 
3159         BEGIN
3160 
3161             -- we need to get the plan_id since this plan order and it's plan
3162             -- order demand may belong to different plans.
3163 
3164             SELECT  identifier3, pegging_id, NVL(identifier2, p_plan_id), supply_demand_date,
3165                     supply_demand_quantity, organization_id, supplier_atp_date, summary_flag
3166             INTO    l_demand_id, l_child_pegging_id, l_plan_id, l_sd_date, l_sd_qty, l_organization_id,
3167                     l_supplier_atp_date, l_summary_flag
3168             FROM    mrp_atp_details_temp
3169             WHERE   parent_pegging_id = p_pegging_id
3170             AND     record_type = 3
3171             AND     session_id = MSC_ATP_PVT.G_SESSION_ID;
3172 
3173         EXCEPTION
3174             WHEN others THEN
3175                 l_demand_id := NULL;
3176                 l_child_pegging_id := NULL;
3177                 l_plan_id := NULL;
3178         END ;
3179 
3180         IF PG_DEBUG in ('Y', 'C') THEN
3181             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_demand_id := '|| l_demand_id);
3182             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_plan_id := '|| l_plan_id);
3183             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_sd_date :=' || l_sd_date);
3184             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_sd_qty :=' || l_sd_qty);
3185             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_organization_id := ' || l_organization_id);
3186             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_instance_id := ' || l_instance_id);
3187             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_supplier_atp_date := ' || l_supplier_atp_date);
3188             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_summary_flag := ' || l_summary_flag);
3189         END IF;
3190 
3191         --- update supplier info
3192         IF l_demand_id is not null AND p_quantity IS NOT NULL THEN
3193 
3194             IF PG_DEBUG in ('Y', 'C') THEN
3195                 msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'after 4');
3196             END IF;
3197             l_inventory_item_id := null;
3198 
3199             -- dsting diag_atp do not adjust demands
3200             IF MSC_ATP_PVT.G_DIAGNOSTIC_ATP <> 1 THEN
3201 
3202                 l_prim_uom_dmd_qty := p_quantity * nvl(p_uom_conv_rate, 1);
3203 
3204                 UPDATE  msc_demands
3205                 SET     USING_REQUIREMENT_QUANTITY = MSC_ATP_UTILS.Truncate_Demand(l_prim_uom_dmd_qty),
3206                         -- 24x7		-- 5598066
3207                         atp_synchronization_flag = 0
3208                 WHERE   demand_id = l_demand_id
3209                 AND     plan_id = l_plan_id
3210                 returning sr_instance_id, inventory_item_id into l_instance_id, l_inventory_item_id;
3211 
3212                 IF PG_DEBUG in ('Y', 'C') THEN
3213                     msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'No of rows update := ' || SQL%ROWCOUNT);
3214                     msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_instance_id := ' || l_instance_id);
3215                     msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
3216                 END IF;
3217 
3218                 -- dsting 2754446. do i really want to do this
3219                 IF l_prim_uom_dmd_qty <> l_sd_qty THEN
3220                     MSC_ATP_DB_UTILS.update_pegging(l_child_pegging_id, null, l_prim_uom_dmd_qty);
3221                 END IF;
3222 
3223                 -- time_phased_atp
3224                 IF ((p_mem_item_id <> p_pf_item_id) and (l_atf_date is not null)) THEN
3225                         MSC_ATP_PF.Update_PF_Bucketed_Demands(
3226                                 l_plan_id,
3227                                 l_demand_id,
3228                                 l_sd_date,
3229                                 l_atf_date,
3230                                 l_po_qty,
3231                                 l_prim_uom_dmd_qty,       --bug 7361001
3232                                 l_return_status
3233                         );
3234                         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3235                                 IF PG_DEBUG in ('Y', 'C') THEN
3236                                         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'Error occured in procedure Update_PF_Bucketed_Demands');
3237                                 END IF;
3238                                 RAISE FND_API.G_EXC_ERROR;
3239                         END IF;
3240 
3241                 --  Allocated ATP Based on Planning Details -- Agilent changes Begin
3242                 ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
3243                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
3244                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
3245                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
3246 
3247                     IF PG_DEBUG in ('Y', 'C') THEN
3248                         msc_sch_wb.atp_debug('Update_Planned_Order: before update of' ||
3249                             ' msc_alloc_demands');
3250                     END IF;
3251                     UPDATE msc_alloc_demands
3252                     SET    allocated_quantity = MSC_ATP_UTILS.Truncate_Demand(l_prim_uom_dmd_qty)  --5598066
3253                     WHERE  parent_demand_id = l_demand_id
3254                     AND    plan_id = l_plan_id;
3255 
3256                     IF PG_DEBUG in ('Y', 'C') THEN
3257                         msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'No of rows updated := ' || SQL%ROWCOUNT);
3258                     END IF;
3259 
3260                 END IF;
3261 
3262                 --  Allocated ATP Based on Planning Details -- Agilent changes End
3263 
3264                 -- Code to update summary records and committing removed for summary enhancement
3265                 /** code commented for time being. Will be removed after code review
3266                 IF l_summary_flag = 'Y' THEN
3267 
3268                   IF PG_DEBUG in ('Y', 'C') THEN
3269                      msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'Summary Mode- update planned order demand');
3270                   END IF;
3271                   MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(l_instance_id,
3272                                                   l_plan_id,
3273                                                   l_organization_id,
3274                                                   l_inventory_item_id,
3275                                                   l_sd_date,
3276                                                   null,
3277                                                   null,
3278                                                   null,
3279                                                   null,
3280                                                   null,
3281                                                   null,
3282                                                   2);
3283 
3284                   UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* MSC_ATP_SUMMARY_SD
3285                   -- dsting 2754446
3286                   SET sd_qty = sd_qty + l_sd_qty - l_prim_uom_dmd_qty
3287                 --          SET sd_qty = sd_qty + l_sd_qty - p_quantity
3288                   where plan_id = l_plan_id
3289                   and   sr_instance_id = l_instance_id
3290                   and   organization_id = l_organization_id
3291                   and   inventory_item_id = l_inventory_item_id
3292                   and   sd_date = trunc(l_sd_date);
3293                   commit;
3294                 END IF;
3295                 **/
3296             END IF; -- if not DIAGNOSTIC ATP then update demands
3297 	      --5239441
3298         ELSIF p_quantity IS NOT NULL and NVL(MSC_ATP_PVT.G_PLAN_INFO_REC.plan_type, 1) = 5 THEN
3299                 IF PG_DEBUG in ('Y', 'C') THEN
3300             msc_sch_wb.atp_debug('Update_Planned_Order: ' || 'l_demand_id :=  '|| l_demand_id);
3301             END IF;
3302             l_prim_uom_dmd_qty := p_quantity * nvl(p_uom_conv_rate, 1);
3303             IF l_prim_uom_dmd_qty <> l_sd_qty THEN
3304                MSC_ATP_DB_UTILS.UPDATE_PEGGING(l_child_pegging_id, null, l_prim_uom_dmd_qty);
3305             END IF;
3306         END IF;
3307 
3308     END IF;
3309 
3310     IF PG_DEBUG in ('Y', 'C') THEN
3311         msc_sch_wb.atp_debug('***** END Update_Planned_Order Procedure *****');
3312     END IF;
3313 EXCEPTION
3314     WHEN no_data_found THEN
3315         null;
3316         IF PG_DEBUG in ('Y', 'C') THEN
3317             msc_sch_wb.atp_debug('No Data Found in Update_Planned_Order');
3318         END IF;
3319 END Update_Planned_Order;
3320 
3321 PROCEDURE Update_SD_Date(p_identifier           IN NUMBER,
3322                          p_instance_id          IN NUMBER,
3323                          p_supply_demand_date   IN DATE,
3324                          p_plan_id              IN NUMBER,
3325                          p_supply_demand_qty    IN NUMBER, -- Bug 1501787
3326                          p_dc_flag              IN NUMBER,
3327                          p_old_demand_date      IN DATE,
3328                          p_old_demand_qty       IN NUMBER,
3329                          p_dmd_satisfied_date   IN DATE, -- bug 2795053-reopen
3330                          p_sd_date_quantity     IN NUMBER,   -- For time_phased_atp
3331                          p_atf_date             IN DATE,     -- For time_phased_atp
3332                          p_atf_date_quantity    IN NUMBER,   -- For time_phased_atp
3333                          p_sch_arrival_date     IN DATE,     -- For ship_rec_cal
3334                          p_order_date_type      IN NUMBER,   -- For ship_rec_cal
3335                          p_lat_date             IN DATE,     -- For ship_rec_cal
3336                          p_ship_set_name        IN VARCHAR2, -- plan by request date
3337                          p_arrival_set_name     IN VARCHAR2, -- plan by request date
3338                          p_override_flag        IN VARCHAR2,   -- plan by request date
3339                          p_request_arrival_date IN DATE,     -- plan by request date
3340                          p_bkwd_pass_atf_date_qty IN NUMBER,    -- For time_phased_atp bug3397904
3341                          p_atp_rec              IN MRP_ATP_PVT.AtpRec := NULL -- For bug 3226083
3342 )
3343 IS
3344     --bug 2465088: increase size of l_demand class from 25 to 30 characters
3345     l_demand_class      varchar2(30);
3346     l_sd_qty            number;
3347     l_sd_date           date;
3348     l_org_id            number;
3349     l_inventory_item_id number;
3350     l_count             number;
3351     temp_sd_qty         number;
3352     l_demand_id         number;
3353     l_summary_flag      varchar2(1);
3354 
3355     -- time_phased_atp
3356     l_bucketed_demands_rec      MSC_ATP_PF.Bucketed_Demands_Rec;
3357     l_return_status             varchar2(1);
3358 		l_alloc_atp VARCHAR2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
3359 
3360 BEGIN
3361 
3362     IF PG_DEBUG in ('Y', 'C') THEN
3363         msc_sch_wb.atp_debug('**********Begin Update_SD_Date Procedure************');
3364         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_supply_demand_date := ' || p_supply_demand_date);
3365         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_supply_demand_quantity := ' || p_supply_demand_qty);
3366         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_plan_id := ' || p_plan_id);
3367         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_old_demand_date := ' || p_old_demand_date);
3368         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_old_demand_qty := ' || p_old_demand_qty);
3369         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_instance_id := ' || p_instance_id);
3370         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_dc_flag := ' || p_dc_flag);
3371         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_identifier := ' || p_identifier);
3372         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'p_atf_date := ' || p_atf_date);
3373     END IF;
3374 
3375     IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
3376 		MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
3377 	END IF;
3378 	-- dsting ATO 2465370
3379 
3380 	BEGIN
3381         UPDATE mrp_atp_details_temp
3382         --bug 3328421
3383         --SET    required_date = TRUNC(NVL(p_supply_demand_date, required_date)) + MSC_ATP_PVT.G_END_OF_DAY
3384         SET    actual_supply_demand_date = TRUNC(NVL(p_supply_demand_date, actual_supply_demand_date)) + MSC_ATP_PVT.G_END_OF_DAY
3385         WHERE  pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
3386         AND    session_id = MSC_ATP_PVT.G_SESSION_ID
3387         AND    record_type = 3
3388         RETURNING identifier3, summary_flag INTO l_demand_id, l_summary_flag;
3389     EXCEPTION
3390         WHEN OTHERS THEN
3391             l_demand_id := null;
3392             l_summary_flag := 'N';
3393             RETURN;
3394     END;
3395 
3396     IF PG_DEBUG in ('Y','C') THEN
3397         msc_sch_wb.atp_debug('ATO update pegging id ' || MSC_ATP_PVT.G_DEMAND_PEGGING_ID);
3398     END IF;
3399 
3400     IF p_plan_id = -1  THEN
3401         -- ngoel, changed for performance reason, don't
3402         -- do anything in case p_supply_demand_date is NULL.
3403         IF p_supply_demand_date IS NOT NULL THEN
3404             -- For bug 2259824, move the demand to the end of day
3405             UPDATE  MSC_SALES_ORDERS
3406             SET     REQUIREMENT_DATE            = TRUNC(p_supply_demand_date) + MSC_ATP_PVT.G_END_OF_DAY,
3407                     REQUEST_DATE                = TRUNC(p_request_arrival_date),--plan by request date
3408                     SCHEDULE_ARRIVAL_DATE       = TRUNC(p_sch_arrival_date) + MSC_ATP_PVT.G_END_OF_DAY,    --plan by request date
3409                     LATEST_ACCEPTABLE_DATE      = p_lat_date,
3410                     ORDER_DATE_TYPE_CODE        = p_order_date_type,
3411                     SHIP_SET_NAME               = p_ship_set_name,              --plan by request date
3412                     ARRIVAL_SET_NAME            = p_arrival_set_name,           --plan by request date
3413                     ATP_OVERRIDE_FLAG           = decode(upper(p_override_flag),'Y',1,2),              --plan by request date
3414                     PROMISE_DATE                = TRUNC(p_sch_arrival_date)     --plan by request date
3415             WHERE   SR_INSTANCE_ID = p_instance_id
3416             AND     DEMAND_SOURCE_LINE = to_char(p_identifier)
3417                     -- dsting ATO 2465370 I already selected l_demand_id
3418             AND     DEMAND_ID = l_demand_id
3419             returning demand_class, inventory_item_id into l_demand_class, l_inventory_item_id;
3420 
3421             IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') AND NVL(l_inventory_item_id, 0) > 0
3422                 AND G_ORIG_INV_CTP = 5 THEN     -- Condition added for bug 3295831 because summary
3423                                                 -- is not supported in PDS-ODS switch.
3424                 IF PG_DEBUG in ('Y', 'C') THEN
3425                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'Summary Flag is on');
3426                 END IF;
3427                 --- get the old quantity and other info from pegging
3428                 BEGIN
3429                     --- G_DEMAND_PEGGING_ID wil be null for non atpbale items
3430                     select supply_demand_quantity, supply_demand_date, organization_id
3431                     into   l_sd_qty, l_sd_date, l_org_id
3432                     from   mrp_atp_details_temp
3433                     where pegging_id = MSC_ATP_PVT.G_DEMAND_PEGGING_ID
3434                     and   session_id = MSC_ATP_PVT.G_SESSION_ID
3435                     and   record_type = 3;
3436                 EXCEPTION
3437                     WHEN OTHERS THEN
3438                         l_sd_qty := null;
3439                         l_sd_date := null;
3440                         l_org_id := null;
3441                         RETURN; -- We dont do anything for itmes which do not have pegging (non atpable items)
3442                 END;
3443                 ----update the quantity on old date
3444 
3445                 IF PG_DEBUG in ('Y', 'C') THEN
3446                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
3447                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_demand_class := ' || l_demand_class);
3448                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_sd_qty := ' || l_sd_qty);
3449                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_sd_date := ' || l_sd_date);
3450                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_org_id := ' || l_org_id);
3451                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'Update summary for old date');
3452                 END IF;
3453 
3454                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3455                                                    -1,
3456                                                    l_org_id,
3457                                                    l_inventory_item_id,
3458                                                    l_sd_date,
3459                                                    null,
3460                                                    null,
3461                                                    null,
3462                                                    null,
3463                                                    p_dc_flag,
3464                                                    l_demand_class,
3465                                                    1);
3466 
3467 
3468                 UPDATE  /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ MSC_ATP_SUMMARY_SO
3469                 set     sd_qty = (sd_qty - l_sd_qty)
3470                 where   sr_instance_id = p_instance_id and
3471                         inventory_item_id = l_inventory_item_id and
3472                         organization_id = l_org_id and
3473                         sd_date = trunc(l_sd_date) and
3474                         demand_class = decode(p_dc_flag, 1, NVL(l_demand_class, '@@@'), '@@@');
3475                 commit;
3476                 ---check if record for new date exists in summary table
3477 
3478                 --- if record exists then update
3479                 IF PG_DEBUG in ('Y', 'C') THEN
3480                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'update the qty on new date ');
3481                 END IF;
3482 
3483                 MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3484                                                    -1,
3485                                                    l_org_id,
3486                                                    l_inventory_item_id,
3487                                                    p_supply_demand_date,
3488                                                    null,
3489                                                    null,
3490                                                    null,
3491                                                    null,
3492                                                    p_dc_flag,
3493                                                    l_demand_class,
3494                                                    1);
3495 
3496                 update  /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
3497                 set     sd_qty = sd_qty + l_sd_qty
3498                 where   sr_instance_id = p_instance_id and
3499                         inventory_item_id = l_inventory_item_id and
3500                         organization_id = l_org_id and
3501                         sd_date = trunc(p_supply_demand_date) and
3502                         demand_class = decode(p_dc_flag, 1, NVL(l_demand_class, '@@@'), '@@@');
3503 
3504                 IF SQL%NOTFOUND THEN
3505                     --  record doesn't exists. insert the record
3506                     IF PG_DEBUG in ('Y', 'C') THEN
3507                         msc_sch_wb.atp_debug('Update_SD_Date: ' || 'insert qty on new date');
3508                     END IF;
3509                     BEGIN
3510                         insert /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ into msc_atp_summary_so
3511                                 (plan_id,
3512                                 inventory_item_id,
3513                                 organization_id,
3514                                 sr_instance_id,
3515                                 sd_date,
3516                                 sd_qty,
3517                                 demand_class,
3518                                 LAST_UPDATE_DATE,
3519                                 LAST_UPDATED_BY,
3520                                 CREATION_DATE,
3521                                 CREATED_BY
3522                                 )
3523                         VALUES
3524                                 (-1,
3525                                 l_inventory_item_id,
3526                                 l_org_id,
3527                                 p_instance_id,
3528                                 trunc(p_supply_demand_date),
3529                                 l_sd_qty,
3530                                 Decode(p_dc_flag, 1, NVL(l_demand_class, '@@@') ,'@@@'),
3531                                 sysdate,
3532                                 FND_GLOBAL.USER_ID,
3533                                 sysdate,
3534                                 FND_GLOBAL.USER_ID);
3535                     EXCEPTION
3536                         WHEN  DUP_VAL_ON_INDEX THEN
3537                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3538                                                                -1,
3539                                                                l_org_id,
3540                                                                l_inventory_item_id,
3541                                                                p_supply_demand_date,
3542                                                                null,
3543                                                                null,
3544                                                                null,
3545                                                                null,
3546                                                                p_dc_flag,
3547                                                                l_demand_class,
3548                                                                1);
3549 
3550 
3551                             update /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
3552                             set sd_qty = sd_qty + l_sd_qty
3553                             where inventory_item_id = l_inventory_item_id
3554                             and sr_instance_id = p_instance_id
3555                             and organization_id = l_org_id
3556                             and sd_date = trunc(p_supply_demand_date)
3557                             and demand_class = Decode(p_dc_flag, 1, NVL(l_demand_class, '@@@'),'@@@') ;
3558 
3559                             MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3560                                                                -1,
3561                                                                l_org_id,
3562                                                                l_inventory_item_id,
3563                                                                p_supply_demand_date,
3564                                                                null,
3565                                                                null,
3566                                                                null,
3567                                                                null,
3568                                                                p_dc_flag,
3569                                                                l_demand_class,
3570                                                                1);
3571 
3572                     END;
3573 
3574                 END IF;
3575                 commit;
3576             END IF;
3577         END IF;
3578     ELSE    -- IF p_plan_id = -1  THEN
3579         -- ngoel 2/13/2001, changed for performance reason, don't
3580         -- do anything in case p_supply_demand_date and p_supply_demand_qty is NULL.
3581         IF (p_supply_demand_qty IS NOT NULL OR p_supply_demand_date IS NOT NULL) THEN
3582 
3583             -- For bug 2259824, move the demand to the end of day
3584             IF PG_DEBUG in ('Y', 'C') THEN
3585             msc_sch_wb.atp_debug('Inside If of else p_plan_id = -1'); --bug3397904
3586             END IF;
3587 
3588             UPDATE  MSC_DEMANDS
3589             SET
3590             	    --start changes for plan by request date
3591                     SCHEDULE_SHIP_DATE          = TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3592                     USING_ASSEMBLY_DEMAND_DATE  =
3593                         decode(ORIGINATION_TYPE,
3594                                6,  decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
3595                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
3596                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3597                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
3598                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3599                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
3600                                               TRUNC(NVL(p_supply_demand_date,PROMISE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3601                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
3602                                               TRUNC(NVL(p_supply_demand_date,PROMISE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3603                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
3604                                               REQUEST_SHIP_DATE,
3605                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
3606                                               REQUEST_SHIP_DATE,
3607                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY),
3608                                30, decode(MSC_ATP_PVT.G_PLAN_INFO_REC.schedule_by_date_type,
3609                                           MSC_ATP_PVT.G_SCHEDULE_SHIP_DATE_LEGEND,
3610                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3611                                           MSC_ATP_PVT.G_SCHEDULE_ARRIVAL_DATE_LEGEND,
3612                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3613                                           MSC_ATP_PVT.G_PROMISE_SHIP_DATE_LEGEND,
3614                                               TRUNC(NVL(p_supply_demand_date,PROMISE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3615                                           MSC_ATP_PVT.G_PROMISE_ARRIVAL_DATE_LEGEND,
3616                                               TRUNC(NVL(p_supply_demand_date,PROMISE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3617                                           MSC_ATP_PVT.G_REQUEST_SHIP_DATE_LEGEND,
3618                                               REQUEST_SHIP_DATE,
3619                                           MSC_ATP_PVT.G_REQUEST_ARRIVAL_DATE_LEGEND,
3620                                               REQUEST_SHIP_DATE,
3621                                               TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY),
3622                                TRUNC(NVL(p_supply_demand_date,USING_ASSEMBLY_DEMAND_DATE)) + MSC_ATP_PVT.G_END_OF_DAY), --plan by request date
3623                     promise_ship_date           = TRUNC(NVL(p_supply_demand_date,SCHEDULE_SHIP_DATE)) + MSC_ATP_PVT.G_END_OF_DAY, --plan by request date
3624                     request_date                = p_request_arrival_date, --plan by request date
3625                     promise_date                = p_sch_arrival_date,     --plan by request date
3626                     ship_set_name               = p_ship_set_name,        --plan by request date
3627                     arrival_set_name            = p_arrival_set_name,     --plan by request date
3628                      atp_override_flag           = decode(upper(p_override_flag),'Y',1,2), --plan by request date
3629                     --end changes for plan by request date
3630                     USING_REQUIREMENT_QUANTITY =  MSC_ATP_UTILS.Truncate_Demand(NVL(p_supply_demand_qty,USING_REQUIREMENT_QUANTITY)), -- Bug 1501787
3631                     -- 24x7 -- 5598066
3632                     ATP_SYNCHRONIZATION_FLAG = 0,
3633                     -- bug 2795053-reopen (ssurendr) update the demand_satisfied_date in msc_demands
3634                     DMD_SATISFIED_DATE = TRUNC(GREATEST(p_dmd_satisfied_date,DMD_SATISFIED_DATE)) + MSC_ATP_PVT.G_END_OF_DAY,
3635                     -- ship_rec_cal changes begin
3636                     SCHEDULE_ARRIVAL_DATE = NVL(p_sch_arrival_date, SCHEDULE_ARRIVAL_DATE),
3637                     ORDER_DATE_TYPE_CODE = NVL(p_order_date_type, ORDER_DATE_TYPE_CODE),
3638                     LATEST_ACCEPTABLE_DATE = NVL(p_lat_date, LATEST_ACCEPTABLE_DATE)
3639                     -- ship_rec_cal changes end
3640             WHERE   PLAN_ID = p_plan_id
3641             AND     DEMAND_ID = l_demand_id
3642             returning inventory_item_id, organization_id into l_inventory_item_id, l_org_id ;
3643 
3644             -- time_phased_atp changes begin
3645             IF p_atf_date is not null THEN
3646                 MSC_ATP_PF.Move_PF_Bucketed_Demands(
3647                         p_plan_id,
3648                         l_demand_id,
3649                         p_old_demand_date,
3650                         p_supply_demand_date,
3651                         p_supply_demand_qty,
3652                         p_sd_date_quantity,
3653                         p_atf_date,
3654                         p_atf_date_quantity,
3655                         l_return_status,
3656                         p_bkwd_pass_atf_date_qty, --bug3397904
3657                         p_atp_rec -- For bug 3226083
3658                 );
3659                 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3660                         IF PG_DEBUG in ('Y', 'C') THEN
3661                                 msc_sch_wb.atp_debug('Update_SD_Date: ' || 'Error occured in procedure Move_PF_Bucketed_Demands');
3662                         END IF;
3663                         RAISE FND_API.G_EXC_ERROR;
3664                 END IF;
3665             -- time_phased_atp changes end
3666 
3667             --  Allocated ATP Based on Planning Details -- Agilent changes Begin
3668             ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
3669                 (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
3670                 (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
3671                 (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
3672 
3673                 IF PG_DEBUG in ('Y', 'C') THEN
3674                     msc_sch_wb.atp_debug('Update_SD_Date: before update of ' ||
3675                         ' msc_alloc_demands');
3676                 END IF;
3677 
3678                 UPDATE  MSC_ALLOC_DEMANDS
3679                 SET     ALLOCATED_QUANTITY = MSC_ATP_UTILS.Truncate_Demand(NVL(p_supply_demand_qty,
3680                         ALLOCATED_QUANTITY)),	--5598066
3681                         DEMAND_DATE = NVL(p_supply_demand_date, DEMAND_DATE),
3682                         LAST_UPDATED_BY = FND_GLOBAL.USER_ID,
3683                         LAST_UPDATE_DATE = sysdate
3684                 WHERE   PLAN_ID = p_plan_id
3685                 AND     PARENT_DEMAND_ID = l_demand_id
3686                 AND     INVENTORY_ITEM_ID = l_inventory_item_id
3687                 AND     ORGANIZATION_ID = l_org_id;
3688 
3689                 IF PG_DEBUG in ('Y', 'C') THEN
3690                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'No of rows updated := ' || SQL%ROWCOUNT);
3691                 END IF;
3692 
3693             END IF;
3694 
3695             --  Allocated ATP Based on Planning Details -- Agilent changes End
3696 
3697             -- Code to update summary records and committing removed for summary enhancement
3698             /** code commented for time being. Will be removed after code review
3699             IF l_summary_flag = 'Y' AND NVL(l_inventory_item_id, 0) > 0 THEN
3700 
3701               IF PG_DEBUG in ('Y', 'C') THEN
3702                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'Summary Flag is on');
3703               END IF;
3704               --update the quantity on old date
3705 
3706               IF PG_DEBUG in ('Y', 'C') THEN
3707                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_inventory_item_id := ' || l_inventory_item_id);
3708                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_demand_class := ' || l_demand_class);
3709                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_sd_qty := ' || p_old_demand_date);
3710                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_sd_date := ' || p_old_demand_qty);
3711                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'l_org_id := ' || l_org_id);
3712                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'Update summary for old date');
3713               END IF;
3714 
3715               MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3716                                           p_plan_id,
3717                                           l_org_id,
3718                                           l_inventory_item_id,
3719                                           p_old_demand_date,
3720                                           null,
3721                                           null,
3722                                           null,
3723                                           null,
3724                                           null,
3725                                           null,
3726                                           2);
3727 
3728               UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* MSC_ATP_SUMMARY_SD
3729               set sd_qty = (sd_qty + p_old_demand_qty)
3730               where sr_instance_id = p_instance_id and
3731                     inventory_item_id = l_inventory_item_id and
3732                     organization_id = l_org_id and
3733                     sd_date = trunc(p_old_demand_date) and
3734                     plan_id = p_plan_id;
3735 
3736               IF PG_DEBUG in ('Y', 'C') THEN
3737                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'No of rows update := ' || SQL%ROWCOUNT);
3738               END IF;
3739               commit;
3740               MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3741                                           p_plan_id,
3742                                           l_org_id,
3743                                           l_inventory_item_id,
3744                                           p_old_demand_date,
3745                                           null,
3746                                           null,
3747                                           null,
3748                                           null,
3749                                           null,
3750                                           null,
3751                                           2);
3752 
3753               --- if record exists then update
3754               IF PG_DEBUG in ('Y', 'C') THEN
3755                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'update the qty on new date ');
3756               END IF;
3757               MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
3758                                           p_plan_id,
3759                                           l_org_id,
3760                                           l_inventory_item_id,
3761                                           p_supply_demand_date,
3762                                           null,
3763                                           null,
3764                                           null,
3765                                           null,
3766                                           null,
3767                                           null,
3768                                           2);
3769 
3770               update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* msc_atp_summary_sd
3771               set sd_qty = sd_qty - NVL(p_supply_demand_qty, p_old_demand_qty)
3772               where plan_id = p_plan_id and
3773                     sr_instance_id = p_instance_id and
3774                     inventory_item_id = l_inventory_item_id and
3775                     organization_id = l_org_id and
3776                     sd_date = trunc(p_supply_demand_date);
3777               IF PG_DEBUG in ('Y', 'C') THEN
3778                  msc_sch_wb.atp_debug('Update_SD_Date: ' || 'No of rows update := ' || SQL%ROWCOUNT);
3779               END IF;
3780 
3781               IF SQL%NOTFOUND THEN
3782                   --  record doesn't exists. insert the record
3783                  IF PG_DEBUG in ('Y', 'C') THEN
3784                     msc_sch_wb.atp_debug('Update_SD_Date: ' || 'insert qty on new date');
3785                  END IF;
3786 
3787                    MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_plan_id,
3788                                                    p_instance_id,
3789                                                    l_org_id,
3790                                                    l_inventory_item_id,
3791                                                    p_supply_demand_date,
3792                                                    -1 * NVL(p_supply_demand_qty, p_old_demand_qty),
3793                                                    '@@@');
3794               END IF; --- if sql%notfound
3795               commit;
3796             END IF; -- if sumamry_flag = 'Y'
3797             **/
3798         END IF; -- if p_summly_demand_qty is not null or....
3799     END IF ; -- if plan_id = -1
3800 
3801     IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
3802 		MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
3803 	END IF;
3804 	-- commit removed for PDS cases - summary enhancement
3805     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') and p_plan_id = -1 AND G_ORIG_INV_CTP = 5 THEN
3806        -- Condition for INV_CTP added for bug 3295831 because summary
3807        -- is not supported in PDS-ODS switch.
3808        commit;
3809     END IF;
3810     IF PG_DEBUG in ('Y', 'C') THEN
3811         msc_sch_wb.atp_debug('**********End Update_SD_Date Procedure************');
3812     END IF;
3813 EXCEPTION
3814     WHEN OTHERS THEN
3815         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3816             FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME , 'Update_SD_Date');
3817         END IF;
3818 
3819         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3820 END Update_SD_Date;
3821 
3822 
3823 -- NGOEL 7/26/2001, Bug 1661545, if scheduling was unsuccessful, make sure that old demand record is
3824 -- preserved back, as it was updated to 0 in the begining in case of reschedule in case of PDS.
3825 
3826 -- RAJJAIN 11/01/2002, Now schedule procedure passes reference to del_demand_ids array to this
3827 -- procedure
3828 PROCEDURE Undo_Delete_Row(p_identifiers            IN   MRP_ATP_PUB.Number_Arr,
3829                           p_plan_ids               IN   MRP_ATP_PUB.Number_Arr,
3830                           p_instance_id            IN   NUMBER,
3831                           p_del_demand_ids         IN   MRP_ATP_PUB.Number_Arr,
3832                           p_inv_item_ids           IN   MRP_ATP_PUB.Number_Arr,
3833                           p_copy_demand_ids        IN   MRP_ATP_PUB.Number_Arr, -- For summary enhancement
3834                           p_copy_plan_ids          IN   MRP_ATP_PUB.Number_Arr, -- For summary enhancement
3835                           p_time_phased_set        IN   VARCHAR2,               -- For time_phased_atp
3836                           -- CTO ODR and Simplified Pegging
3837                           p_del_atp_peg_items      IN   MRP_ATP_PUB.Number_Arr,
3838                           p_del_atp_peg_demands    IN   MRP_ATP_PUB.Number_Arr,
3839                           p_del_atp_peg_supplies   IN   MRP_ATP_PUB.Number_Arr,
3840                           p_del_atp_peg_res_reqs   IN   MRP_ATP_PUB.Number_Arr,
3841                           p_demand_source_type     IN   MRP_ATP_PUB.Number_Arr,  --cmro
3842                           p_atp_peg_demands_plan_ids  IN MRP_ATP_PUB.Number_Arr, --Bug 3629191
3843                           p_atp_peg_supplies_plan_ids IN MRP_ATP_PUB.Number_Arr, --Bug 3629191
3844                           p_atp_peg_res_reqs_plan_ids IN MRP_ATP_PUB.Number_Arr, --Bug 3629191
3845                           p_del_ods_demand_ids         IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3846                           p_del_ods_inv_item_ids       IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3847                           p_del_ods_demand_src_type    IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3848                           p_del_ods_cto_demand_ids     IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3849                           p_del_ods_cto_inv_item_ids   IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3850                           p_del_ods_cto_dem_src_type   IN MRP_ATP_PUB.Number_Arr, --3720018, added for support of rescheduling in ODS
3851                           p_del_ods_atp_refresh_no     IN MRP_ATP_PUB.Number_Arr,
3852                           p_del_ods_cto_atp_refresh_no IN MRP_ATP_PUB.Number_Arr
3853                           -- End CTO ODR and Simplified Pegging
3854 )
3855 
3856 IS
3857     l_del_rows	        NUMBER;
3858     i                   NUMBER;
3859     --l_identifiers	MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3860     --l_plan_ids	MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3861     --l_instance_ids	MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3862 
3863     --rajjain 11/01/2002
3864     m                   PLS_INTEGER := 1;
3865 
3866     -- For bug 2738280.
3867     l_inventory_item_id	            MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3868     l_using_assembly_demand_date    MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
3869     l_using_requirement_quantity    MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3870     l_organization_id               MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3871     l_plan_id                       MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
3872 
3873     -- CTO ODR and Simplified Pegging
3874     l_return_status         VARCHAR2(1);
3875 	l_alloc_atp VARCHAR2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES -12973673
3876 
3877 BEGIN
3878     IF PG_DEBUG in ('Y', 'C') THEN
3879         msc_sch_wb.atp_debug('**********Begin Undo_Delete_Row Procedure************');
3880     END IF;
3881 
3882 	IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
3883 		MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_PVT.G_ORIG_ALLOC_ATP;
3884 	END IF;
3885 
3886     --3720018, reverting back incase, rescheduling fails
3887     IF (p_del_ods_demand_ids IS NOT NULL AND p_del_ods_demand_ids.count > 0) THEN
3888       FOR m in 1..p_del_ods_demand_ids.count LOOP
3889         Update msc_sales_orders
3890                set Primary_uom_quantity = MSC_ATP_UTILS.Truncate_Demand(Old_primary_uom_quantity), --5598066
3891                reservation_quantity = MSC_ATP_UTILS.Truncate_Demand(old_reservation_quantity), --5598066
3892                inventory_item_id = p_del_ods_inv_item_ids(m),
3893                atp_refresh_number = p_del_ods_atp_refresh_no(m)
3894         WHERE  sr_instance_id = p_instance_id
3895         AND    demand_id = p_del_ods_demand_ids(m)
3896         AND    decode(demand_source_type,100,demand_source_type,-1)
3897                             =decode(p_del_ods_demand_src_type(m),
3898                                                 100,
3899                                                 p_del_ods_demand_src_type(m),
3900                                                 -1);
3901       END LOOP;
3902       IF PG_DEBUG in ('Y', 'C') THEN
3903           msc_sch_wb.atp_debug('Number of SO lines updated := ' || SQL%ROWCOUNT);
3904       END IF;
3905       --5357370
3906       IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
3907          IF PG_DEBUG in ('Y', 'C') THEN
3908             msc_sch_wb.atp_debug('Inside summary mode set to yes');
3909             msc_sch_wb.atp_debug('p_identifiers.count := ' || p_identifiers.count);
3910          END IF;
3911          FOR m in 1..p_identifiers.count LOOP
3912             MSC_ATP_DB_UTILS.UNDO_DELETE_SUMMARY_ROW(p_identifiers(m),
3913                                                      p_instance_id,
3914                                                      p_del_ods_demand_src_type(m));
3915          END LOOP;
3916       END IF;
3917     END IF;
3918 
3919     --3720018, reverting back in case, rescheduling fails
3920     IF (p_del_ods_cto_demand_ids IS NOT NULL AND p_del_ods_cto_demand_ids.count > 0) THEN
3921       FOR m in 1..p_del_ods_cto_demand_ids.count LOOP
3922         Update msc_sales_orders
3923                set Primary_uom_quantity = MSC_ATP_UTILS.Truncate_Demand(Old_primary_uom_quantity),  --5598066
3924                inventory_item_id = p_del_ods_cto_inv_item_ids(m),
3925                reservation_quantity = MSC_ATP_UTILS.Truncate_Demand(old_reservation_quantity), -- 5598066
3926                atp_refresh_number = p_del_ods_cto_atp_refresh_no(m)
3927         WHERE  sr_instance_id = p_instance_id
3928         AND    demand_id = p_del_ods_cto_demand_ids(m)
3929         AND    decode(demand_source_type,100,demand_source_type,-1)
3930                             =decode(p_del_ods_cto_dem_src_type(1),
3931                                                 100,
3932                                                 p_del_ods_cto_dem_src_type(m),
3933                                                 -1);
3934       END LOOP;
3935       IF PG_DEBUG in ('Y', 'C') THEN
3936            msc_sch_wb.atp_debug('Number of SO lines updated for CTO := ' || SQL%ROWCOUNT);
3937       END IF;
3938     END IF;
3939 
3940     -- Bug 2738280. For loop should be inside if condition.
3941     IF ( p_del_demand_ids IS NOT NULL AND p_del_demand_ids.count > 0) THEN --3720018
3942        IF PG_DEBUG in ('Y', 'C') THEN
3943            FOR i in 1..p_del_demand_ids.COUNT LOOP
3944                msc_sch_wb.atp_debug('Undo_Delete_Row: ' || 'p_del_demand_ids('||i||') = '|| p_del_demand_ids(i)||
3945                                     'p_inv_item_ids('||i||') = '|| p_inv_item_ids(i)||
3946                                     'p_plan_ids('||i||') = '|| p_plan_ids(i)||
3947                                     'p_identifiers('||i||') = '|| p_identifiers(i));
3948            END LOOP;
3949        END IF;
3950 
3951     FORALL m IN 1..p_del_demand_ids.COUNT
3952     UPDATE  msc_demands
3953     SET     using_requirement_quantity = MSC_ATP_UTILS.Truncate_Demand(old_demand_quantity),	-- 5598066
3954             -- bug 2863322 : change the column used to store date
3955             using_assembly_demand_date = old_using_assembly_demand_date,
3956             inventory_item_id = p_inv_item_ids(m),
3957             applied = 2,
3958             status = 0,
3959             -- 24x7
3960             atp_synchronization_flag = 0,
3961             refresh_number = old_refresh_number -- For summary enhancement
3962     WHERE   sr_instance_id = p_instance_id
3963     AND     plan_id = p_plan_ids(m)
3964             --- rajjain we dont need this as demand_id is unique identifier
3965             --- AND    sales_order_line_id = p_identifiers(m)
3966             ---subst
3967     AND     demand_id = p_del_demand_ids(m)
3968             -- Bug 2738280. Also bulk collect values of the updated rows to be passed to procedure undo_plan_summary_row.
3969     RETURNING inventory_item_id, using_assembly_demand_date, using_requirement_quantity,
3970             organization_id, plan_id
3971     BULK COLLECT INTO l_inventory_item_id, l_using_assembly_demand_date, l_using_requirement_quantity,
3972             l_organization_id, l_plan_id;
3973 
3974 	/* commented for bug 2738280. We should use SQL%BULK_ROWCOUNT for FORALL statemenrs
3975 	l_del_rows := SQL%ROWCOUNT;
3976 	IF PG_DEBUG in ('Y', 'C') THEN
3977 	   	msc_sch_wb.atp_debug('Undo_Delete_Row: ' || 'No. of demands updated = '|| l_del_rows);
3978 	END IF;
3979 	*/
3980 
3981     -- Bug 2738280. Count how many rows were updated for each demand id
3982     IF PG_DEBUG in ('Y', 'C') THEN
3983         FOR m IN 1..p_del_demand_ids.COUNT LOOP
3984             msc_sch_wb.atp_debug('For Demand id '|| p_del_demand_ids(m)||': updated '||
3985                 SQL%BULK_ROWCOUNT(m)||' records');
3986         END LOOP;
3987     END IF;
3988 
3989     IF (MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y') and (p_copy_demand_ids IS NOT NULL) and (p_copy_demand_ids.COUNT > 0) THEN
3990         -- Code to update summary records removed for summary enhancement
3991         /** code commented for time being. Will be removed after code review
3992         IF PG_DEBUG in ('Y', 'C') THEN
3993             msc_sch_wb.atp_debug('Undo_Delete_Row: ' || 'update Demands in summary mode');
3994         END IF;
3995 
3996         -- rajjain 11/05/2002 Now we pass reference to p_identifiers and p_plan_ids array
3997         -- and do bulk update in Undo_Plan_Summary_Row
3998         -- Bug 2738280. Complete change of the spec and body of this procedure.
3999         -- MSC_ATP_DB_UTILS.UNDO_PLAN_SUMMARY_ROW(p_identifiers, p_plan_ids, p_instance_id);
4000         MSC_ATP_DB_UTILS.UNDO_PLAN_SUMMARY_ROW(l_inventory_item_id,
4001                                                l_using_assembly_demand_date,
4002                                                l_using_requirement_quantity,
4003                                                l_organization_id,
4004                                                l_plan_id,
4005                                                p_instance_id);
4006         **/
4007 
4008         -- Delete the copy SOs and copy stealing records for summary enhancement
4009         IF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
4010             (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4011             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4012             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) OR
4013             (p_time_phased_set = 'Y') THEN
4014 
4015             -- Delete from MSC_ALLOC_DEMANDS
4016             FORALL  i IN 1..p_copy_demand_ids.COUNT
4017             DELETE  FROM MSC_ALLOC_DEMANDS
4018             WHERE   parent_demand_id = p_copy_demand_ids(i)
4019             AND     plan_id = p_copy_plan_ids(i);
4020 
4021         ELSE
4022 
4023             -- Delete from MSC_ALLOC_DEMANDS
4024             FORALL  i IN 1..p_copy_demand_ids.COUNT
4025             DELETE  FROM MSC_DEMANDS
4026             WHERE   demand_id = p_copy_demand_ids(i)
4027             AND     plan_id = p_copy_plan_ids(i);
4028 
4029         END IF;
4030 
4031     END IF;
4032 
4033     -- rajjain 09/16/2002 Bug 2552015, this update not needed as in Schedule procedure
4034     -- we'll be looping on demand_id's.
4035     /*
4036     	--UPDATE statement for updating comp demands was here
4037     */
4038 
4039     --  Allocated ATP Based on Planning Details -- Agilent changes Begin
4040 
4041     IF (p_time_phased_set = 'Y')
4042      OR ((MSC_ATP_PVT.G_INV_CTP = 4) AND
4043         (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4044         (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4045         (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
4046 
4047         IF PG_DEBUG in ('Y', 'C') THEN
4048             msc_sch_wb.atp_debug('Undo_Delete_Row: before update of ' ||
4049                 ' msc_alloc_demands');
4050         END IF;
4051 
4052         /*bug 6642564  we do no want  inventory item id to be updated
4053         in case of time phased ATP as,the inventory item id we are picking up
4054         from msc_demands but for time phased ATP inventory item id in msc_demands
4055         is of member item and msc_alloc_demands could me member item of product
4056         family item */
4057 
4058         IF  p_time_phased_set = 'Y' THEN
4059 
4060         FORALL  m IN 1..p_del_demand_ids.COUNT
4061         UPDATE  msc_alloc_demands
4062         SET     allocated_quantity = MSC_ATP_UTILS.Truncate_Demand(old_allocated_quantity),  --5598066
4063                 demand_date = old_demand_date ,
4064                 old_allocated_quantity = null,
4065                 old_demand_date = null,
4066                 --inventory_item_id = p_inv_item_ids(m),
4067                 refresh_number = old_refresh_number -- For summary enhancement
4068         WHERE   sr_instance_id = p_instance_id
4069         AND     plan_id = p_plan_ids(m)
4070                 --- rajjain we dont need this
4071                 --- AND    sales_order_line_id = p_identifiers(m)
4072         AND     parent_demand_id = p_del_demand_ids(m)
4073         AND     old_allocated_quantity IS NOT NULL;  --bug 8731672
4074 
4075         ELSE
4076 
4077         FORALL  m IN 1..p_del_demand_ids.COUNT
4078         UPDATE  msc_alloc_demands
4079         SET     allocated_quantity = MSC_ATP_UTILS.Truncate_Demand(old_allocated_quantity),  --5598066
4080                 demand_date = old_demand_date ,
4081                 old_allocated_quantity = null,
4082                 old_demand_date = null,
4083                 inventory_item_id = p_inv_item_ids(m),
4084                 refresh_number = old_refresh_number -- For summary enhancement
4085         WHERE   sr_instance_id = p_instance_id
4086         AND     plan_id = p_plan_ids(m)
4087                 --- rajjain we dont need this
4088                 --- AND    sales_order_line_id = p_identifiers(m)
4089         AND     parent_demand_id = p_del_demand_ids(m)
4090         AND     old_allocated_quantity IS NOT NULL;  --bug 8731672
4091 
4092         END IF;
4093 
4094         -- Bug 2738280. Count how many rows were updated for each demand id
4095         IF PG_DEBUG in ('Y', 'C') THEN
4096             FOR m IN 1..p_del_demand_ids.COUNT LOOP
4097                 msc_sch_wb.atp_debug('For Demand id '|| p_del_demand_ids(m)||': updated '||
4098                     SQL%BULK_ROWCOUNT(m)||' records');
4099             END LOOP;
4100         END IF;
4101 
4102         /*
4103             UPDATE statement for comp demands was here
4104         */
4105 
4106         IF PG_DEBUG in ('Y', 'C') THEN
4107             msc_sch_wb.atp_debug('Undo_Delete_Row: before update of ' ||
4108                 '  msc_alloc_supplies');
4109         END IF;
4110 
4111         FORALL  m IN 1..p_del_demand_ids.COUNT
4112         UPDATE  msc_alloc_supplies
4113         SET     allocated_quantity = old_allocated_quantity ,
4114                 supply_date = old_supply_date ,
4115                 old_allocated_quantity = null,
4116                 old_supply_date = null,
4117                 refresh_number = old_refresh_number -- For summary enhancement
4118         WHERE   sr_instance_id = p_instance_id
4119         AND     plan_id = p_plan_ids(m)
4120         AND     sales_order_line_id = p_identifiers(m)
4121         AND     decode(demand_source_type,100,demand_source_type,-1)
4122                             =decode(p_demand_source_type(m),
4123                                                 100,
4124                                                 p_demand_source_type(m),
4125                                                 -1) --CMRO
4126         AND     stealing_flag = 1
4127                 --- rajjain 09/16/2002
4128         AND     old_allocated_quantity IS NOT NULL;
4129 
4130         -- Bug 2738280. Count how many rows were updated for each demand id
4131         IF PG_DEBUG in ('Y', 'C') THEN
4132             FOR m IN 1..p_del_demand_ids.COUNT LOOP
4133                 msc_sch_wb.atp_debug('For Demand id '|| p_del_demand_ids(m)||': updated '||
4134                     SQL%BULK_ROWCOUNT(m)||' records');
4135             END LOOP;
4136         END IF;
4137 
4138     END IF;
4139     --  Allocated ATP Based on Planning Details -- Agilent changes End
4140 
4141     -- CTO ODR and Simplified Pegging
4142     MSC_ATP_PEG.Remove_Offset_Data(
4143                   --p_identifiers  , --Bug 3629191
4144                   --p_plan_ids ,     --Bug 3629191
4145                   p_del_atp_peg_items  ,
4146                   p_del_atp_peg_demands ,
4147                   p_del_atp_peg_supplies,
4148                   p_del_atp_peg_res_reqs,
4149                   p_demand_source_type,--cmro
4150                   p_atp_peg_demands_plan_ids,  --Bug 3629191
4151                   p_atp_peg_supplies_plan_ids, --Bug 3629191
4152                   p_atp_peg_res_reqs_plan_ids, --Bug 3629191
4153                   l_return_status);
4154      -- End CTO ODR and Simplified Pegging
4155     END IF; --3720018
4156 
4157 		IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
4158 		MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
4159     END IF;
4160 
4161     IF PG_DEBUG in ('Y', 'C') THEN
4162         msc_sch_wb.atp_debug('**********End Undo_Delete_Row Procedure************');
4163     END IF;
4164 
4165 EXCEPTION
4166     WHEN OTHERS THEN
4167         IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4168             FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME , 'Undo_Delete_Row');
4169         END IF;
4170 
4171         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4172 
4173 END Undo_Delete_Row;
4174 
4175 PROCEDURE DELETE_SUMMARY_ROW (p_identifier                      IN NUMBER,
4176                      	      p_plan_id                         IN NUMBER,
4177                               p_instance_id                     IN NUMBER,
4178                               p_demand_source_type              IN NUMBER)  --cmro
4179 
4180 IS
4181 l_instance_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4182 l_organization_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4183 l_inventory_item_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4184 l_demand_class MRP_ATP_PUB.char30_arr := MRP_ATP_PUB.char30_arr();
4185 l_sd_date MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
4186 l_sd_qty  MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
4187 i  number;
4188 -- 5357370 changes, need user id/sysdate for insert/update
4189 l_user_id number;
4190 l_sysdate date;
4191 
4192 BEGIN
4193         IF PG_DEBUG in ('Y', 'C') THEN
4194            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'Inside delete summary row');
4195            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'p_identifier := ' || p_identifier);
4196            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'p_instance_id := ' || p_instance_id);
4197         END IF;
4198         BEGIN
4199 	   SELECT D.sr_instance_id,
4200       	   D.organization_id,
4201            D.inventory_item_id,
4202            Decode(NVL(R.DEMAND_CLASS_ATP_FLAG,0),1 , NVL(D.DEMAND_CLASS, '@@@'), '@@@'),
4203            /*In case of reserved quantity check move to next working day*/
4204            DECODE(D.RESERVATION_TYPE,2,C2.next_date, trunc(D.REQUIREMENT_DATE)) SD_DATE, --5161110
4205            --D.REQUIREMENT_DATE SD_DATE, --5161110
4206            (D.PRIMARY_UOM_QUANTITY-GREATEST(NVL(D.RESERVATION_QUANTITY,0),
4207               D.COMPLETED_QUANTITY)) sd_qty
4208            BULK COLLECT INTO l_instance_id, l_organization_id, l_inventory_item_id, l_demand_class, l_sd_date, l_sd_qty
4209            FROM
4210            MSC_SALES_ORDERS D,
4211            MSC_ATP_RULES R,
4212            MSC_SYSTEM_ITEMS I,
4213            MSC_TRADING_PARTNERS P,
4214            msc_calendar_dates C,
4215            MSC_CALENDAR_DATES C2
4216            WHERE       D.DEMAND_SOURCE_LINE = TO_CHAR(P_IDENTIFIER)
4217            AND     decode(demand_source_type,100,demand_source_type,-1)
4218                             =decode(p_demand_source_type,
4219                                                 100,
4220                                                 p_demand_source_type,
4221                                                 -1) --CMRO
4222            AND         D.SR_INSTANCE_ID = p_instance_id
4223            AND         I.ORGANIZATION_ID = D.ORGANIZATION_ID
4224     	   AND         I.SR_INSTANCE_ID = D.SR_INSTANCE_ID
4225            AND         D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
4226     	   AND         I.PLAN_ID = -1
4227            AND         P.SR_TP_ID = I.ORGANIZATION_ID
4228            AND         P.SR_INSTANCE_ID = I.SR_INSTANCE_ID
4229            AND         P.PARTNER_TYPE = 3
4230     	   AND         R.RULE_ID  = NVL(I.ATP_RULE_ID, P.DEFAULT_ATP_RULE_ID)
4231     	   AND         R.SR_INSTANCE_ID = I.SR_INSTANCE_ID
4232     	   AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_SALES_ORDERS,2,2,-1)
4233     	   AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_INTERNAL_ORDERS,2,8,-1)
4234     	   AND         D.PRIMARY_UOM_QUANTITY > GREATEST(NVL(D.RESERVATION_QUANTITY,0),
4235             		                 D.COMPLETED_QUANTITY)
4236     	   AND         (D.SUBINVENTORY IS NULL OR D.SUBINVENTORY IN
4237                 		      (SELECT S.SUB_INVENTORY_CODE
4238                     		      FROM   MSC_SUB_INVENTORIES S
4239                     		      WHERE  S.ORGANIZATION_ID=D.ORGANIZATION_ID
4240                     		      AND    S.PLAN_ID = I.PLAN_ID
4241                     		      AND    S.SR_INSTANCE_ID = D.SR_INSTANCE_ID
4242                     		      AND    S.INVENTORY_ATP_CODE =DECODE(R.DEFAULT_ATP_SOURCES,
4243                                		          1, 1, NULL, 1, S.INVENTORY_ATP_CODE)
4244                     		      AND    S.NETTING_TYPE =DECODE(R.DEFAULT_ATP_SOURCES,
4245                                		          2, 1, S.NETTING_TYPE)))
4246     				      AND         (D.RESERVATION_TYPE = 2
4247                  		      OR D.PARENT_DEMAND_ID IS NULL
4248                  		      OR (D.RESERVATION_TYPE = 3 AND
4249                      			      ((R.INCLUDE_DISCRETE_WIP_RECEIPTS = 1) or
4250                       			      (R.INCLUDE_NONSTD_WIP_RECEIPTS = 1))))
4251            -- Changed for 5161110
4252            AND C.PRIOR_SEQ_NUM >=
4253                 		      DECODE(R.PAST_DUE_DEMAND_CUTOFF_FENCE,
4254                         	      NULL, C.PRIOR_SEQ_NUM,
4255           			      C2.next_seq_num - NVL(R.PAST_DUE_DEMAND_CUTOFF_FENCE,0))
4256     	  AND     C.CALENDAR_CODE = P.CALENDAR_CODE
4257     	  AND     C.SR_INSTANCE_ID = p_instance_id
4258     	  AND     C.EXCEPTION_SET_ID = -1
4259     	  AND     C.CALENDAR_DATE = TRUNC(D.REQUIREMENT_DATE)
4260     	  AND     C2.CALENDAR_CODE = P.calendar_code
4261 	  AND     C2.EXCEPTION_SET_ID = P.calendar_exception_set_id
4262 	  AND     C2.SR_INSTANCE_ID = P.SR_INSTANCE_ID
4263 	  AND     C2.CALENDAR_DATE = TRUNC(sysdate);
4264 
4265 
4266         EXCEPTION
4267              WHEN NO_DATA_FOUND THEN
4268                 IF PG_DEBUG in ('Y', 'C') THEN
4269                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'No row found that need to be deleted from summary table');
4270                 END IF;
4271 	END;
4272         -- 5357370: need user id for insert
4273         l_user_id  := FND_GLOBAL.USER_ID;
4274         l_sysdate := sysdate;
4275 
4276 
4277         IF PG_DEBUG in ('Y', 'C') THEN
4278            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_inventory_item_id.count := ' || l_inventory_item_id.count);
4279            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_user_id := '|| l_user_id);
4280            msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_sysdate := '|| l_sysdate);
4281 
4282         END IF;
4283 
4284         FOR i in 1..l_inventory_item_id.count LOOP
4285                 IF PG_DEBUG in ('Y', 'C') THEN
4286                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'Row found, delete it from summary');
4287                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_ivnevtory_item_id := ' || l_inventory_item_id(i));
4288                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_organization_id := ' || l_organization_id(i) );
4289                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_demand_class := ' || l_demand_class(i));
4290                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_sd_date := ' || l_sd_date(i));
4291                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_sd_qty := ' || l_sd_qty(i));
4292                    msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: ' || 'l_instance_id := ' || l_instance_id(i));
4293                 END IF;
4294 
4295                 update /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
4296                 set sd_qty = (sd_qty - l_sd_qty(i))
4297                 where inventory_item_id = l_inventory_item_id(i)
4298                 and sr_instance_id = l_instance_id(i)
4299                 and organization_id = l_organization_id(i)
4300                 and sd_date = trunc(l_sd_date(i))
4301                 and demand_class = l_demand_class(i) ;
4302                 -- 5357370: this is to handle that we have reservation on the past due date, and
4303                 -- we won't be able to find record on sysdate to update.
4304                 IF (SQL%NOTFOUND) THEN
4305                   msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: update failed, now try insert');
4306                   --- Insert the new record
4307                   BEGIN
4308                     INSERT INTO MSC_ATP_SUMMARY_SO
4309                             (plan_id,
4310                              sr_instance_id,
4311                              organization_id,
4312                              inventory_item_id,
4313                              demand_class,
4314                              sd_date,
4315                              sd_qty,
4316                              last_update_date,
4317                              last_updated_by,
4318                              creation_date,
4319                              created_by)
4320                     VALUES ( -1,
4321                              l_instance_id(i),
4322                              l_organization_id(i),
4323                              l_inventory_item_id(i),
4324                              l_demand_class(i),
4325                              trunc(l_sd_date(i)),
4326                              - l_sd_qty(i),
4327                              l_sysdate,
4328                              l_user_id ,
4329                              l_sysdate,
4330                              l_user_id
4331                            );
4332                   EXCEPTION
4333 
4334                   -- If a record has already been inserted by another process
4335                   -- If insert fails then update.
4336                     WHEN DUP_VAL_ON_INDEX THEN
4337                       -- Update the record.
4338                       update /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
4339                       set sd_qty = (sd_qty - l_sd_qty(i)),
4340                           last_update_date = l_sysdate,
4341                           last_updated_by = l_user_id
4342                       where inventory_item_id = l_inventory_item_id(i)
4343                       and sr_instance_id = l_instance_id(i)
4344                       and organization_id = l_organization_id(i)
4345                       and sd_date = trunc(l_sd_date(i))
4346                       and demand_class = l_demand_class(i) ;
4347 
4348                   END;
4349                 END IF;
4350                 -- 5357370: end of changes to handle the update failure.
4351                 commit;
4352 
4353         END LOOP;
4354 
4355 END DELETE_SUMMARY_ROW;
4356 
4357 -- Bug 2738280. Change the body of procedure
4358 PROCEDURE UPDATE_PLAN_SUMMARY_ROW (p_inventory_item_id		     IN MRP_ATP_PUB.Number_Arr,
4359 				   p_old_demand_date                 IN MRP_ATP_PUB.Date_Arr,
4360                                    p_old_demand_quantity	     IN MRP_ATP_PUB.Number_Arr,
4361 				   p_organization_id		     IN MRP_ATP_PUB.Number_Arr,
4362 				   p_plan_id                         IN NUMBER,
4363                                    p_instance_id                     IN NUMBER)
4364 IS
4365 l_counter	PLS_INTEGER;
4366 BEGIN
4367         IF PG_DEBUG in ('Y', 'C') THEN
4368            msc_sch_wb.atp_debug('******Begin Update_plan_summary_row *******');
4369            msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_instance_id := ' || p_instance_id);
4370            msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_plan_id := ' || p_plan_id);
4371 
4372 	   FOR l_counter IN 1..p_inventory_item_id.COUNT LOOP
4373 	    msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'Input parameters loop : '|| l_counter);
4374 	    msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_inventory_item_id := ' || p_inventory_item_id(l_counter));
4375 	    msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_organization_id := ' || p_organization_id(l_counter));
4376 	    msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_old_demand_date := ' || p_old_demand_date(l_counter));
4377 	    msc_sch_wb.atp_debug('UPDATE_PLAN_SUMMARY_ROW: ' || 'p_old_demand_quantity := ' || p_old_demand_quantity(l_counter));
4378            END LOOP;
4379 
4380         END IF;
4381 
4382 	FORALL	l_counter IN 1..p_inventory_item_id.COUNT
4383 	UPDATE  /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ MSC_ATP_SUMMARY_SD
4384 	SET	SD_QTY = SD_QTY  + p_old_demand_quantity(l_counter)
4385 	WHERE	INVENTORY_ITEM_ID = p_inventory_item_id(l_counter)
4386 	AND	PLAN_ID = p_plan_id
4387 	AND	SR_INSTANCE_ID = p_instance_id
4388 	AND	ORGANIZATION_ID = p_organization_id(l_counter)
4389 	AND	SD_DATE = trunc(p_old_demand_date(l_counter));
4390 
4391 	-- Count how many rows were updated for each item
4392 	IF PG_DEBUG in ('Y', 'C') THEN
4393 	  FOR l_counter IN 1..p_inventory_item_id.COUNT LOOP
4394 	     msc_sch_wb.atp_debug('For inventory Item id '||p_inventory_item_id(l_counter)||': updated '||
4395                           SQL%BULK_ROWCOUNT(l_counter)||' records');
4396 	  END LOOP;
4397 	END IF;
4398 
4399 	-- issue commit;
4400 	commit;
4401 
4402 END UPDATE_PLAN_SUMMARY_ROW;
4403 
4404 -- Bug 2738280. Change the body of procedure
4405 PROCEDURE UNDO_PLAN_SUMMARY_ROW (p_inventory_item_id		   IN MRP_ATP_PUB.Number_Arr,
4406 				 p_using_assembly_demand_date      IN MRP_ATP_PUB.Date_Arr,
4407                                  p_using_requirement_quantity	   IN MRP_ATP_PUB.Number_Arr,
4408 				 p_organization_id		   IN MRP_ATP_PUB.Number_Arr,
4409 				 p_plan_id                         IN MRP_ATP_PUB.Number_Arr,
4410                                  p_instance_id                     IN NUMBER)
4411 IS
4412 l_counter	PLS_INTEGER;
4413 BEGIN
4414 
4415 	IF PG_DEBUG in ('Y', 'C') THEN
4416            msc_sch_wb.atp_debug('******Begin Undo_plan_summary_row *******');
4417            msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_instance_id := ' || p_instance_id);
4418 
4419 	   FOR l_counter IN 1..p_inventory_item_id.COUNT LOOP
4420 	    msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'Input parameters loop : '|| l_counter);
4421 	    msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_inventory_item_id := ' || p_inventory_item_id(l_counter));
4422 	    msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_organization_id := ' || p_organization_id(l_counter));
4423             msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_plan_id := ' || p_plan_id(l_counter));
4424 	    msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_using_assembly_demand_date := ' || p_using_assembly_demand_date(l_counter));
4425 	    msc_sch_wb.atp_debug('UNDO_PLAN_SUMMARY_ROW: ' || 'p_using_requirement_quantity := ' || p_using_requirement_quantity(l_counter));
4426            END LOOP;
4427 
4428         END IF;
4429 
4430 	FORALL l_counter IN 1..p_inventory_item_id.COUNT
4431              UPDATE /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ MSC_ATP_SUMMARY_SD
4432              SET    SD_QTY	      = SD_QTY  - p_using_requirement_quantity(l_counter)
4433              WHERE  INVENTORY_ITEM_ID = p_inventory_item_id(l_counter)
4434              AND    PLAN_ID	      = p_plan_id(l_counter)
4435              AND    SR_INSTANCE_ID    = p_instance_id
4436              AND    ORGANIZATION_ID   = p_organization_id(l_counter)
4437              AND    SD_DATE	      = trunc(p_using_assembly_demand_date(l_counter));
4438 
4439 	-- Count how many rows were updated for each item
4440 	IF PG_DEBUG in ('Y', 'C') THEN
4441 	  FOR l_counter IN 1..p_inventory_item_id.COUNT LOOP
4442 	     msc_sch_wb.atp_debug('For inventory Item id '||p_inventory_item_id(l_counter)||': updated '||
4443                           SQL%BULK_ROWCOUNT(l_counter)||' records');
4444 	  END LOOP;
4445 	END IF;
4446 
4447 	-- issue commit;
4448 	commit;
4449 
4450 END UNDO_PLAN_SUMMARY_ROW;
4451 
4452 PROCEDURE INSERT_SUMMARY_SD_ROW( p_plan_id           IN NUMBER,
4453                                  p_instance_id       IN NUMBER,
4454                                  p_organization_id   IN NUMBER,
4455                                  p_inventory_item_id IN NUMBER,
4456                                  p_date              IN DATE,
4457                                  p_quantity          IN NUMBER,
4458                                  p_demand_class       IN VARCHAR2)
4459 IS
4460 BEGIN
4461 
4462 	IF PG_DEBUG in ('Y', 'C') THEN
4463 	   msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'insert into summary row');
4464            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_plan_id := ' || p_plan_id);
4465            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_instance_id := ' || p_instance_id);
4466            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_organization_id := ' || p_organization_id);
4467            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_inventory_item_id := ' || p_inventory_item_id);
4468            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_date := ' || p_date);
4469            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || ' p_quantity := ' || p_quantity);
4470            msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'p_demand_class := ' || p_demand_class);
4471         END IF;
4472         BEGIN
4473            insert /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ into msc_atp_summary_sd
4474                (plan_id,
4475 	        inventory_item_id,
4476                 organization_id,
4477                 sr_instance_id,
4478                 sd_date,
4479                 sd_qty,
4480                 demand_class,
4481                 LAST_UPDATE_DATE,
4482                 LAST_UPDATED_BY,
4483                 CREATION_DATE,
4484                 CREATED_BY
4485                  )
4486                 VALUES
4487                 (p_plan_id,
4488 		p_inventory_item_id,
4489                 p_organization_id,
4490                 p_instance_id,
4491                 trunc(p_date),
4492                 p_quantity,
4493                 '@@@',
4494                 sysdate,
4495                 FND_GLOBAL.USER_ID,
4496                 sysdate,
4497                 FND_GLOBAL.USER_ID);
4498         EXCEPTION
4499            WHEN DUP_VAL_ON_INDEX THEN
4500                  MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_instance_id,
4501                                           p_plan_id,
4502                                           p_organization_id,
4503                                           p_inventory_item_id,
4504                                           p_date,
4505                                           null,
4506                                           null,
4507                                           null,
4508                                           null,
4509                                           null,
4510                                           null,
4511                                           2);
4512 
4513                 IF PG_DEBUG in ('Y', 'C') THEN
4514                    msc_sch_wb.atp_debug('INSERT_SUMMARY_SD_ROW: ' || 'Row already exists in table. Update the row');
4515                 END IF;
4516                 update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) */ msc_atp_summary_sd
4517                 set sd_qty = sd_qty + p_quantity
4518                 where plan_id = p_plan_id
4519                 and  inventory_item_id = p_inventory_item_id
4520                 and  sr_instance_id = p_instance_id
4521                 and  organization_id = p_organization_id
4522                 and  sd_date = trunc(p_date);
4523 	END;
4524 END INSERT_SUMMARY_SD_ROW;
4525 
4526 /* New procedure for Allocated ATP Based on Planning Details for Agilent */
4527 
4528 PROCEDURE Add_Stealing_Supply_Details (
4529         p_plan_id               IN NUMBER,
4530         p_identifier            IN NUMBER,
4531         p_inventory_item_id     IN NUMBER,
4532         p_organization_id       IN NUMBER,
4533         p_sr_instance_id        IN NUMBER,
4534         p_stealing_quantity     IN NUMBER,
4535         p_stealing_demand_class IN VARCHAR2,
4536         p_stolen_demand_class   IN VARCHAR2,
4537         p_ship_date             IN DATE,
4538         p_transaction_id        OUT NoCopy NUMBER,
4539         p_refresh_number        IN NUMBER,
4540         p_ato_model_line_id       IN number,
4541         p_demand_source_type      IN    Number,  --cmro
4542         --bug3684383
4543         p_order_number           IN NUMBER
4544         ) IS -- For summary enhancement
4545 
4546 l_rows_proc    NUMBER := 0;
4547 BEGIN
4548 
4549     -- First add the Stealing Data.
4550 
4551     IF PG_DEBUG in ('Y', 'C') THEN
4552         msc_sch_wb.atp_debug('*** Begin Add_Stealing_Supply_Details Procedure ***');
4553         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_plan_id ='||to_char(p_plan_id));
4554         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_identifier ='||to_char(p_identifier));
4555         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_demand_source_type ='||to_char(p_demand_source_type));
4556         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_inventory_item_id ='||to_char(p_inventory_item_id));
4557         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_organization_id = ' ||to_char(p_organization_id));
4558         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_instance_id = ' ||to_char(p_sr_instance_id));
4559         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_stealing_quantity ='||to_char(p_stealing_quantity));
4560         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_stealing_demand_class = '||p_stealing_demand_class);
4561         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_stolen_demand_class = ' ||p_stolen_demand_class);
4562         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_ship_date = ' ||to_char(p_ship_date));
4563         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'p_refresh_number = ' ||p_refresh_number);
4564     END IF;
4565 
4566     SELECT msc_supplies_s.nextval into p_transaction_id from dual;
4567 
4568     IF PG_DEBUG in ('Y', 'C') THEN
4569         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'before insert into msc_alloc_supplies-Stealing Info');
4570     END IF;
4571 
4572     --ssurendr 25-NOV-2002: From_Demand_Class added in both the insert statements for alloc w/b drill down
4573     INSERT INTO MSC_ALLOC_SUPPLIES
4574         (plan_id, inventory_item_id, organization_id, sr_instance_id,
4575         demand_class, supply_date, parent_transaction_id,
4576         allocated_quantity, order_type, sales_order_line_id,demand_source_type, stealing_flag,  --cmro
4577         created_by, creation_date, last_updated_by, last_update_date, from_demand_class,
4578         ato_model_line_id,  refresh_number, -- For summary enhancement
4579         --bug3684383
4580         order_number,customer_id,ship_to_site_id
4581         )
4582     VALUES
4583         (p_plan_id, p_inventory_item_id, p_organization_id,
4584         p_sr_instance_id, p_stealing_demand_class, p_ship_date,
4585         p_transaction_id, p_stealing_quantity, 46, p_identifier,p_demand_source_type, 1,  ---cmro
4586         FND_GLOBAL.USER_ID, sysdate, FND_GLOBAL.USER_ID, sysdate, p_stolen_demand_class,
4587         p_ato_model_line_id, p_refresh_number, -- For summary enhancement
4588         --bug3684383
4589         p_order_number,MSC_ATP_PVT.G_PARTNER_ID,MSC_ATP_PVT.G_PARTNER_SITE_ID);
4590 
4591     l_rows_proc := SQL%ROWCOUNT;
4592 
4593     -- Next add the Stolen Data.
4594 
4595     IF PG_DEBUG in ('Y', 'C') THEN
4596         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'before insert into msc_alloc_supplies-Stolen Info');
4597     END IF;
4598 
4599     INSERT INTO MSC_ALLOC_SUPPLIES
4600         (plan_id, inventory_item_id, organization_id, sr_instance_id,
4601         demand_class, supply_date, parent_transaction_id,
4602         allocated_quantity, order_type, sales_order_line_id,demand_source_type, stealing_flag, --cmro
4603         created_by, creation_date, last_updated_by, last_update_date, from_demand_class,
4604         ato_model_line_id, refresh_number, -- For summary enhancement
4605         --bug3684383
4606         order_number,customer_id,ship_to_site_id)
4607     VALUES
4608         (p_plan_id, p_inventory_item_id, p_organization_id,
4609         p_sr_instance_id, p_stolen_demand_class, p_ship_date,
4610         p_transaction_id, -1 * p_stealing_quantity, 47, p_identifier,p_demand_source_type,1, --cmro
4611         FND_GLOBAL.USER_ID, sysdate, FND_GLOBAL.USER_ID, sysdate, p_stealing_demand_class,
4612         p_ato_model_line_id,  p_refresh_number, -- For summary enhancement
4613         --bug3684383
4614         p_order_number,MSC_ATP_PVT.G_PARTNER_ID,MSC_ATP_PVT.G_PARTNER_SITE_ID);
4615 
4616     l_rows_proc := l_rows_proc + SQL%ROWCOUNT;
4617 
4618     IF PG_DEBUG in ('Y', 'C') THEN
4619         msc_sch_wb.atp_debug('Add_Stealing_Supply_Details: ' || 'Total Rows inserted ' || l_rows_proc);
4620     END IF;
4621 
4622 END Add_Stealing_Supply_Details;
4623 
4624 
4625 PROCEDURE Remove_Invalid_Future_SD(
4626         p_future_pegging_tab            IN      MRP_ATP_PUB.Number_Arr
4627 )
4628 IS
4629 
4630 CURSOR pegging(p_pegging_id	IN	NUMBER)
4631 IS
4632 SELECT	pegging_id, identifier1, identifier2, identifier3
4633 FROM 	mrp_atp_details_temp
4634 WHERE	pegging_id = p_pegging_id
4635 AND	record_type in (3,4)
4636 AND	session_id = MSC_ATP_PVT.G_SESSION_ID
4637 START WITH pegging_id = p_pegging_id
4638 AND 	session_id = MSC_ATP_PVT.G_SESSION_ID
4639 AND 	record_type = 3
4640 CONNECT BY parent_pegging_id = PRIOR pegging_id
4641 AND 	session_id = prior session_id
4642 AND 	record_type in (3,4);
4643 
4644 c1 	pegging%ROWTYPE;
4645 
4646 BEGIN
4647   IF PG_DEBUG in ('Y', 'C') THEN
4648      msc_sch_wb.atp_debug('***** Begin Remove_Invalid_Future_SD Procedure *****');
4649   END IF;
4650 
4651   FOR i IN p_future_pegging_tab.FIRST..p_future_pegging_tab.COUNT LOOP
4652       OPEN pegging(p_future_pegging_tab(i));
4653       LOOP
4654 
4655          FETCH pegging INTO c1;
4656          EXIT WHEN pegging%NOTFOUND;
4657 
4658          IF PG_DEBUG in ('Y', 'C') THEN
4659             msc_sch_wb.atp_debug('Remove_Invalid_Future_SD: ' || 'instance id = '||c1.identifier1);
4660             msc_sch_wb.atp_debug('Remove_Invalid_Future_SD: ' || 'plan_id (identifier2) = '||c1.identifier2);
4661             msc_sch_wb.atp_debug('Remove_Invalid_Future_SD: ' || 'identifier3 = '||c1.identifier3);
4662             msc_sch_wb.atp_debug('Remove_Invalid_Future_SD: ' || 'delete pegging_id '||c1.pegging_id);
4663          END IF;
4664 
4665          MSC_ATP_DB_UTILS.Delete_Pegging(c1.pegging_id);
4666 
4667          DELETE MSC_ALLOC_SUPPLIES
4668          WHERE parent_transaction_id = c1.identifier3
4669          AND   plan_id = c1.identifier2
4670 	 AND   sr_instance_id = c1.identifier1;
4671 
4672          IF PG_DEBUG in ('Y', 'C') THEN
4673             msc_sch_wb.atp_debug('Remove_Invalid_Future_SD: ' || 'Records deleted from msc_alloc_supplies = '|| SQL%ROWCOUNT);
4674          END IF;
4675 
4676       END LOOP;
4677       CLOSE pegging;
4678   END LOOP; 	--FOR i IN p_future_pegging_tab.FIRST..p_future_pegging_tab.COUNT LOOP
4679 
4680   IF PG_DEBUG in ('Y', 'C') THEN
4681      msc_sch_wb.atp_debug('***** End Remove_Invalid_Future_SD Procedure *****');
4682   END IF;
4683 
4684 END Remove_Invalid_Future_SD;
4685 
4686 ---------------------------------------------------------------------------
4687 
4688 /*
4689  * dsting: 9/17/2002
4690  *
4691  * Copy the data from the msc_atp_sd_details_temp table
4692  * into mrp_atp_details_temp. Plus, slap on a pegging id and
4693  * end_pegging_id
4694  *
4695  * Delete the entries in msc_atp_sd_details_temp
4696  * since we don't need them anymore too.
4697  */
4698 PROCEDURE move_SD_temp_into_mrp_details(
4699   p_pegging_id     IN NUMBER,
4700   p_end_pegging_id IN NUMBER)
4701 IS
4702 sql_stmt VARCHAR2(3000);
4703 who_cols VARCHAR2(100);
4704 BEGIN
4705 	IF PG_DEBUG in ('Y', 'C') THEN
4706 	   msc_sch_wb.atp_debug('PROCEDURE move_SD_temp_into_mrp_details');
4707 	   msc_sch_wb.atp_debug('move_SD_temp_into_mrp_details: ' || '   p_pegging_id: '     || p_pegging_id);
4708 	   msc_sch_wb.atp_debug('move_SD_temp_into_mrp_details: ' || '   p_end_pegging_id: ' || p_end_pegging_id);
4709 	   msc_sch_wb.atp_debug('move_SD_temp_into_mrp_details: ' || '   session_id: ' || MSC_ATP_PVT.G_SESSION_ID);
4710 	END IF;
4711 
4712 	INSERT INTO mrp_atp_details_temp (
4713 			session_id,
4714 	 		scenario_id,
4715 		 	order_line_id,
4716 		 	ATP_Level,
4717 	 		Inventory_Item_Id,
4718 		 	Request_Item_Id,
4719 		 	Organization_Id,
4720 	 		Department_Id,
4721 		 	Resource_Id,
4722 		 	Supplier_Id,
4723 	 		Supplier_Site_Id,
4724 		 	From_Organization_Id,
4725 		 	From_Location_Id,
4726 		 	To_Organization_Id,
4727 		 	To_Location_Id,
4728 	 		Ship_Method,
4729 		 	Uom_code,
4730 		 	Identifier1,
4731 	 		Identifier2,
4732 		 	Identifier3,
4733 		 	Identifier4,
4734 	 		Supply_Demand_Type,
4735 		 	Supply_Demand_Source_Type,
4736 		 	Supply_Demand_Source_type_name,
4737 	 		Supply_Demand_Date,
4738 		 	supply_demand_quantity,
4739 		 	disposition_type,
4740 	 		disposition_name,
4741 	         	record_type,
4742         	 	pegging_id,
4743          		end_pegging_id,
4744 		 	creation_date,
4745 	 		created_by,
4746 		 	last_update_date,
4747 		 	last_updated_by,
4748 	 		last_update_login,
4749 	 		-- time_phased_atp
4750 	 		Allocated_Quantity,
4751 	 		Original_Demand_Quantity,
4752 	 		Original_Demand_Date,
4753 	 		Original_Item_Id,
4754 	 		Original_Supply_Demand_Type,
4755 	 		Pf_Display_Flag,
4756 	 		ORIG_CUSTOMER_SITE_NAME,--bug3263368
4757                         ORIG_CUSTOMER_NAME, --bug3263368
4758                         ORIG_DEMAND_CLASS, --bug3263368
4759                         ORIG_REQUEST_DATE, --bug3263368
4760                         INVENTORY_ITEM_NAME --bug3579625
4761 		)
4762 		SELECT
4763 			MSC_ATP_PVT.G_SESSION_ID,
4764 	 		scenario_id,
4765 		 	order_line_id,
4766 		 	ATP_Level,
4767 	 		Inventory_Item_Id,
4768 		 	Request_Item_Id,
4769 		 	Organization_Id,
4770 	 		Department_Id,
4771 		 	Resource_Id,
4772 		 	Supplier_Id,
4773 	 		Supplier_Site_Id,
4774 		 	From_Organization_Id,
4775 		 	From_Location_Id,
4776 	 		To_Organization_Id,
4777 		 	To_Location_Id,
4778 		 	Ship_Method,
4779 	 		Uom_code,
4780 		 	Identifier1,
4781 		 	Identifier2,
4782 	 		Identifier3,
4783 		 	Identifier4,
4784 		 	Supply_Demand_Type,
4785 	 		Supply_Demand_Source_Type,
4786 		 	Supply_Demand_Source_type_name,
4787 		 	Supply_Demand_Date,
4788 	 		supply_demand_quantity,
4789 		 	disposition_type,
4790 		 	disposition_name,
4791          		2,
4792 	         	p_pegging_id,
4793         	 	p_end_pegging_id,
4794 		 	creation_date,
4795 	 		created_by,
4796 		 	last_update_date,
4797 		 	last_updated_by,
4798 	 		last_update_login,
4799 	 		-- time_phased_atp
4800 	 		Allocated_Quantity,
4801 	 		Original_Demand_Quantity,
4802 	 		Original_Demand_Date,
4803 	 		Original_Item_Id,
4804 	 		Original_Supply_Demand_Type,
4805 	 		Pf_Display_Flag,
4806 	 		ORIG_CUSTOMER_SITE_NAME,--bug3263368
4807                         ORIG_CUSTOMER_NAME, --bug3263368
4808                         ORIG_DEMAND_CLASS, --bug3263368
4809                         ORIG_REQUEST_DATE, --bug3263368
4810                         INVENTORY_ITEM_NAME --bug3579625
4811 		FROM msc_atp_sd_details_temp;
4812 
4813 		IF PG_DEBUG in ('Y', 'C') THEN
4814 		   msc_sch_wb.atp_debug('move_SD_temp_into_mrp_details: ' || '    Num rows dumped: ' || SQL%ROWCOUNT);
4815 		END IF;
4816 END move_SD_temp_into_mrp_details;
4817 
4818 PROCEDURE Clear_SD_Details_Temp
4819 IS
4820 BEGIN
4821 	IF PG_DEBUG in ('Y', 'C') THEN
4822 	   msc_sch_wb.atp_debug('PROCEDURE Clear_SD_Details_Temp');
4823 	   msc_sch_wb.atp_debug('PG_Clear_SD_Details_Temp: ' || PG_CLEAR_SD_DETAILS_TEMP);
4824  	END IF;
4825 
4826 	IF PG_CLEAR_SD_DETAILS_TEMP = 1 THEN
4827 		DELETE msc_atp_sd_details_temp;
4828 		IF PG_DEBUG in ('Y', 'C') THEN
4829 		   msc_sch_wb.atp_debug('Clear_SD_Details_Temp rows: ' || SQL%ROWCOUNT);
4830 	 	END IF;
4831 	END IF;
4832 	PG_CLEAR_SD_DETAILS_TEMP := 1;
4833 END;
4834 
4835 -- new procedure for summary enhancement
4836 -- Delete the copy demands that were created in this transaction if refresh number has moved.
4837 PROCEDURE Delete_Copy_Demand (
4838                 p_copy_demand_ids           IN  MRP_ATP_PUB.Number_Arr,
4839                 p_copy_plan_ids             IN  MRP_ATP_PUB.Number_Arr,
4840                 p_time_phased_set           IN  VARCHAR2,
4841                 x_return_status             OUT NOCOPY VARCHAR2)
4842 IS
4843 BEGIN
4844     IF PG_DEBUG in ('Y', 'C') THEN
4845         msc_sch_wb.atp_debug(' ******** Delete_Copy_Demand Begin *************');
4846     END IF;
4847 
4848     x_return_status := FND_API.G_RET_STS_SUCCESS;
4849 
4850     msc_sch_wb.atp_debug('Delete_Copy_Demand : ' || 'p_copy_demand_ids.COUNT : ' || p_copy_demand_ids.COUNT);
4851 
4852     IF p_copy_demand_ids.COUNT > 0 THEN
4853         IF ((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4854             (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4855             (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) OR
4856             (p_time_phased_set = 'Y') THEN
4857 
4858             msc_sch_wb.atp_debug('Delete_Copy_Demand : ' || 'allocated ATP or time phased PF');
4859 
4860             FORALL i IN 1..p_copy_demand_ids.COUNT
4861             DELETE FROM msc_alloc_demands
4862             WHERE  parent_demand_id = p_copy_demand_ids(i)
4863             AND    plan_id = p_copy_plan_ids(i)
4864             AND    refresh_number <= (SELECT nvl(latest_refresh_number,-1)
4865                                       FROM   MSC_PLANS
4866                                       WHERE  plan_id = p_copy_plan_ids(i));
4867 
4868         END IF;
4869 
4870         IF NOT ((MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
4871                 (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
4872                 (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
4873 
4874             msc_sch_wb.atp_debug('Delete_Copy_Demand : ' || 'unallocated ATP');
4875 
4876             FORALL i IN 1..p_copy_demand_ids.COUNT
4877             DELETE FROM msc_demands
4878             WHERE  demand_id = p_copy_demand_ids(i)
4879             AND    plan_id = p_copy_plan_ids(i)
4880             AND    refresh_number <= (SELECT nvl(latest_refresh_number,-1)
4881                                       FROM   MSC_PLANS
4882                                       WHERE  plan_id = p_copy_plan_ids(i));
4883 
4884         END IF;
4885     END IF;
4886 
4887     IF PG_DEBUG in ('Y', 'C') THEN
4888         msc_sch_wb.atp_debug(' ******** Delete_Copy_Demand End *************');
4889     END IF;
4890 
4891 EXCEPTION
4892     WHEN OTHERS THEN
4893         x_return_status := FND_API.G_RET_STS_ERROR;
4894 END Delete_Copy_Demand;
4895 
4896 -- New procedure added for ship_rec_cal project
4897 PROCEDURE Flush_Data_In_Pds(
4898 	p_ship_arrival_date_rec         IN	MSC_ATP_PVT.ship_arrival_date_rec_typ,
4899 	x_return_status                 OUT     NOCOPY  VARCHAR2
4900 	)
4901 IS
4902 
4903 BEGIN
4904 	x_return_status := FND_API.G_RET_STS_SUCCESS;
4905 
4906         IF PG_DEBUG in ('Y', 'C') THEN
4907                 msc_sch_wb.atp_debug(' ******** Flush_Data_In_Pds Begin *************');
4908                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'sch arrival date: ' || p_ship_arrival_date_rec.scheduled_arrival_date);
4909                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'lat accep date  : ' || p_ship_arrival_date_rec.latest_acceptable_date);
4910                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'order_date_type : ' || p_ship_arrival_date_rec.order_date_type);
4911                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'demand_id       : ' || p_ship_arrival_date_rec.demand_id);
4912                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'instance_id     : ' || p_ship_arrival_date_rec.instance_id);
4913                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'plan_id         : ' || p_ship_arrival_date_rec.plan_id);
4914                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'ship_set_name   : ' || p_ship_arrival_date_rec.ship_set_name);
4915                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'arrival_set_name: ' || p_ship_arrival_date_rec.arrival_set_name);
4916                 msc_sch_wb.atp_debug('Flush_Data_In_Pds : ' || 'override_flag   : ' || p_ship_arrival_date_rec.atp_override_flag);
4917         END IF;
4918 
4919 	-- Update PDS
4920 	UPDATE 	MSC_DEMANDS
4921 	SET	SCHEDULE_ARRIVAL_DATE   = p_ship_arrival_date_rec.scheduled_arrival_date,
4922 		LATEST_ACCEPTABLE_DATE  = p_ship_arrival_date_rec.latest_acceptable_date,
4923 		ORDER_DATE_TYPE_CODE    = p_ship_arrival_date_rec.order_date_type,
4924 		SHIP_SET_NAME		= p_ship_arrival_date_rec.ship_set_name,--plan by request date
4925 		ARRIVAL_SET_NAME	= p_ship_arrival_date_rec.arrival_set_name,--plan by request date
4926 		ATP_OVERRIDE_FLAg	= decode(upper(p_ship_arrival_date_rec.atp_override_flag),'Y',1,2),--plan by request date
4927 		PROMISE_DATE		= p_ship_arrival_date_rec.scheduled_arrival_date, --plan by request date
4928 		REQUEST_DATE		= p_ship_arrival_date_rec.request_arrival_date
4929 	WHERE	DEMAND_ID               = p_ship_arrival_date_rec.demand_id
4930 	AND	SR_INSTANCE_ID          = p_ship_arrival_date_rec.instance_id
4931 	AND	PLAN_ID                 = p_ship_arrival_date_rec.plan_id;
4932 
4933 
4934 EXCEPTION
4935     WHEN OTHERS THEN
4936     	msc_sch_wb.atp_debug('Flush_Data_In_Pds : '||sqlerrm);
4937         x_return_status := FND_API.G_RET_STS_ERROR;
4938 
4939 END Flush_Data_In_Pds;
4940 
4941 -- New procedure added for plan by request date project
4942 PROCEDURE Flush_Data_In_Ods(
4943 	p_ship_arrival_date_rec         IN	MSC_ATP_PVT.ship_arrival_date_rec_typ,
4944 	x_return_status                 OUT     NOCOPY  VARCHAR2
4945 	)
4946 IS
4947 
4948 BEGIN
4949 	x_return_status := FND_API.G_RET_STS_SUCCESS;
4950 
4951 
4952         IF PG_DEBUG in ('Y', 'C') THEN
4953                 msc_sch_wb.atp_debug(' ******** Flush_Data_In_Ods Begin *************');
4954                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'sch arrival date    : ' || p_ship_arrival_date_rec.scheduled_arrival_date);
4955                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'lat accep date      : ' || p_ship_arrival_date_rec.latest_acceptable_date);
4956                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'order_date_type     : ' || p_ship_arrival_date_rec.order_date_type);
4957                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'demand_id           : ' || p_ship_arrival_date_rec.demand_id);
4958                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'instance_id         : ' || p_ship_arrival_date_rec.instance_id);
4959                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'plan_id             : ' || p_ship_arrival_date_rec.plan_id);
4960                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'ship_set_name       : ' || p_ship_arrival_date_rec.ship_set_name);
4961                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'arrival_set_name    : ' || p_ship_arrival_date_rec.arrival_set_name);
4962                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'override_flag       : ' || p_ship_arrival_date_rec.atp_override_flag);
4963                 msc_sch_wb.atp_debug('Flush_Data_In_Ods : ' || 'request arrival date: ' || p_ship_arrival_date_rec.request_arrival_date);
4964 
4965         END IF;
4966 
4967 	-- Update PDS
4968 	UPDATE 	MSC_SALES_ORDERS
4969 	SET	SCHEDULE_ARRIVAL_DATE   	= p_ship_arrival_date_rec.scheduled_arrival_date,
4970 		LATEST_ACCEPTABLE_DATE  	= p_ship_arrival_date_rec.latest_acceptable_date,
4971 		ORDER_DATE_TYPE_CODE    	= p_ship_arrival_date_rec.order_date_type,
4972 		SHIP_SET_NAME			= p_ship_arrival_date_rec.ship_set_name,
4973 		ARRIVAL_SET_NAME		= p_ship_arrival_date_rec.arrival_set_name,
4974 		ATP_OVERRIDE_FLAg		= decode(upper(p_ship_arrival_date_rec.atp_override_flag),'Y',1,2),
4975 		REQUEST_DATE			= p_ship_arrival_date_rec.request_arrival_date
4976 	WHERE	DEMAND_ID                       = p_ship_arrival_date_rec.demand_id
4977 	AND	SR_INSTANCE_ID                  = p_ship_arrival_date_rec.instance_id;
4978 
4979 EXCEPTION
4980     WHEN OTHERS THEN
4981     	msc_sch_wb.atp_debug('Flush_Data_In_Ods : '||sqlerrm);
4982         x_return_status := FND_API.G_RET_STS_ERROR;
4983 
4984 END Flush_Data_In_Ods;
4985 
4986 --bug 3766179: add new procedure to add supplies irrespective of supply type
4987 PROCEDURE Add_Supplies ( p_supply_rec_type IN OUT NOCOPY MSC_ATP_DB_UTILS.supply_rec_typ)
4988 
4989 IS
4990     temp_sd_qty number;
4991 
4992     -- time_phased_atp
4993     l_time_phased_atp      varchar2(1) := 'N';
4994     l_insert_item_id       number;
4995 
4996 BEGIN
4997     IF PG_DEBUG in ('Y', 'C') THEN
4998         msc_sch_wb.atp_debug('***** Begin Add_Supplies Procedure *****');
4999         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_supply_rec_type.order_quantity := ' || p_supply_rec_type.order_quantity);
5000         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_inventory_item_id := ' || p_supply_rec_type.inventory_item_id);
5001         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_schedule_date := ' || p_supply_rec_type.schedule_date);
5002         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_organization_id := ' || p_supply_rec_type.organization_id);
5003         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_schedule_date := ' || p_supply_rec_type.schedule_date);
5004         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_supplier_id := ' || p_supply_rec_type.supplier_id);
5005         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_supplier_site_id := ' || p_supply_rec_type.supplier_site_id);
5006         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_refresh_number := ' || p_supply_rec_type.refresh_number);
5007         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_request_item_id := ' || p_supply_rec_type.request_item_id);
5008         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_atf_date := ' || p_supply_rec_type.atf_date);
5009         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_new_dock_date := ' || p_supply_rec_type.new_dock_date);
5010         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_new_ship_date := ' || p_supply_rec_type.new_ship_date);
5011         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_start_date := ' || p_supply_rec_type.start_date);
5012         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_order_date := ' || p_supply_rec_type.order_date);
5013         msc_sch_wb.atp_debug('Add_Supplies: ' || 'p_intransit_lead_time := ' || p_supply_rec_type.intransit_lead_time); --4127630
5014     END IF;
5015 
5016     p_supply_rec_type.return_status := FND_API.G_RET_STS_SUCCESS;
5017 
5018     -- time_phased_atp changes begin
5019     IF (p_supply_rec_type.inventory_item_id <> p_supply_rec_type.request_item_id) and p_supply_rec_type.atf_date is not null THEN
5020         l_time_phased_atp := 'Y';
5021         /* In time phased atp scenarios add planned order in msc_supplies for member item*/
5022         l_insert_item_id := p_supply_rec_type.request_item_id;
5023         IF PG_DEBUG in ('Y', 'C') THEN
5024             msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'Time Phased ATP = ' || l_time_phased_atp);
5025         END IF;
5026     ELSE
5027         l_insert_item_id := p_supply_rec_type.inventory_item_id;
5028     END IF;
5029     IF PG_DEBUG in ('Y', 'C') THEN
5030         msc_sch_wb.atp_debug('Add_Supplies: ' || 'Insert planned order in msc_supplies for ' || l_insert_item_id);
5031     END IF;
5032     -- time_phased_atp changes end
5033 
5034     -- Insert updated quantity as a Planned Order in msc_supplies
5035     INSERT into MSC_SUPPLIES
5036            (plan_id,
5037             transaction_id,
5038             organization_id,
5039             sr_instance_id,
5040             inventory_item_id,
5041             last_update_date,
5042             last_updated_by,
5043             creation_date,
5044             created_by,
5045             new_schedule_date,
5046             disposition_status_type, -- 1512366
5047             order_type,
5048             new_order_quantity,
5049             supplier_id,
5050             supplier_site_id,
5051             -- rajjain 02/19/2003 Bug 2788302 Begin
5052             source_supplier_id,
5053             source_supplier_site_id,
5054             source_sr_instance_id,
5055             source_organization_id,
5056             process_seq_id,
5057             -- rajjain 02/19/2003 Bug 2788302 End
5058             firm_planned_type,
5059             demand_class,
5060             customer_id,
5061             ship_to_site_id,
5062             record_source,-- for plan order pegging, rmehra
5063             refresh_number, -- for summary enhancement
5064             -- ship_rec_cal changes begin
5065             new_dock_date,
5066             new_ship_date,
5067             new_wip_start_date,         -- Bug 3241766
5068             new_order_placement_date,   -- Bug 3241766
5069             ship_calendar,
5070             receiving_calendar,
5071             intransit_calendar,
5072             ship_method,
5073             -- ship_rec_cal changes end
5074             disposition_id,
5075             INTRANSIT_LEAD_TIME --4127630
5076             )
5077     VALUES (p_supply_rec_type.plan_id,
5078             msc_supplies_s.nextval,
5079             p_supply_rec_type.organization_id,
5080             p_supply_rec_type.instance_id,
5081             l_insert_item_id, -- for time_phased_atp
5082             sysdate,
5083             FND_GLOBAL.USER_ID,
5084             sysdate,
5085             FND_GLOBAL.USER_ID,
5086             -- For bug 2259824, move the supply to the end of day
5087             TRUNC(p_supply_rec_type.schedule_date) + MSC_ATP_PVT.G_END_OF_DAY,
5088             p_supply_rec_type.disposition_status_type, -- 1512366: open
5089             p_supply_rec_type.supply_type,
5090             p_supply_rec_type.order_quantity,
5091             p_supply_rec_type.supplier_id,
5092             p_supply_rec_type.supplier_site_id,
5093             -- rajjain 02/19/2003 Bug 2788302 Begin
5094             p_supply_rec_type.supplier_id,
5095             p_supply_rec_type.supplier_site_id,
5096             p_supply_rec_type.source_sr_instance_id,
5097             p_supply_rec_type.source_organization_id,
5098             p_supply_rec_type.process_seq_id,
5099             -- rajjain 02/19/2003 Bug 2788302 End
5100             p_supply_rec_type.firm_planned_type,
5101             --decode(MSC_ATP_PVT.G_HIERARCHY_PROFILE, 1, p_demand_class, null),
5102             ---2424357: after demand priority p_demand_class contains converted demand calss
5103             --- we always store the actual demand class passes as a part of the query.
5104             -- Bug 3558125 - Populate '-1' if G_ATP_DEMAND_CLASS is NULL
5105             decode(MSC_ATP_PVT.G_HIERARCHY_PROFILE, 1,NVL(MSC_ATP_PVT.G_ATP_DEMAND_CLASS,'-1'), null),
5106             -- decode(MSC_ATP_PVT.G_HIERARCHY_PROFILE, 1,MSC_ATP_PVT.G_ATP_DEMAND_CLASS, null),
5107             NVL(MSC_ATP_PVT.G_PARTNER_ID,-1),           -- Bug 3558125
5108             NVL(MSC_ATP_PVT.G_PARTNER_SITE_ID,-1),      -- Bug 3558125
5109             p_supply_rec_type.record_source, -- for plan order pegging
5110             p_supply_rec_type.refresh_number, -- for summary enhancement
5111             -- ship_rec_cal changes begin
5112             -- Bug 3821358, Making the dates at the end of the day.
5113             TRUNC(p_supply_rec_type.new_dock_date) + MSC_ATP_PVT.G_END_OF_DAY,
5114             TRUNC(p_supply_rec_type.new_ship_date) + MSC_ATP_PVT.G_END_OF_DAY,
5115             TRUNC(p_supply_rec_type.start_date) + MSC_ATP_PVT.G_END_OF_DAY,       -- Bug 3241766
5116             TRUNC(p_supply_rec_type.order_date) + MSC_ATP_PVT.G_END_OF_DAY,       -- Bug 3241766
5117             p_supply_rec_type.shipping_cal_code,
5118             p_supply_rec_type.receiving_cal_code,
5119             p_supply_rec_type.intransit_cal_code,
5120             p_supply_rec_type.ship_method,
5121             -- ship_rec_cal changes end
5122             --bug 3766179
5123             p_supply_rec_type.disposition_id,
5124             p_supply_rec_type.intransit_lead_time --4127630
5125             )
5126     RETURNING transaction_id INTO p_supply_rec_type.transaction_id;
5127 
5128    -- time_phased_atp
5129    IF (p_supply_rec_type.inventory_item_id <> p_supply_rec_type.request_item_id) AND (p_supply_rec_type.atf_date is not null) THEN
5130         MSC_ATP_PF.Add_PF_Rollup_Supplies(
5131                 p_supply_rec_type.plan_id,
5132                 p_supply_rec_type.request_item_id,
5133                 p_supply_rec_type.inventory_item_id,
5134                 p_supply_rec_type.organization_id,
5135                 p_supply_rec_type.instance_id,
5136                 p_supply_rec_type.demand_class,
5137                 p_supply_rec_type.schedule_date,
5138                 p_supply_rec_type.supply_type,
5139                 p_supply_rec_type.order_quantity,
5140                 p_supply_rec_type.transaction_id,
5141                 p_supply_rec_type.atf_date,
5142                 p_supply_rec_type.refresh_number,
5143                 p_supply_rec_type.return_status
5144         );
5145         IF p_supply_rec_type.return_status <> FND_API.G_RET_STS_SUCCESS THEN
5146                 IF PG_DEBUG in ('Y', 'C') THEN
5147                         msc_sch_wb.atp_debug('Add_Supplies: ' || 'Error occured in procedure Add_PF_Rollup_Supplies');
5148                 END IF;
5149                 RAISE FND_API.G_EXC_ERROR;
5150         END IF;
5151 
5152    --  Allocated ATP Based on Planning Details -- Agilent changes Begin
5153    ELSIF ((MSC_ATP_PVT.G_INV_CTP = 4) AND
5154        (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
5155        (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
5156        (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
5157 
5158         IF PG_DEBUG in ('Y', 'C') THEN
5159             msc_sch_wb.atp_debug('Add_Supplies: ' || ' before insert into' ||
5160                                 ' msc_alloc_supplies');
5161         END IF;
5162 
5163         INSERT INTO MSC_ALLOC_SUPPLIES(
5164                 PLAN_ID,
5165                 INVENTORY_ITEM_ID,
5166                 ORGANIZATION_ID,
5167                 SR_INSTANCE_ID,
5168                 DEMAND_CLASS,
5169                 SUPPLY_DATE,
5170                 PARENT_TRANSACTION_ID,
5171                 ALLOCATED_QUANTITY,
5172                 ORDER_TYPE,
5173                 CREATED_BY,
5174                 CREATION_DATE,
5175                 LAST_UPDATED_BY,
5176                 LAST_UPDATE_DATE,
5177                 REFRESH_NUMBER
5178                )
5179         VALUES (
5180                 p_supply_rec_type.plan_id,
5181                 l_insert_item_id,
5182                 p_supply_rec_type.organization_id,
5183                 p_supply_rec_type.instance_id,
5184                 p_supply_rec_type.demand_class,
5185                 p_supply_rec_type.schedule_date,
5186                 p_supply_rec_type.transaction_id,
5187                 p_supply_rec_type.order_quantity,
5188                 p_supply_rec_type.supply_type,
5189                 FND_GLOBAL.USER_ID,
5190                 sysdate,
5191                 FND_GLOBAL.USER_ID,
5192                 sysdate,
5193                 p_supply_rec_type.refresh_number); -- for summary enhancement
5194     END IF;
5195 
5196     --  Allocated ATP Based on Planning Details -- Agilent changes End
5197 
5198     -- Code to make summary updates and commit removed for summary enhancement
5199     /** code commented for time being. Will be removed after code review
5200     IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
5201         IF PG_DEBUG in ('Y', 'C') THEN
5202             msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'Create planned orders for summary mode');
5203             msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'First try to update ');
5204         END IF;
5205 
5206         MSC_ATP_PROC.SHOW_SUMMARY_QUANTITY(p_supply_rec_type.instance_id,
5207                                           p_supply_rec_type.plan_id,
5208                                           p_supply_rec_type.organization_id,
5209                                           p_supply_rec_type.inventory_item_id,
5210                                           p_supply_rec_type.schedule_date,
5211                                           null,
5212                                           null,
5213                                           null,
5214                                           null,
5215                                           null,
5216                                           null,
5217                                           2);
5218 
5219         update /*+ INDEX(msc_atp_summary_sd MSC_ATP_SUMMARY_SD_U1) *//* msc_atp_summary_sd
5220         set sd_qty = sd_qty + p_supply_rec_type.order_quantity
5221         where inventory_item_id =  p_supply_rec_type.inventory_item_id
5222         and   plan_id = p_supply_rec_type.plan_id
5223         and   sr_instance_id = p_supply_rec_type.instance_id
5224         and   organization_id = p_supply_rec_type.organization_id
5225         and   sd_date = trunc(p_supply_rec_type.schedule_date);
5226         commit;
5227 
5228         IF PG_DEBUG in ('Y', 'C') THEN
5229             msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'After update  to summary table');
5230         END IF;
5231         IF SQL%NOTFOUND THEN
5232             IF PG_DEBUG in ('Y', 'C') THEN
5233                 msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'Couldnt update try to insert');
5234             END IF;
5235 
5236             MSC_ATP_DB_UTILS.INSERT_SUMMARY_SD_ROW(p_supply_rec_type.plan_id,
5237                                                p_supply_rec_type.instance_id,
5238                                                p_supply_rec_type.organization_id,
5239                                                p_supply_rec_type.inventory_item_id,
5240                                                p_supply_rec_type.schedule_date,
5241                                                p_supply_rec_type.order_quantity,
5242                                                '@@@');
5243         END IF;
5244         IF PG_DEBUG in ('Y', 'C') THEN
5245             msc_sch_wb.atp_debug('Add_Planned_Order: ' || 'commit in summary mode');
5246         END IF;
5247         commit;
5248     END IF;
5249     ***/
5250 
5251     IF PG_DEBUG in ('Y', 'C') THEN
5252         msc_sch_wb.atp_debug('Add_Supplies: ' || 'transaction_id = '||p_supply_rec_type.transaction_id);
5253         msc_sch_wb.atp_debug('***** End Add_Supplies Procedure *****');
5254     END IF;
5255 
5256 END Add_Supplies;
5257 
5258 
5259 --3720018, new procedure to call delete row for line/set/request level.
5260 Procedure call_delete_row (
5261 p_instance_id              IN    NUMBER,
5262 p_atp_table                IN    MRP_ATP_PUB.ATP_Rec_Typ,
5263 p_refresh_number           IN    NUMBER,
5264 x_delete_atp_rec           OUT   NoCopy MSC_ATP_PVT.DELETE_ATP_REC,
5265 x_return_status      	   OUT   NoCopy VARCHAR2
5266 ) IS
5267 i                                 PLS_INTEGER := 1;
5268 m			          PLS_INTEGER := 1;
5269 l_count                           NUMBER;
5270 l_line_id_count                   NUMBER;
5271 l_so_tbl_status                   PLS_INTEGER;
5272 l_summary_flag                    number;
5273 l_old_plan_id                     MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
5274 l_atf_dates                       MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
5275 l_old_pf_item_id                  number;
5276 l_pf_item_id		          MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5277 l_plan_id                         NUMBER := -1;
5278 l_return_status		          VARCHAR2(10);
5279 l_temp_assign_set_id              NUMBER;
5280 l_time_phased_atp                 VARCHAR2(1) := 'N';
5281 l_time_phased_set                 VARCHAR2(1) := 'N';
5282 l_demand_ids                      MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5283 l_inv_item_ids                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5284 l_plan_info_rec                   MSC_ATP_PVT.plan_info_rec;
5285 l_del_demand_ids                  MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5286 l_del_inv_item_ids                MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5287 l_del_plan_ids                    MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5288 l_del_identifiers                 MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();
5289 l_del_demand_source_type          MRP_ATP_PUB.number_arr:=MRP_ATP_PUB.number_arr();--cmro
5290 l_delete_demand_flag              PLS_INTEGER;
5291 l_copy_demand_ids                 mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
5292 l_del_copy_demand_ids             mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
5293 l_del_copy_demand_plan_ids        mrp_atp_pub.number_arr := mrp_atp_pub.number_arr();
5294 l_atp_peg_items                   MRP_ATP_PUB.Number_Arr ;
5295 l_atp_peg_demands                 MRP_ATP_PUB.Number_Arr ;
5296 l_atp_peg_supplies                MRP_ATP_PUB.Number_Arr ;
5297 l_atp_peg_res_reqs                MRP_ATP_PUB.Number_Arr ;
5298 l_demand_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
5299 l_supply_instance_id              MRP_ATP_PUB.Number_Arr ; --Bug 3629191
5300 l_res_instance_id                 MRP_ATP_PUB.Number_Arr ; --Bug 3629191
5301 l_del_atp_peg_items               MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5302 l_del_atp_peg_demands             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5303 l_del_atp_peg_supplies            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5304 l_del_atp_peg_res_reqs            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5305 l_atp_peg_demands_plan_ids        MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5306 l_atp_peg_supplies_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5307 l_atp_peg_res_reqs_plan_ids       MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5308 l_off_demand_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5309 l_off_supply_instance_id          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5310 l_off_res_instance_id             MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --Bug 3629191
5311 l_del_ods_demand_ids              MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5312 l_del_ods_inv_item_ids            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5313 l_del_ods_demand_src_type         MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5314 l_ods_cto_demand_ids              MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5315 l_ods_cto_inv_item_ids            MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5316 l_del_ods_cto_demand_ids          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5317 l_del_ods_cto_inv_item_ids        MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5318 l_del_ods_cto_dem_src_type        MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr(); --3720018, added for support of rescheduling in ODS
5319 l_ods_atp_refresh_no          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5320 l_ods_cto_atp_refresh_no          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5321 l_del_ods_atp_refresh_no          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5322 l_del_ods_cto_atp_refresh_no          MRP_ATP_PUB.Number_Arr := MRP_ATP_PUB.Number_Arr();
5323 l_delete_atp_rec                  MSC_ATP_PVT.DELETE_ATP_REC;
5324 l_attribute_07                    MRP_ATP_PUB.char30_Arr := MRP_ATP_PUB.char30_Arr();
5325 l_alloc_atp varchar2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; -- ALLOC ATP CHANGES, 12973673
5326 
5327 BEGIN
5328        IF PG_DEBUG in ('Y', 'C') THEN
5329            msc_sch_wb.atp_debug('*** Begin Call_delete_row ***');
5330        END IF;
5331 
5332        x_return_status := FND_API.G_RET_STS_SUCCESS;
5333 
5334        l_demand_ids.Extend(p_atp_table.action.count);
5335        l_inv_item_ids.extend(p_atp_table.action.count);
5336        x_delete_atp_rec.error_code.extend(p_atp_table.action.count);
5337 
5338        SELECT so_tbl_status,
5339                 NVL(summary_flag,1)
5340        INTO   l_so_tbl_status,
5341                 l_summary_flag
5342        FROM   msc_apps_instances
5343        WHERE  instance_id = p_instance_id;
5344 
5345        IF PG_DEBUG in ('Y', 'C') THEN
5346           msc_sch_wb.atp_debug('Call_delete_row: ' || 'l_so_tbl_status = '||l_so_tbl_status);
5347           msc_sch_wb.atp_debug('Call_delete_row: ' || 'l_summary_flag = ' || l_summary_flag);
5348        END IF;
5349        IF NVL(l_so_tbl_status, 1) = 2 THEN
5350             -- not available for atp
5351           IF PG_DEBUG in ('Y', 'C') THEN
5352               msc_sch_wb.atp_debug('Call_delete_row: ' || 'ATP not available');
5353           END IF;
5354           x_delete_atp_rec.error_code(1) := MSC_ATP_PVT.TRY_ATP_LATER;
5355           RETURN;
5356 
5357        ELSIF l_summary_flag = 1 THEN
5358               ---- summary table is not ready. Switch to detail tables
5359           IF PG_DEBUG in ('Y', 'C') THEN
5360                msc_sch_wb.atp_debug('Call_delete_row: ' || ' summary table is not ready. Switch to detail tables');
5361           END IF;
5362           MSC_ATP_PVT.G_SUMMARY_FLAG := 'N';
5363 
5364        ELSIF l_summary_flag = 2 THEN
5365             IF PG_DEBUG in ('Y', 'C') THEN
5366                msc_sch_wb.atp_debug('Call_delete_row: ' || 'Summary Concurrent program is running');
5367             END IF;
5368             x_delete_atp_rec.error_code(1) := MSC_ATP_PVT.SUMM_CONC_PROG_RUNNING;
5369             RETURN;
5370        END IF;
5371 
5372        IF (p_atp_table.Action.COUNT > 0 ) THEN
5373           -- ship set or arrival set exists
5374            l_old_plan_id.Extend(p_atp_table.action.count);
5375            l_atf_dates.extend(p_atp_table.action.count); -- for time_phased_atp
5376            l_attribute_07.extend(p_atp_table.action.count);
5377 
5378            FOR m in 1.. p_atp_table.Action.COUNT LOOP
5379 	      ---l_old_plan_id.Extend;
5380               -- we need to get the plan_id based on old org and old demand
5381               -- class
5382               --IF MSC_ATP_PVT.G_INV_CTP = 4 THEN  --anuarg
5383               IF p_atp_table.old_source_organization_id(m) IS NOT NULL THEN -- we want to find plan only in case
5384                                                                               -- of reschedulling
5385                    ----Subst: Get pf id for old inv item if
5386                  IF NVL(p_atp_table.old_inventory_item_id(m), p_atp_table.inventory_item_id(m))
5387                                                                   = p_atp_table.inventory_item_id(m) THEN
5388                     l_old_pf_item_id := MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
5389                                             l_plan_id,
5390                                             p_atp_table.inventory_item_id(m),
5391                                             p_atp_table.old_source_organization_id(m));
5392                  ELSE
5393                       l_old_pf_item_id :=  MSC_ATP_PF.Get_PF_Atp_Item_Id(p_instance_id,
5394                                                            l_plan_id,
5395                                                            p_atp_table.old_inventory_item_id(m),
5396                                                            p_atp_table.old_source_organization_id(m));
5397                  END IF;
5398                  IF PG_DEBUG in ('Y', 'C') THEN
5399                       msc_sch_wb.atp_debug('Call_delete_row: ' || 'l_old_pf_item_id := ' || l_old_pf_item_id);
5400                  END IF;
5401               END IF;
5402               IF MSC_ATP_PVT.G_INV_CTP = 4 THEN  --3720018, moved from above
5403                    /* time_phased_atp changes begin
5404                       Call new procedure Get_PF_Plan_Info*/
5405                 IF p_atp_table.old_source_organization_id(m) IS NOT NULL THEN
5406                    MSC_ATP_PF.Get_PF_Plan_Info(
5407                                p_instance_id,
5408                                NVL(p_atp_table.old_inventory_item_id(m), p_atp_table.inventory_item_id(m)),
5409                                l_old_pf_item_id,
5410                                p_atp_table.old_source_organization_id(m),
5411                                p_atp_table.old_demand_class(m),
5412                                l_atf_dates(m),
5413                                x_delete_atp_rec.error_code(m), -- l_atp_table.error_code(m), --3720018
5414                                l_return_status,
5415                                NULL --bug3510475
5416                    );
5417 
5418                    IF PG_DEBUG in ('Y', 'C') THEN
5419                       msc_sch_wb.atp_debug('Call_delete_row: l_return_status:= ' || l_return_status);
5420                    END IF;
5421 
5422                    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5423                       IF PG_DEBUG in ('Y', 'C') THEN
5424                          msc_sch_wb.atp_debug('Call_delete_row: ' || 'ATP Downtime during Re-schedule');
5425                       END IF;
5426                       x_return_status := FND_API.G_RET_STS_ERROR;
5427                       return; -- Return in case of error in get_pf_plan_info, no point in proceding further.
5428                    END IF;
5429                    /* time_phased_atp changes end*/
5430                    IF PG_DEBUG in ('Y', 'C') THEN
5431                       msc_sch_wb.atp_debug('Call_delete_row: ' || 'reached here2');
5432                    END IF;
5433 
5434                    l_plan_info_rec := MSC_ATP_PVT.G_PLAN_INFO_REC;
5435 
5436                    IF PG_DEBUG in ('Y', 'C') THEN
5437                       msc_sch_wb.atp_debug('Call_delete_row: ' || 'l_plan_info_rec.plan_id = '||l_plan_info_rec.plan_id);
5438                    END IF;
5439 
5440                    l_old_plan_id(m)            := l_plan_info_rec.plan_id;
5441                    l_temp_assign_set_id        := l_plan_info_rec.assignment_set_id;
5442 
5443                    --3720018
5444                    IF l_attribute_07.Exists(m) THEN
5445                       l_attribute_07(m) := l_plan_info_rec.plan_name;
5446                    END IF; --3720018
5447 
5448                    IF PG_DEBUG in ('Y', 'C') THEN
5449                       msc_sch_wb.atp_debug('Call_delete_row: ' || 'old_source_organization_id = '|| p_atp_table.old_source_organization_id(m));
5450                       msc_sch_wb.atp_debug('Call_delete_row: ' || 'old_demand_class = '|| p_atp_table.old_demand_class(m));
5451                    END IF;
5452                 END IF;
5453               ELSE
5454                  IF p_atp_table.old_source_organization_id(m) IS NOT NULL THEN
5455                      l_old_plan_id(m) := l_plan_id;
5456                  END IF;
5457               END IF;
5458 
5459               IF PG_DEBUG in ('Y', 'C') THEN
5460                  msc_sch_wb.atp_debug('Call_delete_row: ' || 'l_old_plan_id = '||l_old_plan_id(m));
5461               END IF;
5462 
5463               -- 2152184: We remove rows only if we have old plan id
5464               IF l_old_plan_id(m) IS NOT NULL THEN
5465                  ---subst: add demand field
5466                  ---bug 2384224: delete demand only if it has not been deleted
5467                  l_delete_demand_flag := 1;
5468                  --IF MSC_ATP_PVT.G_INV_CTP = 4 THEN  --3720018, removed this condition
5469                     -- Chagned the following loop
5470                     --FOR i in 1..l_del_demand_ids.count LOOP
5471                     --    IF p_atp_table.Identifier(m) = l_del_identifiers(i) and l_old_plan_id(m) = l_del_plan_ids(i) THEN
5472                     --        IF PG_DEBUG in ('Y', 'C') THEN
5473                     --           msc_sch_wb.atp_debug('Call_delete_row: ' || 'demand has already been deleted');
5474                     --        END IF;
5475                     --        l_delete_demand_flag := 0;
5476                     --        EXIT;
5477                     --    END IF;
5478                     --END LOOP;
5479                     -- l_del_demand_ids.count might not be in sync with
5480                     -- l_del_identifiers.count
5481                     FOR i in 1..l_del_identifiers.count LOOP
5482                         IF p_atp_table.Identifier(m) = l_del_identifiers(i) THEN
5483                           FOR i in 1..l_del_demand_ids.count LOOP
5484                             IF  l_old_plan_id(m) = l_del_plan_ids(i) THEN
5485                               IF PG_DEBUG in ('Y', 'C') THEN
5486                                msc_sch_wb.atp_debug('Call_delete_row: ' || 'demand has already been deleted');
5487                               END IF;
5488                               l_delete_demand_flag := 0;
5489                               EXIT;
5490                             END IF;
5491                           END LOOP;
5492                           IF l_delete_demand_flag = 0 THEN
5493                             EXIT;
5494                           END IF;
5495                         END IF;
5496                     END LOOP;
5497                  --END IF;  --3720018
5498                  IF (MSC_ATP_PVT.G_INV_CTP = 5) or  (MSC_ATP_PVT.G_INV_CTP = 4 and l_delete_demand_flag = 1) THEN
5499                     /* time_phased_atp */
5500                     IF l_atf_dates(m) is not null THEN
5501                         l_time_phased_atp := 'Y';
5502                     END IF;
5503 
5504                     IF l_time_phased_set <> 'Y' and l_time_phased_atp = 'Y' THEN
5505                        l_time_phased_set := 'Y';
5506                     END IF;
5507 
5508                     -- ALLOC ATP CHANGES, 12973673
5509 					IF (MSC_ATP_PVT.G_INV_CTP = 4 and MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y') THEN
5510 						IF PG_DEBUG in ('Y', 'C') THEN
5511 							msc_sch_wb.atp_debug('Call_delete_row: ' || 'G_HYBRID_ALLOC_ATP is Y');
5512 						END IF;
5513 
5514 						MSC_ATP_PVT.G_ALLOCATED_ATP := MSC_ATP_FUNC.check_alloc_exists(p_instance_id,
5515 													-1, --PLAN_ID
5516 													p_atp_table.Inventory_Item_Id(m),
5517 													p_atp_table.old_source_organization_id(m),
5518 													nvl(p_atp_table.Requested_Arrival_Date(m), p_atp_table.Requested_Ship_Date(m)));
5519 
5520 						IF PG_DEBUG in ('Y', 'C') THEN
5521 							msc_sch_wb.atp_debug('Call_delete_row: ' || 'MSC_ATP_PVT.G_ALLOCATED_ATP = '||MSC_ATP_PVT.G_ALLOCATED_ATP);
5522 						END IF;
5523 					END IF;
5524 
5525                     MSC_ATP_DB_UTILS.Delete_Row(
5526                        -- CTO re-arch Changes to deal with "across-plan" cases
5527                        p_atp_table.Identifier(m),
5528                        p_atp_table.Config_item_line_id(m),
5529                        -- CTO re-arch
5530                        l_old_plan_id(m),
5531                        p_instance_id,
5532                        p_refresh_number, -- Bug 2831298 Ensure that the refresh_number is updated.
5533                        p_atp_table.order_number(m), -- Bug 2840734 : populate numeric order no.
5534                        l_time_phased_atp,  -- For time_phased_atp
5535                        p_atp_table.ato_model_line_id(m),
5536                        --subst
5537                        p_atp_table.demand_source_type(m), --cmro
5538                        p_atp_table.old_source_organization_id(m), --bug#9055675 --Bug 7118988
5539                        l_demand_ids,
5540                        l_inv_item_ids,
5541                        l_copy_demand_ids,  -- For summary enhancement
5542                        l_atp_peg_items  ,
5543                        l_atp_peg_demands ,
5544                        l_atp_peg_supplies,
5545                        l_atp_peg_res_reqs,
5546                        l_demand_instance_id, --Bug 3629191
5547                        l_supply_instance_id, --Bug 3629191
5548                        l_res_instance_id,    --Bug 3629191
5549                        l_ods_cto_demand_ids,  --3720018, added for support of rescheduling in ODS
5550                        l_ods_cto_inv_item_ids, --3720018, added for support of rescheduling in ODS
5551                        l_ods_atp_refresh_no,
5552                        l_ods_cto_atp_refresh_no
5553                        -- End CTO ODR and Simplified Pegging
5554                        );
5555                        --l_demand_class_flag);
5556 
5557                     IF PG_DEBUG in ('Y', 'C') THEN
5558                        msc_sch_wb.atp_debug('Call_delete_row: After call to Delete_Row' );
5559                     END IF;
5560 
5561                     --IF MSC_ATP_PVT.G_INV_CTP = 4  and l_old_plan_id(m) <> -1 THEN  --3720018
5562                     IF (l_old_plan_id(m) = -1) THEN
5563                            IF l_demand_ids IS NOT NULL and l_demand_ids.count > 0 THEN
5564                               l_count := l_del_ods_demand_ids.count;
5565                               l_del_ods_demand_ids.extend(l_demand_ids.count);
5566                               l_del_ods_inv_item_ids.extend(l_demand_ids.count);
5567                               l_del_ods_demand_src_type.extend(l_demand_ids.count);
5568                               l_del_ods_atp_refresh_no.extend(l_demand_ids.count);
5569                               --l_del_identifiers.extend(l_demand_ids.count);
5570                               FOR i in 1..l_demand_ids.count LOOP
5571                                   l_del_ods_demand_ids(l_count + i) := l_demand_ids(i);
5572                                   l_del_ods_inv_item_ids(l_count + i) := l_inv_item_ids(i);
5573                                   l_del_ods_demand_src_type(l_count + i):= p_atp_table.demand_source_type(m);
5574                                   l_del_ods_atp_refresh_no(l_count + i) := l_ods_atp_refresh_no(i);
5575                               END LOOP;
5576                            END IF;
5577                            IF l_ods_cto_demand_ids IS NOT NULL and l_ods_cto_demand_ids.count > 0 THEN
5578                                  l_count := l_del_ods_cto_demand_ids.count;
5579                                  l_del_ods_cto_demand_ids.extend(l_ods_cto_demand_ids.count);
5580                                  l_del_ods_cto_inv_item_ids.extend(l_ods_cto_demand_ids.count);
5581                                  l_del_ods_cto_dem_src_type.extend(l_ods_cto_demand_ids.count);
5582                                  l_del_ods_cto_atp_refresh_no.extend(l_ods_cto_demand_ids.count);
5583                                  FOR i in 1..l_ods_cto_demand_ids.count LOOP
5584                                      l_del_ods_cto_demand_ids(l_count + i) := l_ods_cto_demand_ids(i);
5585                                      l_del_ods_cto_inv_item_ids(l_count + i) := l_ods_cto_inv_item_ids(i);
5586                                      l_del_ods_cto_dem_src_type(l_count + i) := p_atp_table.demand_source_type(m);
5587                                      l_del_ods_cto_atp_refresh_no(l_count + i) := l_ods_cto_atp_refresh_no(i);
5588                                  END LOOP;
5589                            END IF;
5590                            --5357370
5591                            IF MSC_ATP_PVT.G_SUMMARY_FLAG = 'Y' THEN
5592                               l_del_identifiers.extend(p_atp_table.Identifier.count);
5593                               l_del_identifiers := p_atp_table.Identifier;
5594                            END IF;
5595 
5596                     ELSE
5597                            l_count := l_del_demand_ids.count;
5598                            l_del_demand_ids.extend(l_demand_ids.count);
5599                            l_del_plan_ids.extend(l_demand_ids.count);
5600                            l_del_inv_item_ids.extend(l_demand_ids.count);
5601                            --l_del_identifiers.extend(l_demand_ids.count);
5602                            FOR i in 1..l_demand_ids.count LOOP
5603                                   l_del_demand_ids(l_count + i) := l_demand_ids(i);
5604                                   l_del_plan_ids(l_count + i) := l_old_plan_id(m);
5605                                   l_del_inv_item_ids(l_count + i) := l_inv_item_ids(i);
5606                            END LOOP;
5607                            l_line_id_count := l_del_identifiers.count;
5608                            FOR i in 1..l_demand_ids.count LOOP
5609                                l_line_id_count := l_line_id_count + 1;
5610                                l_del_identifiers.extend();
5611                                l_del_demand_source_type.extend();--cmro
5612                                l_del_identifiers(l_line_id_count) := p_atp_table.Identifier(m);
5613                                l_del_demand_source_type(l_line_id_count):=p_atp_table.demand_source_type(m);--cmro
5614                                IF p_atp_table.Config_item_line_id(m) IS NOT NULL THEN
5615                                   l_line_id_count := l_line_id_count + 1;
5616                                   l_del_identifiers.extend();
5617                                   l_del_demand_source_type.extend();--cmro
5618                                   l_del_identifiers(l_line_id_count) :=
5619                                              p_atp_table.Config_item_line_id(m);
5620                                   l_del_demand_source_type(l_line_id_count):=
5621                                              p_atp_table.demand_source_type(m);--cmro
5622                                END IF;
5623                            END LOOP;
5624 
5625                            -- Append copy SO ids for summary enhancement
5626                            IF l_copy_demand_ids IS NOT NULL and l_copy_demand_ids.COUNT > 0 THEN
5627                                l_count := l_del_copy_demand_ids.count;
5628                                l_del_copy_demand_ids.extend(l_copy_demand_ids.count);
5629                                l_del_copy_demand_plan_ids.extend(l_copy_demand_ids.count);
5630                                FOR i in 1..l_copy_demand_ids.count LOOP
5631                                    l_del_copy_demand_ids(l_count + i) := l_copy_demand_ids(i);
5632                                    l_del_copy_demand_plan_ids(l_count + i) := l_old_plan_id(m);
5633                                END LOOP;
5634                            END IF;
5635                            -- CTO ODR and Simplified Pegging
5636 
5637                            -- This check not necessary but used as an insurance.
5638                            IF PG_DEBUG in ('Y', 'C') THEN
5639                               msc_sch_wb.atp_debug('Call_delete_row: l_atp_peg_items.count ' || l_atp_peg_items.count);
5640                            END IF;
5641                            IF l_atp_peg_items IS NOT NULL AND l_atp_peg_items.count > 0 THEN
5642                              l_count := l_atp_peg_items.count;
5643                              l_del_atp_peg_items.extend(l_count);
5644                              l_count := l_del_atp_peg_items.count - l_atp_peg_items.count;
5645                              FOR i in 1..l_atp_peg_items.count LOOP
5646                                l_del_atp_peg_items(l_count + i) := l_atp_peg_items(i);
5647                              END LOOP;
5648                            END IF;
5649                            IF PG_DEBUG in ('Y', 'C') THEN
5650                               msc_sch_wb.atp_debug('Call_delete_row: l_atp_peg_demands.count ' || l_atp_peg_demands.count);
5651                            END IF;
5652                            IF l_atp_peg_demands IS NOT NULL AND l_atp_peg_demands.count > 0 THEN
5653                              l_count := l_atp_peg_demands.count;
5654                              l_del_atp_peg_demands.extend(l_count);
5655                              l_atp_peg_demands_plan_ids.extend(l_count);
5656                              l_off_demand_instance_id.extend(l_count);
5657                              l_count := l_del_atp_peg_demands.count - l_atp_peg_demands.count;
5658                              FOR i in 1..l_atp_peg_demands.count LOOP
5659                                  l_del_atp_peg_demands(l_count + i) := l_atp_peg_demands(i);
5660                                  l_atp_peg_demands_plan_ids(l_count + i) := l_old_plan_id(m); --Bug 3629191
5661                                  l_off_demand_instance_id(l_count + i) := l_demand_instance_id(i); --Bug 3629191
5662                              END LOOP;
5663                            END IF;
5664                            IF PG_DEBUG in ('Y', 'C') THEN
5665                               msc_sch_wb.atp_debug('Call_delete_row: l_atp_peg_supplies.count ' || l_atp_peg_supplies.count);
5666                            END IF;
5667                            IF l_atp_peg_supplies IS NOT NULL AND l_atp_peg_supplies.count > 0 THEN
5668                              l_count := l_atp_peg_supplies.count;
5669                              l_del_atp_peg_supplies.extend(l_count);
5670                              l_atp_peg_supplies_plan_ids.extend(l_count);
5671                              l_off_supply_instance_id.extend(l_count);
5672                              l_count := l_del_atp_peg_supplies.count - l_atp_peg_supplies.count;
5673                              FOR i in 1..l_atp_peg_supplies.count LOOP
5674                                l_del_atp_peg_supplies(l_count + i) := l_atp_peg_supplies(i);
5675                                l_atp_peg_supplies_plan_ids(l_count + i) := l_old_plan_id(m); --Bug 3629191
5676                                l_off_supply_instance_id(l_count + i) := l_supply_instance_id(i); --Bug 3629191
5677                              END LOOP;
5678                            END IF;
5679                            IF PG_DEBUG in ('Y', 'C') THEN
5680                               msc_sch_wb.atp_debug('Call_delete_row: l_atp_peg_res_reqs.count ' || l_atp_peg_res_reqs.count);
5681                            END IF;
5682                            IF l_atp_peg_res_reqs IS NOT NULL AND l_atp_peg_res_reqs.count > 0 THEN
5683                              l_count := l_atp_peg_res_reqs.count;
5684                              l_del_atp_peg_res_reqs.extend(l_count);
5685                              l_atp_peg_res_reqs_plan_ids.extend(l_count);
5686                              l_off_res_instance_id.extend(l_count);
5687                              l_count := l_del_atp_peg_res_reqs.count - l_atp_peg_res_reqs.count;
5688                              FOR i in 1..l_atp_peg_res_reqs.count LOOP
5689                                l_del_atp_peg_res_reqs(l_count + i) := l_atp_peg_res_reqs(i);
5690                                l_atp_peg_res_reqs_plan_ids(l_count + i) := l_old_plan_id(m); --Bug 3629191
5691                                l_off_res_instance_id(l_count + i) := l_res_instance_id(i); --Bug 3629191
5692                              END LOOP;
5693                            END IF;
5694                            -- END CTO ODR and Simplified Pegging
5695                     END IF; --3720018
5696                  END IF; --- IF (MSC_ATP_PVT.G_INV_CTP = 5) or  (MSC_ATP_PVT.G_INV_CTP = 4 and l_delete_demand_flag = 1)
5697               END IF;
5698               IF PG_DEBUG in ('Y', 'C') THEN
5699                  msc_sch_wb.atp_debug('Call_delete_row: ' || 'Count of records := ' || l_del_demand_ids.count);
5700               END IF;
5701               --l_atp_table.Error_Code(m) := 0;
5702               x_delete_atp_rec.error_code(m) := 0;
5703 
5704               /* time_phased_atp - Reset the variable */
5705               l_time_phased_atp := 'N';
5706 
5707            END LOOP;
5708 
5709            x_delete_atp_rec.time_phased_set           :=       l_time_phased_set;
5710            x_delete_atp_rec.attribute_07              :=       l_attribute_07 ;
5711            x_delete_atp_rec.old_plan_id               :=       l_old_plan_id ;
5712            x_delete_atp_rec.del_demand_ids            :=       l_del_demand_ids;
5713            x_delete_atp_rec.del_inv_item_ids          :=       l_del_inv_item_ids;
5714            x_delete_atp_rec.del_plan_ids              :=       l_del_plan_ids;
5715            x_delete_atp_rec.del_identifiers           :=       l_del_identifiers;
5716            x_delete_atp_rec.del_demand_source_type    :=       l_del_demand_source_type;
5717            x_delete_atp_rec.del_copy_demand_ids       :=       l_del_copy_demand_ids;
5718            x_delete_atp_rec.del_copy_demand_plan_ids  :=       l_del_copy_demand_plan_ids;
5719            x_delete_atp_rec.del_atp_peg_items         :=       l_del_atp_peg_items;
5720            x_delete_atp_rec.del_atp_peg_demands       :=       l_del_atp_peg_demands;
5721            x_delete_atp_rec.del_atp_peg_supplies      :=       l_del_atp_peg_supplies;
5722            x_delete_atp_rec.del_atp_peg_res_reqs      :=       l_del_atp_peg_res_reqs;
5723            x_delete_atp_rec.atp_peg_demands_plan_ids  :=       l_atp_peg_demands_plan_ids;
5724            x_delete_atp_rec.atp_peg_supplies_plan_ids :=       l_atp_peg_supplies_plan_ids;
5725            x_delete_atp_rec.atp_peg_res_reqs_plan_ids :=       l_atp_peg_res_reqs_plan_ids ;
5726            x_delete_atp_rec.off_demand_instance_id    :=       l_off_demand_instance_id ;
5727            x_delete_atp_rec.off_supply_instance_id    :=       l_off_supply_instance_id ;
5728            x_delete_atp_rec.off_res_instance_id       :=       l_off_res_instance_id  ;
5729            x_delete_atp_rec.del_ods_demand_ids        :=       l_del_ods_demand_ids;         --3720018, added for support of rescheduling in ODS
5730            x_delete_atp_rec.del_ods_inv_item_ids      :=       l_del_ods_inv_item_ids;       --3720018, added for support of rescheduling in ODS
5731            x_delete_atp_rec.del_ods_demand_src_type   :=       l_del_ods_demand_src_type;    --3720018, added for support of rescheduling in ODS
5732            x_delete_atp_rec.del_ods_cto_demand_ids    :=       l_del_ods_cto_demand_ids;     --3720018, added for support of rescheduling in ODS
5733            x_delete_atp_rec.del_ods_cto_inv_item_ids  :=       l_del_ods_cto_inv_item_ids;   --3720018, added for support of rescheduling in ODS
5734            x_delete_atp_rec.del_ods_cto_dem_src_type  :=       l_del_ods_cto_dem_src_type;   --3720018, added for support of rescheduling in ODS
5735            x_delete_atp_rec.del_ods_atp_refresh_no    :=       l_del_ods_atp_refresh_no;
5736            x_delete_atp_rec.del_ods_cto_atp_refresh_no    :=       l_del_ods_cto_atp_refresh_no;
5737        END IF;
5738 
5739   	   IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
5740 		MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; -- ALLOC ATP CHANGES, 12973673
5741 	   END IF;
5742 
5743        IF PG_DEBUG in ('Y', 'C') THEN
5744              msc_sch_wb.atp_debug('*** END Call_delete_row ***');
5745        END IF;
5746 
5747 END call_delete_row;
5748 --5357370 starts
5749 PROCEDURE UNDO_DELETE_SUMMARY_ROW (p_identifier                      IN NUMBER,
5750                               p_instance_id                     IN NUMBER,
5751                               p_demand_source_type              IN NUMBER)
5752 
5753 IS
5754 l_instance_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
5755 l_organization_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
5756 l_inventory_item_id MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
5757 l_demand_class MRP_ATP_PUB.char30_arr := MRP_ATP_PUB.char30_arr();
5758 l_sd_date MRP_ATP_PUB.date_arr := MRP_ATP_PUB.date_arr();
5759 l_sd_qty  MRP_ATP_PUB.number_arr := MRP_ATP_PUB.number_arr();
5760 i  number;
5761 -- 5357370 changes, need user id/sysdate for insert/update
5762 l_user_id number;
5763 l_sysdate date;
5764 
5765 BEGIN
5766         IF PG_DEBUG in ('Y', 'C') THEN
5767            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'Inside delete summary row');
5768            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'p_identifier := ' || p_identifier);
5769            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'p_instance_id := ' || p_instance_id);
5770         END IF;
5771         BEGIN
5772 	   SELECT D.sr_instance_id,
5773       	   D.organization_id,
5774            D.inventory_item_id,
5775            Decode(NVL(R.DEMAND_CLASS_ATP_FLAG,0),1 , NVL(D.DEMAND_CLASS, '@@@'), '@@@'),
5776            /*In case of reserved quantity check move to next working day*/
5777            DECODE(D.RESERVATION_TYPE,2,C2.next_date, trunc(D.REQUIREMENT_DATE)) SD_DATE, --5148349
5778            --D.REQUIREMENT_DATE SD_DATE, --5148349
5779            (D.PRIMARY_UOM_QUANTITY-GREATEST(NVL(D.RESERVATION_QUANTITY,0),
5780               D.COMPLETED_QUANTITY)) sd_qty
5781            BULK COLLECT INTO l_instance_id, l_organization_id, l_inventory_item_id, l_demand_class, l_sd_date, l_sd_qty
5782            FROM
5783            MSC_SALES_ORDERS D,
5784            MSC_ATP_RULES R,
5785            MSC_SYSTEM_ITEMS I,
5786            MSC_TRADING_PARTNERS P,
5787            msc_calendar_dates C,
5788            MSC_CALENDAR_DATES C2
5789            WHERE       D.DEMAND_SOURCE_LINE = TO_CHAR(P_IDENTIFIER)
5790            AND     decode(demand_source_type,100,demand_source_type,-1)
5791                             =decode(p_demand_source_type,
5792                                                 100,
5793                                                 p_demand_source_type,
5794                                                 -1) --CMRO
5795            AND         D.SR_INSTANCE_ID = p_instance_id
5796            AND         I.ORGANIZATION_ID = D.ORGANIZATION_ID
5797     	   AND         I.SR_INSTANCE_ID = D.SR_INSTANCE_ID
5798            AND         D.INVENTORY_ITEM_ID = I.INVENTORY_ITEM_ID
5799     	   AND         I.PLAN_ID = -1
5800            AND         P.SR_TP_ID = I.ORGANIZATION_ID
5801            AND         P.SR_INSTANCE_ID = I.SR_INSTANCE_ID
5802            AND         P.PARTNER_TYPE = 3
5803     	   AND         R.RULE_ID  = NVL(I.ATP_RULE_ID, P.DEFAULT_ATP_RULE_ID)
5804     	   AND         R.SR_INSTANCE_ID = I.SR_INSTANCE_ID
5805     	   AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_SALES_ORDERS,2,2,-1)
5806     	   AND         D.DEMAND_SOURCE_TYPE <> DECODE(R.INCLUDE_INTERNAL_ORDERS,2,8,-1)
5807     	   AND         D.PRIMARY_UOM_QUANTITY > GREATEST(NVL(D.RESERVATION_QUANTITY,0),
5808             		                 D.COMPLETED_QUANTITY)
5809     	   AND         (D.SUBINVENTORY IS NULL OR D.SUBINVENTORY IN
5810                 		      (SELECT S.SUB_INVENTORY_CODE
5811                     		      FROM   MSC_SUB_INVENTORIES S
5812                     		      WHERE  S.ORGANIZATION_ID=D.ORGANIZATION_ID
5813                     		      AND    S.PLAN_ID = I.PLAN_ID
5814                     		      AND    S.SR_INSTANCE_ID = D.SR_INSTANCE_ID
5815                     		      AND    S.INVENTORY_ATP_CODE =DECODE(R.DEFAULT_ATP_SOURCES,
5816                                		          1, 1, NULL, 1, S.INVENTORY_ATP_CODE)
5817                     		      AND    S.NETTING_TYPE =DECODE(R.DEFAULT_ATP_SOURCES,
5818                                		          2, 1, S.NETTING_TYPE)))
5819     				      AND         (D.RESERVATION_TYPE = 2
5820                  		      OR D.PARENT_DEMAND_ID IS NULL
5821                  		      OR (D.RESERVATION_TYPE = 3 AND
5822                      			      ((R.INCLUDE_DISCRETE_WIP_RECEIPTS = 1) or
5823                       			      (R.INCLUDE_NONSTD_WIP_RECEIPTS = 1))))
5824            AND C.PRIOR_SEQ_NUM >=
5825                 		      DECODE(R.PAST_DUE_DEMAND_CUTOFF_FENCE,
5826                         	      NULL, C.PRIOR_SEQ_NUM,
5827           			      C2.next_seq_num - NVL(R.PAST_DUE_DEMAND_CUTOFF_FENCE,0))
5828     	  AND     C.CALENDAR_CODE = P.CALENDAR_CODE
5829     	  AND     C.SR_INSTANCE_ID = p_instance_id
5830     	  AND     C.EXCEPTION_SET_ID = -1
5831     	  AND     C.CALENDAR_DATE = TRUNC(D.REQUIREMENT_DATE)
5832     	  AND     C2.CALENDAR_CODE = P.calendar_code
5833 	  AND     C2.EXCEPTION_SET_ID = P.calendar_exception_set_id
5834 	  AND     C2.SR_INSTANCE_ID = P.SR_INSTANCE_ID
5835 	  AND     C2.CALENDAR_DATE = TRUNC(sysdate);
5836         EXCEPTION
5837              WHEN NO_DATA_FOUND THEN
5838                 IF PG_DEBUG in ('Y', 'C') THEN
5839                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'No row found that need to be deleted from summary table');
5840                 END IF;
5841 	END;
5842         -- 5357370: need user id for insert
5843         l_user_id  := FND_GLOBAL.USER_ID;
5844         l_sysdate := sysdate;
5845 
5846         IF PG_DEBUG in ('Y', 'C') THEN
5847            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_inventory_item_id.count := ' || l_inventory_item_id.count);
5848            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_user_id := '|| l_user_id);
5849            msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_sysdate := '|| l_sysdate);
5850 
5851         END IF;
5852         FOR i in 1..l_inventory_item_id.count LOOP
5853                 IF PG_DEBUG in ('Y', 'C') THEN
5854                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'Row found, delete it from summary');
5855                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_ivnevtory_item_id := ' || l_inventory_item_id(i));
5856                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_organization_id := ' || l_organization_id(i) );
5857                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_demand_class := ' || l_demand_class(i));
5858                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_sd_date := ' || l_sd_date(i));
5859                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_sd_qty := ' || l_sd_qty(i));
5860                    msc_sch_wb.atp_debug('UNDO_DELETE_SUMMARY_ROW: ' || 'l_instance_id := ' || l_instance_id(i));
5861                 END IF;
5862 
5863                 update /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
5864                 set sd_qty = (sd_qty + l_sd_qty(i))
5865                 where inventory_item_id = l_inventory_item_id(i)
5866                 and sr_instance_id = l_instance_id(i)
5867                 and organization_id = l_organization_id(i)
5868                 and sd_date = trunc(l_sd_date(i))
5869                 and demand_class = l_demand_class(i) ;
5870 
5871                 -- 5357370: this is to handle that we have reservation on the past due date, and
5872                 -- we won't be able to find record on sysdate to update.
5873                 IF (SQL%NOTFOUND) THEN
5874                   msc_sch_wb.atp_debug('DELETE_SUMMARY_ROW: update failed, now try insert');
5875                   --- Insert the new record
5876                   BEGIN
5877                     INSERT INTO MSC_ATP_SUMMARY_SO
5878                             (plan_id,
5879                              sr_instance_id,
5880                              organization_id,
5881                              inventory_item_id,
5882                              demand_class,
5883                              sd_date,
5884                              sd_qty,
5885                              last_update_date,
5886                              last_updated_by,
5887                              creation_date,
5888                              created_by)
5889                     VALUES ( -1,
5890                              l_instance_id(i),
5891                              l_organization_id(i),
5892                              l_inventory_item_id(i),
5893                              l_demand_class(i),
5894                              trunc(l_sd_date(i)),
5895                              l_sd_qty(i),
5896                              l_sysdate,
5897                              l_user_id ,
5898                              l_sysdate,
5899                              l_user_id
5900                            );
5901                   EXCEPTION
5902                   -- If a record has already been inserted by another process
5903                   -- If insert fails then update.
5904                     WHEN DUP_VAL_ON_INDEX THEN
5905                       -- Update the record.
5906                       update /*+ INDEX(msc_atp_summary_so MSC_ATP_SUMMARY_SO_U1) */ msc_atp_summary_so
5907                       set sd_qty = (sd_qty + l_sd_qty(i)),
5908                           last_update_date = l_sysdate,
5909                           last_updated_by = l_user_id
5910                       where inventory_item_id = l_inventory_item_id(i)
5911                       and sr_instance_id = l_instance_id(i)
5912                       and organization_id = l_organization_id(i)
5913                       and sd_date = trunc(l_sd_date(i))
5914                       and demand_class = l_demand_class(i) ;
5915 
5916                   END;
5917                 END IF;
5918                 -- 5357370: end of changes to handle the update failure.
5919 
5920                 commit;
5921 
5922         END LOOP;
5923 
5924 END UNDO_DELETE_SUMMARY_ROW;
5925 --5357370 ends
5926 
5927 --optional_fw
5928 
5929 /*--Hide_SD_Rec-------------------------------------------------
5930 |  o  This procedure is called from Schedule to hide the
5931 |       demands/supplies for particular call of ATP_CHECK
5932 +---------------------------------------------------------------*/
5933 PROCEDURE Hide_SD_Rec(
5934         p_identifier          IN      NUMBER,
5935         x_return_status       OUT     NoCopy VARCHAR2
5936 )
5937 IS
5938 
5939     CURSOR pegging IS
5940         select  pegging_id, identifier3, identifier2, identifier1,
5941                 supply_demand_type, inventory_item_id, char1, organization_id, supply_demand_date,
5942                 supply_demand_quantity, department_id, resource_id, order_line_id, supplier_id, supplier_site_id,
5943                 supplier_atp_date, dest_inv_item_id, summary_flag
5944                 , aggregate_time_fence_date
5945         from    mrp_atp_details_temp
5946         where   record_type in (3,4)
5947         and     session_id = MSC_ATP_PVT.G_SESSION_ID
5948         start with pegging_id = p_identifier
5949         and     session_id = MSC_ATP_PVT.G_SESSION_ID
5950         and     record_type = 3
5951         connect by parent_pegging_id = prior pegging_id
5952         AND     session_id = prior session_id
5953         AND     record_type in (3,4);
5954 
5955     c1 pegging%ROWTYPE;
5956 		l_alloc_atp varchar2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; -- ALLOC ATP CHANGES, 12973673
5957 BEGIN
5958     IF PG_DEBUG in ('Y', 'C') THEN
5959         msc_sch_wb.atp_debug('***** Begin Hide_SD_Rec Procedure *****');
5960         msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'Reset all the global variables ');
5961         msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'p_identifier= ' || p_identifier);
5962     END IF;
5963 
5964     -- for ods, just need to remove the record from msc_sales_orders
5965     -- initialize API return status to success
5966     x_return_status                 := FND_API.G_RET_STS_SUCCESS;
5967     MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'),2);
5968     MSC_ATP_PVT.G_ALLOCATED_ATP     := NVL(FND_PROFILE.value('MSC_ALLOCATED_ATP'),'N');
5969     MSC_ATP_PVT.G_INV_CTP           := FND_PROFILE.value('INV_CTP') ;
5970 
5971     OPEN pegging;
5972     LOOP
5973 
5974         FETCH pegging INTO c1;
5975         EXIT WHEN pegging%NOTFOUND;
5976 
5977         IF PG_DEBUG in ('Y', 'C') THEN
5978             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'pegging_id = '                 || c1.pegging_id);
5979             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'identifier3 = '                || c1.identifier3);
5980             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'identifier1 = '                || c1.identifier1);
5981             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'plan_id (identifier2) = '      || c1.identifier2);
5982             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'supply_demand_type = '         || c1.supply_demand_type);
5983             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'inventory_item_id := '         || c1.inventory_item_id);
5984             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'char1 := '                     || c1.char1);
5985             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'resource_id := '               || c1.resource_id);
5986             msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'aggregate_time_fence_date := ' || c1.aggregate_time_fence_date);
5987         END IF;
5988 
5989         IF c1.supply_demand_type = 2 THEN --This is supply line
5990             IF NVL(c1.inventory_item_id, -1) > 0 THEN
5991                 -- Hide the planned order that we may have enterred.
5992                 UPDATE MSC_SUPPLIES
5993                 SET   inventory_item_id = -1*inventory_item_id
5994                 WHERE transaction_id = c1.identifier3
5995                 AND   plan_id = c1.identifier2
5996                 AND   inventory_item_id > 0;
5997 
5998                 IF PG_DEBUG in ('Y', 'C') THEN
5999                    msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of supply updated from msc_supplies = '|| SQL%ROWCOUNT);
6000                 END IF;
6001 
6002                 -- time_phased_atp or demand priority AATP
6003                 IF (c1.aggregate_time_fence_date is not null)
6004                    OR
6005                    ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6006                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6007                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6008                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6009 
6010                     IF PG_DEBUG in ('Y', 'C') THEN
6011                         msc_sch_wb.atp_debug('Hide_SD_Rec: before update of msc_alloc_supplies');
6012                     END IF;
6013 
6014                     UPDATE MSC_ALLOC_SUPPLIES
6015                     SET   inventory_item_id = -1*inventory_item_id
6016                     WHERE parent_transaction_id = c1.identifier3
6017                     AND   plan_id = c1.identifier2
6018                     AND   inventory_item_id > 0;
6019 
6020                     IF PG_DEBUG in ('Y', 'C') THEN
6021                         msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of supply updated from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6022                     END IF;
6023                 END IF;
6024                 IF NVL(c1.char1, '@@@') <> '@@@' THEN
6025 
6026                     UPDATE MSC_DEMANDS
6027                     SET   inventory_item_id = -1*inventory_item_id
6028                     WHERE demand_id = c1.identifier3
6029                     AND   plan_id = c1.identifier2
6030                     AND   inventory_item_id > 0;
6031 
6032                     IF PG_DEBUG in ('Y', 'C') THEN
6033                         msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of stealing demand updated from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6034                     END IF;
6035                 END IF;
6036             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6037         ELSE    -- IF c1.supply_demand_type = 2 THEN
6038             -- update the demand records we may have entrered.
6039             IF NVL(c1.inventory_item_id, -1) > 0 and c1.identifier2 <> -1 THEN
6040 
6041                 UPDATE MSC_DEMANDS
6042                 SET   inventory_item_id = -1*inventory_item_id
6043                 WHERE   demand_id = c1.identifier3
6044                 AND     plan_id = c1.identifier2
6045                 AND	    old_demand_quantity IS NULL
6046                 AND     inventory_item_id > 0;
6047 
6048                 IF PG_DEBUG in ('Y', 'C') THEN
6049                     msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of demand updated from msc_demand = '|| SQL%ROWCOUNT);
6050                 END IF;
6051 
6052                 IF (c1.aggregate_time_fence_date is not null)
6053                    OR
6054                    ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6055                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6056                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6057                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6058 
6059                     IF PG_DEBUG in ('Y', 'C') THEN
6060                         msc_sch_wb.atp_debug('Hide_SD_Rec: before update of' ||
6061                             ' msc_alloc_demands');
6062                     END IF;
6063 
6064                     UPDATE MSC_ALLOC_DEMANDS
6065                     SET   inventory_item_id = -1*inventory_item_id
6066                     WHERE parent_demand_id = c1.identifier3
6067                     AND	  old_allocated_quantity IS NULL
6068                     AND   plan_id = c1.identifier2
6069                     AND   inventory_item_id > 0;
6070 
6071                     IF PG_DEBUG in ('Y', 'C') THEN
6072                         msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of demand updated from msc_alloc_demands = '|| SQL%ROWCOUNT);
6073                     END IF;
6074                 END IF;
6075             ELSE    -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6076 
6077                 UPDATE MSC_RESOURCE_REQUIREMENTS
6078                 SET   resource_id = -1*resource_id
6079                 WHERE   transaction_id = c1.identifier3
6080                 AND     plan_id = c1.identifier2
6081                 AND   sr_instance_id = c1.identifier1
6082                 AND   resource_id > 0;
6083 
6084                 IF PG_DEBUG in ('Y', 'C') THEN
6085                        msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'No. of demand deleted from msc_resource_requirements = '|| SQL%ROWCOUNT);
6086                 END IF;
6087 
6088             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6089         END IF; -- IF c1.supply_demand_type = 2 THEN
6090 
6091     END LOOP;
6092     CLOSE pegging;
6093 
6094 	IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
6095 		MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; -- ALLOC ATP CHANGES, 12973673
6096 	END IF;
6097 
6098     IF PG_DEBUG in ('Y', 'C') THEN
6099         msc_sch_wb.atp_debug('***** End Hide_SD_Rec Procedure *****');
6100     END IF;
6101 EXCEPTION
6102     WHEN OTHERS THEN
6103       IF PG_DEBUG in ('Y', 'C') THEN
6104               msc_sch_wb.atp_debug('Hide_SD_Rec: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6105       END IF;
6106       x_return_status := FND_API.G_RET_STS_ERROR;
6107 END Hide_SD_Rec;
6108 
6109 /*--Delete_SD_Rec-----------------------------------------------
6110 |  o  This procedure is called from Schedule to delete the
6111 |       demands/supplies for particular call of ATP_CHECK
6112 |  0  New Procedure needed as inventory item id is -ve.
6113 +---------------------------------------------------------------*/
6114 PROCEDURE Delete_SD_Rec(
6115         p_pegging_tab           IN      MRP_ATP_PUB.Number_Arr,
6116         x_return_status       OUT     NoCopy VARCHAR2
6117 )
6118 IS
6119 
6120     CURSOR pegging(p_pegging_id	IN	NUMBER) IS
6121         select  pegging_id, identifier3, identifier2, identifier1,
6122                 supply_demand_type, inventory_item_id, char1, organization_id, supply_demand_date,
6123                 supply_demand_quantity, department_id, resource_id, order_line_id, supplier_id, supplier_site_id,
6124                 supplier_atp_date, dest_inv_item_id, summary_flag
6125                 , aggregate_time_fence_date,from_organization_id
6126         from    mrp_atp_details_temp
6127         where	record_type in (3,4)
6128         and     session_id = MSC_ATP_PVT.G_SESSION_ID
6129         start with pegging_id = p_pegging_id
6130         and     session_id = MSC_ATP_PVT.G_SESSION_ID
6131         and     record_type = 3
6132         connect by parent_pegging_id = prior pegging_id
6133         AND     session_id = prior session_id
6134         AND     record_type in (3,4);
6135 
6136     c1 pegging%ROWTYPE;
6137 		l_alloc_atp VARCHAR2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
6138 BEGIN
6139     IF PG_DEBUG in ('Y', 'C') THEN
6140         msc_sch_wb.atp_debug('***** Begin Delete_SD_Rec Procedure *****');
6141     END IF;
6142 
6143     x_return_status                 := FND_API.G_RET_STS_SUCCESS;
6144     MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'),2);
6145     MSC_ATP_PVT.G_ALLOCATED_ATP     := NVL(FND_PROFILE.value('MSC_ALLOCATED_ATP'),'N');
6146     MSC_ATP_PVT.G_INV_CTP           := FND_PROFILE.value('INV_CTP') ;
6147 
6148     FOR i IN p_pegging_tab.FIRST..p_pegging_tab.COUNT LOOP
6149      IF PG_DEBUG in ('Y', 'C') THEN
6150         msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'p_pegging_tab(i) = '              || p_pegging_tab(i));
6151      END IF;
6152      OPEN pegging(p_pegging_tab(i));
6153      LOOP
6154 
6155         FETCH pegging INTO c1;
6156         EXIT WHEN pegging%NOTFOUND;
6157 
6158         IF PG_DEBUG in ('Y', 'C') THEN
6159             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'pegging_id = '                 || c1.pegging_id);
6160             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'identifier3 = '                || c1.identifier3);
6161             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'identifier1 = '                || c1.identifier1);
6162             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'plan_id (identifier2) = '      || c1.identifier2);
6163             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'supply_demand_type = '         || c1.supply_demand_type);
6164             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'inventory_item_id := '         || c1.inventory_item_id);
6165             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'char1 := '                     || c1.char1);
6166             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'resource_id := '               || c1.resource_id);
6167             msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'aggregate_time_fence_date := ' || c1.aggregate_time_fence_date);
6168         END IF;
6169 
6170         MSC_ATP_DB_UTILS.Delete_Pegging(c1.pegging_id);
6171 
6172         IF c1.supply_demand_type = 2 THEN
6173             IF NVL(c1.inventory_item_id, -1) > 0 THEN
6174 
6175                 DELETE FROM MSC_SUPPLIES
6176                 WHERE transaction_id = c1.identifier3
6177                 AND   plan_id = c1.identifier2;
6178 
6179                 IF PG_DEBUG in ('Y', 'C') THEN
6180                    msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of supply deleted from msc_supplies = '|| SQL%ROWCOUNT);
6181                 END IF;
6182 
6183                 -- time_phased_atp
6184                 IF (c1.aggregate_time_fence_date is not null)
6185                    OR
6186                    ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6187                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6188                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6189                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6190 
6191                     DELETE FROM MSC_ALLOC_SUPPLIES
6192                     WHERE parent_transaction_id = c1.identifier3
6193                     AND   plan_id = c1.identifier2;
6194 
6195                     IF PG_DEBUG in ('Y', 'C') THEN
6196                         msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of supply deleted from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6197                     END IF;
6198                 END IF;
6199                 IF NVL(c1.char1, '@@@') <> '@@@' THEN
6200 
6201                     DELETE FROM MSC_DEMANDS
6202                     WHERE demand_id = c1.identifier3
6203                     AND   plan_id = c1.identifier2;
6204 
6205                     IF PG_DEBUG in ('Y', 'C') THEN
6206                         msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of stealing demands deleted from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6207                     END IF;
6208                 END IF;
6209             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6210 
6211         ELSE    -- IF c1.supply_demand_type = 2 THEN
6212             -- delete the demand records we may have entrered.
6213             IF NVL(c1.inventory_item_id, -1) > 0 and c1.identifier2 <> -1 THEN
6214 
6215                DELETE  FROM MSC_DEMANDS
6216                WHERE   demand_id = c1.identifier3
6217                AND     plan_id = c1.identifier2
6218                AND     old_demand_quantity IS NULL;
6219 
6220                IF PG_DEBUG in ('Y', 'C') THEN
6221                    msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of demand deleted from msc_demand = '|| SQL%ROWCOUNT);
6222                END IF;
6223 
6224                -- time_phased_atp
6225                IF (c1.aggregate_time_fence_date is not null)
6226                   OR
6227                   ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6228                    (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6229                    (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6230                    (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6231 
6232                    DELETE FROM MSC_ALLOC_DEMANDS
6233                    WHERE parent_demand_id = c1.identifier3
6234                    AND   old_allocated_quantity IS NULL
6235                    AND   plan_id = c1.identifier2;
6236 
6237                    IF PG_DEBUG in ('Y', 'C') THEN
6238                        msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of demand deleted from msc_alloc_demands = '|| SQL%ROWCOUNT);
6239                    END IF;
6240                END IF;
6241 
6242             ELSE    -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6243 
6244                 DELETE  FROM MSC_RESOURCE_REQUIREMENTS
6245                 WHERE   transaction_id = c1.identifier3
6246                 AND     plan_id = c1.identifier2
6247                 AND   sr_instance_id = c1.identifier1;
6248 
6249                 IF PG_DEBUG in ('Y', 'C') THEN
6250                        msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of demand deleted from msc_resource_requirements = '|| SQL%ROWCOUNT);
6251                 END IF;
6252 
6253             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6254         END IF; -- IF c1.supply_demand_type = 2 THEN
6255 
6256     END LOOP;
6257     CLOSE pegging;
6258   END LOOP; --FOR i IN p_pegging_tab.FIRST..p_pegging_tab.COUNT LOOP
6259 
6260   IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
6261 		MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
6262   END IF;
6263 
6264     IF PG_DEBUG in ('Y', 'C') THEN
6265         msc_sch_wb.atp_debug('***** End Delete_SD_Rec Procedure *****');
6266     END IF;
6267 EXCEPTION
6268     WHEN OTHERS THEN
6269       IF PG_DEBUG in ('Y', 'C') THEN
6270               msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6271       END IF;
6272       x_return_status := FND_API.G_RET_STS_ERROR;
6273 END Delete_SD_Rec;
6274 
6275 /*--Restore_SD_Rec-----------------------------------------------
6276 |  o  This procedure is called from Schedule to restore the
6277 |       demands/supplies for particular call of ATP_CHECK
6278 +---------------------------------------------------------------*/
6279 PROCEDURE Restore_SD_Rec(
6280         p_pegging_tab           IN      MRP_ATP_PUB.Number_Arr,
6281         x_return_status       OUT     NoCopy VARCHAR2
6282 )
6283 IS
6284 
6285     CURSOR pegging(p_pegging_id	IN	NUMBER) IS
6286         select  pegging_id, identifier3, identifier2, identifier1,
6287                 supply_demand_type, inventory_item_id, char1, organization_id, supply_demand_date,
6288                 supply_demand_quantity, department_id, resource_id, order_line_id, supplier_id, supplier_site_id,
6289                 supplier_atp_date, dest_inv_item_id, summary_flag
6290                 , aggregate_time_fence_date,from_organization_id
6291         from    mrp_atp_details_temp
6292         where	record_type in (3,4)
6293         and     session_id = MSC_ATP_PVT.G_SESSION_ID
6294         start with pegging_id = p_pegging_id
6295         and     session_id = MSC_ATP_PVT.G_SESSION_ID
6296         and     record_type = 3
6297         connect by parent_pegging_id = prior pegging_id
6298         AND     session_id = prior session_id
6299         AND     record_type in (3,4);
6300 
6301     c1 pegging%ROWTYPE;
6302 		l_alloc_atp VARCHAR2(1) := MSC_ATP_PVT.G_ALLOCATED_ATP; --ALLOC ATP CHANGES, 12973673
6303 
6304 BEGIN
6305     IF PG_DEBUG in ('Y', 'C') THEN
6306         msc_sch_wb.atp_debug('***** Begin Restore_SD_Rec Procedure *****');
6307     END IF;
6308 
6309     -- for ods, just need to remove the record from msc_sales_orders
6310     -- initialize API return status to success
6311     x_return_status                 := FND_API.G_RET_STS_SUCCESS;
6312     MSC_ATP_PVT.G_ALLOCATION_METHOD := NVL(FND_PROFILE.VALUE('MSC_ALLOCATION_METHOD'),2);
6313     MSC_ATP_PVT.G_ALLOCATED_ATP     := NVL(FND_PROFILE.value('MSC_ALLOCATED_ATP'),'N');
6314     MSC_ATP_PVT.G_INV_CTP           := FND_PROFILE.value('INV_CTP') ;
6315 
6316     FOR i IN p_pegging_tab.FIRST..p_pegging_tab.COUNT LOOP
6317      IF PG_DEBUG in ('Y', 'C') THEN
6318         msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'p_pegging_tab(i) = '              || p_pegging_tab(i));
6319      END IF;
6320      OPEN pegging(p_pegging_tab(i));
6321      LOOP
6322 
6323 
6324         FETCH pegging INTO c1;
6325         EXIT WHEN pegging%NOTFOUND;
6326 
6327         IF PG_DEBUG in ('Y', 'C') THEN
6328             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'pegging_id = '                 || c1.pegging_id);
6329             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'identifier3 = '                || c1.identifier3);
6330             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'identifier1 = '                || c1.identifier1);
6331             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'plan_id (identifier2) = '      || c1.identifier2);
6332             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'supply_demand_type = '         || c1.supply_demand_type);
6333             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'inventory_item_id := '         || c1.inventory_item_id);
6334             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'char1 := '                     || c1.char1);
6335             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'resource_id := '               || c1.resource_id);
6336             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'aggregate_time_fence_date := ' || c1.aggregate_time_fence_date);
6337         END IF;
6338 
6339         IF c1.supply_demand_type = 2 THEN
6340             IF NVL(c1.inventory_item_id, -1) > 0 THEN
6341                 -- update the planned order that we may have enterred.
6342                 UPDATE MSC_SUPPLIES
6343                 SET   inventory_item_id = -1*inventory_item_id
6344                 WHERE transaction_id = c1.identifier3
6345                 AND   plan_id = c1.identifier2
6346                 AND   inventory_item_id < 0;
6347 
6348                 IF PG_DEBUG in ('Y', 'C') THEN
6349                    msc_sch_wb.atp_debug('Delete_SD_Rec: ' || 'No. of supply updated from msc_supplies = '|| SQL%ROWCOUNT);
6350                 END IF;
6351 
6352                 -- time_phased_atp
6353                 IF (c1.aggregate_time_fence_date is not null)
6354                    OR
6355                    ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6356                     (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6357                     (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6358                     (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6359 
6360                     UPDATE MSC_ALLOC_SUPPLIES
6361                     SET   inventory_item_id = -1*inventory_item_id
6362                     WHERE parent_transaction_id = c1.identifier3
6363                     AND   plan_id = c1.identifier2
6364                     AND   inventory_item_id < 0;
6365 
6366                     IF PG_DEBUG in ('Y', 'C') THEN
6367                         msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'No. of supply updated from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6368                     END IF;
6369                 END IF;
6370                 IF NVL(c1.char1, '@@@') <> '@@@' THEN
6371 
6372                     UPDATE MSC_DEMANDS
6373                     SET   inventory_item_id = -1*inventory_item_id
6374                     WHERE demand_id = c1.identifier3
6375                     AND   plan_id = c1.identifier2
6376                     AND   inventory_item_id < 0;
6377 
6378                     IF PG_DEBUG in ('Y', 'C') THEN
6379                         msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'No. of stealing demands updated from msc_alloc_supplies = '|| SQL%ROWCOUNT);
6380                     END IF;
6381                 END IF;
6382             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6383 
6384         ELSE    -- IF c1.supply_demand_type = 2 THEN
6385             -- update the demand records we may have entrered.
6386             IF NVL(c1.inventory_item_id, -1) > 0 and c1.identifier2 <> -1 THEN
6387 
6388                UPDATE MSC_DEMANDS
6389                SET   inventory_item_id = -1*inventory_item_id
6390                WHERE   demand_id = c1.identifier3
6391                AND     plan_id = c1.identifier2
6392                AND	    old_demand_quantity IS NULL
6393                AND   inventory_item_id < 0;
6394 
6395                IF PG_DEBUG in ('Y', 'C') THEN
6396                    msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'No. of demand updated from msc_demand = '|| SQL%ROWCOUNT);
6397                END IF;
6398 
6399                -- time_phased_atp
6400                IF (c1.aggregate_time_fence_date is not null)
6401                   OR
6402                   ((MSC_ATP_PVT.G_INV_CTP = 4) AND
6403                    (MSC_ATP_PVT.G_ALLOCATED_ATP = 'Y') AND
6404                    (MSC_ATP_PVT.G_HIERARCHY_PROFILE = 1) AND
6405                    (MSC_ATP_PVT.G_ALLOCATION_METHOD = 1)) THEN
6406 
6407                    UPDATE MSC_ALLOC_DEMANDS
6408                    SET   inventory_item_id = -1*inventory_item_id
6409                    WHERE parent_demand_id = c1.identifier3
6410                    AND	  old_allocated_quantity IS NULL
6411                    AND   plan_id = c1.identifier2
6412                    AND   inventory_item_id < 0;
6413 
6414                    IF PG_DEBUG in ('Y', 'C') THEN
6415                        msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'No. of demand updated from msc_alloc_demands = '|| SQL%ROWCOUNT);
6416                    END IF;
6417                END IF;
6418 
6419             ELSE    -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6420 
6421                 UPDATE MSC_RESOURCE_REQUIREMENTS
6422                 SET   resource_id = -1*resource_id
6423                 WHERE   transaction_id = c1.identifier3
6424                 AND     plan_id = c1.identifier2
6425                 AND   sr_instance_id = c1.identifier1
6426                 AND   resource_id < 0;
6427 
6428                 IF PG_DEBUG in ('Y', 'C') THEN
6429                        msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'No. of demand updated from msc_resource_requirements = '|| SQL%ROWCOUNT);
6430                 END IF;
6431             END IF; -- IF NVL(c1.inventory_item_id, -1) > 0 THEN
6432         END IF; -- IF c1.supply_demand_type = 2 THEN
6433     END LOOP;
6434     CLOSE pegging;
6435   END LOOP; --FOR i IN p_pegging_tab.FIRST..p_pegging_tab.COUNT LOOP
6436 
6437   IF MSC_ATP_PVT.G_HYBRID_ALLOC_ATP = 'Y' THEN
6438 	MSC_ATP_PVT.G_ALLOCATED_ATP := l_alloc_atp; --ALLOC ATP CHANGES, 12973673
6439   END IF;
6440 
6441   IF PG_DEBUG in ('Y', 'C') THEN
6442      msc_sch_wb.atp_debug('***** End Restore_SD_Rec Procedure *****');
6443   END IF;
6444 EXCEPTION
6445   WHEN OTHERS THEN
6446     IF PG_DEBUG in ('Y', 'C') THEN
6447             msc_sch_wb.atp_debug('Restore_SD_Rec: ' || 'Error occurred: ' || to_char(sqlcode) || ':' || SQLERRM);
6448     END IF;
6449     x_return_status := FND_API.G_RET_STS_ERROR;
6450 END Restore_SD_Rec;
6451 
6452 END MSC_ATP_DB_UTILS;