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