DBA Data[Home] [Help]

PACKAGE BODY: APPS.OE_SCHEDULE_UTIL

Source


1 PACKAGE BODY OE_SCHEDULE_UTIL AS
2 /* $Header: OEXUSCHB.pls 120.54.12010000.8 2009/02/05 06:07:08 vbkapoor ship $ */
3 
4 
5 /*-----------------------------------------------------------
6 CONSTANTS and Forward declarations
7 ------------------------------------------------------------*/
8 G_PKG_NAME             CONSTANT     VARCHAR2(30):='OE_SCHEDULE_UTIL';
9 G_OVERRIDE_FLAG        VARCHAR2(1)  := 'N';
10 G_ATP_CHECK_SESSION_ID NUMBER;
11 
12 /*-- 3288805 -- */ -- moved to spec.
13 --G_HEADER_ID        NUMBER       := null;
14 --G_DATE_TYPE        VARCHAR2(30) := null;
15 
16 --G_ATP_TBL          OE_ATP.atp_tbl_type; -- 2434807 - moved to package spec.
17 
18 -- INVCONV below procedure not used now because of OPM inventory convergence
19 /*PROCEDURE get_process_query_quantities
20   (   p_org_id       IN  NUMBER
21    ,  p_item_id      IN  NUMBER
22    ,  p_line_id      IN  NUMBER
23 , x_on_hand_qty OUT NOCOPY NUMBER
24 
25 , x_avail_to_reserve OUT NOCOPY NUMBER
26 
27   ) ; */
28 
29 -- Added for ER 6110708
30 PROCEDURE VALIDATE_ITEM_SUBSTITUTION
31 (
32 p_new_inventory_item_id   IN NUMBER,
33 p_old_inventory_item_id   IN NUMBER,
34 p_new_ship_from_org_id    IN NUMBER,
35 p_old_ship_from_org_id    IN NUMBER,
36 p_old_shippable_flag      IN VARCHAR2
37 );
38 
39 /*--------------------------------------------------------
40 PROCEDURE Print_Time
41 
42 --------------------------------------------------------*/
43 
44 PROCEDURE Print_Time(p_msg   IN  VARCHAR2)
45 IS
46   l_time    VARCHAR2(100);
47   --
48   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
49   --
50 BEGIN
51   l_time := to_char (new_time (sysdate, 'PST', 'EST'),
52                                  'DD-MON-YY HH24:MI:SS');
53   IF l_debug_level  > 0 THEN
54       oe_debug_pub.add(  P_MSG || ': '|| L_TIME , 1 ) ;
55   END IF;
56 END Print_Time;
57 
58 --- Start 2434807 ---
59 /*--------------------------------------------------------------------------
60 Procedure Name : Get_Atp_Table_Count
61 Description    : This procedure returns the count of record in g_atp_tbl
62                  along with the contents in the table.
63 --------------------------------------------------------------------------*/
64 PROCEDURE Get_Atp_Table_Count(p_atp_tbl  OUT NOCOPY OE_ATP.Atp_Tbl_Type,
65                               p_atp_tbl_cnt  OUT  NOCOPY NUMBER)
66 IS
67 BEGIN
68    p_atp_tbl_cnt := g_atp_tbl.count;
69    p_atp_tbl := g_atp_tbl;
70 END Get_Atp_Table_Count;
71 --- End 2434807 --
72 
73 
74 /*--------------------------------------------------------------------------
75 Procedure Name : Get_Session_Id
76 Description    : This procedure returns the session_id which will be
77                  passed to MRP's ATP API.
78 --------------------------------------------------------------------------*/
79 FUNCTION Get_Session_Id
80 RETURN number
81 IS
82 --
83 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
84 --
85 BEGIN
86 
87   SELECT mrp_atp_schedule_temp_s.nextval
88   INTO   MRP_SESSION_ID
89   from dual;
90 
91   return MRP_SESSION_ID;
92 EXCEPTION
93   WHEN OTHERS THEN
94     return 0;
95 END Get_Session_Id;
96 
97 /*--------------------------------------------------------------------------
98 Procedure Name : Get_MRP_Session_Id
99 Description    : This procedure returns the MRP_session_id which will be
100                  Used in the pld.
101 --------------------------------------------------------------------------*/
102 FUNCTION Get_MRP_Session_Id
103 RETURN number
104 IS
105 --
106 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
107 --
108 BEGIN
109   return MRP_SESSION_ID;
110 EXCEPTION
111    WHEN OTHERS THEN
112         return 0;
113 END Get_MRP_Session_Id;
114 
115 /*--------------------------------------------------------------------------
116 Function Name : Get_ATP_CHECK_Session_Id
117 Description    : This procedure returns the ATP_CHECK_session_id which will be
118                  Used in the pld.
119 --------------------------------------------------------------------------*/
120 FUNCTION Get_ATP_CHECK_Session_Id
121 RETURN number
122 IS
123 --
124 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
125 --
126 BEGIN
127   return G_ATP_CHECK_SESSION_ID;
128 EXCEPTION
129    WHEN OTHERS THEN
130         return 0;
131 END Get_ATP_CHECK_Session_Id;
132 
133 FUNCTION Validate_ship_method
134 (p_new_ship_method IN VARCHAR2,
135  p_old_ship_method IN VARCHAR2,
136  p_ship_from_org_id IN NUMBER)
137 RETURN BOOLEAN
138 IS
139 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
140 l_count NUMBER := 0;
141 BEGIN
142 
143   IF l_debug_level > 0 THEN
144       oe_debug_pub.add('Entering Validate_ship_method',1);
145       oe_debug_pub.add('p_new_ship_method ' || p_new_ship_method,1);
146       oe_debug_pub.add('p_old_ship_method ' || p_old_ship_method,1);
147   END IF;
148 
149   IF NOT OE_GLOBALS.Equal(p_new_ship_method,
150                           p_old_ship_method)  THEN
151 
152    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110509' THEN
153 
154         SELECT count(*)
155         INTO   l_count
156         FROM   wsh_carrier_services wsh,
157                wsh_org_carrier_services wsh_org
158         WHERE  wsh_org.organization_id    = p_ship_from_org_id
159         AND  wsh.carrier_service_id       = wsh_org.carrier_service_id
160         AND  wsh.ship_method_code         = p_new_ship_method
161         AND  wsh_org.enabled_flag         = 'Y';
162 
163    ELSE
164 
165         SELECT count(*)
166         INTO l_count
167         FROM    wsh_carrier_ship_methods
168         WHERE   ship_method_code = p_new_ship_method
169         AND   organization_id = p_ship_from_org_id;
170 
171    END IF;
172 
173        IF l_debug_level > 0 THEN
174          oe_debug_pub.add('l_count ' || l_count,1);
175        END IF;
176 
177        IF l_count  = 0 THEN
178           RETURN FALSE;
179        END IF;
180 
181  END IF;
182 
183 
184  RETURN TRUE;
185 
186 EXCEPTION
187 
188   WHEN OTHERS THEN
189         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
190         THEN
191             OE_MSG_PUB.Add_Exc_Msg
192             (   G_PKG_NAME
193             ,   'oe_schedule_util.validate_ship_method'
194             );
195         END IF;
196         RETURN FALSE;
197 
198 END Validate_ship_method;
199 -- Pack J
200 /*--------------------------------------------------------------------------
201 PROCEDURE Name : validate_with_LAD
202 Description    : This API will check schedule ship date or schedule arrival date
203                  with latest acceptable date and raise error/warning based on flag
204 -------------------------------------------------------------------------- */
205 PROCEDURE validate_with_LAD
206 ( p_header_id IN NUMBER
207  ,p_latest_acceptable_date IN DATE
208  ,p_schedule_ship_date     IN DATE
209  ,p_schedule_arrival_date  IN DATE
210 )
211 IS
212   l_order_date_type_code  VARCHAR2(20);
213   l_return_status  BOOLEAN := FALSE;
214   l_lad_flag        VARCHAR2(1);
215 --
216 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
217 --
218 
219 BEGIN
220    IF l_debug_level  > 0 THEN
221       oe_debug_pub.add(  'ENTERING VALIDATE_WITH_LAD WITH LAD FLAG ' , 1 ) ;
222    END IF;
223    l_order_date_type_code := NVL(Get_Date_Type(p_header_id),'SHIP');
224 
225    -- To check violation of LAD when parameter set to - Ignore LAD
226    IF l_debug_level  > 0 THEN
227       oe_debug_pub.add(  'CHECKING FOR LAD ' , 1 ) ;
228    END IF;
229    l_lad_flag := Oe_Sys_Parameters.Value('LATEST_ACCEPTABLE_DATE_FLAG');
230    IF l_lad_flag = 'I' THEN
231       IF ((l_order_date_type_code = 'SHIP'
232         AND p_schedule_ship_date
233                             > p_latest_acceptable_date)
234       OR (l_order_date_type_code = 'ARRIVAL'
235         AND p_schedule_arrival_date
236                             > p_latest_acceptable_date)) THEN
237          IF l_debug_level  > 0 THEN
238             oe_debug_pub.add(  'LAD VIOLATED ' , 1 ) ;
239          END IF;
240          Fnd_Message.set_name('ONT','ONT_SCH_LAD_VIOLATE');
241          Oe_Msg_Pub.Add;
242       END IF;
243    /* -- 3349770 this will be only validated when user enters manually.
244    ELSIF l_lad_flag = 'H' THEN
245       IF ((l_order_date_type_code = 'SHIP'
246         AND p_schedule_ship_date
247                            > p_latest_acceptable_date)
248       OR (l_order_date_type_code = 'ARRIVAL'
249         AND p_schedule_arrival_date
250                             > p_latest_acceptable_date)) THEN
251            IF l_debug_level  > 0 THEN
252                oe_debug_pub.add(  'SCHEDULE DATE EXCEEDS LAD ' , 1 ) ;
253            END IF;
254            Fnd_Message.set_name('ONT','ONT_SCH_LAD_SCH_FAILED');
255            Oe_Msg_Pub.Add;
256            RAISE FND_API.G_EXC_ERROR;
257       END IF;
258    */
259    END IF;
260    IF l_debug_level  > 0 THEN
261       oe_debug_pub.add(  'EXITING VALIDATE_WITH_LAD' , 1 ) ;
262    END IF;
263 END validate_with_LAD;
264 -- End Pack J
265 /*--------------------------------------------------------------------------
266 Procedure Name : Insert_Mandatory_Components
267 Description    : This procedure is called from the form side, when the user
268                  clicks on global availability button and the item to check
269                  global availability is an ATO Model. We insert the mandatory
270                  components in MRP_ATP_SCHEDULE_TEMP for global availability.
271 --------------------------------------------------------------------------*/
272 Procedure Insert_Mandatory_Components
273 (p_order_number           IN  NUMBER,
274 p_ato_line_id             IN  NUMBER,
275 p_customer_name           IN  VARCHAR2,
276 p_customer_location       IN  VARCHAR2,
277 p_arrival_set_name        IN  VARCHAR2,
278 p_ship_set_name           IN  VARCHAR2,
279 p_ship_set_id             IN  NUMBER,
280 p_requested_ship_date     IN  DATE,
281 p_requested_arrival_date  IN  DATE,
282 p_session_id              IN  NUMBER,
283 p_instance_id             IN  NUMBER,
284 p_insert_code             IN  NUMBER,
285 x_return_status OUT NOCOPY VARCHAR2
286 
287 )
288 IS
289 l_model_line_rec          OE_ORDER_PUB.line_rec_type;
290 l_model_rec               MRP_ATP_PUB.ATP_Rec_Typ;
291 l_smc_rec                 MRP_ATP_PUB.ATP_Rec_Typ;
292 l_ship_set                VARCHAR2(30);
293 lTableName                VARCHAR2(30);
294 lMessageName              VARCHAR2(30);
295 lErrorMessage             VARCHAR2(2000);
296 l_result                  NUMBER := 1;
297 
298 l_scenario_id             NUMBER := -1;
299 l_line_id                 NUMBER;
300 l_header_id               NUMBER;
301 l_ato_line_id             NUMBER;
302 l_inventory_item_id       NUMBER;
303 l_ordered_item            VARCHAR2(2000);
304 l_sold_to_org_id          NUMBER;
305 l_ship_to_org_id          NUMBER;
306 l_ship_from_org_id        NUMBER;
307 l_quantity_ordered        NUMBER;
308 l_uom_code                VARCHAR2(3);
309 l_latest_acceptable_date  DATE;
310 l_line_number             NUMBER;
311 l_shipment_number         NUMBER;
312 l_option_number           NUMBER;
313 l_delivery_lead_time      NUMBER;
314 l_promise_date            DATE;
315 l_project_id              NUMBER;
316 l_task_id                 NUMBER;
317 l_ship_method             VARCHAR2(30) := null;
318 l_demand_class            VARCHAR2(30) := null;
319 l_ship_set_id             NUMBER;
320 l_arrival_set_id          NUMBER;
321 l_ship_method_text        VARCHAR2(80);
322 l_project_number          NUMBER;
323 l_task_number             NUMBER;
324 l_st_atp_lead_time        NUMBER := 0;
325 l_order_number            NUMBER;
326 --
327 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
328 --
329 BEGIN
330 
331 
332    IF NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
333    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 ) THEN
334 
335       SAVEPOINT insert_mand_comp;
336 
337 --      l_model_line_rec := OE_LINE_UTIL.Query_Row(p_ato_line_id);
338 
339     OE_Line_Util.Query_Row(p_line_id  => p_ato_line_id,
340                                  x_line_rec => l_model_line_rec);
341 
342        l_st_atp_lead_time :=
343           Get_Lead_Time
344             (p_ato_line_id      => l_model_line_rec.ato_line_id,
345              p_ship_from_org_id => l_model_line_rec.ship_from_org_id);
346 
347       l_model_rec.Inventory_Item_Id := MRP_ATP_PUB.number_arr
348                             (l_model_line_rec.Inventory_Item_Id);
349 
350       l_model_rec.Source_Organization_Id := MRP_ATP_PUB.number_arr
351                             (l_model_line_rec.ship_from_org_id);
352 
353       l_model_rec.Identifier := MRP_ATP_PUB.number_arr
354                             (l_model_line_rec.line_id);
355 
356       l_model_rec.Calling_Module := MRP_ATP_PUB.number_arr
357                             (660);
358 
359       l_model_rec.Customer_Id := MRP_ATP_PUB.number_arr
360                             (l_model_line_rec.sold_to_org_id);
361 
362       l_model_rec.Customer_Site_Id := MRP_ATP_PUB.number_arr
363                             (l_model_line_rec.ship_to_org_id);
364 
365       l_model_rec.Destination_Time_Zone := MRP_ATP_PUB.char30_arr
366                             (null);
367 
368       l_model_rec.Quantity_Ordered := MRP_ATP_PUB.number_arr
369                             (l_model_line_rec.ordered_quantity);
370 
371       l_model_rec.Quantity_UOM := MRP_ATP_PUB.char3_arr
372                             (l_model_line_rec.order_quantity_uom);
373 
374       l_model_rec.Earliest_Acceptable_Date := MRP_ATP_PUB.date_arr
375                             (l_model_line_rec.Earliest_Acceptable_Date);
376 
377       l_model_rec.Requested_Ship_Date := MRP_ATP_PUB.date_arr
378                             (l_model_line_rec.request_date);
379 
380       l_model_rec.Requested_Arrival_Date := MRP_ATP_PUB.date_arr
381                             (l_model_line_rec.request_date);
382 
383       l_model_rec.Latest_Acceptable_Date := MRP_ATP_PUB.date_arr
384                             (l_model_line_rec.Latest_Acceptable_Date);
385 
386       l_model_rec.Delivery_Lead_Time := MRP_ATP_PUB.number_arr
387                             (l_model_line_rec.Delivery_Lead_Time);
388       l_model_rec.Atp_lead_Time := MRP_ATP_PUB.number_arr
389                             (l_st_atp_lead_time);
390 
391       l_model_rec.Freight_Carrier := MRP_ATP_PUB.char30_arr
392                             (l_model_line_rec.Freight_Carrier_Code);
393 
394       l_model_rec.Ship_Method := MRP_ATP_PUB.char30_arr
395                             (null);
396 
397       l_model_rec.Demand_Class := MRP_ATP_PUB.char30_arr
398                             (l_model_line_rec.Demand_Class_Code);
399 
400       l_model_rec.Ship_Set_Name := MRP_ATP_PUB.char30_arr
401                             (l_model_line_rec.ship_set_id);
402 
403       l_model_rec.Arrival_Set_Name := MRP_ATP_PUB.char30_arr
404                             (l_model_line_rec.arrival_set_id);
405 
406       l_model_rec.Override_Flag := MRP_ATP_PUB.char1_arr
407                             (null);
408 
409       l_model_rec.Ship_Date := MRP_ATP_PUB.date_arr
410                             (null);
411 
412       l_model_rec.Available_Quantity := MRP_ATP_PUB.number_arr
413                             (null);
414 
415       l_model_rec.Requested_Date_Quantity := MRP_ATP_PUB.number_arr
416                             (null);
417 
418       l_model_rec.Group_Ship_Date := MRP_ATP_PUB.date_arr
419                             (null);
420 
421       l_model_rec.Group_Arrival_Date := MRP_ATP_PUB.date_arr
422                             (null);
423 
424       l_model_rec.Vendor_Id := MRP_ATP_PUB.number_arr
425                             (null);
426 
427       l_model_rec.Vendor_Site_Id := MRP_ATP_PUB.number_arr
428                             (null);
429 
430       l_model_rec.Insert_Flag := MRP_ATP_PUB.number_arr
431                             (null);
432 
433       l_model_rec.Error_Code := MRP_ATP_PUB.number_arr
434                             (null);
435 
436       l_model_rec.Message := MRP_ATP_PUB.char2000_arr
437                             (null);
438 
439       l_model_rec.Action  := MRP_ATP_PUB.number_arr
440                             (null);
441 
442       l_order_number := Get_order_number(l_model_line_rec.header_id);
443 
444       l_model_rec.Order_number  := MRP_ATP_PUB.number_arr
445                             (l_order_number);
446 
447       IF l_debug_level  > 0 THEN
448           oe_debug_pub.add(  '1.. CALLING CTO GET_BOM_MANDATORY_COMPS' , 1 ) ;
449       END IF;
450 
451       BEGIN
452       l_result  :=  CTO_CONFIG_ITEM_PK.GET_MANDATORY_COMPONENTS
453                       (p_ship_set           => l_model_rec,
454                        p_organization_id    => null,
455                        p_inventory_item_id  => null,
456                        x_smc_rec            => l_smc_rec,
457                        xErrorMessage        => lErrorMessage,
458                        xMessageName         => lMessageName,
459                        xTableName           => lTableName);
460 
461       IF l_debug_level  > 0 THEN
462           oe_debug_pub.add(  '1. AFTER CALLING CTO API : ' || L_RESULT , 1 ) ;
463       END IF;
464       IF l_debug_level  > 0 THEN
465           oe_debug_pub.add(  'COUNT IS: ' || L_SMC_REC.INVENTORY_ITEM_ID.COUNT , 1 ) ;
466       END IF;
467 
468       EXCEPTION
469          WHEN OTHERS THEN
470               IF l_debug_level  > 0 THEN
471                   oe_debug_pub.add(  'CTO API RETURNED AN UNEXPECTED ERROR' ) ;
472               END IF;
473               l_result := 0;
474       END;
475 
476       IF l_result = 1 AND
477          l_smc_rec.Identifier.count >= 1 THEN
478 
479                               IF l_debug_level  > 0 THEN
480                                   oe_debug_pub.add(  'SMC COUNT IS : ' || L_SMC_REC.IDENTIFIER.COUNT , 1 ) ;
481                               END IF;
482 
483          FOR J IN 1..l_smc_rec.Identifier.count LOOP
484              l_line_id                 := l_smc_rec.Identifier(J);
485              l_header_id               := l_model_line_rec.header_id;
486              l_ato_line_id             := l_model_line_rec.ato_line_id;
487              l_inventory_item_id       := l_smc_rec.Inventory_Item_Id(J);
488              l_ordered_item            := null;
489              l_sold_to_org_id          := l_model_line_rec.sold_to_org_id;
490              l_ship_to_org_id          := l_model_line_rec.ship_to_org_id;
491              l_ship_from_org_id        := l_model_line_rec.ship_from_org_id;
492              l_demand_class            := l_model_line_rec.demand_class_code;
493              l_quantity_ordered        := l_smc_rec.Quantity_Ordered(J);
494              l_uom_code                := l_smc_rec.Quantity_UOM(J);
495              l_latest_acceptable_date  :=
496                                  l_model_line_rec.latest_acceptable_date;
497              l_line_number             := l_model_line_rec.line_number;
498              l_shipment_number         := l_model_line_rec.line_number;
499              l_option_number           := l_model_line_rec.option_number;
500              l_delivery_lead_time      := l_model_line_rec.delivery_lead_time;
501              l_promise_date            := l_model_line_rec.promise_date;
502              l_project_id              := l_model_line_rec.project_id;
503              l_task_id                 := l_model_line_rec.task_id;
504              l_ship_method             := l_model_line_rec.shipping_method_code;
505              l_arrival_set_id          := l_model_line_rec.arrival_set_id;
506 
507              l_ship_method_text        := l_ship_method;
508              l_project_number          := l_project_id;
509              l_task_number             := l_task_id;
510 
511              IF l_inventory_item_id is not null AND
512                 l_ship_from_org_id is not null
513              THEN
514                 BEGIN
515 
516                   SELECT concatenated_segments
517                   INTO  l_ordered_item
518                   FROM  mtl_system_items_vl
519                   WHERE inventory_item_id = l_inventory_item_id
520                   AND organization_id = l_ship_from_org_id;
521 
522                 EXCEPTION
523                   WHEN OTHERS THEN
524                      null;
525                 END;
526 
527              END IF;
528 
529 
530             INSERT INTO MRP_ATP_SCHEDULE_TEMP
531             (INVENTORY_ITEM_ID,
532           SR_INSTANCE_ID,
533              SOURCE_ORGANIZATION_ID,
534              CUSTOMER_ID,
535              CUSTOMER_SITE_ID,
536              DESTINATION_TIME_ZONE,
537              QUANTITY_ORDERED,
538              UOM_CODE,
539              REQUESTED_SHIP_DATE,
540              REQUESTED_ARRIVAL_DATE,
541              LATEST_ACCEPTABLE_DATE,
542              DELIVERY_LEAD_TIME,
543              FREIGHT_CARRIER,
544              INSERT_FLAG,
545              SHIP_METHOD,
546              DEMAND_CLASS,
547              SHIP_SET_NAME,
548              SHIP_SET_ID,
549              ARRIVAL_SET_NAME,
550              ARRIVAL_SET_ID,
551              ATP_LEAD_TIME,
552              OVERRIDE_FLAG,
553              SESSION_ID,
554              ORDER_HEADER_ID,
555              ORDER_LINE_ID,
556              INVENTORY_ITEM_NAME,
557              SOURCE_ORGANIZATION_CODE,
558              ORDER_LINE_NUMBER,
559              SHIPMENT_NUMBER,
560              OPTION_NUMBER,
561              PROMISE_DATE,
562              CUSTOMER_NAME,
563              CUSTOMER_LOCATION,
564              OLD_LINE_SCHEDULE_DATE,
565              OLD_SOURCE_ORGANIZATION_CODE,
566              CALLING_MODULE,
567              ACTION,
568              STATUS_FLAG,
569              SCENARIO_ID,
570              ORDER_NUMBER,
571              OLD_SOURCE_ORGANIZATION_ID,
572              OLD_DEMAND_CLASS,
573              PROJECT_ID,
574              TASK_ID,
575              PROJECT_NUMBER,
576              TASK_NUMBER,
577              SHIP_METHOD_TEXT
578              )
579             VALUES
580             (l_inventory_item_id,
581              p_instance_id,
582              null,
583              l_sold_to_org_id, -- CUSTOMER_ID
584              l_ship_to_org_id, -- CUSTOMER_SITE_ID
585              null,  -- DESTINATION_TIME_ZONE
586              l_quantity_ordered,
587              l_uom_code,
588              p_requested_ship_date,
589              p_requested_arrival_date,
590              l_latest_acceptable_date,
591              l_delivery_lead_time,
592              null, -- FREIGHT_CARRIER,
593              p_insert_code,
594              l_ship_method,
595              l_demand_class,
596              p_ship_set_name,
597              p_ship_set_id,
598              p_arrival_set_name,
599              l_arrival_set_id,
600              l_st_atp_lead_time,
601              null, -- OVERRIDE_FLAG
602              p_session_id,
603              l_header_id,
604              l_line_id,
605              l_ordered_item, -- l_INVENTORY_ITEM_NAME,
606              null, -- l_SOURCE_ORGANIZATION_CODE,
607              l_line_number,
608              l_shipment_number,
609              l_option_number,
610              l_promise_date,
611              p_customer_name,
612              p_customer_location,
613              null, -- l_OLD_LINE_SCHEDULE_DATE,
614              null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
615              null, -- l_CALLING_MODULE,
616              100,
617              4, -- l_STATUS_FLAG,
618              l_scenario_id,
619              p_order_number,
620              l_ship_from_org_id,
621              l_demand_class,
622              l_project_id,
623              l_task_id,
624              l_project_number,
625              l_task_number,
626              l_ship_method_text
627              );
628 
629          END LOOP;
630       END IF;
631 
632    END IF; -- GOP Code control
633    x_return_status := FND_API.G_RET_STS_SUCCESS;
634 
635 EXCEPTION
636     WHEN OTHERS THEN
637         ROLLBACK TO SAVEPOINT insert_mand_comp;
638         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
639 
640 END Insert_Mandatory_Components;
641 
642 /*--------------------------------------------------------------------------
643 Procedure Name : Update_PO
644 Description    : This procedure is called whenever there is a change to
645                  schedule_ship_date on an internal order. PO has a callback
646                  we need to call to notify them of this change.
647 --------------------------------------------------------------------------*/
648 
649 Procedure Update_PO(p_schedule_ship_date       IN DATE,
650                     p_source_document_id       IN VARCHAR2,
651                     p_source_document_line_id  IN VARCHAR2)
652 IS
653 po_result    BOOLEAN;
654 --
655 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
656 --
657 BEGIN
658 
659   IF l_debug_level  > 0 THEN
660       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.UPDATE_PO' , 2 ) ;
661   END IF;
662 
663   -- Call po if internal req and quantity is changed
664 
665   IF p_source_document_line_id IS NOT NULL THEN
666 
667        IF l_debug_level  > 0 THEN
668            oe_debug_pub.add(  'DATE ' || P_SCHEDULE_SHIP_DATE , 2 ) ;
669        END IF;
670 
671        po_result := po_supply.po_req_supply(
672                        p_docid         => p_source_document_id,
673                        p_lineid        => p_source_document_line_id,
674                        p_shipid        => p_source_document_line_id,
675                        p_action        => 'Update_Req_Line_Date',
676                        p_recreate_flag => FALSE,
677                        p_qty           => null,
678                        p_receipt_date  => p_schedule_ship_date);
679   END IF;
680 
681   IF l_debug_level  > 0 THEN
682       oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.UPDATE_PO' , 1 ) ;
683   END IF;
684 
685 EXCEPTION
686     WHEN OTHERS THEN
687          IF l_debug_level  > 0 THEN
688              oe_debug_pub.add(  'EXCEPTION IN UPDATE_PO' , 2 ) ;
689          END IF;
690 END Update_PO;
691 
692 /*---------------------------------------------------------------------
693 Function Name : Within_Rsv_Time_Fence
694 Description   : The function returns:
695                 TRUE:  If the Schedule_Ship_Date is within the
696                        time fence of the system date. The time fence
697                        is defined in the profile option
698                        ONT_RESERVATION_TIME_FENCE.
699                 FALSE: If the Schedule_Ship_Date is note within the
700                        time fence of the system date.
701                 The date part of the dates (and not the time) are compared
702                 to return the value.
703 
704                 If schedule_date - SYSDATE < reservation_time_fence
705                 we will return TRUE, else will return FALSE
706 --------------------------------------------------------------------- */
707 Function Within_Rsv_Time_Fence(p_schedule_ship_date IN DATE,
708                 p_org_id IN NUMBER) --4689197
709 RETURN BOOLEAN
710 IS
711   l_rsv_time_fence_profile VARCHAR2(30);
712   l_rsv_time_fence         NUMBER;
713   l_time_to_ship           NUMBER;
714   l_sysdate                DATE;
715   l_schedule_ship_date     DATE;
716   --
717   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
718   --
719 BEGIN
720   l_rsv_time_fence_profile :=
721          Oe_Sys_Parameters.Value('ONT_RESERVATION_TIME_FENCE', p_org_id);
722 
723   BEGIN
724     l_rsv_time_fence := to_number(l_rsv_time_fence_profile);
725   EXCEPTION
726     WHEN OTHERS THEN
727       IF l_debug_level  > 0 THEN
728           oe_debug_pub.add(  'IGNORING RESERVATION TIME FENCE' , 1 ) ;
729       END IF;
730       l_rsv_time_fence := null;
731   END;
732 
733   l_sysdate            := trunc(SYSDATE);
734   l_schedule_ship_date := trunc(p_schedule_ship_date);
735 
736   l_time_to_ship := to_number(l_schedule_ship_date -l_sysdate);
737 
738   IF l_debug_level  > 0 THEN
739       oe_debug_pub.add(  'L_TIME_TO_SHIP ' || L_TIME_TO_SHIP , 1 ) ;
740   END IF;
741   IF l_debug_level  > 0 THEN
742       oe_debug_pub.add(  'L_RSV_TIME_FENCE ' || L_RSV_TIME_FENCE , 1 ) ;
743   END IF;
744 
745 /* Commented the following code to fix the bug 3109349
746 
747   IF l_time_to_ship < 0 THEN
748     -- We don't know what this means. Schedule ship date is already
749     -- past due. So we will not reserve any inventory for this line.
750      RETURN FALSE;
751   ELSIF l_time_to_ship <= l_rsv_time_fence THEN
752 */
753 
754   IF l_time_to_ship <= l_rsv_time_fence THEN
755     RETURN TRUE;
756   ELSE
757     RETURN FALSE;
758   END IF;
759 
760 END Within_Rsv_Time_Fence;
761 /*---------------------------------------------------------------------
762 Function Name : SMC_OVERRIDDEN
763 Description   : This function retuns true if one of the line in a smc model
764                 is overridden. This will be used in procedure load_mrp_request.
765 
766 -----------------------------------------------------------------------*/
767 Function SMC_OVERRIDDEN(p_top_model_line_id IN NUMBER)
768 RETURN BOOLEAN
769 IS
770  l_overridden VARCHAR2(1) := 'N';
771 BEGIN
772 
773    SELECT 'Y'
774    INTO   l_overridden
775    FROM   oe_order_lines_all
776    WHERE  top_model_line_id = p_top_model_line_id
777    AND    open_flag = 'Y'
778    AND    override_atp_date_code = 'Y'
779    AND    rownum < 2;
780 
781    IF l_overridden = 'Y' THEN
782       RETURN TRUE;
783    ELSE
784      RETURN FALSE;
785    END IF;
786 
787 EXCEPTION
788  WHEN OTHERS THEN
789 
790    RETURN FALSE;
791 END SMC_OVERRIDDEN;
792 /*---------------------------------------------------------------------
793 Function Name : SET_OVERRIDDEN
794 Description   : This function retuns true if one of the line in a set
795                 is overridden. This will be used in log_set_request procedure.
796 
797 -----------------------------------------------------------------------*/
798 Function SET_OVERRIDDEN(p_header_id      IN NUMBER
799                        ,p_ship_set_id    IN NUMBER
800                        ,p_arrival_set_id IN NUMBER)
801 RETURN BOOLEAN
802 IS
803  l_overridden VARCHAR2(1) := 'N';
804 BEGIN
805 
806 
807 
808    SELECT 'Y'
809    INTO   l_overridden
810    FROM   oe_order_lines_all
811    WHERE  header_id = p_header_id
812    AND    (ship_set_id = p_ship_set_id
813    OR     arrival_set_id = p_arrival_set_id)
814    AND    override_atp_date_code = 'Y'
815    AND    rownum < 2;
816 
817    IF l_overridden = 'Y' THEN
818       RETURN TRUE;
819    ELSE
820      RETURN FALSE;
821    END IF;
822 
823 EXCEPTION
824  WHEN OTHERS THEN
825 
826    RETURN FALSE;
827 END SET_OVERRIDDEN;
828 /*---------------------------------------------------------------------
829 Procedure Name : Reserve_line
830 Description    : This API calls Inventory's APIs to reserve.
831 --------------------------------------------------------------------- */
832 
833 Procedure Reserve_line
834 ( p_line_rec              IN  OE_ORDER_PUB.Line_Rec_Type
835  ,p_quantity_to_reserve   IN  NUMBER
836  ,p_quantity2_to_reserve   IN  NUMBER  DEFAULT NULL -- INVCONV
837  ,p_rsv_update            IN  BOOLEAN DEFAULT FALSE
838 ,x_return_status OUT NOCOPY VARCHAR2)
839 
840 IS
841 l_msg_count           NUMBER;
842 l_msg_data            VARCHAR2(2000);
843 l_reservation_rec     inv_reservation_global.mtl_reservation_rec_type;
844 l_quantity_reserved   NUMBER;
845 l_quantity2_reserved   NUMBER;  -- INVCONV
846 l_rsv_id              NUMBER;
847 l_reservable_type     NUMBER;
848 l_dummy_sn            inv_reservation_global.serial_number_tbl_type;
849 l_buffer              VARCHAR2(2000);
850 
851 l_substitute_flag BOOLEAN; -- Added for ER 6110708
852 
853 -- subinventory
854 l_revision_code NUMBER;
855 l_lot_code      NUMBER;
856 l_serial_code   NUMBER;
857 l_do_partial_reservation  VARCHAR2(1) := FND_API.G_FALSE;
858 --
859 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
860 --
861 BEGIN
862    IF l_debug_level  > 0 THEN
863        oe_debug_pub.add(  'ENTERING RESERVE LINE :' || P_QUANTITY_TO_RESERVE , 1 ) ;
864          oe_debug_pub.add(  'ENTERING RESERVE LINE qty2 :' || P_QUANTITY2_TO_RESERVE , 1 ) ;
865    END IF;
866    x_return_status := FND_API.G_RET_STS_SUCCESS;
867 
868    SELECT RESERVABLE_TYPE
869    INTO   l_reservable_type
870    FROM   MTL_SYSTEM_ITEMS
871    WHERE  INVENTORY_ITEM_ID = p_line_rec.inventory_item_id
872    AND    ORGANIZATION_ID = p_line_rec.ship_from_org_id;
873 
874    IF l_reservable_type = 1 THEN
875 
876        IF l_debug_level  > 0 THEN
877            oe_debug_pub.add(  'L_QTY_TO_RESERVE : ' || P_QUANTITY_TO_RESERVE , 3 ) ;
878        END IF;
879        --newsub check if item is under lot/revision/serial control
880        IF  p_line_rec.subinventory is not null
881        AND p_line_rec.subinventory <> FND_API.G_MISS_CHAR THEN
882            BEGIN
883               SELECT revision_qty_control_code, lot_control_code,
884                      serial_number_control_code
885               INTO   l_revision_code, l_lot_code,
886                      l_serial_code
887               FROM   mtl_system_items
888               WHERE  inventory_item_id = p_line_rec.inventory_item_id
889               AND    organization_id   = p_line_rec.ship_from_org_id;
890 
891            EXCEPTION
892                WHEN OTHERS THEN
893                 x_return_status := FND_API.G_RET_STS_ERROR;
894                 fnd_message.set_name('ONT', 'OE_INVALID_ITEM_WHSE');
895                 OE_MSG_PUB.Add;
896            END;
897 
898            IF l_revision_code = 2
899            OR l_lot_code = 2
900            THEN
901               -- 2 == YES
902               FND_MESSAGE.SET_NAME('ONT', 'OE_SUBINV_NOT_ALLOWED');
903               OE_MSG_PUB.Add;
904               IF  p_line_rec.Schedule_action_code =  OESCH_ACT_RESERVE THEN
905                  x_return_status := FND_API.G_RET_STS_ERROR;
906                  RAISE FND_API.G_EXC_ERROR;
907               ELSE
908                  x_return_status     := FND_API.G_RET_STS_SUCCESS;
909                  IF l_debug_level  > 0 THEN
910                      oe_debug_pub.add(  'BEFORE RETURN' , 3 ) ;
911                  END IF;
912                  RETURN;
913               END IF;
914 
915            END IF;
916        END IF;
917        --end newsub
918 
919 
920 
921 
922        Load_INV_Request
923        ( p_line_rec              => p_line_rec
924        , p_quantity_to_reserve   => p_quantity_to_reserve
925        , p_quantity2_to_reserve   => p_quantity2_to_reserve -- INVCONV
926        , x_reservation_rec       => l_reservation_rec);
927 
928        -- Call INV with action = RESERVE
929 
930        IF l_debug_level  > 0 THEN
931            oe_debug_pub.add(  'CALLING INVS CREATE_RESERVATION' , 1 ) ;
932        END IF;
933        -- Pack J
934        -- Check for partial reservation flag
935        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
936          AND Oe_Sys_Parameters.Value('PARTIAL_RESERVATION_FLAG') = 'Y' THEN
937          l_do_partial_reservation := FND_API.G_TRUE;
938        END IF;
939        l_quantity2_reserved :=NULL;
940 
941        /* Added the below code for ER 6110708 */
942        IF OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED = 'Y' THEN
943          l_substitute_flag := TRUE;
944        ELSE
945          l_substitute_flag := FALSE;
946        END IF;
947        /* End of code changes for ER 6110708 */
948 
949         IF l_debug_level  > 0 THEN
950          oe_debug_pub.add(  'before  CREATE RESERVATION ',  1 ) ;
951          oe_debug_pub.add(  ' qty2 = ' || l_reservation_rec.secondary_reservation_quantity , 1 ) ;
952          oe_debug_pub.add(  ' uom2 = ' || l_reservation_rec.secondary_uom_code , 1 ) ;
953 
954 
955          END IF;
956 
957        -- p_partial_rsv_exists will be TRUE if reservation get increased
958        inv_reservation_pub.create_reservation
959        (
960         p_api_version_number        => 1.0
961       , p_init_msg_lst              => FND_API.G_TRUE
962       , x_return_status             => x_return_status
963       , x_msg_count                 => l_msg_count
964       , x_msg_data                  => l_msg_data
965       , p_rsv_rec                   => l_reservation_rec
966       , p_serial_number             => l_dummy_sn
967       , x_serial_number             => l_dummy_sn
968       , p_partial_reservation_flag  => l_do_partial_reservation  --FND_API.G_FALSE --Pack J
969       , p_force_reservation_flag    => FND_API.G_FALSE
970       , p_validation_flag           => FND_API.G_TRUE
971       , x_quantity_reserved         => l_quantity_reserved
972       , x_secondary_quantity_reserved  => l_quantity2_reserved -- INVCONV
973       , x_reservation_id            => l_rsv_id
974       , p_partial_rsv_exists        => p_rsv_update
975       , p_substitute_flag           => l_substitute_flag  -- Added for ER 6110708
976       );
977 
978       IF l_debug_level  > 0 THEN
979          oe_debug_pub.add(  '2. AFTER CALLING CREATE RESERVATION' || X_RETURN_STATUS , 1 ) ;
980       END IF;
981       IF l_debug_level  > 0 THEN
982           oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
983       END IF;
984 
985       -- Bug No:2097933
986       -- If the Reservation was succesfull we set
987       -- the package variable to "Y".
988       IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN
989          OESCH_PERFORMED_RESERVATION := 'Y';
990       END IF;
991 
992       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
993          -- 4091185
994          -- Messages are being handled in exceptions block
995          -- oe_msg_pub.transfer_msg_stack;
996          -- l_msg_count:=OE_MSG_PUB.COUNT_MSG;
997          -- for I in 1..l_msg_count loop
998          --     l_msg_data := OE_MSG_PUB.Get(I,'F');
999          --     IF l_debug_level  > 0 THEN
1000          --         oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
1001          --     END IF;
1002          -- end loop;
1003          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1004       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
1005           -- 4091185
1006           oe_msg_pub.transfer_msg_stack(p_type => 'WARNING');
1007           l_msg_count:=OE_MSG_PUB.COUNT_MSG;
1008           for I in 1..l_msg_count loop
1009               l_msg_data := OE_MSG_PUB.Get(I,'F');
1010               IF l_debug_level  > 0 THEN
1011                   oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
1012               END IF;
1013           end loop;
1014           IF p_line_rec.Schedule_action_code = OESCH_ACT_RESERVE THEN
1015              RAISE FND_API.G_EXC_ERROR;
1016           ELSE
1017              x_return_status := FND_API.G_RET_STS_SUCCESS;
1018              IF l_debug_level  > 0 THEN
1019                  oe_debug_pub.add(  'UNABLE TO RESERVE' , 2 ) ;
1020              END IF;
1021           END IF;
1022 
1023        END IF;
1024 
1025    END IF;
1026 
1027                                           IF l_debug_level  > 0 THEN
1028                                               oe_debug_pub.add(  'AFTER CALLING INVS CREATE_RESERVATION' || X_RETURN_STATUS , 1 ) ;
1029                                           END IF;
1030 
1031 EXCEPTION
1032   WHEN FND_API.G_EXC_ERROR THEN
1033     x_return_status := FND_API.G_RET_STS_ERROR;
1034 
1035   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1036     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1037 
1038      oe_msg_pub.transfer_msg_stack;
1039      l_msg_count:=OE_MSG_PUB.COUNT_MSG;
1040      for I in 1..l_msg_count loop
1041         l_msg_data := OE_MSG_PUB.Get(I,'F');
1042         -- 4091185
1043         -- oe_msg_pub.transfer_msg_stack has already added messages.
1044         -- oe_msg_pub.add_text(l_msg_data);
1045         IF l_debug_level  > 0 THEN
1046             oe_debug_pub.add(  'INV : ' || L_MSG_DATA , 2 ) ;
1047         END IF;
1048      end loop;
1049 
1050   WHEN OTHERS THEN
1051     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1052 
1053     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1054     THEN
1055       OE_MSG_PUB.Add_Exc_Msg
1056       (   G_PKG_NAME
1057       ,   'Reserve_line'
1058        );
1059     END IF;
1060 
1061 END Reserve_Line;
1062 /*---------------------------------------------------------------------
1063 Procedure Name : Unreserve_Line
1064 Description    : This API calls Inventory's APIs to Unreserve. It first
1065                  queries the reservation records, and then calls
1066                  delete_reservations until the p_quantity_to_unreserve
1067                  is satisfied.
1068 --------------------------------------------------------------------- */
1069 
1070 Procedure Unreserve_Line
1071 ( p_line_rec              IN  OE_ORDER_PUB.Line_Rec_Type
1072  ,p_old_ship_from_org_id IN NUMBER DEFAULT NULL -- 6628134
1073  ,p_quantity_to_unreserve IN  NUMBER
1074  ,p_quantity2_to_unreserve IN  NUMBER  DEFAULT NULL -- INVCONV
1075 ,x_return_status OUT NOCOPY VARCHAR2)
1076 
1077 IS
1078   l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
1079   l_rsv_new_rec           inv_reservation_global.mtl_reservation_rec_type;
1080   l_msg_count             NUMBER;
1081   l_msg_data              VARCHAR2(240);
1082   l_rsv_id                NUMBER;
1083   l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
1084   l_count                 NUMBER;
1085   l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
1086   l_qty_to_unreserve      NUMBER;
1087   l_source_code           VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
1088   l_sales_order_id        NUMBER;
1089   l_x_error_code          NUMBER;
1090   l_lock_records          VARCHAR2(1);
1091   l_sort_by_req_date      NUMBER ;
1092   l_buffer                VARCHAR2(2000);
1093 
1094    l_quantity2_to_UNreserve NUMBER; -- INVCONV
1095 
1096 --
1097 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1098 --
1099 BEGIN
1100 
1101   IF l_debug_level  > 0 THEN
1102       oe_debug_pub.add(  'ENTERING UNRESERVE LINE' , 3 ) ;
1103   END IF;
1104   IF l_debug_level  > 0 THEN
1105       oe_debug_pub.add(  'QUANTITY TO UNRESERVE : ' || P_QUANTITY_TO_UNRESERVE , 3 ) ;
1106             oe_debug_pub.add(  'QUANTITY2 TO UNRESERVE : ' || P_QUANTITY2_TO_UNRESERVE , 3 ) ;
1107   END IF;
1108 
1109   /* Bug 6335352
1110      OM will handle Resevrations both before and after Shipping Interface is done.
1111   IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y' THEN
1112      IF l_debug_level  > 0 THEN
1113          oe_debug_pub.add(  'LINE HAS BEEN SHIPPING INTERFACED' , 3 ) ;
1114      END IF;
1115      goto end_of_loop;
1116   END IF;
1117   */
1118   -- Start 2595661
1119   --  All codes except shipping interface check have been moved to Procedure Do_Unreserve
1120 
1121   l_quantity2_to_UNreserve := p_quantity2_to_UNreserve; -- INVCONV
1122   IF p_quantity2_to_UNreserve = 0 -- INVCONV
1123         then
1124           l_quantity2_to_UNreserve := null;
1125   END IF;
1126 
1127   Do_Unreserve
1128         ( p_line_rec               => p_line_rec
1129         , p_quantity_to_unreserve  => p_quantity_to_unreserve
1130         , p_quantity2_to_unreserve  => l_quantity2_to_unreserve -- INVCONV
1131         , p_old_ship_from_org_id    => p_old_ship_from_org_id -- 6628134
1132         , x_return_status          => x_return_status);
1133   -- End 2595661
1134 
1135   <<end_of_loop>>
1136 
1137   IF l_debug_level  > 0 THEN
1138       oe_debug_pub.add(  'EXITING UNRESERVE_LINES' , 3 ) ;
1139   END IF;
1140 
1141 EXCEPTION
1142   WHEN FND_API.G_EXC_ERROR THEN
1143     x_return_status := FND_API.G_RET_STS_ERROR;
1144 
1145   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1146     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1147 
1148   WHEN OTHERS THEN
1149     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1150 
1151     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1152     THEN
1153       OE_MSG_PUB.Add_Exc_Msg
1154       (   G_PKG_NAME
1155       ,   'Unreserve_line'
1156        );
1157     END IF;
1158 
1159 END Unreserve_Line;
1160 
1161 
1162 
1163 /* ---------------------------------------------------------------
1164 FUNCTION  : Valid_Set_Addition
1165 Description: This function is called to make sure the model/kit which is
1166              getting added to set should not have lines overridden for
1167              differenr dates.
1168  ---------------------------------------------------------------*/
1169 FUNCTION Valid_Set_Addition(p_top_model_line_id IN NUMBER ,
1170                             p_set_type          IN VARCHAR2)
1171 RETURN BOOLEAN
1172 IS
1173 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1174 l_count       NUMBER := 0;
1175 BEGIN
1176 
1177    IF l_debug_level  > 0 THEN
1178        oe_debug_pub.add(  'p_top_model_line_id :' || p_top_model_line_id , 2 ) ;
1179        oe_debug_pub.add(  'p_set_type :' || P_SET_TYPE , 2 ) ;
1180    END IF;
1181 
1182 
1183    IF p_set_type = 'SHIP_SET' THEN
1184 
1185      SELECT count(a.line_id)
1186      INTO   l_count
1187      FROM oe_order_lines_all a,
1188           oe_order_lines_all b
1189      WHERE a.top_model_line_id = p_top_model_line_id
1190      AND a.top_model_line_id = b.top_model_line_id
1191      AND a.override_atp_date_code = 'Y'
1192      AND b.override_atp_date_code = 'Y'
1193      AND a.schedule_ship_date <> b.schedule_ship_date;
1194 
1195    ELSIF p_set_type = 'ARRIVAL_SET' THEN
1196 
1197      SELECT count(a.line_id)
1198      INTO   l_count
1199      FROM oe_order_lines_all a,
1200           oe_order_lines_all b
1201      WHERE a.top_model_line_id = p_top_model_line_id
1202      AND a.top_model_line_id = b.top_model_line_id
1203      AND a.override_atp_date_code = 'Y'
1204      AND b.override_atp_date_code = 'Y'
1205      AND a.schedule_arrival_date <> b.schedule_arrival_date;
1206 
1207    END IF;
1208 
1209    IF l_debug_level  > 0 THEN
1210     oe_debug_pub.add('count: ' || l_count,2);
1211    END IF;
1212    IF l_count = 0 THEN
1213 
1214       RETURN TRUE;
1215 
1216    ELSE
1217 
1218       RETURN FALSE;
1219 
1220    END IF;
1221 EXCEPTION
1222 
1223   WHEN OTHERS THEN
1224 
1225    IF l_debug_level  > 0 THEN
1226     oe_debug_pub.add('return false from Valid_Set_Addition');
1227    END IF;
1228     RETURN FALSE;
1229 
1230 END Valid_Set_Addition;
1231 /* ---------------------------------------------------------------
1232 FUNCTION  : Set_Attr_Matched
1233 Description: This function is called to compare set and line record
1234              for set attributes. This will help in avoiding additional call
1235              to MRP if the line is scheduled for a same set attributes.
1236  ---------------------------------------------------------------*/
1237 FUNCTION Set_Attr_Matched(p_set_ship_from_org_id IN NUMBER ,
1238                           p_line_ship_from_org_id IN NUMBER,
1239                           p_set_ship_to_org_id IN NUMBER,
1240                           p_line_ship_to_org_id IN NUMBER,
1241                           p_set_schedule_ship_date IN DATE,
1242                           p_line_schedule_ship_date IN DATE,
1243                           p_set_arrival_date IN DATE,
1244                           p_line_arrival_date IN DATE,
1245                           p_set_shipping_method_code IN VARCHAR2,
1246                           p_line_shipping_method_code IN VARCHAR2,
1247                           p_set_type IN VARCHAR2)
1248 RETURN BOOLEAN
1249 IS
1250 
1251 --
1252 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1253 --
1254 BEGIN
1255 
1256    IF l_debug_level  > 0 THEN
1257        oe_debug_pub.add(  'P_SET_TYPE :' || P_SET_TYPE , 2 ) ;
1258        oe_debug_pub.add(  'P_LINE_SHIP_FROM_ORG_ID :' || P_LINE_SHIP_FROM_ORG_ID , 2 ) ;
1259        oe_debug_pub.add(  'P_SET_SHIP_FROM_ORG_ID :' || P_SET_SHIP_FROM_ORG_ID , 2 ) ;
1260        oe_debug_pub.add(  'P_LINE_SHIP_TO_ORG_ID :' || P_LINE_SHIP_TO_ORG_ID , 2 ) ;
1261        oe_debug_pub.add(  'P_SET_SHIP_TO_ORG_ID :' || P_SET_SHIP_TO_ORG_ID , 2 ) ;
1262        oe_debug_pub.add(  'P_LINE_SCHEDULE_SHIP_DATE :' || P_LINE_SCHEDULE_SHIP_DATE , 2 ) ;
1263        oe_debug_pub.add(  'P_SET_SCHEDULE_SHIP_DATE :' || P_SET_SCHEDULE_SHIP_DATE , 2 ) ;
1264        oe_debug_pub.add(  'P_LINE_ARRIVAL_DATE :' || P_LINE_ARRIVAL_DATE , 2 ) ;
1265        oe_debug_pub.add(  'P_SET_ARRIVAL_DATE :' || P_SET_ARRIVAL_DATE , 2 ) ;
1266    END IF;
1267 
1268    IF (p_set_type = 'SHIP_SET' AND
1269        p_line_ship_from_org_id  = p_set_ship_from_org_id   AND
1270        p_line_ship_to_org_id     = p_set_Ship_To_Org_Id     AND
1271        p_line_schedule_ship_date = p_set_schedule_ship_date) THEN
1272 
1273 /* Modified the above line and added followinf 7 lines to fix the bug 3393033 */
1274       IF NVL(fnd_profile.value('ONT_SHIP_METHOD_FOR_SHIP_SET'),'N') = 'Y' THEN
1275        -- 3878494
1276        IF  OE_GLOBALS.equal(p_line_shipping_method_code,p_set_shipping_method_code) THEN
1277            RETURN TRUE;
1278        END IF;
1279       ELSE -- profile
1280        RETURN TRUE;
1281       END IF;
1282 
1283 /* Modified the following line to fix the bug 3393033 */
1284    ELSIF (p_set_type = 'ARRIVAL_SET' AND
1285        p_line_ship_to_org_id     = p_set_ship_to_org_id     AND
1286        p_line_arrival_date       = p_set_arrival_date) THEN
1287 
1288        RETURN TRUE;
1289 
1290    END IF;
1291 
1292    IF l_debug_level  > 0 THEN
1293        oe_debug_pub.add(  'EXITING CAMPARE ATTR' , 3 ) ;
1294    END IF;
1295    RETURN FALSE;
1296 
1297 EXCEPTION
1298 
1299   WHEN OTHERS THEN
1300 
1301    IF l_debug_level  > 0 THEN
1302     oe_debug_pub.add('return false from debug');
1303    END IF;
1304     RETURN FALSE;
1305 
1306 END Set_Attr_Matched;
1307 
1308 -- 4026758
1309 /* ----------------------------------------------------------------------
1310 Procedure Log_Delete_Set_Request
1311 Set_id will be deleted
1312 1. When the line gets removed from the set.
1313 2. When the line gets deleted, which is part of the set.
1314 3. When line gets cancelled.
1315 -------------------------------------------------------------------------*/
1316 Procedure Log_Delete_Set_Request
1317 (p_header_id IN NUMBER,
1318  p_line_id   IN NUMBER,
1319  p_set_id    IN NUMBER,
1320  x_return_status OUT NOCOPY VARCHAR2)
1321 
1322 IS
1323 l_set_type   VARCHAR2(30);
1324 --
1325 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1326 --
1327 BEGIN
1328 
1329    IF l_debug_level  > 0 THEN
1330      oe_debug_pub.add(  'ENTERING LOG DELETE SET REQUEST' || P_SET_ID , 1 );
1331    END IF;
1332 
1333    SELECT set_type
1334    INTO l_set_type
1335    FROM oe_sets
1336    WHERE  set_id = p_set_id;
1337 
1338    OE_delayed_requests_Pvt.log_request(
1339    p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
1340    p_entity_id              => p_line_id,
1341    p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
1342    p_requesting_entity_id   => p_line_id,
1343    p_request_type           => OE_GLOBALS.G_DELETE_SET,
1344    p_param1                 => p_set_id,
1345    p_param2                 => p_header_id,
1346    p_param3                 => l_set_type,
1347    x_return_status          => x_return_status);
1348    IF l_debug_level  > 0 THEN
1349       oe_debug_pub.add(  'EXITING LOG DELETE SET REQUEST '||x_return_status , 1 ) ;
1350    END IF;
1351 
1352 EXCEPTION
1353    WHEN FND_API.G_EXC_ERROR THEN
1354 
1355         x_return_status := FND_API.G_RET_STS_ERROR;
1356 
1357    WHEN OTHERS THEN
1358 
1359         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1360 
1361         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1362         THEN
1363             OE_MSG_PUB.Add_Exc_Msg
1364             (   G_PKG_NAME,
1365               'Log_Delete_Set_Request');
1366         END IF;
1367 END Log_Delete_Set_Request;
1368 
1369 /* ----------------------------------------------------------------------
1370 Procedure Log_Set_Request
1371 1. Set will populate set id when set name is populated by user.
1372 2. log delayed request when a line is added to set.
1373 3. Log delayed request when scheduling related attribute changed on
1374    on the line which belong to set.
1375 3. Execute all delayed requests at once.
1376 
1377  Need to take care logging delyed request when action is passed on the
1378  line. Action can be passed by scheduling workflow or order import call.
1379 
1380   If the line is part of ato model/line  or SMC  model then log group request.
1381   Other wise log a schedule or reschedule delayed request.
1382 
1383 -------------------------------------------------------------------------*/
1384 Procedure Log_Set_Request
1385 (p_line_rec       IN OE_ORDER_PUB.Line_rec_type,
1386  p_old_line_rec   IN OE_ORDER_PUB.Line_rec_type,
1387  p_sch_action     IN VARCHAR2,
1388  p_caller         IN VARCHAR2,
1389 x_return_status OUT NOCOPY VARCHAR2)
1390 
1391 IS
1392 l_group_request        BOOLEAN;
1393 l_action               VARCHAR2(30);
1394 l_schedule_ship_date    DATE := Null;
1395 l_schedule_arrival_date DATE := Null;
1396 l_param1               NUMBER;
1397 l_ship_to_org_id       NUMBER := Null;
1398 l_ship_from_org_id     NUMBER := Null;
1399 l_entity_type          VARCHAR2(30);
1400 
1401 l_set_schedule_ship_date    DATE;
1402 l_set_schedule_arrival_date DATE;
1403 l_set_ship_to_org_id        NUMBER;
1404 l_set_ship_from_org_id      NUMBER;
1405 l_set_status                VARCHAR2(1);
1406 l_set_type                  VARCHAR2(30);
1407 l_matched                   BOOLEAN := FALSE;
1408 l_param12                   VARCHAR2(1);
1409 l_shipping_method_code      VARCHAR2(30);
1410 /* Added the following 1 line to fix the bug 2740480 */
1411 l_push_logic                VARCHAR2(1) := 'N';
1412 /* Added the following 1 line to fix the bug 3393033 */
1413 l_set_shipping_method_code      VARCHAR2(30);
1414 
1415 -- 2391781
1416 l_cascade_line_id           NUMBER := NULL;
1417 l_line_id                   NUMBER := NULL;
1418 
1419 l_order_date_type_code      VARCHAR2(30);
1420 --
1421 --3314157
1422 l_operation         VARCHAR2(30);
1423 --3344843
1424 l_diff_res_qty              NUMBER;
1425 l_qty_to_reserve            NUMBER;
1426 l_qty_to_unreserve          NUMBER;
1427 l_index                     NUMBER;
1428 --
1429 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1430 --
1431 BEGIN
1432 
1433  -- If set does not exists, the the first time the lines are getting added
1434  -- to set. Do a group schedule. Other wise log a delayed request based on
1435  -- ato or smc.
1436    IF l_debug_level  > 0 THEN
1437        oe_debug_pub.add(  'ENTERING LOG SET REQUEST' || P_LINE_REC.SHIP_SET_ID , 1 ) ;
1438    END IF;
1439  BEGIN
1440 
1441 
1442 /* Removed ship_method_code from the following select , to fix the bug 2916814 */
1443 
1444   Select schedule_ship_date,ship_to_org_id,ship_from_org_id,
1445          schedule_arrival_date,shipping_method_code,
1446          set_status, set_type
1447   Into   l_schedule_ship_date,l_ship_to_org_id,l_ship_from_org_id,
1448          l_schedule_arrival_date,l_shipping_method_code,
1449          l_set_status, l_set_type
1450   From   oe_sets
1451   Where  set_id = nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id);
1452 
1453    IF l_debug_level  > 0 THEN
1454        oe_debug_pub.add(  'SET SCHEDULE DATE'|| L_SCHEDULE_SHIP_DATE , 1 ) ;
1455    END IF;
1456  EXCEPTION
1457    WHEN OTHERS THEN
1458      l_schedule_ship_date := null;
1459  END;
1460 
1461  l_set_schedule_ship_date    := l_schedule_ship_date;
1462  l_set_schedule_arrival_date := l_schedule_arrival_date;
1463  l_set_ship_to_org_id        := l_ship_to_org_id;
1464  l_set_ship_from_org_id      := l_ship_from_org_id;
1465  l_set_shipping_method_code  := l_shipping_method_code;
1466 
1467  IF l_schedule_ship_date is null
1468  AND NVL(fnd_profile.value('ONT_SET_FOR_EACH_LINE'),'N') = 'Y'
1469  AND  p_line_rec.schedule_status_code is null
1470  AND  l_set_status = 'T'  THEN
1471 
1472   l_action :=  OESCH_ACT_SCHEDULE;
1473 
1474     -- 4188166
1475     -- Log request if no request logged for the set id. Also remember ship_to_org_id
1476     -- and ship_from_org_id for  cascading
1477     IF NOT OE_Delayed_Requests_PVT.Check_for_Request
1478     (   p_entity_code  =>OE_GLOBALS.G_ENTITY_LINE
1479     ,   p_entity_id    =>nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id)
1480     ,   p_request_type =>OE_GLOBALS.G_GROUP_SET)  THEN
1481 
1482       OE_delayed_requests_Pvt.log_request(
1483       p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
1484       p_entity_id              => nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id),
1485       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
1486       p_requesting_entity_id   => p_line_rec.line_id,
1487       p_request_type           => OE_GLOBALS.G_GROUP_SET,
1488       p_param1                 => l_set_type,
1489       p_param2                 => p_line_rec.header_id,
1490       p_param3                 => p_line_rec.line_id,
1491       p_param4                 => p_line_rec.top_model_line_id,
1492       x_return_status          => x_return_status);
1493    END IF;
1494 
1495 
1496   GOTO END_PROCESS;
1497 
1498  END IF;
1499 
1500  IF l_schedule_ship_date is null
1501  OR p_line_rec.schedule_action_code is not null
1502  THEN
1503   --  Log a group request.
1504 /*
1505    IF p_line_rec.schedule_status_code is not null THEN --2369951
1506 
1507      oe_debug_pub.add('1 Setting schedule line',2);
1508 
1509      l_schedule_ship_date    := p_line_rec.schedule_ship_date;
1510      l_schedule_arrival_date := p_line_rec.schedule_arrival_date;
1511      l_ship_from_org_id      := p_line_rec.ship_from_org_id;
1512      l_ship_to_org_id        := p_line_rec.ship_to_org_id;
1513 
1514      l_action := OESCH_ACT_RESCHEDULE;
1515 
1516    ELSE
1517      oe_debug_pub.add('1 Setting group schedule',2);
1518      l_group_request := TRUE;
1519      l_action := OESCH_ACT_SCHEDULE;
1520    END IF;
1521 */
1522    IF l_debug_level  > 0 THEN
1523        oe_debug_pub.add(  '1 SETTING GROUP SCHEDULE' , 2 ) ;
1524    END IF;
1525    l_group_request := TRUE;
1526    l_action := OESCH_ACT_RESCHEDULE;
1527 
1528 
1529 
1530  ELSIF OE_GLOBALS.Equal(p_line_rec.ship_set_id,
1531                         p_old_line_rec.ship_set_id)
1532  AND   OE_GLOBALS.Equal(p_line_rec.arrival_set_id,
1533                         p_old_line_rec.arrival_set_id)
1534  AND   p_line_rec.schedule_status_code is not null
1535  THEN
1536 
1537    -- some attribute changed in the line which belongs to set.
1538    -- Log group request.
1539     IF l_debug_level  > 0 THEN
1540         oe_debug_pub.add(  '2 SETTING GROUP SCHEDULE' , 2 ) ;
1541     END IF;
1542     l_group_request := TRUE;
1543     l_action := OESCH_ACT_RESCHEDULE;
1544     l_order_date_type_code :=
1545             NVL(Get_Date_Type(p_line_rec.header_id), 'SHIP'); -- 2920081
1546 
1547      --User has changed some attribute on the set.
1548      --Log a group request and send the changed information to
1549      --deleyed request.
1550 
1551 
1552     IF l_ship_from_org_id is not null THEN
1553        IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
1554                                p_old_line_rec.ship_from_org_id)
1555        THEN
1556           l_ship_from_org_id := p_line_rec.ship_from_org_id;
1557        END IF;
1558     END IF;
1559 
1560     IF l_ship_to_org_id is not null THEN
1561        IF NOT OE_GLOBALS.Equal(p_line_rec.ship_to_org_id,
1562                                p_old_line_rec.ship_to_org_id)
1563        THEN
1564           l_ship_to_org_id := p_line_rec.ship_to_org_id;
1565        END IF;
1566     END IF;
1567 
1568     IF l_schedule_ship_date is not null THEN
1569 
1570        IF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1571                                p_old_line_rec.request_date)
1572        AND NOT SET_OVERRIDDEN(p_header_id => p_line_rec.header_id
1573                              ,p_ship_set_id => p_line_rec.ship_set_id
1574                              ,p_arrival_set_id => p_line_rec.arrival_set_id)
1575        THEN
1576           IF l_order_date_type_code ='SHIP'
1577           THEN -- 2920081
1578              --Bug 6057897
1579              --Request date change should be considered for doing re-scheudling of the set based on the parameter
1580              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1581              THEN
1582                 l_schedule_ship_date := p_line_rec.request_date;
1583              END IF;
1584           ELSE
1585              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1586              THEN
1587                 l_schedule_arrival_date := p_line_rec.request_date;
1588              END IF;
1589           END IF;
1590          -- l_schedule_ship_date := p_line_rec.request_date;
1591        END IF;
1592 
1593        IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
1594                                p_old_line_rec.schedule_ship_date)
1595        THEN
1596           l_schedule_ship_date := p_line_rec.schedule_ship_date;
1597        END IF;
1598     END IF;
1599 
1600     IF l_schedule_arrival_date is not null THEN
1601 
1602        IF p_line_rec.arrival_set_id is not null
1603        AND NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1604                                 p_old_line_rec.request_date)
1605        AND NOT SET_OVERRIDDEN(p_header_id      => p_line_rec.header_id
1606                              ,p_ship_set_id    => p_line_rec.ship_set_id
1607                              ,p_arrival_set_id => p_line_rec.arrival_set_id)
1608        THEN
1609           IF l_order_date_type_code ='SHIP'
1610           THEN -- 2920081
1611              --Bug 6057897
1612              --Request date change should be considered for doing re-scheudling of the set based on the parameter
1613              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1614              THEN
1615                 l_schedule_ship_date := p_line_rec.request_date;
1616              END IF;
1617           ELSE
1618              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1619              THEN
1620                 l_schedule_arrival_date := p_line_rec.request_date;
1621              END IF;
1622           END IF;
1623         --l_schedule_arrival_date := p_line_rec.request_date;
1624        END IF;
1625 
1626        IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
1627                                p_old_line_rec.schedule_arrival_date)
1628        THEN
1629           l_schedule_arrival_date := p_line_rec.schedule_arrival_date;
1630        END IF;
1631     END IF;
1632 
1633     IF NOT OE_GLOBALS.Equal(p_line_rec.shipping_method_code ,
1634                                p_old_line_rec.shipping_method_code )
1635     THEN
1636           l_shipping_method_code := p_line_rec.shipping_method_code ;
1637     END IF;
1638 
1639 
1640     -- 2391781,2787962
1641     -- Warehouse change only for arrival set
1642 
1643     IF  (NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
1644                               p_old_line_rec.ship_from_org_id) AND
1645          (NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y' OR
1646           p_line_rec.ato_line_id is not null) AND
1647           p_line_rec.arrival_set_id is not null)
1648     OR  NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1649                              p_old_line_rec.request_date)
1650     THEN
1651 
1652        IF  NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y'
1653        OR  NVL(p_line_rec.top_model_line_id ,-99) = p_line_rec.line_id
1654        THEN
1655 
1656           l_cascade_line_id := p_line_rec.top_model_line_id;
1657           IF l_debug_level  > 0 THEN
1658            oe_debug_pub.add('Going to Log Cascade warehouse for SMC Model',2);
1659           END IF;
1660 
1661        ELSIF p_line_rec.ato_line_id is not null
1662        AND   NOT(p_line_rec.ato_line_id = p_line_rec.line_id
1663        AND   p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
1664                                            OE_GLOBALS.G_ITEM_OPTION))
1665        THEN
1666 
1667           IF l_debug_level  > 0 THEN
1668            oe_debug_pub.add('Going to Log Cascade warehouse for ato Model',2);
1669           END IF;
1670 
1671           l_cascade_line_id := p_line_rec.ato_line_id;
1672 
1673        ELSIF p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CLASS THEN
1674           l_cascade_line_id := p_line_rec.line_id;
1675        END IF;
1676     END IF;
1677     -- 2391781, 2787962
1678 
1679 
1680  ELSE
1681 
1682   --   Log a request based on the type of the line.
1683 
1684 
1685     IF  ((p_line_rec.ato_line_id is not null AND
1686          NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
1687               p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
1688                                             OE_GLOBALS.G_ITEM_OPTION)))
1689     OR  (p_line_rec.line_id = p_line_rec.top_model_line_id))
1690     AND  NOT (OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
1691          AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 )
1692     THEN
1693 
1694     IF l_debug_level  > 0 THEN
1695         oe_debug_pub.add(  '3 SETTING GROUP SCHEDULE' , 2 ) ;
1696     END IF;
1697 
1698 /* Added the following 1 line to fix the bug 2740480 */
1699      l_push_logic := 'Y';
1700 
1701      l_group_request := TRUE;
1702      l_action := OESCH_ACT_RESCHEDULE;
1703      -- Scheduling action would be reschedule.
1704 
1705     ELSE
1706 
1707     IF l_debug_level  > 0 THEN
1708         oe_debug_pub.add(  '4 SCHEDULE LINE' , 2 ) ;
1709     END IF;
1710      /*changes for bug 6719457*/
1711       IF l_debug_level  > 0 THEN
1712         oe_debug_pub.add(  'Checking if Group_Schedule delayed request already exists for the line' , 2 ) ;
1713     END IF;
1714 
1715       IF OE_Delayed_Requests_PVT.Check_for_Request
1716      (   p_entity_code  =>OE_GLOBALS.G_ENTITY_ALL
1717      ,   p_entity_id    =>p_line_rec.line_id
1718      ,   p_request_type =>OE_GLOBALS.G_GROUP_SCHEDULE)  THEN
1719 
1720         l_group_request := TRUE;
1721      ELSE
1722           l_group_request := FALSE;
1723      END IF;
1724 
1725  /*Changes for bug 6719457*/
1726 
1727      -- scheduling action would be schedule or re-schedule
1728      -- based on the scheduling status.
1729 
1730      IF p_line_rec.schedule_status_code is not null THEN
1731         l_action := OESCH_ACT_RESCHEDULE;
1732      ELSE
1733         l_action := OESCH_ACT_SCHEDULE;
1734      END IF;
1735 
1736      -- scheduling action would be schedule or re-schedule
1737      -- based on the scheduling status.
1738 
1739     END IF;
1740 
1741  END IF;
1742 
1743  IF p_line_rec.arrival_set_id is not null THEN
1744     l_param1      := p_line_rec.arrival_set_id;
1745     l_entity_type := OESCH_ENTITY_ARRIVAL_SET;
1746  ELSE
1747      l_param1      := p_line_rec.ship_set_id;
1748      l_entity_type := OESCH_ENTITY_SHIP_SET;
1749  END IF;
1750 
1751 
1752  -- Store the old inventory item to pass it to
1753  -- MRP during rescheduling the scheduled item.
1754 
1755  IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
1756                          p_old_line_rec.inventory_item_id)
1757  AND p_line_rec.schedule_status_code IS NOT NULL THEN
1758 
1759   OE_Item_Tbl(p_line_rec.line_id).line_id
1760                     := p_line_rec.line_id;
1761   OE_Item_Tbl(p_line_rec.line_id).inventory_item_id
1762                     := p_old_line_rec.inventory_item_id;
1763  END IF;
1764  -- 3384975
1765  l_param12 := 'N';
1766 
1767  -- set the variable 12 when scheduling attributes changed on a
1768  -- scheduled line.
1769  --- 3344843 Also set when reserved quantity changed
1770 
1771  IF  (p_line_rec.schedule_status_code is NOT NULL
1772   AND (Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1773                                 p_old_line_rec => p_old_line_rec)
1774    OR  p_line_rec.ordered_quantity <> p_old_line_rec.ordered_quantity
1775    OR  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1776                              p_old_line_rec.override_atp_date_code)
1777    OR NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1778                            p_old_line_rec.reserved_quantity)))
1779    OR  p_line_rec.schedule_action_code is not null
1780  THEN
1781 
1782     IF l_debug_level  > 0 THEN
1783        oe_debug_pub.add(  'SET L PARAM 12' , 4 ) ;
1784     END IF;
1785     l_param12 := 'Y';
1786 
1787  END IF;
1788 
1789 
1790  IF l_group_request THEN
1791 
1792     IF l_debug_level  > 0 THEN
1793         oe_debug_pub.add(  'BEFORE LOGGING G_GROUP_SCHEDULE' , 2 ) ;
1794     END IF;
1795     -- Log group set. which will execute a group schedule.
1796    /* -- 3384975 moved out of l_group_request check
1797     l_param12 := 'N';
1798 
1799     -- set the variable 12 when scheduling attributes changed on a
1800     -- scheduled line.
1801     --- 3344843 Also set when reserved quantity changed
1802 
1803     IF  p_line_rec.schedule_status_code is NOT NULL
1804     AND (Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1805                                     p_old_line_rec => p_old_line_rec)
1806     OR  p_line_rec.ordered_quantity <> p_old_line_rec.ordered_quantity
1807     OR  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1808                              p_old_line_rec.override_atp_date_code)
1809     OR NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1810                            p_old_line_rec.reserved_quantity))
1811     THEN
1812 
1813        IF l_debug_level  > 0 THEN
1814            oe_debug_pub.add(  'SET L PARAM 12' , 4 ) ;
1815        END IF;
1816        l_param12 := 'Y';
1817 
1818     END IF;
1819    */
1820     --for bug 3314157
1821     IF OE_QUOTE_UTIL.G_COMPLETE_NEG='Y'
1822     THEN
1823        l_operation:=OE_GLOBALS.G_OPR_CREATE;
1824     ELSE
1825        l_operation:= p_line_rec.operation;
1826     END IF;
1827     -- 3586151 OE_GLOBALS.G_ENTITY_LINE is replaced by OE_GLOBALS.G_ENTITY_ALL for p_entity_code to execute at the end.
1828     OE_delayed_requests_Pvt.log_request
1829      (p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,  --OE_GLOBALS.G_ENTITY_LINE,
1830       p_entity_id              => p_line_rec.line_id,
1831       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
1832       p_requesting_entity_id   => p_line_rec.line_id,
1833       p_request_type           => OE_GLOBALS.G_GROUP_SCHEDULE,
1834       p_param1                 => l_param1,
1835       p_param2                 => p_line_rec.header_id,
1836       p_param3                 => l_action,
1837       p_param4                 => p_old_line_rec.ship_from_org_id,
1838       p_param5                 => p_old_line_rec.ship_to_org_id,
1839       p_date_param1            => p_old_line_rec.schedule_ship_date,
1840       p_date_param2            => p_old_line_rec.schedule_arrival_date,
1841       p_date_param3            => p_old_line_rec.request_date,
1842       p_date_param4            => l_schedule_ship_date,
1843       p_date_param5            => l_schedule_arrival_date,
1844       p_param6                 => p_old_line_rec.ship_set_id,
1845       p_param7                 => p_old_line_rec.arrival_set_id,
1846       p_param8                 => l_entity_type,
1847       p_param9                 => l_ship_to_org_id,
1848       p_param10                => l_ship_from_org_id,
1849       p_param11                => nvl(l_shipping_method_code,p_line_rec.shipping_method_code),
1850 /* removed param11 to fix the bug 2916814 */
1851       p_param12                => l_param12,
1852 /* Added the following 1 line to fix the bug 2740480 */
1853       p_param13                => l_push_logic,
1854     /*REplaced  p_line_rec.operation with l_operation as a fix for bug 3314157*/
1855       p_param14                => l_operation,
1856       x_return_status          => x_return_status);
1857 
1858 
1859    -- 2391781
1860    IF l_cascade_line_id IS NOT NULL
1861    THEN
1862       IF l_debug_level  > 0 THEN
1863          oe_debug_pub.add(  'Logging cascade warehouse / Request date ' , 4 ) ;
1864       END IF;
1865       OE_delayed_requests_Pvt.log_request
1866          (p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
1867           p_entity_id              => l_cascade_line_id,
1868           p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
1869           p_requesting_entity_id   => l_cascade_line_id,
1870           p_request_type           => OE_GLOBALS.G_CASCADE_SCH_ATTRBS,
1871           p_param1                 => NVL(p_line_rec.arrival_set_id,
1872                                           p_line_rec.ship_set_id),
1873           p_param2                 => p_line_rec.ship_from_org_id,
1874           p_date_param1            => p_line_rec.request_date,
1875           x_return_status          => x_return_status);
1876 
1877 
1878    END IF;
1879    -- 2391781
1880 
1881 
1882 
1883  ELSE
1884 
1885     -- Log schedule or reschedule based on the action.
1886 
1887     -- See of the line is scheduled and getting added to set with
1888     -- same scheduling and set attributes, avoid logging the request.
1889     -- 3384975 Override_atp_date_code check added
1890 
1891     IF p_line_rec.schedule_status_code IS NOT NULL AND
1892     NOT Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1893                                    p_old_line_rec => p_old_line_rec)
1894     AND     OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1895                              p_old_line_rec.override_atp_date_code)
1896     AND p_line_rec.ordered_quantity = p_old_line_rec.ordered_quantity
1897     AND (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD OR
1898         nvl(p_line_rec.model_remnant_flag,'N') = 'Y') THEN
1899 
1900         IF l_debug_level  > 0 THEN
1901             oe_debug_pub.add(  'ARRIVAL_SET_ID : ' || P_LINE_REC.ARRIVAL_SET_ID || ':' || P_LINE_REC.SHIP_SET_ID , 2 ) ;
1902         END IF;
1903 
1904         IF l_debug_level  > 0 THEN
1905             oe_debug_pub.add(  'OLD SHIP DATE ' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 2 ) ;
1906         END IF;
1907         --- 3344843 Put to old set if set attribs match and there is no change in reserve quantity.
1908         IF Set_Attr_Matched
1909            (p_set_ship_from_org_id    => l_set_ship_from_org_id ,
1910             p_line_ship_from_org_id   => p_line_rec.ship_from_org_id,
1911             p_set_ship_to_org_id      => l_set_ship_to_org_id ,
1912             p_line_ship_to_org_id     => p_line_rec.ship_to_org_id ,
1913             p_set_schedule_ship_date  => l_set_schedule_ship_date ,
1914             p_line_schedule_ship_date => p_line_rec.schedule_ship_date,
1915             p_set_arrival_date        => l_set_schedule_arrival_date,
1916             p_line_arrival_date       => p_line_rec.schedule_arrival_date,
1917             p_line_shipping_method_code => p_line_rec.shipping_method_code ,
1918             p_set_shipping_method_code => l_set_shipping_method_code ,
1919             p_set_type                => l_entity_type)
1920           AND OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1921            p_old_line_rec.reserved_quantity) THEN
1922 
1923              IF l_debug_level  > 0 THEN
1924                  oe_debug_pub.add(  'ONLY SCHEDULED LINE IS GETTING INTO OLD SET' , 2 ) ;
1925              END IF;
1926              l_matched := TRUE;
1927              -- Since we are not logging delayed request, we will update the
1928              -- ship method if it does not match.
1929 
1930 /* Commented the following code to fix the bug 2916814
1931 
1932             IF NOT OE_GLOBALS.EQUAL(p_line_rec.shipping_method_code,
1933                                     l_shipping_method_code) THEN
1934 
1935                BEGIN
1936 
1937                 Update oe_order_lines_all
1938                 Set shipping_method_code = l_shipping_method_code
1939                 where header_id = p_line_rec.header_id
1940                 and line_id = p_line_rec.line_id;
1941               EXCEPTION
1942                 WHEN OTHERS THEN
1943                      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1944 
1945                END;
1946             END IF;
1947 
1948     */
1949 
1950         END IF; -- compare.
1951 
1952     END IF;  -- not null
1953 
1954     IF NOT l_matched THEN
1955     IF l_debug_level  > 0 THEN
1956         oe_debug_pub.add(  'BEFORE LOGGING G_SCHEDULE_LINE' , 2 ) ;
1957     END IF;
1958 
1959      l_line_id := p_line_rec.line_id;
1960 
1961      IF (OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
1962      AND MSC_ATP_GLOBAL.GET_APS_VERSION = 10 ) THEN
1963 
1964          l_line_id := Nvl(p_line_rec.ato_line_id,p_line_rec.line_id);
1965      END IF;
1966 
1967      OE_delayed_requests_Pvt.log_request
1968      (p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
1969       p_entity_id              => l_line_id,
1970       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
1971       p_requesting_entity_id   => p_line_rec.line_id,
1972       p_request_type           => OE_GLOBALS.G_SCHEDULE_LINE,
1973       p_param1                 => l_param1,
1974       p_param2                 => p_line_rec.header_id,
1975       p_param3                 => l_action,
1976       p_param4                 => p_old_line_rec.ship_from_org_id,
1977       p_param5                 => p_old_line_rec.ship_to_org_id,
1978       p_param6                 => p_old_line_rec.ship_set_id,
1979       p_param7                 => p_old_line_rec.arrival_set_id,
1980       p_param8                 => l_entity_type,
1981       p_param9                 => l_ship_to_org_id,
1982       p_param10                => l_ship_from_org_id,
1983       p_param11                => l_set_shipping_method_code,
1984       p_param12                => l_param12,
1985 /* commented the above line to fix the bug 2916814 */
1986       p_date_param1            => p_old_line_rec.schedule_ship_date,
1987       p_date_param2            => p_old_line_rec.schedule_arrival_date,
1988       p_date_param3            => p_old_line_rec.request_date,
1989       p_date_param4            => l_schedule_ship_date,
1990       p_date_param5            => l_schedule_arrival_date,
1991       x_return_status          => x_return_status);
1992 
1993     END IF;
1994 
1995 
1996  END IF;
1997     --3344843 Storing the change in reserve quantity
1998     l_diff_res_qty := nvl(p_line_rec.reserved_quantity, 0) -
1999                     nvl(p_old_line_rec.reserved_quantity, 0);
2000 -- INVCONV may need changes here for sets
2001 
2002     IF l_debug_level  > 0 THEN
2003       oe_debug_pub.add(  'RES QTY DIFF '|| L_DIFF_RES_QTY , 1 ) ;
2004     END IF;
2005     IF l_diff_res_qty <> 0 THEN
2006        IF l_diff_res_qty > 0 THEN
2007           l_qty_to_reserve   := l_diff_res_qty;
2008           l_qty_to_unreserve := null;
2009        ELSIF l_diff_res_qty < 0 THEN
2010           l_qty_to_unreserve := 0 - l_diff_res_qty;
2011        END IF;
2012 
2013 
2014        l_qty_to_reserve
2015          := NVL(p_old_line_rec.reserved_quantity,0) + l_diff_res_qty;
2016        l_qty_to_unreserve
2017          := p_old_line_rec.reserved_quantity;
2018        l_index          := p_line_rec.line_id;
2019        IF l_qty_to_reserve is not NULL OR
2020              l_qty_to_unreserve  is not NULL THEN
2021 
2022           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).entity_id
2023                            := p_line_rec.top_model_line_id;
2024           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).line_id
2025                            := p_line_rec.line_id;
2026           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).qty_to_reserve
2027                            := l_qty_to_reserve;
2028           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).qty_to_unreserve
2029                            := l_qty_to_unreserve;
2030        END IF;
2031     END IF;
2032 
2033  <<END_PROCESS>>
2034 
2035  IF l_debug_level  > 0 THEN
2036     oe_debug_pub.add(  'EXITING LOG SET REQUEST' , 1 ) ;
2037  END IF;
2038 
2039  --If the scheduling action is not null then system should execute the delyed request.
2040 
2041 EXCEPTION
2042    WHEN FND_API.G_EXC_ERROR THEN
2043 
2044         x_return_status := FND_API.G_RET_STS_ERROR;
2045 
2046    WHEN OTHERS THEN
2047 
2048         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2049 
2050         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2051         THEN
2052             OE_MSG_PUB.Add_Exc_Msg
2053             (   G_PKG_NAME,
2054               'Log_Set_Request');
2055         END IF;
2056 END Log_Set_Request;
2057 
2058 /*-----------------------------------------------------------------------------
2059 Procedure Name : Schedule_Attribute_Changed
2060 Description    : This function returns TRUE is scheduling attribute is changed
2061                  on a line. This is required for rescheduling.
2062 ----------------------------------------------------------------------------- */
2063 
2064 
2065 FUNCTION Schedule_Attribute_Changed
2066 ( p_line_rec     IN Oe_Order_Pub.line_rec_type
2067 , p_old_line_rec IN Oe_Order_Pub.line_rec_type)
2068 RETURN BOOLEAN
2069 IS
2070 --
2071 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2072 --
2073 BEGIN
2074 
2075     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIP_FROM_ORG_ID,
2076                            p_old_line_rec.SHIP_FROM_ORG_ID)
2077     THEN
2078        RETURN TRUE;
2079     END IF;
2080 
2081     IF NOT OE_GLOBALS.Equal(p_line_rec.SUBINVENTORY,
2082                            p_old_line_rec.SUBINVENTORY)
2083     THEN
2084        RETURN TRUE;
2085     END IF;
2086 
2087     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIP_TO_ORG_ID,
2088                            p_old_line_rec.SHIP_TO_ORG_ID)
2089     THEN
2090        RETURN TRUE;
2091     END IF;
2092 
2093     IF NOT OE_GLOBALS.Equal(p_line_rec.DEMAND_CLASS_CODE,
2094                            p_old_line_rec.DEMAND_CLASS_CODE)
2095     THEN
2096        RETURN TRUE;
2097     END IF;
2098 
2099     IF NOT OE_GLOBALS.Equal(p_line_rec.SCHEDULE_SHIP_DATE,
2100                            p_old_line_rec.SCHEDULE_SHIP_DATE)
2101     THEN
2102        RETURN TRUE;
2103     END IF;
2104 
2105     IF NOT OE_GLOBALS.Equal(p_line_rec.SCHEDULE_ARRIVAL_DATE,
2106                            p_old_line_rec.SCHEDULE_ARRIVAL_DATE)
2107     THEN
2108        RETURN TRUE;
2109     END IF;
2110 
2111     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIPPING_METHOD_CODE,
2112                            p_old_line_rec.SHIPPING_METHOD_CODE)
2113     THEN
2114        RETURN TRUE;
2115     END IF;
2116 
2117     IF NOT OE_GLOBALS.Equal(p_line_rec.REQUEST_DATE,
2118                            p_old_line_rec.REQUEST_DATE)
2119     THEN
2120        RETURN TRUE;
2121     END IF;
2122 
2123     IF NOT OE_GLOBALS.Equal(p_line_rec.DELIVERY_LEAD_TIME,
2124                            p_old_line_rec.DELIVERY_LEAD_TIME)
2125     THEN
2126        RETURN TRUE;
2127     END IF;
2128 
2129 
2130     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
2131                             p_old_line_rec.inventory_item_id)
2132     THEN
2133        RETURN TRUE;
2134     END IF;
2135 
2136     IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,
2137                             p_old_line_rec.order_quantity_uom)
2138     THEN
2139        RETURN TRUE;
2140     END IF;
2141 
2142     IF NOT OE_GLOBALS.Equal(p_line_rec.sold_to_org_id,
2143                             p_old_line_rec.sold_to_org_id)
2144     THEN
2145        RETURN TRUE;
2146     END IF;
2147 
2148 
2149     RETURN FALSE;
2150     IF l_debug_level  > 0 THEN
2151         oe_debug_pub.add(  'RETURNING FALSE ' , 3 ) ;
2152     END IF;
2153 
2154 EXCEPTION
2155 
2156    WHEN OTHERS THEN
2157 
2158         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2159         THEN
2160             OE_MSG_PUB.Add_Exc_Msg
2161             (   G_PKG_NAME,
2162               'Schedule_Attribute_Changed');
2163         END IF;
2164 END Schedule_Attribute_Changed;
2165 
2166 /*-----------------------------------------------------------------------
2167 -- BUG 1955004
2168 Procedure Name : Inactive_Demand_Scheduling
2169 Description    : This procedure is invoked when it is desired to bypass
2170                  the call to ATP for SCHEDULING.  It is primarily written
2171                  for the new Scheduling Levels of FOUR and FIVE, which are
2172                  'Inactive Demand with Reservations' and 'Inactive Demand
2173                  without Reservations'.  This procedure will act on four
2174                  fields - schedule_ship_date, schedule_arrival_date,
2175                  visible_demand_flag, and schedule_status_code.
2176 ------------------------------------------------------------------------*/
2177 PROCEDURE Inactive_Demand_Scheduling
2178 ( p_x_old_line_rec  IN OE_ORDER_PUB.line_rec_type
2179 , p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type
2180 , p_sch_action      IN VARCHAR2 := NULL
2181 , x_return_status OUT NOCOPY VARCHAR2
2182 
2183 ) IS
2184 
2185 l_sch_action           VARCHAR2(30) := p_sch_action;
2186 l_order_date_type_code VARCHAR2(30);
2187 
2188 l_return_status   VARCHAR2(1);  -- Added for IR ISO Tracking bug 7667702
2189 --
2190 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2191 --
2192 BEGIN
2193 
2194   IF l_debug_level  > 0 THEN
2195       oe_debug_pub.add(  'ENTERING INACTIVE_DEMAND_SCHEDULING' , 1 ) ;
2196   END IF;
2197   X_return_status := FND_API.G_RET_STS_SUCCESS;
2198 
2199   IF l_sch_action is NULL THEN
2200     L_sch_action := p_x_line_rec.schedule_action_code;
2201   END IF;
2202 
2203   l_order_date_type_code :=
2204             NVL(Get_Date_Type(p_x_line_rec.header_id), 'SHIP');
2205 
2206 
2207   IF l_sch_action = OESCH_ACT_SCHEDULE OR
2208      l_sch_action = OESCH_ACT_RESCHEDULE OR
2209      l_sch_action = OESCH_ACT_REDEMAND OR
2210      l_sch_action = OESCH_ACT_DEMAND OR
2211      (l_sch_action = OESCH_ACT_RESERVE AND
2212       p_x_line_rec.schedule_status_code IS NULL) THEN
2213     -- set the data accordingly.
2214 
2215     IF l_debug_level  > 0 THEN
2216         oe_debug_pub.add(  'INACTIVE DEMAND - GOING TO SCHEDULE' , 1 ) ;
2217     END IF;
2218 
2219     IF l_order_date_type_code = 'SHIP' THEN
2220       IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
2221                               p_x_old_line_rec.schedule_ship_date) AND
2222          p_x_line_rec.schedule_ship_date IS NOT NULL AND
2223          p_x_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE THEN
2224            -- If the user provides a ship_date, or changes the existing, use it
2225            p_x_line_rec.schedule_arrival_date := p_x_line_rec.schedule_ship_date;
2226 
2227       ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.request_date,
2228                                  p_x_old_line_rec.request_date) AND
2229          p_x_line_rec.request_date IS NOT NULL AND
2230          p_x_line_rec.request_date <> FND_API.G_MISS_DATE THEN
2231            -- if the user changed request date, use it
2232            p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2233            p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2234 
2235       ELSE
2236         --  dates have not changed, so use whichever is not null, ship first
2237           IF p_x_line_rec.schedule_ship_date IS NOT NULL THEN
2238             p_x_line_rec.schedule_arrival_date := p_x_line_rec.schedule_ship_date;
2239           ELSE
2240             p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2241             p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2242           END IF;
2243 
2244       END IF;
2245 
2246     ELSE -- Arrival
2247 
2248       IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_arrival_date,
2249                               p_x_old_line_rec.schedule_arrival_date) AND
2250          p_x_line_rec.schedule_arrival_date IS NOT NULL AND
2251          p_x_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE THEN
2252            -- If the user provides a arrival_date, or changes the existing, use it
2253            p_x_line_rec.schedule_ship_date := p_x_line_rec.schedule_arrival_date;
2254 
2255       ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.request_date,
2256                                  p_x_old_line_rec.request_date) AND
2257          p_x_line_rec.request_date IS NOT NULL AND
2258          p_x_line_rec.request_date <> FND_API.G_MISS_DATE THEN
2259            -- if the user changed request date, use it
2260            p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2261            p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2262 
2263       ELSE
2264         --  dates have not changed, so use whichever is not null, ship first
2265         IF p_x_line_rec.schedule_arrival_date IS NOT NULL THEN
2266           -- it is arrival date type, so use the arrival date provided to set ship
2267           p_x_line_rec.schedule_ship_date := p_x_line_rec.schedule_arrival_date;
2268 
2269         ELSE
2270           -- if user does not provide a date, use request date
2271           p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2272           p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2273 
2274         END IF;
2275        END IF;
2276     END IF;
2277 
2278     -- we want this line scheduled, but not visible for demand
2279     p_x_line_rec.visible_demand_flag := 'N';
2280     p_x_line_rec.schedule_status_code := OESCH_STATUS_SCHEDULED;
2281     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
2282        -- Promise date setup
2283        Promise_Date_for_Sch_Action
2284             (p_x_line_rec => p_x_line_rec
2285             ,p_sch_action => l_sch_action
2286             ,P_header_id  => p_x_line_rec.header_id);
2287     END IF;
2288 
2289   ELSIF l_sch_action = OESCH_ACT_UNSCHEDULE OR
2290         L_sch_action = OESCH_ACT_UNDEMAND THEN
2291     -- we are unscheduling
2292 
2293     IF l_debug_level  > 0 THEN
2294         oe_debug_pub.add(  'INACTIVE DEMAND - GOING TO UNSCHEDULE' , 1 ) ;
2295     END IF;
2296 
2297     P_x_line_rec.schedule_ship_date := NULL;
2298     P_x_line_rec.schedule_arrival_date := NULL;
2299     p_x_line_rec.visible_demand_flag := NULL;
2300     p_x_line_rec.schedule_status_code := NULL;
2301 
2302     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
2303        -- Promise date setup
2304        Promise_Date_for_Sch_Action
2305             (p_x_line_rec => p_x_line_rec
2306             ,p_sch_action => l_sch_action
2307             ,P_header_id  => p_x_line_rec.header_id);
2308     END IF;
2309   ELSE
2310   -- We should not have any other actions here.
2311   -- If we do, it is an error.
2312     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2313 
2314   END IF;
2315 
2316 /* IR ISO Tracking bug 7667702: IR ISO Change Management Begins */
2317 
2318 -- This code is hooked for IR ISO project, where if the schedule ship/
2319 -- arrival date is changed and MRP is not installed then a delayed request is
2320 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
2321 -- API from Purchasing, responsible for conditionally updating the Need By
2322 -- Date column in internal requisition line. This will be done based on
2323 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
2324 
2325 -- For details on IR ISO CMS project, please refer to FOL >
2326 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
2327 
2328 
2329     IF (p_x_line_rec.order_source_id = 10) AND
2330        (
2331         ((p_x_old_line_rec.schedule_ship_date IS NOT NULL) AND
2332           NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,p_x_old_line_rec.schedule_ship_date))
2333        OR
2334         ((p_x_old_line_rec.schedule_arrival_date IS NOT NULL) AND
2335           NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_arrival_date,p_x_old_line_rec.schedule_arrival_date))
2336        )  THEN
2337 
2338        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
2339          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
2340 
2341          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
2342 
2343            IF l_debug_level > 0 THEN
2344              oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change');
2345            END IF;
2346 
2347            -- Log a delayed request to update the change in Schedule Ship Date to
2348            -- Requisition Line. This request will be logged only if the change is
2349            -- not initiated from Requesting Organization, and it is not a case of
2350            -- Internal Sales Order Full Cancellation. It will even not be logged
2351            -- Purchasing profile option does not allow update of Need By Date when
2352            -- Schedule Ship Date changes on internal sales order line
2353 
2354            OE_delayed_requests_Pvt.log_request
2355            ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
2356            , p_entity_id              => p_x_line_rec.line_id
2357            , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
2358            , p_requesting_entity_id   => p_x_line_rec.line_id
2359            , p_request_unique_key1    => p_x_line_rec.header_id  -- Order Hdr_id
2360            , p_request_unique_key2    => p_x_line_rec.source_document_id -- Req Hdr_id
2361            , p_request_unique_key3    => p_x_line_rec.source_document_line_id -- Req Line_id
2362            , p_date_param1            => p_x_line_rec.schedule_arrival_date
2363            -- Note: p_date_param1 is used for both Schedule_Ship_Date and
2364            -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
2365            -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
2366            -- it can expect change with respect to Ship or Arrival date. Thus, will
2367            -- not raise any issues.
2368            , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
2369            , x_return_status          => l_return_status
2370            );
2371 
2372            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2373              RAISE FND_API.G_EXC_ERROR;
2374            END IF;
2375 
2376          ELSE
2377            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
2378              IF l_debug_level > 0 THEN
2379                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
2380              END IF;
2381 
2382              OE_SCHEDULE_UTIL.Update_PO(p_x_line_rec.schedule_arrival_date,
2383                 p_x_line_rec.source_document_id,
2384                 p_x_line_rec.source_document_line_id);
2385            END IF;
2386          END IF;
2387 
2388        END IF;
2389      END IF; -- Order_Source_id
2390 
2391 /* ============================= */
2392 /* IR ISO Change Management Ends */
2393 
2394 EXCEPTION
2395 
2396    WHEN FND_API.G_EXC_ERROR THEN -- Added for IR ISO Tracking bug 7667702
2397       x_return_status :=  FND_API.G_RET_STS_ERROR;
2398 
2399   WHEN OTHERS THEN
2400 
2401     X_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2402 
2403     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2404     THEN
2405 
2406       OE_MSG_PUB.Add_Exc_Msg
2407       (G_PKG_NAME,
2408        'Inactive_Demand_Scheduling');
2409     END IF;
2410 
2411     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2412 
2413 END Inactive_Demand_Scheduling;
2414 -- END 1955004
2415 
2416 /*-----------------------------------------------------------------------
2417 Procedure Name : Initialize_mrp_record
2418 Description    : This procedure create l_count records each for each table
2419                  in the record of tables of MRP's p_atp_rec.
2420 ------------------------------------------------------------------------*/
2421 Procedure Initialize_mrp_record
2422 ( p_x_atp_rec IN  OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ
2423  ,l_count     IN  NUMBER)
2424 IS
2425  l_return_status           VARCHAR2(1);
2426  --
2427  l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2428  --
2429 BEGIN
2430   IF l_debug_level  > 0 THEN
2431       oe_debug_pub.add(  'EXTENDING THE TABLE BY ' || L_COUNT , 5 ) ;
2432   END IF;
2433 /*
2434   p_x_atp_rec.Inventory_Item_Id.extend(l_count);
2435   p_x_atp_rec.Source_Organization_Id.extend(l_count);
2436   p_x_atp_rec.Identifier.extend(l_count);
2437   p_x_atp_rec.Order_Number.extend(l_count);
2438   p_x_atp_rec.Calling_Module.extend(l_count);
2439   p_x_atp_rec.Customer_Id.extend(l_count);
2440   p_x_atp_rec.Customer_Site_Id.extend(l_count);
2441   p_x_atp_rec.Destination_Time_Zone.extend(l_count);
2442   p_x_atp_rec.Quantity_Ordered.extend(l_count);
2443   p_x_atp_rec.Quantity_UOM.extend(l_count);
2444   p_x_atp_rec.Requested_Ship_Date.extend(l_count);
2445   p_x_atp_rec.Requested_Arrival_Date.extend(l_count);
2446   p_x_atp_rec.Earliest_Acceptable_Date.extend(l_count);
2447   p_x_atp_rec.Latest_Acceptable_Date.extend(l_count);
2448   p_x_atp_rec.Delivery_Lead_Time.extend(l_count);
2449   p_x_atp_rec.Atp_Lead_Time.extend(l_count);
2450   p_x_atp_rec.Freight_Carrier.extend(l_count);
2451   p_x_atp_rec.Ship_Method.extend(l_count);
2452   p_x_atp_rec.Demand_Class.extend(l_count);
2453   p_x_atp_rec.Ship_Set_Name.extend(l_count);
2454   p_x_atp_rec.Arrival_Set_Name.extend(l_count);
2455   p_x_atp_rec.Override_Flag.extend(l_count);
2456   p_x_atp_rec.Action.extend(l_count);
2457   p_x_atp_rec.ship_date.extend(l_count);
2458   p_x_atp_rec.Available_Quantity.extend(l_count);
2459   p_x_atp_rec.Requested_Date_Quantity.extend(l_count);
2460   p_x_atp_rec.Group_Ship_Date.extend(l_count);
2461   p_x_atp_rec.Group_Arrival_Date.extend(l_count);
2462   p_x_atp_rec.Vendor_Id.extend(l_count);
2463   p_x_atp_rec.Vendor_Site_Id.extend(l_count);
2464   p_x_atp_rec.Insert_Flag.extend(l_count);
2465   p_x_atp_rec.Error_Code.extend(l_count);
2466   p_x_atp_rec.Message.extend(l_count);
2467   p_x_atp_rec.Old_Source_Organization_Id.extend(l_count);
2468   p_x_atp_rec.Old_Demand_Class.extend(l_count);
2469   p_x_atp_rec.oe_flag.extend(l_count);
2470   -- Added below attributes to fix bug 1912138.
2471   p_x_atp_rec.ato_delete_flag.extend(l_count);
2472   p_x_atp_rec.attribute_01.extend(l_count);
2473   p_x_atp_rec.attribute_05.extend(l_count);
2474   p_x_atp_rec.substitution_typ_code.extend(l_count);
2475   p_x_atp_rec.old_inventory_item_id.extend(l_count);
2476 */
2477 
2478   IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
2479   AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
2480 
2481    MSC_ATP_GLOBAL.EXTEND_ATP
2482     (p_atp_tab       => p_x_atp_rec,
2483      p_index         => l_count,
2484      x_return_status => l_return_status);
2485 
2486   ELSE
2487 
2488     MSC_SATP_FUNC.Extend_ATP
2489     (p_atp_tab       => p_x_atp_rec,
2490      p_index         => l_count,
2491      x_return_status => l_return_status);
2492 
2493   END IF;
2494 
2495 
2496 EXCEPTION
2497 
2498    WHEN OTHERS THEN
2499 
2500         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2501         THEN
2502             OE_MSG_PUB.Add_Exc_Msg
2503             (   G_PKG_NAME,
2504               'Initialize_mrp_record');
2505         END IF;
2506 END Initialize_mrp_record;
2507 /*--------------------------------------------------------------------
2508 Procedure Name : Load_MRP_request
2509 Description    :
2510   This procedure loads the MRP record or tables to be passed
2511   to MRP's API from the OM's table of records of order lines.
2512 
2513   If line line to be passed to MRP is an ATO model, we call
2514   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
2515   components, and we pass them along with the ATO model to MRP.
2516 -------------------------------------------------------------------- */
2517 Procedure Load_MRP_request
2518 ( p_line_rec           IN  OE_ORDER_PUB.line_rec_type
2519  ,p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type
2520  ,p_sch_action         IN  VARCHAR2 := NULL
2521  ,p_mrp_calc_sd        IN  VARCHAR2
2522  ,p_type_code          IN  VARCHAR2
2523  ,p_order_number       IN  NUMBER
2524  ,p_partial_set        IN  BOOLEAN := FALSE
2525  ,p_config_line_id     IN  NUMBER  := NULL
2526  ,p_part_of_set        IN  VARCHAR2 DEFAULT 'N' -- 4405004
2527  ,p_index              IN OUT NOCOPY NUMBER
2528  ,x_atp_rec            IN OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
2529 IS
2530   I                   NUMBER := p_index;
2531   l_insert_flag       NUMBER;
2532   l_oe_flag           VARCHAR2(1);
2533   l_inv_ctp           VARCHAR2(240);
2534   l_explode           BOOLEAN;
2535   l_st_atp_lead_time  NUMBER;
2536   l_st_ato_line_id    NUMBER;
2537   l_ship_set          VARCHAR2(30);
2538   l_arrival_set       VARCHAR2(30);
2539   l_action_code       VARCHAR2(30);
2540   l_action            NUMBER;
2541   l_organization_id   NUMBER;
2542   l_inventory_item_id NUMBER;
2543   l_result            NUMBER := 1;
2544 
2545   l_model_rec         MRP_ATP_PUB.ATP_Rec_Typ;
2546   l_smc_rec           MRP_ATP_PUB.ATP_Rec_Typ;
2547 
2548   lTableName          VARCHAR2(30);
2549   lMessageName        VARCHAR2(30);
2550   lErrorMessage       VARCHAR2(2000);
2551 
2552   l_model_override_atp_date_code VARCHAR2(30);
2553   --
2554   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2555   --
2556 Begin
2557 
2558   IF l_debug_level  > 0 THEN
2559       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.LOAD_MRP_REQUEST' , 1 ) ;
2560   END IF;
2561   IF l_debug_level  > 0 THEN
2562       oe_debug_pub.add('------------Load MRP Table-----------',1);
2563   END IF;
2564 
2565     IF l_action_code <> OESCH_ACT_ATP_CHECK THEN
2566        G_ATP_CHECK_Session_Id := Null;
2567     END IF;
2568 
2569     IF p_sch_action IS NULL THEN
2570         l_action_code := p_line_rec.schedule_action_code;
2571     ELSE
2572         l_action_code := p_sch_action;
2573     END IF;
2574 
2575     IF nvl(p_mrp_calc_sd,'N') = 'Y' THEN
2576       l_insert_flag   := 1;
2577     ELSE
2578       l_insert_flag   := 0;
2579     END IF;
2580 
2581     x_atp_rec.atp_lead_time(I)   := 0;
2582 
2583     IF l_debug_level  > 0 THEN
2584        oe_debug_pub.add('Line Id              : ' || p_line_rec.line_id,3);
2585     END IF;
2586     IF l_debug_level  > 0 THEN
2587        oe_debug_pub.add('Schedule Action      : ' || l_action_code,3);
2588     END IF;
2589 
2590 /*
2591     IF p_line_rec.source_document_type_id = 10 THEN
2592       oe_debug_pub.add('It is an internal order ',3);
2593       l_oe_flag := 'Y';
2594 
2595       IF (p_line_rec.schedule_ship_date IS NOT NULL AND
2596           p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE ) OR
2597          (p_line_rec.schedule_arrival_date IS NOT NULL AND
2598           p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE ) THEN
2599 
2600           oe_debug_pub.add('No changes to date as it has been passed',3);
2601       ELSE
2602           oe_debug_pub.add('Pass the request date as arrival date',3);
2603 
2604           x_atp_rec.Requested_ship_Date(I)  := null;
2605           x_atp_rec.Requested_arrival_Date(I) := p_line_rec.request_date;
2606 
2607       END IF;
2608 
2609       x_atp_rec.attribute_01(I) := p_line_rec.source_document_id;
2610 
2611     ELSE
2612 
2613       oe_debug_pub.add('It is not an internal order ',3);
2614       l_oe_flag := 'N';
2615 
2616     END IF;
2617 
2618     x_atp_rec.oe_flag(I) := l_oe_flag;
2619     oe_debug_pub.add('OE Flag is : '||x_atp_rec.oe_flag(I),3);
2620 */
2621 
2622     IF p_line_rec.arrival_set_id is null
2623     THEN
2624       l_arrival_set := p_line_rec.arrival_set;
2625     ELSE
2626       l_arrival_set := nvl(p_line_rec.arrival_set,to_char(p_line_rec.arrival_set_id));
2627     END IF;
2628 
2629     IF  p_line_rec.ship_set_id is null
2630     THEN
2631       l_ship_set := p_line_rec.ship_set;
2632     ELSE
2633       l_ship_set := nvl(p_line_rec.ship_set,to_char(p_line_rec.ship_set_id));
2634     END IF;
2635 
2636     IF l_arrival_set = FND_API.G_MISS_CHAR THEN
2637        l_arrival_set := Null;
2638     END IF;
2639     IF l_ship_set = FND_API.G_MISS_CHAR THEN
2640        l_ship_set := Null;
2641     END IF;
2642     IF l_debug_level  > 0 THEN
2643         oe_debug_pub.add(  'SHIP_SET : ' || L_SHIP_SET , 3 ) ;
2644     END IF;
2645     IF l_debug_level  > 0 THEN
2646         oe_debug_pub.add(  'ARRIVAL SET : ' || L_ARRIVAL_SET , 3 ) ;
2647     END IF;
2648 
2649 
2650 /*      IF (p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
2651             p_line_rec.ship_from_org_id := null;
2652         END IF;
2653         IF (p_old_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
2654             p_old_line_rec.ship_from_org_id := null;
2655         END IF;
2656 */
2657 /*
2658 
2659         IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
2660                                 p_old_line_rec.ship_from_org_id) OR
2661                (p_line_rec.re_source_flag = 'N')
2662 */
2663 
2664     x_atp_rec.Inventory_Item_Id(I)         := p_line_rec.inventory_item_id;
2665 
2666     IF (p_line_rec.ship_from_org_id IS NOT NULL) THEN
2667       x_atp_rec.Source_Organization_Id(I)
2668                       := p_line_rec.ship_from_org_id;
2669       IF l_debug_level  > 0 THEN
2670           oe_debug_pub.add(  'SHIP FROM : ' || P_LINE_REC.SHIP_FROM_ORG_ID , 3 ) ;
2671       END IF;
2672 
2673     ELSE
2674       x_atp_rec.Source_Organization_Id(I) := null;
2675       IF l_debug_level  > 0 THEN
2676           oe_debug_pub.add(  'SHIP FROM IS NULL ' , 3 ) ;
2677       END IF;
2678     END IF;
2679 
2680     x_atp_rec.Identifier(I)                := p_line_rec.line_id;
2681     x_atp_rec.Order_Number(I)              := p_order_number;
2682     x_atp_rec.Calling_Module(I)            := 660;
2683     x_atp_rec.Customer_Id(I)               := p_line_rec.sold_to_org_id;
2684     x_atp_rec.Customer_Site_Id(I)          := p_line_rec.ship_to_org_id;
2685     x_atp_rec.Destination_Time_Zone(I)     := p_line_rec.item_type_code;
2686     x_atp_rec.Quantity_Ordered(I)          := p_line_rec.ordered_quantity;
2687     x_atp_rec.Quantity_UOM(I)              := p_line_rec.order_quantity_uom;
2688     x_atp_rec.Earliest_Acceptable_Date(I)  := null;
2689 
2690     IF l_debug_level  > 0 THEN
2691         oe_debug_pub.add(  'A1 : ' || P_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
2692     END IF;
2693     IF l_debug_level  > 0 THEN
2694         oe_debug_pub.add(  'A2 : ' || P_OLD_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
2695     END IF;
2696 
2697     -- Start 2691579 --
2698     IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
2699        IF  (p_line_rec.operation = OE_GLOBALS.G_OPR_DELETE)
2700        AND (p_line_rec.ato_line_id IS NOT NULL AND
2701        NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
2702             p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
2703                                           OE_GLOBALS.G_ITEM_OPTION))) THEN
2704           x_atp_rec.ato_delete_flag(I) := 'Y';
2705           IF l_debug_level  > 0 THEN
2706              oe_debug_pub.add(  'ATO DELETE FLAG : '
2707                              || x_atp_rec.ato_delete_flag(I) , 1 ) ;
2708           END IF;
2709        END IF;
2710     END IF;
2711     -- End 2691579 --
2712 
2713 /* Commented the following condition to fix the bug 2823868 */
2714 /* The problem is that if someone has set order date type as arrival
2715    and the puts the line in a ship set , then p_line_rec.ship_set_id will be not null
2716    so the schedule_ship_date will be passed to the atp record, where as arrival_date should have
2717    passed to the arrival record */
2718 /*
2719     IF p_line_rec.arrival_set_id is not null THEN
2720 
2721       IF l_debug_level  > 0 THEN
2722           oe_debug_pub.add(  'T1' , 1 ) ;
2723       END IF;
2724       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
2725          x_atp_rec.Requested_Arrival_Date(I) :=
2726                                  p_line_rec.request_date;
2727 
2728       ELSE
2729         x_atp_rec.Requested_Arrival_Date(I) :=
2730                                p_line_rec.schedule_arrival_date;
2731       END IF;
2732       x_atp_rec.Requested_Ship_Date(I)    := null;
2733 
2734     ELSIF  p_line_rec.ship_set_id is not null THEN
2735 
2736       IF l_debug_level  > 0 THEN
2737           oe_debug_pub.add(  'T2' , 1 ) ;
2738       END IF;
2739       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
2740          x_atp_rec.Requested_Ship_Date(I)    :=
2741                                p_line_rec.request_date;
2742       ELSE
2743          x_atp_rec.Requested_Ship_Date(I)    :=
2744                                p_line_rec.schedule_ship_date;
2745       END IF;
2746       x_atp_rec.Requested_Arrival_Date(I) := null;
2747 */
2748 
2749     IF (p_type_code = 'ARRIVAL') THEN
2750 
2751       -- If user changes schedule_arrival_date then schedule based
2752       -- on the arrival_date. Otherwise look for the change in request date.
2753       -- If user changed request date, schedule based on the request
2754       -- date. Otherwise if the scheduling is happening because of
2755       -- some other changes, use nvl on arrival_date and request_dates.
2756 
2757       IF l_debug_level  > 0 THEN
2758           oe_debug_pub.add(  'T3' , 1 ) ;
2759       END IF;
2760 
2761       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
2762          x_atp_rec.Requested_Arrival_Date(I) :=
2763                                  p_line_rec.request_date;
2764 
2765       ELSE
2766 
2767        IF  p_line_rec.schedule_status_code is not null
2768        AND p_line_rec.ship_model_complete_flag = 'Y'
2769        AND smc_overridden(p_line_rec.top_model_line_id) THEN
2770 
2771            x_atp_rec.Requested_Arrival_Date(I) :=
2772                      p_line_rec.schedule_arrival_date;
2773 
2774        ELSE -- not overridden.
2775 
2776         IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
2777                                 p_old_line_rec.schedule_arrival_date) AND
2778            p_line_rec.schedule_arrival_date IS NOT NULL AND
2779            p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE
2780         THEN
2781            x_atp_rec.Requested_Arrival_Date(I) :=
2782                      p_line_rec.schedule_arrival_date;
2783 
2784         ELSIF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
2785                                  p_old_line_rec.request_date) AND
2786             p_line_rec.request_date IS NOT NULL AND
2787             p_line_rec.request_date <> FND_API.G_MISS_DATE AND
2788             nvl(p_line_rec.override_atp_date_code,'N') = 'N' AND
2789             --Bug 6057897
2790             --Added the below condition to prevent rescheduling based on request date, if scheduling is triggered due to
2791             --some other changes. For example change in Order quantity will trigger scheduling, but request date change
2792             --should not be honoured.
2793             NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
2794         THEN
2795             x_atp_rec.Requested_Arrival_Date(I) :=
2796                        p_line_rec.request_date;
2797         ELSE
2798             x_atp_rec.Requested_Arrival_Date(I) :=
2799              nvl(p_line_rec.schedule_arrival_date,p_line_rec.request_date);
2800         END IF;
2801        END IF; --  overridden
2802       END IF; --atp
2803 
2804       x_atp_rec.Requested_Ship_Date(I)    := null;
2805       IF l_debug_level  > 0 THEN
2806          oe_debug_pub.add(  'REQ ARR DATE : ' ||X_ATP_REC.REQUESTED_ARRIVAL_DATE ( I ) , 3 ) ;
2807       END IF;
2808 
2809     ELSE
2810       -- If user changes schedule_ship_date then schedule based
2811       -- on the ship_date. Otherwise look for the change in request date.
2812       -- If user changed request date, schedule based on the request
2813       -- date. Otherwise if the scheduling is happening because of
2814       -- some other changes, use nvl on schedule_ship and request_dates.
2815 
2816       IF l_debug_level  > 0 THEN
2817           oe_debug_pub.add(  'T4' , 1 ) ;
2818       END IF;
2819       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
2820          x_atp_rec.Requested_Ship_Date(I)    :=
2821                                p_line_rec.request_date;
2822       ELSE
2823        IF  p_line_rec.schedule_status_code is not null
2824        AND p_line_rec.ship_model_complete_flag = 'Y'
2825        AND smc_overridden(p_line_rec.top_model_line_id) THEN
2826 
2827            x_atp_rec.Requested_Ship_Date(I) :=
2828                           p_line_rec.schedule_ship_date;
2829        ELSE -- Not overridden
2830 
2831         IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
2832                                 p_old_line_rec.schedule_ship_date) AND
2833            p_line_rec.schedule_ship_date IS NOT NULL AND
2834            p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE
2835         THEN
2836           x_atp_rec.Requested_Ship_Date(I) :=
2837                           p_line_rec.schedule_ship_date;
2838 
2839         ELSIF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
2840                                    p_old_line_rec.request_date) AND
2841               p_line_rec.request_date IS NOT NULL AND
2842               p_line_rec.request_date <> FND_API.G_MISS_DATE AND
2843               nvl(p_line_rec.override_atp_date_code,'N') = 'N' AND
2844               --Bug 6057897
2845               NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
2846         THEN
2847           x_atp_rec.Requested_Ship_Date(I) :=
2848                         p_line_rec.request_date;
2849         ELSE
2850           x_atp_rec.Requested_Ship_Date(I)    :=
2851            nvl(p_line_rec.schedule_ship_date,p_line_rec.request_date);
2852 
2853         END IF; -- sch ship date changed.
2854        END IF; -- Overridden
2855       END IF; --Atp
2856 
2857       x_atp_rec.Requested_Arrival_Date(I)  := null;
2858                                      IF l_debug_level  > 0 THEN
2859                                          oe_debug_pub.add(  'REQ SHIP DATE : ' || X_ATP_REC.REQUESTED_SHIP_DATE ( I ) , 3 ) ;
2860                                      END IF;
2861 
2862     END IF;
2863 
2864 
2865     IF p_partial_set THEN
2866 
2867       -- If the line is part of a set and we are rescheduling it
2868       -- just by itself, we should not let MRP change the date.
2869       -- Thus we will pass null Latest_Acceptable_Date
2870 
2871       x_atp_rec.Latest_Acceptable_Date(I)  := null;
2872     ELSE
2873 
2874       x_atp_rec.Latest_Acceptable_Date(I)  :=
2875                        p_line_rec.latest_acceptable_date;
2876     END IF;
2877 
2878                             IF l_debug_level  > 0 THEN
2879                                 oe_debug_pub.add(  'LATEST ACCEPTABLE DATE :' || X_ATP_REC.LATEST_ACCEPTABLE_DATE ( I ) , 1 ) ;
2880                             END IF;
2881 
2882     x_atp_rec.Delivery_Lead_Time(I)     := Null;
2883     --x_atp_rec.Delivery_Lead_Time(I)     := p_line_rec.delivery_lead_time;
2884                             IF l_debug_level  > 0 THEN
2885                                 oe_debug_pub.add(  'DELIVERYLEAD TIME :' || X_ATP_REC.DELIVERY_LEAD_TIME ( I ) , 1 ) ;
2886                             END IF;
2887     x_atp_rec.Freight_Carrier(I)        := null;
2888     x_atp_rec.Ship_Method(I)            := p_line_rec.shipping_method_code;
2889     x_atp_rec.Demand_Class(I)           := p_line_rec.demand_class_code;
2890     x_atp_rec.Ship_Set_Name(I)          := l_ship_set;
2891     x_atp_rec.Arrival_Set_Name(I)       := l_arrival_set;
2892     -- 4405004
2893     x_atp_rec.part_of_set(I)            := p_part_of_set;
2894     IF l_debug_level  > 0 THEN
2895        oe_debug_pub.add(  'PART OF SET FLAG :' || X_ATP_REC.part_of_set( I ) , 1 ) ;
2896     END IF;
2897 
2898     IF G_OVERRIDE_FLAG = 'Y' THEN
2899       x_atp_rec.Override_Flag(I)     := 'Y';
2900     ELSE
2901       IF l_action_code <> OESCH_ACT_ATP_CHECK THEN
2902         x_atp_rec.Override_Flag(I)     := p_line_rec.override_atp_date_code;
2903       END IF;
2904       IF l_debug_level  > 0 THEN
2905           oe_debug_pub.add(  'OVERRIDE_FLAG :' || X_ATP_REC.OVERRIDE_FLAG ( I ) , 1 ) ;
2906       END IF;
2907     END IF;
2908 
2909     x_atp_rec.Ship_Date(I)              := null;
2910     x_atp_rec.Available_Quantity(I)     := null;
2911     x_atp_rec.Requested_Date_Quantity(I) := null;
2912     x_atp_rec.Group_Ship_Date(I)        := null;
2913     x_atp_rec.Group_Arrival_Date(I)     := null;
2914     x_atp_rec.Vendor_Id(I)              := null;
2915     x_atp_rec.Vendor_Site_Id(I)         := null;
2916     x_atp_rec.Insert_Flag(I)            := l_insert_flag;
2917     IF l_debug_level  > 0 THEN
2918         oe_debug_pub.add(  'INSERT FLAG IN ATP_REC : '||X_ATP_REC.INSERT_FLAG ( I ) , 3 ) ;
2919     END IF;
2920     x_atp_rec.Error_Code(I)             := null;
2921     x_atp_rec.Message(I)                := null;
2922 
2923 
2924     IF p_line_rec.source_document_type_id = 10 THEN
2925       IF l_debug_level  > 0 THEN
2926           oe_debug_pub.add(  'IT IS AN INTERNAL ORDER ' , 3 ) ;
2927       END IF;
2928       l_oe_flag := 'Y';
2929 
2930       IF (p_line_rec.schedule_ship_date IS NOT NULL AND
2931           p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE ) OR
2932          (p_line_rec.schedule_arrival_date IS NOT NULL AND
2933           p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE ) THEN
2934 
2935           IF l_debug_level  > 0 THEN
2936               oe_debug_pub.add(  'NO CHANGES TO DATE AS IT HAS BEEN PASSED' , 3 ) ;
2937           END IF;
2938       ELSE
2939           IF l_debug_level  > 0 THEN
2940               oe_debug_pub.add(  'PASS THE REQUEST DATE AS ARRIVAL DATE' , 3 ) ;
2941           END IF;
2942 
2943           x_atp_rec.Requested_ship_Date(I)  := null;
2944           x_atp_rec.Requested_arrival_Date(I) := p_line_rec.request_date;
2945 
2946       END IF;
2947 
2948       x_atp_rec.attribute_01(I) := p_line_rec.source_document_id;
2949 
2950     ELSE
2951 
2952       IF l_debug_level  > 0 THEN
2953           oe_debug_pub.add(  'IT IS NOT AN INTERNAL ORDER ' , 3 ) ;
2954       END IF;
2955       l_oe_flag := 'N';
2956 
2957     END IF;
2958 
2959     x_atp_rec.oe_flag(I) := l_oe_flag;
2960     IF l_debug_level  > 0 THEN
2961         oe_debug_pub.add(  'OE FLAG IS : '||X_ATP_REC.OE_FLAG ( I ) , 3 ) ;
2962     END IF;
2963 
2964                              IF l_debug_level  > 0 THEN
2965                                  oe_debug_pub.add(  'REQUEST SHIP DATE : ' || TO_CHAR ( X_ATP_REC.REQUESTED_SHIP_DATE ( I ) , 'DD-MON-RR:HH:MM:SS' ) , 3 ) ;
2966                              END IF;
2967                              IF l_debug_level  > 0 THEN
2968                                  oe_debug_pub.add(  'REQUEST ARRIVAL DATE : ' || TO_CHAR ( X_ATP_REC.REQUESTED_ARRIVAL_DATE ( I ) , 'DD-MON-RR:HH:MM:SS' ) , 3 ) ;
2969                              END IF;
2970 
2971     IF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_ATP_CHECK) THEN
2972       x_atp_rec.Action(I)                    := 100;
2973       IF l_debug_level  > 0 THEN
2974           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
2975       END IF;
2976     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_DEMAND) OR
2977           (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_SCHEDULE)
2978     THEN
2979       x_atp_rec.Action(I)                    := 110;
2980       IF l_debug_level  > 0 THEN
2981           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
2982       END IF;
2983     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_REDEMAND) OR
2984           (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE)
2985     THEN
2986       x_atp_rec.Action(I)                     := 120;
2987       x_atp_rec.Old_Source_Organization_Id(I) :=
2988                           p_old_line_rec.ship_from_org_id;
2989       x_atp_rec.Old_Demand_Class(I)           :=
2990                           p_old_line_rec.demand_class_code;
2991       IF l_debug_level  > 0 THEN
2992           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
2993       END IF;
2994     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_UNDEMAND)
2995     THEN
2996       x_atp_rec.Action(I)                    := 120;
2997       x_atp_rec.Quantity_Ordered(I)          := 0;
2998       x_atp_rec.Old_Source_Organization_Id(I) :=
2999                             p_old_line_rec.ship_from_org_id;
3000       x_atp_rec.Old_Demand_Class(I)           :=
3001                             p_old_line_rec.demand_class_code;
3002 
3003       /*L.G. OPM bug 1828340 jul 19,01*/
3004       IF l_debug_level  > 0 THEN
3005           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
3006       END IF;
3007       -- Bug3361870 (commenting this piece of code. Not required)
3008      /*  IF INV_GMI_RSV_BRANCH.Process_Branch   -- INVCONV - delete this
3009          (p_organization_id => p_old_line_rec.ship_from_org_id)
3010       THEN
3011         Update oe_order_lines_all
3012         Set ordered_quantity = 0,
3013             ordered_quantity2 = 0
3014         Where line_id=p_old_line_rec.line_id;
3015       END IF; */
3016     END IF; -- action=**
3017 
3018     -- Schords (R12 Project #6403)
3019     IF p_line_rec.ship_model_complete_flag = 'N' AND
3020        p_line_rec.top_model_line_id IS NOT NULL AND
3021        (p_line_rec.ato_line_id IS NULL OR
3022         p_line_rec.ato_line_id <> p_line_rec.top_model_line_id) AND
3023        p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_INCLUDED THEN
3024        IF NOT OE_SCH_CONC_REQUESTS.included_processed(p_line_rec.line_id) THEN
3025           IF l_debug_level  > 0 THEN
3026              oe_debug_pub.add(  'INCLUDED PROCESSED : ' || p_line_rec.line_id, 3 ) ;
3027           END IF;
3028           --5166476
3029           OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_line_rec.line_id) := 'Y';
3030           --OE_SCH_CONC_REQUESTS.g_process_records := OE_SCH_CONC_REQUESTS.g_process_records + 1;
3031        END IF;
3032     ELSE
3033        IF OE_SCH_CONC_REQUESTS.g_recorded = 'N' THEN -- 5166476
3034           --5166476
3035           OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_line_rec.line_id) := 'Y';
3036           --OE_SCH_CONC_REQUESTS.g_process_records
3037           --       := OE_SCH_CONC_REQUESTS.g_process_records + 1;
3038        END IF;
3039     END IF;
3040 
3041     -- storing in local var to assing action to ato mandatory components
3042     -- to fix bug 1947539.
3043 
3044     l_action := x_atp_rec.Action(I);
3045     x_atp_rec.atp_lead_time(I)   := 0;
3046     IF p_line_rec.ato_line_id is not null AND
3047        p_line_rec.line_id <> p_line_rec.ato_line_id
3048     THEN
3049 
3050       -- This lines is a ato option or class.
3051       -- Set the atp_lead_time for it.
3052 
3053       IF p_line_rec.ato_line_id = l_st_ato_line_id
3054       THEN
3055         x_atp_rec.atp_lead_time(I)   := l_st_atp_lead_time;
3056         IF l_debug_level  > 0 THEN
3057             oe_debug_pub.add(  'ATO LEAD TIME IS ' || L_ST_ATP_LEAD_TIME , 3 ) ;
3058         END IF;
3059       ELSE
3060         IF l_debug_level  > 0 THEN
3061             oe_debug_pub.add(  'CALLING GET_LEAD_TIME' , 3 ) ;
3062         END IF;
3063         l_st_atp_lead_time :=
3064                      Get_Lead_Time
3065                      (p_ato_line_id      => p_line_rec.ato_line_id,
3066                       p_ship_from_org_id => p_line_rec.ship_from_org_id);
3067 
3068         IF l_debug_level  > 0 THEN
3069             oe_debug_pub.add(  'AFTER CALLING GET_LEAD_TIME' , 3 ) ;
3070         END IF;
3071         IF l_debug_level  > 0 THEN
3072             oe_debug_pub.add(  'LEAD TIME: ' || L_ST_ATP_LEAD_TIME , 3 ) ;
3073         END IF;
3074 
3075         x_atp_rec.atp_lead_time(I)   := l_st_atp_lead_time;
3076         l_st_ato_line_id := p_line_rec.ato_line_id;
3077       END IF;
3078     END IF;
3079 
3080     -- Item Substitution Code.
3081 
3082    -- Sunbstitution will be supported only for
3083    -- Standard not ATO item.
3084    -- Before booking
3085    -- Non internal order item
3086    -- Not on split line
3087 
3088     IF l_debug_level  > 0 THEN
3089         oe_debug_pub.add(  'ITEM_TYPE_CODE :' || P_LINE_REC.ITEM_TYPE_CODE , 1 ) ;
3090     END IF;
3091     IF l_debug_level  > 0 THEN
3092         oe_debug_pub.add(  'ATO_LINE_ID :' || P_LINE_REC.ATO_LINE_ID , 1 ) ;
3093     END IF;
3094     IF l_debug_level  > 0 THEN
3095         oe_debug_pub.add(  'BOOKED_FLAG :' || P_LINE_REC.BOOKED_FLAG , 1 ) ;
3096     END IF;
3097     IF l_debug_level  > 0 THEN
3098         oe_debug_pub.add(  'LINE_SET_ID :' || P_LINE_REC.LINE_SET_ID , 1 ) ;
3099     END IF;
3100     IF l_debug_level  > 0 THEN
3101         oe_debug_pub.add(  'SOURCE_DOCUMENT_TYPE_ID :' || P_LINE_REC.SOURCE_DOCUMENT_TYPE_ID , 1 ) ;
3102     END IF;
3103 
3104     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
3105                             p_old_line_rec.inventory_item_id)
3106     AND p_line_rec.schedule_status_code is NOT NULL THEN
3107 
3108      IF OE_Item_Tbl.EXISTS(p_line_rec.line_id) THEN
3109         x_atp_rec.old_inventory_item_id(I) :=
3110                      OE_Item_Tbl(p_line_rec.line_id).inventory_item_id;
3111         OE_Item_Tbl.DELETE(p_line_rec.line_id);
3112      ELSE
3113         x_atp_rec.old_inventory_item_id(I) :=
3114                                       p_old_line_rec.inventory_item_id;
3115     END IF;
3116      IF l_debug_level  > 0 THEN
3117          oe_debug_pub.add(  'OLD_ITEM ID :' || X_ATP_REC.OLD_INVENTORY_ITEM_ID ( I ) , 1 ) ;
3118      END IF;
3119     END IF;
3120 
3121     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
3122 
3123     --  Bug fix 2331427
3124      IF   p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD
3125      AND  p_line_rec.ato_line_id is null
3126      AND  ( NVL(p_line_rec.booked_flag,'N') = 'N' OR
3127             ( NVL(p_line_rec.booked_flag,'N') = 'Y' and
3128               NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_line_rec.ship_from_org_id) and
3129               NVL(p_line_rec.reserved_quantity, 0) = 0  -- ER 6110708 Do not allow item substitutions for Booked lines if line is reserved
3130             )
3131           ) -- Modified for ER 6110708, allow item substitution for Booked Lines also, but do not allow for OPM after Booking
3132      AND  p_line_rec.line_set_id is null
3133      AND  nvl(p_line_rec.source_document_type_id,-99) <> 10
3134      THEN
3135         --  Set substution code to 1 when OM can allow substitution on th
3136         -- line. For atp check get the values from MRP to show the substitute
3137         -- item details by setting req_item_detail_flag to 1.
3138 
3139         IF l_debug_level  > 0 THEN
3140             oe_debug_pub.add(  'LINE IS MARKED FOR SUBSTITUTION' , 1 ) ;
3141         END IF;
3142          x_atp_rec.substitution_typ_code(I) := 1;
3143          IF l_action_code = OESCH_ACT_ATP_CHECK THEN
3144             x_atp_rec.req_item_detail_flag(I) := 1;
3145          ELSE
3146            x_atp_rec.req_item_detail_flag(I) := 2;
3147          END IF;
3148 
3149      ELSE
3150         IF l_debug_level  > 0 THEN
3151             oe_debug_pub.add(  'LINE IS NOT MARKED FOR SUBSTITUTION' , 1 ) ;
3152         END IF;
3153          x_atp_rec.substitution_typ_code(I) := 4;
3154          x_atp_rec.req_item_detail_flag(I) := 2;
3155 
3156      END IF;
3157 
3158     END IF;
3159 
3160     IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
3161     AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
3162 
3163       IF l_debug_level  > 0 THEN
3164        Oe_debug_pub.add('Do not explode SMC records',2);
3165       END IF;
3166 
3167       x_atp_rec.Included_item_flag(I)  := 1;
3168       x_atp_rec.top_model_line_id(I)   := p_line_rec.top_model_line_id;
3169       x_atp_rec.ato_model_line_id(I)   := p_line_rec.ato_line_id;
3170       x_atp_rec.parent_line_id(I)      := p_line_rec.link_to_line_id;
3171       x_atp_rec.validation_org(I)      :=
3172                   OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
3173       x_atp_rec.component_code(I)      :=  p_line_rec.component_code;
3174       x_atp_rec.component_sequence_id(I) := p_line_rec.component_sequence_id;
3175       x_atp_rec.line_number(I) :=
3176           OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(p_line_rec.line_id);
3177 
3178 
3179       IF(p_line_rec.ato_line_id IS NOT NULL AND
3180       NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
3181            p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
3182                                          OE_GLOBALS.G_ITEM_OPTION))) THEN
3183 
3184          x_atp_rec.config_item_line_id(I) := p_config_line_id ;
3185 
3186       END IF;
3187 
3188     ELSE
3189 
3190     l_inv_ctp :=  fnd_profile.value('INV_CTP');
3191 
3192     IF l_debug_level  > 0 THEN
3193         oe_debug_pub.add(  'INV_CTP : '||L_INV_CTP , 3 ) ;
3194     END IF;
3195 
3196 
3197     IF p_line_rec.ato_line_id = p_line_rec.line_id AND
3198        (p_line_rec.item_type_code in ('MODEL','CLASS') OR
3199        (p_line_rec.item_type_code in ('STANDARD','OPTION') AND
3200         l_inv_ctp = '5')) THEN
3201 
3202        l_explode := TRUE;
3203        -- Added this code to fix bug 1998613.
3204        IF p_line_rec.schedule_status_code is not null
3205           AND nvl(p_line_rec.ordered_quantity,0) <
3206                   p_old_line_rec.ordered_quantity
3207           AND p_old_line_rec.reserved_quantity > 0
3208           AND NOT Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
3209                                              p_old_line_rec => p_old_line_rec)
3210           THEN
3211 
3212             IF l_debug_level  > 0 THEN
3213              oe_debug_pub.add('ONLY ORDERED QTY GOT REDUCED,NO EXPLOSION',3);
3214             END IF;
3215              l_explode := FALSE;
3216 
3217        END IF;
3218        IF l_explode THEN
3219     -- If the line scheduled is an ATO Model, call ATO's API
3220     -- to get the Standard Mandatory Components
3221 
3222         IF l_debug_level  > 0 THEN
3223             oe_debug_pub.add('ATO ITEM TYPE: '||P_LINE_REC.ITEM_TYPE_CODE,3);
3224         END IF;
3225 
3226         IF  p_line_rec.item_type_code = 'STANDARD' AND
3227             x_atp_rec.ship_set_name(I) is NULL THEN
3228 
3229             IF l_debug_level  > 0 THEN
3230                 oe_debug_pub.add(  'ASSIGNING SHIP SET FOR ATO ITEM ' , 3 ) ;
3231             END IF;
3232             x_atp_rec.Ship_Set_Name(I)          := p_line_rec.ato_line_id;
3233 
3234         END IF;
3235 
3236         IF p_line_rec.item_type_code = 'STANDARD' THEN
3237 
3238            IF l_debug_level  > 0 THEN
3239                oe_debug_pub.add(  'ASSIGNING WAREHOUSE AND ITEM ' , 3 ) ;
3240            END IF;
3241            l_organization_id := p_line_rec.ship_from_org_id;
3242            l_inventory_item_id := p_line_rec.inventory_item_id;
3243            IF l_debug_level  > 0 THEN
3244               oe_debug_pub.add('WAREHOUSE/ITEM : '||L_ORGANIZATION_ID||'/' ||L_INVENTORY_ITEM_ID,3 ) ;
3245            END IF;
3246 
3247         ELSE
3248 
3249           l_organization_id := NULL;
3250         l_inventory_item_id := NULL;
3251           IF l_debug_level  > 0 THEN
3252              oe_debug_pub.add('WAREHOUSE/ITEM : '||L_ORGANIZATION_ID||'/' ||L_INVENTORY_ITEM_ID,3);
3253            END IF;
3254 
3255   END IF;
3256 
3257         --Load Model Rec to pass to ATO's API
3258         --Load Model Rec to pass to ATO's API
3259 
3260         l_model_rec.Inventory_Item_Id := MRP_ATP_PUB.number_arr
3261                             (x_atp_rec.Inventory_Item_Id(I));
3262 
3263         l_model_rec.Source_Organization_Id := MRP_ATP_PUB.number_arr
3264                             (x_atp_rec.Source_Organization_Id(I));
3265 
3266         l_model_rec.Identifier := MRP_ATP_PUB.number_arr
3267                             (x_atp_rec.Identifier(I));
3268 
3269         l_model_rec.Calling_Module := MRP_ATP_PUB.number_arr
3270                             (x_atp_rec.Calling_Module(I));
3271 
3272         l_model_rec.Customer_Id := MRP_ATP_PUB.number_arr
3273                             (x_atp_rec.Customer_Id(I));
3274 
3275         l_model_rec.Customer_Site_Id := MRP_ATP_PUB.number_arr
3276                             (x_atp_rec.Customer_Site_Id(I));
3277 
3278         l_model_rec.Destination_Time_Zone := MRP_ATP_PUB.char30_arr
3279                             (x_atp_rec.Destination_Time_Zone(I));
3280 
3281         l_model_rec.Quantity_Ordered := MRP_ATP_PUB.number_arr
3282                             (x_atp_rec.Quantity_Ordered(I));
3283 
3284         l_model_rec.Quantity_UOM := MRP_ATP_PUB.char3_arr
3285                             (x_atp_rec.Quantity_UOM(I));
3286 
3287         l_model_rec.Earliest_Acceptable_Date := MRP_ATP_PUB.date_arr
3288                             (x_atp_rec.Earliest_Acceptable_Date(I));
3289 
3290         l_model_rec.Requested_Ship_Date := MRP_ATP_PUB.date_arr
3291                             (x_atp_rec.Requested_Ship_Date(I));
3292 
3293         l_model_rec.Requested_Arrival_Date := MRP_ATP_PUB.date_arr
3294                             (x_atp_rec.Requested_Arrival_Date(I));
3295 
3296         l_model_rec.Latest_Acceptable_Date := MRP_ATP_PUB.date_arr
3297                             (x_atp_rec.Latest_Acceptable_Date(I));
3298 
3299         l_model_rec.Delivery_Lead_Time := MRP_ATP_PUB.number_arr
3300                             (x_atp_rec.Delivery_Lead_Time(I));
3301 
3302         l_model_rec.Atp_lead_Time := MRP_ATP_PUB.number_arr
3303                             (x_atp_rec.Atp_lead_Time(I));
3304 
3305         l_model_rec.Freight_Carrier := MRP_ATP_PUB.char30_arr
3306                             (x_atp_rec.Freight_Carrier(I));
3307 
3308         l_model_rec.Ship_Method := MRP_ATP_PUB.char30_arr
3309                             (x_atp_rec.Ship_Method(I));
3310 
3311         l_model_rec.Demand_Class := MRP_ATP_PUB.char30_arr
3312                             (x_atp_rec.Demand_Class(I));
3313 
3314         l_model_rec.Ship_Set_Name := MRP_ATP_PUB.char30_arr
3315                             (x_atp_rec.Ship_Set_Name(I));
3316 
3317         l_model_rec.Arrival_Set_Name := MRP_ATP_PUB.char30_arr
3318                             (x_atp_rec.Arrival_Set_Name(I));
3319 
3320         l_model_rec.Override_Flag := MRP_ATP_PUB.char1_arr
3321                             (x_atp_rec.Override_Flag(I));
3322 
3323         l_model_rec.Ship_Date := MRP_ATP_PUB.date_arr
3324                             (x_atp_rec.Ship_Date(I));
3325 
3326         l_model_rec.Available_Quantity := MRP_ATP_PUB.number_arr
3327                             (x_atp_rec.Available_Quantity(I));
3328 
3329         l_model_rec.Requested_Date_Quantity := MRP_ATP_PUB.number_arr
3330                             (x_atp_rec.Requested_Date_Quantity(I));
3331 
3332         l_model_rec.Group_Ship_Date := MRP_ATP_PUB.date_arr
3333                             (x_atp_rec.Group_Ship_Date(I));
3334 
3335         l_model_rec.Group_Arrival_Date := MRP_ATP_PUB.date_arr
3336                             (x_atp_rec.Group_Arrival_Date(I));
3337 
3338         l_model_rec.Vendor_Id := MRP_ATP_PUB.number_arr
3339                             (x_atp_rec.Vendor_Id(I));
3340 
3341         l_model_rec.Vendor_Site_Id := MRP_ATP_PUB.number_arr
3342                             (x_atp_rec.Vendor_Site_Id(I));
3343 
3344         l_model_rec.Insert_Flag := MRP_ATP_PUB.number_arr
3345                             (x_atp_rec.Insert_Flag(I));
3346 
3347         l_model_rec.Error_Code := MRP_ATP_PUB.number_arr
3348                             (x_atp_rec.Error_Code(I));
3349 
3350         l_model_rec.Message := MRP_ATP_PUB.char2000_arr
3351                             (x_atp_rec.Message(I));
3352 
3353         l_model_rec.Action  := MRP_ATP_PUB.number_arr
3354                             (x_atp_rec.action(I));
3355 
3356         l_model_rec.order_number  := MRP_ATP_PUB.number_arr
3357                             (x_atp_rec.order_number(I));
3358 
3359         IF x_atp_rec.Old_Source_Organization_Id.Exists(I) THEN
3360            l_model_rec.Old_Source_Organization_Id := MRP_ATP_PUB.number_arr
3361                                (x_atp_rec.Old_Source_Organization_Id(I));
3362         END IF;
3363 
3364         IF x_atp_rec.Old_Demand_Class.Exists(I) THEN
3365            l_model_rec.Old_Demand_Class  :=
3366                          MRP_ATP_PUB.char30_arr(x_atp_rec.Old_Demand_Class(I));
3367         END IF;
3368 
3369         BEGIN
3370              IF l_debug_level  > 0 THEN
3371                  oe_debug_pub.add(  '2.. CALLING CTO GET_BOM_MANDATORY_COMPS' , 3 ) ;
3372              END IF;
3373 
3374              l_result := CTO_CONFIG_ITEM_PK.GET_MANDATORY_COMPONENTS
3375                          (p_ship_set           => l_model_rec,
3376                           p_organization_id    => l_organization_id,
3377                           p_inventory_item_id  => l_inventory_item_id,
3378                           x_smc_rec            => l_smc_rec,
3379                           xErrorMessage        => lErrorMessage,
3380                           xMessageName         => lMessageName,
3381                           xTableName           => lTableName);
3382 
3383              IF l_debug_level  > 0 THEN
3384                  oe_debug_pub.add(  '2..AFTER CALLING CTO API : ' || L_RESULT , 3 ) ;
3385              END IF;
3386 
3387         EXCEPTION
3388             WHEN OTHERS THEN
3389                 IF l_debug_level  > 0 THEN
3390                     oe_debug_pub.add(  'CTO API RETURNED AN UNEXPECTED ERROR' ) ;
3391                 END IF;
3392                 l_result := 0;
3393         END;
3394 
3395         IF l_result = 1 AND
3396            l_smc_rec.Identifier.count >= 1 THEN
3397                               IF l_debug_level  > 0 THEN
3398                                   oe_debug_pub.add(  'SMC COUNT IS : ' || L_SMC_REC.IDENTIFIER.COUNT , 1 ) ;
3399                               END IF;
3400 
3401            Initialize_mrp_record(p_x_atp_rec => x_atp_rec,
3402                                  l_count   => l_smc_rec.Identifier.count);
3403 
3404            FOR J IN 1..l_smc_rec.Identifier.count LOOP
3405             I := I + 1;
3406             -- Added atp_lead_time, order Number to fix bug 1560461.
3407             x_atp_rec.atp_lead_time(I)   := 0;
3408             x_atp_rec.oe_flag(I) := l_oe_flag;
3409 
3410            -- As part of the bug fix 2910899, OM will indicate and remember the
3411            -- Standard Madatory record positions using vendor_name. This will be
3412            -- used in the load_results procedure to bypass the SMC records.
3413 
3414             x_atp_rec.vendor_name(I) := 'SMC';
3415             IF l_debug_level  > 0 THEN
3416                 oe_debug_pub.add(  'OE FLAG IS : '||X_ATP_REC.OE_FLAG ( I ) , 3 ) ;
3417                  oe_debug_pub.add(  'SMC  : '|| I || ' iden :' ||
3418                                 l_smc_rec.Identifier(J) , 3 ) ;
3419             END IF;
3420 
3421             x_atp_rec.Inventory_Item_Id(I)      := l_smc_rec.Inventory_Item_Id(J);
3422             x_atp_rec.Source_Organization_Id(I) :=
3423                                        l_smc_rec.Source_Organization_Id(J);
3424 
3425             x_atp_rec.Identifier(I)             := l_smc_rec.Identifier(J);
3426             x_atp_rec.Order_Number(I)           := p_order_number;
3427             x_atp_rec.Calling_Module(I)         := l_smc_rec.Calling_Module(J);
3428             x_atp_rec.Customer_Id(I)            := l_smc_rec.Customer_Id(J);
3429             x_atp_rec.Customer_site_Id(I)       := l_smc_rec.Customer_site_Id(J);
3430             x_atp_rec.Destination_Time_Zone(I)  :=
3431                                        l_smc_rec.Destination_Time_Zone(J);
3432             x_atp_rec.Quantity_Ordered(I)       := l_smc_rec.Quantity_Ordered(J);
3433             x_atp_rec.Quantity_UOM(I)           := l_smc_rec.Quantity_UOM(J);
3434             x_atp_rec.Earliest_Acceptable_Date(I) :=
3435                                        l_smc_rec.Earliest_Acceptable_Date(J);
3436             x_atp_rec.Requested_Ship_Date(I)    :=
3437                                        l_smc_rec.Requested_Ship_Date(J);
3438             x_atp_rec.Requested_Arrival_Date(I) :=
3439                                        l_smc_rec.Requested_Arrival_Date(J);
3440             x_atp_rec.Latest_Acceptable_Date(I) :=
3441                                        l_smc_rec.Latest_Acceptable_Date(J);
3442             x_atp_rec.Delivery_Lead_Time(I)     :=
3443                                        l_smc_rec.Delivery_Lead_Time(J);
3444             x_atp_rec.Freight_Carrier(I)        :=
3445                                        l_smc_rec.Freight_Carrier(J);
3446             x_atp_rec.Ship_Method(I)            :=
3447                                        l_smc_rec.Ship_Method(J);
3448             x_atp_rec.Demand_Class(I)           :=
3449                                        l_smc_rec.Demand_Class(J);
3450             x_atp_rec.Ship_Set_Name(I)          :=
3451                                        l_smc_rec.Ship_Set_Name(J);
3452             x_atp_rec.Arrival_Set_Name(I)       :=
3453                                        l_smc_rec.Arrival_Set_Name(J);
3454             x_atp_rec.Override_Flag(I)          :=
3455                                        l_smc_rec.Override_Flag(J);
3456             x_atp_rec.Ship_Date(I)              :=
3457                                        l_smc_rec.Ship_Date(J);
3458             x_atp_rec.Available_Quantity(I)     :=
3459                                        l_smc_rec.Available_Quantity(J);
3460             x_atp_rec.Requested_Date_Quantity(I):=
3461                                        l_smc_rec.Requested_Date_Quantity(J);
3462             x_atp_rec.Group_Ship_Date(I)        :=
3463                                        l_smc_rec.Group_Ship_Date(J);
3464             x_atp_rec.Group_Arrival_Date(I)     :=
3465                                        l_smc_rec.Group_Arrival_Date(J);
3466             x_atp_rec.Vendor_Id(I)              :=
3467                                        l_smc_rec.Vendor_Id(J);
3468             x_atp_rec.Vendor_Site_Id(I)         :=
3469                                        l_smc_rec.Vendor_Site_Id(J);
3470             x_atp_rec.Insert_Flag(I)            :=
3471                                        l_smc_rec.Insert_Flag(J);
3472             x_atp_rec.atp_lead_time(I)           :=
3473                                        l_smc_rec.atp_lead_time(J);
3474             x_atp_rec.Error_Code(I)  := l_smc_rec.Error_Code(J);
3475             x_atp_rec.Message(I)     := l_smc_rec.Message(J);
3476             x_atp_rec.Action(I)      := l_action;
3477 
3478             x_atp_rec.Old_Source_Organization_Id(I) :=
3479                           l_smc_rec.Old_Source_Organization_Id(J);
3480             x_atp_rec.Old_Demand_Class(I)           :=
3481                           l_smc_rec.Old_Demand_Class(J);
3482 
3483             -- Schords (R12 Project #6403)
3484             OE_SCH_CONC_REQUESTS.g_process_records := OE_SCH_CONC_REQUESTS.g_process_records + 1;
3485             IF l_debug_level  > 0 THEN
3486                oe_debug_pub.add(  'INCLUDED TO PROCESSED : ' , 3 ) ;
3487             END IF;
3488 
3489            END LOOP;
3490         END IF; -- Identifier count is greater than 1.
3491        END IF; -- l_explode.
3492     END IF;  -- Need to explode smc.
3493 
3494     END IF; -- Code control
3495     P_index := I;
3496 
3497 
3498 EXCEPTION
3499    WHEN FND_API.G_EXC_ERROR THEN
3500 
3501      Raise FND_API.G_EXC_ERROR;
3502    WHEN OTHERS THEN
3503 
3504         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3505         THEN
3506             OE_MSG_PUB.Add_Exc_Msg
3507             (   G_PKG_NAME,
3508               'Load_MRP_request');
3509         END IF;
3510 
3511 End Load_MRP_request;
3512 
3513 /*--------------------------------------------------------------------
3514 Procedure Name : Load_MRP_request_from_rec
3515 Description    :
3516   This procedure loads the MRP record or tables to be passed
3517   to MRP's API from the OM's table of records of order lines.
3518 
3519   If line line to be passed to MRP is an ATO model, we call
3520   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
3521   components, and we pass them along with the ATO model to MRP.
3522 -------------------------------------------------------------------- */
3523 Procedure Load_MRP_request_from_rec
3524 ( p_line_rec           IN  OE_ORDER_PUB.Line_rec_Type
3525  ,p_old_line_rec       IN  OE_ORDER_PUB.Line_rec_Type
3526  ,p_sch_action         IN  VARCHAR2 := NULL
3527 ,x_mrp_atp_rec OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
3528 
3529 IS
3530   l_mrp_calc_sd       VARCHAR2(240);
3531   l_type_code         VARCHAR2(30);
3532   l_order_number      NUMBER;
3533   l_index             NUMBER := 1;
3534   --
3535   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
3536   --
3537 BEGIN
3538 
3539   l_mrp_calc_sd :=  fnd_profile.value('MRP_ATP_CALC_SD');
3540 
3541   IF l_debug_level  > 0 THEN
3542       oe_debug_pub.add(  'MRP_ATP_CALC_SD : '||L_MRP_CALC_SD , 3 ) ;
3543   END IF;
3544 
3545   l_type_code    := Get_Date_Type(p_line_rec.header_id);
3546   l_order_number := Get_Order_Number(p_line_rec.header_id);
3547 
3548 
3549     IF p_line_rec.item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
3550 
3551        Initialize_mrp_record
3552        (p_x_atp_rec => x_mrp_atp_rec,
3553         l_count     => 1);
3554 
3555 
3556       Load_MRP_request
3557       ( p_line_rec       => p_line_rec
3558        ,p_old_line_rec   => p_old_line_rec
3559        ,p_sch_action     => p_sch_action
3560        ,p_mrp_calc_sd    => l_mrp_calc_sd
3561        ,p_type_code      => l_type_code
3562        ,p_order_number   => l_order_number
3563        ,p_index          => l_index
3564        ,x_atp_rec        => x_mrp_atp_rec);
3565 
3566     END IF;
3567 
3568 
3569 EXCEPTION
3570    WHEN FND_API.G_EXC_ERROR THEN
3571 
3572      Raise FND_API.G_EXC_ERROR;
3573 
3574    WHEN OTHERS THEN
3575 
3576         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3577         THEN
3578             OE_MSG_PUB.Add_Exc_Msg
3579             (   G_PKG_NAME,
3580               'Log_Set_Request');
3581         END IF;
3582 
3583 END Load_MRP_request_from_rec;
3584 
3585 /*--------------------------------------------------------------------
3586 Procedure Name : Load_MRP_request_from_tbl
3587 Description    :
3588   This procedure loads the MRP record or tables to be passed
3589   to MRP's API from the OM's table of records of order lines.
3590 
3591   If line line to be passed to MRP is an ATO model, we call
3592   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
3593   components, and we pass them along with the ATO model to MRP.
3594 
3595 -------------------------------------------------------------------- */
3596 Procedure Load_MRP_request_from_tbl
3597 ( p_line_tbl           IN  OE_ORDER_PUB.Line_Tbl_Type
3598  ,p_old_line_tbl       IN  OE_ORDER_PUB.Line_Tbl_Type
3599  ,p_partial_set        IN  BOOLEAN := FALSE
3600  ,p_sch_action         IN  VARCHAR2 := NULL
3601  ,p_part_of_set        IN  VARCHAR2 DEFAULT 'N' -- 4405004
3602 ,x_mrp_atp_rec OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
3603 
3604 IS
3605   l_mrp_calc_sd       VARCHAR2(240);
3606   l_type_code         VARCHAR2(30);
3607   l_order_number      NUMBER;
3608   l_index             NUMBER := 0;
3609   l_config_count      NUMBER := 0;
3610   l_config_line_id    NUMBER;
3611   l_ato_line_id       NUMBER := -999;
3612 
3613   -- BUG 1955004
3614   l_inactive_demand_count NUMBER:= 0;
3615   l_scheduling_level_code VARCHAR2(30);
3616 
3617   --
3618   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
3619   --
3620 BEGIN
3621 
3622   IF l_debug_level  > 0 THEN
3623       oe_debug_pub.add(  'ENTERING LOAD_MRP_REQUEST_FROM_TBL' , 1 ) ;
3624   END IF;
3625   l_mrp_calc_sd :=  fnd_profile.value('MRP_ATP_CALC_SD');
3626 
3627   IF l_debug_level  > 0 THEN
3628       oe_debug_pub.add(  'MRP_ATP_CALC_SD : '||L_MRP_CALC_SD , 3 ) ;
3629   END IF;
3630 
3631   IF l_debug_level  > 0 THEN
3632       oe_debug_pub.add(  'COUNT OF NEW ' || P_LINE_TBL.COUNT , 1 ) ;
3633   END IF;
3634   IF l_debug_level  > 0 THEN
3635       oe_debug_pub.add(  'COUNT OF NEW ' || P_OLD_LINE_TBL.COUNT , 1 ) ;
3636   END IF;
3637   IF l_debug_level  > 0 THEN
3638       oe_debug_pub.add(  'FIRST ' || P_LINE_TBL.FIRST ) ;
3639   END IF;
3640   IF l_debug_level  > 0 THEN
3641       oe_debug_pub.add(  'LAST ' || P_LINE_TBL.LAST ) ;
3642   END IF;
3643 
3644   l_type_code    := Get_Date_Type(p_line_tbl(1).header_id);
3645   l_order_number := Get_Order_Number(p_line_tbl(1).header_id);
3646 
3647   -- When config line is created on the model, we should not extend the
3648   -- ato table for config line, since we do not pass config line to MRP.
3649 
3650   IF p_sch_action = OESCH_ACT_RESCHEDULE OR
3651      p_sch_action = OESCH_ACT_UNSCHEDULE OR
3652      p_sch_action = OESCH_ACT_ATP_CHECK THEN
3653      FOR  cnt IN 1..p_line_tbl.count LOOP
3654 
3655         IF p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_CONFIG THEN
3656 
3657           l_config_count := l_config_count + 1;
3658 
3659         END IF;
3660 
3661      END LOOP;
3662   END IF;
3663 
3664    --BUG 1955004
3665   IF p_sch_action <> OESCH_ACT_ATP_CHECK THEN
3666   -- loop through the records to identify which need to bypass scheduling
3667 
3668     FOR I in 1..p_line_tbl.count LOOP
3669 
3670       IF l_debug_level  > 0 THEN
3671           oe_debug_pub.add(  'HEADER_ID IS : ' || P_LINE_TBL ( I ) .HEADER_ID ) ;
3672       END IF;
3673       IF l_debug_level  > 0 THEN
3674           oe_debug_pub.add(  'LINE_TYPE_ID IS : ' || P_LINE_TBL ( I ) .LINE_TYPE_ID ) ;
3675       END IF;
3676 
3677       l_scheduling_level_code := Get_Scheduling_Level(p_line_tbl(I).header_id,
3678                                                    P_line_tbl(I).line_type_id);
3679 
3680       IF l_scheduling_level_code IS NULL THEN
3681          l_scheduling_level_code := SCH_LEVEL_THREE;
3682       END IF;
3683       IF l_debug_level  > 0 THEN
3684           oe_debug_pub.add(  'SCHEDULING LEVEL IS : ' || L_SCHEDULING_LEVEL_CODE ) ;
3685       END IF;
3686 
3687       --3763015
3688       IF l_scheduling_level_code = SCH_LEVEL_FOUR OR
3689          l_scheduling_level_code = SCH_LEVEL_FIVE  OR
3690          NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y' THEN
3691 
3692          IF l_debug_level  > 0 THEN
3693              oe_debug_pub.add(  'INACTIVE DEMAND LINE , LINE_ID = ' || P_LINE_TBL.LAST ) ;
3694          END IF;
3695 
3696          OE_inactive_demand_tbl(p_line_tbl(I).line_id).line_id:=
3697                                 P_line_tbl(I).line_id;
3698          OE_inactive_demand_tbl(p_line_tbl(I).line_id).scheduling_level_code:=
3699                                 l_scheduling_level_code;
3700 
3701          IF l_debug_level  > 0 THEN
3702              oe_debug_pub.add(  'INACTIVE DEMAND LINE' ) ;
3703          END IF;
3704                         IF l_debug_level  > 0 THEN
3705                             oe_debug_pub.add(  'LINE_ID = ' || OE_INACTIVE_DEMAND_TBL ( P_LINE_TBL ( I ) .LINE_ID ) .LINE_ID ) ;
3706                         END IF;
3707                         IF l_debug_level  > 0 THEN
3708                             oe_debug_pub.add(  'SCHEDULING_LEVEL_CODE =' || OE_INACTIVE_DEMAND_TBL ( P_LINE_TBL ( I ) .LINE_ID ) .SCHEDULING_LEVEL_CODE ) ;
3709                         END IF;
3710 
3711          l_inactive_demand_count := l_inactive_demand_count + 1;
3712 
3713        END IF;
3714 
3715     END LOOP;
3716   END IF;
3717   --END 1955004
3718 
3719   Initialize_mrp_record
3720   (p_x_atp_rec  => x_mrp_atp_rec,
3721    l_count      => p_line_tbl.count - l_config_count - l_inactive_demand_count);  -- 1955004 added last minus
3722 
3723   FOR  cnt IN 1..p_line_tbl.count LOOP
3724 
3725     IF p_line_tbl(cnt).item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
3726 
3727        --1955004
3728       IF OE_inactive_demand_tbl.EXISTS(p_line_tbl(cnt).line_id) THEN
3729           IF l_debug_level  > 0 THEN
3730               oe_debug_pub.add(  'SKIPPING LOAD_MRP_REQUEST BECAUSE INACTIVE DEMAND LINE' ) ;
3731           END IF;
3732         NULL; -- skip this record
3733       ELSE
3734       --END 1955004
3735 
3736       IF l_config_count > 0
3737       AND p_line_tbl(cnt).ato_line_id is not null
3738       AND p_line_tbl(cnt).ato_line_id <> l_ato_line_id
3739       AND NOT(p_line_tbl(cnt).ato_line_id = p_line_tbl(cnt).line_id
3740           AND (p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_STANDARD
3741           OR   p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_OPTION))
3742       THEN
3743 
3744           l_ato_line_id := p_line_tbl(cnt).ato_line_id;
3745 
3746 
3747           BEGIN
3748 
3749             Select line_id
3750             Into   l_config_line_id
3751             From   oe_order_lines_all
3752             Where  ato_line_id = p_line_tbl(cnt).ato_line_id
3753             And    item_type_code = 'CONFIG';
3754 
3755           EXCEPTION
3756 
3757             WHEN Others THEN
3758 
3759               l_config_line_id := Null;
3760 
3761           END;
3762 
3763       END IF;
3764 
3765       l_index := l_index + 1;
3766 
3767       Load_MRP_request
3768       ( p_line_rec       => p_line_tbl(cnt)
3769        ,p_old_line_rec   => p_old_line_tbl(cnt)
3770        ,p_sch_action     => p_sch_action
3771        ,p_mrp_calc_sd    => l_mrp_calc_sd
3772        ,p_type_code      => l_type_code
3773        ,p_order_number   => l_order_number
3774        ,p_partial_set    => p_partial_set
3775        ,p_config_line_id => l_config_line_id
3776        ,p_part_of_set    => p_part_of_set --4405004
3777        ,p_index          => l_index
3778        ,x_atp_rec        => x_mrp_atp_rec);
3779 
3780       END IF; -- 1955004 check for inactive_demand
3781     END IF;
3782 
3783   END LOOP;
3784 
3785   IF l_debug_level  > 0 THEN
3786       oe_debug_pub.add(  'EXITING LOAD_MRP_REQUEST_FROM_TBL' , 1 ) ;
3787   END IF;
3788 
3789 EXCEPTION
3790    WHEN FND_API.G_EXC_ERROR THEN
3791      Raise FND_API.G_EXC_ERROR;
3792 
3793    WHEN OTHERS THEN
3794 
3795         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3796         THEN
3797             OE_MSG_PUB.Add_Exc_Msg
3798             (   G_PKG_NAME,
3799               'Load_MRP_request_from_tbl');
3800         END IF;
3801 END Load_MRP_request_from_tbl;
3802 
3803 /*--------------------------------------------------------------------------
3804 Procedure Name : Load_Results_from_rec
3805 Description    : This API loads the results from MRP's ATP_REC_TYPE to
3806                  OM's order line. It also populates OM's ATP Table which
3807                  is used to display the ATP results on the client side.
3808                  We ignore the mandatory components which we passed to MRP
3809                  while loading the results.
3810 -------------------------------------------------------------------------- */
3811 Procedure Load_Results_from_rec
3812 ( p_atp_rec         IN  MRP_ATP_PUB.ATP_Rec_Typ
3813 , p_x_line_rec      IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type
3814 , p_index           IN  NUMBER := 1
3815 , p_sch_action      IN  VARCHAR2 := NULL
3816 , p_config_exists   IN  VARCHAR2 := 'N'
3817 , p_partial_set     IN BOOLEAN := FALSE
3818 , x_return_status OUT NOCOPY VARCHAR2)
3819 
3820 IS
3821 J                  NUMBER := p_index;
3822 atp_count          NUMBER := 1;
3823 l_msg_count        NUMBER;
3824 l_msg_data         VARCHAR2(2000);
3825 l_explanation      VARCHAR2(80);
3826 l_type_code        VARCHAR2(30);
3827 l_ship_set_name    VARCHAR2(30);
3828 l_arrival_set_name VARCHAR2(30);
3829 l_arrival_date     DATE := NULL;
3830 l_sch_action       VARCHAR2(30) := p_sch_action;
3831 l_organization_id  NUMBER;
3832 l_inventory_item   VARCHAR2(2000);
3833 l_order_date_type_code VARCHAR2(30);
3834 l_return_status   VARCHAR2(1);  -- Added for IR ISO CMS Project
3835 --
3836 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
3837 --
3838 l_old_ship_from_org_id  number; -- Added for ER 6110708
3839 BEGIN
3840 
3841   IF l_debug_level  > 0 THEN
3842       oe_debug_pub.add(  '2. ENTERING LOAD_RESULTS_FROM_REC' , 1 ) ;
3843   END IF;
3844   x_return_status :=  FND_API.G_RET_STS_SUCCESS;
3845 
3846   IF l_sch_action is NULL THEN
3847      l_sch_action := p_x_line_rec.schedule_action_code;
3848   END IF;
3849 
3850   atp_count := g_atp_tbl.count + 1;
3851   IF l_debug_level  > 0 THEN
3852      oe_debug_pub.add('-----------------Loading MRP Result---------------',1);
3853      oe_debug_pub.add( 'MRP COUNT IS ' || P_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
3854      oe_debug_pub.add(  'SCHEDULE ACTION CODE ' || L_SCH_ACTION , 1 ) ;
3855   END IF;
3856 
3857      -- Check for the MRP data. If MRP is not returning any data, then
3858      -- raise an error.
3859 
3860      IF p_atp_rec.error_code.count = 0 THEN
3861 
3862         IF l_debug_level  > 0 THEN
3863             oe_debug_pub.add(  'MRP HAS RETURNED ANY DATA' , 1 ) ;
3864         END IF;
3865 
3866         FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
3867         OE_MSG_PUB.Add;
3868         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3869 
3870      END IF;
3871      -- 4504197
3872      l_order_date_type_code :=
3873             NVL(Get_Date_Type(p_x_line_rec.header_id), 'SHIP');
3874 
3875      -- 4535580 Start
3876      IF p_atp_rec.error_code(J) <> -99  THEN --5673809
3877       --  p_atp_rec.error_code(J) <> 150  THEN
3878 
3879         -- Populate the inventory_item_id from atp record so that, if there is any
3880         -- substitution, then substitution item will come into inventory_item_id
3881         g_atp_tbl(atp_count).request_item_id     := p_x_line_rec.inventory_item_id;
3882         g_atp_tbl(atp_count).inventory_item_id   := p_atp_rec.inventory_item_id(J);
3883         g_atp_tbl(atp_count).ordered_quantity    := p_x_line_rec.ordered_quantity;
3884         g_atp_tbl(atp_count).order_quantity_uom  := p_x_line_rec.order_quantity_uom;
3885         g_atp_tbl(atp_count).request_date        := p_x_line_rec.request_date;
3886         g_atp_tbl(atp_count).ship_from_org_id    :=
3887                     p_atp_rec.Source_Organization_Id(J);
3888         g_atp_tbl(atp_count).qty_on_request_date :=
3889                     p_atp_rec.Requested_Date_Quantity(J);
3890         g_atp_tbl(atp_count).ordered_qty_Available_Date :=
3891                     p_atp_rec.Ship_Date(J);
3892         g_atp_tbl(atp_count).qty_on_available_date  :=
3893                     p_atp_rec.Available_Quantity(J);
3894         --4504197
3895         IF l_order_date_type_code = 'SHIP' THEN
3896            g_atp_tbl(atp_count).group_available_date  :=
3897                     p_atp_rec.group_ship_date(J);
3898         ELSIF p_atp_rec.group_arrival_date(J) is not null THEN
3899            g_atp_tbl(atp_count).group_available_date  :=
3900                     p_atp_rec.group_arrival_date(J);
3901         END IF;
3902 
3903         -- Display Values
3904         g_atp_tbl(atp_count).line_id         := p_x_line_rec.line_id;
3905         g_atp_tbl(atp_count).header_id       := p_x_line_rec.header_id;
3906         g_atp_tbl(atp_count).line_number     := p_x_line_rec.line_number;
3907         g_atp_tbl(atp_count).shipment_number := p_x_line_rec.shipment_number;
3908         g_atp_tbl(atp_count).option_number   := p_x_line_rec.option_number;
3909         g_atp_tbl(atp_count).component_number := p_x_line_rec.component_number;
3910         g_atp_tbl(atp_count).item_input      := p_x_line_rec.ordered_item;
3911         g_atp_tbl(atp_count).error_message   := l_explanation;
3912         --4772886
3913         g_atp_tbl(atp_count).org_id := p_x_line_rec.org_id;
3914 
3915         IF p_x_line_rec.ship_set_id is not null THEN
3916            BEGIN
3917               SELECT SET_NAME
3918               INTO l_ship_set_name
3919               FROM OE_SETS
3920               WHERE set_id = p_x_line_rec.ship_set_id;
3921            EXCEPTION
3922               WHEN NO_DATA_FOUND THEN
3923                  l_ship_set_name := null;
3924            END;
3925         END IF;
3926         IF p_x_line_rec.arrival_set_id is not null THEN
3927            BEGIN
3928               SELECT SET_NAME
3929               INTO l_arrival_set_name
3930               FROM OE_SETS
3931               WHERE set_id = p_x_line_rec.arrival_set_id;
3932            EXCEPTION
3933               WHEN NO_DATA_FOUND THEN
3934                  l_arrival_set_name := null;
3935            END;
3936         END IF;
3937 
3938         g_atp_tbl(atp_count).ship_set    := l_ship_set_name;
3939         g_atp_tbl(atp_count).arrival_set := l_arrival_set_name;
3940 
3941         IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id  THEN
3942 
3943            IF l_debug_level  > 0 THEN
3944               oe_debug_pub.add(  'SUBSTITUTION OCCURED' , 1 ) ;
3945               oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
3946               oe_debug_pub.add(  'ORIG ITEM :' || P_ATP_REC.REQUEST_ITEM_ID ( J ) , 1 ) ;
3947               oe_debug_pub.add(  'ORIG NAME :' || P_ATP_REC.REQUEST_ITEM_NAME ( J ) , 1 ) ;
3948               oe_debug_pub.add(  'ORIG REQ_ITEM_REQ_DATE_QTY :'|| P_ATP_REC.REQ_ITEM_REQ_DATE_QTY ( J ) , 1 ) ;
3949               oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE_QTY :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
3950               oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
3951 
3952               oe_debug_pub.add(  'SUB QTY_ON_REQUEST_DATE :'|| P_ATP_REC.REQUESTED_DATE_QUANTITY ( J ) , 1 ) ;
3953               oe_debug_pub.add(  'SUB ORDERED_QTY_AVAILABLE_DATE :' || P_ATP_REC.SHIP_DATE ( J ) , 1 ) ;
3954               oe_debug_pub.add(  'SUB QTY_ON_AVAILABLE_DATE :' || P_ATP_REC.AVAILABLE_QUANTITY ( J ) , 1 ) ;
3955            END IF;
3956 
3957               --g_atp_tbl(atp_count).request_item_id
3958                --                    := p_atp_rec.request_item_id(J);
3959            g_atp_tbl(atp_count).Request_item_name
3960                                    := p_atp_rec.request_item_name(J);
3961            g_atp_tbl(atp_count).req_item_req_date_qty
3962                                    := p_atp_rec.req_item_req_date_qty(J);
3963            g_atp_tbl(atp_count).req_item_available_date_qty
3964                                    := p_atp_rec.req_item_available_date_qty(J);
3965            g_atp_tbl(atp_count).req_item_available_date
3966                                    := p_atp_rec.req_item_available_date(J);
3967            g_atp_tbl(atp_count).substitute_flag := 'Y';
3968            g_atp_tbl(atp_count).substitute_item_name
3969                                     := p_atp_rec.inventory_item_name(J);
3970         ELSE
3971            g_atp_tbl(atp_count).substitute_flag := 'N';
3972         END IF;
3973 
3974         l_organization_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
3975            --parameter change made below to fix bug 2819093
3976         OE_ID_TO_VALUE.Ordered_Item
3977             (p_Item_Identifier_type  => p_x_line_rec.item_identifier_type
3978             ,p_inventory_item_id     => p_x_line_rec.inventory_item_id
3979             ,p_organization_id       => l_organization_id
3980             ,p_ordered_item_id       => p_x_line_rec.ordered_item_id
3981             ,p_sold_to_org_id        => p_x_line_rec.sold_to_org_id
3982             ,p_ordered_item          => p_x_line_rec.ordered_item
3983             ,x_ordered_item          => g_atp_tbl(atp_count).Ordered_item_name
3984             ,x_inventory_item        => l_inventory_item);
3985 
3986         OE_ID_TO_VALUE.Ordered_Item
3987             (p_Item_Identifier_type => 'INT'
3988             ,p_inventory_item_id    => p_atp_rec.inventory_item_id(J)
3989             ,p_organization_id      => l_organization_id
3990             ,p_ordered_item_id      => Null
3991             ,p_sold_to_org_id       => Null
3992             ,p_ordered_item         => Null
3993             ,x_ordered_item         => g_atp_tbl(atp_count).Substitute_item_name
3994             ,x_inventory_item       => l_inventory_item);
3995 
3996      END IF;
3997      -- 4535580 End
3998 
3999      IF p_atp_rec.error_code(J) <> 0 AND
4000         l_sch_action <>   OESCH_ACT_ATP_CHECK AND
4001         p_atp_rec.error_code(J) <> -99  AND -- Multi org changes.
4002         p_atp_rec.error_code(J) <> 150 -- to fix bug 1880166
4003 
4004      THEN
4005 
4006         -- Schords (R12 Project #6403)
4007         --5166476
4008         IF OE_SCH_CONC_REQUESTS.g_recorded = 'N' THEN
4009            OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_x_line_rec.line_id) := 'N';
4010            OE_SCH_CONC_REQUESTS.g_recorded :='Y';
4011         END IF;
4012 
4013         IF l_debug_level  > 0 THEN
4014             oe_debug_pub.add(  'ERROR FROM MRP: ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
4015         END IF;
4016         IF p_atp_rec.error_code(J) = 80 THEN
4017 
4018              FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
4019              OE_MSG_PUB.Add;
4020 
4021         ELSE
4022 
4023             IF l_debug_level  > 0 THEN
4024                 oe_debug_pub.add(  'SCHEDULING FAILED' , 1 ) ;
4025             END IF;
4026             IF l_debug_level  > 0 THEN
4027                 oe_debug_pub.add(  P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
4028             END IF;
4029 
4030             OE_MSG_PUB.set_msg_context(
4031              p_entity_code                 => 'LINE'
4032              ,p_entity_id                  => p_x_line_rec.line_id
4033              ,p_header_id                  => p_x_line_rec.header_id
4034              ,p_line_id                    => p_x_line_rec.line_id
4035              ,p_order_source_id            => p_x_line_rec.order_source_id
4036              ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
4037              ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
4038              ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
4039              ,p_change_sequence            => p_x_line_rec.change_sequence
4040              ,p_source_document_type_id    => p_x_line_rec.source_document_type_id
4041              ,p_source_document_id         => p_x_line_rec.source_document_id
4042              ,p_source_document_line_id    => p_x_line_rec.source_document_line_id );
4043 
4044             l_explanation := null;
4045 
4046             select meaning
4047             into l_explanation
4048             from mfg_lookups where
4049             lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
4050             and lookup_code = p_atp_rec.error_code(J) ;
4051             --4535580
4052             g_atp_tbl(atp_count).error_message   := l_explanation;
4053 
4054             IF p_atp_rec.error_code(J) = 19 THEN
4055              -- This error code is given for those lines which are
4056              -- in a group and whose scheduling failed due to some other lines.
4057              -- We do not want to give this out as a message.
4058              null;
4059             ELSIF OESCH_PERFORM_GRP_SCHEDULING = 'N'  THEN
4060 
4061              -- Flag OESCH_PERFORM_GRP_SCHEDULING is set to 'N' when
4062              -- scheduling is called from delayed request to schedule
4063              -- a line being inserted into a set. If there is an error,
4064              -- we will be trying to schedule the whole set again, so
4065              -- we should not display this error message.
4066              null;
4067             -- Commenting Code to fix the Bug-2487471
4068             /*  ELSIF p_atp_rec.Ship_Set_Name(J) is not null OR
4069                   p_atp_rec.Arrival_Set_Name(J) is not null THEN
4070 
4071              -- This line belongs to a scheduling group. We do not want
4072              -- to give out individual messages for each line. We will store
4073              -- them in atp_tbl which can be displayed by the user.
4074              null;
4075            */
4076             ELSIF p_partial_set THEN
4077                -- This call is made by schedule_set_lines procedure.
4078                -- No need to set the message here. Caller will take care
4079                -- of setting the message in this scenario.
4080                Null;
4081             ELSE
4082               IF l_debug_level  > 0 THEN
4083                   oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
4084               END IF;
4085               -- 4558018
4086               IF  p_atp_rec.Ship_Date(J) IS NOT NULL THEN
4087                  FND_MESSAGE.SET_NAME('ONT','ONT_SCH_FAILED_WITH_DATE');
4088                  FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
4089                  FND_MESSAGE.SET_TOKEN('NEXT_DATE',p_atp_rec.Ship_Date(J));
4090 
4091               ELSE
4092                  FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
4093                  FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
4094               END IF;
4095               OE_MSG_PUB.Add;
4096             END IF;
4097         END IF; -- 80
4098 
4099         IF l_debug_level  > 0 THEN
4100             oe_debug_pub.add(  'SETTING ERROR' , 1 ) ;
4101         END IF;
4102         x_return_status := FND_API.G_RET_STS_ERROR;
4103 
4104 
4105 
4106      ELSE
4107 
4108         IF l_debug_level  > 0 THEN
4109             oe_debug_pub.add(  'LOADING ATP RECORD' , 1 ) ;
4110             oe_debug_pub.add(  P_ATP_REC.SOURCE_ORGANIZATION_ID ( 1 ) , 1 ) ;
4111 
4112             oe_debug_pub.add(  'ERROR CODE : ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
4113         END IF;
4114         -- Muti org changes.
4115       IF (p_atp_rec.error_code(J) <> -99 ) THEN
4116 
4117         IF l_debug_level  > 0 THEN
4118             oe_debug_pub.add(  '3. ERROR CODE : ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
4119             oe_debug_pub.add(  '3. J : ' || J , 3 ) ;
4120             oe_debug_pub.add(  '3. IDENTIFIER : ' || P_ATP_REC.IDENTIFIER ( J ) , 1 ) ;
4121             oe_debug_pub.add(  '3. ITEM : ' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
4122         END IF;
4123 
4124          IF l_debug_level  > 0 THEN
4125              oe_debug_pub.add(  '3.REQUEST SHIP DATE :' || TO_CHAR ( P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4126               oe_debug_pub.add(  '3.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 1 ) ;
4127               oe_debug_pub.add(  '3.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4128               oe_debug_pub.add(  '3.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4129               oe_debug_pub.add(  '3.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
4130               oe_debug_pub.add(  '3.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
4131               oe_debug_pub.add(  '3.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 1 ) ;
4132               oe_debug_pub.add(  '3.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 1 ) ;
4133          END IF;
4134 
4135         IF l_sch_action = OESCH_ACT_ATP_CHECK THEN
4136 
4137            l_explanation := null;
4138 
4139            IF (p_atp_rec.error_code(J) <> 0) THEN
4140 
4141               BEGIN
4142                  select meaning
4143                  into l_explanation
4144                  from mfg_lookups where
4145                  lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
4146                  and lookup_code = p_atp_rec.error_code(J) ;
4147 
4148                  g_atp_tbl(atp_count).error_message   := l_explanation;
4149                  IF l_debug_level  > 0 THEN
4150                      oe_debug_pub.add(  'EXPLANATION IS : ' || L_EXPLANATION , 1 ) ;
4151                  END IF;
4152 
4153               /*   IF p_atp_rec.error_code(J) = 150 THEN -- to fix bug 1880166.
4154                     OE_MSG_PUB.add_text(l_explanation);
4155                  END IF;
4156               */ -- 2393433.
4157               EXCEPTION
4158                  WHEN OTHERS THEN
4159                    Null;
4160               END;
4161 
4162            END IF;
4163 
4164            -- Populate the inventory_item_id from atp record so that, if there is any
4165            -- substitution, then substitution item will come into inventory_item_id
4166            g_atp_tbl(atp_count).request_item_id     := p_x_line_rec.inventory_item_id;
4167            g_atp_tbl(atp_count).inventory_item_id   := p_atp_rec.inventory_item_id(J);
4168            g_atp_tbl(atp_count).ordered_quantity    := p_x_line_rec.ordered_quantity;
4169            g_atp_tbl(atp_count).order_quantity_uom  := p_x_line_rec.order_quantity_uom;
4170            g_atp_tbl(atp_count).request_date        := p_x_line_rec.request_date;
4171            g_atp_tbl(atp_count).ship_from_org_id    :=
4172                     p_atp_rec.Source_Organization_Id(J);
4173            g_atp_tbl(atp_count).qty_on_request_date :=
4174                     p_atp_rec.Requested_Date_Quantity(J);
4175            g_atp_tbl(atp_count).ordered_qty_Available_Date :=
4176                     p_atp_rec.Ship_Date(J);
4177            g_atp_tbl(atp_count).qty_on_available_date  :=
4178                     p_atp_rec.Available_Quantity(J);
4179            --4504197
4180            IF l_order_date_type_code = 'SHIP' THEN
4181               g_atp_tbl(atp_count).group_available_date  :=
4182                     p_atp_rec.group_ship_date(J);
4183            ELSIF p_atp_rec.group_arrival_date(J) is not null THEN
4184              g_atp_tbl(atp_count).group_available_date  :=
4185                     p_atp_rec.group_arrival_date(J);
4186            END IF;
4187 
4188            -- Display Values
4189            g_atp_tbl(atp_count).line_id         := p_x_line_rec.line_id;
4190            g_atp_tbl(atp_count).header_id       := p_x_line_rec.header_id;
4191            g_atp_tbl(atp_count).line_number     := p_x_line_rec.line_number;
4192            g_atp_tbl(atp_count).shipment_number := p_x_line_rec.shipment_number;
4193            g_atp_tbl(atp_count).option_number   := p_x_line_rec.option_number;
4194            g_atp_tbl(atp_count).component_number := p_x_line_rec.component_number;
4195            g_atp_tbl(atp_count).item_input      := p_x_line_rec.ordered_item;
4196            g_atp_tbl(atp_count).error_message   := l_explanation;
4197            --4772886
4198            g_atp_tbl(atp_count).org_id  := p_x_line_rec.org_id;
4199 
4200            IF p_x_line_rec.ship_set_id is not null THEN
4201              BEGIN
4202                 SELECT SET_NAME
4203                 INTO l_ship_set_name
4204                 FROM OE_SETS
4205                 WHERE set_id = p_x_line_rec.ship_set_id;
4206              EXCEPTION
4207                 WHEN NO_DATA_FOUND THEN
4208                   l_ship_set_name := null;
4209              END;
4210            END IF;
4211 
4212            IF p_x_line_rec.arrival_set_id is not null THEN
4213              BEGIN
4214                 SELECT SET_NAME
4215                 INTO l_arrival_set_name
4216                 FROM OE_SETS
4217                 WHERE set_id = p_x_line_rec.arrival_set_id;
4218              EXCEPTION
4219                 WHEN NO_DATA_FOUND THEN
4220                   l_arrival_set_name := null;
4221              END;
4222            END IF;
4223 
4224            g_atp_tbl(atp_count).ship_set    := l_ship_set_name;
4225            g_atp_tbl(atp_count).arrival_set := l_arrival_set_name;
4226 
4227            IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id
4228            THEN
4229 
4230               IF l_debug_level  > 0 THEN
4231                   oe_debug_pub.add(  'SUBSTITUTION OCCURED' , 1 ) ;
4232                   oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
4233                   oe_debug_pub.add(  'ORIG ITEM :' || P_ATP_REC.REQUEST_ITEM_ID ( J ) , 1 ) ;
4234                   oe_debug_pub.add(  'ORIG NAME :' || P_ATP_REC.REQUEST_ITEM_NAME ( J ) , 1 ) ;
4235                   oe_debug_pub.add(  'ORIG REQ_ITEM_REQ_DATE_QTY :'|| P_ATP_REC.REQ_ITEM_REQ_DATE_QTY ( J ) , 1 ) ;
4236                   oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE_QTY :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
4237                   oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
4238 
4239                   oe_debug_pub.add(  'SUB QTY_ON_REQUEST_DATE :'|| P_ATP_REC.REQUESTED_DATE_QUANTITY ( J ) , 1 ) ;
4240                   oe_debug_pub.add(  'SUB ORDERED_QTY_AVAILABLE_DATE :' || P_ATP_REC.SHIP_DATE ( J ) , 1 ) ;
4241                   oe_debug_pub.add(  'SUB QTY_ON_AVAILABLE_DATE :' || P_ATP_REC.AVAILABLE_QUANTITY ( J ) , 1 ) ;
4242               END IF;
4243 
4244 
4245               --g_atp_tbl(atp_count).request_item_id
4246                --                    := p_atp_rec.request_item_id(J);
4247               g_atp_tbl(atp_count).Request_item_name
4248                                    := p_atp_rec.request_item_name(J);
4249               g_atp_tbl(atp_count).req_item_req_date_qty
4250                                    := p_atp_rec.req_item_req_date_qty(J);
4251               g_atp_tbl(atp_count).req_item_available_date_qty
4252                                    := p_atp_rec.req_item_available_date_qty(J);
4253               g_atp_tbl(atp_count).req_item_available_date
4254                                    := p_atp_rec.req_item_available_date(J);
4255               g_atp_tbl(atp_count).substitute_flag := 'Y';
4256 
4257               g_atp_tbl(atp_count).substitute_item_name
4258                                       := p_atp_rec.inventory_item_name(J);
4259            ELSE
4260              g_atp_tbl(atp_count).substitute_flag := 'N';
4261            END IF;
4262 
4263            l_organization_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
4264            --parameter change made below to fix bug 2819093
4265            OE_ID_TO_VALUE.Ordered_Item
4266             (p_Item_Identifier_type  => p_x_line_rec.item_identifier_type
4267             ,p_inventory_item_id     => p_x_line_rec.inventory_item_id
4268             ,p_organization_id       => l_organization_id
4269             ,p_ordered_item_id       => p_x_line_rec.ordered_item_id
4270             ,p_sold_to_org_id        => p_x_line_rec.sold_to_org_id
4271             ,p_ordered_item          => p_x_line_rec.ordered_item
4272             ,x_ordered_item          => g_atp_tbl(atp_count).Ordered_item_name
4273             ,x_inventory_item        => l_inventory_item);
4274 
4275            OE_ID_TO_VALUE.Ordered_Item
4276             (p_Item_Identifier_type => 'INT'
4277             ,p_inventory_item_id    => p_atp_rec.inventory_item_id(J)
4278             ,p_organization_id      => l_organization_id
4279             ,p_ordered_item_id      => Null
4280             ,p_sold_to_org_id       => Null
4281             ,p_ordered_item         => Null
4282             ,x_ordered_item         => g_atp_tbl(atp_count).Substitute_item_name
4283             ,x_inventory_item       => l_inventory_item);
4284 
4285 
4286 
4287         END IF; -- Check for ATP.
4288       END IF; --Check for -99.
4289 
4290       IF x_return_status = FND_API.G_RET_STS_SUCCESS AND
4291          l_sch_action <> OESCH_ACT_ATP_CHECK
4292       THEN
4293         -- code fix for 3502139
4294         OE_MSG_PUB.set_msg_context(
4295          p_entity_code                 => 'LINE'
4296          ,p_entity_id                  => p_x_line_rec.line_id
4297          ,p_header_id                  => p_x_line_rec.header_id
4298          ,p_line_id                    => p_x_line_rec.line_id
4299          ,p_order_source_id            => p_x_line_rec.order_source_id
4300          ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
4301          ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
4302          ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
4303          ,p_change_sequence            => p_x_line_rec.change_sequence
4304          ,p_source_document_type_id    => p_x_line_rec.source_document_type_id
4305          ,p_source_document_id         => p_x_line_rec.source_document_id
4306          ,p_source_document_line_id    => p_x_line_rec.source_document_line_id );
4307         -- code fix for 3502139
4308 
4309         IF l_sch_action = OESCH_ACT_DEMAND
4310         OR l_sch_action = OESCH_ACT_SCHEDULE
4311         THEN
4312           IF l_debug_level  > 0 THEN
4313               oe_debug_pub.add(  'LOADING RESULTS OF SCHEDULE' , 1 ) ;
4314               oe_debug_pub.add(  '1.REQUEST SHIP DATE :' || TO_CHAR ( P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4315              oe_debug_pub.add(  '1.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 1 ) ;
4316              oe_debug_pub.add(  '1.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4317              oe_debug_pub.add(  '1.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
4318              oe_debug_pub.add(  '1.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
4319              oe_debug_pub.add(  '1.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 1 ) ;
4320              oe_debug_pub.add(  '1.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 1 ) ;
4321           END IF;
4322 
4323           l_old_ship_from_org_id := p_x_line_rec.ship_from_org_id; -- Added for ER 6110708
4324 
4325           p_x_line_rec.ship_from_org_id      :=
4326                                 p_atp_rec.Source_Organization_Id(J);
4327 
4328           -- If the item subtitution occurs on the line then populate
4329           -- new inventory item from atp record and also populate
4330           -- Original inventory items.
4331 
4332           IF p_atp_rec.inventory_item_id(J)  <>  p_x_line_rec.inventory_item_id THEN
4333 
4334              IF l_debug_level  > 0 THEN
4335                  oe_debug_pub.add(  'SCH: ITEM HAS BEEN SUBSTITUTED' , 1 ) ;
4336              END IF;
4337              IF l_debug_level  > 0 THEN
4338                  oe_debug_pub.add(  'NEW INVENTORY ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
4339              END IF;
4340 
4341              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ITEM_CHANGE');
4342              FND_MESSAGE.SET_TOKEN('OLDITEM',p_x_line_rec.ordered_item);
4343              FND_MESSAGE.SET_TOKEN('NEWITEM', p_atp_rec.inventory_item_name(J));
4344              OE_MSG_PUB.Add;
4345 
4346              -- Added below call for ER 6110708
4347              IF nvl(p_x_line_rec.booked_flag, 'N') = 'Y' THEN
4348                VALIDATE_ITEM_SUBSTITUTION
4349                (
4350                  p_new_inventory_item_id => p_atp_rec.inventory_item_id(J),
4351                  p_old_inventory_item_id => p_x_line_rec.inventory_item_id,
4352                  p_old_ship_from_org_id => l_old_ship_from_org_id,
4353                  p_new_ship_from_org_id => p_atp_rec.Source_Organization_Id(J),
4354                  p_old_shippable_flag => p_x_line_rec.shippable_flag
4355                );
4356              END IF;
4357 
4358              IF  p_x_line_rec.Original_Inventory_Item_Id is null
4359              THEN
4360                 p_x_line_rec.Original_Inventory_Item_Id
4361                              := p_x_line_rec.Inventory_Item_id;
4362                 p_x_line_rec.Original_item_identifier_Type
4363                              := p_x_line_rec.item_identifier_type;
4364                 p_x_line_rec.Original_ordered_item_id
4365                              := p_x_line_rec.ordered_item_id;
4366                 p_x_line_rec.Original_ordered_item
4367                              := p_x_line_rec.ordered_item;
4368 
4369              END IF;
4370              IF l_debug_level  > 0 THEN
4371                  oe_debug_pub.add(  'ORIGINAL ITEM :' || P_X_LINE_REC.INVENTORY_ITEM_ID , 2 ) ;
4372              END IF;
4373              IF l_debug_level  > 0 THEN
4374                  oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 2 ) ;
4375              END IF;
4376              p_x_line_rec.inventory_item_id
4377                              := p_atp_rec.inventory_item_id(J);
4378              p_x_line_rec.item_identifier_type := 'INT';
4379 
4380              -- This variable is to track that the Item is being Substituted by Scheduling and not being changed manully by user.
4381              OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y';  -- Added for ER 6110708.
4382           END IF; -- inv changed.
4383 
4384           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
4385 
4386 
4387              IF p_atp_rec.group_ship_date(J) IS NOT NULL
4388              THEN
4389                 p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
4390              ELSE
4391                 p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
4392              END IF;
4393 
4394              IF p_atp_rec.group_arrival_date(J) IS NOT NULL THEN
4395                 p_x_line_rec.schedule_arrival_date := p_atp_rec.group_arrival_date(J);
4396              ELSE
4397                 p_x_line_rec.schedule_arrival_date := p_atp_rec.arrival_date(J);
4398 
4399              END IF;
4400 
4401           ELSE
4402             p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
4403 
4404             p_x_line_rec.schedule_arrival_date  :=
4405                                     p_atp_rec.ship_date(J) +
4406                                     nvl(p_atp_rec.delivery_lead_time(J),0);
4407 
4408             IF p_atp_rec.group_arrival_date(J) IS NOT NULL
4409             THEN
4410               p_x_line_rec.schedule_arrival_date :=
4411                                       p_atp_rec.group_arrival_date(J);
4412               p_x_line_rec.schedule_ship_date :=
4413                   p_x_line_rec.schedule_arrival_date -
4414                   nvl(p_atp_rec.delivery_lead_time(J),0);
4415 
4416             END IF;
4417 
4418             IF p_atp_rec.group_ship_date(J) IS NOT NULL
4419             THEN
4420               p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
4421               p_x_line_rec.schedule_arrival_date  :=
4422                                       p_x_line_rec.schedule_ship_date +
4423                                       nvl(p_atp_rec.delivery_lead_time(J),0);
4424 
4425             END IF;
4426           END IF;
4427           IF p_atp_rec.ship_method(J) IS NOT NULL THEN
4428              p_x_line_rec.shipping_method_code  := p_atp_rec.ship_method(J);
4429           END IF;
4430 
4431           p_x_line_rec.delivery_lead_time  := p_atp_rec.delivery_lead_time(J);
4432           p_x_line_rec.mfg_lead_time       := p_atp_rec.atp_lead_time(J);
4433           p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
4434 
4435           IF l_debug_level  > 0 THEN
4436               oe_debug_pub.add(  'BEFORE ATTRIBUTE 05' , 5 ) ;
4437           END IF;
4438 
4439         -- bug fix 1965182/1925326
4440           IF p_atp_rec.attribute_05.COUNT > 0 THEN
4441              IF p_atp_rec.attribute_05(J) IS NULL THEN
4442                 IF p_config_exists = 'N' THEN
4443                    p_x_line_rec.visible_demand_flag   := 'Y';
4444                 ELSE
4445                    IF l_debug_level  > 0 THEN
4446                        oe_debug_pub.add(  'INSIDE CONFIG EXISTS' , 3 ) ;
4447                    END IF;
4448                 END IF;
4449 
4450              ELSIF p_atp_rec.attribute_05(J) = 'N' THEN
4451                p_x_line_rec.visible_demand_flag   := 'N';
4452              ELSIF p_atp_rec.attribute_05(J) = 'Y' THEN
4453                p_x_line_rec.visible_demand_flag   := 'Y';
4454              END IF;
4455           ELSE
4456              IF p_config_exists = 'N' THEN
4457               p_x_line_rec.visible_demand_flag   := 'Y';
4458              ELSE
4459               IF l_debug_level  > 0 THEN
4460                   oe_debug_pub.add(  'CONFIG EXISTS' , 3 ) ;
4461               END IF;
4462              END IF;
4463           END IF;
4464           IF l_debug_level  > 0 THEN
4465               oe_debug_pub.add(  'AFTER ATTRIBUTE 05' , 5 ) ;
4466           END IF;
4467 
4468           -- We had set the ship_set and arrival_set (which are value
4469           -- fields) to the set name values for calling MRP purpose.
4470           -- Setting these back to null since sets defaulting logic
4471           -- gets fired if these values are populated.
4472 
4473           IF  p_x_line_rec.ship_set_id IS NOT NULL
4474           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
4475              p_x_line_rec.ship_set     := null;
4476           END IF;
4477 
4478           IF  p_x_line_rec.arrival_set_id IS NOT NULL
4479           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
4480              p_x_line_rec.arrival_set  := null;
4481           END IF;
4482 
4483           -- Pack J
4484           -- Promise Date setup with Schedule date
4485           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
4486              -- Check for LAD violation
4487              Validate_with_LAD
4488                 ( p_header_id              => p_x_line_rec.header_id
4489                  ,p_latest_acceptable_date => p_x_line_rec.latest_acceptable_date
4490                  ,p_schedule_ship_date     => p_x_line_rec.schedule_ship_date
4491                  ,p_schedule_arrival_date  => p_x_line_rec.schedule_arrival_date);
4492              -- Promise Date setup
4493              Promise_Date_for_Sch_Action
4494                   (p_x_line_rec => p_x_line_rec
4495                   ,p_sch_action => l_sch_action
4496                   ,P_header_id  => p_x_line_rec.header_id);
4497 
4498              -- Firm Demand Flag.
4499              IF  nvl(p_x_line_rec.firm_demand_flag,'N') = 'N'
4500              AND Oe_Sys_Parameters.Value('FIRM_DEMAND_EVENTS') = 'SCHEDULE' THEN
4501                  p_x_line_rec.firm_demand_flag := 'Y';
4502 
4503              END IF;
4504           END IF;
4505 
4506           -- Adding code to trap if mrp is returning success and not
4507           -- returning correct data to OM.
4508           IF p_x_line_rec.schedule_ship_date is null
4509           OR p_x_line_rec.schedule_arrival_date is null THEN
4510 
4511              IF l_debug_level  > 0 THEN
4512                  oe_debug_pub.add(  'SCH: MRP HAS NOT RETURNING VALID SHIP DATE' , 2 ) ;
4513                  oe_debug_pub.add(  'SCH: Shedule ship date ' ||
4514                                                p_x_line_rec.schedule_ship_date , 2 ) ;
4515                  oe_debug_pub.add(  'SCH: Schedule Arr Date' ||
4516                                                p_x_line_rec.schedule_arrival_date , 2 ) ;
4517              END IF;
4518              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
4519              OE_MSG_PUB.Add;
4520              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4521           END IF;
4522 
4523         ELSIF l_sch_action = OESCH_ACT_REDEMAND OR
4524               l_sch_action = OESCH_ACT_RESCHEDULE
4525         THEN
4526           IF l_debug_level  > 0 THEN
4527              oe_debug_pub.add(  'LOAD THE RESULT OF RESCHEDULE' , 3 ) ;
4528              oe_debug_pub.add(  '2.REQUEST SHIP DATE :' || P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 3 ) ;
4529              oe_debug_pub.add(  '2.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 3 ) ;
4530              oe_debug_pub.add(  '2.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 3 ) ;
4531              oe_debug_pub.add(  '2.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 3 ) ;
4532              oe_debug_pub.add(  '2.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 3 ) ;
4533              oe_debug_pub.add(  '2.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 3 ) ;
4534              oe_debug_pub.add(  '2.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 3 ) ;
4535           END IF;
4536 
4537           l_old_ship_from_org_id := p_x_line_rec.ship_from_org_id; -- Added for ER 6110708
4538 
4539           p_x_line_rec.ship_from_org_id :=
4540                                   p_atp_rec.Source_Organization_Id(J);
4541 
4542 
4543           -- If the item subtitution occurs on the line then populate
4544           -- new inventory item from atp record and also populate
4545           -- Original inventory items.
4546 
4547           IF p_atp_rec.inventory_item_id(J)  <>  p_x_line_rec.inventory_item_id
4548           THEN
4549 
4550              IF l_debug_level  > 0 THEN
4551                  oe_debug_pub.add(  'RSCH: ITEM HAS BEEN SUBSTITUTED' , 1 ) ;
4552                  oe_debug_pub.add(  'RSCH NEW INVENTORY ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
4553              END IF;
4554 
4555              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ITEM_CHANGE');
4556              FND_MESSAGE.SET_TOKEN('OLDITEM',p_x_line_rec.ordered_item);
4557              FND_MESSAGE.SET_TOKEN('NEWITEM', p_atp_rec.inventory_item_name(J));
4558              OE_MSG_PUB.Add;
4559 
4560              -- Added below call for ER 6110708
4561              IF nvl(p_x_line_rec.booked_flag, 'N') = 'Y' THEN
4562                VALIDATE_ITEM_SUBSTITUTION
4563                (
4564                  p_new_inventory_item_id => p_atp_rec.inventory_item_id(J),
4565                  p_old_inventory_item_id => p_x_line_rec.inventory_item_id,
4566                  p_old_ship_from_org_id => l_old_ship_from_org_id,
4567                  p_new_ship_from_org_id => p_atp_rec.Source_Organization_Id(J),
4568                  p_old_shippable_flag => p_x_line_rec.shippable_flag
4569                );
4570              END IF;
4571 
4572              IF  p_x_line_rec.Original_Inventory_Item_Id is null
4573              THEN
4574                 p_x_line_rec.Original_Inventory_Item_Id
4575                              := p_x_line_rec.Inventory_Item_id;
4576                 p_x_line_rec.Original_item_identifier_Type
4577                              := p_x_line_rec.item_identifier_type;
4578                 p_x_line_rec.Original_ordered_item_id
4579                              := p_x_line_rec.ordered_item_id;
4580                 p_x_line_rec.Original_ordered_item
4581                              := p_x_line_rec.ordered_item;
4582 
4583              END IF;
4584              p_x_line_rec.inventory_item_id
4585                              := p_atp_rec.inventory_item_id(J);
4586              p_x_line_rec.item_identifier_type := 'INT';
4587 
4588              -- This variable is to track that the Item is being Substituted by Scheduling and not being changed manully by user.
4589              OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y';  -- Added for ER 6110708.
4590           END IF; -- inv changed.
4591 
4592 
4593           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
4594 
4595 
4596              IF p_atp_rec.group_ship_date(J) IS NOT NULL
4597              THEN
4598                 p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
4599              ELSE
4600                 p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
4601              END IF;
4602 
4603              IF p_atp_rec.group_arrival_date(J) IS NOT NULL THEN
4604                 p_x_line_rec.schedule_arrival_date := p_atp_rec.group_arrival_date(J);
4605              ELSE
4606                 p_x_line_rec.schedule_arrival_date := p_atp_rec.arrival_date(J);
4607 
4608              END IF;
4609           ELSE
4610              p_x_line_rec.schedule_ship_date := p_atp_rec.ship_date(J);
4611 
4612              p_x_line_rec.schedule_arrival_date  :=
4613                                   p_atp_rec.ship_date(J) +
4614                                   nvl(p_atp_rec.delivery_lead_time(J),0);
4615 
4616 
4617              IF p_atp_rec.group_arrival_date(J) IS NOT NULL
4618              THEN
4619                p_x_line_rec.schedule_arrival_date :=
4620                                        p_atp_rec.group_arrival_date(J);
4621                p_x_line_rec.schedule_ship_date :=
4622                    p_x_line_rec.schedule_arrival_date -
4623                    nvl(p_atp_rec.delivery_lead_time(J),0);
4624 
4625              END IF;
4626 
4627              IF p_atp_rec.group_ship_date(J) IS NOT NULL
4628              THEN
4629                p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
4630                p_x_line_rec.schedule_arrival_date  :=
4631                                     p_x_line_rec.schedule_ship_date +
4632                                     nvl(p_atp_rec.delivery_lead_time(J),0);
4633 
4634              END IF;
4635 
4636           END IF;
4637 
4638           IF p_atp_rec.ship_method(J) IS NOT NULL THEN
4639              p_x_line_rec.shipping_method_code  := p_atp_rec.ship_method(J);
4640           END IF;
4641 
4642           p_x_line_rec.delivery_lead_time  := p_atp_rec.delivery_lead_time(J);
4643           p_x_line_rec.mfg_lead_time       := p_atp_rec.atp_lead_time(J);
4644 
4645           -- When a new option is added to scheduled SMC/SET OM will
4646           -- call MRP with action re-schedule. So, for the new line we need to
4647           -- assign the following values.
4648 
4649           -- Status assignment will be done after promise date setup call
4650           --p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
4651 
4652           IF l_debug_level  > 0 THEN
4653               oe_debug_pub.add(  'RSCH BEFORE ATTRIBUTE 05' , 5 ) ;
4654           END IF;
4655         -- bug fix 1965182/1925326
4656           IF p_atp_rec.attribute_05.COUNT > 0 THEN
4657              IF p_atp_rec.attribute_05(J) IS NULL THEN
4658                 IF p_config_exists = 'N' THEN
4659                    p_x_line_rec.visible_demand_flag   := 'Y';
4660                 ELSE
4661                    IF l_debug_level  > 0 THEN
4662                        oe_debug_pub.add(  'INSIDE CONFIG EXISTS' , 3 ) ;
4663                    END IF;
4664                 END IF;
4665 
4666              ELSIF p_atp_rec.attribute_05(J) = 'N' THEN
4667                p_x_line_rec.visible_demand_flag   := 'N';
4668              ELSIF p_atp_rec.attribute_05(J) = 'Y' THEN
4669                p_x_line_rec.visible_demand_flag   := 'Y';
4670              END IF;
4671           ELSE
4672              IF p_config_exists = 'N' THEN
4673               p_x_line_rec.visible_demand_flag   := 'Y';
4674              ELSE
4675               IF l_debug_level  > 0 THEN
4676                   oe_debug_pub.add(  'CONFIG EXISTS' , 3 ) ;
4677               END IF;
4678              END IF;
4679           END IF;
4680           IF l_debug_level  > 0 THEN
4681               oe_debug_pub.add(  'RSCH AFTER ATTRIBUTE 05' , 5 ) ;
4682           END IF;
4683 
4684           IF (p_x_line_rec.ordered_quantity = 0)
4685           THEN
4686             -- Bug 2101332. On a cancelled line, keeping
4687             -- warehouse is not a harm.
4688             -- p_x_line_rec.ship_from_org_id   := null;
4689             p_x_line_rec.schedule_ship_date    := null;
4690             p_x_line_rec.schedule_arrival_date := null;
4691             p_x_line_rec.schedule_status_code  := null;
4692             p_x_line_rec.visible_demand_flag   := 'N';
4693             p_x_line_rec.override_atp_date_code := null;
4694           END IF;
4695 
4696           -- We had set the ship_set and arrival_set (which are value
4697           -- fields) to the set name values for calling MRP purpose.
4698           -- Setting these back to null since sets defaulting logic
4699           -- gets fired if these values are populated.
4700 
4701           IF  p_x_line_rec.ship_set_id IS NOT NULL
4702           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
4703               p_x_line_rec.ship_set     := null;
4704           END IF;
4705 
4706           IF  p_x_line_rec.arrival_set_id IS NOT NULL
4707           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
4708               p_x_line_rec.arrival_set  := null;
4709           END IF;
4710 
4711           IF p_x_line_rec.top_model_line_id = p_x_line_rec.line_id THEN
4712 
4713                            IF l_debug_level  > 0 THEN
4714                                oe_debug_pub.add(  'STORE ARRIVAL_DATE ' || P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 2 ) ;
4715                            END IF;
4716              l_arrival_date := p_x_line_rec.schedule_arrival_date;
4717 
4718           END IF;
4719           -- Pack J
4720           -- Promise Date setup with Schedule date
4721           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
4722              -- Check for LAD violation
4723              Validate_with_LAD
4724                 ( p_header_id              => p_x_line_rec.header_id
4725                  ,p_latest_acceptable_date => p_x_line_rec.latest_acceptable_date
4726                  ,p_schedule_ship_date     => p_x_line_rec.schedule_ship_date
4727                  ,p_schedule_arrival_date  => p_x_line_rec.schedule_arrival_date);
4728              -- Promise date setup
4729              Promise_Date_for_Sch_Action
4730                   (p_x_line_rec => p_x_line_rec
4731                   ,p_sch_action => l_sch_action
4732                   ,P_header_id  => p_x_line_rec.header_id);
4733 
4734              -- Firm Demand Flag.
4735              IF  nvl(p_x_line_rec.firm_demand_flag,'N') = 'N'
4736              AND Oe_Sys_Parameters.Value('FIRM_DEMAND_EVENTS') = 'SCHEDULE' THEN
4737                  p_x_line_rec.firm_demand_flag := 'Y';
4738 
4739              END IF;
4740           END IF;
4741           IF p_x_line_rec.ordered_quantity > 0 THEN
4742              p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
4743           END IF;
4744 
4745           IF l_debug_level  > 0 THEN
4746              oe_debug_pub.add(  'RSCH: PROMISE DATE '||p_x_line_rec.promise_date , 2 ) ;
4747           END IF;
4748 
4749 
4750           -- Adding code to trap if mrp is returning success and not
4751           -- returning correct data to OM.
4752           IF (p_x_line_rec.schedule_ship_date is null
4753           OR  p_x_line_rec.schedule_arrival_date is null)
4754           AND p_x_line_rec.ordered_quantity > 0 THEN
4755 
4756              IF l_debug_level  > 0 THEN
4757                  oe_debug_pub.add(  'RSCH: MRP HAS NOT RETURNING VALID DATE' , 2 ) ;
4758              END IF;
4759              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
4760              OE_MSG_PUB.Add;
4761              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4762           END IF;
4763 
4764         ELSIF l_sch_action = OESCH_ACT_UNDEMAND
4765         THEN
4766           IF l_debug_level  > 0 THEN
4767              oe_debug_pub.add(  'RR2:Load the results of undemand: '
4768                                             || P_X_LINE_REC.RE_SOURCE_FLAG , 1 ) ;
4769           END IF;
4770           --bug 2921202
4771           if p_x_line_rec.ordered_quantity > 0 then
4772            IF p_x_line_rec.re_source_flag='Y' or
4773              p_x_line_rec.re_source_flag is null THEN
4774                IF l_debug_level  > 0 THEN
4775                    oe_debug_pub.add(  'SETTING SHIP FROM TO NULL' , 1 ) ;
4776                END IF;
4777                p_x_line_rec.ship_from_org_id      := null;
4778            END IF;
4779           end if;
4780           p_x_line_rec.schedule_ship_date    := null;
4781           p_x_line_rec.schedule_arrival_date := null;
4782           p_x_line_rec.schedule_status_code  := null;
4783           p_x_line_rec.visible_demand_flag   := null;
4784 
4785           -- We had set the ship_set and arrival_set (which are value
4786           -- fields) to the set name values for calling MRP purpose.
4787           -- Setting these back to null since sets defaulting logic
4788           -- gets fired if these values are populated.
4789 
4790           IF  p_x_line_rec.ship_set_id IS NOT NULL
4791           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
4792               p_x_line_rec.ship_set     := null;
4793           END IF;
4794 
4795           IF  p_x_line_rec.arrival_set_id IS NOT NULL
4796           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
4797               p_x_line_rec.arrival_set  := null;
4798           END IF;
4799 
4800           -- BUG 1282873
4801           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
4802 
4803             -- Unscheduling a line will also clear the Override Atp flag
4804              p_x_line_rec.override_atp_date_code := Null;
4805           END IF;
4806           -- END 1282873
4807           -- 3345776
4808           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
4809              -- Promise date setup
4810              Promise_Date_for_Sch_Action
4811                   (p_x_line_rec => p_x_line_rec
4812                   ,p_sch_action => l_sch_action
4813                   ,P_header_id  => p_x_line_rec.header_id);
4814           END IF;
4815 
4816         END IF;
4817 
4818 /*** Moved the below code for bug #8205242 from procedure Load_Results_From_Tbl
4819      to procedure Load_Results_from_Rec ***/
4820 
4821 /* 7576948: IR ISO Change Management Begins */
4822 
4823 -- This code is hooked for IR ISO project, where if the schedule ship
4824 -- date is changed as a result of MRP call then a delayed request is
4825 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
4826 -- API from Purchasing, responsible for conditionally updating the Need By
4827 -- Date column in internal requisition line. This will be done based on
4828 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
4829 
4830 -- For details on IR ISO CMS project, please refer to FOL >
4831 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
4832 
4833     IF l_debug_level > 0 THEN
4834       oe_debug_pub.add(' Source Type for the order line is : '||p_x_line_rec.order_source_id,5);
4835       oe_debug_pub.add(' Line_id : '||p_x_line_rec.line_id,5);
4836       oe_debug_pub.add(' Header_id : '||p_x_line_rec.header_id,5);
4837       oe_debug_pub.add(' Source Document id : '||p_x_line_rec.source_document_id,5);
4838       oe_debug_pub.add(' Src Doc Line_id : '||p_x_line_rec.source_document_line_id,5);
4839       oe_debug_pub.add(' Schedule Arrival Date : '||p_x_line_rec.schedule_arrival_date,5);
4840     END IF;
4841 
4842     IF p_x_line_rec.order_source_id = 10 THEN
4843        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
4844          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
4845          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
4846          -- Modified for IR ISO Tracking bug 7667702
4847 
4848          IF l_debug_level > 0 THEN
4849            oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change',5);
4850          END IF;
4851 
4852          -- Log a delayed request to update the change in Schedule Ship Date to
4853          -- Requisition Line. This request will be logged only if the change is
4854          -- not initiated from Requesting Organization, and it is not a case of
4855          -- Internal Sales Order Full Cancellation. It will even not be logged
4856          -- Purchasing profile option does not allow update of Need By Date when
4857          -- Schedule Ship Date changes on internal sales order line
4858 
4859          OE_delayed_requests_Pvt.log_request
4860          ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
4861          , p_entity_id              => p_x_line_rec.line_id
4862          , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
4863          , p_requesting_entity_id   => p_x_line_rec.line_id
4864          , p_request_unique_key1    => p_x_line_rec.header_id  -- Order Hdr_id
4865          , p_request_unique_key2    => p_x_line_rec.source_document_id -- Req Hdr_id
4866          , p_request_unique_key3    => p_x_line_rec.source_document_line_id -- Req Line_id
4867          , p_date_param1            => p_x_line_rec.schedule_arrival_date -- schedule_ship_date
4868 -- Note: p_date_param1 is used for both Schedule_Ship_Date and
4869 -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
4870 -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
4871 -- it can expect change with respect to Ship or Arrival date. Thus, will
4872 -- not raise any issues.
4873          , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
4874          , x_return_status          => l_return_status
4875          );
4876 
4877          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4878            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4879          ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
4880            RAISE FND_API.G_EXC_ERROR;
4881          END IF;
4882 
4883          ELSE -- Added for IR ISO Tracking bug 7667702
4884            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
4885              IF l_debug_level > 0 THEN
4886                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
4887              END IF;
4888 
4889              OE_SCHEDULE_UTIL.Update_PO(p_x_line_rec.schedule_arrival_date,
4890                 p_x_line_rec.source_document_id,
4891                 p_x_line_rec.source_document_line_id);
4892            END IF;
4893          END IF;
4894 
4895        END IF;
4896      END IF; -- Order_Source_id
4897 
4898 /* ============================= */
4899 /* IR ISO Change Management Ends */
4900 
4901 
4902       END IF; -- Return Status.
4903      END IF; -- Main If;
4904 
4905   IF l_debug_level  > 0 THEN
4906       oe_debug_pub.add(  'VALUE OF THE INDEX ' || J , 1 ) ;
4907   END IF;
4908   IF l_debug_level  > 0 THEN
4909       oe_debug_pub.add(  'EXITING LOAD_RESULTS_FROM_REC' || X_RETURN_STATUS , 1 ) ;
4910   END IF;
4911 
4912 EXCEPTION
4913    WHEN FND_API.G_EXC_ERROR THEN
4914      x_return_status :=  FND_API.G_RET_STS_ERROR;
4915   WHEN OTHERS THEN
4916       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4917       THEN
4918             OE_MSG_PUB.Add_Exc_Msg
4919             (   G_PKG_NAME
4920             ,   'Load_Results_from_rec'
4921             );
4922       END IF;
4923       IF l_debug_level  > 0 THEN
4924           oe_debug_pub.add(  'UNEXPECTED ERROR IN LOAD_RESULTS_FROM_REC' , 1 ) ;
4925       END IF;
4926       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4927 END Load_Results_from_rec;
4928 
4929 
4930 /*--------------------------------------------------------------------------
4931 Procedure Name : Load_Results_from_Tbl
4932 Description    : This API loads the results from MRP's ATP_REC_TYPE to
4933                  OM's order line. It also populates OM's ATP Table which
4934                  is used to display the ATP results on the client side.
4935                  We ignore the mandatory components which we passed to MRP
4936                  while loading the results.
4937 
4938                  Added parameter p_old_line_tbl to support bug 1955004
4939                  for call to Inactive_Demand_Scheduling()
4940 -------------------------------------------------------------------------- */
4941 Procedure Load_Results_from_tbl
4942 ( p_atp_rec         IN MRP_ATP_PUB.ATP_Rec_Typ
4943 , p_old_line_tbl    IN OE_ORDER_PUB.line_tbl_type
4944 , p_x_line_tbl      IN OUT NOCOPY OE_ORDER_PUB.line_tbl_type
4945 , p_sch_action      IN VARCHAR2 := NULL
4946 , p_partial         IN BOOLEAN := FALSE
4947 , p_partial_set     IN BOOLEAN := FALSE
4948 , x_return_status OUT NOCOPY VARCHAR2)
4949 
4950 IS
4951 J                 NUMBER := 1;
4952 K                 NUMBER := 1;
4953 l_sch_action      VARCHAR2(30) := p_sch_action;
4954 l_old_ato_line_id NUMBER := -99;
4955 l_old_set_id      VARCHAR2(30) := -99;
4956 l_new_set_id      VARCHAR2(30) := -99;
4957 l_config_exists   VARCHAR2(1) := 'N';
4958 
4959 l_return_status   VARCHAR2(1);  -- Added for IR ISO CMS Project
4960 --
4961 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
4962 --
4963 l_raise_error     BOOLEAN := FALSE;
4964 BEGIN
4965   IF l_debug_level  > 0 THEN
4966       oe_debug_pub.add(  'ENTERING LOAD_RESULTS_FROM_TBL' , 1 ) ;
4967   END IF;
4968   x_return_status :=  FND_API.G_RET_STS_SUCCESS;
4969 
4970   IF l_sch_action is NULL THEN
4971      l_sch_action := p_x_line_tbl(1).schedule_action_code;
4972   END IF;
4973 
4974   IF l_debug_level  > 0 THEN
4975      oe_debug_pub.add('-----------------Loading MRP Result From Tbl---------------',1);
4976       oe_debug_pub.add(  'Loading mrp results' , 1 ) ;
4977       oe_debug_pub.add(  'Mrp count is ' || P_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
4978       oe_debug_pub.add(  'Line count is ' || P_X_LINE_TBL.COUNT , 1 ) ;
4979       oe_debug_pub.add(  'Scheduling action ' || L_SCH_ACTION , 1 ) ;
4980   END IF;
4981 
4982   g_atp_tbl.delete;
4983 
4984   FOR I in 1..p_x_line_tbl.count LOOP
4985 
4986     -- BUG 1955004
4987     IF OE_inactive_demand_tbl.EXISTS(p_x_line_tbl(I).line_id) THEN
4988        -- we know this is line has an inactive demand scheduling level
4989 
4990         IF l_debug_level  > 0 THEN
4991             oe_debug_pub.add(  'CALLING INACTIVE_DEMAND_SCHEDULING FROM LOAD_RESULTS_FROM_TBL' , 1 ) ;
4992         END IF;
4993         Inactive_demand_scheduling(p_x_old_line_rec => p_old_line_tbl(I)
4994                                   ,p_x_line_rec => p_x_line_tbl(I)
4995                                   ,x_return_status => x_return_status);
4996 
4997         OE_inactive_demand_tbl.DELETE(p_x_line_tbl(I).line_id);
4998 
4999         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5000           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5001         END IF;
5002 
5003     ELSE
5004      -- END 1955004
5005 
5006      IF p_x_line_tbl(I).item_type_code = OE_GLOBALS.G_ITEM_CONFIG THEN
5007 
5008         -- The config item might be a part of the table since query
5009         -- of  group of lines returns back the config item too. But
5010         -- we did not pass this item to MRP (in load_mrp_request). Thus
5011         -- we will bypass this record out here too.
5012 
5013         -- Since we don't pass config line to MRP we need populate schedule date
5014         -- on config from Model line. This is to fix bug1576412.
5015 
5016         -- We need populate the model record scheduling values
5017         -- on the config line.
5018 
5019         K := 1;
5020         WHILE K <= p_atp_rec.error_code.count LOOP
5021 
5022 
5023          -- code has been changed to fix bug 2314594.
5024          IF p_x_line_tbl(I).ato_line_id = p_atp_rec.Identifier(K) THEN
5025             EXIT;
5026          END IF;
5027          K := K +1;
5028 
5029         END LOOP;
5030 
5031         IF l_sch_action =  OESCH_ACT_RESCHEDULE
5032           AND NVL(p_x_line_tbl(I).ordered_quantity,0) > 0 THEN -- 3907522
5033 
5034         -- Modified this part to fix bug 1900085.
5035            IF l_debug_level  > 0 THEN
5036                oe_debug_pub.add(  'UPDATING CONFIG LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
5037            END IF;
5038 
5039            IF p_atp_rec.group_ship_date(k) IS NOT NULL
5040            THEN
5041               p_x_line_tbl(I).schedule_ship_date := p_atp_rec.group_ship_date(k);
5042            ELSE
5043               p_x_line_tbl(I).schedule_ship_date  := p_atp_rec.ship_date(k);
5044            END IF;
5045 
5046            IF p_atp_rec.group_arrival_date(k) IS NOT NULL THEN
5047               p_x_line_tbl(I).schedule_arrival_date := p_atp_rec.group_arrival_date(k);
5048            ELSE
5049               p_x_line_tbl(I).schedule_arrival_date := p_atp_rec.arrival_date(k);
5050 
5051            END IF;
5052 
5053            p_x_line_tbl(I).delivery_lead_time := p_atp_rec.delivery_lead_time(k);
5054 
5055           IF l_debug_level  > 0 THEN
5056              oe_debug_pub.add(  'Config schedule ' || P_X_LINE_TBL(I).SCHEDULE_SHIP_DATE , 2 ) ;
5057              oe_debug_pub.add(  'Config arrival ' || P_X_LINE_TBL(I).SCHEDULE_ARRIVAL_DATE , 2 ) ;
5058           END IF;
5059         END IF;
5060 
5061         --4052648
5062         IF l_sch_action =  OESCH_ACT_UNSCHEDULE THEN
5063            IF l_debug_level  > 0 THEN
5064               oe_debug_pub.add(  'CLEARING SCHEDULE INFORMATION FOR CONFIG LINE ' , 2 ) ;
5065            END IF;
5066            p_x_line_tbl(I).schedule_ship_date    := null;
5067            p_x_line_tbl(I).schedule_arrival_date := null;
5068            p_x_line_tbl(I).schedule_status_code  := null;
5069            p_x_line_tbl(I).visible_demand_flag   := null;
5070 
5071            IF  p_x_line_tbl(I).ship_set_id IS NOT NULL
5072            AND p_x_line_tbl(I).ship_set_id <> FND_API.G_MISS_NUM THEN
5073               p_x_line_tbl(I).ship_set     := null;
5074            END IF;
5075 
5076            IF  p_x_line_tbl(I).arrival_set_id IS NOT NULL
5077            AND p_x_line_tbl(I).arrival_set_id <> FND_API.G_MISS_NUM THEN
5078               p_x_line_tbl(I).arrival_set  := null;
5079            END IF;
5080 
5081            -- Unscheduling a line will also clear the Override Atp flag
5082            p_x_line_tbl(I).override_atp_date_code := Null;
5083         END IF;
5084 
5085      ELSE
5086 
5087       IF p_x_line_tbl(I).ato_line_id is not null
5088       AND p_x_line_tbl(I).ato_line_id <> l_old_ato_line_id
5089       THEN
5090 
5091         l_old_ato_line_id := p_x_line_tbl(I).ato_line_id;
5092         IF l_debug_level  > 0 THEN
5093            oe_debug_pub.add(  'Check for config on line ' || P_X_LINE_TBL ( I ) .ATO_LINE_ID , 1 ) ;
5094         END IF;
5095         BEGIN
5096 
5097           Select 'Y'
5098           Into   l_config_exists
5099           From   oe_order_lines_all
5100           Where  header_id = p_x_line_tbl(I).header_id
5101           And    ato_line_id = p_x_line_tbl(I).ato_line_id
5102           And    item_type_code = OE_GLOBALS.G_ITEM_CONFIG;
5103 
5104         EXCEPTION
5105               WHEN OTHERS THEN
5106                IF l_debug_level  > 0 THEN
5107                    oe_debug_pub.add(  'NO CONFIG EXISTS FOR ATO ' , 1 ) ;
5108                END IF;
5109                l_config_exists := 'N';
5110         END;
5111 
5112       END IF;
5113 
5114       IF l_debug_level  > 0 THEN
5115           oe_debug_pub.add(  'Load results line_id ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
5116           oe_debug_pub.add(  'Index ' || J , 1 ) ;
5117       END IF;
5118 
5119 
5120       IF  l_sch_action = OESCH_ACT_ATP_CHECK
5121       AND OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
5122       AND MSC_ATP_GLOBAL.GET_APS_VERSION = 10
5123       AND p_x_line_tbl(I).ato_line_id is not null
5124       AND p_x_line_tbl(I).ato_line_id <> p_x_line_tbl(I).line_id  THEN
5125 
5126         IF l_debug_level  > 0 THEN
5127           oe_debug_pub.add(  'Do not call load_results for options/calsss');
5128         END IF;
5129 
5130       ELSE
5131 
5132         Load_Results_from_rec(p_atp_rec       => p_atp_rec,
5133                               p_x_line_rec    => p_x_line_tbl(I),
5134                               p_sch_action    => l_sch_action,
5135                               p_index         => J,
5136                               p_config_exists => l_config_exists,
5137                               p_partial_set   => p_partial_set,
5138                               x_return_status => x_return_status);
5139 
5140         IF l_debug_level  > 0 THEN
5141           oe_debug_pub.add(  'After call to load_results_from_rec' || J  || x_return_status, 1 ) ;
5142         END IF;
5143 
5144         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5145            IF l_debug_level  > 0 THEN
5146                oe_debug_pub.add(  'UNEXP ERROR LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
5147            END IF;
5148            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5149         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
5150         -- This code is required, if user performing scheduling action
5151         -- from header or using multi selected lines, system has to
5152         -- commit those lines which went through scheduling fine.
5153         -- Ignore other records.
5154 
5155            l_new_set_id := NVL(p_atp_rec.ship_set_name(J),
5156                               NVL(p_atp_rec.arrival_set_name(J),-99));
5157 
5158            IF l_old_set_id <> l_new_set_id
5159 --           AND p_atp_rec.error_code(J) = 19
5160            AND NOT p_partial_set  THEN
5161 
5162             IF l_debug_level  > 0 THEN
5163                 oe_debug_pub.add(  'Before setting message for: ' || P_ATP_REC.ERROR_CODE(J) , 2 ) ;
5164             END IF;
5165 
5166              l_old_set_id := l_new_set_id;
5167              FND_MESSAGE.SET_NAME('ONT','OE_SCH_GROUP_MEMBER_FAILED');
5168              OE_MSG_PUB.Add;
5169 
5170 
5171            END IF;
5172            IF p_partial THEN
5173               IF l_debug_level  > 0 THEN
5174                   oe_debug_pub.add(  'SET OPERATION TO NONE' , 1 ) ;
5175               END IF;
5176               p_x_line_tbl(I).operation := OE_GLOBALS.G_OPR_NONE;
5177               x_return_status := FND_API.G_RET_STS_SUCCESS;
5178            ELSE
5179               IF l_debug_level  > 0 THEN
5180                   oe_debug_pub.add(  'EXP ERROR LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
5181               END IF;
5182               l_raise_error := TRUE;
5183               --RAISE FND_API.G_EXC_ERROR;
5184            END IF;
5185         END IF;
5186       END IF; -- GOP Code
5187      END IF;
5188 
5189 
5190      -- As part of the bug fix 2910899, OM will indicate and remember the
5191      -- Standard Madatory record positions using vendor_name. This will be
5192      -- used in the load_results procedure to bypass the SMC records.
5193      -- Increment and skip the smc records.
5194 
5195      IF I < p_x_line_tbl.count AND
5196         p_x_line_tbl(I).item_type_code <> 'CONFIG' AND
5197         J < p_atp_rec.Identifier.count  THEN
5198          J := J + 1;
5199          IF (nvl(p_atp_rec.vendor_name(J),'N') = 'SMC')
5200          THEN
5201 
5202             WHILE (nvl(p_atp_rec.vendor_name(J),'N') = 'SMC')
5203             LOOP
5204               IF l_debug_level  > 0 THEN
5205               oe_debug_pub.add(  'SMC  : '|| J  , 3 ) ;
5206               END IF;
5207                J := J + 1;
5208          IF p_atp_rec.identifier.count < J THEN
5209          GOTO END_ATP_WHILE;
5210          END IF;
5211             END LOOP;
5212 
5213       << END_ATP_WHILE >>
5214       NULL;
5215 
5216          END IF;
5217      END IF;
5218      -- End of increment and skip.
5219 
5220   IF l_debug_level  > 0 THEN
5221       oe_debug_pub.add(  'OPERATION : ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
5222   END IF;
5223 
5224     END IF; -- for new IF statement for BUG 1955004
5225 
5226 /* 7576948: IR ISO Change Management Begins */
5227 
5228 /* Commented for bug #8205242
5229 
5230 -- This code is hooked for IR ISO project, where if the schedule ship
5231 -- date is changed as a result of MRP call then a delayed request is
5232 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
5233 -- API from Purchasing, responsible for conditionally updating the Need By
5234 -- Date column in internal requisition line. This will be done based on
5235 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
5236 
5237 -- For details on IR ISO CMS project, please refer to FOL >
5238 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
5239 
5240 
5241     IF (p_x_line_tbl(i).order_source_id = 10) AND
5242        (
5243         ((p_old_line_tbl(i).schedule_ship_date IS NOT NULL) AND
5244           NOT OE_GLOBALS.Equal(p_x_line_tbl(i).schedule_ship_date,p_old_line_tbl(i).schedule_ship_date))
5245        OR
5246         ((p_old_line_tbl(i).schedule_arrival_date IS NOT NULL) AND
5247           NOT OE_GLOBALS.Equal(p_x_line_tbl(i).schedule_arrival_date,p_old_line_tbl(i).schedule_arrival_date))
5248        )  THEN
5249 
5250        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
5251          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
5252          -- AND FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
5253          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
5254          -- Modified for IR ISO Tracking bug 7667702
5255 
5256          IF l_debug_level > 0 THEN
5257            oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change');
5258          END IF;
5259 
5260          -- Log a delayed request to update the change in Schedule Ship Date to
5261          -- Requisition Line. This request will be logged only if the change is
5262          -- not initiated from Requesting Organization, and it is not a case of
5263          -- Internal Sales Order Full Cancellation. It will even not be logged
5264          -- Purchasing profile option does not allow update of Need By Date when
5265          -- Schedule Ship Date changes on internal sales order line
5266 
5267          OE_delayed_requests_Pvt.log_request
5268          ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
5269          , p_entity_id              => p_x_line_tbl(i).line_id
5270          , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
5271          , p_requesting_entity_id   => p_x_line_tbl(i).line_id
5272          , p_request_unique_key1    => p_x_line_tbl(i).header_id  -- Order Hdr_id
5273          , p_request_unique_key2    => p_x_line_tbl(i).source_document_id -- Req Hdr_id
5274          , p_request_unique_key3    => p_x_line_tbl(i).source_document_line_id -- Req Line_id
5275          , p_date_param1            => p_x_line_tbl(i).schedule_arrival_date -- schedule_ship_date
5276 -- Note: p_date_param1 is used for both Schedule_Ship_Date and
5277 -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
5278 -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
5279 -- it can expect change with respect to Ship or Arrival date. Thus, will
5280 -- not raise any issues.
5281          , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
5282          , x_return_status          => l_return_status
5283          );
5284 
5285          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5286            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5287          ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
5288            RAISE FND_API.G_EXC_ERROR;
5289          END IF;
5290 
5291          ELSE -- Added for IR ISO Tracking bug 7667702
5292            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
5293              IF l_debug_level > 0 THEN
5294                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
5295              END IF;
5296 
5297              OE_SCHEDULE_UTIL.Update_PO(p_x_line_tbl(i).schedule_arrival_date,
5298                 p_x_line_tbl(i).source_document_id,
5299                 p_x_line_tbl(i).source_document_line_id);
5300            END IF;
5301          END IF;
5302 
5303        END IF;
5304      END IF; -- Order_Source_id
5305 
5306 */
5307 
5308 /* ============================= */
5309 /* IR ISO Change Management Ends */
5310 
5311 
5312   END LOOP;
5313   IF l_raise_error THEN
5314 
5315      RAISE FND_API.G_EXC_ERROR;
5316 
5317   END IF;
5318 
5319   IF l_debug_level  > 0 THEN
5320       oe_debug_pub.add(  'EXITING LOAD_RESULTS_FROM_TBL' , 1 ) ;
5321   END IF;
5322 
5323 EXCEPTION
5324    WHEN FND_API.G_EXC_ERROR THEN
5325 
5326       x_return_status :=  FND_API.G_RET_STS_ERROR;
5327   WHEN OTHERS THEN
5328       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5329       THEN
5330             OE_MSG_PUB.Add_Exc_Msg
5331             (   G_PKG_NAME
5332             ,   'Load_Results_from_tbl'
5333             );
5334       END IF;
5335       IF l_debug_level  > 0 THEN
5336           oe_debug_pub.add(  'UNEXPECTED ERROR IN LOAD_RESULTS_FROM_TBL' , 1 ) ;
5337       END IF;
5338       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5339 END Load_Results_from_tbl;
5340 
5341 Procedure Display_Sch_Errors
5342 ( p_atp_rec         IN  MRP_ATP_PUB.ATP_Rec_Typ
5343 , p_line_tbl        IN  OE_ORDER_PUB.line_tbl_type
5344                         := OE_ORDER_PUB.G_MISS_LINE_TBL
5345 , p_line_id         IN  NUMBER DEFAULT NULL)
5346 
5347 IS
5348 
5349 J                 NUMBER := 1;
5350 l_old_set_id      Varchar2(30) := -99;
5351 l_new_set_id      Varchar2(30) := -99;
5352 l_explanation     VARCHAR2(240);
5353 l_order_source_id          NUMBER;
5354 l_orig_sys_document_ref    VARCHAR2(50);
5355 l_orig_sys_line_ref        VARCHAR2(50);
5356 l_orig_sys_shipment_ref    VARCHAR2(50);
5357 l_change_sequence          VARCHAR2(50);
5358 l_source_document_id       NUMBER;
5359 l_source_document_line_id  NUMBER;
5360 l_source_document_type_id  NUMBER;
5361 l_header_id                NUMBER;
5362 
5363 --
5364 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
5365 --
5366 BEGIN
5367 
5368     IF l_debug_level  > 0 THEN
5369         oe_debug_pub.add(  'ENTERING DISPLAY_SCH_ERRORS' , 1 ) ;
5370     END IF;
5371 
5372 IF p_atp_rec.error_code.count > 0 THEN
5373 
5374  IF p_line_id is not null THEN
5375 
5376 
5377     IF l_debug_level  > 0 THEN
5378         oe_debug_pub.add(  'SCHEDULING FAILED WITH ERROR CODE: ' || P_LINE_ID ) ;
5379     END IF;
5380 
5381     IF p_line_id <> FND_API.G_MISS_NUM THEN
5382        select order_source_id, orig_sys_document_ref, orig_sys_line_ref,
5383               orig_sys_shipment_ref, change_sequence, source_document_id,
5384               source_document_line_id, source_document_type_id, header_id
5385        into l_order_source_id, l_orig_sys_document_ref, l_orig_sys_line_ref,
5386             l_orig_sys_shipment_ref, l_change_sequence, l_source_document_id,
5387             l_source_document_line_id, l_source_document_type_id, l_header_id
5388        from oe_order_lines
5389        where line_id = p_line_id;
5390      END IF;
5391 
5392      OE_MSG_PUB.set_msg_context(
5393        p_entity_code                => 'LINE'
5394       ,p_entity_id                  => p_line_id
5395       ,p_header_id                  => l_header_id
5396       ,p_line_id                    => p_line_id
5397       ,p_orig_sys_document_ref      => l_orig_sys_document_ref
5398       ,p_orig_sys_document_line_ref => l_orig_sys_line_ref
5399       ,p_orig_sys_shipment_ref      => l_orig_sys_shipment_ref
5400       ,p_change_sequence            => l_change_sequence
5401       ,p_source_document_id         => l_source_document_id
5402       ,p_source_document_line_id    => l_source_document_line_id
5403       ,p_order_source_id            => l_order_source_id
5404       ,p_source_document_type_id    => l_source_document_type_id);
5405 
5406      IF p_atp_rec.error_code(J) = 80 THEN
5407 
5408         FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
5409         OE_MSG_PUB.Add;
5410         l_explanation := null;
5411 
5412      ELSIF p_atp_rec.error_code(J) <> 0 THEN
5413 
5414         select meaning
5415         into l_explanation
5416         from mfg_lookups where
5417         lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
5418         and lookup_code = p_atp_rec.error_code(J) ;
5419 
5420         IF l_debug_level  > 0 THEN
5421             oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
5422         END IF;
5423         FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
5424         FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
5425         OE_MSG_PUB.Add;
5426 
5427       ELSE
5428 
5429         FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
5430         OE_MSG_PUB.Add;
5431       END IF;
5432 
5433 
5434  ELSE
5435 
5436   FOR I in 1..p_line_tbl.count LOOP
5437 
5438    IF p_line_tbl(I).Item_type_code <> 'CONFIG' THEN
5439 
5440      l_new_set_id := NVL(p_atp_rec.ship_set_name(J),
5441                      NVL(p_atp_rec.arrival_set_name(J),-99));
5442 
5443      IF l_old_set_id <> l_new_set_id
5444      AND p_atp_rec.error_code(J) = 19 THEN
5445 
5446       IF l_debug_level  > 0 THEN
5447          oe_debug_pub.add(  'Before setting message for: ' || P_ATP_REC.ERROR_CODE ( J ) , 2 ) ;
5448       END IF;
5449 
5450       l_old_set_id := l_new_set_id;
5451       FND_MESSAGE.SET_NAME('ONT','OE_SCH_GROUP_MEMBER_FAILED');
5452       OE_MSG_PUB.Add;
5453 
5454      ELSIF p_atp_rec.error_code(J) <> 0
5455      AND   p_atp_rec.error_code(J) <> 19 THEN
5456 
5457       IF l_debug_level  > 0 THEN
5458           oe_debug_pub.add(  'SCHEDULING FAILED WITH ERROR CODE:'||P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
5459       END IF;
5460 
5461 
5462           OE_MSG_PUB.set_msg_context(
5463            p_entity_code                => 'LINE'
5464           ,p_entity_id                  => p_line_tbl(I).line_id
5465           ,p_header_id                  => p_line_tbl(I).header_id
5466           ,p_line_id                    => p_line_tbl(I).line_id
5467           ,p_order_source_id            => p_line_tbl(I).order_source_id
5468           ,p_orig_sys_document_ref      => p_line_tbl(I).orig_sys_document_ref
5469           ,p_orig_sys_document_line_ref => p_line_tbl(I).orig_sys_line_ref
5470           ,p_orig_sys_shipment_ref      => p_line_tbl(I).orig_sys_shipment_ref
5471           ,p_change_sequence            => p_line_tbl(I).change_sequence
5472           ,p_source_document_type_id    => p_line_tbl(I).source_document_type_id
5473           ,p_source_document_id         => p_line_tbl(I).source_document_id
5474           ,p_source_document_line_id    => p_line_tbl(I).source_document_line_id);
5475 
5476           IF p_atp_rec.error_code(J) = 80 THEN
5477 
5478             FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
5479             OE_MSG_PUB.Add;
5480             l_explanation := null;
5481 
5482           ELSE
5483             select meaning
5484             into l_explanation
5485             from mfg_lookups where
5486             lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
5487             and lookup_code = p_atp_rec.error_code(J) ;
5488 
5489             IF l_debug_level  > 0 THEN
5490                 oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
5491             END IF;
5492             FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
5493             FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
5494             OE_MSG_PUB.Add;
5495 
5496 
5497           END IF;
5498       END IF;
5499 
5500    -- Increment and skip the smc records.
5501       IF I < p_line_tbl.count AND
5502          p_line_tbl(I).item_type_code <> 'CONFIG' AND
5503          J < p_atp_rec.Identifier.count  THEN
5504 
5505           J := J + 1;
5506           IF (p_line_tbl(I).line_id = p_atp_rec.Identifier(J))
5507           THEN
5508 
5509             WHILE (p_atp_rec.Identifier(J) = p_line_tbl(I).line_id)
5510             LOOP
5511                 J := J + 1;
5512               IF p_atp_rec.identifier.count < J THEN
5513                 GOTO END_ATP_WHILE;
5514               END IF;
5515             END LOOP;
5516 
5517           << END_ATP_WHILE >>
5518           NULL;
5519 
5520          END IF;
5521       END IF;
5522     END IF;
5523   END LOOP;
5524  END IF; -- p_line
5525 
5526 END IF;
5527 
5528 End Display_Sch_Errors;
5529 
5530 /*---------------------------------------------------------------------
5531 Procedure Name : Call_MRP_ATP
5532 Description    : Create and call MRP API.
5533 --------------------------------------------------------------------- */
5534 
5535 Procedure Call_MRP_ATP
5536 ( p_x_line_rec       IN OUT NOCOPY OE_ORDER_PUB.Line_Rec_Type
5537  ,p_old_line_rec     IN OE_ORDER_PUB.Line_Rec_Type
5538 ,x_return_status OUT NOCOPY VARCHAR2)
5539 
5540 IS
5541 l_msg_count               NUMBER;
5542 l_session_id              NUMBER := 0;
5543 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
5544 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
5545 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5546 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
5547 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
5548 l_mrp_msg_data            VARCHAR2(200);
5549 
5550 --
5551 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
5552 --
5553 BEGIN
5554 
5555    IF l_debug_level  > 0 THEN
5556        oe_debug_pub.add(  'ENTERING CALL MRP ATP' , 1 ) ;
5557    END IF;
5558 
5559 -- BUG 1955004
5560  -- 3763015
5561  IF (sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
5562     sch_cached_sch_level_code = SCH_LEVEL_FIVE OR
5563     NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y') THEN
5564     -- this is an inactive demand line.
5565 
5566    IF l_debug_level  > 0 THEN
5567        oe_debug_pub.add(  'THIS IS A SINGLE INACTIVE DEMAND LINE' , 1 ) ;
5568    END IF;
5569 
5570    Inactive_Demand_Scheduling(p_x_old_line_rec => p_old_line_rec
5571                             ,p_x_line_rec     => p_x_line_rec
5572                             ,x_return_status  => x_return_status);
5573 
5574    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5575           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5576    ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
5577           Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
5578                              p_line_id => p_x_line_rec.line_id);
5579           RAISE FND_API.G_EXC_ERROR;
5580    END IF;
5581 
5582  ELSE
5583 
5584    IF l_debug_level  > 0 THEN
5585        oe_debug_pub.add(  'ELSE CALL FOR CALL MRP ATP' , 1 ) ;
5586    END IF;
5587 -- END 1955004
5588 
5589    Load_MRP_request_from_rec
5590        ( p_line_rec              => p_x_line_rec
5591        , p_old_line_rec          => p_old_line_rec
5592        , x_mrp_atp_rec             => l_mrp_atp_rec);
5593 
5594 
5595    IF l_mrp_atp_rec.error_code.count > 0 THEN
5596       l_session_id := Get_Session_Id;
5597 
5598       -- Call ATP
5599 
5600        IF l_debug_level  > 0 THEN
5601            oe_debug_pub.add(  '1. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
5602        END IF;
5603 
5604     /* Added the following 10  lines to fix the bug 6378240 */
5605        oe_debug_pub.add(  '6378240 : inserted the line_id into temp table '||p_x_line_rec.line_id ||'  ' ||p_x_line_rec.schedule_action_code);
5606      BEGIN
5607        insert into oe_schedule_lines_temp
5608                    (LINE_ID ,
5609                     SCHEDULE_ACTION_CODE)
5610               values(p_x_line_rec.line_id,p_x_line_rec.schedule_action_code);
5611      EXCEPTION
5612          WHEN OTHERS THEN
5613                   oe_debug_pub.add(  '6378240 : INSERT ERROR ');
5614      END;
5615 
5616 
5617 
5618        MRP_ATP_PUB.Call_ATP
5619               (  p_session_id             =>  l_session_id
5620                , p_atp_rec                =>  l_mrp_atp_rec
5621                , x_atp_rec                =>  l_out_mrp_atp_rec
5622                , x_atp_supply_demand      =>  l_atp_supply_demand
5623                , x_atp_period             =>  l_atp_period
5624                , x_atp_details            =>  l_atp_details
5625                , x_return_status          =>  x_return_status
5626                , x_msg_data               =>  l_mrp_msg_data
5627                , x_msg_count              =>  l_msg_count);
5628 
5629                                               IF l_debug_level  > 0 THEN
5630                                                   oe_debug_pub.add(  '3. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 1 ) ;
5631                                               END IF;
5632 
5633        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5634           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5635        ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
5636           IF p_x_line_rec.schedule_action_code <> OESCH_ACT_ATP_CHECK THEN
5637              Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
5638                                 p_line_id => p_x_line_rec.line_id);
5639              RAISE FND_API.G_EXC_ERROR;
5640           END IF;
5641        END IF;
5642 
5643        Load_Results_from_rec(p_atp_rec       => l_out_mrp_atp_rec,
5644                              p_x_line_rec    => p_x_line_rec,
5645                              x_return_status => x_return_status);
5646 
5647        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5648           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5649        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
5650           RAISE FND_API.G_EXC_ERROR;
5651        END IF;
5652 
5653    END IF; -- Mrp count.
5654    IF l_debug_level  > 0 THEN
5655        oe_debug_pub.add(  'EXITING CALL ATP' , 1 ) ;
5656    END IF;
5657 
5658   END IF;  -- for new IF statement added for 1955004
5659 
5660 EXCEPTION
5661    WHEN FND_API.G_EXC_ERROR THEN
5662 
5663         x_return_status := FND_API.G_RET_STS_ERROR;
5664 
5665    WHEN OTHERS THEN
5666 
5667         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5668 
5669         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5670         THEN
5671             OE_MSG_PUB.Add_Exc_Msg
5672             (   G_PKG_NAME,
5673               'Call_MRP_ATP');
5674         END IF;
5675         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5676 END Call_MRP_ATP;
5677 PROCEDURE Cascade_Override_atp
5678  (p_line_rec IN     OE_ORDER_PUB.line_rec_type)
5679 IS
5680 --
5681 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
5682 --
5683 BEGIN
5684 
5685    IF l_debug_level  > 0 THEN
5686        oe_debug_pub.add(  'ENTERING CASCADE_OVERRIDE_ATP ' , 1 ) ;
5687    END IF;
5688 
5689     IF   p_line_rec.ato_line_id IS NOT NULL AND
5690     NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
5691          p_line_rec.item_type_code = 'OPTION') THEN
5692 
5693        UPDATE OE_ORDER_LINES_ALL
5694        SET    override_atp_date_code = Null
5695        WHERE  header_id = p_line_rec.header_id
5696        AND    ato_line_id = p_line_rec.ato_line_id;
5697 
5698     ELSIF  p_line_rec.item_type_code = 'CLASS'
5699        OR  p_line_rec.item_type_code = 'MODEL'
5700        OR  p_line_rec.item_type_code = 'KIT'
5701     THEN
5702 
5703        UPDATE OE_ORDER_LINES_ALL
5704        SET    override_atp_date_code = Null
5705        WHERE  header_id = p_line_rec.header_id
5706        AND    link_to_line_id  = p_line_rec.line_id
5707        AND    item_type_code = 'INCLUDED';
5708 
5709     END IF;
5710 
5711    IF l_debug_level  > 0 THEN
5712        oe_debug_pub.add(  'EXITING CASCADE_OVERRIDE_ATP ' , 1 ) ;
5713    END IF;
5714 EXCEPTION
5715 
5716    WHEN OTHERS THEN
5717 
5718         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5719         THEN
5720             OE_MSG_PUB.Add_Exc_Msg
5721             (   G_PKG_NAME,
5722               'Cascade Override Atp');
5723         END IF;
5724         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5725 END Cascade_Override_atp;
5726 /*---------------------------------------------------------------------
5727 Procedure Name : Query_Qty_Tree
5728 Description    : Queries the On-Hand and Available to Reserve
5729                  quantites by calling INV's
5730                  inv_quantity_tree_pub.query_quantities.
5731                  The quantities are given at the highest level (Item, Org
5732                  combination).
5733 --------------------------------------------------------------------- */
5734 Procedure Query_Qty_Tree(p_org_id            IN NUMBER,
5735                          p_item_id           IN NUMBER,
5736                          p_line_id           IN NUMBER DEFAULT NULL,
5737                          p_sch_date          IN DATE DEFAULT NULL,
5738 --                         p_grade_code        IN VARCHAR2 DEFAULT NULL, -- INVCONV NOT NEEDED NOW
5739 x_on_hand_qty OUT NOCOPY NUMBER,
5740 x_avail_to_reserve OUT NOCOPY NUMBER,
5741 x_on_hand_qty2 OUT NOCOPY NUMBER, -- INVCONV
5742 x_avail_to_reserve2 OUT NOCOPY NUMBER -- INVCONV
5743 
5744 )
5745 
5746 IS
5747   l_return_status           VARCHAR2(1);
5748   l_msg_count               NUMBER;
5749   l_msg_data                VARCHAR2(2000);
5750   l_qoh                     NUMBER;
5751   l_rqoh                    NUMBER;
5752   l_qr                      NUMBER;
5753   l_qs                      NUMBER;
5754   l_att                     NUMBER;
5755   l_atr                     NUMBER;
5756 
5757   l_sqoh                     NUMBER; -- INVCONV
5758   l_srqoh                    NUMBER; -- INVCONV
5759   l_sqr                      NUMBER; -- INVCONV
5760   l_sqs                      NUMBER; -- INVCONV
5761   l_satt                     NUMBER; -- INVCONV
5762   l_satr                     NUMBER; -- INVCONV
5763 
5764 
5765   l_msg_index               number;
5766   l_lot_control_flag        BOOLEAN;
5767   l_lot_control_code        NUMBER;
5768   l_org_id                  NUMBER;
5769 
5770 
5771   -- added by fabdi 03/May/2001
5772 --  l_process_flag      VARCHAR2(1) := FND_API.G_FALSE; -- INVCONV
5773 --  l_ic_item_mst_rec         GMI_RESERVATION_UTIL.ic_item_mst_rec; -- INVCONV
5774   -- end fabdi
5775 
5776 --
5777 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
5778 --
5779 BEGIN
5780 
5781   IF l_debug_level  > 0 THEN
5782       oe_debug_pub.add(  'ENTERING QUERY_QTY_TREE ' , 1 ) ;
5783   END IF;
5784   IF l_debug_level  > 0 THEN
5785       oe_debug_pub.add(  'ORG IS : ' || P_ORG_ID , 1 ) ;
5786   END IF;
5787   IF l_debug_level  > 0 THEN
5788       oe_debug_pub.add(  'ITEM IS : ' || P_ITEM_ID , 1 ) ;
5789   END IF;
5790   -- added by fabdi 03/May/2001
5791 /*  IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_org_id) -- INVCONV remove OPM stuff
5792   THEN
5793   l_process_flag := FND_API.G_FALSE;
5794   ELSE
5795   l_process_flag := FND_API.G_TRUE;
5796   END IF;
5797 
5798   IF l_process_flag = FND_API.G_TRUE
5799   THEN
5800 
5801         GMI_RESERVATION_UTIL.Get_OPM_item_from_Apps
5802         ( p_organization_id          =>  p_org_id
5803         , p_inventory_item_id        =>  p_item_id
5804         , x_ic_item_mst_rec          =>  l_ic_item_mst_rec
5805         , x_return_status            =>  l_return_status
5806         , x_msg_count                =>  l_msg_count
5807         , x_msg_data                 =>  l_msg_data);
5808 
5809         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)
5810         THEN
5811            FND_MESSAGE.SET_NAME('GMI','GMI_ERROR');
5812            FND_MESSAGE.SET_TOKEN('BY_PROC',
5813                             'GMI_Reservation_Util.Get_OPM_item_from_Apps');
5814            FND_MESSAGE.SET_TOKEN('WHERE','OE_SCHEDULE_UTIL');
5815            RAISE FND_API.G_EXC_ERROR;
5816         END IF;
5817         get_process_query_quantities ( p_org_id => p_org_id,
5818                                        p_item_id =>  l_ic_item_mst_rec.item_id,
5819                                        p_line_id => p_line_id,
5820                                        x_on_hand_qty => l_qoh,
5821                                        x_avail_to_reserve => l_atr
5822                                       );
5823 
5824   -- end fabdi
5825   ELSE   */
5826 
5827   BEGIN
5828      -- Added code to fix bug 2111470
5829     IF p_org_id is null THEN
5830        l_org_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
5831     END IF;
5832 
5833     SELECT msi.lot_control_code
5834     INTO   l_lot_control_code
5835     FROM   mtl_system_items msi
5836     WHERE  msi.inventory_item_id = p_item_id
5837     AND    msi.organization_id   = nvl(p_org_id,l_org_id);
5838 
5839     IF l_lot_control_code = 2 THEN
5840        l_lot_control_flag := TRUE;
5841     ELSE
5842        l_lot_control_flag := FALSE;
5843     END IF;
5844 
5845   EXCEPTION
5846    WHEN OTHERS THEN
5847    l_lot_control_flag := FALSE;
5848   END;
5849 
5850   -- Bug 2259553.
5851   --inv_quantity_tree_pvt.clear_quantity_cache;
5852     inv_quantity_tree_pvt.mark_all_for_refresh
5853   (  p_api_version_number  => 1.0
5854    , p_init_msg_lst        => FND_API.G_TRUE
5855    , x_return_status       => l_return_status
5856    , x_msg_count           => l_msg_count
5857    , x_msg_data            => l_msg_data
5858    );
5859 
5860   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5861          oe_msg_pub.transfer_msg_stack;
5862          l_msg_count:=OE_MSG_PUB.COUNT_MSG;
5863          for I in 1..l_msg_count loop
5864              l_msg_data := OE_MSG_PUB.Get(I,'F');
5865              IF l_debug_level  > 0 THEN
5866                  oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
5867              END IF;
5868          end loop;
5869          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5870   ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
5871           oe_msg_pub.transfer_msg_stack;
5872           l_msg_count:=OE_MSG_PUB.COUNT_MSG;
5873           for I in 1..l_msg_count loop
5874               l_msg_data := OE_MSG_PUB.Get(I,'F');
5875               IF l_debug_level  > 0 THEN
5876                   oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
5877               END IF;
5878           end loop;
5879           RAISE FND_API.G_EXC_ERROR;
5880   END IF;
5881 
5882 
5883   inv_quantity_tree_pub.query_quantities
5884     (  p_api_version_number      => 1.0
5885      , x_return_status           => l_return_status
5886      , x_msg_count               => l_msg_count
5887      , x_msg_data                => l_msg_data
5888      , p_organization_id         => p_org_id
5889      , p_inventory_item_id       => p_item_id
5890      , p_tree_mode               => 2
5891      , p_is_revision_control     => false
5892      , p_is_lot_control          => l_lot_control_flag
5893      , p_lot_expiration_date     => nvl(p_sch_date,sysdate)
5894      , p_is_serial_control       => false
5895      , p_grade_code              => NUll  -- INVCONV      NOT NEEDED NOW
5896      , p_revision                => null
5897      , p_lot_number              => null
5898      , p_subinventory_code       => null
5899      , p_locator_id              => null
5900      , x_qoh                     => l_qoh
5901      , x_rqoh                    => l_rqoh
5902      , x_qr                      => l_qr
5903      , x_qs                      => l_qs
5904      , x_att                     => l_att
5905      , x_atr                     => l_atr
5906      , x_sqoh                    => l_sqoh        -- INVCONV
5907      , x_srqoh                   => l_srqoh       -- INVCONV
5908      , x_sqr                     => l_sqr         -- INVCONV
5909      , x_sqs                     => l_sqs         -- INVCONV
5910      , x_satt                    => l_satt        -- INVCONV
5911      , x_satr                    => l_satr        -- INVCONV
5912      );
5913 
5914   IF l_debug_level  > 0 THEN
5915       oe_debug_pub.add(  'AFTER CALLING QUERY_QUANTITIES' , 1 ) ;
5916   END IF;
5917  -- END IF; INVCONV
5918   IF l_debug_level  > 0 THEN
5919       oe_debug_pub.add(  'RR: L_QOH ' || L_QOH , 1 ) ;
5920   END IF;
5921   IF l_debug_level  > 0 THEN
5922       oe_debug_pub.add(  'RR: L_QOH ' || L_ATR , 1 ) ;
5923   END IF;
5924 
5925   x_on_hand_qty      := l_qoh;
5926   x_avail_to_reserve := l_atr;
5927 
5928   x_on_hand_qty2      := l_sqoh; -- INVCONV
5929   x_avail_to_reserve2 := l_satr; -- INVCONV
5930 
5931   IF l_debug_level  > 0 THEN
5932       oe_debug_pub.add(  'EXITING QUERY_QTY_TREE ' , 1 ) ;
5933   END IF;
5934 
5935 EXCEPTION
5936 
5937    WHEN OTHERS THEN
5938 
5939         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5940         THEN
5941             OE_MSG_PUB.Add_Exc_Msg
5942             (   G_PKG_NAME,
5943               'Query_Qty_Tree');
5944         END IF;
5945         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5946 END Query_Qty_Tree;
5947 /*---------------------------------------------------------------------
5948 Procedure Name : Action ATP
5949 Description    : This procedure is called to perform atp_check on a single
5950                  line
5951 --------------------------------------------------------------------- */
5952 Procedure Action_ATP
5953 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
5954  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
5955 x_return_status OUT NOCOPY VARCHAR2)
5956 
5957 IS
5958 l_msg_count               NUMBER;
5959 l_mrp_msg_data            VARCHAR2(2000);
5960 --l_session_id              NUMBER := 0;
5961 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
5962 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
5963 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
5964 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
5965 l_on_hand_qty             NUMBER;
5966 l_avail_to_reserve        NUMBER;
5967 l_on_hand_qty2             NUMBER; -- INVCONV
5968 l_avail_to_reserve2        NUMBER; -- INVCONV
5969 
5970 
5971 -- l_process_flag            VARCHAR2(1) := FND_API.G_FALSE;
5972 --
5973 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level; --INVCONV
5974 --
5975 BEGIN
5976 
5977    IF l_debug_level  > 0 THEN
5978        oe_debug_pub.add(  'ENTERING ACTION ATP ' || P_X_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
5979    END IF;
5980 
5981      -- Call MRP API
5982 
5983    Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
5984                 p_old_line_rec  => p_old_line_rec,
5985                 x_return_status => x_return_status);
5986 
5987    IF l_debug_level  > 0 THEN
5988        oe_debug_pub.add(  'AFTER CALLING MRP API: ' || X_RETURN_STATUS , 2 ) ;
5989    END IF;
5990 
5991    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5992       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5993    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
5994       RAISE FND_API.G_EXC_ERROR;
5995    END IF;
5996 
5997   /* Load_Mrp_Request_From_Rec
5998       ( p_line_rec        => p_x_line_rec
5999       , p_old_line_rec    => p_old_line_rec
6000       , x_mrp_atp_rec     => l_mrp_atp_rec);
6001 
6002    IF l_mrp_atp_rec.error_code.count > 0 THEN
6003 
6004      l_session_id := Get_Session_Id;
6005 
6006      G_ATP_CHECK_session_id := l_session_id;
6007      -- Call ATP
6008 
6009 
6010      oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,1);
6011 
6012      MRP_ATP_PUB.Call_ATP
6013          ( p_session_id          =>  l_session_id
6014           , p_atp_rec            =>  l_mrp_atp_rec
6015           , x_atp_rec            =>  l_mrp_atp_rec
6016           , x_atp_supply_demand  =>  l_atp_supply_demand
6017           , x_atp_period         =>  l_atp_period
6018           , x_atp_details        =>  l_atp_details
6019           , x_return_status      =>  x_return_status
6020           , x_msg_data           =>  l_mrp_msg_data
6021           , x_msg_count          =>  l_msg_count);
6022 
6023 
6024      oe_debug_pub.add('5. After Calling MRP_ATP_PUB.Call_ATP' ||
6025                                               x_return_status,1);
6026      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6027         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6028      END IF;
6029 
6030      Load_Results_from_rec(p_atp_rec        => l_mrp_atp_rec,
6031                            p_x_line_rec     => p_x_line_rec,
6032                            x_return_status  => x_return_status);
6033 
6034      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6035             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6036      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6037             RAISE FND_API.G_EXC_ERROR;
6038      END IF;
6039 
6040    END IF; -- Mrp count.
6041   */
6042      -- We also need to pass back on-hand qty and available_to_reserve
6043      -- qties while performing ATP. Getting these values from inventory.
6044 
6045      FOR K IN 1..g_atp_tbl.count LOOP
6046         IF l_debug_level  > 0 THEN
6047             oe_debug_pub.add(  'CALLING QUERY_QTY_TREE' , 1 ) ;
6048         END IF;
6049         Query_Qty_Tree(p_org_id     => g_atp_tbl(K).ship_from_org_id,
6050                        p_item_id    => g_atp_tbl(K).inventory_item_id,
6051                        p_line_id    => g_atp_tbl(K).line_id,
6052                        p_sch_date   => nvl(g_atp_tbl(K).group_available_date,
6053                                        g_atp_tbl(K).ordered_qty_Available_Date),
6054                        x_on_hand_qty      => l_on_hand_qty,
6055                        x_avail_to_reserve => l_avail_to_reserve,
6056                        x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
6057                        x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
6058                        );
6059 
6060        /*  --  added by fabdi 03/May/2001  NOT NEEDED NOW FOR OPM INVENTORY CONVERGENCE INVCONV
6061         IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id
6062                                            => g_atp_tbl(K).ship_from_org_id)
6063         THEN
6064              l_process_flag := FND_API.G_FALSE;
6065         ELSE
6066              l_process_flag := FND_API.G_TRUE;
6067         END IF;
6068 
6069         IF l_process_flag = FND_API.G_TRUE
6070         THEN
6071           g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
6072           g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
6073           g_atp_tbl(K).QTY_ON_REQUEST_DATE  := l_avail_to_reserve;
6074                                                  -- Available field in ATP
6075 
6076           IF l_debug_level  > 0 THEN
6077               oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
6078           END IF;
6079           IF l_debug_level  > 0 THEN
6080               oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
6081           END IF;
6082           IF l_debug_level  > 0 THEN
6083               oe_debug_pub.add(  'AVAILABLE ' || L_AVAIL_TO_RESERVE ) ;
6084           END IF;
6085         ELSE   */  --  INVCONV
6086 
6087           IF l_debug_level  > 0 THEN
6088               oe_debug_pub.add(  'SUBSTITUTE_FLAG :' || G_ATP_TBL ( K ) .SUBSTITUTE_FLAG , 1 ) ;
6089           END IF;
6090           IF g_atp_tbl(K).substitute_flag = 'N' THEN
6091              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
6092              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
6093              IF l_debug_level  > 0 THEN
6094                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
6095              END IF;
6096              IF l_debug_level  > 0 THEN
6097                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
6098              END IF;
6099           ELSE
6100              g_atp_tbl(K).sub_on_hand_qty          := l_on_hand_qty;
6101              g_atp_tbl(K).sub_available_to_reserve := l_avail_to_reserve;
6102              IF l_debug_level  > 0 THEN
6103                  oe_debug_pub.add(  'SUB L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
6104              END IF;
6105              IF l_debug_level  > 0 THEN
6106                  oe_debug_pub.add(  'SUB L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
6107              END IF;
6108 
6109              Query_Qty_Tree
6110                   (p_org_id     => g_atp_tbl(K).ship_from_org_id,
6111                    p_item_id    => g_atp_tbl(K).request_item_id,
6112                    p_line_id    => g_atp_tbl(K).line_id,
6113                    p_sch_date   => g_atp_tbl(K).req_item_available_date,
6114                    x_on_hand_qty      => l_on_hand_qty,
6115                    x_avail_to_reserve => l_avail_to_reserve,
6116                    x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
6117                    x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
6118                    );
6119              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
6120              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
6121              IF l_debug_level  > 0 THEN
6122                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
6123              END IF;
6124              IF l_debug_level  > 0 THEN
6125                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
6126              END IF;
6127           END IF;
6128         -- END IF; -- INVCONV
6129         -- end fabdi
6130 
6131      END LOOP;
6132 
6133     IF l_debug_level  > 0 THEN
6134         oe_debug_pub.add(  'EXITING ACTION ATP' || X_RETURN_STATUS , 1 ) ;
6135     END IF;
6136 
6137 EXCEPTION
6138    WHEN FND_API.G_EXC_ERROR THEN
6139 
6140         x_return_status := FND_API.G_RET_STS_ERROR;
6141         RAISE FND_API.G_EXC_ERROR;
6142    WHEN OTHERS THEN
6143 
6144         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6145 
6146         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6147         THEN
6148             OE_MSG_PUB.Add_Exc_Msg
6149             (   G_PKG_NAME,
6150               'Action_ATP');
6151         END IF;
6152         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6153 END Action_ATP;
6154 /*---------------------------------------------------------------------
6155 Procedure Name : Action_Schedule
6156 Description    : This procedure is called from Process_Request proecudure
6157                  to perform the action of SCHEDULE or RESERVE on the line.
6158 --------------------------------------------------------------------- */
6159 Procedure Action_Schedule
6160 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
6161  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
6162  p_sch_action     IN  VARCHAR2,
6163  p_qty_to_reserve IN  NUMBER := Null,
6164  x_return_status OUT NOCOPY VARCHAR2)
6165 
6166 IS
6167 l_msg_count               NUMBER;
6168 --l_session_id              NUMBER := 0;
6169 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
6170 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6171 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
6172 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
6173 l_mrp_msg_data            VARCHAR2(200);
6174 l_old_reserved_quantity   NUMBER;
6175 l_qty_to_reserve          NUMBER;
6176 
6177 l_old_reserved_quantity2   NUMBER; -- INVCONV
6178 l_qty2_to_reserve          NUMBER := null;  -- INVCONV
6179 
6180 l_p_qty2_to_reserve          NUMBER := null;  -- INVCONV
6181 
6182 --
6183 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6184 --
6185 BEGIN
6186  IF l_debug_level  > 0 THEN
6187      oe_debug_pub.add(  'ENTERING ACTION SCHEDULE' , 1 ) ;
6188  END IF;
6189 
6190 l_p_qty2_to_reserve := nvl(p_x_line_rec.reserved_quantity2, 0); -- INVCONV
6191 
6192   IF (p_x_line_rec.schedule_status_code is null)
6193   THEN
6194 
6195     -- The line is not scheduled, so go ahead and schedule the line
6196     -- Create MRP record from the line record with the action of schedule
6197     -- The result of the request should be in x_request_rec
6198 
6199     -- Setting the action to schedule since first we need to schedule the line.
6200     -- Will reset the action to what is actually was after calling MRP.
6201 
6202     IF l_debug_level  > 0 THEN
6203         oe_debug_pub.add(  'SCHEDULING FROM ACTION SCHEDULE' , 1 ) ;
6204     END IF;
6205     p_x_line_rec.schedule_action_code := OESCH_ACT_SCHEDULE;
6206 
6207 /*
6208     Load_MRP_Request_from_rec
6209           ( p_line_rec              => p_x_line_rec
6210           , p_old_line_rec          => p_old_line_rec
6211           , x_mrp_atp_rec               => l_mrp_atp_rec);
6212 
6213    oe_debug_pub.add('Action Schedule Count is ' ||
6214                        l_mrp_atp_rec.error_code.count,1);
6215 
6216    -- We are adding this so that we will not call MRP when
6217    -- table count is 0.
6218 
6219    IF l_mrp_atp_rec.error_code.count > 0 THEN
6220 
6221 
6222     l_session_id := Get_Session_Id;
6223 
6224     oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,1);
6225 
6226     MRP_ATP_PUB.Call_ATP
6227             (  p_session_id             =>  l_session_id
6228              , p_atp_rec                =>  l_mrp_atp_rec
6229              , x_atp_rec                =>  l_mrp_atp_rec
6230              , x_atp_supply_demand      =>  l_atp_supply_demand
6231              , x_atp_period             =>  l_atp_period
6232              , x_atp_details            =>  l_atp_details
6233              , x_return_status          =>  x_return_status
6234              , x_msg_data               =>  l_mrp_msg_data
6235              , x_msg_count              =>  l_msg_count);
6236 
6237      oe_debug_pub.add('1. After Calling MRP_ATP_PUB.Call_ATP' ||
6238                                               x_return_status,1);
6239     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6240             oe_debug_pub.add('Error is' || l_mrp_msg_data,1);
6241             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6242     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6243           --Display_Sch_Errors;
6244           Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
6245                              p_line_id => p_x_line_rec.line_id);
6246           RAISE FND_API.G_EXC_ERROR;
6247     END IF;
6248 
6249     Load_Results_from_rec
6250                 (p_atp_rec         => l_mrp_atp_rec,
6251                  p_x_line_rec      => p_x_line_rec,
6252                  x_return_status   => x_return_status);
6253 
6254     END IF; -- Check for MRP count.
6255 */
6256 
6257 
6258 
6259     Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
6260                 p_old_line_rec  => p_old_line_rec,
6261                 x_return_status => x_return_status);
6262 
6263     IF l_debug_level  > 0 THEN
6264         oe_debug_pub.add(  'X_RETURN_STATUS ' || X_RETURN_STATUS , 1 ) ;
6265     END IF;
6266     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6267             IF l_debug_level  > 0 THEN
6268                 oe_debug_pub.add(  'UNEXPECTED ERROR FROM ' , 1 ) ;
6269             END IF;
6270             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6271     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6272             IF l_debug_level  > 0 THEN
6273                 oe_debug_pub.add(  'RR: L2' , 1 ) ;
6274             END IF;
6275             RAISE FND_API.G_EXC_ERROR;
6276     END IF;
6277 
6278     -- Reloading p_x_line_rec from l_out_line_tbl since the record
6279     -- in the table is the one which is demanded.
6280 
6281     IF l_debug_level  > 0 THEN
6282         oe_debug_pub.add(  'RR: L3' , 1 ) ;
6283     END IF;
6284     p_x_line_rec.schedule_action_code := p_sch_action;
6285 
6286   END IF; /* If schedule_status_code is null */
6287 
6288   IF l_debug_level  > 0 THEN
6289       oe_debug_pub.add(  'RR: L4' , 1 ) ;
6290   END IF;
6291   IF l_debug_level  > 0 THEN
6292       oe_debug_pub.add(  'SCH_CACHED_SCH_LEVEL_CODE ' || SCH_CACHED_SCH_LEVEL_CODE , 1 ) ;
6293   END IF;
6294 
6295   IF (p_sch_action = OESCH_ACT_RESERVE)
6296       OR (p_sch_action = OESCH_ACT_SCHEDULE AND
6297          (sch_cached_sch_level_code = SCH_LEVEL_THREE OR
6298           -- BUG 1955004
6299           sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
6300           -- END 1955004
6301           sch_cached_sch_level_code is null) AND
6302           Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id)) OR --4689197
6303      (p_qty_to_reserve is not null)
6304   THEN
6305 
6306     IF l_debug_level  > 0 THEN
6307         oe_debug_pub.add(  'SCHEDULING IN ACTION_SCHEDULE' , 1 ) ;
6308     END IF;
6309 
6310     -- Assigning reserved_quantity to 0 if MISS_NUM, to fix the bug 1384831
6311 
6312     l_old_reserved_quantity := p_old_line_rec.reserved_quantity;
6313     l_old_reserved_quantity2 := nvl(p_old_line_rec.reserved_quantity2,0); -- INVCONV
6314 
6315     IF p_old_line_rec.reserved_quantity = FND_API.G_MISS_NUM THEN
6316 
6317        l_old_reserved_quantity := 0;
6318 
6319     END IF;
6320         IF p_old_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM THEN -- INVCONV
6321        l_old_reserved_quantity2 := 0;
6322     END IF;
6323 
6324     IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
6325     THEN
6326       -- Create INV record from the line to reserve
6327 
6328       IF p_qty_to_reserve is null THEN
6329          l_qty_to_reserve := p_x_line_rec.ordered_quantity -
6330                            nvl(l_old_reserved_quantity,0);
6331 
6332 
6333       ELSE
6334          l_qty_to_reserve := p_qty_to_reserve;
6335       END IF;
6336 
6337       IF l_p_qty2_to_reserve is null or
6338              l_p_qty2_to_reserve = FND_API.G_MISS_NUM THEN -- INVCONV
6339       -- KYH BUG 4245418 BEGIN
6340       -- =====================
6341       -- l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0)  -      -- need to test if this falls over
6342       --                   nvl(l_old_reserved_quantity2,0);
6343       -- It is dangerous to compute secondary quantity to reserve based on
6344       -- ordered_quantity2 minus reserved_quantity2 as above.
6345       -- This is because ordered_quantity 2 always reflects a standard conversion from ordered_quantity
6346       -- whereas reserved_quantity2 may be the result of one or more lot specific calculations
6347       -- Combining values from these different conversion rates may not give the correct result.
6348       -- Better to compute the secondary to reserve by converting l_qty_to_reserve
6349          IF p_x_line_rec.ordered_quantity_uom2 is not null and
6350                         p_x_line_rec.ordered_quantity_uom2 <> FND_API.G_MISS_CHAR THEN
6351            -- Only invoke the conversion for dual tracked items
6352            IF l_debug_level  > 0 THEN
6353              oe_debug_pub.add(  'DUAL Tracked quantity so convert the qty to reserve ' || l_qty_to_reserve , 1 ) ;
6354            END IF;
6355            l_qty2_to_reserve    := inv_convert.inv_um_convert(
6356                                    item_id                      => p_x_line_rec.inventory_item_id
6357                                  , lot_number                   => NULL
6358                                  , organization_id              => p_x_line_rec.ship_from_org_id
6359                                  , PRECISION                    => 5
6360                                  , from_quantity                => l_qty_to_reserve
6361                                  , from_unit                    => p_x_line_rec.order_quantity_uom
6362                                  , to_unit                      => p_x_line_rec.ordered_quantity_uom2
6363                                  , from_name                    => NULL -- from uom name
6364                                  , to_name                      => NULL -- to uom name
6365                                  );
6366            IF l_debug_level  > 0 THEN
6367              oe_debug_pub.add(  'After UOM conversion the secondary to reserve is  ' || l_qty2_to_reserve , 1 ) ;
6368            END IF;
6369 
6370            IF l_qty2_to_reserve = -99999 THEN
6371              -- conversion failed
6372              FND_MESSAGE.SET_NAME('INV','INV_NO_CONVERSION_ERR'); -- INVCONV
6373              OE_MSG_PUB.ADD;
6374              IF l_debug_level  > 0 THEN
6375                oe_debug_pub.add(  'ERROR on UOM conversion to Secondary UOM which is '||p_x_line_rec.ordered_quantity_uom2 , 1 ) ;
6376              END IF;
6377              RAISE FND_API.G_EXC_ERROR;
6378            END IF;
6379          END IF;
6380       -- KYH BUG 4245418 END
6381       ELSE
6382          l_qty2_to_reserve := l_p_qty2_to_reserve;
6383       END IF;
6384       IF l_qty2_to_reserve = 0 -- INVCONV
6385          THEN
6386                     l_qty2_to_reserve := NULL;
6387       END IF;
6388 
6389 
6390       IF l_qty_to_reserve > 0 THEN
6391 
6392          -- Since we are calling schedule line from post write.
6393          -- line is posted to db. we do not need to skip the code.
6394      --    IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE THEN
6395             -- We cannot create a reservation currently if the line is
6396             -- being created (since the lines is yet not in the database,
6397             -- and there is a validation done with this line id when we
6398             -- call INV API.). We will populate the reserved quantity on
6399             -- the line record, and in the post-write procedure (in OEXULINB),
6400             -- we will perform the reservation.
6401 
6402             --l_out_line_rec.schedule_status_code := OESCH_STATUS_SCHEDULED;
6403       --        p_x_line_rec.reserved_quantity    := l_qty_to_reserve;
6404 
6405        --  ELSE
6406              IF l_debug_level  > 0 THEN
6407            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
6408                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
6409        END IF;
6410 
6411 
6412              Reserve_Line
6413               ( p_line_rec              => p_x_line_rec
6414               , p_quantity_to_reserve   => l_qty_to_reserve
6415               , p_quantity2_to_reserve   => l_qty2_to_reserve -- INVCONV
6416               , x_return_status         => x_return_status);
6417 
6418         -- END IF; -- Operation on the line is create or not
6419 
6420       ELSE
6421 
6422          p_x_line_rec.reserved_quantity :=
6423                                   p_old_line_rec.reserved_quantity;
6424 
6425            p_x_line_rec.reserved_quantity2 :=
6426                                   p_old_line_rec.reserved_quantity2; -- INVCONV
6427 
6428            IF l_debug_level  > 0 THEN
6429                 oe_debug_pub.add(  'p_old_line_rec.reserved_quantity ' || p_old_line_rec.reserved_quantity , 2 ) ;
6430                      oe_debug_pub.add(  'p_old_line_rec.reserved_quantity2 ' || p_old_line_rec.reserved_quantity2 , 2 ) ;
6431        END IF;
6432 
6433       END IF; -- l_qty_to_reserve
6434     END IF; -- If shippable Flag = Y
6435 
6436   END IF; /* If reservation needs to be performed */
6437 
6438 
6439     IF l_debug_level  > 0 THEN
6440         oe_debug_pub.add(  'EXITING ACTION SCHEDULE: ' || X_RETURN_STATUS , 1 ) ;
6441     END IF;
6442 
6443 EXCEPTION
6444    WHEN FND_API.G_EXC_ERROR THEN
6445 
6446         x_return_status := FND_API.G_RET_STS_ERROR;
6447 
6448    WHEN OTHERS THEN
6449 
6450         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6451 
6452         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6453         THEN
6454             OE_MSG_PUB.Add_Exc_Msg
6455             (   G_PKG_NAME,
6456               'Action_Schedule');
6457         END IF;
6458 
6459 END Action_Schedule;
6460 /*---------------------------------------------------------------------
6461 Procedure Name : Action_UnSchedule
6462 Description    : This procedure is called from Process_Request proecudure
6463                  to perform the action of UNSCHEDULE or UNRESERVE on the line.
6464 --------------------------------------------------------------------- */
6465 
6466 Procedure Action_UnSchedule
6467 (p_old_line_rec  IN  OE_ORDER_PUB.line_rec_type,
6468  p_sch_action    IN  VARCHAR2,
6469  p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
6470 x_return_status OUT NOCOPY VARCHAR2)
6471 
6472 IS
6473 
6474 l_msg_count               NUMBER;
6475 --l_msg_data                VARCHAR2(2000);
6476 --l_session_id              NUMBER := 0;
6477 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
6478 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6479 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
6480 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
6481 l_mrp_msg_data            VARCHAR2(200);
6482 l_qty_to_unreserve        NUMBER;
6483 l_qty2_to_unreserve        NUMBER; -- INVCONV
6484 --
6485 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6486 --
6487 BEGIN
6488 
6489      IF l_debug_level  > 0 THEN
6490          oe_debug_pub.add(  'ENTERING ACTION_UNSCHEDULE ' , 1 ) ;
6491      END IF;
6492 
6493      -- shipping_interfaced_flag
6494      IF (p_old_line_rec.reserved_quantity is not null AND
6495          p_old_line_rec.reserved_quantity <> FND_API.G_MISS_NUM)
6496      THEN
6497 
6498         -- Call INV API to delete the reservations on  the line.
6499 
6500         l_qty_to_unreserve := p_old_line_rec.reserved_quantity;
6501         l_qty2_to_unreserve := p_old_line_rec.reserved_quantity2; -- INCONV
6502         IF l_qty2_to_unreserve = 0 -- INVCONV
6503          THEN
6504           l_qty2_to_unreserve := NULL;
6505         END IF;
6506 
6507         Unreserve_Line
6508             ( p_line_rec               => p_old_line_rec
6509             , p_quantity_to_unreserve  => l_qty_to_unreserve
6510             , p_quantity2_to_unreserve  => l_qty2_to_unreserve
6511             , x_return_status          => x_return_status);
6512 
6513         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6514              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6515         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6516              RAISE FND_API.G_EXC_ERROR;
6517         END IF;
6518 
6519      END IF;
6520 
6521      -- If the action was unreserve, we do not need to unschedule the line.
6522      -- Thus we will check for this condition before unscheduling.
6523 
6524      IF p_sch_action <> OESCH_ACT_UNRESERVE THEN
6525 
6526        -- Create MRP record with action of UNDEMAND.
6527 
6528        p_x_line_rec.schedule_action_code := OESCH_ACT_UNDEMAND;
6529 
6530  /*
6531       Load_MRP_Request_from_rec
6532           ( p_line_rec              => p_x_line_rec
6533           , p_old_line_rec          => p_old_line_rec
6534           , x_mrp_atp_rec             => l_mrp_atp_rec);
6535 
6536       IF l_mrp_atp_rec.error_code.count > 0 THEN
6537 
6538         l_session_id := Get_Session_Id;
6539 
6540         -- Call ATP
6541 
6542         oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,1);
6543 
6544         MRP_ATP_PUB.Call_ATP
6545           ( p_session_id             =>  l_session_id
6546           , p_atp_rec                =>  l_mrp_atp_rec
6547           , x_atp_rec                =>  l_mrp_atp_rec
6548           , x_atp_supply_demand      =>  l_atp_supply_demand
6549           , x_atp_period             =>  l_atp_period
6550           , x_atp_details            =>  l_atp_details
6551           , x_return_status          =>  x_return_status
6552           , x_msg_data               =>  l_mrp_msg_data
6553           , x_msg_count              =>  l_msg_count);
6554 
6555         oe_debug_pub.add('2. After Calling MRP_ATP_PUB.Call_ATP' ||
6556                                               x_return_status,1);
6557 
6558         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6559            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6560         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6561            --Display_Sch_Errors;
6562            Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
6563                               p_line_id => p_x_line_rec.line_id);
6564            RAISE FND_API.G_EXC_ERROR;
6565         END IF;
6566 
6567         Load_Results_from_rec
6568                     (p_atp_rec         => l_mrp_atp_rec,
6569                      p_x_line_rec      => p_x_line_rec,
6570                      x_return_status   => x_return_status);
6571 
6572         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6573                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6574         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6575                 RAISE FND_API.G_EXC_ERROR;
6576         END IF;
6577 
6578       END IF; -- MRP Count.
6579 
6580  */
6581 
6582 
6583         Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
6584                      p_old_line_rec  => p_old_line_rec,
6585                      x_return_status => x_return_status);
6586 
6587        IF l_debug_level  > 0 THEN
6588            oe_debug_pub.add(  'AFTER CALLING MRP API: ' || X_RETURN_STATUS , 2 ) ;
6589        END IF;
6590 
6591         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6592                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6593         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6594                 RAISE FND_API.G_EXC_ERROR;
6595         END IF;
6596 
6597     END IF;
6598 
6599     -- If the action performed is UNSCHEDULE, then the reserved quantity is now     -- null.
6600     p_x_line_rec.reserved_quantity    := null;
6601     p_x_line_rec.reserved_quantity2   := null; -- INVCONV
6602 
6603 
6604   IF l_debug_level  > 0 THEN
6605       oe_debug_pub.add(  'EXITING ACTION_UNSCHEDULE ' , 1 ) ;
6606   END IF;
6607 EXCEPTION
6608    WHEN FND_API.G_EXC_ERROR THEN
6609 
6610         x_return_status := FND_API.G_RET_STS_ERROR;
6611 
6612    WHEN OTHERS THEN
6613 
6614         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6615 
6616         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6617         THEN
6618             OE_MSG_PUB.Add_Exc_Msg
6619             (   G_PKG_NAME,
6620               'Action_UnSchedule');
6621         END IF;
6622 
6623 END Action_UnSchedule;
6624 
6625 /*---------------------------------------------------------------------
6626 Procedure Name : Action_Undemand
6627 Description    : This procedure is called from SCHEDULE LINE proecudure
6628                  to perform the UNDEMAD on the line when an item is changed.
6629 --------------------------------------------------------------------- */
6630 
6631 Procedure Action_Undemand
6632 (p_old_line_rec  IN  OE_ORDER_PUB.line_rec_type,
6633 x_return_status OUT NOCOPY VARCHAR2)
6634 
6635 
6636 IS
6637 l_msg_count               NUMBER;
6638 l_msg_data                VARCHAR2(2000);
6639 l_session_id              NUMBER := 0;
6640 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
6641 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
6642 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6643 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
6644 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
6645 -- Bug 1955004
6646 l_scheduling_level_code   VARCHAR2(30);
6647 
6648 --
6649 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6650 --
6651 BEGIN
6652 
6653       IF l_debug_level  > 0 THEN
6654           oe_debug_pub.add(  'ENTERING ACTION_UNDEMAND' , 1 ) ;
6655       END IF;
6656 
6657    --BUG 1955004
6658    IF l_debug_level  > 0 THEN
6659        oe_debug_pub.add(  'CHECKING SCHEDULING LEVEL...' , 1 ) ;
6660    END IF;
6661    l_scheduling_level_code := Get_Scheduling_Level(p_old_line_rec.header_id,
6662                                                    p_old_line_rec.line_type_id);
6663 
6664    IF l_debug_level  > 0 THEN
6665        oe_debug_pub.add(  'L_SCHEDULING_LEVEL_CODE : ' || L_SCHEDULING_LEVEL_CODE , 1 ) ;
6666    END IF;
6667    -- 3763015
6668    IF (l_scheduling_level_code = SCH_LEVEL_FOUR OR
6669     l_scheduling_level_code = SCH_LEVEL_FIVE OR
6670     NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y') THEN
6671     -- this is an inactive demand line.
6672 
6673       IF l_debug_level  > 0 THEN
6674           oe_debug_pub.add(  'IT IS AN INACTIVE DEMAND LINE' , 1 ) ;
6675       END IF;
6676       NULL;
6677    ELSE
6678    --END 1955004
6679 
6680       IF l_debug_level  > 0 THEN
6681           oe_debug_pub.add(  'STD ACTION_UNDEMAND ACTION' , 1 ) ;
6682       END IF;
6683 
6684       -- Create MRP record with action of UNDEMAND.
6685 
6686       Load_MRP_Request_from_rec
6687           ( p_line_rec      => p_old_line_rec
6688           , p_old_line_rec  => p_old_line_rec
6689           , p_sch_action    => OESCH_ACT_UNDEMAND
6690           , x_mrp_atp_rec   => l_mrp_atp_rec);
6691 
6692       IF l_mrp_atp_rec.error_code.count > 0 THEN
6693 
6694         l_session_id := Get_Session_Id;
6695 
6696         -- Call ATP
6697 
6698         IF l_debug_level  > 0 THEN
6699             oe_debug_pub.add(  '4. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
6700         END IF;
6701 
6702         MRP_ATP_PUB.Call_ATP
6703           ( p_session_id             =>  l_session_id
6704           , p_atp_rec                =>  l_mrp_atp_rec
6705           , x_atp_rec                =>  l_out_mrp_atp_rec
6706           , x_atp_supply_demand      =>  l_atp_supply_demand
6707           , x_atp_period             =>  l_atp_period
6708           , x_atp_details            =>  l_atp_details
6709           , x_return_status          =>  x_return_status
6710           , x_msg_data               =>  l_msg_data
6711           , x_msg_count              =>  l_msg_count);
6712 
6713                                               IF l_debug_level  > 0 THEN
6714                                                   oe_debug_pub.add(  '4. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 1 ) ;
6715                                               END IF;
6716 
6717         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6718            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6719         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6720            --Display_Sch_Errors;
6721            Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
6722                               p_line_id => p_old_line_rec.line_id);
6723            RAISE FND_API.G_EXC_ERROR;
6724         END IF;
6725       END IF; -- MRP count.
6726      END IF;  -- For new IF statement for 1955004
6727 
6728       IF l_debug_level  > 0 THEN
6729           oe_debug_pub.add(  'EXITING ACTION_UNDEMAND' , 1 ) ;
6730       END IF;
6731 
6732 EXCEPTION
6733 
6734    WHEN FND_API.G_EXC_ERROR THEN
6735 
6736         x_return_status := FND_API.G_RET_STS_ERROR;
6737 
6738    WHEN OTHERS THEN
6739 
6740         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6741 
6742         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6743         THEN
6744             OE_MSG_PUB.Add_Exc_Msg
6745             (   G_PKG_NAME,
6746               'Action_Undemand');
6747         END IF;
6748 
6749 END Action_Undemand;
6750 
6751 /*---------------------------------------------------------------------
6752 Procedure Name : Action_Reserve
6753 Description    : This procedure is called from Process_Request proecudure
6754 --------------------------------------------------------------------- */
6755 Procedure Action_Reserve
6756 (p_x_line_rec     IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
6757  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
6758 x_return_status OUT NOCOPY VARCHAR2)
6759 
6760 IS
6761 l_old_reserved_qty     NUMBER :=nvl(p_old_line_rec.reserved_quantity,0);
6762 l_changed_reserved_qty NUMBER;
6763 l_qty_to_reserve       NUMBER;
6764 --
6765 l_old_reserved_qty2     NUMBER :=nvl(p_old_line_rec.reserved_quantity2,0); -- INVCONV
6766 l_changed_reserved_qty2 NUMBER; -- INVCONV
6767 l_qty2_to_reserve       NUMBER; -- INVCONV
6768 
6769 
6770 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6771 --
6772 BEGIN
6773 
6774  IF l_debug_level  > 0 THEN
6775      oe_debug_pub.add(  'ENTERING ACTION RESERVE ' ) ;
6776  END IF;
6777  x_return_status := FND_API.G_RET_STS_SUCCESS;
6778 
6779  -- When only subinventory changed on the line. Need Scheduling
6780  -- Will pass line action as reserve. We will unreserve and reserve
6781  -- the line for new subinventory.
6782 
6783  -- shipping_interfaced_flag.
6784   -- 4653097
6785  IF (NOT OE_GLOBALS.Equal(p_x_line_rec.subinventory,
6786                          p_old_line_rec.subinventory)
6787    OR NOT OE_GLOBALS.Equal(p_x_line_rec.project_id,
6788                          p_old_line_rec.project_id)
6789    OR NOT OE_GLOBALS.Equal(p_x_line_rec.task_id,
6790                          p_old_line_rec.task_id))
6791  AND l_old_reserved_qty > 0
6792  AND NVL(p_x_line_rec.shipping_interfaced_flag,'N') = 'N'
6793  THEN
6794 
6795     IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
6796        then
6797      l_qty2_to_reserve := NULL;
6798 
6799     END IF;
6800 
6801     Unreserve_Line
6802     (p_line_rec              => p_old_line_rec,
6803      p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
6804      p_quantity2_to_unreserve => l_qty2_to_reserve, -- INVCONV
6805      x_return_status         => x_return_status);
6806 
6807      l_old_reserved_qty := 0;
6808      l_old_reserved_qty2 := 0; -- INVCONV
6809      -- Start Added for bug 2470416
6810      -- To set the Cascading_Request_Logged to True
6811      -- which will trigger the form to requery
6812 
6813      OE_GLOBALS.G_CASCADING_REQUEST_LOGGED :=TRUE;
6814      -- End Added for bug 2470416
6815 
6816     IF l_debug_level  > 0 THEN
6817         oe_debug_pub.add(  'AFTER DELETING THE RESERVATION' , 3 ) ;
6818     END IF;
6819  END IF;
6820 
6821  IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
6822  THEN
6823     -- 3239619 / 3362461 / 3456052 Convert the old reserve qty as per new UOM if not same.
6824     IF NOT OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom)
6825       AND l_old_reserved_qty > 0 THEN
6826     l_old_reserved_qty := INV_CONVERT.INV_UM_CONVERT(item_id   =>p_old_line_rec.inventory_item_id,
6827                                       precision    =>5,
6828                                       from_quantity=>l_old_reserved_qty,
6829                                       from_unit    => p_old_line_rec.order_quantity_uom,
6830                                       to_unit      => p_x_line_rec.order_quantity_uom,
6831                                       from_name    => NULL,
6832                                       to_name      => NULL
6833                                       );
6834     END IF;
6835     l_changed_reserved_qty   := l_old_reserved_qty -
6836                                 nvl(p_x_line_rec.reserved_quantity,0);
6837     l_changed_reserved_qty2   := nvl(l_old_reserved_qty2, 0)  -
6838                                 nvl(p_x_line_rec.reserved_quantity2,0); -- INVCONV
6839     IF l_changed_reserved_qty2 = 0 -- INVCONV
6840      THEN
6841         l_changed_reserved_qty2 := NULL;
6842     END IF;
6843 
6844     -- shipping_interfaced_flag
6845     IF l_changed_reserved_qty > 0 THEN
6846 
6847        IF l_debug_level  > 0 THEN
6848            oe_debug_pub.add(  'RESERVED QUANTITY HAS DECREASED' , 1 ) ;
6849        END IF;
6850 
6851        -- No need to pass old record. Since this is a change
6852        -- due to quantity.
6853        -- Start 2595661
6854        IF (nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'Y'
6855            AND NOT Get_Pick_Status(p_x_line_rec.line_id) ) THEN
6856 
6857           Do_Unreserve
6858                 ( p_line_rec               => p_x_line_rec
6859                 , p_quantity_to_unreserve  => l_changed_reserved_qty
6860                 , p_quantity2_to_unreserve  => l_changed_reserved_qty2 -- INVCONV
6861                 , p_old_ship_from_org_id    => p_old_line_rec.ship_from_org_id --5024936
6862                 , x_return_status          => x_return_status);
6863        ELSE
6864        -- End 2595661
6865 
6866           Unreserve_Line
6867                ( p_line_rec              => p_x_line_rec
6868                , p_quantity_to_unreserve => l_changed_reserved_qty
6869                , p_quantity2_to_unreserve => l_changed_reserved_qty2 -- INVCONV
6870                , x_return_status         => x_return_status);
6871        END IF;  --2595661
6872 
6873        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6874           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6875        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6876           RAISE FND_API.G_EXC_ERROR;
6877        END IF;
6878 
6879 
6880     ELSIF l_changed_reserved_qty < 0 THEN
6881 
6882        IF l_debug_level  > 0 THEN
6883            oe_debug_pub.add(  'RESERVED QUANTITY HAS INCREASED' , 1 ) ;
6884        END IF;
6885 
6886        -- Make sure reservation qty should not be more than
6887        -- Ordered qty.
6888 
6889        IF p_x_line_rec.reserved_quantity > p_x_line_rec.ordered_quantity
6890        THEN
6891 
6892           l_qty_to_reserve := p_x_line_rec.ordered_quantity -
6893                               l_old_reserved_qty;
6894 
6895        ELSE
6896 
6897           l_qty_to_reserve := p_x_line_rec.reserved_quantity -
6898                              l_old_reserved_qty;
6899 
6900        END IF;
6901        IF p_x_line_rec.reserved_quantity2 > p_x_line_rec.ordered_quantity2 -- INVCONV
6902        THEN
6903 
6904           l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0) -
6905                               nvl(l_old_reserved_qty2, 0);
6906 
6907        ELSE
6908 
6909           l_qty2_to_reserve := nvl(p_x_line_rec.reserved_quantity2, 0) -
6910                              nvl(l_old_reserved_qty2, 0) ;
6911 
6912        END IF;
6913        IF l_qty2_to_reserve = 0 -- INVCONV
6914          THEN
6915           l_qty2_to_reserve := null;
6916        end if;
6917 
6918 
6919        IF l_debug_level  > 0 THEN
6920            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
6921                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
6922        END IF;
6923 
6924        IF l_qty_to_reserve > 0
6925        THEN
6926 
6927 
6928 
6929 
6930           Reserve_Line
6931           ( p_line_rec             => p_x_line_rec
6932           , p_quantity_to_reserve  => l_qty_to_reserve
6933           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
6934           , p_rsv_update           => TRUE  -- Going to increase reservation
6935           , x_return_Status        => x_return_status);
6936 
6937        END IF;
6938 
6939        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6940           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6941        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6942           RAISE FND_API.G_EXC_ERROR;
6943        END IF;
6944 
6945     END IF; -- end of reserved_quantity change code
6946 
6947  END IF; -- Check for shippable flag.
6948  IF l_debug_level  > 0 THEN
6949      oe_debug_pub.add(  'EXITING ACTION RESERVE ' ) ;
6950  END IF;
6951 
6952 EXCEPTION
6953    WHEN FND_API.G_EXC_ERROR THEN
6954 
6955         x_return_status := FND_API.G_RET_STS_ERROR;
6956 
6957    WHEN OTHERS THEN
6958 
6959         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6960 
6961         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6962         THEN
6963             OE_MSG_PUB.Add_Exc_Msg
6964             (   G_PKG_NAME,
6965               'Action_Reserve');
6966         END IF;
6967 END Action_Reserve;
6968 
6969 /*---------------------------------------------------------------------
6970 Procedure Name : Action_Reschedule
6971 Description    : This procedure is called from Process_Request proecudure
6972 --------------------------------------------------------------------- */
6973 Procedure Action_Reschedule
6974 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
6975  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
6976 x_return_status OUT NOCOPY VARCHAR2,
6977 
6978 x_reserve_later OUT NOCOPY VARCHAR2)
6979 
6980 IS
6981 l_re_reserve_flag      VARCHAR2(1) := 'Y';
6982 l_old_reserved_qty     NUMBER := nvl(p_old_line_rec.reserved_quantity,0);
6983 l_qty_to_reserve       NUMBER;
6984 l_changed_reserved_qty NUMBER;
6985 l_qty_to_unreserve     NUMBER;
6986 -- INVCONV
6987 l_old_reserved_qty2     NUMBER := nvl(p_old_line_rec.reserved_quantity2,0);
6988 l_qty2_to_reserve       NUMBER;
6989 l_changed_reserved_qty2 NUMBER;
6990 l_qty2_to_unreserve     NUMBER;
6991 
6992 
6993 
6994 l_reservation_rec      inv_reservation_global.mtl_reservation_rec_type;
6995 l_rsv_tbl              inv_reservation_global.mtl_reservation_tbl_type;
6996 l_sales_order_id       NUMBER;
6997 l_x_error_code         NUMBER;
6998 l_lock_records         VARCHAR2(1);
6999 l_sort_by_req_date     NUMBER ;
7000 l_count                NUMBER;
7001 l_msg_count            NUMBER;
7002 l_msg_data             VARCHAR2(2000);
7003 l_dummy_sn             inv_reservation_global.serial_number_tbl_type;
7004 l_buffer               VARCHAR2(2000);
7005 l_rsv_update           BOOLEAN :=FALSE;
7006 --
7007 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7008 --
7009 /*OPM BEGIN Bug 2794760
7010 ====================*/
7011 -- l_item_rec         OE_ORDER_CACHE.item_rec_type; -- OPM  -- INVCONV Not needed now because of OPM inventory convergence
7012 /*OPM End Bug 2794760
7013 ====================*/
7014 BEGIN
7015     x_return_status := FND_API.G_RET_STS_SUCCESS;
7016     x_reserve_later := 'N';
7017     IF l_debug_level  > 0 THEN
7018         oe_debug_pub.add(  'ENTERING ACTION_RESCHEDULE ' || P_X_LINE_REC.OPEN_FLAG , 1 ) ;
7019     END IF;
7020 
7021     IF (p_old_line_rec.reserved_quantity is not null)
7022     THEN
7023 
7024       -- Added this part of code to fix bug 1797109.
7025       -- Unreserve only when one of the below mentioned, attrubutes
7026       -- changes, in other case simply re-schedule the line.
7027       --During the fix l_re_reserve_flag is introduced.
7028 
7029 -- shipping_interfaced_flag
7030 --      l_re_reserve_flag := 'N';
7031       --4653097
7032       IF
7033          -- Bug 6335352
7034          -- Commenting the below line
7035          -- nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'N' AND
7036         (NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
7037                                 p_old_line_rec.inventory_item_id)
7038         OR NOT OE_GLOBALS.Equal(p_x_line_rec.subinventory,
7039                                 p_old_line_rec.subinventory)
7040         OR NOT OE_GLOBALS.Equal(p_x_line_rec.order_quantity_uom,
7041                                 p_old_line_rec.order_quantity_uom)
7042         OR NOT OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id,
7043                                 p_old_line_rec.ship_from_org_id)
7044         OR NOT OE_GLOBALS.Equal(p_x_line_rec.project_id,
7045                                 p_old_line_rec.project_id)
7046         OR NOT OE_GLOBALS.Equal(p_x_line_rec.task_id,
7047                                 p_old_line_rec.task_id)) THEN
7048 
7049  --         l_re_reserve_flag := 'Y';
7050           -- Call INV to delete the old reservations
7051 
7052           IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
7053            THEN
7054               l_qty2_to_unreserve := NULL;
7055           END IF;
7056 
7057           Unreserve_Line
7058              (p_line_rec              => p_old_line_rec,
7059               p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
7060               p_quantity2_to_unreserve => l_qty2_to_unreserve , -- INVCONV
7061               x_return_status         => x_return_status);
7062           l_old_reserved_qty := 0;
7063           l_old_reserved_qty2 := 0; -- INVCONV
7064       END IF;
7065     END IF;
7066 
7067 
7068     -- If the scheduling is happening due to inventory item change.
7069     -- We should call MRP twice. First time we should call the with
7070     -- Undemand for old item. Second call would be redemand.
7071 
7072     IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
7073                             p_old_line_rec.inventory_item_id)
7074     THEN
7075 
7076         Action_undemand(p_old_line_rec  => p_old_line_rec,
7077                         x_return_status => x_return_status);
7078 
7079         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7080            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7081         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7082            RAISE FND_API.G_EXC_ERROR;
7083         END IF;
7084     END IF;
7085 
7086 
7087      -- If the rescheduling is happening for order change then
7088      -- Undmand the line, else redemand.
7089 
7090      IF p_x_line_rec.ordered_quantity > 0 THEN
7091         p_x_line_rec.schedule_action_code := OESCH_ACT_REDEMAND;
7092      ELSE
7093         p_x_line_rec.schedule_action_code := OESCH_ACT_UNDEMAND;
7094      END IF;
7095 
7096      Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
7097                   p_old_line_rec  => p_old_line_rec,
7098                   x_return_status => x_return_status);
7099 
7100      --4161641
7101      IF l_debug_level  > 0 THEN
7102         oe_debug_pub.add(  'X_RETURN_STATUS ' || X_RETURN_STATUS , 1 ) ;
7103      END IF;
7104      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7105         IF l_debug_level  > 0 THEN
7106            oe_debug_pub.add(  'UNEXPECTED ERROR FROM MRP CALL ' , 1 ) ;
7107         END IF;
7108         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7109      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7110         IF l_debug_level  > 0 THEN
7111            oe_debug_pub.add(  'ACTION RESCHEDULE : ERROR FROM MRP CALL' , 1 ) ;
7112         END IF;
7113         RAISE FND_API.G_EXC_ERROR;
7114      END IF;
7115 
7116      -- Item has been substituted by MRP. Remove any old reservation
7117      -- on old item. Since reservation cannot be made without posting
7118      -- the new item to db postpone the reservation on new item to
7119      -- process_request.
7120 
7121      IF nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'N'
7122      AND NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
7123                               p_old_line_rec.inventory_item_id)
7124      THEN
7125 
7126        IF l_debug_level  > 0 THEN
7127            oe_debug_pub.add(  'SUB: REMOVE THE RESERVATION ON OLD LINE' , 1 ) ;
7128        END IF;
7129 
7130        IF l_old_reserved_qty > 0 THEN
7131 
7132           IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
7133            THEN
7134               l_qty2_to_unreserve := NULL;
7135           END IF;
7136 
7137 
7138           Unreserve_Line
7139              (p_line_rec              => p_old_line_rec,
7140               p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
7141               p_quantity2_to_unreserve => l_qty2_to_unreserve, -- INVCONV
7142               x_return_status         => x_return_status);
7143 
7144           l_old_reserved_qty := 0;
7145           l_old_reserved_qty2 := 0; -- INVCONV
7146        END IF;
7147 
7148        IF  (p_x_line_rec.reserved_quantity is NOT NULL OR
7149             Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id)) --4689197
7150        AND (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
7151             -- BUG 1955004
7152             sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
7153             -- END 1955004
7154             sch_cached_sch_level_code is null)
7155        AND  l_old_reserved_qty = 0
7156        THEN
7157 
7158          IF l_debug_level  > 0 THEN
7159              oe_debug_pub.add(  'SUB: SETTING RESERVE LATER FLAG' , 3 ) ;
7160          END IF;
7161          x_reserve_later := 'Y';
7162        END IF;
7163 
7164        RETURN;
7165      END IF;
7166 
7167      IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
7168      THEN
7169 
7170        IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
7171                                p_old_line_rec.schedule_ship_date)
7172           AND OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id, -- 5178175
7173                                p_old_line_rec.ship_from_org_id)
7174        AND l_old_reserved_qty > 0
7175        AND p_x_line_rec.ordered_quantity > 0
7176        THEN
7177 
7178 
7179          l_reservation_rec.reservation_id := fnd_api.g_miss_num;
7180 
7181          l_sales_order_id
7182                      := Get_mtl_sales_order_id(p_old_line_rec.header_id);
7183          l_reservation_rec.demand_source_header_id  := l_sales_order_id;
7184          l_reservation_rec.demand_source_line_id    := p_old_line_rec.line_id;
7185          l_reservation_rec.organization_id  := p_old_line_rec.ship_from_org_id;
7186 
7187 
7188          IF l_debug_level  > 0 THEN
7189              oe_debug_pub.add(  'RSCH: CALLING INVS QUERY_RESERVATION ' , 1 ) ;
7190          END IF;
7191          inv_reservation_pub.query_reservation
7192            ( p_api_version_number       => 1.0
7193            , p_init_msg_lst              => fnd_api.g_true
7194            , x_return_status             => x_return_status
7195            , x_msg_count                 => l_msg_count
7196            , x_msg_data                  => l_msg_data
7197            , p_query_input               => l_reservation_rec
7198            , x_mtl_reservation_tbl       => l_rsv_tbl
7199            , x_mtl_reservation_tbl_count => l_count
7200            , x_error_code                => l_x_error_code
7201            , p_lock_records              => l_lock_records
7202            , p_sort_by_req_date          => l_sort_by_req_date
7203            );
7204 
7205                               IF l_debug_level  > 0 THEN
7206                                   oe_debug_pub.add(  'AFTER CALLING INVS QUERY_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
7207                               END IF;
7208 
7209          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7210              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7211          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7212              RAISE FND_API.G_EXC_ERROR;
7213          END IF;
7214 
7215                                        IF l_debug_level  > 0 THEN
7216                                            oe_debug_pub.add(  'RESERVATION RECORD COUNT IS: ' || L_RSV_TBL.COUNT , 1 ) ;
7217                                        END IF;
7218 
7219          -- Let's get the total reserved_quantity
7220          FOR K IN 1..l_rsv_tbl.count LOOP
7221 
7222           l_reservation_rec := l_rsv_tbl(K);
7223           l_reservation_rec.requirement_date := p_x_line_rec.schedule_ship_date;
7224 
7225           IF l_debug_level  > 0 THEN
7226               oe_debug_pub.add(  'RSCH: CALLING INVS UPDATE RESERVATION ' , 1 ) ;
7227           END IF;
7228           inv_reservation_pub.update_reservation
7229             ( p_api_version_number        => 1.0
7230             , p_init_msg_lst              => fnd_api.g_true
7231             , x_return_status             => x_return_status
7232             , x_msg_count                 => l_msg_count
7233             , x_msg_data                  => l_msg_data
7234             , p_original_rsv_rec          => l_rsv_tbl(k)
7235             , p_to_rsv_rec                => l_reservation_rec
7236             , p_original_serial_number    => l_dummy_sn -- no serial contorl
7237             , p_to_serial_number          => l_dummy_sn -- no serial control
7238             , p_validation_flag           => fnd_api.g_true
7239             , p_over_reservation_flag     => 2 -- 4715544
7240             );
7241 
7242                                 IF l_debug_level  > 0 THEN
7243                                     oe_debug_pub.add(  'AFTER CALLING INVS UPDATE_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
7244                                 END IF;
7245 
7246           IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7247                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7248           ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7249                  IF l_msg_data is not null THEN
7250                     fnd_message.set_encoded(l_msg_data);
7251                     l_buffer := fnd_message.get;
7252                     oe_msg_pub.add_text(p_message_text => l_buffer);
7253                     IF l_debug_level  > 0 THEN
7254                         oe_debug_pub.add(  'ERROR : '|| L_BUFFER , 1 ) ;
7255                     END IF;
7256                   END IF;
7257                   RAISE FND_API.G_EXC_ERROR;
7258           END IF;
7259          END LOOP;
7260 
7261        END IF; -- Ship date has changed
7262 
7263           -- End code for bug 2126165.
7264 
7265         -- Inv code expect the item to be available in database before
7266         -- making reservation. If the reservation is being made due to
7267         -- change in inventory_item_id, we will move the reservation
7268         -- call to post_write. -- 1913263.
7269 
7270         -- pre-write scheduling call has been moved to post write.
7271         -- we can perform reservation right here for item change.
7272 
7273     /*    IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
7274                                 p_old_line_rec.inventory_item_id)
7275         THEN
7276 
7277              oe_debug_pub.add('No re-reserve due to item change',1);
7278              RETURN;
7279         END IF;
7280   */
7281         -- 4316272
7282         IF  (nvl(p_x_line_rec.reserved_quantity, 0) > 0 OR
7283              Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date,
7284 				   p_x_line_rec.org_id))
7285         AND (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
7286             -- BUG 1955004
7287             sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
7288             -- END 1955004
7289              sch_cached_sch_level_code is null)
7290         AND  l_old_reserved_qty = 0
7291         THEN
7292 
7293        IF l_debug_level  > 0 THEN
7294            oe_debug_pub.add(  'RESERVING IN ACTION_RESCHEDULE' ) ;
7295        END IF;
7296 
7297           -- Old reservation does not exists on the line.
7298           -- create a new reservation.
7299           -- (Post Pack J)Create Reservation if within reservation time fence
7300           -- 4316272 Removed redundant condition.
7301           IF (nvl(p_x_line_rec.reserved_quantity,0) >
7302                  p_x_line_rec.ordered_quantity)
7303           OR nvl(p_x_line_rec.reserved_quantity,0) = 0
7304           OR (nvl(p_x_line_rec.reserved_quantity,0) <
7305                      p_x_line_rec.ordered_quantity
7306             AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG',p_x_line_rec.org_id) = 'Y') THEN
7307              l_qty_to_reserve := p_x_line_rec.ordered_quantity;
7308 
7309           ELSE
7310              l_qty_to_reserve := p_x_line_rec.reserved_quantity;
7311           END IF;
7312 
7313          -- Bug 6335352
7314          -- Added the below code
7315          IF NOT OE_GLOBALS.EQUAL(p_x_line_rec.order_quantity_uom, p_old_line_rec.order_quantity_uom) THEN
7316            IF p_x_line_rec.ordered_quantity > p_x_line_rec.reserved_quantity AND
7317               Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) THEN
7318                 l_qty_to_reserve := p_x_line_rec.ordered_quantity;
7319            ELSIF p_x_line_rec.ordered_quantity < p_x_line_rec.reserved_quantity THEN
7320                l_qty_to_reserve := p_x_line_rec.ordered_quantity;
7321            END IF;
7322          END IF;
7323 
7324       IF (nvl(p_x_line_rec.reserved_quantity2,0) >   -- INVCONV
7325                  nvl(p_x_line_rec.ordered_quantity2, 0) )
7326           OR nvl(p_x_line_rec.reserved_quantity2,0) = 0
7327           --OR (OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
7328           OR Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) --4689197
7329           THEN
7330              l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0);
7331           ELSE
7332              l_qty2_to_reserve := nvl(p_x_line_rec.reserved_quantity2, 0);
7333           END IF;
7334           IF l_qty2_to_reserve = 0 -- INVCONV
7335             THEN
7336             l_qty2_to_reserve := NULL;
7337           END IF;
7338 
7339           IF l_debug_level  > 0 THEN
7340            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
7341                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
7342        END IF;
7343 
7344 
7345           Reserve_Line
7346           ( p_line_rec             => p_x_line_rec
7347           , p_quantity_to_reserve  => l_qty_to_reserve
7348           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
7349           , x_return_Status        => x_return_status);
7350 
7351 
7352         ELSIF l_old_reserved_qty > 0 THEN
7353 
7354            --If ordered qty is not changed on the line, take care of the
7355            --reservation changes.
7356 
7357           IF  OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
7358                                p_old_line_rec.ordered_quantity)
7359           AND NOT OE_GLOBALS.Equal(p_x_line_rec.reserved_quantity,
7360                                    l_old_reserved_qty)
7361           THEN
7362              Action_reserve(p_x_line_rec    => p_x_line_rec,
7363                             p_old_line_rec  => p_old_line_rec,
7364                             x_return_status => x_return_status);
7365              -- reserved qty has changed during rescheduling process.
7366 
7367           ELSE -- Ordered qty has changed on the line.
7368 
7369 
7370            l_changed_reserved_qty   := l_old_reserved_qty -
7371                                        p_x_line_rec.reserved_quantity;
7372                  l_changed_reserved_qty2   := nvl(l_old_reserved_qty2, 0) -   -- INVCONV
7373                                        nvl(p_x_line_rec.reserved_quantity2,0);
7374            IF l_changed_reserved_qty2 = 0 -- INVCONV
7375             THEN
7376                  l_changed_reserved_qty2 := NULL;
7377            END IF;
7378 
7379            IF  p_x_line_rec.ordered_quantity  >=  p_x_line_rec.reserved_quantity
7380            THEN
7381             -- Ordered qty is greater than res qty. Take care of reservation
7382             -- changes if any.
7383 
7384             IF l_changed_reserved_qty > 0 THEN
7385 
7386                IF l_debug_level  > 0 THEN
7387                    oe_debug_pub.add(  'RESERVED QUANTITY HAS DECREASED' , 1 ) ;
7388                END IF;
7389 
7390                 -- No need to pass old record. Since this is a change
7391                 -- due to quantity.
7392                 Unreserve_Line
7393                 ( p_line_rec              => p_x_line_rec
7394                 , p_quantity_to_unreserve => l_changed_reserved_qty
7395                 , p_quantity2_to_unreserve => l_changed_reserved_qty2 -- INVCONV
7396                 , x_return_status         => x_return_status);
7397 
7398                 IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7399                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7400                 ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7401                    RAISE FND_API.G_EXC_ERROR;
7402                 END IF;
7403             /* -- Moved to the last part of If-elsif check
7404             -- Pack J
7405             -- Call reserve_line if partial flag is set to 'Yes' and
7406             --ordered quantity is greater than reserved quantity.
7407             ELSIF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
7408                  --AND OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
7409                  --                  p_old_line_rec.ordered_quantity)
7410                  AND p_x_line_rec.ordered_quantity >NVL(p_x_line_rec.reserved_quantity,0)
7411                  AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
7412                  AND Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date) THEN
7413                    l_qty_to_reserve :=
7414                       p_x_line_rec.ordered_quantity - NVL(p_x_line_rec.reserved_quantity,0);
7415                    l_qty2_to_reserve :=
7416                       nvl(p_x_line_rec.ordered_quantity2, 0) - NVL(p_x_line_rec.reserved_quantity2,0); -- INVCONV
7417                  -- Setting rsv_update flag to TRUE if reservation exists
7418                  IF NVL(p_x_line_rec.reserved_quantity,0) > 0 THEN
7419                  -- Going to update the reservation
7420                     l_rsv_update := TRUE;
7421                  END IF;
7422                  Reserve_Line
7423                   ( p_line_rec              => p_x_line_rec
7424                   , p_quantity_to_reserve   => l_qty_to_reserve
7425                   , p_quantity2_to_reserve   => l_qty2_to_reserve -- INVCONV
7426                   , p_rsv_update            => l_rsv_update
7427                   , x_return_status         => x_return_status);
7428             */
7429 
7430             ELSIF l_changed_reserved_qty < 0 THEN
7431 
7432               IF l_debug_level  > 0 THEN
7433                   oe_debug_pub.add(  'RESERVED QUANTITY HAS INCREASED' , 1 ) ;
7434               END IF;
7435 
7436               l_qty_to_reserve := p_x_line_rec.reserved_quantity -
7437                                   l_old_reserved_qty;
7438 
7439                             l_qty2_to_reserve := NVL(p_x_line_rec.reserved_quantity2,0) -   -- INVCONV
7440                                   nvl(l_old_reserved_qty2, 0);
7441 
7442               IF l_qty2_to_reserve = 0 THEN -- INVCONV
7443                 l_qty2_to_reserve := null;
7444               end if;
7445               IF l_debug_level  > 0 THEN
7446            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
7447                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
7448        END IF;
7449 
7450 
7451               Reserve_Line
7452               ( p_line_rec             => p_x_line_rec
7453               , p_quantity_to_reserve  => l_qty_to_reserve
7454               , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
7455               , p_rsv_update           => TRUE
7456               , x_return_Status        => x_return_status);
7457 
7458               IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7459                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7460               ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7461                   RAISE FND_API.G_EXC_ERROR;
7462               END IF;
7463 
7464             /* -- Begin Bug 2794760    -- INVCONV take out OPM code
7465             ELSIF   (OE_Line_Util.Process_Characteristics
7466                     (p_x_line_rec.inventory_item_id
7467                     ,p_x_line_rec.ship_from_org_id
7468                     ,l_item_rec) AND
7469                     p_x_line_rec.ordered_quantity > p_old_line_rec.ordered_quantity AND
7470                     l_changed_reserved_qty = 0 AND
7471                     (NVL(p_x_line_rec.reserved_quantity,0) > 0 OR
7472                      Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date)))  THEN
7473 
7474                     OE_DEBUG_PUB.ADD('OPM Only - Reserved Quantity should also Increase',1);
7475 
7476               l_qty_to_reserve := p_x_line_rec.ordered_quantity - l_old_reserved_qty;
7477 
7478               Reserve_Line
7479               ( p_line_rec             => p_x_line_rec
7480               , p_quantity_to_reserve  => l_qty_to_reserve
7481               , p_rsv_update           => TRUE
7482               , x_return_Status        => x_return_status);
7483 
7484               IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7485                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7486               ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7487                   RAISE FND_API.G_EXC_ERROR;
7488               END IF;
7489             -- END Bug 2794760   */
7490 
7491 
7492             -- Post Pack J
7493             -- Call reserve_line if ordered quantity is greater than reserved quantity.
7494             ELSIF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
7495                  --AND OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
7496                  --                  p_old_line_rec.ordered_quantity)
7497                  AND p_x_line_rec.ordered_quantity >NVL(p_x_line_rec.reserved_quantity,0)
7498                  --AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
7499                  AND Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) THEN --4689197
7500                    l_qty_to_reserve :=
7501                       p_x_line_rec.ordered_quantity - NVL(p_x_line_rec.reserved_quantity,0);
7502                 -- KYH BUG 4245418 BEGIN
7503                 -- =====================
7504                 -- l_qty2_to_reserve :=   -- INVCONV
7505                 --    nvl(p_x_line_rec.ordered_quantity2, 0) - NVL(p_x_line_rec.reserved_quantity2,0);
7506                 -- It is dangerous to compute secondary quantity to reserve based on
7507                 -- ordered_quantity2 minus reserved_quantity2 as above.
7508                 -- This is because ordered_quantity2 always reflects a standard conversion from ordered_quantity
7509                 -- whereas reserved_quantity2 may be the result of one or more lot specific calculations
7510                 -- Combining values from these different conversion rates may not give the correct result.
7511                 -- Better to compute the secondary to reserve by converting l_qty_to_reserve
7512                    IF p_x_line_rec.ordered_quantity_uom2 is not null and
7513                      p_x_line_rec.ordered_quantity_uom2 <> FND_API.G_MISS_CHAR THEN
7514                      -- Only invoke the conversion for dual tracked items
7515                      IF l_debug_level  > 0 THEN
7516                        oe_debug_pub.add(  'DUAL Tracked quantity so convert the qty to reserve ' || l_qty_to_reserve , 1 ) ;
7517                      END IF;
7518                      l_qty2_to_reserve    := inv_convert.inv_um_convert(
7519                                           item_id                      => p_x_line_rec.inventory_item_id
7520                                         , lot_number                   => NULL
7521                                         , organization_id              => p_x_line_rec.ship_from_org_id
7522                                         , PRECISION                    => 5
7523                                         , from_quantity                => l_qty_to_reserve
7524                                         , from_unit                    => p_x_line_rec.order_quantity_uom
7525                                         , to_unit                      => p_x_line_rec.ordered_quantity_uom2
7526                                         , from_name                    => NULL -- from uom name
7527                                         , to_name                      => NULL -- to uom name
7528                                         );
7529                      IF l_debug_level  > 0 THEN
7530                        oe_debug_pub.add(  'After UOM conversion the secondary to reserve is  ' || l_qty2_to_reserve , 1 ) ;
7531                      END IF;
7532 
7533                      IF l_qty2_to_reserve = -99999 THEN
7534                        -- conversion failed
7535                        FND_MESSAGE.SET_NAME('INV','INV_NO_CONVERSION_ERR'); -- INVCONV
7536                        OE_MSG_PUB.ADD;
7537                        IF l_debug_level  > 0 THEN
7538                          oe_debug_pub.add(  'ERROR on UOM conversion to Secondary UOM which is '||p_x_line_rec.ordered_quantity_uom2 , 1 ) ;
7539                        END IF;
7540                        RAISE FND_API.G_EXC_ERROR;
7541                      END IF;
7542                    END IF;
7543                 -- KYH BUG 4245418 END
7544 
7545                IF l_debug_level  > 0 THEN
7546                 oe_debug_pub.add(  'p_x_line_rec.ordered_quantity ' || p_x_line_rec.ordered_quantity , 2 ) ;
7547                                         oe_debug_pub.add(  'p_x_line_rec.reserved_quantity ' || p_x_line_rec.reserved_quantity , 2 ) ;
7548 
7549                          oe_debug_pub.add(  'p_x_line_rec.ordered_quantity2 ' || p_x_line_rec.ordered_quantity2 , 2 ) ;
7550                                         oe_debug_pub.add(  'p_x_line_rec.reserved_quantity2 ' || p_x_line_rec.reserved_quantity2 , 2 ) ;
7551                         END IF;
7552 
7553 
7554                    IF l_qty2_to_reserve = 0 THEN -- INVCONV
7555                             l_qty2_to_reserve := null;
7556                      end if;
7557 
7558 
7559                  -- Setting rsv_update flag to TRUE if reservation exists
7560                  IF NVL(p_x_line_rec.reserved_quantity,0) > 0 THEN
7561                  -- Going to update the reservation
7562                     l_rsv_update := TRUE;
7563                  END IF;
7564 
7565                  IF l_debug_level  > 0 THEN
7566            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
7567                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
7568        END IF;
7569 
7570 
7571 
7572                  Reserve_Line
7573                   ( p_line_rec              => p_x_line_rec
7574                   , p_quantity_to_reserve   => l_qty_to_reserve
7575                                     , p_quantity2_to_reserve   => l_qty2_to_reserve
7576                   , p_rsv_update            => l_rsv_update
7577                   , x_return_status         => x_return_status);
7578 
7579             END IF; -- end of reserved_quantity change code
7580 
7581            ELSE
7582 
7583             -- Order qty is less than res qty. That means, we are
7584             -- need to unreserve the extra qty. However, that extra res
7585             -- may not exists in reservation table.
7586             -- For example Orderes qty is getting updated as 10 to 9
7587             -- and reservation qty from 9 to 12. In this case we do not
7588             -- need any res changes. To deal this, check the old reservation
7589             -- with the new ordered qty.
7590 
7591             IF l_debug_level  > 0 THEN
7592                 oe_debug_pub.add(  'ORDERED QTY IS GREATER THAN RES QTY' , 1 ) ;
7593             END IF;
7594 
7595             l_qty_to_unreserve := l_old_reserved_qty -
7596                                   p_x_line_rec.ordered_quantity;
7597 
7598             l_qty2_to_unreserve := nvl(l_old_reserved_qty2, 0) -   -- INVCONV
7599                                   nvl(p_x_line_rec.ordered_quantity2, 0);
7600             IF l_qty2_to_unreserve = 0 THEN -- INVCONV
7601                 l_qty2_to_unreserve := null;
7602             end if;
7603             IF l_qty_to_unreserve > 0 THEN
7604                 Unreserve_Line
7605                ( p_line_rec              => p_x_line_rec
7606                , p_quantity_to_unreserve => l_qty_to_unreserve
7607                , p_quantity2_to_unreserve => l_qty2_to_unreserve -- INVCONV
7608                , x_return_status         => x_return_status);
7609 
7610                IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7611                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7612                ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7613                   RAISE FND_API.G_EXC_ERROR;
7614                END IF;
7615 
7616             END IF; -- reserved_quantity same, ordered_quantity changed
7617            END IF; -- Order qty > reserve qty.
7618           END IF; -- Order qty and res qty changes.
7619         END IF; -- Need reservation changes.
7620      END IF; -- Shippable flag.
7621 
7622 EXCEPTION
7623    WHEN FND_API.G_EXC_ERROR THEN
7624 
7625         x_return_status := FND_API.G_RET_STS_ERROR;
7626 
7627    WHEN OTHERS THEN
7628 
7629         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7630 
7631         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7632         THEN
7633             OE_MSG_PUB.Add_Exc_Msg
7634             (   G_PKG_NAME,
7635               'Action_Reschedule');
7636         END IF;
7637 END Action_Reschedule;
7638 
7639 /*-----------------------------------------------------------------------------
7640 Procedure Name : Load_INV_Request
7641 Description    : This procedure loads the INV's record structure which
7642                  we will pass to INV for reservation purpose.
7643                  We need to pass to INV the idenfier for OM demand.
7644                  We pass the constant INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE
7645                  for all OM Order Lines except Internal Orders.
7646                  For Internal Orders we pass
7647                  INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD
7648                  as the identifier.
7649 ----------------------------------------------------------------------------- */
7650 Procedure Load_INV_Request
7651 ( p_line_rec                 IN  Oe_Order_Pub.Line_Rec_Type
7652 , p_quantity_to_reserve      IN  NUMBER
7653 , p_quantity2_to_reserve      IN  NUMBER DEFAULT NULL
7654 , x_reservation_rec OUT NOCOPY Inv_Reservation_Global.Mtl_Reservation_Rec_Type)
7655 
7656 IS
7657 l_source_code          VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
7658 l_sales_order_id       NUMBER;
7659 l_subinventory         VARCHAR2(10);
7660 --
7661 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7662 
7663 l_quantity2_to_reserve NUMBER; --INVCONV
7664 --
7665 BEGIN
7666    IF l_debug_level  > 0 THEN
7667        oe_debug_pub.add(  'ENTERING LOAD INV REQUEST' , 1 ) ;
7668    END IF;
7669 
7670    x_reservation_rec                      := null;
7671    x_reservation_rec.reservation_id       := fnd_api.g_miss_num; -- cannot know
7672    x_reservation_rec.requirement_date     := p_line_rec.schedule_ship_date;
7673    x_reservation_rec.organization_id      := p_line_rec.ship_from_org_id;
7674    x_reservation_rec.inventory_item_id    := p_line_rec.inventory_item_id;
7675 
7676    IF p_line_rec.source_document_type_id = 10 THEN
7677 
7678       -- This is an internal order line. We need to give
7679       -- a different demand sourc2e type for these lines.
7680 
7681       x_reservation_rec.demand_source_type_id    :=
7682               INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD;
7683                                                  -- intenal order
7684 
7685    ELSE
7686 
7687       x_reservation_rec.demand_source_type_id    :=
7688              INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE; -- order entry
7689 
7690    END IF;
7691 
7692 
7693      -- Get demand_source_header_id from mtl_sales_orders
7694 
7695      l_sales_order_id := Get_mtl_sales_order_id(p_line_rec.HEADER_ID);
7696 
7697      IF l_debug_level  > 0 THEN
7698          oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 1 ) ;
7699      END IF;
7700 
7701      IF p_line_rec.subinventory = FND_API.G_MISS_CHAR THEN
7702         l_subinventory := NULL;
7703      ELSE
7704         l_subinventory := p_line_rec.subinventory;
7705      END IF;
7706 
7707      x_reservation_rec.demand_source_header_id := l_sales_order_id;
7708      x_reservation_rec.demand_source_line_id   := p_line_rec.line_id;
7709      x_reservation_rec.reservation_uom_code    := p_line_rec.order_quantity_uom;
7710      x_reservation_rec.reservation_quantity    := p_quantity_to_reserve;
7711      x_reservation_rec.supply_source_type_id   :=
7712              INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INV;
7713      x_reservation_rec.subinventory_code       := l_subinventory;
7714      /* OPM 02/JUN/00 send process attributes into the reservation -- INVCONV - no longer using attributes
7715      =============================================================
7716      x_reservation_rec.attribute1  := p_line_rec.preferred_grade;
7717      x_reservation_rec.secondary_reservation_quantity   := p_line_rec.ordered_quantity2;
7718      x_reservation_rec.secondary_uom_code    := p_line_rec.ordered_quantity_uom2;
7719      x_reservation_rec.attribute3  := p_line_rec.ordered_quantity_uom2;
7720 
7721       OPM 02/JUN/00 END
7722     ====================*/
7723     --- diff between  x_reservation_rec.reservation_quantity    and x_reservation_rec.primary_reservation_quantity
7724     -- INVCONV
7725 
7726       IF p_quantity2_to_reserve = 0 -- INVCONV
7727         then
7728          l_quantity2_to_reserve := null;
7729        ELSE
7730 
7731         l_quantity2_to_reserve := p_quantity2_to_reserve;
7732        END IF;
7733 
7734 
7735 
7736      x_reservation_rec.secondary_reservation_quantity   := l_quantity2_to_reserve;
7737      x_reservation_rec.secondary_uom_code               := p_line_rec.ordered_quantity_uom2;
7738      --4653097
7739      IF p_line_rec.project_id IS NOT NULL THEN
7740         x_reservation_rec.project_id := p_line_rec.project_id;
7741      END IF;
7742      IF p_line_rec.task_id IS NOT NULL THEN
7743         x_reservation_rec.task_id := p_line_rec.task_id;
7744      END IF;
7745 
7746      IF l_debug_level  > 0 THEN
7747          oe_debug_pub.add(  'p_quantity2_to_reserve = ' || p_quantity2_to_reserve , 1 ) ;
7748          oe_debug_pub.add(  'p_line_rec.ordered_quantity_uom2 = ' || p_line_rec.ordered_quantity_uom2 , 1 ) ;
7749      END IF;
7750 
7751 
7752    IF l_debug_level  > 0 THEN
7753        oe_debug_pub.add(  'EXITING LOAD INV REQUEST' , 1 ) ;
7754    END IF;
7755 EXCEPTION
7756 
7757    WHEN NO_DATA_FOUND THEN
7758        IF l_debug_level  > 0 THEN
7759            oe_debug_pub.add(  'WHEN OTHERS OF LOAD INV REQUEST' , 1 ) ;
7760        END IF;
7761        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7762 
7763 END Load_INV_Request;
7764 
7765 PROCEDURE Set_Auto_sch_flag_for_batch(p_header_id IN NUMBER)
7766 IS
7767 --
7768 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7769 --
7770 BEGIN
7771                                         IF l_debug_level  > 0 THEN
7772                                             oe_debug_pub.add(  'ENTERING PROCEDURE SET_AUTO_SCH_FLAG_FOR_BATCH: ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
7773                                         END IF;
7774 
7775     IF OESCH_AUTO_SCHEDULE_PROFILE = 'Y' THEN
7776        Set_Auto_Sch_Flag('Y');
7777     ELSE
7778      IF l_debug_level  > 0 THEN
7779          oe_debug_pub.add(  'LOAD HEADER : ' || P_HEADER_ID , 2 ) ;
7780      END IF;
7781      OE_Order_Cache.Load_Order_Header(p_header_id);
7782      OE_ORDER_CACHE.Load_order_type(OE_ORDER_CACHE.g_header_rec.order_type_id);
7783                      IF l_debug_level  > 0 THEN
7784                          oe_debug_pub.add(  'ORDER TYPE ID : ' || OE_ORDER_CACHE.G_ORDER_TYPE_REC.ORDER_TYPE_ID , 2 ) ;
7785                      END IF;
7786      IF nvl(OE_ORDER_CACHE.g_order_type_rec.auto_scheduling_flag,'N') = 'Y' THEN
7787        Set_Auto_Sch_Flag('Y');
7788      ELSE
7789        Set_Auto_Sch_Flag('N');
7790      END IF;
7791     END IF;
7792 
7793                                 IF l_debug_level  > 0 THEN
7794                                     oe_debug_pub.add(  'EXITING PROCEDURE SET_AUTO_SCH_FLAG_FOR_BATCH: ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
7795                                 END IF;
7796 END Set_Auto_sch_flag_for_batch;
7797 FUNCTION Get_Scheduling_Level( p_header_id IN NUMBER,
7798                                p_line_type_id IN NUMBER)
7799 RETURN VARCHAR2
7800 IS
7801 l_scheduling_level_code  VARCHAR2(30) := null;
7802 l_line_type              VARCHAR2(80) := null;
7803 l_order_type             VARCHAR2(80) := null;
7804 --
7805 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7806 --
7807 BEGIN
7808 
7809   IF l_debug_level  > 0 THEN
7810       oe_debug_pub.add(  'ENTERING GET_SCHEDULING_LEVEL: ' || P_HEADER_ID||'/'||P_LINE_TYPE_ID , 1 ) ;
7811   END IF;
7812 
7813   IF p_line_type_id = sch_cached_line_type_id
7814   THEN
7815 
7816      sch_cached_sch_level_code := sch_cached_sch_level_code_line;
7817      RETURN sch_cached_sch_level_code;
7818 
7819   END IF;
7820 
7821   IF p_line_type_id IS NOT NULL THEN
7822    SELECT name, scheduling_level_code
7823    INTO   l_line_type,l_scheduling_level_code
7824    FROM   oe_transaction_types
7825    WHERE  transaction_type_id = p_line_type_id AND
7826             transaction_type_code = 'LINE';
7827   END IF;
7828 
7829   IF  l_scheduling_level_code IS NOT NULL THEN
7830 
7831       sch_cached_line_type_id   := p_line_type_id;
7832       sch_cached_sch_level_code := l_scheduling_level_code;
7833       sch_cached_sch_level_code_line := l_scheduling_level_code;
7834       sch_cached_line_type      := l_line_type;
7835       RETURN l_scheduling_level_code;
7836 
7837   END IF;
7838 
7839   IF p_header_id = sch_cached_header_id
7840   THEN
7841 
7842      sch_cached_sch_level_code := sch_cached_sch_level_code_head;
7843      RETURN sch_cached_sch_level_code;
7844 
7845   END IF;
7846   SELECT /* MOAC_SQL_CHANGE*/ name, scheduling_level_code
7847   INTO   l_order_type,l_scheduling_level_code
7848   FROM   oe_order_types_v ot, oe_order_headers_all h
7849   WHERE  h.header_id     = p_header_id AND
7850          h.order_type_id =  ot.order_type_id;
7851 
7852   sch_cached_header_id      := p_header_id;
7853   sch_cached_sch_level_code := l_scheduling_level_code;
7854   sch_cached_sch_level_code_head := l_scheduling_level_code;
7855   sch_cached_order_type     := l_order_type;
7856 
7857   IF l_debug_level  > 0 THEN
7858       oe_debug_pub.add(  'EXITING GET_SCHEDULING_LEVEL' , 1 ) ;
7859   END IF;
7860   RETURN l_scheduling_level_code;
7861 
7862 EXCEPTION
7863    WHEN NO_DATA_FOUND THEN
7864         IF l_debug_level  > 0 THEN
7865             oe_debug_pub.add(  'NO DATA FOUND GET_SCHEDULING_LEVEL' , 1 ) ;
7866         END IF;
7867         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7868 
7869 END Get_Scheduling_Level;
7870 
7871 
7872 /*---------------------------------------------------------------
7873 Handle_External_Lines
7874 ----------------------------------------------------------------*/
7875 PROCEDURE Handle_External_Lines
7876 ( p_x_line_rec  IN OUT NOCOPY   OE_ORDER_PUB.line_rec_type)
7877 IS
7878 
7879   l_line_tbl              OE_ORDER_PUB.line_tbl_type;
7880   l_old_line_tbl          OE_ORDER_PUB.line_tbl_type;
7881   l_line_rec              OE_ORDER_PUB.line_rec_type
7882                           := OE_Order_Pub.G_MISS_LINE_REC;
7883   l_control_rec           OE_GLOBALS.control_rec_type;
7884   l_index                 NUMBER;
7885   l_return_status         VARCHAR2(1);
7886 
7887   CURSOR ato_options IS
7888   SELECT line_id
7889   FROM   oe_order_lines
7890   WHERE  top_model_line_id = p_x_line_rec.top_model_line_id
7891   AND    ato_line_id       = p_x_line_rec.ato_line_id
7892   AND    open_flag = 'Y'
7893   AND    source_type_code = 'EXTERNAL';
7894 
7895 --
7896 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7897 --
7898 BEGIN
7899 
7900   IF l_debug_level  > 0 THEN
7901       oe_debug_pub.add(  'UPDATE SHIP DATE , EXTERNAL '|| P_X_LINE_REC.LINE_ID , 1 ) ;
7902   END IF;
7903 
7904   IF p_x_line_rec.ship_from_org_id is NULL THEN
7905     FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATO_WHSE_REQD');
7906     OE_MSG_PUB.Add;
7907     RAISE FND_API.G_EXC_ERROR;
7908   END IF;
7909 
7910   IF  p_x_line_rec.schedule_ship_date is NULL THEN
7911     p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
7912   END IF;
7913 
7914   IF l_debug_level  > 0 THEN
7915       oe_debug_pub.add(  'SHIP DATE '|| P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
7916   END IF;
7917 
7918   l_index := 0;
7919   l_line_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
7920 
7921   IF p_x_line_rec.ato_line_id is NULL OR
7922      p_x_line_rec.top_model_line_id is NULL
7923   THEN
7924 
7925     l_index := l_index + 1;
7926 
7927     l_line_rec.line_id            := p_x_line_rec.line_id;
7928     l_line_rec.schedule_ship_date := p_x_line_rec.schedule_ship_date;
7929 
7930     l_line_tbl(l_index) := l_line_rec;
7931   ELSE
7932 
7933     FOR ato_rec in ato_options
7934     LOOP
7935 
7936       l_index := l_index + 1;
7937       l_line_rec.line_id            := ato_rec.line_id;
7938       l_line_rec.schedule_ship_date := p_x_line_rec.schedule_ship_date;
7939 
7940       l_line_tbl(l_index) := l_line_rec;
7941 
7942     END LOOP;
7943 
7944     IF l_debug_level  > 0 THEN
7945         oe_debug_pub.add(  'ATO EXTERNAL '|| P_X_LINE_REC.ATO_LINE_ID , 1 ) ;
7946     END IF;
7947   END IF;
7948 
7949 
7950   OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
7951   OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
7952 
7953   IF l_debug_level  > 0 THEN
7954       oe_debug_pub.add(  'CALLING PROCESS ORDER' , 1 ) ;
7955   END IF;
7956 
7957   OE_Order_PVT.Lines
7958   ( p_validation_level            => FND_API.G_VALID_LEVEL_NONE
7959    ,p_control_rec                 => l_control_rec
7960    ,p_x_line_tbl                  => l_line_tbl
7961    ,p_x_old_line_tbl              => l_old_line_tbl
7962    ,x_return_status               => l_return_status);
7963 
7964    IF l_debug_level  > 0 THEN
7965        oe_debug_pub.add(  'SCH RETURN_STATUS IS ' || L_RETURN_STATUS , 1 ) ;
7966    END IF;
7967 
7968    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7969      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7970    ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
7971      RAISE FND_API.G_EXC_ERROR;
7972    END IF;
7973 
7974    OE_Order_PVT.Process_Requests_And_Notify
7975    ( p_process_requests    => TRUE
7976     ,p_notify              => TRUE
7977     ,p_line_tbl            => l_line_tbl
7978     ,p_old_line_tbl        => l_old_line_tbl
7979     ,x_return_status       => l_return_status);
7980 
7981    IF l_debug_level  > 0 THEN
7982        oe_debug_pub.add(  '1 SCH RETURN_STATUS IS ' || L_RETURN_STATUS , 1 ) ;
7983    END IF;
7984 
7985    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7986      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7987    ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
7988      RAISE FND_API.G_EXC_ERROR;
7989    END IF;
7990 
7991    OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
7992    IF l_debug_level  > 0 THEN
7993        oe_debug_pub.add(  'UI CASCADE FLAG SET TO TRUE' , 4 ) ;
7994    END IF;
7995 
7996    OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
7997    OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
7998 
7999 EXCEPTION
8000   WHEN OTHERS THEN
8001     IF l_debug_level  > 0 THEN
8002         oe_debug_pub.add(  'HANDLE_EXTERNAL_LINES ERROR '|| SQLERRM , 1 ) ;
8003     END IF;
8004     RAISE;
8005 END Handle_External_Lines;
8006 
8007 
8008 /*----------------------------------------------------------------
8009 PROCEDURE Schedule_line
8010 Description:
8011   This procedure will be called from
8012    oe_line_util.pre_write
8013    **
8014    **
8015 
8016   Parameter:
8017   p_caller         => Internal/External.
8018 
8019   If the call is being made by process order then it is called as
8020   internal. In all other cases it is external.
8021 
8022   If the value is set to True that means (oe_line_util) it is a
8023   recursive call and pass appropriate value to process and notify.
8024 
8025   This will be set to external from workflow and grp call. If it is set
8026   external execute request then and there.
8027 
8028   The procedure checks if we need to perform any scheduling action
8029   and then branches the code acoording to the type of sch request.
8030 
8031   Added new call to Handle_External_Lines for config dropshipments.
8032 -----------------------------------------------------------------*/
8033 Procedure Schedule_line
8034 ( p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type,
8035   p_x_line_rec         IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
8036   p_caller             IN  VARCHAR2 := SCH_INTERNAL,
8037 x_return_status OUT NOCOPY VARCHAR2 )
8038 
8039 IS
8040   l_old_line_rec         OE_ORDER_PUB.line_rec_type;
8041   l_sales_order_id       NUMBER;
8042   l_need_sch             BOOLEAN;
8043   l_line_action          VARCHAR2(30) := Null;
8044   l_auto_sch             VARCHAR2(1)  := Null;
8045   l_old_rsv_qty          NUMBER;
8046   l_old_rsv_qty2          NUMBER; -- INVCONV
8047   l_index                NUMBER;
8048   --
8049   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
8050   --
8051 
8052 
8053 BEGIN
8054 
8055    Print_Time('Entering ** OE_SCHEDULE_UTIL.Schedule_Line ');
8056    IF l_debug_level  > 0 THEN
8057       oe_debug_pub.add('---- Old Record ---- ',1);
8058       OE_DEBUG_PUB.ADD ( 'LINE ID : '||P_OLD_LINE_REC.LINE_ID , 1 ) ;
8059       oe_debug_pub.add(  'ATO LINE ID : '||P_OLD_LINE_REC.ATO_LINE_ID , 1 ) ;
8060       oe_debug_pub.add(  'ORDERED QTY : '||P_OLD_LINE_REC.ORDERED_QUANTITY , 1 ) ;
8061       oe_debug_pub.add(  'SHIP FROM : '||P_OLD_LINE_REC.SHIP_FROM_ORG_ID , 1 ) ;
8062       oe_debug_pub.add(  'SUBINVENTORY : '||P_OLD_LINE_REC.SUBINVENTORY , 1 ) ;
8063       oe_debug_pub.add(  'SCH SHIP DATE: '||P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
8064       oe_debug_pub.add(  'SCH ARR DATE : '||P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
8065       oe_debug_pub.add(  'SHIP SET ID : '||P_OLD_LINE_REC.SHIP_SET_ID , 1 ) ;
8066       oe_debug_pub.add(  'ARR SET ID : '||P_OLD_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
8067       oe_debug_pub.add(  'ACTION : '||P_OLD_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
8068       oe_debug_pub.add(  'STATUS : '||P_OLD_LINE_REC.SCHEDULE_STATUS_CODE , 1 ) ;
8069       oe_debug_pub.add(  'RES QUANTITY : '||P_OLD_LINE_REC.RESERVED_QUANTITY , 1 ) ;
8070       oe_debug_pub.add(  'RES QUANTITY2 : '||P_OLD_LINE_REC.RESERVED_QUANTITY2 , 1 ) ;
8071       oe_debug_pub.add(  'OVERRIDE ATP : '||P_OLD_LINE_REC.OVERRIDE_ATP_DATE_CODE , 1 ) ;
8072       oe_debug_pub.add(  ' ' , 1 ) ;
8073       oe_debug_pub.add('---- New Record ----',1);
8074       OE_DEBUG_PUB.ADD ( 'LINE ID : '||P_X_LINE_REC.LINE_ID , 1 ) ;
8075       oe_debug_pub.add(  'ATO LINE ID : '||P_X_LINE_REC.ATO_LINE_ID , 1 ) ;
8076       oe_debug_pub.add(  'ORDERED QTY : '||P_X_LINE_REC.ORDERED_QUANTITY , 1 ) ;
8077       oe_debug_pub.add(  'SHIP FROM : '||P_X_LINE_REC.SHIP_FROM_ORG_ID , 1 ) ;
8078       oe_debug_pub.add(  'SUBINVENTORY : '||P_X_LINE_REC.SUBINVENTORY , 1 ) ;
8079       oe_debug_pub.add(  'SCH SHIP DATE: '||P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
8080       oe_debug_pub.add(  'SCH ARR DATE : '||P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
8081       oe_debug_pub.add(  'SHIP SET ID : '||P_X_LINE_REC.SHIP_SET_ID , 1 ) ;
8082       oe_debug_pub.add(  'ARR SET ID : '||P_X_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
8083       oe_debug_pub.add(  'ACTION : '||P_X_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
8084       oe_debug_pub.add(  'STATUS : '||P_X_LINE_REC.SCHEDULE_STATUS_CODE , 1 ) ;
8085       oe_debug_pub.add(  'RES QTY : '||P_X_LINE_REC.RESERVED_QUANTITY , 1 ) ;
8086       oe_debug_pub.add(  'RES QTY2 : '||P_X_LINE_REC.RESERVED_QUANTITY2 , 1 ) ;
8087       oe_debug_pub.add(  'OPERATION : '||P_X_LINE_REC.OPERATION , 1 ) ;
8088       oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG : '||OESCH_AUTO_SCH_FLAG , 1 ) ;
8089       oe_debug_pub.add(  'OVERRIDE ATP : '||P_X_LINE_REC.OVERRIDE_ATP_DATE_CODE , 1 ) ;
8090       oe_debug_pub.add(  ' ' , 1 ) ;
8091    END IF;
8092 
8093    x_return_status := FND_API.G_RET_STS_SUCCESS;
8094 
8095    l_old_line_rec := p_old_line_rec;
8096 
8097    IF p_x_line_rec.ship_from_org_id = FND_API.G_MISS_NUM THEN
8098       p_x_line_rec.ship_from_org_id := null;
8099    END IF;
8100    IF  p_old_line_rec.ship_from_org_id = FND_API.G_MISS_NUM THEN
8101       l_old_line_rec.ship_from_org_id := null;
8102    END IF;
8103 
8104 
8105    IF p_x_line_rec.schedule_status_code is not null THEN
8106 
8107       l_sales_order_id := Get_mtl_sales_order_id(p_x_line_rec.HEADER_ID);
8108 
8109       IF l_old_line_rec.reserved_quantity is null THEN
8110          IF l_debug_level  > 0 THEN
8111             oe_debug_pub.add(  'RR1: L_OLD_LINE_REC.RESERVED_QUANTITY IS NULL' , 1 ) ;
8112          END IF;
8113       ELSIF l_old_line_rec.reserved_quantity = FND_API.G_MISS_NUM THEN
8114          IF l_debug_level  > 0 THEN
8115             oe_debug_pub.add(  'RR2: L_OLD_LINE_REC.RESERVED_QUANTITY IS MISSING' , 1 ) ;
8116          END IF;
8117       END IF;
8118 
8119       --l_old_line_rec.reserved_quantity :=
8120       --     OE_LINE_UTIL.Get_Reserved_Quantity
8121       --     (p_header_id   => l_sales_order_id,
8122       --      p_line_id     => p_x_line_rec.line_id,
8123       --      p_org_id      => p_x_line_rec.ship_from_org_id);
8124 
8125       -- INVCONV
8126 
8127       IF l_old_line_rec.reserved_quantity2 is null THEN
8128          IF l_debug_level  > 0 THEN
8129             oe_debug_pub.add(  'RR1: L_OLD_LINE_REC.RESERVED_QUANTITY2 IS NULL' , 1 ) ;
8130          END IF;
8131       ELSIF l_old_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM THEN
8132          IF l_debug_level  > 0 THEN
8133             oe_debug_pub.add(  'RR2: L_OLD_LINE_REC.RESERVED_QUANTITY2 IS MISSING' , 1 ) ;
8134          END IF;
8135       END IF;
8136 
8137       --l_old_line_rec.reserved_quantity2 :=
8138       --    OE_LINE_UTIL.Get_Reserved_Quantity2
8139       --    (p_header_id   => l_sales_order_id,
8140       --     p_line_id     => p_x_line_rec.line_id,
8141       --     p_org_id      => p_x_line_rec.ship_from_org_id);
8142 
8143       -- INVCONV - MERGED CALLS     FOR OE_LINE_UTIL.Get_Reserved_Quantity and OE_LINE_UTIL.Get_Reserved_Quantity2
8144 
8145       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
8146                                            ,p_line_id   => p_x_line_rec.line_id
8147                                            --Bug 6335352  ,p_org_id    => p_x_line_rec.ship_from_org_id
8148                                            ,p_org_id    => l_old_line_rec.ship_from_org_id
8149                                            ,p_order_quantity_uom => l_old_line_rec.order_quantity_uom
8150                                            ,x_reserved_quantity =>  l_old_line_rec.reserved_quantity
8151                                            ,x_reserved_quantity2 => l_old_line_rec.reserved_quantity2
8152                                            );
8153 
8154 
8155    ELSE
8156       l_old_line_rec.reserved_quantity := null;
8157       l_old_line_rec.reserved_quantity2 := null; -- INVCONV
8158    END IF;
8159 
8160 
8161    IF l_old_line_rec.reserved_quantity = 0
8162    THEN
8163       -- Currently setting the reserved quantity to null if it is zero.
8164       l_old_line_rec.reserved_quantity := null;
8165    END IF;
8166 
8167    IF l_old_line_rec.reserved_quantity2 = 0 -- INVCONV
8168    THEN
8169       -- Currently setting the reserved quantity2 to null if it is zero.
8170       l_old_line_rec.reserved_quantity2 := null;
8171    END IF;
8172 
8173    --By Achaubey for Bug 3239619/3362461/3456052
8174    -- Old reserve qty quired based on old UOM. Convert it if old UOM is not same as new UOM
8175    --Bug 6335352
8176    --This conversion is not required now, as we are passing old_line_rec.order_quantity_uon to Get_Reserved_Quantities API
8177    /*
8178    IF l_old_line_rec.reserved_quantity > 0  THEN
8179       l_old_rsv_qty := l_old_line_rec.reserved_quantity; -- This qty is as per old UOM
8180       IF NOT OE_GLOBALS.Equal(l_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom) THEN
8181          l_old_rsv_qty := INV_CONVERT.INV_UM_CONVERT(item_id      =>l_old_line_rec.inventory_item_id,
8182                                                      precision    =>5,
8183                                                      from_quantity=>l_old_line_rec.reserved_quantity,
8184                                                      from_unit    => l_old_line_rec.order_quantity_uom,    --old uom
8185                                                      to_unit      => p_x_line_rec.order_quantity_uom,  --new uom
8186                                                      from_name    => NULL,
8187                                                      to_name      => NULL
8188                                                      );
8189       END IF;
8190    END IF;
8191    */
8192 
8193   -- Bug 6335352 starts
8194   IF p_x_line_rec.reserved_quantity = FND_API.G_MISS_NUM
8195   THEN
8196      -- Converting missing to old value
8197      IF NOT OE_GLOBALS.Equal(l_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom)
8198      AND nvl(l_old_line_rec.reserved_quantity, 0) > 0
8199      THEN
8200               p_x_line_rec.reserved_quantity := INV_CONVERT.INV_UM_CONVERT
8201                                                 (item_id =>l_old_line_rec.inventory_item_id,
8202                                                  precision =>5,
8203                                                  from_quantity=>l_old_line_rec.reserved_quantity,
8204                                                  from_unit => l_old_line_rec.order_quantity_uom,    --old uom
8205                                                  to_unit => p_x_line_rec.order_quantity_uom,  --new uom
8206                                                  from_name => NULL,
8207                                                  to_name => NULL
8208                                                 );
8209      ELSE
8210        -- p_x_line_rec.reserved_quantity := l_old_rsv_qty;
8211        p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
8212      END IF;
8213   END IF;
8214   -- Bug 6335352 ends
8215 
8216    IF l_old_line_rec.reserved_quantity2 > 0  THEN -- INVCONV
8217       l_old_rsv_qty2 := l_old_line_rec.reserved_quantity2; -- INVCONV
8218    END IF;
8219 
8220    IF p_x_line_rec.reserved_quantity = FND_API.G_MISS_NUM
8221    THEN
8222       -- Converting missing to old value
8223       p_x_line_rec.reserved_quantity := l_old_rsv_qty;
8224       -- p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
8225    END IF;
8226 
8227    IF p_x_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM -- INVCONV
8228    THEN
8229       -- Converting missing to old value
8230       p_x_line_rec.reserved_quantity2 := l_old_rsv_qty2;
8231       -- p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
8232    END IF;
8233 
8234 
8235    IF l_debug_level  > 0 THEN
8236       oe_debug_pub.add(  'OLD RES QTY :' || L_OLD_LINE_REC.RESERVED_QUANTITY , 1 ) ;
8237    END IF;
8238    IF l_debug_level  > 0 THEN
8239       oe_debug_pub.add(  'NEW RES QTY :' || P_X_LINE_REC.RESERVED_QUANTITY , 1 ) ;
8240    END IF;
8241 
8242    ------------ reserved qty and miss num handling done ----------
8243 
8244    IF p_x_line_rec.source_type_code = 'EXTERNAL' AND
8245       p_x_line_rec.schedule_ship_date is NOT NULL AND
8246       NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
8247                            p_old_line_rec.schedule_ship_date) AND
8248       p_x_line_rec.ato_line_id is NOT NULL
8249    THEN
8250       Handle_External_Lines
8251          (p_x_line_rec   => p_x_line_rec);
8252    END IF;
8253 
8254    -- Item substitution code
8255    -- Clear original attributes when item is changed by user
8256    -- on a scheduled line. On an unscheduled substituted item
8257    -- original item will cleared in pre-write code.
8258 
8259    IF  NOT OE_GLOBALS.Equal(p_x_line_rec.Inventory_Item_Id,
8260                             p_old_line_rec.Inventory_Item_Id)
8261       AND p_x_line_rec.schedule_status_code is not null
8262       AND p_x_line_rec.item_relationship_type is null
8263    THEN
8264       oe_debug_pub.add('SL: clearing out the original item fields') ;
8265       p_x_line_rec.Original_Inventory_Item_Id    := Null;
8266       p_x_line_rec.Original_item_identifier_Type := Null;
8267       p_x_line_rec.Original_ordered_item_id      := Null;
8268       p_x_line_rec.Original_ordered_item         := Null;
8269 
8270 
8271    END IF;
8272 
8273    -- Set auto scheduling flag for batch calls.
8274 
8275 
8276    IF OE_GLOBALS.G_UI_FLAG THEN
8277 
8278       IF l_debug_level  > 0 THEN
8279          oe_debug_pub.add(  'UI IS SET' , 1 ) ;
8280       END IF;
8281    ELSE
8282 
8283       IF l_debug_level  > 0 THEN
8284          oe_debug_pub.add(  'UI IS NOT SET' , 1 ) ;
8285       END IF;
8286    END IF;
8287 
8288    IF l_debug_level  > 0 THEN
8289       oe_debug_pub.add(  'SOURCE :' || P_X_LINE_REC.SOURCE_DOCUMENT_TYPE_ID , 1 ) ;
8290    END IF;
8291    IF l_debug_level  > 0 THEN
8292       oe_debug_pub.add(  'OPR :' || P_X_LINE_REC.OPERATION , 1 ) ;
8293    END IF;
8294 
8295    IF  NOT OE_GLOBALS.G_UI_FLAG
8296       AND NOT (nvl(p_x_line_rec.source_document_type_id,-99) = 2)
8297       AND OE_CONFIG_UTIL.G_CONFIG_UI_USED = 'N'
8298       -- QUOTING change - set auto scheduling flag for complete negotiation step
8299       AND (p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
8300            OR OE_Quote_Util.G_COMPLETE_NEG = 'Y'
8301            )
8302    THEN --2998550 added check for G_CONFIG_UI_USED to set flag for batch
8303 
8304       IF l_debug_level  > 0 THEN
8305          oe_debug_pub.add(  'BEFORE SETTING AUTO SCHEDULING FLAG FOR BATCH' , 1 ) ;
8306       END IF;
8307 
8308       Set_Auto_sch_flag_for_batch(p_x_line_rec.header_id);
8309 
8310    ELSE
8311       IF l_debug_level  > 0 THEN
8312          oe_debug_pub.add(  'ELSE SETTING AUTO SCHEDULING FLAG FOR' , 1 ) ;
8313       END IF;
8314    END IF;
8315 
8316    -- 4026758
8317    IF (NOT OE_GLOBALS.Equal(p_x_line_rec.ship_set_id,
8318                             p_old_line_rec.ship_set_id)
8319        AND p_old_line_rec.ship_set_id IS NOT NULL)
8320       OR (NOT OE_GLOBALS.Equal(p_x_line_rec.arrival_set_id,
8321                                p_old_line_rec.arrival_set_id)
8322           AND p_old_line_rec.arrival_set_id IS NOT NULL)
8323       OR ( ( p_x_line_rec.arrival_set_id is not null
8324              OR p_x_line_rec.ship_set_id is not null)
8325            AND p_x_line_rec.ordered_quantity = 0) THEN
8326       -- Line is being removed from  set.
8327 
8328 
8329    --bug5631508
8330     --Record the set history before deleting it
8331 
8332     OE_AUDIT_HISTORY_PVT.RECORD_SET_HISTORY(p_header_id   => p_x_line_rec.header_id,
8333 					    p_line_id     => p_x_line_rec.line_id,
8334 					    p_set_id      => nvl(l_old_line_rec.ship_set_id,l_old_line_rec.arrival_set_id),
8335 					    x_return_status => x_return_status);
8336 
8337     IF l_debug_level  > 0 THEN
8338          oe_debug_pub.add(  'AFTER Inserting data in OE_SETS_HISTORY table ' || x_return_status , 1 ) ;
8339     END IF;
8340 
8341       Log_Delete_Set_Request
8342          (p_header_id   => p_x_line_rec.header_id,
8343           p_line_id     => p_x_line_rec.line_id,
8344           p_set_id      => nvl(l_old_line_rec.ship_set_id,l_old_line_rec.arrival_set_id),
8345           x_return_status => x_return_status);
8346       IF l_debug_level  > 0 THEN
8347          oe_debug_pub.add(  'AFTER LOGGING DELETE SETS DELAYED REQUEST IN SCHEDULE LINE' || X_RETURN_STATUS , 1 ) ;
8348       END IF;
8349    END IF;
8350 
8351    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
8352       AND   NOT OE_GLOBALS.Equal(l_old_line_rec.firm_demand_flag,
8353                                  p_x_line_rec.firm_demand_flag)
8354       AND  p_x_line_rec.operation = 'UPDATE' THEN
8355 
8356       IF p_x_line_rec.ship_model_complete_flag = 'Y'
8357       THEN
8358          Update oe_order_lines_all
8359             Set firm_demand_flag = p_x_line_rec.firm_demand_flag
8360             Where top_model_line_id = p_x_line_rec.top_model_line_id;
8361 
8362          OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
8363       ELSIF p_x_line_rec.ato_line_id is not null
8364             AND   NOT(p_x_line_rec.ato_line_id =p_x_line_rec.line_id
8365                       AND   p_x_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
8366                                                             OE_GLOBALS.G_ITEM_OPTION))
8367       THEN
8368 
8369          Update oe_order_lines_all
8370             Set firm_demand_flag = p_x_line_rec.firm_demand_flag
8371             Where ato_line_id = p_x_line_rec.ato_line_id;
8372 
8373          OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
8374       END IF;
8375    END IF; -- Code
8376 
8377 
8378    l_need_sch :=  Need_Scheduling(p_line_rec         => p_x_line_rec,
8379                                   p_old_line_rec     => l_old_line_rec,
8380                                   x_line_action      => l_line_action,
8381                                   x_auto_sch         => l_auto_sch);
8382 
8383    IF not(l_need_sch) THEN
8384 
8385       IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_SHIP_METHOD_FLAG'),'Y')  = 'N'
8386          AND NOT OE_GLOBALS.Equal(p_x_line_rec.shipping_method_code,
8387                                   l_old_line_rec.shipping_method_code)
8388          AND fnd_profile.value('ONT_SHIP_METHOD_FOR_SHIP_SET') = 'Y'
8389       THEN
8390 
8391          IF l_debug_level  > 0 THEN
8392             oe_debug_pub.add(  'SHIPPING_METHOD CHANGED , CASCADE' , 4 ) ;
8393          END IF;
8394 
8395          oe_delayed_requests_pvt.log_request(
8396                                              p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
8397                                              p_entity_id              => p_x_line_rec.line_id,
8398                                              p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
8399                                              p_requesting_entity_id   => p_x_line_rec.line_id,
8400                                              p_request_type           => OE_GLOBALS.G_CASCADE_SHIP_SET_ATTR,
8401                                              p_param1                 => p_x_line_rec.header_id,
8402                                              p_param2                 => p_x_line_rec.ship_set_id,
8403                                              p_param3                 => p_x_line_rec.shipping_method_code,
8404                                              x_return_status          => x_return_status);
8405 
8406 
8407       END IF;
8408 
8409       IF l_debug_level  > 0 THEN
8410          oe_debug_pub.add(  'SCHEDULING NOT REQUIRED' , 1 ) ;
8411       END IF;
8412       RETURN;
8413    END IF;
8414 
8415    IF l_debug_level  > 0 THEN
8416       oe_debug_pub.add(  'SCHEDULING LINE_ACTION :' || L_LINE_ACTION , 1 ) ;
8417    END IF;
8418 
8419    -- 5223953 - Versioning and reason not required for system-driven changes.
8420    IF p_caller = SCH_EXTERNAL THEN
8421       p_x_line_rec.change_reason := 'SYSTEM';
8422    END IF;
8423 
8424    ------------ need scheduling and resource flag done ----------
8425 
8426    <<RE_VALIDATE>>
8427 
8428    IF l_debug_level  > 0 THEN
8429       oe_debug_pub.add(  'CALLING OE_SCHEDULE_UTIL.VALIDATE LINE' , 1 ) ;
8430    END IF;
8431 
8432    Validate_Line(p_line_rec      => p_x_line_rec,
8433                  p_old_line_rec  => l_old_line_rec,
8434                  p_sch_action    => l_line_action,
8435                  p_caller        => SCH_INTERNAL,
8436                  x_return_status => x_return_status);
8437 
8438    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8439       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8440    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8441 
8442       -- When header preference is set and if line is failing
8443       -- in validation clear the line from set and do not fail the call
8444       -- so that line can be saved. -- 2404401.
8445       IF (p_x_line_rec.ship_set_id is not null
8446           OR p_x_line_rec.arrival_set_id is not null)
8447          AND p_x_line_rec.operation = oe_globals.g_opr_create
8448          AND (p_x_line_rec.top_model_line_id = p_x_line_rec.line_id
8449               OR   p_x_line_rec.top_model_line_id IS NULL) THEN
8450 
8451          OE_Order_Cache.Load_Order_Header(p_x_line_rec.header_id);
8452          IF OE_ORDER_CACHE.g_header_rec.customer_preference_set_code = 'SHIP' OR
8453             OE_ORDER_CACHE.g_header_rec.customer_preference_set_code = 'ARRIVAL'
8454          THEN
8455 
8456             IF l_debug_level  > 0 THEN
8457                oe_debug_pub.add(  'VALIDATION FAILED FOR SET' , 2 ) ;
8458             END IF;
8459             BEGIN
8460 
8461                OE_ORDER_UTIL.Update_Global_Picture
8462                   (p_Upd_New_Rec_If_Exists => False,
8463                    p_old_line_rec  => p_old_line_rec,
8464                    p_line_rec      => p_x_line_rec,
8465                    p_line_id       => p_x_line_rec.line_id,
8466                    x_index         => l_index,
8467                    x_return_status => x_return_status);
8468 
8469                IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8470                   IF l_debug_level  > 0 THEN
8471                      oe_debug_pub.add(  'After update global pic: UNEXP ERRORED OUT' , 1 ) ;
8472                   END IF;
8473                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8474                ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8475                   IF l_debug_level  > 0 THEN
8476                      oe_debug_pub.add(  'After update global pic : ERRORED OUT' , 1 ) ;
8477                   END IF;
8478                   RAISE FND_API.G_EXC_ERROR;
8479                END IF;
8480 
8481                IF l_index is not NULL THEN
8482                   --update Global Picture directly
8483                   OE_ORDER_UTIL.g_line_tbl(l_index).ship_set_id := Null;
8484                   OE_ORDER_UTIL.g_line_tbl(l_index).arrival_set_id  := Null;
8485                   OE_ORDER_UTIL.g_line_tbl(l_index).ship_set := Null;
8486                   OE_ORDER_UTIL.g_line_tbl(l_index).arrival_set := Null;
8487                   IF l_debug_level  > 0 THEN
8488                      oe_debug_pub.add(  'GLOBAL SHIP_set is : ' || OE_ORDER_UTIL.G_LINE_TBL (L_INDEX).line_id , 1 ) ;
8489                   END IF;
8490                END IF; /*l_index is not null check*/
8491 
8492                Update oe_order_lines_all
8493                   Set arrival_set_id = Null,
8494                   ship_set_id    = Null
8495                   Where Line_id = p_x_line_rec.line_id;
8496 
8497                p_x_line_rec.arrival_set_id := Null;
8498                p_x_line_rec.ship_set_id := Null;
8499 
8500                -- Change the status since we do not want to fail
8501                -- line to get saved .
8502                x_return_status := FND_API.G_RET_STS_SUCCESS;
8503 
8504                IF nvl(p_x_line_rec.override_atp_date_code,'N') = 'Y'
8505                   OR p_x_line_rec.schedule_ship_date is not null
8506                   OR p_x_line_rec.schedule_arrival_date is not null THEN
8507 
8508                   IF l_debug_level  > 0 THEN
8509                      oe_debug_pub.add(  'VALIDATION FAILED re-validate' , 2 ) ;
8510                   END IF;
8511 
8512                   GOTO RE_VALIDATE;
8513 
8514                END IF;
8515             END;
8516          END IF; -- preference
8517 
8518       END IF; -- ship set id
8519       IF l_auto_sch = 'Y' THEN
8520          IF l_debug_level  > 0 THEN
8521             oe_debug_pub.add(  'VALIDATION FAILED WHILE AUTO SCH' , 1 ) ;
8522          END IF;
8523          -- Change the status since we do not want to fail
8524          -- line to get saved .
8525          x_return_status := FND_API.G_RET_STS_SUCCESS;
8526       END IF;
8527       RETURN;
8528    END IF;
8529 
8530 
8531    ------------ here starts the branch based on groups ----------
8532    -- If rescheduling happening due change of the inventory item,
8533    -- if it is belong to group undemand the item before logging a
8534    -- request.After logging a request, we will not have visibility
8535    -- to old data.
8536 
8537    IF  l_line_action = OESCH_ACT_RESCHEDULE
8538       AND NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
8539                                p_old_line_rec.inventory_item_id)
8540       AND (p_x_line_rec.ship_set_id is NOT NULL OR
8541            p_x_line_rec.arrival_set_id is NOT NULL OR
8542               (nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N' AND
8543                p_x_line_rec.top_model_line_id is NOT NULL))
8544    THEN
8545 
8546       IF l_old_line_rec.reserved_quantity is not null
8547       THEN
8548          -- Call INV API to delete the reservations on  the line.
8549          -- shipping_interfaced_flag
8550          Unreserve_Line
8551          ( p_line_rec               => l_old_line_rec
8552            , p_quantity_to_unreserve  => l_old_line_rec.reserved_quantity
8553            , p_quantity2_to_unreserve  => l_old_line_rec.reserved_quantity2 -- INVCONV
8554            , x_return_status          => x_return_status);
8555 
8556          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8557             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8558          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8559             RAISE FND_API.G_EXC_ERROR;
8560          END IF;
8561       END IF;
8562 
8563       Action_Undemand(p_old_line_rec   => l_old_line_rec,
8564                       x_return_status  => x_return_status);
8565 
8566 
8567    END IF; -- Undemand.
8568 
8569    -- Breaking the link. Since, if line belong to set may not require to
8570    -- log a sets request, might require group_scheduling since they are belong
8571    -- to Model.
8572 
8573    IF nvl(p_x_line_rec.override_atp_date_code,'N') = 'N' AND
8574       nvl(p_old_line_rec.override_atp_date_code,'N') = 'Y'
8575       AND p_x_line_rec.top_model_line_id is not null THEN
8576 
8577       Cascade_override_atp(p_line_rec => p_x_line_rec);
8578 
8579    END IF;
8580 
8581    IF (p_x_line_rec.ship_set_id is NOT NULL OR
8582        p_x_line_rec.arrival_set_id is NOT NULL)
8583       AND p_x_line_rec.ordered_quantity > 0
8584       AND l_line_action <> OESCH_ACT_RESERVE  THEN -- remnant??
8585 
8586       IF l_debug_level  > 0 THEN
8587          oe_debug_pub.add(  'NEW SCHEDULING SETS RELATED' , 1 ) ;
8588       END IF;
8589       Log_Set_Request
8590          (p_line_rec      => p_x_line_rec,
8591           p_old_line_rec  => l_old_line_rec,
8592           p_sch_action    => l_line_action,
8593           p_caller        => p_caller,
8594           x_return_status => x_return_status);
8595 
8596       --sets related
8597       IF l_debug_level  > 0 THEN
8598          oe_debug_pub.add(  'AFTER LOGGING SETS DELAYED REQUEST ' || X_RETURN_STATUS , 1 ) ;
8599       END IF;
8600 
8601    -- ELSIF nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N'
8602    --Begin bug fix for bug#6153528
8603     ELSIF ( nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N' or
8604           ( nvl(p_x_line_rec.model_remnant_flag, 'N') = 'Y' and p_x_line_rec.ato_line_id is not null)
8605           )
8606    --End bug fix for bug#6153528
8607          AND   p_x_line_rec.top_model_line_id is NOT NULL
8608          AND   l_line_action <> OESCH_ACT_RESERVE
8609    THEN
8610 
8611 
8612       IF l_debug_level  > 0 THEN
8613          oe_debug_pub.add(  'NEW SCHEDULING CONFIG RELATED' , 1 ) ;
8614       END IF;
8615 
8616       OE_CONFIG_SCHEDULE_PVT.Log_Config_Sch_Request
8617          ( p_line_rec       => p_x_line_rec
8618            ,p_old_line_rec   => l_old_line_rec
8619            ,p_sch_action     => l_line_action
8620            ,p_caller         => p_caller
8621            ,x_return_status  => x_return_status);
8622 
8623    ELSE
8624       IF l_debug_level  > 0 THEN
8625          oe_debug_pub.add(  'NEW SCHEDULING STANDARD LINE ' , 1 ) ;
8626       END IF;
8627 
8628       Process_request( p_old_line_rec   => l_old_line_rec,
8629                        p_caller         => p_caller,
8630                        p_x_line_rec     => p_x_line_rec,
8631                        p_sch_action     => l_line_action,
8632                        x_return_status  => x_return_status);
8633    END IF; -- Group, parent and independent
8634 
8635 
8636    IF l_debug_level  > 0 THEN
8637       oe_debug_pub.add(  'SCH SHIP DATE ' || P_X_LINE_REC.SCHEDULE_SHIP_DATE , 5 ) ;
8638    END IF;
8639    IF l_debug_level  > 0 THEN
8640       oe_debug_pub.add(  'SCH ARR DATE ' || P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 5 ) ;
8641    END IF;
8642    IF l_debug_level  > 0 THEN
8643       oe_debug_pub.add(  'SHIP_FROM ' || P_X_LINE_REC.SHIP_FROM_ORG_ID , 5 ) ;
8644    END IF;
8645 
8646    Print_Time('Exiting schedule_line');
8647 EXCEPTION
8648    WHEN FND_API.G_EXC_ERROR THEN
8649 
8650       IF l_debug_level  > 0 THEN
8651          oe_debug_pub.add(  'G_EXC_ERROR IN SCHEDULE_LINE' , 1 ) ;
8652       END IF;
8653       x_return_status := FND_API.G_RET_STS_ERROR;
8654 
8655    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8656 
8657       IF l_debug_level  > 0 THEN
8658          oe_debug_pub.add(  'G_EXC__UNEXP_ERROR IN SCHEDULE_LINE' , 1 ) ;
8659       END IF;
8660       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8661 
8662    WHEN OTHERS THEN
8663 
8664       IF l_debug_level  > 0 THEN
8665          oe_debug_pub.add(  'OTHERS IN SCHEDULE_LINE'|| SQLERRM , 1 ) ;
8666       END IF;
8667       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8668 
8669       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8670       THEN
8671          OE_MSG_PUB.Add_Exc_Msg
8672             (   G_PKG_NAME
8673                 ,'Schedule_line');
8674       END IF;
8675 
8676 END Schedule_line;
8677 
8678 
8679 /*---------------------------------------------------------------------
8680 Function Name  : Need_Scheduling
8681 Description    : This API will return to the calling process if scheduling
8682                  needs to be performed on the line or not.
8683 
8684 The output param x_line_action,
8685   can be used as a guideline to decide
8686   the scheduling action on a line.
8687   NOTE: 1) The value of x_line_action should be ignored if this
8688            function returns false.
8689         2) More than 1 attributes can have a change, however this
8690            procedure returns TRUE as soon as first in the code
8691            is found to be changed.
8692 
8693 For a line which is not scheduled, entering following
8694 attributes will result in scheduling:
8695   schedule_ship_date
8696   schedue_arrival_date
8697   reserved_quantity
8698 
8699 
8700 Cases which need more explaination have comments inside code.
8701 -- We do not schedule service lines.
8702 -- We do not schedule OTA lines. (Changed as per bug 7392538)
8703 -- we will not do scheduling for lines with source_type=EXTERNAL
8704 -- We should not perform scheduling if the line is already scheduled
8705 --------------------------------------------------------------------- */
8706 FUNCTION Need_Scheduling
8707 (p_line_rec           IN OE_ORDER_PUB.line_rec_type,
8708  p_old_line_rec       IN OE_ORDER_PUB.line_rec_type,
8709 x_line_action OUT NOCOPY VARCHAR2,
8710 
8711 x_auto_sch OUT NOCOPY VARCHAR2
8712 
8713  )
8714 RETURN BOOLEAN
8715 IS
8716   l_order_date_type_code   VARCHAR2(30):='';
8717   l_request_date_flag      VARCHAR2(1) := 'Y';
8718   l_shipping_method_flag   VARCHAR2(1) := 'Y';
8719 /* 6663462 Added the following 3 variables for the Delayed Scheduling feature */
8720   l_delayed_schedule       VARCHAR2(1) := NVL(fnd_profile.value('ONT_DELAY_SCHEDULING'),'N') ;
8721   l_index                  NUMBER := 0;
8722   l_return_status          VARCHAR2(1);
8723 
8724 
8725 
8726 --
8727 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
8728 --
8729 BEGIN
8730 
8731    IF l_debug_level  > 0 THEN
8732        oe_debug_pub.add(  'ENTERING NEED SCHEDULING' , 1 ) ;
8733    END IF;
8734 
8735    x_line_action := Null;
8736    x_auto_sch    := 'N';
8737 
8738    -- QUOTING changes - return FALSE for lines in negotiation phase.
8739    IF p_line_rec.transaction_phase_code = 'N' THEN
8740        RETURN FALSE;
8741    END IF;
8742 
8743    IF (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_SERVICE) THEN
8744        RETURN FALSE;
8745    END IF;
8746 
8747    IF l_debug_level  > 0 THEN
8748        oe_debug_pub.add(  'N1 NOT A SERVICE LINE' , 1 ) ;
8749    END IF;
8750    IF p_line_rec.line_category_code =  'RETURN' THEN
8751        RETURN FALSE;
8752    END IF;
8753 
8754    IF l_debug_level  > 0 THEN
8755        oe_debug_pub.add(  'N01 NOT A RETURN LINE' , 1 ) ;
8756    END IF;
8757  -- bug 7392538 changes start here
8758  -- Issue Discription
8759  -- The function call to OE_OTA_UTIL.Is_OTA_Line has been
8760  -- commented out due to bug 7385681. The issue here was that
8761  -- if a class line had UOM in ENR or EVT then this function
8762  -- call did not allow the class to be scheduled. Hence the
8763  -- workflow of any included item inside this class always left
8764  -- the LINE_SCHEDULING activity in the workflow with the result
8765  -- as INCOMPLETE and got stuck.
8766  -- The new behavior replicated the manual scheduling behavior.
8767 /*
8768    IF OE_OTA_UTIL.Is_OTA_Line(p_line_rec.order_quantity_uom)
8769    THEN
8770       RETURN FALSE;
8771    END IF;
8772 
8773    IF l_debug_level  > 0 THEN
8774        oe_debug_pub.add(  'N2 NOT A OTA ITEM' , 1 ) ;
8775    END IF;
8776  */
8777 
8778    -- If a config item is deleted, we do not need to call scheduling.
8779    -- Config Item can be deleted only through delink API. While delinking,
8780    -- CTO team takes care of updating the demand picture for the
8781    -- configuration. If a config item is getting created,
8782    -- we do not need to call scheduling.
8783 
8784    IF p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CONFIG AND
8785       (p_line_rec.operation = OE_GLOBALS.G_OPR_DELETE OR
8786        p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE )
8787    THEN
8788       RETURN FALSE;
8789    END IF;
8790 
8791    IF l_debug_level  > 0 THEN
8792        oe_debug_pub.add(  'N3 NOT CONFIG WITH DELETE AND CREATE OPR' , 1 ) ;
8793    END IF;
8794 
8795    -- Check to see if this line is a new line which has been created
8796    -- due to the split action. If yes, then do not schedule it, since we
8797    -- have already scheduled the line before.
8798 
8799    IF (p_line_rec.split_from_line_id is not null) AND
8800       (p_line_rec.split_from_line_id <> FND_API.G_MISS_NUM) AND
8801       (p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE)
8802    THEN
8803       IF l_debug_level  > 0 THEN
8804           oe_debug_pub.add(  'THIS IS A NEW LINE CREATED THRU SPLIT' , 1 ) ;
8805       END IF;
8806       RETURN FALSE;
8807    END IF;
8808 
8809    IF l_debug_level  > 0 THEN
8810        oe_debug_pub.add(  'N4 NOT A SPLIT LINE' , 1 ) ;
8811    END IF;
8812 
8813    -- Check to see if this line is the one which is getting split.
8814    -- If it is, then return FALSE, since this line is already rescheduled.
8815    IF (p_line_rec.split_action_code = 'SPLIT') THEN
8816        IF  (p_line_rec.schedule_status_code is not null) AND
8817            (p_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE)
8818        THEN
8819            IF l_debug_level  > 0 THEN
8820                oe_debug_pub.add(  'THIS LINE IS BEING SPLIT' , 1 ) ;
8821            END IF;
8822            RETURN FALSE;
8823        END IF;
8824    END IF;
8825 
8826 /*
8827    IF (p_line_rec.split_action_code = 'SPLIT') THEN
8828       oe_debug_pub.add('This is a split action',1);
8829       RETURN FALSE;
8830    END IF;
8831 */
8832    IF l_debug_level  > 0 THEN
8833        oe_debug_pub.add(  'N5 NOT A SPLIT PARENT' , 1 ) ;
8834    END IF;
8835 
8836 
8837    IF  p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_EXTERNAL
8838    AND p_line_rec.schedule_status_code is null
8839    THEN
8840         IF l_debug_level  > 0 THEN
8841             oe_debug_pub.add(  'UNSCHEDULED EXTERNAL LINE' , 1 ) ;
8842         END IF;
8843         RETURN FALSE;
8844    END IF;
8845 
8846    IF l_debug_level  > 0 THEN
8847        oe_debug_pub.add(  'N6 NOT A DROP SHIP LINE' , 1 ) ;
8848    END IF;
8849 
8850    If (p_line_rec.schedule_status_code is null) AND
8851       ((p_line_rec.schedule_action_code = OESCH_ACT_UNSCHEDULE) OR
8852       (p_line_rec.schedule_action_code = OESCH_ACT_UNDEMAND) OR
8853       (p_line_rec.schedule_action_code = OESCH_ACT_UNRESERVE))
8854    THEN
8855        IF l_debug_level  > 0 THEN
8856            oe_debug_pub.add(  'INVALID SCH ACTION ' , 1 ) ;
8857        END IF;
8858        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
8859        OE_MSG_PUB.Add;
8860        RETURN FALSE;
8861    END IF;
8862 
8863    IF l_debug_level  > 0 THEN
8864        oe_debug_pub.add(  'N7 NOT AN INVALID SCH ACTION' , 1 ) ;
8865    END IF;
8866 
8867    x_line_action := OESCH_ACT_SCHEDULE;
8868 
8869    IF p_line_rec.schedule_status_code is null
8870    THEN
8871 
8872      IF  p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_INTERNAL
8873      AND p_old_line_rec.source_type_code =  OE_GLOBALS.G_SOURCE_EXTERNAL
8874      AND p_line_rec.schedule_ship_date IS NOT NULL THEN
8875 
8876         IF l_debug_level  > 0 THEN
8877             oe_debug_pub.add(  'EXTERNAL -> INTERNAL WITH SHIP DATE' , 4 ) ;
8878         END IF;
8879         RETURN TRUE;
8880 
8881      END IF;
8882 
8883 
8884      IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
8885                              p_old_line_rec.schedule_ship_date)
8886      THEN
8887         IF l_debug_level  > 0 THEN
8888             oe_debug_pub.add(  'SCH_SHIP_DATE IS CHANGED , SCHEDULE' , 4 ) ;
8889         END IF;
8890         RETURN TRUE;
8891      END IF;
8892 
8893      IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
8894                              p_old_line_rec.schedule_arrival_date)
8895      THEN
8896         IF l_debug_level  > 0 THEN
8897             oe_debug_pub.add(  'SCH_ARR_DATE IS CHANGED , SCHEDULE' , 4 ) ;
8898         END IF;
8899         RETURN TRUE;
8900      END IF;
8901 
8902      IF l_debug_level  > 0 THEN
8903          oe_debug_pub.add(  'NEW RESERVED QTY' || P_LINE_REC.RESERVED_QUANTITY ) ;
8904      END IF;
8905      IF l_debug_level  > 0 THEN
8906          oe_debug_pub.add(  'OLD RESERVED QTY' || P_OLD_LINE_REC.RESERVED_QUANTITY ) ;
8907      END IF;
8908 
8909      IF NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
8910                              p_old_line_rec.reserved_quantity)
8911      THEN
8912         IF l_debug_level  > 0 THEN
8913             oe_debug_pub.add(  'RESERVED QTY ENTERED , SCHEDULE' , 4 ) ;
8914         END IF;
8915         RETURN TRUE;
8916      END IF;
8917 
8918      IF  NOT OE_GLOBALS.Equal(p_line_rec.ship_set_id,
8919                               p_old_line_rec.ship_set_id)
8920      AND p_line_rec.ship_set_id IS NOT NULL
8921      THEN
8922 
8923         IF l_debug_level  > 0 THEN
8924             oe_debug_pub.add(  'LINE IS BEING ADDED TO SHIP SET' , 4 ) ;
8925         END IF;
8926         RETURN TRUE;
8927      END IF;
8928 
8929      IF  NOT OE_GLOBALS.Equal(p_line_rec.arrival_Set_id,
8930                               p_old_line_rec.arrival_Set_id)
8931      AND  p_line_rec.arrival_set_id IS NOT NULL
8932      THEN
8933 
8934         IF l_debug_level  > 0 THEN
8935             oe_debug_pub.add(  'LINE IS BEING ADDED TO ARRIVAL SET' , 4 ) ;
8936         END IF;
8937         RETURN TRUE;
8938      END IF;
8939 
8940   END IF;
8941 
8942 
8943    IF l_debug_level  > 0 THEN
8944        oe_debug_pub.add(  'N8' , 1 ) ;
8945    END IF;
8946 
8947    -- QUOTING changes - trigger auto-scheduling if call is from
8948    -- complete negotiation
8949    IF ((p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
8950         OR OE_Quote_Util.G_COMPLETE_NEG = 'Y')
8951        AND OESCH_AUTO_SCH_FLAG = 'Y') THEN
8952 
8953          IF (p_line_rec.top_model_line_id is null OR
8954              p_line_rec.top_model_line_id = FND_API.G_MISS_NUM) AND
8955              p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD
8956          THEN
8957             IF l_debug_level  > 0 THEN
8958                 oe_debug_pub.add(  'AUTO SCHEDULE IS TRUE' , 4 ) ;
8959                 oe_debug_pub.add(  '6663462 : Delay Scheduling : ' || l_delayed_schedule , 4 ) ;
8960 
8961             END IF;
8962             IF l_delayed_schedule = 'N' THEN
8963               x_auto_sch := 'Y';
8964               RETURN TRUE;
8965             ELSE
8966                 OE_delayed_requests_Pvt.log_request(
8967                  p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
8968                  p_entity_id              => p_line_rec.header_id,
8969                  p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
8970                  p_requesting_entity_id   => p_line_rec.header_id,
8971                  p_request_type           => OE_GLOBALS.G_DELAYED_SCHEDULE,
8972                  x_return_status          => l_return_status);
8973                  l_index := OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl.count;
8974                  OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl(l_index+1) := p_line_rec;
8975                  oe_debug_pub.add(  '6663462  : logging delayed scheduling req for header_id ' || OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl(l_index+1).header_id, 1 ) ;
8976             END IF;
8977 
8978          END IF;
8979    END IF;
8980 
8981    IF l_debug_level  > 0 THEN
8982        oe_debug_pub.add(  'N9 AFTER AUTO SCHEDULE CHECK' , 1 ) ;
8983    END IF;
8984 
8985    IF  p_line_rec.schedule_status_code is NULL AND
8986        p_line_rec.schedule_action_code is NULL AND
8987        OESCH_AUTO_SCH_FLAG = 'N'
8988    THEN
8989       x_line_action := Null;
8990       RETURN FALSE;
8991    END IF;
8992 
8993    IF l_debug_level  > 0 THEN
8994        oe_debug_pub.add(  'N10' , 1 ) ;
8995    END IF;
8996 
8997    IF p_line_rec.schedule_status_code = OESCH_STATUS_SCHEDULED AND
8998       p_line_rec.schedule_action_code = OESCH_ACT_SCHEDULE
8999    THEN
9000       x_line_action := Null;
9001       RETURN FALSE;
9002    END IF;
9003 
9004    IF l_debug_level  > 0 THEN
9005        oe_debug_pub.add(  'N11' , 1 ) ;
9006    END IF;
9007 
9008    IF (p_line_rec.schedule_action_code is not null)
9009    THEN
9010       x_line_action := p_line_rec.schedule_action_code;
9011       IF l_debug_level  > 0 THEN
9012           oe_debug_pub.add(  'SCH ACTION CODE '|| P_LINE_REC.SCHEDULE_ACTION_CODE , 4 ) ;
9013       END IF;
9014       RETURN TRUE;
9015    END IF;
9016 
9017    IF l_debug_level  > 0 THEN
9018        oe_debug_pub.add(  'N12 AFTER ACTION CHECK' , 1 ) ;
9019    END IF;
9020    -------------- Starts already scheduled line part ------
9021 
9022    -- We should avoid calling scheduling when user changes values
9023    -- of the below attributes on the unscheduled lines. The code
9024    -- below is valid only for scheduled lines.
9025 
9026   IF p_line_rec.schedule_status_code is NOT NULL THEN
9027 
9028     x_line_action := OESCH_ACT_RESCHEDULE;
9029 
9030     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
9031                             p_old_line_rec.schedule_ship_date)
9032     THEN
9033       -- On a scheduled line, if user is clearing schedule_ship_date
9034       -- treat is as inschedule.
9035       IF  p_line_rec.schedule_ship_date is null
9036 --      AND p_line_rec.ship_set_id is null
9037 --      AND p_line_rec.arrival_set_id is null
9038       THEN
9039          x_line_action := OESCH_ACT_UNSCHEDULE;
9040       END IF;
9041       IF l_debug_level  > 0 THEN
9042           oe_debug_pub.add(  'SCH_SHIP_DATE CHANGED , RESCHEDULE' , 4 ) ;
9043       END IF;
9044       RETURN TRUE;
9045     END IF;
9046 
9047     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
9048                             p_old_line_rec.schedule_arrival_date)
9049     THEN
9050       IF l_debug_level  > 0 THEN
9051           oe_debug_pub.add(  'SCH_ARR_DATE CHANGED , RESCHEDULE' , 4 ) ;
9052       END IF;
9053       IF p_line_rec.schedule_arrival_date is null
9054       AND p_line_rec.ship_set_id is null
9055       AND p_line_rec.arrival_set_id is null THEN
9056          x_line_action := OESCH_ACT_UNSCHEDULE;
9057       END IF;
9058       RETURN TRUE;
9059     END IF;
9060 
9061     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
9062                             p_old_line_rec.ship_from_org_id)
9063     THEN
9064        IF l_debug_level  > 0 THEN
9065            oe_debug_pub.add(  'SHIP_FROM_ORG CHANGED , RESCHEDULE' , 4 ) ;
9066        END IF;
9067        RETURN TRUE;
9068     END IF;
9069 
9070 
9071     IF NOT OE_GLOBALS.Equal(p_line_rec.ordered_quantity,
9072                             p_old_line_rec.ordered_quantity)
9073     THEN
9074        IF l_debug_level  > 0 THEN
9075            oe_debug_pub.add(  'ORD QTY CHANGED , RESCHEDULE' , 4 ) ;
9076        END IF;
9077        RETURN TRUE;
9078     END IF;
9079 
9080     IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,
9081                             p_old_line_rec.order_quantity_uom)
9082     THEN
9083        IF l_debug_level  > 0 THEN
9084            oe_debug_pub.add(  'ORDER QTY UOM CHANGED , RESCHEDULE' , 4 ) ;
9085        END IF;
9086        RETURN TRUE;
9087     END IF;
9088 
9089     IF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
9090                             p_old_line_rec.request_date)
9091     THEN
9092        -- Pack J
9093        --- Return True only if Request date parameter value is set to 'Yes'
9094        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
9095           l_request_date_flag :=
9096                     NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y');
9097        END IF;
9098 
9099        -- This code has been added to avoid re-scheduling the line when request date is changed
9100        -- on a scheduled and overridden line. -- 3524314
9101 
9102        IF OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
9103                            p_old_line_rec.override_atp_date_code)
9104        AND nvl(p_line_rec.override_atp_date_code,'N') = 'Y' THEN
9105           l_request_date_flag := 'N';
9106        END IF;
9107 
9108        IF l_request_date_flag = 'Y' THEN
9109 
9110           IF l_debug_level  > 0 THEN
9111              oe_debug_pub.add(  'REQ_DATE CHANGED , RESCHEDULE' , 4 ) ;
9112           END IF;
9113           RETURN TRUE;
9114        END IF;
9115 
9116     END IF;
9117 
9118     IF NOT OE_GLOBALS.Equal(p_line_rec.shipping_method_code,
9119                             p_old_line_rec.shipping_method_code)
9120     THEN
9121        -- Pack J
9122        -- Return True if Ship method parameter value set to 'Yes'.
9123        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
9124          l_shipping_method_flag :=
9125                         NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_SHIP_METHOD_FLAG'),'Y');
9126        END IF;
9127        IF l_shipping_method_flag = 'Y' THEN
9128 
9129           IF l_debug_level  > 0 THEN
9130              oe_debug_pub.add(  'SHIPPING_METHOD CHANGED , RESCHEDULE' , 4 ) ;
9131           END IF;
9132           RETURN TRUE;
9133        END IF;
9134 
9135     END IF;
9136 
9137     IF NOT OE_GLOBALS.Equal(p_line_rec.delivery_lead_time,
9138                             p_old_line_rec.delivery_lead_time)
9139     THEN
9140       BEGIN
9141         select order_date_type_code
9142         into l_order_date_type_code
9143         from oe_order_headers_all
9144         where header_id =  p_line_rec.header_id;
9145 
9146         IF l_order_date_type_code  = 'ARRIVAL' THEN
9147            IF l_debug_level  > 0 THEN
9148                oe_debug_pub.add(  'DEL LEAD TIME CHANGED , RESCHEDULE' , 4 ) ;
9149            END IF;
9150            RETURN TRUE;
9151         END IF;
9152       EXCEPTION
9153         WHEN OTHERS THEN
9154           NULL;
9155       END;
9156     END IF;
9157 
9158     IF NOT OE_GLOBALS.Equal(p_line_rec.demand_class_code,
9159                             p_old_line_rec.demand_class_code)
9160     THEN
9161        IF l_debug_level  > 0 THEN
9162            oe_debug_pub.add(  'DEMAND_CLASS CHANGED , RESCHEDULE' , 4 ) ;
9163        END IF;
9164        RETURN TRUE;
9165     END IF;
9166 
9167     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_to_org_id,
9168                             p_old_line_rec.ship_to_org_id)
9169     THEN
9170        IF l_debug_level  > 0 THEN
9171            oe_debug_pub.add(  'SHIP_TO_ORG CHANGED , RESCHEDULE' , 4 ) ;
9172        END IF;
9173        RETURN TRUE;
9174     END IF;
9175 
9176     IF NOT OE_GLOBALS.Equal(p_line_rec.sold_to_org_id,
9177                            p_old_line_rec.sold_to_org_id)
9178     THEN
9179        IF l_debug_level  > 0 THEN
9180            oe_debug_pub.add(  'SOLD_TO_ORG CHANGED , RESCHEDULE' , 4 ) ;
9181        END IF;
9182        RETURN TRUE;
9183     END IF;
9184 
9185     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
9186                              p_old_line_rec.inventory_item_id)
9187     THEN
9188         IF l_debug_level  > 0 THEN
9189             oe_debug_pub.add(  'INV ITEM ID CHANGED , RESCHEDULE' , 4 ) ;
9190         END IF;
9191         RETURN TRUE;
9192     END IF;
9193 
9194     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_set_id,
9195                              p_old_line_rec.ship_set_id)
9196        AND p_line_rec.ship_set_id IS NOT NULL
9197     THEN
9198 
9199         IF l_debug_level  > 0 THEN
9200             oe_debug_pub.add(  'LINE IS BEING ADDED TO SHIP SET' , 4 ) ;
9201         END IF;
9202         RETURN TRUE;
9203     END IF;
9204 
9205     IF NOT OE_GLOBALS.Equal(p_line_rec.arrival_set_id,
9206                              p_old_line_rec.arrival_set_id)
9207        AND p_line_rec.arrival_set_id IS NOT NULL
9208     THEN
9209 
9210         IF l_debug_level  > 0 THEN
9211             oe_debug_pub.add(  'LINE IS BEING ADDED TO ARRIVAL SET' , 4 ) ;
9212         END IF;
9213         RETURN TRUE;
9214     END IF;
9215 
9216       -- BUG 1282873
9217     IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
9218       IF  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
9219                                p_old_line_rec.override_atp_date_code) THEN
9220          -- This line was previously scheduled with the Override ATP Flag set
9221          -- but it is now not set.  Must re-schedule the line
9222          IF l_debug_level  > 0 THEN
9223              oe_debug_pub.add(  'OVERRIDE ATP UNCHECKED , RESCHEDULE' , 1 ) ;
9224          END IF;
9225          Return TRUE;
9226       END IF;
9227     END IF;
9228      -- END 1282873
9229 
9230     -- Changing the source type on a scheduled line.
9231     -- We should unschedule the line
9232     IF p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_EXTERNAL AND
9233        NOT OE_GLOBALS.Equal(p_line_rec.source_type_code,
9234                             p_old_line_rec.source_type_code)
9235     THEN
9236         IF l_debug_level  > 0 THEN
9237             oe_debug_pub.add(  'SOURCE TYPE MADE EXTERNAL , UNSCHEDULE' , 4 ) ;
9238         END IF;
9239         x_line_action := OESCH_ACT_UNSCHEDULE;
9240         RETURN TRUE;
9241     END IF;
9242 
9243     IF NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
9244                             p_old_line_rec.reserved_quantity)
9245     THEN
9246         IF l_debug_level  > 0 THEN
9247             oe_debug_pub.add(  'RESERVED QTY CHANGED , RESERVE' , 4 ) ;
9248         END IF;
9249         x_line_action := OESCH_ACT_RESERVE;
9250         RETURN TRUE;
9251     END IF;
9252 
9253     -- subinventory changes require only the change
9254     --reservation. If reservation does not exists for the
9255     --line no action required here.
9256 
9257     -- Do not move this code
9258     IF  NOT OE_GLOBALS.Equal(p_line_rec.subinventory,
9259                              p_old_line_rec.subinventory)
9260     AND p_old_line_rec.reserved_quantity > 0
9261     THEN
9262        IF l_debug_level  > 0 THEN
9263            oe_debug_pub.add(  'SUBINVENTORY CHANGED , RESCHEDULE' , 4 ) ;
9264        END IF;
9265        x_line_action := OESCH_ACT_RESERVE;
9266        RETURN TRUE;
9267     END IF;
9268     --4653097:Start
9269     IF  NOT OE_GLOBALS.Equal(p_line_rec.project_id,
9270                              p_old_line_rec.project_id)
9271     AND p_old_line_rec.reserved_quantity > 0
9272     THEN
9273        IF l_debug_level  > 0 THEN
9274            oe_debug_pub.add(  'PROJECT CHANGED , RESCHEDULE' , 4 ) ;
9275        END IF;
9276        x_line_action := OESCH_ACT_RESERVE;
9277        RETURN TRUE;
9278     END IF;
9279 
9280     IF  NOT OE_GLOBALS.Equal(p_line_rec.task_id,
9281                              p_old_line_rec.task_id)
9282     AND p_old_line_rec.reserved_quantity > 0
9283     THEN
9284        IF l_debug_level  > 0 THEN
9285            oe_debug_pub.add(  'TASK CHANGED , RESCHEDULE' , 4 ) ;
9286        END IF;
9287        x_line_action := OESCH_ACT_RESERVE;
9288        RETURN TRUE;
9289     END IF;
9290     --4653097 :End
9291   END IF; -- Check for schedule_status_code not NULL.
9292 
9293   IF l_debug_level  > 0 THEN
9294       oe_debug_pub.add(  'N13' , 1 ) ;
9295   END IF;
9296   RETURN FALSE;
9297 
9298 
9299 EXCEPTION
9300 
9301   WHEN OTHERS THEN
9302     x_line_action := Null;
9303 
9304     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
9305     THEN
9306       OE_MSG_PUB.Add_Exc_Msg
9307       (   G_PKG_NAME
9308       ,   'Need_Scheduling'
9309        );
9310     END IF;
9311 
9312 END Need_Scheduling;
9313 
9314 
9315 /*---------------------------------------------------------------------
9316 Procedure Name : Validate_Line
9317 Description    : Validates a line before scheduling.
9318                  It will make sure the required attributes are the
9319                  there on the line.
9320                  Only standard lines can be scheduled.Service lines
9321                  return lines cannot be scheduled.
9322                  IF the profile OE:Schedule Line on Hold is set to 'Y'
9323                  we will perform scheduling on lines on hold. If it is
9324                  set to 'N', we will not perform scheduling.
9325 --------------------------------------------------------------------- */
9326 Procedure Validate_Line
9327 (p_line_rec      IN OE_ORDER_PUB.Line_Rec_Type,
9328  p_old_line_rec  IN OE_ORDER_PUB.Line_Rec_Type,
9329  p_sch_action    IN VARCHAR2,
9330  p_caller        IN VARCHAR2 := SCH_EXTERNAL,
9331 x_return_status OUT NOCOPY VARCHAR2)
9332 
9333 IS
9334   l_msg_count              NUMBER;
9335   l_msg_data               VARCHAR2(2000);
9336   l_result                 Varchar2(30);
9337   l_scheduling_level_code  VARCHAR2(30) := NULL;
9338   l_type_code              VARCHAR2(30);
9339   l_out_return_status      VARCHAR2(1)  := FND_API.G_RET_STS_SUCCESS;
9340   l_org_id                 NUMBER;
9341   l_bill_seq_id            NUMBER;
9342   l_make_buy               NUMBER;
9343   l_config_id              NUMBER;
9344   l_org_code               VARCHAR2(30);
9345   l_order_date_type_code   VARCHAR2(30) := null; -- Bug-2371760
9346   l_auth_to_override_atp   VARCHAR2(3) := NULL; -- BUG 1282873
9347   l_override               NUMBER;  -- BUG 1282873
9348   l_set_rec                OE_ORDER_CACHE.set_rec_type;
9349   l_found                  VARCHAR2(1) :='N';  --Bug 2746497
9350   l_overridden             VARCHAR2(1) :='N';  --Bug 2716220
9351 --
9352 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9353 --
9354 BEGIN
9355 
9356                                    IF l_debug_level  > 0 THEN
9357                                        oe_debug_pub.add(  '..ENTERING OE_SCHEDULE_UTIL.VALIDATE_LINE :' || P_SCH_ACTION , 1 ) ;
9358                                    END IF;
9359    x_return_status  := FND_API.G_RET_STS_SUCCESS;
9360 
9361    -- If the quantity on the line is missing or null and if
9362    -- the user is trying to performing scheduling, it is an error
9363 
9364    IF ((p_old_line_rec.ordered_quantity is null OR
9365         p_old_line_rec.ordered_quantity = FND_API.G_MISS_NUM) AND
9366          (p_line_rec.ordered_quantity is null OR
9367           p_line_rec.ordered_quantity = FND_API.G_MISS_NUM)) THEN
9368 
9369              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_QUANTITY');
9370              OE_MSG_PUB.Add;
9371              IF l_debug_level  > 0 THEN
9372                  oe_debug_pub.add(  'E1' , 1 ) ;
9373              END IF;
9374              x_return_status := FND_API.G_RET_STS_ERROR;
9375    END IF;
9376 
9377    -- If the quantity on the line is zero(which is different from
9378    -- missing)  and if the user is trying to performing scheduling,
9379    -- it is an error
9380 
9381    IF (((p_old_line_rec.ordered_quantity is null OR
9382          p_old_line_rec.ordered_quantity = FND_API.G_MISS_NUM OR
9383          p_old_line_rec.ordered_quantity = 0) AND
9384          p_line_rec.ordered_quantity = 0) AND
9385          (nvl(p_line_rec.cancelled_flag,'N') = 'N')) THEN
9386 
9387          IF  p_sch_action is not null
9388          AND p_caller = SCH_INTERNAL  THEN
9389 
9390              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ZERO_QTY');
9391              OE_MSG_PUB.Add;
9392 
9393              IF l_debug_level  > 0 THEN
9394                  oe_debug_pub.add(  'E2' , 1 ) ;
9395              END IF;
9396              x_return_status := FND_API.G_RET_STS_ERROR;
9397          END IF;
9398 
9399 
9400    END IF;
9401 
9402    IF l_debug_level  > 0 THEN
9403        oe_debug_pub.add(  'BEFORE CANCEL CHECK ' || X_RETURN_STATUS , 2 ) ;
9404    END IF;
9405    -- If the line is cancelled, scheduling is not allowed.
9406 
9407    IF (p_line_rec.cancelled_flag = 'Y' AND
9408        p_caller = SCH_INTERNAL) THEN
9409 
9410           IF p_line_rec.schedule_action_code is not null THEN
9411 
9412              -- The line is cancelled. Cannot perform scheduling
9413              -- on it.
9414 
9415              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_FULLY_CANCELLED');
9416              OE_MSG_PUB.Add;
9417 
9418              IF l_debug_level  > 0 THEN
9419                  oe_debug_pub.add(  'E3' , 1 ) ;
9420              END IF;
9421              x_return_status := FND_API.G_RET_STS_ERROR;
9422 
9423          END IF;
9424    END IF;
9425 
9426    -- If the line is shipped, scheduling is not allowed.
9427 
9428    IF l_debug_level  > 0 THEN
9429        oe_debug_pub.add(  'BEFORE SHIPPIED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
9430    END IF;
9431    IF (p_line_rec.shipped_quantity is not null) AND
9432         (p_line_rec.shipped_quantity <> FND_API.G_MISS_NUM) THEN
9433 
9434          IF p_sch_action is not null THEN
9435 
9436              -- The line is cancelled. Cannot perform scheduling
9437              -- on it.
9438 
9439              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_SHIPPED');
9440              OE_MSG_PUB.Add;
9441 
9442              IF l_debug_level  > 0 THEN
9443                  oe_debug_pub.add(  'E4' , 1 ) ;
9444              END IF;
9445              x_return_status := FND_API.G_RET_STS_ERROR;
9446 
9447          END IF;
9448    END IF;
9449 IF l_debug_level  > 0 THEN
9450        oe_debug_pub.add(  'BEFORE FULFILLED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
9451    END IF;
9452 --Added for bug 6873122
9453    IF (p_line_rec.fulfilled_quantity is not null) AND
9454         (p_line_rec.fulfilled_quantity <> FND_API.G_MISS_NUM) THEN
9455 
9456          IF p_sch_action is not null THEN
9457 
9458              -- The line is Fulfilled. Cannot perform scheduling
9459              -- on it.
9460 
9461              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_FULFILLED');
9462              OE_MSG_PUB.Add;
9463 
9464              IF l_debug_level  > 0 THEN
9465                  oe_debug_pub.add(  'E4.1' , 1 ) ;
9466              END IF;
9467              x_return_status := FND_API.G_RET_STS_ERROR;
9468 
9469          END IF;
9470    END IF;
9471 --Added for bug 6873122
9472 
9473    IF l_debug_level  > 0 THEN
9474        oe_debug_pub.add(  'BEFORE RESERVED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
9475    END IF;
9476    -- Check to see if the reserved quantity is changed and is more
9477    -- than the ordered quantity. This should not be allowed.
9478    IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity,
9479                              p_line_rec.reserved_quantity)
9480    THEN
9481         -- Reserved Quantity has changed
9482        IF (p_line_rec.ordered_quantity < p_line_rec.reserved_quantity)
9483        AND OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom, p_line_rec.order_quantity_uom) --Bug 6335352
9484        THEN
9485 
9486          FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
9487          OE_MSG_PUB.Add;
9488 
9489          IF l_debug_level  > 0 THEN
9490              oe_debug_pub.add(  'E5' , 1 ) ;
9491          END IF;
9492          x_return_status := FND_API.G_RET_STS_ERROR;
9493        END IF;
9494 
9495        -- after changing reserved qty, trying to unschedule or unreserve
9496        -- dose not make sense.
9497        IF (p_sch_action = OESCH_ACT_UNSCHEDULE OR
9498            p_sch_action = OESCH_ACT_UNRESERVE) AND
9499            (p_line_rec.reserved_quantity is not null) THEN
9500 
9501            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_QTY_CHG_NOT_ALLOWED');
9502            OE_MSG_PUB.Add;
9503            x_return_status := FND_API.G_RET_STS_ERROR;
9504            IF l_debug_level  > 0 THEN
9505                oe_debug_pub.add(  'E6' , 1 ) ;
9506            END IF;
9507        END IF;
9508    END IF;
9509 
9510 IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity2, -- INVCONV
9511                              p_line_rec.reserved_quantity2)
9512    THEN
9513         -- Reserved Quantity2 has changed
9514        IF (p_line_rec.ordered_quantity2 < p_line_rec.reserved_quantity2)
9515        THEN
9516 
9517          FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
9518          OE_MSG_PUB.Add;
9519 
9520          IF l_debug_level  > 0 THEN
9521              oe_debug_pub.add(  'E5a' , 1 ) ;
9522          END IF;
9523          x_return_status := FND_API.G_RET_STS_ERROR;
9524        END IF;
9525 
9526        -- after changing reserved qty, trying to unschedule or unreserve
9527        -- dose not make sense.
9528        IF (p_sch_action = OESCH_ACT_UNSCHEDULE OR
9529            p_sch_action = OESCH_ACT_UNRESERVE) AND
9530            (p_line_rec.reserved_quantity2 is not null) THEN
9531 
9532            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_QTY_CHG_NOT_ALLOWED');
9533            OE_MSG_PUB.Add;
9534            x_return_status := FND_API.G_RET_STS_ERROR;
9535            IF l_debug_level  > 0 THEN
9536                oe_debug_pub.add(  'E6A' , 1 ) ;
9537            END IF;
9538        END IF;
9539    END IF;
9540 
9541 
9542 
9543    -- Check to see if the ordered quantity and reserved quantity
9544    -- both have changed and if the ordered quantity is less than
9545    -- the reserved quantity. This should not be allowed.
9546 
9547    IF NOT OE_GLOBALS.Equal(p_old_line_rec.ordered_quantity,
9548                            p_line_rec.ordered_quantity)
9549    THEN
9550         -- Ordered Quantity has changed
9551        IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity,
9552                                p_line_rec.reserved_quantity)
9553           AND OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom, p_line_rec.order_quantity_uom) -- Bug 6335352
9554        THEN
9555          IF (p_line_rec.ordered_quantity < p_line_rec.reserved_quantity)
9556          THEN
9557 
9558            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
9559            OE_MSG_PUB.Add;
9560 
9561            x_return_status := FND_API.G_RET_STS_ERROR;
9562            IF l_debug_level  > 0 THEN
9563                oe_debug_pub.add(  'E7' , 1 ) ;
9564            END IF;
9565          END IF;
9566        END IF;
9567    END IF;
9568 
9569     IF NOT OE_GLOBALS.Equal(p_old_line_rec.ordered_quantity2, -- INVCONV
9570                            p_line_rec.ordered_quantity2)
9571    THEN
9572         -- Ordered Quantity has changed
9573        IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity2,
9574                                p_line_rec.reserved_quantity2)
9575        THEN
9576          IF (p_line_rec.ordered_quantity2 < p_line_rec.reserved_quantity2)
9577          THEN
9578 
9579            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
9580            OE_MSG_PUB.Add;
9581 
9582            x_return_status := FND_API.G_RET_STS_ERROR;
9583            IF l_debug_level  > 0 THEN
9584                oe_debug_pub.add(  'E7A' , 1 ) ;
9585            END IF;
9586          END IF;
9587        END IF;
9588    END IF;
9589 
9590 
9591    -- If the order quantity uom on the line is missing or null
9592    -- and if the user is trying to performing scheduling,
9593    -- it is an error
9594 
9595    IF (p_line_rec.order_quantity_uom is null OR
9596        p_line_rec.order_quantity_uom = FND_API.G_MISS_CHAR) THEN
9597 
9598              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_UOM');
9599              OE_MSG_PUB.Add;
9600 
9601              x_return_status := FND_API.G_RET_STS_ERROR;
9602              IF l_debug_level  > 0 THEN
9603                  oe_debug_pub.add(  'E8' , 1 ) ;
9604              END IF;
9605    END IF;
9606 
9607    -- If the item on the line is missing or null and if the user
9608    -- is trying to performing scheduling, it is an error
9609 
9610    IF l_debug_level  > 0 THEN
9611        oe_debug_pub.add(  'CHECKING THE ITEM....' , 1 ) ;
9612    END IF;
9613 
9614    IF (p_line_rec.inventory_item_id is null OR
9615        p_line_rec.inventory_item_id = FND_API.G_MISS_NUM) THEN
9616 
9617              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_ITEM');
9618              OE_MSG_PUB.Add;
9619 
9620              IF l_debug_level  > 0 THEN
9621                  oe_debug_pub.add(  'E9' , 1 ) ;
9622              END IF;
9623              x_return_status := FND_API.G_RET_STS_ERROR;
9624    END IF;
9625 
9626    -- If the request_date on the line is missing or null and
9627    -- if the user is trying to performing scheduling,
9628    -- it is an error
9629 
9630    IF l_debug_level  > 0 THEN
9631        oe_debug_pub.add(  'CHECKING THE REQUEST DATE....' , 1 ) ;
9632    END IF;
9633    IF (p_line_rec.request_date is null OR
9634           p_line_rec.request_date = FND_API.G_MISS_DATE) THEN
9635 
9636              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_REQUEST_DATE');
9637              OE_MSG_PUB.Add;
9638              IF l_debug_level  > 0 THEN
9639                  oe_debug_pub.add(  'E10' , 1 ) ;
9640              END IF;
9641              x_return_status := FND_API.G_RET_STS_ERROR;
9642    END IF;
9643    -- 3763015
9644    IF NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y' THEN
9645 
9646       -- An item with this profile set  MUST have a warehouse
9647       IF l_debug_level  > 0 THEN
9648          oe_debug_pub.add(  'CHECKING THAT LINE HAS A WAREHOUSE...' , 1 ) ;
9649       END IF;
9650       IF (p_line_rec.ship_from_org_id is null OR
9651           p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
9652           FND_MESSAGE.SET_NAME('ONT','ONT_SCH_BYPASS_MISS_WSH');
9653           OE_MSG_PUB.Add;
9654 
9655          IF l_debug_level  > 0 THEN
9656             oe_debug_pub.add(  'ERROR BYPASS' , 1 ) ;
9657          END IF;
9658          x_return_status := FND_API.G_RET_STS_ERROR;
9659       END IF;
9660    END IF;
9661 
9662    -- If the line belongs to a set, you cannot unschedule the line
9663    IF l_debug_level  > 0 THEN
9664        oe_debug_pub.add(  'CHECKING FOR SET VALIDATIONS....' , 1 ) ;
9665    END IF;
9666    IF ((p_line_rec.ship_set_id is not null AND
9667         p_line_rec.ship_set_id <> FND_API.G_MISS_NUM) OR
9668        (p_line_rec.arrival_set_id is not null AND
9669         p_line_rec.arrival_set_id <> FND_API.G_MISS_NUM)) AND
9670          (p_sch_action = OESCH_ACT_UNDEMAND OR
9671           p_sch_action = OESCH_ACT_UNSCHEDULE)
9672           THEN
9673 
9674              FND_MESSAGE.SET_NAME('ONT','OE_SCH_CANNOT_UNSCH_SET');
9675              OE_MSG_PUB.Add;
9676              IF l_debug_level  > 0 THEN
9677                  oe_debug_pub.add(  'E11' , 1 ) ;
9678              END IF;
9679              x_return_status := FND_API.G_RET_STS_ERROR;
9680    END IF;
9681 
9682 
9683    -- Bug 2434132.
9684    -- Reducing the qty should be allowed and not increasing the qty.
9685    IF l_debug_level  > 0 THEN
9686        oe_debug_pub.add(  'CHECKING FOR HOLDS....' , 1 ) ;
9687    END IF;
9688 
9689    IF  Oe_Sys_Parameters.Value('ONT_SCHEDULE_LINE_ON_HOLD') = 'N'
9690    AND (p_sch_action = OESCH_ACT_SCHEDULE OR
9691         p_sch_action = OESCH_ACT_RESERVE OR
9692         (p_sch_action = OESCH_ACT_RESCHEDULE AND
9693          p_line_rec.schedule_status_code is Null) OR
9694        (p_line_rec.schedule_status_code is not null AND
9695         Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
9696                                    p_old_line_rec => p_old_line_rec)) OR
9697        (p_line_rec.schedule_status_code is not null AND
9698         p_line_rec.ordered_quantity > p_old_line_rec.ordered_quantity))
9699 
9700    THEN
9701 
9702         -- Since the profile is set to NO, we should not schedule
9703         -- the line if the line is on hold.
9704 
9705         IF l_debug_level  > 0 THEN
9706             oe_debug_pub.add(  'CALLING CHECK HOLDS' , 1 ) ;
9707         END IF;
9708 
9709         OE_Holds_PUB.Check_Holds
9710                  (   p_api_version       => 1.0
9711                  ,   p_init_msg_list     => FND_API.G_FALSE
9712                  ,   p_commit            => FND_API.G_FALSE
9713                  ,   p_validation_level  => FND_API.G_VALID_LEVEL_FULL
9714                  ,   x_return_status     => l_out_return_status
9715                  ,   x_msg_count         => l_msg_count
9716                  ,   x_msg_data          => l_msg_data
9717                  ,   p_line_id           => p_line_rec.line_id
9718                  ,   p_header_id           => p_line_rec.header_id
9719                  ,   p_hold_id           => NULL
9720                  ,   p_entity_code       => NULL
9721                  ,   p_entity_id         => NULL
9722                  ,   x_result_out        => l_result
9723                  );
9724 
9725         IF l_debug_level  > 0 THEN
9726             oe_debug_pub.add(  'AFTER CALLING CHECK HOLDS: ' || L_OUT_RETURN_STATUS , 1 ) ;
9727         END IF;
9728 
9729 
9730         IF (l_out_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
9731             IF l_out_return_status = FND_API.G_RET_STS_ERROR THEN
9732                RAISE FND_API.G_EXC_ERROR;
9733             ELSE
9734                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9735             END IF;
9736         END IF;
9737 
9738         IF (l_result = FND_API.G_TRUE) THEN
9739             FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_ON_HOLD');
9740             OE_MSG_PUB.Add;
9741             x_return_status := FND_API.G_RET_STS_ERROR;
9742         END IF;
9743 
9744    END IF;
9745 
9746    -- Check to see what scheduling level is allowed to be performed
9747    -- on this line. If the action requested is not allowed for the
9748    -- scheduling action, error out.
9749 
9750    IF l_debug_level  > 0 THEN
9751        oe_debug_pub.add(  'CHECKING SCHEDULING LEVEL...' , 1 ) ;
9752    END IF;
9753    l_scheduling_level_code := Get_Scheduling_Level(p_line_rec.header_id,
9754                                                    p_line_rec.line_type_id);
9755 
9756    IF l_debug_level  > 0 THEN
9757        oe_debug_pub.add(  'L_SCHEDULING_LEVEL_CODE : ' || L_SCHEDULING_LEVEL_CODE , 1 ) ;
9758    END IF;
9759 
9760 --BUG 1955004
9761    -- Checking if transaction type is using a
9762    --Repair Item Shipping Scheduling Level
9763    IF l_debug_level  > 0 THEN
9764        oe_debug_pub.add(  'CHECKING IF LINE HAS REPAIR ITEM SHIPPING SCHEDULING LEVEL...' , 1 ) ;
9765    END IF;
9766 
9767    IF (l_scheduling_level_code = SCH_LEVEL_FOUR
9768        OR l_scheduling_level_code = SCH_LEVEL_FIVE) THEN
9769 
9770      -- An item with this scheduling level MUST have a warehouse
9771     IF l_debug_level  > 0 THEN
9772         oe_debug_pub.add(  'CHECKING THAT LINE HAS A WAREHOUSE...' , 1 ) ;
9773     END IF;
9774     IF (p_line_rec.ship_from_org_id is null OR
9775         p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
9776         FND_MESSAGE.SET_NAME('ONT','OE_SCH_INACTIVE_MISS_WSH');
9777         FND_MESSAGE.SET_TOKEN('LTYPE',
9778                        nvl(sch_cached_line_type,'0'));
9779         OE_MSG_PUB.Add;
9780 
9781         IF l_debug_level  > 0 THEN
9782             oe_debug_pub.add(  'E11.1' , 1 ) ;
9783         END IF;
9784         x_return_status := FND_API.G_RET_STS_ERROR;
9785     END IF;
9786 
9787      -- An item with this schedling level MUST be a Standard
9788      -- Item, and NOT be a part of an ATO
9789      IF l_debug_level  > 0 THEN
9790          oe_debug_pub.add(  'CHECKING THAT IT IS A STANDARD ITEM...' , 1 ) ;
9791      END IF;
9792      IF (p_line_rec.item_type_code <> 'STANDARD'
9793          OR p_line_rec.ato_line_id is not null) THEN
9794 
9795         FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_INACTIVE_STD_ONLY');
9796         FND_MESSAGE.SET_TOKEN('LTYPE',
9797                        nvl(sch_cached_line_type,'0'));
9798         OE_MSG_PUB.Add;
9799 
9800         IF l_debug_level  > 0 THEN
9801             oe_debug_pub.add(  'E11.2' , 1 ) ;
9802         END IF;
9803         X_return_status := FND_API.G_RET_STS_ERROR;
9804       END IF;
9805 
9806       -- An item with this scheduling level MUST NOT be part
9807       -- of ANY set
9808       IF l_debug_level  > 0 THEN
9809           oe_debug_pub.add(  'CHECKING FOR NO SETS...' , 1 ) ;
9810       END IF;
9811       /* Commenting for Standalone project.
9812       -- We will now allow standard lines into sets.
9813       IF (p_line_rec.ship_set_id is not null OR
9814           P_line_rec.arrival_set_id is not null)THEN
9815 
9816         FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_INACTIVE_STD_ONLY');
9817         FND_MESSAGE.SET_TOKEN('LTYPE',
9818                        nvl(sch_cached_line_type,'0'));
9819         OE_MSG_PUB.Add;
9820 
9821         IF l_debug_level  > 0 THEN
9822             oe_debug_pub.add(  'E11.3' , 1 ) ;
9823         END IF;
9824         X_return_status := FND_API.G_RET_STS_ERROR;
9825       END IF;
9826       */
9827    END IF;
9828 -- END 1955004
9829 
9830    -- 3763015
9831    IF (l_scheduling_level_code is not null
9832    AND l_scheduling_level_code <> SCH_LEVEL_THREE)
9833    OR NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y'  THEN
9834         IF l_scheduling_level_code = SCH_LEVEL_ONE THEN
9835            IF p_sch_action = OESCH_ACT_SCHEDULE OR
9836               p_sch_action = OESCH_ACT_RESERVE OR
9837              (p_line_rec.schedule_status_code is  null AND
9838              (p_line_rec.schedule_ship_date is NOT NULL OR
9839               p_line_rec.schedule_arrival_date is NOT NULL))
9840             THEN
9841 
9842               FND_MESSAGE.SET_NAME('ONT','OE_SCH_ACTION_NOT_ALLOWED');
9843               FND_MESSAGE.SET_TOKEN('ACTION',
9844                        nvl(p_sch_action,OESCH_ACT_SCHEDULE));
9845               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
9846                        nvl(sch_cached_line_type,sch_cached_order_type));
9847               OE_MSG_PUB.Add;
9848               IF l_debug_level  > 0 THEN
9849                   oe_debug_pub.add(  'E12' , 1 ) ;
9850               END IF;
9851               x_return_status := FND_API.G_RET_STS_ERROR;
9852            END IF;
9853         ELSE
9854            IF l_scheduling_level_code = SCH_LEVEL_TWO OR
9855               -- BUG 1955004
9856               l_scheduling_level_code = SCH_LEVEL_FIVE
9857               -- Level Five and Two cannot have Reservations performed
9858               -- END 1955004
9859            THEN
9860            -- 2766876
9861            IF (NVL(p_line_rec.reserved_quantity,0) > 0
9862            AND p_sch_action = OESCH_ACT_SCHEDULE)
9863            OR p_sch_action = OESCH_ACT_RESERVE THEN
9864               FND_MESSAGE.SET_NAME('ONT','OE_SCH_ACTION_NOT_ALLOWED');
9865               FND_MESSAGE.SET_TOKEN('ACTION',
9866                         nvl(p_sch_action,OESCH_ACT_RESERVE));
9867               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
9868                         nvl(sch_cached_line_type,sch_cached_order_type));
9869               OE_MSG_PUB.Add;
9870               IF l_debug_level  > 0 THEN
9871                   oe_debug_pub.add(  'E13' , 1 ) ;
9872               END IF;
9873               x_return_status := FND_API.G_RET_STS_ERROR;
9874            END IF;
9875         END IF;
9876            --BUG 1955004
9877           -- 3763015
9878           IF l_scheduling_level_code = SCH_LEVEL_FOUR OR
9879              l_scheduling_level_code = SCH_LEVEL_FIVE OR
9880              NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y' THEN
9881             IF p_sch_action = OESCH_ACT_ATP_CHECK THEN
9882             -- levels Four and Five CANNOT have ATP Performed
9883               FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_ACTION_NOT_ALLOWED');
9884               FND_MESSAGE.SET_TOKEN('ACTION',
9885                           (nvl(p_sch_action, OESCH_ACT_ATP_CHECK)));
9886               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
9887                         nvl(sch_cached_line_type,sch_cached_order_type));
9888               OE_MSG_PUB.Add;
9889               IF l_debug_level  > 0 THEN
9890                   oe_debug_pub.add(  'E13.1' , 1 ) ;
9891               END IF;
9892               X_return_status := FND_API.G_RET_STS_ERROR;
9893             END IF;
9894           END IF;
9895         --END 1955004
9896        END IF;
9897    END IF;
9898 
9899    IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y'
9900    AND (((p_sch_action  = OESCH_ACT_RESERVE OR
9901           p_sch_action  = OESCH_ACT_RESCHEDULE) AND
9902           p_old_line_rec.reserved_quantity >
9903           nvl(p_line_rec.reserved_quantity,0) AND
9904           Get_Pick_Status(p_line_rec.line_id)) OR
9905          (p_sch_action  = OESCH_ACT_UNRESERVE AND
9906           Get_Pick_Status(p_line_rec.line_id)))
9907    THEN  -- 2595661
9908 
9909        -- Reservation qty cannot be reduced when line is
9910        -- interfaced to wsh. Give a message here tell the user we are
9911        -- not unreserving. Added code here to fix bug 2038201.
9912 
9913        FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNRSV_NOT_ALLOWED');
9914        OE_MSG_PUB.Add;
9915        IF l_debug_level  > 0 THEN
9916            oe_debug_pub.add(  'E14' , 1 ) ;
9917        END IF;
9918        x_return_status := FND_API.G_RET_STS_ERROR;
9919 
9920    END IF;
9921 
9922    -- following 2 checks are only for ato's
9923 
9924    IF p_line_rec.ato_line_id is not null AND
9925       NOT(p_line_rec.ato_line_id = p_line_rec.line_id AND
9926           p_line_rec.item_type_code IN ( OE_GLOBALS.G_ITEM_OPTION,
9927                                          OE_GLOBALS.G_ITEM_STANDARD))
9928    THEN
9929 
9930      IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
9931    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
9932 
9933        IF l_debug_level  > 0 THEN
9934         oe_debug_pub.add('Warehouse Validation is not rqd',1);
9935        END IF;
9936      ELSE
9937 
9938       IF p_line_rec.ship_from_org_id is NULL AND
9939         p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_SCHEDULE
9940       THEN
9941 
9942        FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATO_WHSE_REQD');
9943        OE_MSG_PUB.Add;
9944        IF l_debug_level  > 0 THEN
9945            oe_debug_pub.add(  'E15' , 1 ) ;
9946        END IF;
9947        x_return_status := FND_API.G_RET_STS_ERROR;
9948 
9949       END IF;
9950      END IF; -- Gop code level
9951      IF (p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
9952      OR  p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_UNRESERVE)
9953      AND p_line_rec.schedule_status_code is null
9954      THEN
9955 
9956        FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_NO_CONFIG');
9957        OE_MSG_PUB.Add;
9958        IF l_debug_level  > 0 THEN
9959            oe_debug_pub.add(  'E16' , 1 ) ;
9960        END IF;
9961        -- code fix for 3300528
9962        -- no need for returning error in this case
9963        --IF nvl(p_line_rec.ship_model_complete_flag,'N') = 'N' THEN
9964        --   x_return_status := FND_API.G_RET_STS_ERROR;
9965        --END IF;
9966        -- code fix for 3300528
9967      END IF;
9968      /* 4171389:  Reservations cannot be placed for config items if order
9969       * is not booked.
9970       */
9971      IF p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
9972         AND p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CONFIG
9973         AND p_line_rec.booked_flag <> 'Y'
9974      THEN
9975         FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
9976         OE_MSG_PUB.Add;
9977         IF l_debug_level  > 0 THEN
9978            oe_debug_pub.add(  'E16.0' , 1 );
9979         END IF;
9980         x_return_status := FND_API.G_RET_STS_ERROR;
9981      END IF;
9982    -- code fix for 3300528
9983    -- a message has to be given when the user tries to reserve PTO models and classes
9984    ELSIF nvl(p_line_rec.shippable_flag,'N') = 'N'
9985      AND  ( p_sch_action = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
9986        OR p_sch_action = OE_SCHEDULE_UTIL.OESCH_ACT_UNRESERVE)
9987        AND p_line_rec.schedule_status_code IS NULL
9988    THEN
9989      FND_MESSAGE.SET_NAME('ONT','ONT_SCH_NOT_RESERVABLE');
9990      OE_MSG_PUB.Add;
9991      IF l_debug_level  > 0 THEN
9992        oe_debug_pub.add(  'E16.1' , 1 ) ;
9993      END IF;
9994      -- code fix for 3300528
9995    END IF; -- part of ato
9996 
9997    -- Added for 3451987
9998    IF NVL(FND_PROFILE.VALUE('ONT_SCH_ATO_ITEM_WO_BOM'), 'N') = 'N' THEN
9999       -- Added this part of validation to fix bug 2051855
10000       IF p_line_rec.ato_line_id = p_line_rec.line_id
10001          AND p_line_rec.item_type_code in ('STANDARD','OPTION')
10002          AND  fnd_profile.value('INV_CTP') = '5'THEN
10003 
10004          l_org_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
10005 
10006          -- Added code to fix bug 2156268
10007          BEGIN
10008 
10009             SELECT planning_make_buy_code
10010             INTO   l_make_buy
10011             FROM   mtl_system_items
10012             WHERE  inventory_item_id = p_line_rec.inventory_item_id
10013             AND    ORGANIZATION_ID = nvl(p_line_rec.ship_from_org_id,
10014                                             l_org_id);
10015 
10016          EXCEPTION
10017             WHEN NO_DATA_FOUND THEN
10018                l_make_buy := 1;
10019          END;
10020 
10021          IF l_debug_level  > 0 THEN
10022             oe_debug_pub.add(  'L_MAKE_BUY' || L_MAKE_BUY , 2 );
10023          END IF;
10024 
10025          IF nvl(l_make_buy,1) <> 2 THEN
10026             BEGIN
10027                -- Modified code to fix bug 2307501.
10028                SELECT BILL_SEQUENCE_ID
10029                INTO   l_bill_seq_id
10030                FROM   BOM_BILL_OF_MATERIALS
10031                WHERE  ORGANIZATION_ID = nvl(p_line_rec.ship_from_org_id,
10032                                             l_org_id)
10033                AND    ASSEMBLY_ITEM_ID = p_line_rec.inventory_item_id
10034                AND    ALTERNATE_BOM_DESIGNATOR IS NULL;
10035 
10036             EXCEPTION
10037                WHEN NO_DATA_FOUND THEN
10038                   IF l_debug_level  > 0 THEN
10039                      oe_debug_pub.add(  'NO BILL IS DEFINED' , 2 ) ;
10040                   END IF;
10041                   IF l_debug_level  > 0 THEN
10042                      oe_debug_pub.add(  'E17' , 1 ) ;
10043                   END IF;
10044                   FND_MESSAGE.SET_NAME('ONT','OE_BOM_NO_BILL_IN_SHP_ORG');
10045                   FND_MESSAGE.SET_TOKEN('ITEM',p_line_rec.ordered_item);
10046 
10047                   -- Bug 2367743  Start
10048                   Select ORGANIZATION_CODE
10049                   Into   l_org_code
10050                   From   Inv_Organization_Info_v   --ORG_ORGANIZATION_DEFINITIONS
10051                   Where  ORGANIZATION_ID = NVL(p_line_rec.ship_from_org_id,l_org_id);
10052                   IF l_debug_level  > 0 THEN
10053                      oe_debug_pub.add(  'ORGANIZATION CODE:'||L_ORG_CODE , 2 ) ;
10054                   END IF;
10055                   FND_MESSAGE.SET_TOKEN('ORG',l_org_code);
10056                   -- Bug 2367743 End
10057 
10058                   OE_MSG_PUB.Add;
10059                   x_return_status := FND_API.G_RET_STS_ERROR;
10060 
10061                WHEN OTHERS THEN
10062                   Null;
10063             END;
10064          END IF;
10065       END IF;
10066    END IF; -- ONT_SCH_ATO_ITEM_WO_BOM = 'N'
10067 
10068 /* Duplicate validation to E14. Bug 2312341
10069    IF  nvl(p_line_rec.shipping_interfaced_flag, 'N') = 'Y'
10070    AND (p_sch_action = OESCH_ACT_RESERVE
10071    OR   p_sch_action = OESCH_ACT_RESCHEDULE)
10072    AND p_old_line_rec.reserved_quantity >
10073        p_line_rec.reserved_quantity
10074    THEN
10075 
10076       -- Added code here to fix bug 2038201.
10077       oe_debug_pub.add('E18',1);
10078       FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNRSV_NOT_ALLOWED');
10079       OE_MSG_PUB.Add;
10080       x_return_status := FND_API.G_RET_STS_ERROR;
10081 
10082    END IF;
10083 */
10084    IF (p_sch_action = OESCH_ACT_UNSCHEDULE)
10085    AND (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_MODEL OR
10086         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_OPTION OR
10087         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CLASS OR
10088         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_INCLUDED)
10089    AND  NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
10090    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 )
10091 
10092    THEN
10093      -- This action is not allowed on an ATO configuration if the config
10094      -- item is created.
10095 
10096      BEGIN
10097         SELECT line_Id
10098         INTO   l_config_id
10099         FROM   OE_ORDER_LINES_ALL
10100         WHERE  header_id = p_line_rec.header_id
10101         AND   (ato_line_id =p_line_rec.ato_line_id OR
10102               (top_model_line_id = p_line_rec.top_model_line_id AND
10103                Ship_model_complete_flag = 'Y'))
10104         AND    item_type_code = 'CONFIG';
10105 
10106       IF l_debug_level  > 0 THEN
10107           oe_debug_pub.add(  'E19' , 1 ) ;
10108       END IF;
10109       FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNSCH_CONFIG_EXISTS');
10110       OE_MSG_PUB.Add;
10111       x_return_status := FND_API.G_RET_STS_ERROR;
10112 
10113      EXCEPTION
10114         WHEN NO_DATA_FOUND THEN
10115              null;
10116         WHEN TOO_MANY_ROWS THEN
10117           IF l_debug_level  > 0 THEN
10118               oe_debug_pub.add(  'E19 TOO MANY ROWS' , 1 ) ;
10119           END IF;
10120           FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNSCH_CONFIG_EXISTS');
10121           OE_MSG_PUB.Add;
10122           x_return_status := FND_API.G_RET_STS_ERROR;
10123 
10124      END;
10125 
10126    END IF; /* If action was unschedule */
10127 
10128  -- Start Bug-2371760
10129  -- Change in Schedule Ship/Arrival Date is not allowed
10130  -- when Order Date Type is specified.
10131 /*
10132  OE_DEBUG_PUB.Add('Checking for Order date Type - Arrival');
10133 
10134  IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
10135                             p_old_line_rec.schedule_ship_date)
10136     THEN
10137        -- If the Order Type is ARRIVAL, the user is not
10138        -- allowed to change the schedule ship date
10139 
10140        l_order_date_type_code    := Get_Date_Type(p_line_rec.header_id);
10141 
10142        IF nvl(l_order_date_type_code,'SHIP') = 'ARRIVAL' THEN
10143 
10144           FND_MESSAGE.SET_NAME('ONT','OE_SCH_INV_SHP_DATE');
10145           OE_MSG_PUB.Add;
10146 
10147           OE_DEBUG_PUB.Add('E20 Order date Type - Arrival');
10148           x_return_status := FND_API.G_RET_STS_ERROR;
10149 
10150        END IF;
10151 
10152 
10153     END IF;
10154 
10155    OE_DEBUG_PUB.Add('Checking for Order date Type - Ship');
10156 
10157     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
10158                             p_old_line_rec.schedule_arrival_date)
10159     THEN
10160 
10161        -- If the Order Type is SHIP (or null), the user is not
10162        -- allowed to change the schedule arrival date
10163 
10164        l_order_date_type_code    := Get_Date_Type(p_line_rec.header_id);
10165 
10166        IF nvl(l_order_date_type_code,'SHIP') = 'SHIP' THEN
10167 
10168           FND_MESSAGE.SET_NAME('ONT','OE_SCH_INV_ARR_DATE');
10169           OE_MSG_PUB.Add;
10170 
10171           OE_DEBUG_PUB.Add('E21 Order date Type - Ship');
10172 
10173           x_return_status := FND_API.G_RET_STS_ERROR;
10174 
10175        END IF;
10176    END IF;
10177 
10178  -- End Bug-2371760
10179  */
10180    -- BUG 1282873
10181    IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
10182      l_auth_to_override_atp :=
10183          NVL(FND_PROFILE.VALUE('ONT_OVERRIDE_ATP'), 'N');
10184 
10185     IF l_debug_level  > 0 THEN
10186         oe_debug_pub.add(  'ONT_OVERRIDE_ATP' || L_AUTH_TO_OVERRIDE_ATP , 3 ) ;
10187     END IF;
10188     IF l_auth_to_override_atp = 'N' THEN
10189 
10190        IF  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
10191                                 p_old_line_rec.override_atp_date_code)
10192        THEN
10193 
10194           -- only authorized users have authority to update the
10195           -- Overide ATP Field
10196           FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH');
10197           OE_MSG_PUB.Add;
10198           IF l_debug_level  > 0 THEN
10199               oe_debug_pub.add(  'E22 INVALID AUTOTIZE' ) ;
10200           END IF;
10201                             IF l_debug_level  > 0 THEN
10202                                 oe_debug_pub.add(  'USER DOES NOT HAVE AUTHORITY TO CHANGE THE OVERRIDE ATP FIELD' , 1 ) ;
10203                             END IF;
10204           x_return_status := FND_API.G_RET_STS_ERROR;
10205 
10206        END IF;
10207 
10208 
10209        IF p_line_rec.schedule_status_code IS NOT NULL THEN
10210 
10211           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' AND
10212              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
10213                                         ,p_old_line_rec => p_old_line_rec) OR
10214           NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
10215 
10216           THEN
10217 
10218            -- only authorized users have authority to update scheduling attributes
10219            -- on an overridden line
10220              FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_MOD');
10221              OE_MSG_PUB.Add;
10222                   IF l_debug_level  > 0 THEN
10223                       oe_debug_pub.add(  'E23 USER DOES NOT HAVE AUTHORITY TO UPDATE SCHEDULE ATTRIBUTES' , 1 ) ;
10224                   END IF;
10225              X_return_status := FND_API.G_RET_STS_ERROR;
10226 
10227           END IF;
10228 
10229           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' AND
10230              (p_sch_action = OESCH_ACT_UNDEMAND OR
10231               P_sch_action = OESCH_ACT_UNSCHEDULE) THEN
10232              -- Only Authorized users have authority to unschedule an overriddenline
10233              FND_MESSAGE.SET_NAME('ONT','OE_SCH_OVER_ATP_NO_AUTH_UNCHK');
10234              OE_MSG_PUB.Add;
10235              IF l_debug_level  > 0 THEN
10236                 oe_debug_pub.add(  'E24 GENERAL USER CANNOT UNSCHEDULE AN OVERRIDDEN LINE' , 1 ) ;
10237              END IF;
10238              X_return_status := FND_API.G_RET_STS_ERROR;
10239 
10240           END IF;
10241           -- Start 2746497
10242           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'N' AND
10243              NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y' AND
10244              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
10245                                        ,p_old_line_rec => p_old_line_rec) OR
10246              NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
10247           THEN
10248                BEGIN
10249                       SELECT  'Y' INTO l_found
10250                       FROM   oe_order_lines
10251                       WHERE  top_model_line_id = p_line_rec.top_model_line_id
10252                       AND override_atp_date_code = 'Y'
10253                       AND rownum <2;
10254                EXCEPTION
10255                    WHEN OTHERS THEN
10256                         NULL;
10257                END;
10258                IF l_found = 'Y' THEN
10259                   FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_MOD');
10260                   OE_MSG_PUB.Add;
10261                   X_return_status := FND_API.G_RET_STS_ERROR;
10262              END IF;
10263           END IF;
10264           -- End 2746497
10265 
10266 
10267           IF (p_line_rec.ship_set_id IS NOT NULL OR
10268              p_line_rec.arrival_set_id IS NOT NULL) AND
10269              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
10270                                        ,p_old_line_rec => p_old_line_rec) OR
10271              NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
10272           THEN
10273 
10274              l_override := 0;
10275 
10276 
10277              SELECT count('x')
10278              INTO   l_override
10279              FROM   oe_order_lines_all
10280              WHERE  header_id = p_line_rec.header_id
10281              AND    (ship_set_id = p_line_rec.ship_set_id
10282              OR     arrival_set_id = p_line_rec.arrival_set_id)
10283              AND    override_atp_date_code = 'Y';
10284 
10285              IF l_override > 0 THEN
10286 
10287                -- only authorized users have authority to update scheduling
10288                -- attributes in a set with an overridden line
10289                FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
10290                OE_MSG_PUB.Add;
10291                IF l_debug_level  > 0 THEN
10292                    oe_debug_pub.add(  'E25 USER DOES NOT HAVE AUTHORITY TO UPDATE SCHEDULE ATTRIBUTES ON A SET WITH AN OVERRIDDEN LINE' , 1 ) ;
10293                END IF;
10294                X_return_status := FND_API.G_RET_STS_ERROR;
10295 
10296              END IF; -- l_override > 0
10297           END IF; -- ship and arrival not null
10298        END IF;  -- schedule_status_code IS NOT NULL
10299     END IF; -- overide_atp = N
10300 
10301     IF ((p_line_rec.ship_set_id IS NOT NULL AND
10302         NOT oe_globals.equal(p_line_rec.ship_set_id,
10303                              p_old_line_rec.ship_set_id))  OR
10304        (p_line_rec.arrival_set_id IS NOT NULL AND
10305         NOT oe_globals.equal(p_line_rec.arrival_set_id,
10306                             p_old_line_rec.arrival_set_id))) THEN
10307 
10308        l_set_rec := OE_ORDER_CACHE.Load_Set
10309                   (nvl(p_line_rec.arrival_set_id,p_line_rec.ship_set_id));
10310        IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' THEN
10311 
10312         IF  l_set_rec.ship_from_org_id is not null
10313         AND NOT Set_Attr_Matched
10314            (p_set_ship_from_org_id      => l_set_rec.ship_from_org_id ,
10315             p_line_ship_from_org_id     => p_line_rec.ship_from_org_id,
10316             p_set_ship_to_org_id        => l_set_rec.ship_to_org_id ,
10317             p_line_ship_to_org_id       => p_line_rec.ship_to_org_id ,
10318             p_set_schedule_ship_date    => l_set_rec.schedule_ship_date ,
10319             p_line_schedule_ship_date   => p_line_rec.schedule_ship_date,
10320             p_set_arrival_date          => l_set_rec.schedule_arrival_date,
10321             p_line_arrival_date         => p_line_rec.schedule_arrival_date,
10322             p_set_shipping_method_code  => l_set_rec.shipping_method_code ,
10323             p_line_shipping_method_code => p_line_rec.shipping_method_code,
10324             p_set_type                  => l_set_rec.set_type) THEN
10325 
10326           -- General users can only add new lines to the set provided the new
10327           -- lines can be scheduled for the schedule ship date of the set AND
10328           -- Authorized users cannot add an overridden line if the schedule
10329           -- ship date soes not match the set
10330 
10331           FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
10332           OE_MSG_PUB.Add;
10333           IF l_debug_level  > 0 THEN
10334               oe_debug_pub.add(  'E26 UNABLE TO ADD THE LINE TO THE SET' , 1 ) ;
10335           END IF;
10336           x_return_status := FND_API.G_RET_STS_ERROR;
10337 
10338         -- 2716220
10339         ELSIF p_line_rec.ship_set_id is not null
10340 --          AND p_line_rec.schedule_status_code is not null THEN
10341         THEN
10342            BEGIN
10343               SELECT 'Y'
10344               INTO   l_overridden
10345               FROM   oe_order_lines_all
10346               WHERE  header_id = p_line_rec.header_id
10347               AND    line_id <> p_line_rec.line_id
10348               AND    ship_set_id = p_line_rec.ship_set_id
10349               AND    override_atp_date_code = 'Y'
10350               AND    schedule_ship_date <> p_line_rec.schedule_ship_date
10351               AND    rownum < 2;
10352            EXCEPTION
10353               WHEN OTHERS THEN
10354                  NULL;
10355            END;
10356         ELSIF p_line_rec.arrival_set_id is not null
10357  --         AND p_line_rec.schedule_status_code is not null THEN
10358         THEN
10359            BEGIN
10360               SELECT 'Y'
10361               INTO   l_overridden
10362               FROM   oe_order_lines_all
10363               WHERE  header_id = p_line_rec.header_id
10364               AND    line_id <> p_line_rec.line_id
10365               AND    arrival_set_id = p_line_rec.arrival_set_id
10366               AND    override_atp_date_code = 'Y'
10367               AND    schedule_arrival_date <> p_line_rec.schedule_arrival_date
10368               AND    rownum < 2;
10369            EXCEPTION
10370               WHEN OTHERS THEN
10371                  NULL;
10372            END;
10373 
10374         END IF; -- ship from
10375         IF l_overridden = 'Y' THEN
10376         --3517527 set_status check commented
10377         --  IF l_set_rec.set_status <> 'T' THEN
10378            FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
10379            OE_MSG_PUB.Add;
10380         --  END IF;
10381            IF l_debug_level  > 0 THEN
10382               oe_debug_pub.add(  'E27 UNABLE TO ADD THE line TO THE SET' , 1 ) ;
10383            END IF;
10384            x_return_status := FND_API.G_RET_STS_ERROR;
10385         END IF;
10386 
10387        END IF; -- override
10388 
10389         -- If a non smc model or kit has lines that are overridden for
10390         -- different dates, stop adding the model to set.
10391 
10392        IF p_line_rec.top_model_line_id = p_line_rec.line_id
10393        AND nvl(p_line_rec.ship_model_complete_flag,'N') = 'N'
10394        AND p_line_rec.ato_line_id is null THEN
10395 
10396           IF NOT Valid_Set_Addition
10397              (p_top_model_line_id  => p_line_rec.top_model_line_id,
10398               p_set_type           => l_set_rec.set_type) THEN
10399 
10400            FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
10401            OE_MSG_PUB.Add;
10402            IF l_debug_level  > 0 THEN
10403                oe_debug_pub.add(  'E28 UNABLE TO ADD THE Model TO THE SET' , 1 ) ;
10404            END IF;
10405            x_return_status := FND_API.G_RET_STS_ERROR;
10406 
10407           END IF; -- valid
10408        END IF; -- top model
10409     END IF; -- ship set.
10410 
10411   END IF; -- pack I check
10412   --END 1282873
10413   -- Pack J
10414   -- Honoring Latest Acceptable Date
10415   -- 3940632 : Dates truncated prior to comparison.
10416   IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
10417 
10418       l_order_date_type_code := NVL(Get_Date_Type(p_line_rec.header_id), 'SHIP');
10419       IF  (p_sch_action = OESCH_ACT_SCHEDULE
10420           OR (p_sch_action = OESCH_ACT_RESERVE
10421              AND p_line_rec.schedule_status_code IS NULL)
10422           OR p_sch_action = OESCH_ACT_RESCHEDULE)
10423          AND OE_SYS_PARAMETERS.value ('LATEST_ACCEPTABLE_DATE_FLAG') = 'H'
10424          AND NVL(p_line_rec.override_atp_date_code, 'N') = 'N'
10425 
10426  --Bug 6400995
10427          --Honoring of LAD should be done only when updating/creating lines and not while cancelling
10428          AND nvl(p_line_rec.cancelled_flag,'N') = 'N'
10429 
10430      THEN
10431 
10432          IF trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date))
10433                                                        < trunc(p_line_rec.request_date) THEN
10434             IF l_debug_level  > 0 THEN
10435                oe_debug_pub.add ('E29 Request date exceeds Latest Acceptable Date',1);
10436             END IF;
10437             FND_MESSAGE.SET_NAME('ONT','ONT_SCH_REQUEST_EXCEED_LAD');
10438             OE_MSG_PUB.Add;
10439             x_return_status := FND_API.G_RET_STS_ERROR;
10440          ELSIF ((l_order_date_type_code = 'SHIP'
10441             AND trunc(NVL(p_line_rec.schedule_ship_date,p_line_rec.request_date))
10442                            >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))
10443           OR (l_order_date_type_code = 'ARRIVAL'
10444             AND trunc(NVL(p_line_rec.schedule_arrival_date, p_line_rec.request_date))
10445                           >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))) THEN
10446            IF l_debug_level  > 0 THEN
10447               oe_debug_pub.add('E30 Schedule date exceeds Latest Acceptable Date',1);
10448            END IF;
10449            FND_MESSAGE.SET_NAME('ONT','ONT_SCH_LAD_SCH_FAILED');
10450            OE_MSG_PUB.Add;
10451            x_return_status := FND_API.G_RET_STS_ERROR;
10452          END IF;
10453          -- 3894212
10454          IF ((p_line_rec.ship_set_id IS NOT NULL AND
10455            NOT oe_globals.equal(p_line_rec.ship_set_id,
10456                              p_old_line_rec.ship_set_id))  OR
10457             (p_line_rec.arrival_set_id IS NOT NULL AND
10458            NOT oe_globals.equal(p_line_rec.arrival_set_id,
10459                             p_old_line_rec.arrival_set_id))) THEN
10460 
10461             l_set_rec := OE_ORDER_CACHE.Load_Set
10462                   (nvl(p_line_rec.arrival_set_id,p_line_rec.ship_set_id));
10463 
10464             IF ((l_order_date_type_code = 'SHIP'
10465                AND trunc(l_set_rec.schedule_ship_date)
10466                            >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))
10467                OR (l_order_date_type_code = 'ARRIVAL'
10468                AND trunc(l_set_rec.schedule_arrival_date)
10469                           >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date))))
10470                AND l_set_rec.schedule_arrival_date IS NOT NULL
10471                AND l_set_rec.schedule_ship_date IS NOT NULL THEN
10472                IF l_debug_level  > 0 THEN
10473                   oe_debug_pub.add('E30.1 Schedule date exceeds Latest Acceptable Date',1);
10474                END IF;
10475                FND_MESSAGE.SET_NAME('ONT','ONT_SCH_LAD_SCH_FAILED');
10476                OE_MSG_PUB.Add;
10477                x_return_status := FND_API.G_RET_STS_ERROR;
10478             END IF;
10479          END IF;
10480          --
10481       END IF;
10482    END IF;
10483  -- End Pack J
10484     -- 3288838 --
10485     -- 3361753 subinventory check added
10486    l_found := 'N';
10487    --4653097
10488    IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y'
10489     AND p_sch_action = OESCH_ACT_RESERVE
10490     AND OE_GLOBALS.Equal(p_old_line_rec.subinventory,
10491                          p_line_rec.subinventory)
10492     AND OE_GLOBALS.Equal(p_old_line_rec.project_id,
10493                          p_line_rec.project_id)
10494     AND OE_GLOBALS.Equal(p_old_line_rec.task_id,
10495                          p_line_rec.task_id) THEN
10496       BEGIN
10497          IF l_debug_level > 0 THEN
10498             OE_DEBUG_PUB.Add('Before checking for Staged/Closed deliveries', 1);
10499          END IF;
10500 
10501          SELECT 'Y'
10502          INTO   l_found
10503          FROM   WSH_DELIVERY_DETAILS
10504          WHERE  SOURCE_LINE_ID = p_line_rec.line_id
10505          AND    SOURCE_CODE = 'OE'
10506          AND    RELEASED_STATUS IN ('Y', 'C');
10507 
10508          IF l_debug_level > 0 THEN
10509             OE_DEBUG_PUB.Add('Staged/Closed deliveries exist for the line', 3);
10510          END IF;
10511       EXCEPTION
10512          WHEN NO_DATA_FOUND THEN
10513             NULL;
10514          WHEN TOO_MANY_ROWS THEN
10515             l_found :='Y';
10516       END;
10517       IF l_found = 'Y' THEN
10518          IF l_debug_level  > 0 THEN
10519             oe_debug_pub.add('E31 Reservation(s) cannot be created.  The line has been pick confirmed/staged',1);
10520          END IF;
10521          FND_MESSAGE.SET_NAME('ONT','ONT_SCH_RSV_FAILURE_STAGED');
10522          OE_MSG_PUB.Add;
10523          x_return_status := FND_API.G_RET_STS_ERROR;
10524       END IF;
10525    END IF;
10526 
10527   IF l_debug_level  > 0 THEN
10528      oe_debug_pub.add(  '..EXITING OE_SCHEDULE_UTIL.VALIDATE_LINE WITH ' || X_RETURN_STATUS , 1 ) ;
10529   END IF;
10530 
10531 
10532 EXCEPTION
10533    WHEN FND_API.G_EXC_ERROR THEN
10534 
10535         x_return_status := FND_API.G_RET_STS_ERROR;
10536 
10537    WHEN OTHERS THEN
10538 
10539         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10540 
10541         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
10542         THEN
10543             OE_MSG_PUB.Add_Exc_Msg
10544             (   G_PKG_NAME,
10545               'Validate_Line');
10546         END IF;
10547 END Validate_Line;
10548 /*---------------------------------------------------------------------
10549 Procedure Name : Process_Request
10550 Description    : This procedure is called from the Schedule_Line procedure
10551                  to schedule a SINGLE LINE (a set or a parent line  is scheduled
10552                  in different procedure). The single line could be a part
10553                  of the set which is getting scheduled independently
10554                  (because there was not change in the set related attribute),
10555                  or it could be just a simple standard line which does not
10556                  belong to any set.
10557 
10558 --------------------------------------------------------------------- */
10559 Procedure Process_request(p_old_line_rec  IN OE_ORDER_PUB.line_rec_type,
10560                           p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
10561                           p_caller        IN VARCHAR2,
10562                           p_sch_action    IN VARCHAR2,
10563 x_return_status OUT NOCOPY VARCHAR2)
10564 
10565 IS
10566 l_update_flag    VARCHAR2(1):= FND_API.G_TRUE;
10567 l_orig_line_rec  OE_ORDER_PUB.line_rec_type;
10568 l_reserve_later  VARCHAR2(1) := 'N';
10569 l_qty_to_reserve NUMBER;
10570 l_qty2_to_reserve NUMBER; -- INVCONV
10571 --
10572 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
10573 --
10574 l_return_status       VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS; --Bug 5343902
10575 BEGIN
10576 
10577   IF l_debug_level  > 0 THEN
10578       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.PROCESS_REQUEST' , 1 ) ;
10579   END IF;
10580   IF l_debug_level  > 0 THEN
10581       oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG : ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
10582   END IF;
10583 
10584 -- need to retain orig record to call process order.
10585 
10586   l_orig_line_rec := p_x_line_rec;
10587   x_return_status   := FND_API.G_RET_STS_SUCCESS;
10588 
10589   IF p_sch_action  = OESCH_ACT_SCHEDULE THEN
10590 
10591     -- Based on the p_sch_action  and schedule_status_code call action_schedule.
10592 
10593      IF l_debug_level  > 0 THEN
10594          oe_debug_pub.add(  'NEW' || P_X_LINE_REC.SCHEDULE_SHIP_DATE ) ;
10595      END IF;
10596      IF l_debug_level  > 0 THEN
10597          oe_debug_pub.add(  'OLD' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE ) ;
10598      END IF;
10599 
10600      IF l_debug_level  > 0 THEN
10601          oe_debug_pub.add(  'OLD AD ' || P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
10602      END IF;
10603      IF l_debug_level  > 0 THEN
10604          oe_debug_pub.add(  'NEW AD ' || P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
10605      END IF;
10606 
10607      /* Added code for bug 5343902 */
10608     IF nvl(p_x_line_rec.model_remnant_flag, 'N')  = 'Y' AND
10609        p_x_line_rec.item_type_code in ('KIT', 'CLASS','MODEL')  AND
10610        p_x_line_rec.explosion_date IS NULL
10611     THEN
10612       oe_debug_pub.add('Remnant ' || p_x_line_rec.item_type_code || ', Calling Process_Included_Items', 5);
10613 
10614       l_return_status := OE_CONFIG_UTIL.Process_Included_Items
10615                          (p_line_id   => p_x_line_rec.line_id,
10616                           p_freeze    => FALSE);
10617 
10618       IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10619          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10620       ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
10621          RAISE FND_API.G_EXC_ERROR;
10622       END IF;
10623 
10624        oe_debug_pub.add('After calling Process_Included_Items', 5);
10625     END IF;
10626     /* End of code changes for bug 5343902 */
10627 
10628      Action_Schedule(p_x_line_rec     => p_x_line_rec,
10629                      p_old_line_rec   => p_old_line_rec,
10630                      p_sch_action     => OESCH_ACT_SCHEDULE,
10631                      p_qty_to_reserve => p_x_line_rec.reserved_quantity,
10632                      x_return_status  => x_return_status);
10633 
10634      IF l_debug_level  > 0 THEN
10635          oe_debug_pub.add(  'AFTER ACTION SCHEDULE : ' || X_RETURN_STATUS , 1 ) ;
10636      END IF;
10637 
10638      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10639           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10640      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10641 
10642 /* Added two extra conditions in the following if condition to fix the Bug 2894100 */
10643 
10644          IF  OESCH_AUTO_SCH_FLAG = 'Y'
10645          AND p_x_line_rec.schedule_ship_date is null
10646          AND p_x_line_rec.schedule_arrival_date is null
10647          -- QUOTING changes - check for complete negotiation also
10648          -- as it can trigger auto-scheduling
10649          AND (p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
10650               OR OE_Quote_Util.G_COMPLETE_NEG = 'Y'
10651              )
10652          THEN
10653          -- We donot want to error out the insert if autoscheduling
10654          -- failed. So we will return success. We also do not want to
10655          -- do any update, so we will set the l_update_flag to FALSE.
10656 
10657             l_update_flag     := FND_API.G_FALSE;
10658             x_return_status   := FND_API.G_RET_STS_SUCCESS;
10659 
10660          ELSE
10661 
10662             RAISE FND_API.G_EXC_ERROR;
10663 
10664          END IF;
10665      END IF;
10666 
10667   ELSIF p_sch_action  = OESCH_ACT_RESCHEDULE THEN
10668 
10669 IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
10670                             p_old_line_rec.inventory_item_id)
10671      AND p_x_line_rec.source_type_code = 'EXTERNAL' THEN -- 7139462
10672 
10673         p_x_line_rec.schedule_action_code := OESCH_ACT_UNSCHEDULE;
10674 
10675         Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
10676                         p_old_line_rec  => p_old_line_rec,
10677                         p_sch_action    => OESCH_ACT_UNSCHEDULE,
10678                         x_return_status => x_return_status);
10679         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10680            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10681         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10682            RAISE FND_API.G_EXC_ERROR;
10683         END IF;
10684  ELSE
10685        Action_Reschedule(p_x_line_rec    => p_x_line_rec,
10686                          p_old_line_rec  => p_old_line_rec,
10687                          x_return_status => x_return_status,
10688                          x_reserve_later => l_reserve_later);
10689 
10690         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10691            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10692         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10693            RAISE FND_API.G_EXC_ERROR;
10694         END IF;
10695 END IF;
10696 
10697   ELSIF p_sch_action  = OESCH_ACT_RESERVE THEN
10698 
10699      -- If some one has passes action as reserve on a
10700      -- unscheduled line
10701      IF p_x_line_rec.schedule_status_code IS NULL THEN
10702 
10703         Action_Schedule(p_x_line_rec     => p_x_line_rec,
10704                         p_old_line_rec   => p_old_line_rec,
10705                         p_sch_action     => OESCH_ACT_RESERVE,
10706                         p_qty_to_reserve => p_x_line_rec.reserved_quantity,
10707                         x_return_status  => x_return_status);
10708 
10709         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10710            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10711         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10712            RAISE FND_API.G_EXC_ERROR;
10713         END IF;
10714 
10715      ELSE
10716 
10717         Action_reserve(p_x_line_rec    => p_x_line_rec,
10718                        p_old_line_rec  => p_old_line_rec,
10719                        x_return_status => x_return_status);
10720 
10721         l_update_flag := FND_API.G_FALSE;
10722 
10723         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10724            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10725         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10726            RAISE FND_API.G_EXC_ERROR;
10727         END IF;
10728 
10729 
10730      END IF;
10731 
10732 
10733   ELSIF p_sch_action   = OESCH_ACT_UNRESERVE
10734   THEN
10735 
10736     -- Setting update flag to false, so that schedule_line does not
10737     -- process_order as unreserving does not cause any line attributes
10738     -- to change.
10739 
10740     l_update_flag := FND_API.G_FALSE;
10741 
10742     -- We should never use this action unless the
10743     -- old_status is reserved .
10744 
10745      IF (p_old_line_rec.reserved_quantity is not null AND
10746          p_old_line_rec.reserved_quantity <> FND_API.G_MISS_NUM)
10747      THEN
10748     -- shipping_interfaced_flag
10749        Unreserve_Line
10750         (p_line_rec              => p_old_line_rec,
10751          p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
10752          p_quantity2_to_unreserve => p_old_line_rec.reserved_quantity2, -- INVCONV
10753          x_return_status         => x_return_status);
10754 
10755         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10756            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10757         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10758            RAISE FND_API.G_EXC_ERROR;
10759         END IF;
10760 
10761      ELSE
10762        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
10763        OE_MSG_PUB.Add;
10764        l_update_flag := FND_API.G_FALSE;
10765      END IF;
10766 
10767 
10768   -- schedule_action_code -->  OESCH_ACT_UNDEMAND
10769 
10770   ELSIF p_sch_action  = OESCH_ACT_UNDEMAND
10771   THEN
10772      IF p_x_line_rec.Schedule_status_code IS NOT NULL THEN
10773 
10774         Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
10775                           p_old_line_rec  => p_old_line_rec,
10776                           p_sch_action    => OESCH_ACT_UNDEMAND,
10777                           x_return_status => x_return_status);
10778 
10779         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10780            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10781         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10782            RAISE FND_API.G_EXC_ERROR;
10783         END IF;
10784 
10785      ELSE
10786        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
10787        OE_MSG_PUB.Add;
10788        l_update_flag := FND_API.G_FALSE;
10789      END IF;
10790 
10791   ELSIF p_sch_action  = OESCH_ACT_UNSCHEDULE THEN
10792      -- When action is passed as unschedule.
10793      -- Or if the line's source type is being changed from INTERNAL to
10794      -- EXTERNAL, and the old line was scheduled, we need to unschedule it.
10795 
10796       p_x_line_rec.schedule_action_code := OESCH_ACT_UNSCHEDULE;
10797       Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
10798                         p_old_line_rec  => p_old_line_rec,
10799                         p_sch_action    => OESCH_ACT_UNSCHEDULE,
10800                         x_return_status => x_return_status);
10801 
10802       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10803          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10804       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10805          RAISE FND_API.G_EXC_ERROR;
10806       END IF;
10807 
10808   END IF; -- Main If.
10809 
10810   IF l_update_flag = FND_API.G_TRUE
10811   THEN
10812 /*
10813      Call_Process_Order(p_old_line_rec    => p_old_line_rec,
10814                         p_x_line_rec      => p_x_line_rec,
10815                         p_write_to_db     => p_write_to_db,
10816                         p_caller          => p_caller,
10817                         x_return_status   => x_return_status);
10818 */
10819 
10820      Process_Line (p_old_line_rec    => l_orig_line_rec,
10821                    p_x_line_rec      => p_x_line_rec,
10822                    p_caller          => p_caller,
10823                    x_return_status   => x_return_status);
10824 
10825                                         IF l_debug_level  > 0 THEN
10826                                             oe_debug_pub.add(  'AFTER CALLING CALL_PROCESS_ORDER :' || X_RETURN_STATUS , 1 ) ;
10827                                         END IF;
10828 
10829       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10830          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10831       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10832          RAISE FND_API.G_EXC_ERROR;
10833       END IF;
10834 
10835       IF l_reserve_later = 'Y' THEN
10836                                  IF l_debug_level  > 0 THEN
10837                                      oe_debug_pub.add(  'RESERVE THE NEW ITEM: ' || P_X_LINE_REC.INVENTORY_ITEM_ID , 1 ) ;
10838                                  END IF;
10839         IF p_x_line_rec.shippable_flag = 'Y' THEN
10840 
10841 
10842           IF (nvl(p_x_line_rec.reserved_quantity,0) >
10843                  p_x_line_rec.ordered_quantity)
10844           OR nvl(p_x_line_rec.reserved_quantity,0) = 0
10845           THEN
10846              l_qty_to_reserve := p_x_line_rec.ordered_quantity;
10847           ELSE
10848              l_qty_to_reserve := p_x_line_rec.reserved_quantity;
10849           END IF;
10850 
10851                     IF (nvl(p_x_line_rec.reserved_quantity2,0) >   -- INVCONV
10852                  nvl(p_x_line_rec.ordered_quantity2,0) )
10853           OR nvl(p_x_line_rec.reserved_quantity2,0) = 0
10854           THEN
10855              l_qty2_to_reserve := p_x_line_rec.ordered_quantity2;
10856           ELSE
10857              l_qty2_to_reserve := p_x_line_rec.reserved_quantity2;
10858           END IF;
10859 
10860           IF l_qty2_to_reserve = 0 THEN -- INVCONV
10861                 l_qty2_to_reserve := null;
10862           end if;
10863 
10864           IF l_debug_level  > 0 THEN
10865            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
10866                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
10867        END IF;
10868 
10869 
10870           Reserve_Line
10871           ( p_line_rec             => p_x_line_rec
10872           , p_quantity_to_reserve  => l_qty_to_reserve
10873           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
10874           , x_return_Status        => x_return_status);
10875 
10876 
10877         END IF; -- Shippable flag
10878       END IF; -- reserve later
10879 
10880   END IF; -- update flag
10881 
10882   -- If schedule date has change, we need to call PO callback function
10883   -- to indicate the change.
10884 
10885   -- IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE AND --3888871
10886   IF p_x_line_rec.source_document_type_id = 10 AND --3888871
10887      NOT OE_GLOBALS.EQUAL(p_x_line_rec.schedule_arrival_date,
10888                           p_old_line_rec.schedule_arrival_date)
10889   THEN
10890      IF l_debug_level  > 0 THEN
10891         oe_debug_pub.add(  'PASSING SCHEDULE_ARRIVAL_DATE TO PO ' , 3 ) ;
10892      END IF;
10893      Update_PO(p_x_line_rec.schedule_arrival_date,
10894                p_x_line_rec.source_document_id,
10895                p_x_line_rec.source_document_line_id);
10896      IF l_debug_level  > 0 THEN
10897          oe_debug_pub.add(  'AFTER PO CALL BACK' , 3 ) ;
10898      END IF;
10899   END IF;
10900 
10901   IF l_debug_level  > 0 THEN
10902      oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.PROCESS_REQUEST: ' || X_RETURN_STATUS , 1 ) ;
10903   END IF;
10904 
10905 
10906 EXCEPTION
10907   WHEN FND_API.G_EXC_ERROR THEN
10908     x_return_status := FND_API.G_RET_STS_ERROR;
10909 
10910   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10911     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10912 
10913   WHEN OTHERS THEN
10914     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10915 
10916     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
10917     THEN
10918       OE_MSG_PUB.Add_Exc_Msg
10919       (   G_PKG_NAME
10920       ,   'Process_request'
10921        );
10922     END IF;
10923 
10924 END Process_request;
10925 
10926 /*-----------------------------------------------------------------------------
10927 Procedure Name : Get_Lead_Time
10928 Description    : This function returns the manufacturing lead team for ATO
10929                  Options and Classes. While performing ATP, and scheduling
10930                  for an ATO configuration, we just don't have to check
10931 the availability of the items, we also need to find out nocopy
10932 
10933                  the amount of time it takes to build those items.
10934                  This procedure gives the time it takes to build the ATO.
10935                  It is standard formula which is used. The value is derived
10936                  from the ATO model. Thus all options for a given model
10937                  will have the same Lead Time.
10938 ----------------------------------------------------------------------------- */
10939 FUNCTION Get_Lead_Time
10940 ( p_ato_line_id      IN NUMBER
10941 , p_ship_from_org_id IN NUMBER)
10942 RETURN NUMBER
10943 IS
10944 l_model_ordered_quantity  NUMBER := 0;
10945 l_model_order_qty_uom     NUMBER := 0;
10946 primary_model_qty         NUMBER := 0;
10947 st_lead_time              NUMBER := 0;
10948 db_full_lead_time         NUMBER := 0;
10949 db_fixed_lead_time        NUMBER := 0;
10950 db_variable_lead_time     NUMBER := 0;
10951 db_primary_uom_code       VARCHAR2(3);
10952 db_model_item_id          NUMBER := 0;
10953 db_line_unit_code         VARCHAR2(3);
10954 --
10955 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
10956 --
10957 BEGIN
10958 
10959   IF l_debug_level  > 0 THEN
10960       oe_debug_pub.add(  'ENTERING GET_LEAD_TIME' , 1 ) ;
10961   END IF;
10962   IF l_debug_level  > 0 THEN
10963       oe_debug_pub.add(  'ATO LINE IS ' || P_ATO_LINE_ID , 1 ) ;
10964   END IF;
10965   IF l_debug_level  > 0 THEN
10966       oe_debug_pub.add(  'SHIP FROM IS ' || P_SHIP_FROM_ORG_ID , 1 ) ;
10967   END IF;
10968 
10969   SELECT     NVL ( MSI.FULL_LEAD_TIME , 0 )
10970              , NVL ( MSI.FIXED_LEAD_TIME , 0 )
10971              , NVL ( MSI.VARIABLE_LEAD_TIME , 0 )
10972              , MSI.PRIMARY_UOM_CODE
10973              , NVL ( OL.INVENTORY_ITEM_ID , 0 )
10974              , OL.order_quantity_uom
10975              , OL.ordered_quantity
10976   INTO       db_full_lead_time
10977              , db_fixed_lead_time
10978              , db_variable_lead_time
10979              , db_primary_uom_code
10980              , db_model_item_id
10981              , db_line_unit_code
10982              , primary_model_qty
10983   FROM    MTL_SYSTEM_ITEMS MSI
10984           , OE_ORDER_LINES OL
10985   WHERE   MSI.INVENTORY_ITEM_ID  = OL.INVENTORY_ITEM_ID
10986   AND     MSI.ORGANIZATION_ID    = p_ship_from_org_id
10987   AND     OL.LINE_ID             = p_ato_line_id ;
10988 
10989 
10990   -- Get the model quantity in primary UOM
10991 
10992   -- Set the Lead time
10993 
10994   st_lead_time :=  ceil( nvl(db_fixed_lead_time,0) + nvl(db_variable_lead_time,0)
10995                          * nvl(primary_model_qty,0));
10996 
10997   IF nvl(db_full_lead_time,0) > nvl(st_lead_time,0) THEN
10998      st_lead_time := ceil(db_full_lead_time);
10999   END IF;
11000 
11001   RETURN st_lead_time;
11002 EXCEPTION
11003    WHEN NO_DATA_FOUND THEN
11004         RETURN 0;
11005    WHEN OTHERS THEN
11006         RETURN 0;
11007 END Get_Lead_Time;
11008 
11009 
11010 /*-----------------------------------------------------------------------------
11011 Procedure Name : Get_Date_Type
11012 Description    : This procedure returns the date type of the order.
11013                  The date type could be SHIP or ARRIVAl or null. Null
11014                  value is treated at SHIP in the scheduling code.
11015 -----------------------------------------------------------------------------*/
11016 
11017 FUNCTION Get_Date_Type
11018 ( p_header_id      IN NUMBER)
11019 RETURN VARCHAR2
11020 IS
11021 l_order_date_type_code   VARCHAR2(30) := null;
11022 --
11023 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11024 --
11025 BEGIN
11026 
11027    IF p_header_id <> nvl(G_HEADER_ID,0) THEN
11028        BEGIN
11029           SELECT order_date_type_code
11030           INTO   l_order_date_type_code
11031           FROM   oe_order_headers
11032           WHERE  header_id = p_header_id;
11033 
11034           G_HEADER_ID := p_header_id;
11035           G_DATE_TYPE := l_order_date_type_code;
11036        EXCEPTION
11037           WHEN OTHERS THEN
11038                RETURN null;
11039        END;
11040    ELSE
11041        l_order_date_type_code := G_DATE_TYPE;
11042    END IF;
11043 
11044    RETURN l_order_date_type_code;
11045 
11046 EXCEPTION
11047    WHEN NO_DATA_FOUND THEN
11048         RETURN NULL;
11049 END Get_Date_Type;
11050 
11051 /*--------------------------------------------------------------------------
11052 Procedure Name : Get_Order_Number
11053 Description    : This procedure returns the order_number from the header
11054         record, which we will pass to the MRP API.
11055 --------------------------------------------------------------------------*/
11056 FUNCTION Get_Order_Number(p_header_id in number)
11057 RETURN NUMBER
11058 IS
11059 l_order_number NUMBER;
11060 --
11061 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11062 --
11063 BEGIN
11064 
11065   IF l_debug_level  > 0 THEN
11066       oe_debug_pub.add(  'ENTERING GET_ORDER_NUMBER: ' || P_HEADER_ID , 1 ) ;
11067   END IF;
11068 
11069   IF p_header_id is not null AND p_header_id  <> FND_API.G_MISS_NUM
11070   THEN
11071      BEGIN
11072         select order_number
11073         into l_order_number
11074         from oe_order_headers
11075         where header_id = p_header_id;
11076      EXCEPTION
11077         WHEN OTHERS THEN
11078              RETURN null;
11079      END;
11080   END IF;
11081 
11082   IF l_debug_level  > 0 THEN
11083       oe_debug_pub.add(  'ORDER NUMBER : ' || L_ORDER_NUMBER ) ;
11084   END IF;
11085   RETURN l_order_number;
11086 EXCEPTION
11087    WHEN OTHERS THEN
11088         RETURN null;
11089 END Get_Order_Number;
11090 
11091 /*--------------------------------------------------------------------------
11092 Procedure Name : Get_mtl_sales_order_id
11093 Description    : This funtion returns the SALES_ORDER_ID (frm mtl_sales_orders)
11094                  for a given heeader_id.
11095                  Every header in oe_order_headers_all will have a record
11096                  in MTL_SALES_ORDERS. The unique key to get the sales_order_id
11097                  from mtl_sales_orders is
11098                  Order_Number
11099                  Order_Type (in base language)
11100                  OM:Source Code profile option (stored as ont_source_code).
11101 
11102                  The above values are stored in a flex in MTL_SALES_ORDERS.
11103                  SEGMENT1 : stores the order number
11104                  SEGMENT2 : stores the order type
11105                  SEGMENT3 : stores the ont_source_code value
11106 
11107 -------------------------------------------------------------------------- */
11108 FUNCTION Get_mtl_sales_order_id(p_header_id IN NUMBER,
11109                                 p_order_type_id IN NUMBER DEFAULT NULL)
11110 RETURN NUMBER
11111 IS
11112 l_source_code              VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
11113 l_sales_order_id           NUMBER := 0;
11114 l_order_type_name          VARCHAR2(80);
11115 l_order_type_id            NUMBER;
11116 --
11117 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11118 --
11119 BEGIN
11120 
11121    IF l_debug_level  > 0 THEN
11122        oe_debug_pub.add(  'ENTERING GET_MTL_SALES_ORDER_ID' , 2 ) ;
11123    END IF;
11124    -- 3748723
11125    IF p_order_type_id IS NULL THEN
11126 
11127       IF OE_Order_Cache.g_header_rec.order_type_id IS NOT NULL
11128       AND OE_Order_Cache.g_header_rec.header_id = p_header_id THEN  --Bug4683211
11129 
11130          IF l_debug_level  > 0 THEN
11131             oe_debug_pub.add(  'From Header Cache ' , 2 ) ;
11132          END IF;
11133          l_order_type_id := OE_Order_Cache.g_header_rec.order_type_id;
11134       ELSE
11135 
11136          BEGIN
11137             SELECT order_type_id
11138             INTO   l_order_type_id
11139             FROM   oe_order_headers_all
11140             WHERE  header_id = p_header_id;
11141          EXCEPTION
11142             WHEN OTHERS THEN
11143                RAISE;
11144          END;
11145       END IF;
11146    ELSE
11147       l_order_type_id:=p_order_type_id;
11148    END IF;
11149 
11150    IF l_debug_level  > 0 THEN
11151        oe_debug_pub.add(  'ORDER TYPE ID :' || L_ORDER_TYPE_ID , 2 ) ;
11152    END IF;
11153 
11154    --3748723
11155    IF l_order_type_id = oe_schedule_util.sch_cached_mtl_order_type_id AND
11156       oe_schedule_util.sch_cached_mtl_order_type_name IS NOT NULL THEN
11157       IF l_debug_level  > 0 THEN
11158          oe_debug_pub.add(  'From order_type_id_Cache ' , 2 ) ;
11159       END IF;
11160       l_order_type_name := oe_schedule_util.sch_cached_mtl_order_type_name;
11161    ELSE
11162       BEGIN
11163          SELECT NAME
11164          INTO   l_order_type_name
11165          FROM   OE_TRANSACTION_TYPES_TL
11166          WHERE  TRANSACTION_TYPE_ID = l_order_type_id
11167          AND    language = (select language_code
11168                             from fnd_languages
11169                             where installed_flag = 'B');
11170          oe_schedule_util.sch_cached_mtl_order_type_name := l_order_type_name;
11171  --Begin Bug#6719001
11172          oe_schedule_util.sch_cached_mtl_order_type_id := l_order_type_id;
11173          --End Bug#6719001
11174       EXCEPTION
11175          WHEN NO_DATA_FOUND THEN
11176             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11177       END;
11178    END IF;
11179    IF l_debug_level  > 0 THEN
11180        oe_debug_pub.add(  'ORDER TYPE: ' || L_ORDER_TYPE_NAME , 2 ) ;
11181    END IF;
11182    IF l_debug_level  > 0 THEN
11183        oe_debug_pub.add(  'SOURCE CODE: ' || L_SOURCE_CODE , 2 ) ;
11184    END IF;
11185 
11186    --3748723
11187    IF l_order_type_name = oe_schedule_util.sch_cached_mtl_order_type_name AND
11188       l_source_code = oe_schedule_util.sch_cached_mtl_source_code AND
11189       p_header_id = oe_schedule_util.sch_cached_mtl_header_id THEN
11190 
11191       IF l_debug_level  > 0 THEN
11192          oe_debug_pub.add(  'CACHED SALES_ORDER_ID ' || oe_schedule_util.sch_cached_mtl_sales_order_id , 2 ) ;
11193       END IF;
11194 
11195       RETURN oe_schedule_util.sch_cached_mtl_sales_order_id;
11196    ELSE
11197       SELECT S.SALES_ORDER_ID
11198       INTO l_sales_order_id
11199       FROM MTL_SALES_ORDERS S,
11200            OE_ORDER_HEADERS_ALL H
11201       WHERE S.SEGMENT1 = TO_CHAR(H.ORDER_NUMBER)
11202       AND S.SEGMENT2 = l_order_type_name
11203       AND S.SEGMENT3 = l_source_code
11204       AND H.HEADER_ID = p_header_id;
11205       --3748723
11206       oe_schedule_util.sch_cached_mtl_header_id := p_header_id;
11207       oe_schedule_util.sch_cached_mtl_source_code :=l_source_code;
11208       oe_schedule_util.sch_cached_mtl_sales_order_id := l_sales_order_id;
11209       oe_schedule_util.sch_cached_mtl_order_type_id := l_order_type_id;
11210    END IF;
11211 
11212    IF l_debug_level  > 0 THEN
11213        oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 2 ) ;
11214    END IF;
11215 
11216    RETURN l_sales_order_id;
11217 EXCEPTION
11218     WHEN NO_DATA_FOUND THEN
11219        IF l_debug_level  > 0 THEN
11220            oe_debug_pub.add(  '2. L_SALES_ORDER_ID IS 0' , 2 ) ;
11221        END IF;
11222        RETURN 0;
11223     WHEN OTHERS THEN
11224        IF l_debug_level  > 0 THEN
11225            oe_debug_pub.add(  '2. L_SALES_ORDER_ID IS 0' , 2 ) ;
11226        END IF;
11227        RETURN 0;
11228 END Get_mtl_sales_order_id;
11229 /*--------------------------------------------------------------------------
11230 Procedure Name : Insert_Into_Mtl_Sales_Orders
11231 Description    : This API creates a record in MTL_SALES_ORDERS for a given
11232                  order header.
11233                  Every header in oe_order_headers_all will have a record
11234                  in MTL_SALES_ORDERS. The unique key to get the sales_order_id
11235                  from mtl_sales_orders is
11236                  Order_Number
11237                  Order_Type (in base language)
11238                  OM:Source Code profile option (stored as ont_source_code).
11239 
11240                  The above values are stored in a flex in MTL_SALES_ORDERS.
11241                  SEGMENT1 : stores the order number
11242                  SEGMENT2 : stores the order type
11243                  SEGMENT3 : stores the ont_source_code value
11244 
11245 -------------------------------------------------------------------------- */
11246 Procedure Insert_Into_Mtl_Sales_Orders
11247 ( p_header_rec       IN  OE_ORDER_PUB.header_rec_type)
11248 IS
11249 l_order_type_name          VARCHAR2(80);
11250 l_source_code              VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
11251 l_sales_order_id           NUMBER;
11252 l_msg_data                 VARCHAR2(2000);
11253 l_msg_count                NUMBER;
11254 l_return_status            VARCHAR2(1);
11255 --
11256 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11257 --
11258 BEGIN
11259 
11260   IF l_debug_level  > 0 THEN
11261       oe_debug_pub.add(  'ENTERING INSERT_INTO_MTL_SALES_ORDERS' , 1 ) ;
11262   END IF;
11263 
11264   BEGIN
11265   -- Fix for bug#1078323: the order type name should be selected in
11266   -- the base language
11267      SELECT NAME
11268      INTO l_order_type_name
11269      FROM OE_TRANSACTION_TYPES_TL
11270      WHERE TRANSACTION_TYPE_ID = p_header_rec.order_type_id
11271      AND language = (select language_code
11272                      from fnd_languages
11273                      where installed_flag = 'B');
11274   EXCEPTION
11275      WHEN NO_DATA_FOUND THEN
11276        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11277   END;
11278 
11279   IF l_debug_level  > 0 THEN
11280       oe_debug_pub.add(  'CALLING INVS CREATE_SALESORDER' , 1 ) ;
11281   END IF;
11282   IF l_debug_level  > 0 THEN
11283       oe_debug_pub.add(  'ORDER TYPE: ' || L_ORDER_TYPE_NAME , 1 ) ;
11284   END IF;
11285   IF l_debug_level  > 0 THEN
11286       oe_debug_pub.add(  'SOURCE CODE: ' || L_SOURCE_CODE , 1 ) ;
11287   END IF;
11288   IF l_debug_level  > 0 THEN
11289       oe_debug_pub.add(  'ORDER NUMBER : ' || P_HEADER_REC.ORDER_NUMBER , 1 ) ;
11290   END IF;
11291 
11292   inv_salesorder.create_salesorder
11293       ( p_api_version_number        => 1.0,
11294         p_segment1                  => p_header_rec.order_number,
11295         p_segment2                  => l_order_type_name,
11296         p_segment3                  => l_source_code,
11297         p_validation_date           => p_header_rec.creation_date,
11298         x_salesorder_id             => l_sales_order_id,
11299         x_message_data              => l_msg_data,
11300         x_message_count             => l_msg_count,
11301         x_return_status             => l_return_status);
11302 
11303 
11304   IF l_debug_level  > 0 THEN
11305       oe_debug_pub.add(  'L_MSG_COUNT ' || L_MSG_COUNT , 1 ) ;
11306   END IF;
11307   IF l_debug_level  > 0 THEN
11308       oe_debug_pub.add(  'RETURN STATUS ' || L_RETURN_STATUS , 1 ) ;
11309   END IF;
11310 
11311   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11312        oe_msg_pub.transfer_msg_stack;
11313        l_msg_count:=OE_MSG_PUB.COUNT_MSG;
11314        FOR I in 1..l_msg_count LOOP
11315           l_msg_data := OE_MSG_PUB.Get(I,'F');
11316           IF l_debug_level  > 0 THEN
11317               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
11318           END IF;
11319        END LOOP;
11320 
11321        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11322   ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
11323        oe_msg_pub.transfer_msg_stack;
11324        l_msg_count:=OE_MSG_PUB.COUNT_MSG;
11325        FOR I in 1..l_msg_count LOOP
11326           l_msg_data := OE_MSG_PUB.Get(I,'F');
11327           IF l_debug_level  > 0 THEN
11328               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
11329           END IF;
11330        END LOOP;
11331        RAISE FND_API.G_EXC_ERROR;
11332   END IF;
11333 
11334   IF l_debug_level  > 0 THEN
11335       oe_debug_pub.add(  'EXITING INSERT_INTO_MTL_SALES_ORDERS' , 1 ) ;
11336   END IF;
11337 
11338 EXCEPTION
11339 
11340     WHEN OTHERS THEN
11341 
11342         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
11343         THEN
11344             OE_MSG_PUB.Add_Exc_Msg
11345             (   G_PKG_NAME
11346             ,   'Insert_Into_Mtl_Sales_Orders'
11347             );
11348         END IF;
11349 
11350         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11351 
11352 END Insert_Into_Mtl_Sales_Orders;
11353 
11354 
11355 /*---------------------------------------------------------------------
11356 Procedure Name : call_process_order
11357 Description    : This process is called after scheduling is performed
11358                  on the line and the result needs to be verified and/or
11359                  updated to the database.
11360 --------------------------------------------------------------------- */
11361 Procedure call_process_order
11362 ( p_x_old_line_tbl      IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
11363 , p_x_line_tbl          IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
11364 , p_control_rec         IN  OE_GLOBALS.control_rec_type
11365 , p_caller              IN  VARCHAR2
11366 , x_return_status OUT NOCOPY VARCHAR2)
11367 
11368 IS
11369 is_set_recursion        VARCHAR2(1) := 'Y';
11370 --
11371 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11372 --
11373 BEGIN
11374 
11375     IF l_debug_level  > 0 THEN
11376         oe_debug_pub.add(  'ENTERING CALL_PROCESS_ORDER' , 1 ) ;
11377     END IF;
11378 
11379     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
11380     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
11381 
11382     IF l_debug_level  > 0 THEN
11383         oe_debug_pub.add(  'CALLING PROCESS ORDER' , 1 ) ;
11384     END IF;
11385 
11386      -- Set global set recursive flag
11387      -- The global flag to supress the sets logic to fire in
11388      -- get set id api in lines
11389     IF NOT oe_set_util.g_set_recursive_flag  THEN
11390        is_set_recursion := 'N';
11391        oe_set_util.g_set_recursive_flag := TRUE;
11392     END IF;
11393 
11394     --  Call OE_Order_PVT.Process_order
11395 
11396     OE_Order_PVT.Lines
11397     (p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
11398      p_control_rec                 => p_control_rec,
11399      p_x_line_tbl                  => p_x_line_tbl,
11400      p_x_old_line_tbl              => p_x_old_line_tbl,
11401      x_return_status               => x_return_status);
11402 
11403     -- unset global set recursive flag
11404     -- The global flag to supress the sets logic to
11405     -- fire in get set id api in lines
11406 
11407     IF is_set_recursion  = 'N' THEN
11408        is_set_recursion := 'Y';
11409        oe_set_util.g_set_recursive_flag := FALSE;
11410     END IF;
11411 
11412     IF l_debug_level  > 0 THEN
11413         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS ORDER' , 1 ) ;
11414     END IF;
11415     IF l_debug_level  > 0 THEN
11416         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
11417     END IF;
11418 
11419     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11420         IF l_debug_level  > 0 THEN
11421             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
11422         END IF;
11423         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11424     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11425         IF l_debug_level  > 0 THEN
11426             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
11427         END IF;
11428         RAISE FND_API.G_EXC_ERROR;
11429     END IF;
11430 
11431     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11432     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11433 
11434      -- Resetting the variable after the Process Order API Call.
11435      OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N';  -- Added for ER 6110708
11436 
11437     IF l_debug_level  > 0 THEN
11438         oe_debug_pub.add(  'EXITING CALL PROCESS ORDER' , 1 ) ;
11439     END IF;
11440 EXCEPTION
11441 
11442    WHEN FND_API.G_EXC_ERROR THEN
11443 
11444         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11445         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11446         x_return_status := FND_API.G_RET_STS_ERROR;
11447 
11448     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11449 
11450         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11451         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11452         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11453 
11454     WHEN OTHERS THEN
11455 
11456         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11457         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11458         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11459 
11460         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
11461         THEN
11462             OE_MSG_PUB.Add_Exc_Msg
11463             (   G_PKG_NAME
11464             ,   'Call_Process_Order'
11465             );
11466         END IF;
11467 END call_process_order;
11468 
11469 /*---------------------------------------------------------------------
11470 Procedure Name : Process_Group_of_Lines
11471 Description    : This process is called after scheduling is performed
11472                  on the line and the result needs to be verified and/or
11473                  updated to the database.
11474 --------------------------------------------------------------------- */
11475 Procedure Process_Group_of_Lines
11476 ( p_x_old_line_tbl      IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
11477 , p_x_line_tbl          IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
11478 , p_caller              IN  VARCHAR2
11479 , x_return_status OUT NOCOPY VARCHAR2)
11480 
11481 IS
11482 l_process_requests BOOLEAN;
11483 I                  NUMBER;
11484 l_index            NUMBER;
11485 --
11486 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11487 --
11488 BEGIN
11489 
11490     IF l_debug_level  > 0 THEN
11491         oe_debug_pub.add(  'ENTERING PROCESS_GROUP_OF_LINES' , 1 ) ;
11492     END IF;
11493     x_return_status :=  FND_API.G_RET_STS_SUCCESS;
11494     SAVEPOINT Process_Group_of_Lines;
11495     I := p_x_line_tbl.FIRST;
11496     WHILE I IS NOT NULL LOOP
11497      BEGIN
11498       IF l_debug_level  > 0 THEN
11499           oe_debug_pub.add(  'OPERATION : ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
11500       END IF;
11501       IF p_x_line_tbl(I).operation <> OE_GLOBALS.G_OPR_NONE
11502       THEN
11503          Process_Line (p_old_line_rec    => p_x_old_line_tbl(I),
11504                        p_x_line_rec      => p_x_line_tbl(I),
11505                        p_caller          => p_caller,
11506                        p_call_prn        => FALSE,
11507                        x_return_status   => x_return_status);
11508 
11509          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11510             IF l_debug_level  > 0 THEN
11511                 oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
11512             END IF;
11513             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11514          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11515            IF l_debug_level  > 0 THEN
11516                oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
11517            END IF;
11518            RAISE FND_API.G_EXC_ERROR;
11519          END IF;
11520 
11521          IF p_x_line_tbl(I).top_model_line_id = p_x_line_tbl(I).line_id
11522          THEN
11523            IF l_debug_level  > 0 THEN
11524                oe_debug_pub.add(  'CLEAR THE CACHED TOP MODEL RECORD' , 1 ) ;
11525            END IF;
11526            IF l_debug_level  > 0 THEN
11527                oe_debug_pub.add(  'MODEL LINE: '|| P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
11528            END IF;
11529            OE_Order_Cache.Clear_Top_Model_Line(p_key => p_x_line_tbl(I).line_id);
11530          END IF;
11531 
11532       ELSIF  nvl(p_x_line_tbl(I).open_flag,'Y') = 'N' THEN
11533 
11534 
11535          IF l_debug_level  > 0 THEN
11536              oe_debug_pub.add(  'BEFORE DOING DIRECT UPDATE IN GROUP' , 1 ) ;
11537          END IF;
11538 
11539          p_x_line_tbl(I).last_update_date := SYSDATE;
11540          p_x_line_tbl(I).last_updated_by := FND_GLOBAL.USER_ID;
11541          p_x_line_tbl(I).last_update_login := FND_GLOBAL.LOGIN_ID;
11542          p_x_line_tbl(I).lock_control := p_x_line_tbl(I).lock_control + 1;
11543 
11544    -- added for notification framework
11545    --check code release level first. Notification framework is at Pack H level
11546       IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
11547         -- calling notification framework to get index position
11548         OE_ORDER_UTIL.Update_Global_Picture(p_Upd_New_Rec_If_Exists =>False,
11549                     p_old_line_rec => p_x_old_line_tbl(I),
11550                     p_line_rec =>p_x_line_tbl(I),
11551                     p_line_id => p_x_line_tbl(I).line_id,
11552                     x_index => l_index,
11553                     x_return_status => x_return_status);
11554           IF l_debug_level  > 0 THEN
11555               oe_debug_pub.add(  'UPDATE_GLOBAL RETURN STATUS FROM OE_SCHEDULE_UTIL.PROCESS_GROUP_OF_LINE IS: ' || X_RETURN_STATUS ) ;
11556           END IF;
11557           IF l_debug_level  > 0 THEN
11558               oe_debug_pub.add(  'GLOBAL PICTURE INDEX IS: ' || L_INDEX , 1 ) ;
11559           END IF;
11560 
11561          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11562               IF l_debug_level  > 0 THEN
11563                   oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
11564               END IF;
11565            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11566          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11567              IF l_debug_level  > 0 THEN
11568                  oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
11569              END IF;
11570               RAISE FND_API.G_EXC_ERROR;
11571           END IF;
11572       IF l_index is not NULL THEN
11573           --update Global Picture directly
11574            OE_ORDER_UTIL.g_line_tbl(l_index).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
11575            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_ship_date := p_x_line_tbl(I).schedule_ship_date;
11576            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_arrival_date := p_x_line_tbl(I).schedule_arrival_date;
11577            OE_ORDER_UTIL.g_line_tbl(l_index).delivery_lead_time    := p_x_line_tbl(I).delivery_lead_time;
11578            OE_ORDER_UTIL.g_line_tbl(l_index).mfg_lead_time         := p_x_line_tbl(I).mfg_lead_time;
11579            OE_ORDER_UTIL.g_line_tbl(l_index).shipping_method_code   := p_x_line_tbl(I).shipping_method_code;
11580            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_status_code  := p_x_line_tbl(I).schedule_status_code;
11581            OE_ORDER_UTIL.g_line_tbl(l_index).visible_demand_flag    := p_x_line_tbl(I).visible_demand_flag;
11582            OE_ORDER_UTIL.g_line_tbl(l_index).latest_acceptable_date := p_x_line_tbl(I).latest_acceptable_date;
11583            OE_ORDER_UTIL.g_line_tbl(l_index).last_update_date       := p_x_line_tbl(I).last_update_date;
11584            OE_ORDER_UTIL.g_line_tbl(l_index).last_updated_by        := p_x_line_tbl(I).last_updated_by;
11585            OE_ORDER_UTIL.g_line_tbl(l_index).last_update_login      := p_x_line_tbl(I).last_update_login;
11586            OE_ORDER_UTIL.g_line_tbl(l_index).lock_control           := p_x_line_tbl(I).lock_control;
11587            IF l_debug_level  > 0 THEN
11588                oe_debug_pub.add(  'GLOBAL SHIP_FROM_ORG_ID IS: ' || OE_ORDER_UTIL.G_LINE_TBL ( L_INDEX ) .SHIP_FROM_ORG_ID , 1 ) ;
11589            END IF;
11590         END IF; /*l_index is not null check*/
11591       END IF;  /*code_release_level*/
11592 
11593          UPDATE OE_ORDER_LINES
11594          SET
11595           ship_from_org_id           = p_x_line_tbl(I).ship_from_org_id
11596          ,schedule_ship_date         = p_x_line_tbl(I).schedule_ship_date
11597          ,schedule_arrival_date      = p_x_line_tbl(I).schedule_arrival_date
11598          ,delivery_lead_time         = p_x_line_tbl(I).delivery_lead_time
11599          ,mfg_lead_time              = p_x_line_tbl(I).mfg_lead_time
11600          ,shipping_method_code       = p_x_line_tbl(I).shipping_method_code
11601          ,schedule_status_code       = p_x_line_tbl(I).schedule_status_code
11602          ,visible_demand_flag        = p_x_line_tbl(I).visible_demand_flag
11603          ,latest_acceptable_date     = p_x_line_tbl(I).latest_acceptable_date
11604          ,Original_Inventory_Item_Id
11605                               = p_x_line_tbl(I).Original_Inventory_Item_Id
11606          ,Original_item_identifier_Type
11607                               = p_x_line_tbl(I).Original_item_identifier_Type
11608          ,Original_ordered_item_id   = p_x_line_tbl(I).Original_ordered_item_id
11609          ,Original_ordered_item      = p_x_line_tbl(I).Original_ordered_item
11610          ,last_update_date           = p_x_line_tbl(I).last_update_date
11611          ,last_updated_by            = p_x_line_tbl(I).last_updated_by
11612          ,last_update_login          = p_x_line_tbl(I).last_update_login
11613          ,lock_control               = p_x_line_tbl(I).lock_control
11614          WHERE LINE_ID = p_x_line_tbl(I).line_id;
11615 
11616          IF l_debug_level  > 0 THEN
11617              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE' , 1 ) ;
11618          END IF;
11619       END IF;
11620 
11621      END;
11622      I := p_x_line_tbl.NEXT(I);
11623     END LOOP;
11624 
11625    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
11626     IF p_caller  = SCH_INTERNAL THEN
11627         l_process_requests := FALSE;
11628     ELSE
11629         l_process_requests := TRUE;
11630     END IF;
11631 
11632     OE_Order_PVT.Process_Requests_And_Notify
11633     ( p_process_requests        => l_process_requests
11634     , p_notify                  => FALSE
11635     , p_line_tbl                => p_x_line_tbl
11636     , p_old_line_tbl            => p_x_old_line_tbl
11637     , x_return_status           => x_return_status
11638     );
11639 
11640     IF l_debug_level  > 0 THEN
11641         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST' , 1 ) ;
11642     END IF;
11643     IF l_debug_level  > 0 THEN
11644         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
11645     END IF;
11646 
11647     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11648         IF l_debug_level  > 0 THEN
11649             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
11650         END IF;
11651         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11652     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11653         IF l_debug_level  > 0 THEN
11654             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
11655         END IF;
11656         RAISE FND_API.G_EXC_ERROR;
11657     END IF;
11658 
11659  ELSE  /*pre pack H*/
11660      -- Do not process delayed requests if this was a recursive
11661      -- call (e.g. from oe_line_util.pre_write_process)
11662 
11663     IF p_caller  = SCH_INTERNAL THEN
11664         l_process_requests := FALSE;
11665     ELSE
11666         l_process_requests := TRUE;
11667     END IF;
11668 
11669     OE_Order_PVT.Process_Requests_And_Notify
11670     ( p_process_requests        => l_process_requests
11671     , p_notify                  => TRUE
11672     , p_line_tbl                => p_x_line_tbl
11673     , p_old_line_tbl            => p_x_old_line_tbl
11674     , x_return_status           => x_return_status
11675     );
11676 
11677     IF l_debug_level  > 0 THEN
11678         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST' , 1 ) ;
11679     END IF;
11680     IF l_debug_level  > 0 THEN
11681         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
11682     END IF;
11683 
11684     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11685         IF l_debug_level  > 0 THEN
11686             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
11687         END IF;
11688         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11689     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11690         IF l_debug_level  > 0 THEN
11691             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
11692         END IF;
11693         RAISE FND_API.G_EXC_ERROR;
11694     END IF;
11695  END IF; /*code_release_level*/
11696 
11697 
11698     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11699     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11700     IF l_debug_level  > 0 THEN
11701         oe_debug_pub.add(  'PROCESS GROUP ' || OESCH_PERFORM_SCHEDULING , 1 ) ;
11702     END IF;
11703     IF l_debug_level  > 0 THEN
11704         oe_debug_pub.add(  'EXITING PROCESS GROUP OF LINES' , 1 ) ;
11705     END IF;
11706 EXCEPTION
11707 
11708    WHEN FND_API.G_EXC_ERROR THEN
11709 
11710         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11711         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11712         x_return_status := FND_API.G_RET_STS_ERROR;
11713         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
11714 
11715     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
11716 
11717         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11718         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11719         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11720         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
11721 
11722     WHEN OTHERS THEN
11723 
11724         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11725         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11726         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11727         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
11728 
11729         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
11730         THEN
11731             OE_MSG_PUB.Add_Exc_Msg
11732             (   G_PKG_NAME
11733             ,   'Call_Process_Order'
11734             );
11735         END IF;
11736 END Process_Group_of_Lines;
11737 
11738 
11739 /*---------------------------------------------------------------------
11740 Procedure Name : Process_Line
11741 Description    : This process is called after scheduling is performed
11742                  on the line and the result needs to be verified and/or
11743                  updated to the database.
11744 
11745 New code :  If the warehouse is changed on the line due to scheduling
11746             we will follow older approch. If warehouse is not changed ont
11747             the line then call security based on the operation. Call new API
11748             for clear-dep and defaulting. Based on  the output from the new api
11749             either call process order or do a direct update.
11750             p_call_prn is call process_request and notify.
11751 --------------------------------------------------------------------- */
11752 Procedure Process_Line
11753 ( p_old_line_rec        IN  OE_ORDER_PUB.line_rec_type
11754 , p_x_line_rec          IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type
11755 , p_caller              IN  VARCHAR2
11756 , p_call_prn            IN  BOOLEAN := TRUE
11757 , x_return_status OUT NOCOPY VARCHAR2)
11758 
11759 IS
11760 l_process_requests      BOOLEAN;
11761 l_line_tbl              OE_ORDER_PUB.line_tbl_type;
11762 l_old_line_tbl          OE_ORDER_PUB.line_tbl_type;
11763 l_sec_result            NUMBER;
11764 l_order_type_id         NUMBER := OE_Order_Cache.g_header_rec.order_type_id;
11765 l_control_rec           OE_GLOBALS.control_rec_type;
11766 l_src_attr_tbl          OE_GLOBALS.NUMBER_Tbl_Type;
11767 l_index                 NUMBER;
11768 --
11769 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
11770 --
11771 BEGIN
11772 
11773     x_return_status :=  FND_API.G_RET_STS_SUCCESS;
11774     IF l_debug_level  > 0 THEN
11775         oe_debug_pub.add(  'ENTERING PROCESS_LINE' , 1 ) ;
11776     END IF;
11777     IF l_debug_level  > 0 THEN
11778         oe_debug_pub.add(  'P_CALLER ' || P_CALLER , 1 ) ;
11779     END IF;
11780 
11781     /* Start Audit Trail */
11782     --Added is_mass_change condition for bug 4911340
11783     IF NOT OE_GLOBALS.G_UI_FLAG AND OE_MASS_CHANGE_PVT.IS_MASS_CHANGE = 'F' THEN
11784       p_x_line_rec.change_reason := 'SYSTEM';
11785     END IF;
11786 
11787    -- p_x_line_rec.change_comments := 'Scheduling Action';
11788     /* End Audit Trail */
11789 
11790    -- When warehouse is changed on the line then call process_order.
11791 
11792    IF nvl(p_x_line_rec.open_flag,'Y') = 'N' THEN
11793 
11794                                    IF l_debug_level  > 0 THEN
11795                                        oe_debug_pub.add(  'DIRECT UPDATE FOR A CANCELLED LINE' || P_X_LINE_REC.LINE_ID , 1 ) ;
11796                                    END IF;
11797 
11798          p_x_line_rec.last_update_date := SYSDATE;
11799          p_x_line_rec.last_updated_by := FND_GLOBAL.USER_ID;
11800          p_x_line_rec.last_update_login := FND_GLOBAL.LOGIN_ID;
11801          p_x_line_rec.lock_control := p_x_line_rec.lock_control + 1;
11802 
11803          UPDATE OE_ORDER_LINES
11804          SET
11805           ship_from_org_id           = p_x_line_rec.ship_from_org_id
11806          ,schedule_ship_date         = p_x_line_rec.schedule_ship_date
11807          ,schedule_arrival_date      = p_x_line_rec.schedule_arrival_date
11808          ,delivery_lead_time         = p_x_line_rec.delivery_lead_time
11809          ,mfg_lead_time              = p_x_line_rec.mfg_lead_time
11810          ,shipping_method_code       = p_x_line_rec.shipping_method_code
11811          ,schedule_status_code       = p_x_line_rec.schedule_status_code
11812          ,visible_demand_flag        = p_x_line_rec.visible_demand_flag
11813          ,Original_Inventory_Item_Id = p_x_line_rec.Original_Inventory_Item_Id
11814          ,Original_item_identifier_Type
11815                                  = p_x_line_rec.Original_item_identifier_Type
11816          ,Original_ordered_item_id   = p_x_line_rec.Original_ordered_item_id
11817          ,Original_ordered_item      = p_x_line_rec.Original_ordered_item
11818          ,latest_acceptable_date     = p_x_line_rec.latest_acceptable_date
11819          ,override_atp_date_code     = p_x_line_rec.override_atp_date_code
11820          ,firm_demand_flag           = p_x_line_rec.firm_demand_flag
11821          ,earliest_ship_date         = p_x_line_rec.earliest_ship_date
11822          ,last_update_date           = p_x_line_rec.last_update_date
11823          ,last_updated_by            = p_x_line_rec.last_updated_by
11824          ,last_update_login          = p_x_line_rec.last_update_login
11825          ,lock_control               = p_x_line_rec.lock_control
11826          WHERE LINE_ID = p_x_line_rec.line_id;
11827 
11828          IF l_debug_level  > 0 THEN
11829              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE ON A CANCELLED LINE' , 1 ) ;
11830          END IF;
11831 
11832    ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id,
11833                            p_old_line_rec.ship_from_org_id)
11834    OR    NOT OE_GLOBALS.Equal(p_x_line_rec.ship_to_org_id,
11835                            p_old_line_rec.ship_to_org_id)
11836    OR    NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
11837                            p_old_line_rec.inventory_item_id)
11838    THEN
11839 
11840     IF l_debug_level  > 0 THEN
11841         oe_debug_pub.add(  'WAREHOUSE/SHIP TO HAS CHANGED ON THE LINE CALLING PO' , 1 ) ;
11842     END IF;
11843 
11844     l_control_rec.controlled_operation := TRUE;
11845     l_control_rec.change_attributes    := TRUE;
11846 
11847     l_control_rec.clear_dependents     := TRUE;
11848     l_control_rec.default_attributes   := TRUE;
11849     l_control_rec.check_security       := TRUE;
11850 
11851     l_control_rec.write_to_DB          := TRUE;
11852     l_control_rec.validate_entity      := TRUE;
11853     l_control_rec.process              := FALSE;
11854 
11855     --  Instruct API to retain its caches
11856 
11857     l_control_rec.clear_api_cache      := FALSE;
11858     l_control_rec.clear_api_requests   := FALSE;
11859 
11860     l_old_line_tbl(1) := p_old_line_rec;
11861     l_line_tbl(1)     := p_x_line_rec;
11862 
11863     -- We are doing this since we are calling the po from post write.
11864 
11865     l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
11866 
11867     Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
11868                        p_x_line_tbl      => l_line_tbl,
11869                        p_control_rec     => l_control_rec,
11870                        p_caller          => p_caller,
11871                        x_return_status   => x_return_status);
11872 
11873     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11874        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11875     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11876        RAISE FND_API.G_EXC_ERROR;
11877     END IF;
11878 
11879    ELSE -- warehouse is not changed on the line.
11880 
11881       IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE THEN
11882 
11883         IF l_debug_level  > 0 THEN
11884             oe_debug_pub.add(  'IT IS UPDATE , CALL SECURITY' ) ;
11885         END IF;
11886 
11887         IF p_caller = 'INTERNAL' THEN      -- 5999034
11888            p_x_line_rec.change_reason := 'SYSTEM';
11889         END IF;
11890 
11891 
11892         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
11893 
11894         OE_Line_Security.Attributes
11895                 ( p_line_rec        => p_x_line_rec
11896                 , p_old_line_rec    => p_old_line_rec
11897                 , x_result          => l_sec_result
11898                 , x_return_status   => x_return_status
11899                 );
11900 
11901         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11902            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11903         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11904            RAISE FND_API.G_EXC_ERROR;
11905         END IF;
11906 
11907         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
11908         IF l_debug_level  > 0 THEN
11909             oe_debug_pub.add(  'AFTER SECURITY CALL' || X_RETURN_STATUS , 1 ) ;
11910         END IF;
11911 
11912          -- if operation on any attribute is constrained
11913         IF l_sec_result = OE_PC_GLOBALS.YES THEN
11914            IF l_debug_level  > 0 THEN
11915                oe_debug_pub.add(  'CONSTRAINT FOUND' , 4 ) ;
11916            END IF;
11917            RAISE FND_API.G_EXC_ERROR;
11918         END IF;
11919 
11920       END IF; -- operation update.
11921 
11922       --Value of the G_ATTR_UPDATED_BY_DEF will be set in defaulting
11923       -- We will re-set the value, before calling.
11924       OE_GLOBALS.G_ATTR_UPDATED_BY_DEF := 'N';
11925 
11926       IF l_debug_level  > 0 THEN
11927           oe_debug_pub.add(  'OLD SHIP :' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
11928       END IF;
11929       IF l_debug_level  > 0 THEN
11930           oe_debug_pub.add(  'NEW SHIP :' || P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
11931       END IF;
11932 
11933      IF NOT OE_GLOBALS.Equal(trunc(p_x_line_rec.schedule_ship_date),     -- 5999034
11934                               trunc(p_old_line_rec.schedule_ship_date))
11935 
11936       THEN
11937 
11938        IF l_debug_level  > 0 THEN
11939            oe_debug_pub.add(  'SHIP_DATE HAS CHANGED ON THE LINE' , 1 ) ;
11940        END IF;
11941        l_src_attr_tbl(1) := OE_LINE_UTIL.G_SCHEDULE_SHIP_DATE;
11942 
11943        OE_Line_Util_Ext.Clear_Dep_And_Default
11944           ( p_src_attr_tbl    => l_src_attr_tbl,
11945             p_x_line_rec      => p_x_line_rec,
11946             p_old_line_rec    => p_old_line_rec);
11947        IF l_debug_level  > 0 THEN
11948            oe_debug_pub.add(  'AFTER CALLING CLEAR_DEP_AND_DEFAULT' , 1 ) ;
11949        END IF;
11950 
11951       END IF;
11952       IF l_debug_level  > 0 THEN
11953           oe_debug_pub.add(  'DIRECT/PO ' || OE_GLOBALS.G_ATTR_UPDATED_BY_DEF , 1 ) ;
11954       END IF;
11955       IF OE_GLOBALS.G_ATTR_UPDATED_BY_DEF = 'Y' THEN
11956 
11957 
11958          l_control_rec.controlled_operation := TRUE;
11959          l_control_rec.change_attributes    := TRUE;
11960 
11961          l_control_rec.clear_dependents     := FALSE;
11962          l_control_rec.default_attributes   := FALSE;
11963          l_control_rec.check_security       := FALSE;
11964 
11965          l_control_rec.write_to_DB          := TRUE;
11966          l_control_rec.validate_entity      := TRUE;
11967          l_control_rec.process              := FALSE;
11968 
11969          --  Instruct API to retain its caches
11970 
11971          l_control_rec.clear_api_cache      := FALSE;
11972          l_control_rec.clear_api_requests   := FALSE;
11973 
11974          l_old_line_tbl(1) := p_old_line_rec;
11975          l_line_tbl(1)     := p_x_line_rec;
11976 
11977          -- We are doing this since we are calling the po from post write.
11978 
11979          l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
11980 
11981          Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
11982                             p_x_line_tbl      => l_line_tbl,
11983                             p_control_rec     => l_control_rec,
11984                             p_caller          => p_caller,
11985                             x_return_status   => x_return_status);
11986 
11987          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
11988             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11989          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
11990             RAISE FND_API.G_EXC_ERROR;
11991          END IF;
11992 
11993       ELSE
11994          IF l_debug_level  > 0 THEN
11995              oe_debug_pub.add(  ' DEFAULTING HAS NOT CHANGED ANY THING' , 1 ) ;
11996          END IF;
11997          IF l_debug_level  > 0 THEN
11998              oe_debug_pub.add(  'BEFORE CALLING LOG_SCHEDULING REQUESTS' , 1 ) ;
11999          END IF;
12000 
12001          OE_MSG_PUB.set_msg_context
12002          (p_entity_code                => 'LINE'
12003          ,p_entity_id                  => p_x_line_rec.line_id
12004          ,p_header_id                  => p_x_line_rec.header_id
12005          ,p_line_id                    => p_x_line_rec.line_id
12006          ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
12007          ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
12008          ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
12009          ,p_change_sequence            => p_x_line_rec.change_sequence
12010          ,p_source_document_id         => p_x_line_rec.source_document_id
12011          ,p_source_document_line_id    => p_x_line_rec.source_document_line_id
12012          ,p_order_source_id            => p_x_line_rec.order_source_id
12013          ,p_source_document_type_id    => p_x_line_rec.source_document_type_id);
12014 
12015          OE_LINE_UTIL.Log_Scheduling_Requests
12016          (p_x_line_rec    => p_x_line_rec
12017          ,p_old_line_rec  => p_old_line_rec
12018          ,p_caller        => p_caller
12019          ,p_order_type_id => l_order_type_id
12020          ,x_return_status => x_return_status);
12021 
12022          IF l_debug_level  > 0 THEN
12023              oe_debug_pub.add(  'AFTER CALLING LOG_SCHEDULING REQUESTS' || X_RETURN_STATUS , 1 ) ;
12024          END IF;
12025 
12026          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12027             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12028          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12029             RAISE FND_API.G_EXC_ERROR;
12030          END IF;
12031 
12032          IF l_debug_level  > 0 THEN
12033              oe_debug_pub.add(  'BEFORE DOING DIRECT UPDATE' , 1 ) ;
12034          END IF;
12035          IF NOT validate_ship_method (p_x_line_rec.shipping_method_code,
12036                                   p_old_line_rec.shipping_method_code,
12037                                   p_x_line_rec.ship_from_org_id) THEN
12038 
12039            p_x_line_rec.shipping_method_code := Null;
12040            p_x_line_rec.freight_carrier_code := Null;
12041          END IF;
12042 
12043          -- Start 2806483
12044          IF (p_x_line_rec.shipping_method_code IS NOT NULL
12045          AND p_x_line_rec.shipping_method_code <> FND_API.G_MISS_CHAR)
12046          AND NOT OE_GLOBALS.EQUAL(p_x_line_rec.shipping_method_code
12047                                  ,p_old_line_rec.shipping_method_code)
12048          THEN
12049 
12050             p_x_line_rec.freight_carrier_code :=
12051                     OE_Default_Line.Get_Freight_Carrier(p_line_rec => p_x_line_rec,
12052                                                         p_old_line_rec => p_old_line_rec);
12053          END IF;
12054          -- End 2806483
12055 
12056          p_x_line_rec.last_update_date := SYSDATE;
12057          p_x_line_rec.last_updated_by := FND_GLOBAL.USER_ID;
12058          p_x_line_rec.last_update_login := FND_GLOBAL.LOGIN_ID;
12059          p_x_line_rec.lock_control := p_x_line_rec.lock_control + 1;
12060 
12061          -- Pack J: Promise Date is added to the update to reflect any change to promise date
12062          UPDATE OE_ORDER_LINES
12063          SET
12064           ship_from_org_id           = p_x_line_rec.ship_from_org_id
12065          ,schedule_ship_date         = p_x_line_rec.schedule_ship_date
12066          ,schedule_arrival_date      = p_x_line_rec.schedule_arrival_date
12067          ,delivery_lead_time         = p_x_line_rec.delivery_lead_time
12068          ,mfg_lead_time              = p_x_line_rec.mfg_lead_time
12069          ,shipping_method_code       = p_x_line_rec.shipping_method_code
12070          ,schedule_status_code       = p_x_line_rec.schedule_status_code
12071          ,visible_demand_flag        = p_x_line_rec.visible_demand_flag
12072          ,Original_Inventory_Item_Id = p_x_line_rec.Original_Inventory_Item_Id
12073          ,Original_item_identifier_Type
12074                                   = p_x_line_rec.Original_item_identifier_Type
12075          ,Original_ordered_item_id   = p_x_line_rec.Original_ordered_item_id
12076          ,Original_ordered_item      = p_x_line_rec.Original_ordered_item
12077          ,latest_acceptable_date     = p_x_line_rec.latest_acceptable_date
12078          ,override_atp_date_code     = p_x_line_rec.override_atp_date_code
12079          ,freight_carrier_code       = p_x_line_rec.freight_carrier_code
12080          ,firm_demand_flag           = p_x_line_rec.firm_demand_flag
12081          ,earliest_ship_date         = p_x_line_rec.earliest_ship_date
12082          ,promise_date               = p_x_line_rec.promise_date
12083          ,last_update_date           = p_x_line_rec.last_update_date
12084          ,last_updated_by            = p_x_line_rec.last_updated_by
12085          ,last_update_login          = p_x_line_rec.last_update_login
12086          ,lock_control               = p_x_line_rec.lock_control
12087          WHERE LINE_ID = p_x_line_rec.line_id;  --2806483 Added Fright_carrier_code
12088 
12089          IF l_debug_level  > 0 THEN
12090              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE' , 1 ) ;
12091          END IF;
12092 
12093       END IF; -- OE_GLOBALS.G_ATTR_UPDATED_BY_DEF.
12094    END IF; -- warehouse is changed.
12095 
12096    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12097       IF l_debug_level  > 0 THEN
12098           oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
12099       END IF;
12100       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12101    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12102       IF l_debug_level  > 0 THEN
12103           oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
12104       END IF;
12105       RAISE FND_API.G_EXC_ERROR;
12106    END IF;
12107 
12108    IF l_line_tbl.count = 0 THEN
12109 
12110       l_old_line_tbl(1) := p_old_line_rec;
12111       l_line_tbl(1)     := p_x_line_rec;
12112 
12113        -- We are doing this since we are calling the po from post write.
12114 
12115       l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
12116 
12117 
12118    END IF;
12119 
12120    -- added for notification framework
12121    --check code release level first. Notification framework is at Pack H level
12122 
12123    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
12124 
12125         -- calling notification framework to get index position
12126         OE_ORDER_UTIL.Update_Global_Picture(p_Upd_New_Rec_If_Exists =>False,
12127                     p_old_line_rec => p_old_line_rec,
12128                     p_line_rec =>p_x_line_rec,
12129                     p_line_id => p_x_line_rec.line_id,
12130                     x_index => l_index,
12131                     x_return_status => x_return_status);
12132                            IF l_debug_level  > 0 THEN
12133                                oe_debug_pub.add(  'UPDATE_GLOBAL RETURN STATUS FROM OE_SCHEDULE_UTIL.PROCESS_LINE IS: ' || X_RETURN_STATUS ) ;
12134                            END IF;
12135         IF l_debug_level  > 0 THEN
12136             oe_debug_pub.add(  'GLOBAL PICTURE INDEX IS: ' || L_INDEX , 1 ) ;
12137         END IF;
12138 
12139         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12140            IF l_debug_level  > 0 THEN
12141                oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
12142            END IF;
12143            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12144         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12145            IF l_debug_level  > 0 THEN
12146                oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
12147            END IF;
12148            RAISE FND_API.G_EXC_ERROR;
12149         END IF;
12150 
12151         IF l_index is not NULL THEN
12152           --update Global Picture directly
12153           OE_ORDER_UTIL.g_line_tbl(l_index).ship_from_org_id := p_x_line_rec.ship_from_org_id;
12154           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_ship_date := p_x_line_rec.schedule_ship_date;
12155           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_arrival_date := p_x_line_rec.schedule_arrival_date;
12156           OE_ORDER_UTIL.g_line_tbl(l_index).delivery_lead_time    := p_x_line_rec.delivery_lead_time;
12157           OE_ORDER_UTIL.g_line_tbl(l_index).mfg_lead_time         := p_x_line_rec.mfg_lead_time;
12158           OE_ORDER_UTIL.g_line_tbl(l_index).shipping_method_code   := p_x_line_rec.shipping_method_code;
12159           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_status_code  := p_x_line_rec.schedule_status_code;
12160           OE_ORDER_UTIL.g_line_tbl(l_index).visible_demand_flag    := p_x_line_rec.visible_demand_flag;
12161           OE_ORDER_UTIL.g_line_tbl(l_index).latest_acceptable_date := p_x_line_rec.latest_acceptable_date;
12162           OE_ORDER_UTIL.g_line_tbl(l_index).last_update_date       := p_x_line_rec.last_update_date;
12163           OE_ORDER_UTIL.g_line_tbl(l_index).last_updated_by        := p_x_line_rec.last_updated_by;
12164           OE_ORDER_UTIL.g_line_tbl(l_index).last_update_login      := p_x_line_rec.last_update_login;
12165           OE_ORDER_UTIL.g_line_tbl(l_index).lock_control           := p_x_line_rec.lock_control;
12166           IF l_debug_level  > 0 THEN
12167               oe_debug_pub.add(  'GLOBAL SHIP_FROM_ORG_ID IS: ' || OE_ORDER_UTIL.G_LINE_TBL ( L_INDEX ) .SHIP_FROM_ORG_ID , 1 ) ;
12168           END IF;
12169         END IF; /*l_index is not null check*/
12170 
12171     -- Do not execute delayed request if it is a internal call.
12172     -- call (e.g. from oe_line_util.post_write_process)
12173 
12174     IF p_call_prn THEN
12175 
12176       IF p_caller = SCH_INTERNAL THEN
12177           l_process_requests := FALSE;
12178       ELSE
12179           l_process_requests := TRUE;
12180       END IF;
12181 
12182       OE_Order_PVT.Process_Requests_And_Notify
12183       ( p_process_requests        => l_process_requests
12184       , p_notify                  => FALSE
12185       , p_line_tbl                => l_line_tbl
12186       , p_old_line_tbl            => l_old_line_tbl
12187       , x_return_status           => x_return_status);
12188 
12189 
12190       IF l_debug_level  > 0 THEN
12191           oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST AND NOTIFY' , 1 ) ;
12192       END IF;
12193       IF l_debug_level  > 0 THEN
12194           oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
12195       END IF;
12196 
12197       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12198           IF l_debug_level  > 0 THEN
12199               oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
12200           END IF;
12201           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12202       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12203           IF l_debug_level  > 0 THEN
12204               oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
12205           END IF;
12206           RAISE FND_API.G_EXC_ERROR;
12207       END IF;
12208      END IF;
12209   ELSE /*pre pack H*/
12210 
12211     -- Do not execute delayed request if it is a internal call.
12212     -- call (e.g. from oe_line_util.post_write_process)
12213 
12214     IF p_call_prn THEN
12215 
12216       IF p_caller = SCH_INTERNAL THEN
12217           l_process_requests := FALSE;
12218       ELSE
12219           l_process_requests := TRUE;
12220       END IF;
12221 
12222       OE_Order_PVT.Process_Requests_And_Notify
12223       ( p_process_requests        => l_process_requests
12224       , p_notify                  => TRUE
12225       , p_line_tbl                => l_line_tbl
12226       , p_old_line_tbl            => l_old_line_tbl
12227       , x_return_status           => x_return_status);
12228 
12229 
12230       IF l_debug_level  > 0 THEN
12231           oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST AND NOTIFY' , 1 ) ;
12232       END IF;
12233       IF l_debug_level  > 0 THEN
12234           oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
12235       END IF;
12236 
12237       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12238           IF l_debug_level  > 0 THEN
12239               oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
12240           END IF;
12241           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12242       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12243           IF l_debug_level  > 0 THEN
12244               oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
12245           END IF;
12246           RAISE FND_API.G_EXC_ERROR;
12247       END IF;
12248 
12249     END IF;
12250  END IF; /*code_release_level*/
12251     IF l_debug_level  > 0 THEN
12252         oe_debug_pub.add(  'EXITING PROCESS_LINE ' , 1 ) ;
12253     END IF;
12254 
12255 EXCEPTION
12256 
12257    WHEN FND_API.G_EXC_ERROR THEN
12258 
12259         x_return_status := FND_API.G_RET_STS_ERROR;
12260         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12261         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12262 
12263     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12264 
12265         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12266         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12267         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12268 
12269     WHEN OTHERS THEN
12270 
12271         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12272         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12273         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
12274 
12275         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12276         THEN
12277             OE_MSG_PUB.Add_Exc_Msg
12278             (   G_PKG_NAME
12279             ,   'Process_Line'
12280             );
12281         END IF;
12282 END Process_Line;
12283 /*--------------------------------------------------------
12284 PROCEDURE ATP_Check
12285 
12286 --------------------------------------------------------*/
12287 Procedure ATP_Check
12288          ( p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type,
12289            p_x_line_rec         IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
12290            p_validate           IN  VARCHAR2 := FND_API.G_TRUE,
12291 x_atp_tbl OUT NOCOPY OE_ATP.atp_tbl_type,
12292 
12293 x_return_status OUT NOCOPY VARCHAR2
12294 
12295            )
12296 IS
12297 
12298   -- BUG 1955004
12299   l_scheduling_level_code VARCHAR2(30);
12300   -- END 1955004
12301 
12302 --
12303 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12304 --
12305 BEGIN
12306 
12307      IF l_debug_level  > 0 THEN
12308          oe_debug_pub.add(  'ENTERING ATP_CHECK ' , 2 ) ;
12309      END IF;
12310   -- Since we are not calling need scheduling from here
12311 
12312    g_atp_tbl.Delete;
12313 
12314    IF p_validate = FND_API.G_TRUE THEN
12315 
12316       IF l_debug_level  > 0 THEN
12317           oe_debug_pub.add(  'BEFORE CALLING VALIDATE LINE' , 2 ) ;
12318       END IF;
12319       Validate_Line(p_line_rec      => p_x_line_rec,
12320                     p_old_line_rec  => p_old_line_rec,
12321                     p_sch_action    => p_x_line_rec.schedule_action_code,
12322                     x_return_status => x_return_status);
12323 
12324    END IF;
12325 
12326   -- BUG 1955004
12327   l_scheduling_level_code := Get_Scheduling_Level(p_x_line_rec.header_id,
12328                                                    p_x_line_rec.line_type_id);
12329 
12330   IF l_scheduling_level_code IS NULL THEN
12331 
12332      l_scheduling_level_code := SCH_LEVEL_THREE;
12333   END IF;
12334 
12335   IF l_debug_level  > 0 THEN
12336       oe_debug_pub.add(  'SCHEDULING_LEVEL_CODE IS:' || L_SCHEDULING_LEVEL_CODE , 2 ) ;
12337   END IF;
12338   -- 3763015
12339   IF l_scheduling_level_code <> SCH_LEVEL_FOUR AND
12340      l_scheduling_level_code <> SCH_LEVEL_FIVE AND
12341      NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'N' THEN
12342     -- FOUR and FIVE CANNOT have ATP Performed
12343     -- validation may be off, so we we need to double-check here
12344    --END 1955004
12345 
12346    IF l_debug_level  > 0 THEN
12347        oe_debug_pub.add(  'BEFORE CALLING ACTION_ATP ' , 2 ) ;
12348    END IF;
12349    Action_ATP(p_x_line_rec    => p_x_line_rec,
12350               p_old_line_rec  => p_old_line_rec,
12351               x_return_status => x_return_status);
12352 
12353 
12354 
12355    IF l_debug_level  > 0 THEN
12356        oe_debug_pub.add(  'AFTER CALLING ACTION_ATP ' || X_RETURN_STATUS , 2 ) ;
12357    END IF;
12358    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12359       IF l_debug_level  > 0 THEN
12360           oe_debug_pub.add(  'UNEXPECTED ERROR FROM ' , 1 ) ;
12361       END IF;
12362       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12363    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12364       IF l_debug_level  > 0 THEN
12365           oe_debug_pub.add(  'RR: L2' , 1 ) ;
12366       END IF;
12367       RAISE FND_API.G_EXC_ERROR;
12368    END IF;
12369 
12370     x_atp_tbl := g_atp_tbl;
12371 
12372    END IF;  -- for new IF check added for 1955004
12373 
12374     IF l_debug_level  > 0 THEN
12375         oe_debug_pub.add(  'EXITING ATP_CHECK ' , 2 ) ;
12376     END IF;
12377 EXCEPTION
12378 
12379    WHEN FND_API.G_EXC_ERROR THEN
12380 
12381       x_return_status := FND_API.G_RET_STS_ERROR;
12382       RAISE FND_API.G_EXC_ERROR;
12383 
12384     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12385 
12386       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12387       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12388 
12389     WHEN OTHERS THEN
12390 
12391       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12392 
12393       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12394       THEN
12395           OE_MSG_PUB.Add_Exc_Msg
12396           (   G_PKG_NAME
12397           ,   'ATP_Check'
12398           );
12399       END IF;
12400       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12401 
12402 END ATP_Check;
12403 
12404 /*--------------------------------------------------------
12405 PROCEDURE Multi_ATP_Check
12406 
12407 --------------------------------------------------------*/
12408 PROCEDURE Multi_ATP_Check
12409          ( p_old_line_tbl       IN  OE_ORDER_PUB.line_tbl_type,
12410            p_x_line_tbl         IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type,
12411 x_atp_tbl OUT NOCOPY OE_ATP.atp_tbl_type,
12412 
12413 x_return_status OUT NOCOPY VARCHAR2
12414 
12415            )
12416 IS
12417 l_msg_count               NUMBER;
12418 l_mrp_msg_data            VARCHAR2(2000);
12419 l_session_id              NUMBER := 0;
12420 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
12421 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
12422 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
12423 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
12424 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
12425 l_on_hand_qty             NUMBER;
12426 l_avail_to_reserve        NUMBER;
12427 l_on_hand_qty2             NUMBER; -- INVCONV
12428 l_avail_to_reserve2        NUMBER; -- invconv
12429 l_process_flag            VARCHAR2(1) := FND_API.G_FALSE;
12430 --
12431 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12432 --
12433 BEGIN
12434 
12435    IF l_debug_level  > 0 THEN
12436        oe_debug_pub.add(  'ENTERING MULTI ATP_CHECK ' , 2 ) ;
12437    END IF;
12438 
12439    g_atp_tbl.Delete;
12440 
12441    IF l_debug_level  > 0 THEN
12442        oe_debug_pub.add(  'BEFORE CALLING LOAD_MRP_TBL ' , 2 ) ;
12443    END IF;
12444 
12445    Load_MRP_request_from_tbl
12446        ( p_line_tbl      => p_x_line_tbl
12447         ,p_old_line_tbl  => p_old_line_tbl
12448         ,p_sch_action    => OESCH_ACT_ATP_CHECK
12449         ,x_mrp_atp_rec   => l_mrp_atp_rec);
12450 
12451    IF l_debug_level  > 0 THEN
12452        oe_debug_pub.add(  'AFTER CALLING LOAD_MRP_TBL ' , 2 ) ;
12453    END IF;
12454 
12455    IF l_mrp_atp_rec.error_code.count > 0 THEN
12456 
12457 
12458      l_session_id := Get_Session_Id;
12459      G_ATP_CHECK_session_id := l_session_id;
12460 
12461      -- Call ATP
12462 
12463      IF l_debug_level  > 0 THEN
12464          oe_debug_pub.add(  'CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
12465      END IF;
12466 
12467      MRP_ATP_PUB.Call_ATP
12468         ( p_session_id             =>  l_session_id
12469         , p_atp_rec                =>  l_mrp_atp_rec
12470         , x_atp_rec                =>  l_out_mrp_atp_rec
12471         , x_atp_supply_demand      =>  l_atp_supply_demand
12472         , x_atp_period             =>  l_atp_period
12473         , x_atp_details            =>  l_atp_details
12474         , x_return_status          =>  x_return_status
12475         , x_msg_data               =>  l_mrp_msg_data
12476         , x_msg_count              =>  l_msg_count);
12477 
12478 
12479                                               IF l_debug_level  > 0 THEN
12480                                                   oe_debug_pub.add(  ' AFTER CALLING MRP_ATP_PUB.CALL_ATP ' || X_RETURN_STATUS , 1 ) ;
12481                                               END IF;
12482 
12483      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12484         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12485 /*     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12486             RAISE FND_API.G_EXC_ERROR; */
12487      END IF;
12488 
12489    END IF;  -- Moved the end of the if here becausw we want to call Load_Results
12490              -- with a count of 0 because of bug 1955004
12491 
12492      Load_Results_from_tbl(p_atp_rec        => l_out_mrp_atp_rec,
12493                            p_old_line_tbl   => p_old_line_tbl,  -- Added new parameter to support 1955004
12494                            p_x_line_tbl     => p_x_line_tbl,
12495                            x_return_status  => x_return_status);
12496 
12497      IF l_debug_level  > 0 THEN
12498          oe_debug_pub.add(  'AFTER CALLING ACTION_ATP ' || X_RETURN_STATUS , 2 ) ;
12499      END IF;
12500 
12501      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12502             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12503      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12504             RAISE FND_API.G_EXC_ERROR;
12505      END IF;
12506 
12507    -- END IF; -- MRP count.  Moved this end if per 1955004 above
12508 
12509      -- We also need to pass back on-hand qty and available_to_reserve
12510      -- qties while performing ATP. Getting these values from inventory.
12511 
12512      FOR K IN 1..g_atp_tbl.count LOOP
12513         IF l_debug_level  > 0 THEN
12514             oe_debug_pub.add(  'CALLING QUERY_QTY_TREE' , 1 ) ;
12515         END IF;
12516         Query_Qty_Tree(p_org_id           => g_atp_tbl(K).ship_from_org_id,
12517                        p_item_id          => g_atp_tbl(K).inventory_item_id,
12518                        p_line_id          => g_atp_tbl(K).line_id,
12519                        p_sch_date     => nvl(g_atp_tbl(K).group_available_date,
12520                                        g_atp_tbl(K).ordered_qty_Available_Date),
12521                        x_on_hand_qty      => l_on_hand_qty,
12522                        x_avail_to_reserve => l_avail_to_reserve,
12523                        x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
12524                        x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
12525                        );
12526 
12527 
12528         --  added by fabdi 03/May/2001
12529         IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id
12530                                               => g_atp_tbl(K).ship_from_org_id)
12531         THEN
12532            l_process_flag := FND_API.G_FALSE;
12533         ELSE
12534            l_process_flag := FND_API.G_TRUE;
12535         END IF;
12536 
12537         IF l_process_flag = FND_API.G_TRUE
12538         THEN
12539           g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
12540           g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
12541           g_atp_tbl(K).QTY_ON_REQUEST_DATE  := l_avail_to_reserve;
12542                                              -- Available field in ATP
12543 
12544           IF l_debug_level  > 0 THEN
12545               oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
12546           END IF;
12547           IF l_debug_level  > 0 THEN
12548               oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
12549           END IF;
12550          IF l_debug_level  > 0 THEN
12551              oe_debug_pub.add(  'AVAILABLE ' || L_AVAIL_TO_RESERVE ) ;
12552          END IF;
12553         ELSE
12554           IF g_atp_tbl(K).substitute_flag = 'N' THEN
12555              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
12556              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
12557              IF l_debug_level  > 0 THEN
12558                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
12559              END IF;
12560              IF l_debug_level  > 0 THEN
12561                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
12562              END IF;
12563           ELSE
12564              g_atp_tbl(K).sub_on_hand_qty          := l_on_hand_qty;
12565              g_atp_tbl(K).sub_available_to_reserve := l_avail_to_reserve;
12566              IF l_debug_level  > 0 THEN
12567                  oe_debug_pub.add(  'SUB L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
12568              END IF;
12569              IF l_debug_level  > 0 THEN
12570                  oe_debug_pub.add(  'SUB L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
12571              END IF;
12572 
12573              Query_Qty_Tree
12574                   (p_org_id           => g_atp_tbl(K).ship_from_org_id,
12575                    p_item_id          => g_atp_tbl(K).request_item_id,
12576                    p_line_id          => g_atp_tbl(K).line_id,
12577                    p_sch_date         => g_atp_tbl(K).req_item_available_date,
12578                    x_on_hand_qty      => l_on_hand_qty,
12579                    x_avail_to_reserve => l_avail_to_reserve,
12580                    x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
12581                    x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
12582                        );
12583 
12584              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
12585              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
12586              IF l_debug_level  > 0 THEN
12587                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
12588              END IF;
12589              IF l_debug_level  > 0 THEN
12590                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
12591              END IF;
12592           END IF; -- Substitution.
12593         END IF;
12594         -- end fabdi
12595 
12596      END LOOP;
12597 
12598 
12599     x_atp_tbl := g_atp_tbl;
12600 
12601     IF l_debug_level  > 0 THEN
12602         oe_debug_pub.add(  'EXITING MULTI ATP_CHECK ' , 2 ) ;
12603     END IF;
12604 EXCEPTION
12605 
12606    WHEN FND_API.G_EXC_ERROR THEN
12607 
12608         x_return_status := FND_API.G_RET_STS_ERROR;
12609         RAISE FND_API.G_EXC_ERROR;
12610 
12611     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12612 
12613         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12614         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12615 
12616     WHEN OTHERS THEN
12617 
12618         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12619 
12620         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12621         THEN
12622             OE_MSG_PUB.Add_Exc_Msg
12623             (   G_PKG_NAME
12624             ,   'MULTI_ATP_Check'
12625             );
12626         END IF;
12627 
12628         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12629 END Multi_ATP_Check;
12630 
12631 Procedure Delete_Row(p_line_id      IN NUMBER)
12632 IS
12633 l_line_rec               OE_ORDER_PUB.line_rec_type;
12634 l_return_status          VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
12635 l_sales_order_id         NUMBER;
12636 l_ato_exists             VARCHAR2(1);
12637 l_request_search_rslt    BOOLEAN;
12638 --
12639 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12640 --
12641 BEGIN
12642 
12643   IF l_debug_level  > 0 THEN
12644       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.DELETE_ROW' , 1 ) ;
12645   END IF;
12646   OE_Line_Util.Query_Row(p_line_id    => p_line_id,
12647                          x_line_rec   => l_line_rec);
12648 
12649   /* Fix for bug 2643593, reservations to be removed only for
12650      shippable line */
12651 
12652   IF nvl(l_line_rec.shippable_flag,'N') = 'Y' THEN
12653 
12654     l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id
12655                                               (l_line_rec.HEADER_ID);
12656 
12657 
12658      -- INVCONV - MERGED CALLS   FOR OE_LINE_UTIL.Get_Reserved_Quantity and OE_LINE_UTIL.Get_Reserved_Quantity2
12659 
12660      OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
12661                                               ,p_line_id   => l_line_rec.line_id
12662                                               ,p_org_id    => l_line_rec.ship_from_org_id
12663                                               ,x_reserved_quantity =>  l_line_rec.reserved_quantity
12664                                               ,x_reserved_quantity2 => l_line_rec.reserved_quantity2
12665                                                                                             );
12666 
12667     /*l_line_rec.reserved_quantity :=
12668               OE_LINE_UTIL.Get_Reserved_Quantity
12669                  (p_header_id   => l_sales_order_id,
12670                   p_line_id     => l_line_rec.line_id,
12671                   p_org_id      => l_line_rec.ship_from_org_id);
12672     l_line_rec.reserved_quantity2 :=           -- INVCONV
12673               OE_LINE_UTIL.Get_Reserved_Quantity2
12674                  (p_header_id   => l_sales_order_id,
12675                   p_line_id     => l_line_rec.line_id,
12676                   p_org_id      => l_line_rec.ship_from_org_id);   */
12677 
12678          IF l_debug_level  > 0 THEN
12679             oe_debug_pub.add(  'after Get_Reserved_Quantities, l_line_rec.reserved_quantity2 = ' ||  l_line_rec.reserved_quantity2, 1 ) ;
12680              END IF;
12681 
12682 
12683 
12684   END IF;
12685 
12686   IF l_line_rec.reserved_quantity is not null
12687   THEN
12688     -- Call INV to delete the old reservations
12689     -- shipping_interfaced_flag
12690 
12691        IF l_line_rec.reserved_quantity2 = 0 -- INVCONV
12692         THEN
12693           l_line_rec.reserved_quantity2 := NULL;
12694        END IF;
12695 
12696 
12697        Unreserve_Line
12698         (p_line_rec              => l_line_rec,
12699          p_quantity_to_unreserve => l_line_rec.reserved_quantity,
12700          p_quantity2_to_unreserve => l_line_rec.reserved_quantity2, -- INVCONV
12701          x_return_status         => l_return_status);
12702   END IF;
12703   -- 4026758
12704   IF l_line_rec.ship_set_id IS NOT NULL
12705      OR l_line_rec.arrival_set_id IS NOT NULL THEN
12706      -- Line is with a set.
12707 
12708      Log_Delete_Set_Request
12709        (p_header_id   => l_line_rec.header_id,
12710         p_line_id     => l_line_rec.line_id,
12711         p_set_id      => nvl(l_line_rec.ship_set_id,l_line_rec.arrival_set_id),
12712         x_return_status => l_return_status);
12713      IF l_debug_level  > 0 THEN
12714         oe_debug_pub.add(  'AFTER LOGGING DELETE SETS DELAYED REQUEST ' || L_RETURN_STATUS , 1 ) ;
12715      END IF;
12716 
12717   --bug#5631508
12718    IF L_RETURN_STATUS=FND_API.G_RET_STS_SUCCESS then
12719 
12720 	OE_AUDIT_HISTORY_PVT.DELETE_SET_HISTORY( p_line_id     => l_line_rec.line_id, x_return_status => l_return_status);
12721 
12722 	 IF l_debug_level  > 0 THEN
12723 		 oe_debug_pub.add(  'AFTER DELETE_SET_HISTORY ' || l_return_status , 1 ) ;
12724 	 END IF;
12725 
12726     END IF;
12727 
12728   END IF;
12729 
12730 
12731 
12732   IF l_line_rec.item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
12733 
12734 
12735    IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
12736    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10
12737    AND  l_line_rec.ato_line_id IS NOT NULL
12738    AND  l_line_rec.ato_line_id <> l_line_rec.line_id THEN
12739 
12740      BEGIN
12741 
12742      Select 'Y'
12743        Into l_ato_exists
12744      From oe_order_lines_all
12745      Where line_id = l_line_rec.ato_line_id;
12746      EXCEPTION
12747 
12748        WHEN OTHERS THEN
12749 
12750       l_ato_exists := 'N';
12751 
12752      END;
12753      IF l_ato_exists = 'Y' THEN
12754 
12755       l_request_search_rslt :=
12756           OE_Delayed_Requests_PVT.Check_For_Request
12757           (p_entity_code    => OE_GLOBALS.G_ENTITY_LINE,
12758            p_entity_id      => l_line_rec.ato_line_id,
12759            p_request_type   => OE_GLOBALS.G_SCHEDULE_ATO);
12760 
12761 
12762         IF l_request_search_rslt THEN
12763              Return;
12764         END IF;
12765 
12766     -- Log request.
12767         OE_Delayed_Requests_Pvt.Log_Request
12768        (p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
12769         p_entity_id              => l_line_rec.ato_line_id,
12770         p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
12771         p_requesting_entity_id   => l_line_rec.line_id,
12772         p_request_type           => OE_GLOBALS.G_SCHEDULE_ATO,
12773         p_param1               => OE_Schedule_Util.OESCH_ACT_RESCHEDULE,
12774         p_param2                 => l_line_rec.top_model_line_id,
12775         p_param3                 => l_line_rec.ship_from_org_id,
12776         p_param4                 => l_line_rec.ship_to_org_id,
12777         p_param5                 => l_line_rec.shipping_method_code,
12778         p_param6                 => l_line_rec.demand_class_code,
12779         p_param7                 => l_line_rec.ship_from_org_id,
12780         p_param8                 => l_line_rec.demand_class_code,
12781         p_param11                => l_line_rec.override_atp_date_code,
12782         p_date_param1            => l_line_rec.request_date,
12783         p_date_param2            => l_line_rec.schedule_ship_date,
12784         p_date_param3            => l_line_rec.schedule_arrival_date,
12785         p_date_param4            => l_line_rec.request_date,
12786         p_date_param5            => l_line_rec.schedule_ship_date,
12787         p_date_param6            => l_line_rec.schedule_arrival_date,
12788         p_param12                => 'DELETE',
12789         p_param14                => l_line_rec.ship_set_id,
12790         p_param13                => l_line_rec.arrival_set_id,
12791         p_param15              => l_line_rec.ship_model_complete_flag,
12792         p_param25                => l_line_rec.header_id,
12793         x_return_status          => l_return_status);
12794 
12795     END IF; -- Exists
12796 
12797 
12798    ELSE -- Not an ato child
12799 
12800      -- Start 2691579
12801      l_line_rec.operation := OE_GLOBALS.G_OPR_DELETE;
12802      IF l_debug_level  > 0 THEN
12803         oe_debug_pub.add(  'Operation '||l_line_rec.operation , 1 ) ;
12804      END IF;
12805      -- End 2691579
12806 
12807      Action_Undemand(p_old_line_rec  => l_line_rec,
12808                      x_return_status => l_return_status);
12809 
12810    END IF; -- GOP CODE LEVEL
12811 
12812   END IF;
12813 
12814   IF l_debug_level  > 0 THEN
12815       oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.DELETE_ROW' , 1 ) ;
12816   END IF;
12817 EXCEPTION
12818 
12819     WHEN OTHERS THEN
12820 
12821         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12822         THEN
12823             OE_MSG_PUB.Add_Exc_Msg
12824             (   G_PKG_NAME
12825             ,   'Delete_row'
12826             );
12827         END IF;
12828       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12829 END Delete_Row;
12830 /*---------------------------------------------------------------------
12831 Procedure Name : Transfer_reservation
12832 Description    : This API calls Inventory's APIs to Transfer_reservation.
12833 --------------------------------------------------------------------- */
12834 
12835 Procedure Transfer_reservation
12836 ( p_rsv_rec              IN  inv_reservation_global.mtl_reservation_rec_type
12837  ,p_quantity_to_transfer IN  NUMBER
12838  ,p_quantity2_to_transfer IN  NUMBER DEFAULT 0
12839  ,p_line_to_transfer     IN  NUMBER
12840 ,x_return_status OUT NOCOPY VARCHAR2)
12841 
12842 IS
12843 l_rsv_rec             inv_reservation_global.mtl_reservation_rec_type;
12844 l_msg_count           NUMBER;
12845 l_msg_data            VARCHAR2(240);
12846 l_rsv_id              NUMBER;
12847 l_dummy_sn            inv_reservation_global.serial_number_tbl_type;
12848 l_buffer              VARCHAR2(2000);
12849 
12850 --
12851 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12852 --
12853 BEGIN
12854                                     IF l_debug_level  > 0 THEN
12855                                         oe_debug_pub.add(  'ENTERING TRANSFER_RESERVATION LINE :' || P_QUANTITY_TO_TRANSFER , 1 ) ;
12856                                         oe_debug_pub.add(  'TRANSFER_RESERVATION LINE qty2 :' || P_QUANTITY2_TO_TRANSFER , 1 ) ;
12857                                     END IF;
12858                              IF l_debug_level  > 0 THEN
12859                                  oe_debug_pub.add(  'TOTAL RESERVATION ON THE LINE :' || P_RSV_REC.RESERVATION_QUANTITY , 1 ) ;
12860                                                                  oe_debug_pub.add(  'TOTAL RESERVATION qty2 ON THE LINE :' || P_RSV_REC.SECONDARY_RESERVATION_QUANTITY , 1 ) ;
12861                              END IF;
12862 
12863    IF l_debug_level  > 0 THEN
12864        oe_debug_pub.add(  ' LINE :' || P_LINE_TO_TRANSFER , 1 ) ;
12865    END IF;
12866    x_return_status := FND_API.G_RET_STS_SUCCESS;
12867 
12868    l_rsv_rec := p_rsv_rec;
12869    l_rsv_rec.reservation_id := fnd_api.g_miss_num;
12870    l_rsv_rec.demand_source_line_id := p_line_to_transfer;
12871    --- Start 2346233 --
12872    --l_rsv_rec.reservation_quantity :=  p_quantity_to_transfer;
12873    --l_rsv_rec.primary_reservation_quantity := fnd_api.g_miss_num;
12874    l_rsv_rec.primary_reservation_quantity :=  p_quantity_to_transfer;
12875    l_rsv_rec.secondary_reservation_quantity :=  p_quantity2_to_transfer; -- INVCONV
12876 
12877    l_rsv_rec.reservation_quantity := fnd_api.g_miss_num;
12878    l_rsv_rec.secondary_reservation_quantity := fnd_api.g_miss_num; -- INVCONV
12879    --- End 2346233 --
12880 
12881    IF l_debug_level  > 0 THEN
12882        oe_debug_pub.add(  'CALLING INVS TRANSFER RESERVATION' , 1 ) ;
12883    END IF;
12884 
12885    inv_reservation_pub.Transfer_reservation
12886    ( p_api_version_number        => 1.0
12887    , p_init_msg_lst              => FND_API.G_TRUE
12888    , x_return_status             => x_return_status
12889    , x_msg_count                 => l_msg_count
12890    , x_msg_data                  => l_msg_data
12891    , p_original_rsv_rec          => p_rsv_rec
12892    , p_to_rsv_rec                => l_rsv_rec
12893    , p_original_serial_number    => l_dummy_sn
12894    , p_to_serial_number          => l_dummy_sn
12895    , p_validation_flag           => FND_API.G_FALSE
12896    , x_to_reservation_id         => l_rsv_id);
12897 
12898                                              IF l_debug_level  > 0 THEN
12899                                                  oe_debug_pub.add(  '2. AFTER CALLING TRANSFER RESERVATION' || X_RETURN_STATUS , 1 ) ;
12900                                              END IF;
12901    IF l_debug_level  > 0 THEN
12902        oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
12903    END IF;
12904 
12905    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12906       oe_msg_pub.transfer_msg_stack;
12907       l_msg_count:=OE_MSG_PUB.COUNT_MSG;
12908       for I in 1..l_msg_count loop
12909          l_msg_data := OE_MSG_PUB.Get(I,'F');
12910          IF l_debug_level  > 0 THEN
12911              oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
12912          END IF;
12913       end loop;
12914       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12915    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12916       oe_msg_pub.transfer_msg_stack;
12917       l_msg_count:=OE_MSG_PUB.COUNT_MSG;
12918       for I in 1..l_msg_count loop
12919           l_msg_data := OE_MSG_PUB.Get(I,'F');
12920           IF l_debug_level  > 0 THEN
12921               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
12922           END IF;
12923       end loop;
12924       RAISE FND_API.G_EXC_ERROR;
12925 
12926    END IF;
12927 
12928                                           IF l_debug_level  > 0 THEN
12929                                               oe_debug_pub.add(  'EXITING TRANSFER RESERVATION' || X_RETURN_STATUS , 1 ) ;
12930                                           END IF;
12931 
12932 EXCEPTION
12933   WHEN FND_API.G_EXC_ERROR THEN
12934     x_return_status := FND_API.G_RET_STS_ERROR;
12935 
12936   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12937     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12938 
12939      oe_msg_pub.transfer_msg_stack;
12940      l_msg_count:=OE_MSG_PUB.COUNT_MSG;
12941      for I in 1..l_msg_count loop
12942         l_msg_data := OE_MSG_PUB.Get(I,'F');
12943         oe_msg_pub.add_text(l_msg_data);
12944         IF l_debug_level  > 0 THEN
12945             oe_debug_pub.add(  'INV : ' || L_MSG_DATA , 2 ) ;
12946         END IF;
12947      end loop;
12948 
12949   WHEN OTHERS THEN
12950     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12951 
12952     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12953     THEN
12954       OE_MSG_PUB.Add_Exc_Msg
12955       (   G_PKG_NAME
12956       ,   'Transfer_reservation'
12957        );
12958     END IF;
12959 
12960 END Transfer_reservation;
12961 
12962 PROCEDURE Set_Auto_Sch_Flag
12963 (p_value_from_user  IN VARCHAR2 := FND_API.G_MISS_CHAR)
12964 IS
12965 --
12966 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12967 --
12968 BEGIN
12969     IF l_debug_level  > 0 THEN
12970         oe_debug_pub.add(  'P_VALUE_FROM_USER ' || P_VALUE_FROM_USER , 1 ) ;
12971     END IF;
12972     OESCH_AUTO_SCH_FLAG := p_value_from_user;
12973 END Set_Auto_Sch_Flag;
12974 
12975 --Bug 5948059
12976 FUNCTION Get_Auto_Sch_Flag
12977 RETURN VARCHAR2
12978 IS
12979 --
12980 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12981 --
12982 BEGIN
12983     IF l_debug_level  > 0 THEN
12984         oe_debug_pub.add(  'In Get_Auto_Sch_Flag' ) ;
12985         oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG: '||OESCH_AUTO_SCH_FLAG);
12986     END IF;
12987     RETURN (OESCH_AUTO_SCH_FLAG);
12988 END;
12989 /* Function find line
12990 
12991   This is be used to find the line in the pl/sql table.
12992 */
12993 FUNCTION Find_line( p_x_line_tbl  IN OE_ORDER_PUB.Line_Tbl_Type,
12994                     p_line_id     IN  NUMBER)
12995 Return BOOLEAN
12996 IS
12997 --
12998 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12999 --
13000 BEGIN
13001 
13002   IF l_debug_level  > 0 THEN
13003       oe_debug_pub.add(  'ENTERING FIND_LINE: ' || P_LINE_ID , 1 ) ;
13004   END IF;
13005 
13006   FOR J IN 1..p_x_line_tbl.count LOOP
13007 
13008      IF p_line_id = p_x_line_tbl(J).line_id THEN
13009 
13010          IF l_debug_level  > 0 THEN
13011              oe_debug_pub.add(  ' LINE EXISTS IN THE TABLE' , 1 ) ;
13012          END IF;
13013 
13014          RETURN TRUE;
13015      END IF;
13016   END LOOP;
13017 
13018  RETURN FALSE;
13019 
13020 END Find_line;
13021 
13022 /*PROCEDURE PROCESS_SPLIT
13023   This procedure will be used to call mrp with appropriate records:
13024   If the ato model is part of SMC, then Whome smc model will be called
13025   If the ato model is part of set, then whole set information will
13026   be passed to MRP and so on */
13027 
13028 PROCEDURE PROCESS_SPLIT
13029 (p_x_line_tbl  IN OE_ORDER_PUB.Line_Tbl_Type,
13030  x_return_status OUT NOCOPY VARCHAR2)
13031 
13032 IS
13033 l_line_tbl              OE_ORDER_PUB.line_tbl_type;
13034 l_local_line_tbl        OE_ORDER_PUB.line_tbl_type;
13035 K                       NUMBER;
13036 I                       NUMBER;
13037 l_ato_line_id           NUMBER;
13038 l_entity                VARCHAR2(30);
13039 
13040 -- MRP API variables
13041 l_session_id            NUMBER := 0;
13042 l_mrp_atp_rec           MRP_ATP_PUB.ATP_Rec_Typ;
13043 l_out_mtp_atp_rec       MRP_ATP_PUB.ATP_Rec_Typ;
13044 l_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
13045 l_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
13046 l_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
13047 mrp_msg_data            VARCHAR2(200);
13048 l_on_hand_qty           NUMBER;
13049 l_avail_to_reserve      NUMBER;
13050 l_out_atp_tbl           OE_ATP.atp_tbl_type;
13051 l_found                 BOOLEAN := FALSE;
13052 l_buffer                VARCHAR2(2000);
13053 l_msg_count             NUMBER;
13054 l_return_status         VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
13055 l_set_rec               OE_ORDER_CACHE.set_rec_type;
13056 
13057 --
13058 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13059 --
13060 BEGIN
13061 
13062    IF l_debug_level  > 0 THEN
13063        oe_debug_pub.add(  'ENTERING PROCESS SPLIT' , 1 ) ;
13064    END IF;
13065 
13066    x_return_status := FND_API.G_RET_STS_SUCCESS;
13067    K := 0;
13068 
13069    FOR I IN 1..p_x_line_tbl.count LOOP
13070 
13071     IF NOT find_line(p_x_line_tbl => l_line_tbl,
13072                      p_line_id    => p_x_line_tbl(I).line_id)
13073     THEN
13074 
13075      IF p_x_line_tbl(I).schedule_status_code is not null THEN -- 2680748 to restrict unscheduled lines
13076 
13077         IF p_x_line_tbl(I).arrival_set_id is not null THEN
13078 
13079            OE_Set_Util.Query_Set_Rows(p_set_id   => p_x_line_tbl(I).arrival_set_id,
13080                                  x_line_tbl => l_local_line_tbl);
13081 
13082            l_set_rec := OE_ORDER_CACHE.Load_Set
13083                          (p_x_line_tbl(I).arrival_set_id);
13084 
13085            FOR L IN 1..l_local_line_tbl.count LOOP
13086 
13087               K := K +1;
13088               l_line_tbl(K) := l_local_line_tbl(L);
13089               l_line_tbl(K).schedule_action_code :=
13090                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
13091               l_line_tbl(K).arrival_set := l_set_rec.set_name;
13092 
13093            END LOOP;
13094 
13095            l_local_line_tbl.delete;
13096 
13097         ELSIF p_x_line_tbl(I).ship_set_id is not null THEN
13098 
13099 
13100            OE_Set_Util.Query_Set_Rows(p_set_id   => p_x_line_tbl(I).ship_set_id,
13101                                  x_line_tbl => l_local_line_tbl);
13102 
13103            l_set_rec := OE_ORDER_CACHE.Load_Set
13104                          (p_x_line_tbl(I).ship_set_id);
13105            FOR L IN 1..l_local_line_tbl.count LOOP
13106 
13107               K := K +1;
13108               l_line_tbl(K) := l_local_line_tbl(L);
13109               l_line_tbl(K).schedule_action_code :=
13110                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
13111 
13112               l_line_tbl(K).ship_set := l_set_rec.set_name;
13113            END LOOP;
13114 
13115            l_local_line_tbl.delete;
13116 
13117         ELSIF p_x_line_tbl(I).ship_model_complete_flag ='Y'
13118              AND   nvl(p_x_line_tbl(I).model_remnant_flag,'N') = 'N' THEN
13119 
13120 
13121            OE_Config_Util.Query_Options
13122            (p_top_model_line_id => p_x_line_tbl(I).top_model_line_id,
13123             x_line_tbl          => l_local_line_tbl);
13124 
13125 
13126            FOR L IN 1..l_local_line_tbl.count LOOP
13127 
13128               K := K +1;
13129               l_line_tbl(K) := l_local_line_tbl(L);
13130               l_line_tbl(K).schedule_action_code :=
13131                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
13132               l_line_tbl(K).ship_set := p_x_line_tbl(I).top_model_line_id;
13133 
13134            END LOOP;
13135 
13136            l_local_line_tbl.delete;
13137 
13138         ELSIF p_x_line_tbl(I).ato_line_id is not null
13139              AND   p_x_line_tbl(I).item_type_code <> 'STANDARD'
13140              AND   nvl(p_x_line_tbl(I).model_remnant_flag,'N') = 'N' THEN
13141 
13142             Begin
13143 
13144                Select ato_line_id
13145                Into   l_ato_line_id
13146                From   oe_order_lines_all
13147                Where  line_id = p_x_line_tbl(I).line_id;
13148             EXCEPTION
13149 
13150                WHEN OTHERS THEN
13151 
13152                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13153 
13154             END;
13155 
13156             OE_Config_Util.Query_ATO_Options
13157             (p_ato_line_id => l_ato_line_id,
13158             x_line_tbl    => l_local_line_tbl);
13159 
13160 
13161             FOR L IN 1..l_local_line_tbl.count LOOP
13162 
13163                K := K +1;
13164                l_line_tbl(K) := l_local_line_tbl(L);
13165                l_line_tbl(K).schedule_action_code :=
13166                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
13167                l_line_tbl(K).ship_set := l_ato_line_id;
13168 
13169 
13170            END LOOP;
13171 
13172            l_local_line_tbl.delete;
13173 
13174 
13175         ELSE
13176 
13177            K := K +1;
13178            l_line_tbl(K) := p_x_line_tbl(I);
13179            l_line_tbl(K).schedule_action_code :=
13180                         OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
13181 
13182 
13183         END IF;
13184       END IF;  -- 2680748
13185 
13186     END IF; -- line is not part of the locak table.
13187    END LOOP;
13188 
13189    G_OVERRIDE_FLAG := 'Y';
13190 
13191    IF l_line_tbl.count > 0 THEN
13192 
13193         IF l_debug_level  > 0 THEN
13194             oe_debug_pub.add(  'SPLIT BEFORE CALLING LOAD_MRP_REQUEST' , 2 ) ;
13195         END IF;
13196 
13197           Load_MRP_request_from_tbl
13198           ( p_line_tbl          => l_line_tbl
13199            ,p_old_line_tbl      => l_line_tbl
13200            ,p_sch_action        => OESCH_ACT_RESCHEDULE
13201            ,x_mrp_atp_rec       => l_mrp_atp_rec);
13202 
13203           l_session_id := Get_Session_Id;
13204 
13205           -- Call ATP
13206           IF l_debug_level  > 0 THEN
13207               oe_debug_pub.add(  'COUNT IS ' || L_MRP_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
13208           END IF;
13209 
13210           -- We are adding this so that we will not call MRP when
13211           -- table count is 0.
13212 
13213          IF l_mrp_atp_rec.error_code.count > 0 THEN
13214 
13215           IF l_debug_level  > 0 THEN
13216               oe_debug_pub.add(  'SPLIT CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
13217           END IF;
13218 
13219           MRP_ATP_PUB.Call_ATP
13220           (  p_session_id             =>  l_session_id
13221            , p_atp_rec                =>  l_mrp_atp_rec
13222            , x_atp_rec                =>  l_out_mtp_atp_rec
13223            , x_atp_supply_demand      =>  l_atp_supply_demand
13224            , x_atp_period             =>  l_atp_period
13225            , x_atp_details            =>  l_atp_details
13226            , x_return_status          =>  l_return_status
13227            , x_msg_data               =>  mrp_msg_data
13228            , x_msg_count              =>  l_msg_count);
13229 
13230 
13231           IF l_debug_level  > 0 THEN
13232              oe_debug_pub.add(  'SPLIT. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || L_RETURN_STATUS , 1 ) ;
13233           END IF;
13234 
13235           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13236                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13237           END IF;
13238 
13239           Load_Results_from_tbl
13240           (p_atp_rec        => l_out_mtp_atp_rec,
13241            p_old_line_tbl   => l_line_tbl,
13242            p_x_line_tbl     => l_line_tbl,
13243            x_return_status  => l_return_status);
13244 
13245           IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13246                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13247           ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
13248                 RAISE FND_API.G_EXC_ERROR;
13249           END IF;
13250 
13251          END IF; -- MRP count check.
13252 
13253 
13254    END IF; -- line count.
13255 
13256    G_OVERRIDE_FLAG := 'N';
13257    x_return_status := l_return_status;
13258 
13259   IF l_debug_level  > 0 THEN
13260       oe_debug_pub.add(  'EXITING PROCESS SPLIT' , 1 ) ;
13261   END IF;
13262 EXCEPTION
13263   WHEN OTHERS THEN
13264 
13265         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13266         THEN
13267             OE_MSG_PUB.Add_Exc_Msg
13268             (   G_PKG_NAME
13269             ,   'PROCESS_SPLIT'
13270             );
13271         END IF;
13272 
13273 END PROCESS_SPLIT;
13274 
13275 /*--------------------------------------------------------------------
13276 Procedure Name : Split_Scheduling
13277 Description    : The split API calls this procedure with a table of record.
13278                  There is an update line (the line which is getting split)
13279                  and multiple insert lines (new lines created due to the split).
13280                  We need to do the following:
13281 
13282                  For scheduling
13283                  -------------
13284                  On the updated line: Reschedule the line.
13285                  On the inserted lines: Schedule the lines.
13286 
13287                  For reservation
13288                  ---------------
13289                  If the split is due to shipping, we need to update the
13290                  reservations (whichever exist) to the new line which
13291                  got created.
13292 
13293                  If the split is due to the user splitting, there could be
13294                  multiple records created due to the split. We should update
13295                  the old reservation to reflect the change in qty for the
13296                  original line, and create new reservations for the new lines
13297                  which got created.
13298 
13299 
13300 ---------------------------------------------------------------------- */
13301 
13302 
13303 Procedure SPLIT_SCHEDULING
13304 ( p_x_line_tbl         IN OUT NOCOPY OE_ORDER_PUB.line_tbl_type
13305 , x_return_status OUT NOCOPY VARCHAR2)
13306 
13307 IS
13308 l_rsv_qty               NUMBER := 0;
13309 l_header_id             NUMBER;
13310 l_line_id               NUMBER;
13311 l_sales_order_id        NUMBER;
13312 l_qty_to_retain         NUMBER;
13313 l_x_error_code          NUMBER;
13314 l_lock_records          VARCHAR2(1);
13315 l_sort_by_req_date      NUMBER;
13316 continue_loop           BOOLEAN;
13317 K                       NUMBER;
13318 J                       NUMBER;
13319 l_reserved_quantity     NUMBER;
13320 l_msg_count             NUMBER;
13321 l_query_rsv_rec         inv_reservation_global.mtl_reservation_rec_type;
13322 l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
13323 l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
13324 l_msg_data              VARCHAR2(240);
13325 l_count                 NUMBER;
13326 l_available_qty         NUMBER := 0;
13327 --- 2346233 --
13328 l_ordered_quantity      NUMBER;
13329 l_rsv_qty_primary       NUMBER :=0;
13330 
13331 -- INVCONV
13332 l_reserved_quantity2     NUMBER;
13333 l_ordered_quantity2      NUMBER;
13334 l_rsv_qty_secondary      NUMBER :=0;
13335 l_qty2_to_retain         NUMBER;
13336 l_rsv_qty2               NUMBER := 0;
13337 l_available_qty2         NUMBER := 0;
13338 
13339 --
13340 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13341 --
13342 BEGIN
13343 
13344   IF l_debug_level  > 0 THEN
13345       oe_debug_pub.add(  '31. ENTERING SPLIT_SCHEDULING' , 1 ) ;
13346   END IF;
13347 
13348   x_return_status := FND_API.G_RET_STS_SUCCESS;
13349   IF l_debug_level  > 0 THEN
13350       oe_debug_pub.add(  'PICTURE SENT ' , 1 ) ;
13351   END IF;
13352 
13353   FOR I IN 1..p_x_line_tbl.count LOOP
13354       IF l_debug_level  > 0 THEN
13355           oe_debug_pub.add(  'LINE ID ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
13356       END IF;
13357       IF l_debug_level  > 0 THEN
13358           oe_debug_pub.add(  'SPLIT ID ' || P_X_LINE_TBL ( I ) .SPLIT_FROM_LINE_ID , 1 ) ;
13359       END IF;
13360       IF l_debug_level  > 0 THEN
13361           oe_debug_pub.add(  'SPLIT ACTION ' || P_X_LINE_TBL ( I ) .SPLIT_ACTION_CODE , 1 ) ;
13362       END IF;
13363       IF l_debug_level  > 0 THEN
13364           oe_debug_pub.add(  'OPERATIONS ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
13365       END IF;
13366   END LOOP;
13367 
13368   G_OVERRIDE_FLAG     := 'Y';
13369   IF l_debug_level  > 0 THEN
13370       oe_debug_pub.add(  'COUNT IS :' || P_X_LINE_TBL.COUNT , 1 ) ;
13371   END IF;
13372 
13373   process_split(p_x_line_tbl => p_x_line_tbl,
13374                 x_return_status => x_return_status);
13375 
13376   FOR I in 1..p_x_line_tbl.count LOOP
13377 
13378        IF p_x_line_tbl(I).operation = OE_GLOBALS.G_OPR_UPDATE AND
13379           p_x_line_tbl(I).schedule_status_code is not null AND
13380           p_x_line_tbl(I).split_action_code = 'SPLIT' THEN
13381 
13382           IF l_debug_level  > 0 THEN
13383               oe_debug_pub.add(  'SPLITTING SCHEDULING' , 1 ) ;
13384           END IF;
13385 
13386           IF l_debug_level  > 0 THEN
13387               oe_debug_pub.add(  ' ' , 1 ) ;
13388           END IF;
13389                                                IF l_debug_level  > 0 THEN
13390                                                    oe_debug_pub.add(  'SPLITTING SCHEDULING FOR LINE: ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
13391                                                END IF;
13392 /*
13393           -- This is the line which is getting split. We should
13394           -- reschedule the line with the new quantity.
13395 
13396           p_x_line_tbl(I).schedule_action_code := OESCH_ACT_RESCHEDULE;
13397 
13398           Call_MRP_ATP(p_x_line_rec    =>  p_x_line_tbl(I),
13399                        p_old_line_rec  =>  p_x_line_tbl(I),
13400                        x_return_status =>  x_return_status);
13401 
13402           p_x_line_tbl(I).schedule_action_code := Null;
13403 
13404           -- Now let's schedule the new split lines.
13405           FOR J IN 1..p_x_line_tbl.count LOOP
13406               IF p_x_line_tbl(J).operation = OE_GLOBALS.G_OPR_CREATE AND
13407                  p_x_line_tbl(J).split_from_line_id = p_x_line_tbl(I).line_id
13408               THEN
13409                   oe_debug_pub.add('Split lines ship from: ' ||
13410                                      p_x_line_tbl(J).ship_from_org_id,1);
13411 
13412                   -- Bug fix for 1881229
13413 
13414                    p_x_line_tbl(J).schedule_action_code := OESCH_ACT_SCHEDULE;
13415                    Call_MRP_ATP(p_x_line_rec    => p_x_line_tbl(J),
13416                                 p_old_line_rec  => p_x_line_tbl(J),
13417                                 x_return_status => x_return_status);
13418 
13419                     p_x_line_tbl(J).schedule_action_code :=  Null;
13420 
13421               END IF;
13422           END LOOP;
13423 */
13424           -- Only play with reservation if the line is not interfaced with
13425           -- shipping. Otherwise shipping will take care of transferring
13426           -- reservations.
13427 
13428           IF  (p_x_line_tbl(I).shipped_quantity is null)
13429           AND (nvl(p_x_line_tbl(I).shipping_interfaced_flag, 'N') = 'N')
13430           THEN
13431 
13432             -- We have updated the demand picture in MRP with the split.
13433             -- Now let's update the reservation picture.
13434 
13435       --      l_query_rsv_rec                := null;
13436             l_query_rsv_rec.reservation_id := fnd_api.g_miss_num;
13437             l_sales_order_id
13438                        := Get_mtl_sales_order_id(p_x_line_tbl(I).header_id);
13439             l_query_rsv_rec.demand_source_header_id  := l_sales_order_id;
13440             l_query_rsv_rec.demand_source_line_id    := p_x_line_tbl(I).line_id;
13441 
13442             -- 02-jun-2000 mpetrosi added org_id to query_reservation start
13443             l_query_rsv_rec.organization_id := p_x_line_tbl(I).ship_from_org_id;
13444             -- 02-jun-2000 mpetrosi added org_id to query_reservation end
13445 
13446 
13447             IF l_debug_level  > 0 THEN
13448                 oe_debug_pub.add(  'CALLING INVS QUERY_RESERVATION ' , 1 ) ;
13449             END IF;
13450 
13451             inv_reservation_pub.query_reservation
13452               ( p_api_version_number       => 1.0
13453               , p_init_msg_lst              => fnd_api.g_true
13454               , x_return_status             => x_return_status
13455               , x_msg_count                 => l_msg_count
13456               , x_msg_data                  => l_msg_data
13457               , p_query_input               => l_query_rsv_rec
13458               , x_mtl_reservation_tbl       => l_rsv_tbl
13459               , x_mtl_reservation_tbl_count => l_count
13460               , x_error_code                => l_x_error_code
13461               , p_lock_records              => l_lock_records
13462               , p_sort_by_req_date          => l_sort_by_req_date
13463               );
13464 
13465                                  IF l_debug_level  > 0 THEN
13466                                      oe_debug_pub.add(  'AFTER CALLING INVS QUERY_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
13467                                  END IF;
13468 
13469             IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13470               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13471             ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13472               RAISE FND_API.G_EXC_ERROR;
13473             END IF;
13474 
13475                                           IF l_debug_level  > 0 THEN
13476                                               oe_debug_pub.add(  'RESERVATION RECORD COUNT IS: ' || L_RSV_TBL.COUNT , 1 ) ;
13477                                           END IF;
13478 
13479             -- Let's get the total reserved_quantity
13480             l_reserved_quantity := 0;
13481             -- INVCONV
13482             l_reserved_quantity2 := 0;
13483 
13484             FOR K IN 1..l_rsv_tbl.count LOOP
13485             -- Start 2346233 --
13486            /*    l_reserved_quantity := l_reserved_quantity +
13487                                        l_rsv_tbl(K).reservation_quantity; */
13488 
13489 
13490             l_reserved_quantity := l_reserved_quantity + l_rsv_tbl(K).primary_reservation_quantity;
13491                         l_reserved_quantity2 := l_reserved_quantity2 + l_rsv_tbl(K).secondary_reservation_quantity; -- INVCONV
13492             -- End 2346233 --
13493             END LOOP;
13494 
13495             IF l_debug_level  > 0 THEN
13496                 oe_debug_pub.add(  'RESERVED QUANTITY : ' || L_RESERVED_QUANTITY , 1 ) ;
13497                                 oe_debug_pub.add(  'RESERVED QUANTITY2 : ' || L_RESERVED_QUANTITY2 , 1 ) ;
13498             END IF;
13499 
13500          -- We should process reservation logic only when reservation qty exists
13501          -- and parent line order qty is less than total reserved qty. If parent
13502          -- ordered qty is greter or equal to total reserved qty, we do not need
13503          -- transfer any reservations.
13504 
13505             -- Start 2346233 --
13506             IF  l_reserved_quantity > 0 THEN
13507                IF NOT OE_GLOBALS.Equal(p_x_line_tbl(I).order_quantity_uom,
13508                                        l_rsv_tbl(1).primary_uom_code) THEN
13509                   l_ordered_quantity :=
13510                         INV_CONVERT.INV_UM_CONVERT( item_id       => p_x_line_tbl(I).inventory_item_id,
13511                                                     precision     => 5,
13512                                                     from_quantity =>p_x_line_tbl(I).ordered_quantity,
13513                                                     from_unit     =>p_x_line_tbl(I).order_quantity_uom,
13514                                                     to_unit       =>l_rsv_tbl(1).primary_uom_code,
13515                                                     from_name     =>NULL,
13516                                                     to_name       =>NULL
13517                                                    );
13518 
13519                 ELSE
13520                    l_ordered_quantity := p_x_line_tbl(I).ordered_quantity;
13521                 END IF;
13522                 l_ordered_quantity2 := p_x_line_tbl(I).ordered_quantity2; -- INVCONV
13523             END IF;
13524 
13525             /* IF   l_reserved_quantity > 0
13526             AND  p_x_line_tbl(I).ordered_quantity < l_reserved_quantity
13527             THEN */
13528             IF   l_reserved_quantity > 0
13529                AND  l_ordered_quantity < l_reserved_quantity
13530             THEN
13531            ---- End 2346233 ---
13532 
13533            -- There can be 2 kinds of splits. One where the user split,
13534            -- in which case, the reservations have to split if line is not
13535            -- shipping interfaced. WSH interfaced line will be taken careby WSH.
13536            -- System split happens when shipping occurs partially.
13537            -- In that case, the remaining reservations are trasferred to the
13538            -- new line. This will be taken care by shipping code.
13539 
13540            -- l_qty_to_retain is to retain in the reservation record.
13541            -- l_available_qty is reamining qty that can be transferred.
13542 
13543               IF l_debug_level  > 0 THEN
13544                   oe_debug_pub.add(  'USER INITIATED SPLIT' , 1 ) ;
13545               END IF;
13546 
13547               --- Start 2346233 --
13548               -- l_qty_to_retain := p_x_line_tbl(I).ordered_quantity;
13549               l_qty_to_retain := l_ordered_quantity;
13550               l_qty2_to_retain := l_ordered_quantity2; -- INVCONV
13551               --- End 2346233 --
13552 
13553               /* need OPM branching  -- Don't branch now  INVCONV
13554               IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_x_line_tbl(I).ship_from_org_id) THEN */
13555                  J:= 1;
13556                  FOR K IN 1..l_rsv_tbl.count LOOP
13557                    IF l_qty_to_retain > 0 THEN
13558 
13559                       IF l_debug_level  > 0 THEN
13560                         oe_debug_pub.add(  'L_QTY_TO_RETAIN ' || L_QTY_TO_RETAIN , 1 ) ;
13561                       END IF;
13562                       ---- Start 2346233 ---
13563                       IF l_rsv_tbl(K).primary_reservation_quantity <= l_qty_to_retain
13564                       THEN
13565                          l_qty_to_retain := l_qty_to_retain -
13566                                           l_rsv_tbl(K).primary_reservation_quantity;
13567                          l_qty2_to_retain := nvl(l_qty2_to_retain, 0) -
13568                                           nvl(l_rsv_tbl(K).secondary_reservation_quantity, 0); -- INVCONV
13569                      /*
13570                       IF l_rsv_tbl(K).reservation_quantity <= l_qty_to_retain
13571                       THEN
13572                          l_qty_to_retain := l_qty_to_retain -
13573                                           l_rsv_tbl(K).reservation_quantity;
13574                      */
13575                       ---- End 2346233 ---
13576 
13577                        IF l_debug_level  > 0 THEN
13578                           oe_debug_pub.add(  'BEFORE DELETE ' || K || '/' || L_RSV_TBL ( K ) .RESERVATION_QUANTITY , 1 ) ;
13579                           oe_debug_pub.add(  'BEFORE DELETE Primary ' || K || '/' || L_RSV_TBL ( K ) .PRIMARY_RESERVATION_QUANTITY , 1 ) ;
13580 
13581                        END IF;
13582                        l_rsv_tbl.delete(K);
13583                     END IF;
13584 
13585                    END IF; -- l_qty_to_retain.
13586 
13587                  END LOOP;
13588 
13589                  K := l_rsv_tbl.first;
13590                  continue_loop := TRUE;
13591                  l_rsv_rec := l_rsv_tbl(k);
13592                  WHILE K IS NOT NULL LOOP
13593                    IF l_debug_level  > 0 THEN
13594                        oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP K ' || K , 1 ) ;
13595                    END IF;
13596                    IF l_qty_to_retain = 0 THEN
13597                       l_rsv_rec := l_rsv_tbl(k);
13598                    END IF;
13599 
13600                    WHILE J <= p_x_line_tbl.count AND continue_loop
13601                    LOOP
13602                      IF l_debug_level  > 0 THEN
13603                          oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP J ' || J , 1 ) ;
13604                      END IF;
13605                      IF p_x_line_tbl(J).operation =
13606                                         OE_GLOBALS.G_OPR_CREATE AND
13607                         p_x_line_tbl(J).split_from_line_id =
13608                                          p_x_line_tbl(I).line_id
13609                      THEN
13610                          continue_loop := FALSE;
13611                          l_rsv_qty := p_x_line_tbl(J).ordered_quantity;
13612                          l_rsv_qty2 := p_x_line_tbl(J).ordered_quantity2; -- INVCONV
13613                          ---- Start 2346233 ---
13614                          IF NOT OE_GLOBALS.Equal(p_x_line_tbl(J).order_quantity_uom,
13615                                                  l_rsv_rec.primary_uom_code) THEN
13616                             l_rsv_qty_primary :=
13617                               INV_CONVERT.INV_UM_CONVERT( item_id       => p_x_line_tbl(J).inventory_item_id,
13618                                                           precision     => 5,
13619                                                           from_quantity =>p_x_line_tbl(J).ordered_quantity,
13620                                                           from_unit     =>p_x_line_tbl(J).order_quantity_uom,
13621                                                           to_unit       =>l_rsv_rec.primary_uom_code,
13622                                                           from_name     =>NULL,
13623                                                           to_name       =>NULL
13624                                                         );
13625 
13626                          ELSE
13627                             l_rsv_qty_Primary := p_x_line_tbl(J).ordered_quantity;
13628                          END IF;
13629                          l_rsv_qty_secondary := p_x_line_tbl(J).ordered_quantity2; -- INVCONV
13630                          ---- End 2346233 ---
13631 
13632                          IF l_debug_level  > 0 THEN
13633                             oe_debug_pub.add(  'FOUND NEW RECORD ' || P_X_LINE_TBL ( J ) .LINE_ID || '/' ||L_RSV_QTY , 1 ) ;
13634                          END IF;
13635                          l_line_id :=  p_x_line_tbl(J).line_id;
13636                          J := J +1;
13637                      ELSE
13638                          J := J + 1;
13639                      END IF;
13640                    END LOOP;
13641 
13642                    IF l_rsv_qty > 0  THEN
13643                      --- Start 2346233 --
13644                      /* l_available_qty := l_rsv_rec.reservation_quantity -
13645                                                     l_qty_to_retain; */
13646                      l_available_qty := l_rsv_rec.primary_reservation_quantity -
13647                                         l_qty_to_retain;
13648                                          l_available_qty2 := nvl(l_rsv_rec.secondary_reservation_quantity, 0) -
13649                                         nvl(l_qty2_to_retain, 0) ;  -- INVCONV
13650                      IF l_available_qty2 = 0
13651                       THEN
13652                       l_available_qty2 := NULL;
13653                      END IF;
13654 
13655 
13656 
13657 
13658 
13659                      IF l_debug_level  > 0 THEN
13660                         oe_debug_pub.add(  'L_RSV_QTY_PRIMARY' || l_rsv_qty_primary , 1 ) ;
13661                      END IF;
13662 
13663                      -- IF  l_rsv_qty <= l_available_qty  THEN
13664                      IF  l_rsv_qty_primary <= l_available_qty  THEN
13665 
13666                      --- End 2346233 --
13667 
13668                        IF l_debug_level  > 0 THEN
13669                            oe_debug_pub.add(  'AVAILABLE MORE THAN NEEDED' || L_AVAILABLE_QTY , 1 ) ;
13670                        END IF;
13671                        -- Transfer full l_rsv_qty.
13672                        Transfer_Reservation
13673                        (p_rsv_rec              => l_rsv_rec,
13674                         p_quantity_to_transfer => l_rsv_qty,
13675                         p_quantity2_to_transfer => l_rsv_qty2, -- INVCONV
13676                         p_line_to_transfer     => l_line_id,
13677                         x_return_status        => x_return_status);
13678                         --- Start 2346233 --
13679                         /* l_rsv_rec.reservation_quantity := l_rsv_rec.reservation_quantity -
13680                                                          l_rsv_qty;
13681                         -- l_qty_to_retain := l_qty_to_retain + l_rsv_qty;
13682                         l_available_qty := l_rsv_rec.reservation_quantity -
13683                                                         l_qty_to_retain;*/
13684                         l_rsv_rec.primary_reservation_quantity :=
13685                                 l_rsv_rec.primary_reservation_quantity - l_rsv_qty_primary;
13686                         l_available_qty := l_rsv_rec.primary_reservation_quantity - l_qty_to_retain;
13687                         l_rsv_qty_primary := 0;
13688                                                 -- INVCONV
13689                                                 l_rsv_rec.secondary_reservation_quantity :=
13690                                 nvl(l_rsv_rec.secondary_reservation_quantity,0) - nvl(l_rsv_qty_secondary,0 );
13691 
13692                         IF l_rsv_rec.secondary_reservation_quantity = 0 -- INVCONV
13693                           THEN
13694                            l_rsv_rec.secondary_reservation_quantity := NULL;
13695                         END IF;
13696 
13697                         l_available_qty2 := l_rsv_rec.secondary_reservation_quantity - l_qty2_to_retain;
13698 
13699                         IF l_available_qty2 = 0 -- INVCONV
13700                           THEN
13701                            l_available_qty2 := NULL;
13702                         END IF;
13703 
13704 
13705                         l_rsv_qty_secondary := 0;
13706 
13707 
13708                         --- End 2346233 --
13709 
13710                        l_rsv_qty := 0;
13711                        l_rsv_qty2 := 0; -- INVCONV
13712                        continue_loop := TRUE;
13713                        IF l_debug_level  > 0 THEN
13714                            oe_debug_pub.add(  'AVAILABLE QTY ' || L_AVAILABLE_QTY , 1 ) ;
13715                        END IF;
13716 
13717 
13718                        -- Start 2346233 --
13719                        --IF l_rsv_rec.reservation_quantity = l_qty_to_retain THEN
13720                        -- Bug 5014710
13721                        IF round(l_rsv_rec.primary_reservation_quantity,5) = round(l_qty_to_retain,5) THEN
13722                        -- End 2346233 --
13723                           K := l_rsv_tbl.next(K);
13724                           l_available_qty := 0;
13725                           l_qty_to_retain := 0;
13726                           l_available_qty2 := 0; -- INVCONV
13727                           l_qty2_to_retain := 0; -- INVCONV
13728 
13729                        END IF;
13730                      ELSE
13731                            -- Transfer remaining
13732                        Transfer_Reservation
13733                        (p_rsv_rec               =>l_rsv_rec,
13734                         p_quantity_to_transfer  =>l_available_qty,
13735                         p_quantity2_to_transfer =>l_available_qty2, -- INVCONV
13736                         p_line_to_transfer     =>l_line_id,
13737                         x_return_status        =>x_return_status);
13738 
13739                         l_rsv_qty := l_rsv_qty - l_available_qty;
13740                         l_available_qty := 0;
13741                         l_qty_to_retain := 0;
13742 
13743                         -- INVCONV
13744                         l_rsv_qty2 := nvl(l_rsv_qty2, 0) - nvl(l_available_qty2, 0); -- INVCONV
13745                         l_available_qty2 := 0;
13746                         l_qty2_to_retain := 0;
13747 
13748                         K := l_rsv_tbl.next(K);
13749                       END IF;
13750                     END IF; -- l_rsv_qty > 0
13751                   -- END LOOP;
13752                  END LOOP; -- K loop.
13753                 /*  ELSE  if OPM -- INVCONV  - don;t need this branch cos no OPM now .
13754                  J:= 1;
13755                  -- Bug 3330925 (getting rid of continue_loop)
13756                  -- continue_loop := TRUE;
13757                  WHILE J <= p_x_line_tbl.count -- AND continue_loop
13758                  LOOP
13759                    IF l_debug_level  > 0 THEN
13760                        oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP J TO FIND THE SPLITTED LINE ' || J , 1 ) ;
13761                    END IF;
13762 
13763 
13764                    IF p_x_line_tbl(J).operation =
13765                                       OE_GLOBALS.G_OPR_CREATE AND
13766                       p_x_line_tbl(J).split_from_line_id =
13767                                        p_x_line_tbl(I).line_id
13768                    THEN
13769                        -- continue_loop := FALSE;
13770                        l_rsv_qty := p_x_line_tbl(J).ordered_quantity;
13771                                          IF l_debug_level  > 0 THEN
13772                                              oe_debug_pub.add(  'FOUND NEW RECORD ' || P_X_LINE_TBL ( J ) .LINE_ID || '/' ||L_RSV_QTY , 1 ) ;
13773                                          END IF;
13774                        l_line_id :=  p_x_line_tbl(J).line_id;
13775 
13776                        -- Begin Bug 3330925
13777                        IF l_debug_level  > 0 THEN
13778                         oe_debug_pub.add(  'CALLING GMI SPLIT_TRANS_FROM_OM ' ) ;
13779                        END IF;
13780                        GMI_RESERVATION_UTIL.split_trans_from_OM
13781                          ( p_old_source_line_id     => p_x_line_tbl(I).line_id,
13782                            p_new_source_line_id     => l_line_id,
13783                            p_qty_to_split           => p_x_line_tbl(I).ordered_quantity,
13784                            p_qty2_to_split          => p_x_line_tbl(I).ordered_quantity2,
13785                            x_return_status          => x_return_status,
13786                            x_msg_count              => l_msg_count,
13787                            x_msg_data               => l_msg_data
13788                          );
13789                        -- End Bug 3330925
13790 
13791                        J := J +1;
13792                    ELSE
13793                        J := J + 1;
13794                    END IF;
13795                  END LOOP; -- J loop
13796 
13797                  -- Bug3330925 (moved this within the Loop above)
13798                   IF l_debug_level  > 0 THEN
13799                      oe_debug_pub.add(  'CALLING GMI SPLIT_TRANS_FROM_OM ' ) ;
13800                  END IF;
13801                  GMI_RESERVATION_UTIL.split_trans_from_OM
13802                      ( p_old_source_line_id     => p_x_line_tbl(I).line_id,
13803                        p_new_source_line_id     => l_line_id,
13804                        p_qty_to_split           => p_x_line_tbl(I).ordered_quantity,
13805                        p_qty2_to_split          => p_x_line_tbl(I).ordered_quantity2,
13806                        x_return_status          => x_return_status,
13807                        x_msg_count              => l_msg_count,
13808                        x_msg_data               => l_msg_data
13809                       );
13810                END IF; --  OPM branching  */  -- INVCONV - not needed  now
13811             END IF;  -- l_reserved_quantity > 0
13812           END IF; --  shipped_quantity is null
13813 
13814     END IF; -- If operation on the line was UPDATE
13815 
13816   END LOOP; -- Main Loop.
13817 
13818   G_OVERRIDE_FLAG     := 'N';
13819   IF l_debug_level  > 0 THEN
13820       oe_debug_pub.add(  'SCHEDULING RESULTS OF THE LINES: ' , 1 ) ;
13821   END IF;
13822   IF l_debug_level  > 0 THEN
13823       oe_debug_pub.add(  ' ' , 1 ) ;
13824   END IF;
13825 
13826 
13827   FOR I IN 1..p_x_line_tbl.count LOOP
13828                             IF l_debug_level  > 0 THEN
13829                                 oe_debug_pub.add(  'LINE ID : ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
13830                             END IF;
13831                             IF l_debug_level  > 0 THEN
13832                                 oe_debug_pub.add(  'SCHEDULE STATUS : ' || P_X_LINE_TBL ( I ) .SCHEDULE_STATUS_CODE , 1 ) ;
13833                             END IF;
13834       IF l_debug_level  > 0 THEN
13835           oe_debug_pub.add(  ' ' , 1 ) ;
13836       END IF;
13837   END LOOP;
13838   IF l_debug_level  > 0 THEN
13839       oe_debug_pub.add(  'EXITING SPLIT_SCHEDULING WITH ' || X_RETURN_STATUS , 1 ) ;
13840   END IF;
13841   IF l_debug_level  > 0 THEN
13842       oe_debug_pub.add(  ' ' , 1 ) ;
13843   END IF;
13844 
13845 EXCEPTION
13846     WHEN OTHERS THEN
13847         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13848 
13849         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13850         THEN
13851             OE_MSG_PUB.Add_Exc_Msg
13852             (   G_PKG_NAME
13853             ,   'Split_Scheduling'
13854             );
13855         END IF;
13856         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13857 END SPLIT_SCHEDULING;
13858 
13859 /*------------------------------------------------------------
13860 Procedure Schedule_Split_Lines
13861 p_param1    => p_x_line_tbl(I).schedule_status_code,
13862 p_param2    => p_x_line_tbl(I).arrival_set_id,
13863 p_param3    => p_x_line_tbl(I).ship_set_id,
13864 p_param4    => p_x_line_tbl(I).ship_model_complete_flag,
13865 p_param5    => p_x_line_tbl(I).model_remnant_flag,
13866 p_param6    => p_x_line_tbl(I).top_model_line_id,
13867 p_param7    => p_x_line_tbl(I).ato_line_id,
13868 p_param8    => p_x_line_tbl(I).item_type_code,
13869 
13870 This procedure will be called from OEXVREQB.pls.This procedure will process
13871 the scheduling for the system split records.
13872 
13873 OM will log delayed request named split_schedule from post_line_process
13874 of oe_line_util to fix the bug 2913742.
13875 
13876 From now on split scheduling will not be used for system splits. This API will
13877 take care of scheduling the system split records.
13878 
13879 
13880 -------------------------------------------------------------*/
13881 Procedure Schedule_Split_Lines
13882 ( p_sch_set_tbl     IN  OE_ORDER_PUB.request_tbl_type
13883 , x_return_status   OUT NOCOPY VARCHAR2)
13884 IS
13885 l_line_tbl           OE_ORDER_PUB.line_Tbl_Type;
13886 l_line_rec           OE_ORDER_PUB.Line_Rec_Type;
13887 BEGIN
13888 
13889   oe_debug_pub.add('Entering Schedule_Split_Lines',1);
13890 
13891   x_return_status := FND_API.G_RET_STS_SUCCESS;
13892 
13893   FOR I in 1..p_sch_set_tbl.count LOOP
13894 
13895    IF p_sch_set_tbl(I).param2 IS NOT NULL
13896    OR p_sch_set_tbl(I).param3 IS NOT NULL
13897    OR (p_sch_set_tbl(I).param4 = 'Y'
13898    AND nvl(p_sch_set_tbl(I).param5,'N') = 'N')
13899    OR (p_sch_set_tbl(I).param7 IS NOT NULL
13900    AND p_sch_set_tbl(I).param8 <> 'STANDARD'
13901    AND nvl(p_sch_set_tbl(I).param5,'N') = 'N') THEN -- 4421848: added remnant flag check
13902 
13903    oe_debug_pub.add('Belongs to Set',2);
13904    l_line_tbl(I).line_id                  := p_sch_set_tbl(I).entity_id;
13905    l_line_tbl(I).schedule_status_code     := p_sch_set_tbl(I).param1;
13906    l_line_tbl(I).arrival_set_id           := p_sch_set_tbl(I).param2;
13907    l_line_tbl(I).ship_set_id              := p_sch_set_tbl(I).param3;
13908    l_line_tbl(I).ship_model_complete_flag := p_sch_set_tbl(I).param4;
13909    l_line_tbl(I).model_remnant_flag       := p_sch_set_tbl(I).param5;
13910    l_line_tbl(I).top_model_line_id        := p_sch_set_tbl(I).param6;
13911    l_line_tbl(I).ato_line_id              := p_sch_set_tbl(I).param7;
13912    l_line_tbl(I).item_type_code           := p_sch_set_tbl(I).param8;
13913 
13914    ELSE
13915     -- Standard or independent line.
13916    oe_debug_pub.add('Independent or remnant line',2);
13917 
13918     OE_Line_Util.Query_Row(p_line_id  => p_sch_set_tbl(I).entity_id,
13919                            x_line_rec => l_line_rec);
13920 
13921     l_line_tbl(I) := l_line_rec;
13922 
13923 
13924    END IF;
13925   END LOOP;
13926 
13927   G_OVERRIDE_FLAG     := 'Y';
13928 
13929   oe_debug_pub.add(  'COUNT IS :' || l_line_tbl.COUNT , 1 ) ;
13930 
13931   process_split(p_x_line_tbl => l_line_tbl,
13932                 x_return_status => x_return_status);
13933 
13934   G_OVERRIDE_FLAG     := 'N';
13935 EXCEPTION
13936     WHEN OTHERS THEN
13937         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13938 
13939         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13940         THEN
13941             OE_MSG_PUB.Add_Exc_Msg
13942             (   G_PKG_NAME
13943             ,   'Schedule_Split_Lines'
13944             );
13945         END IF;
13946         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13947 End Schedule_Split_Lines;
13948 
13949 /*--------------------------------------------------------------------------
13950 Procedure Name : Update_Results_from_backlog_wb
13951 Description    : This procedure is called from the backlog's scheduler's
13952                  workbenck and the Supply Chain ATP form, after the user
13953                  has performed some scheduling in their form. They call
13954                  this API to update the results of scheduling on the order
13955                  lines table.
13956                  For the purpose of this call, we have created a new table type
13957                  mrp_line_tbl_type, which is table of mrp_line_rec_type.
13958                  This record is created with only those fields whose values
13959                  we can get back from MRP's form. We take the field values
13960                  from this record and update the lines information in
13961                  oe_order_lines table.
13962 -------------------------------------------------------------------------- */
13963 Procedure Update_Results_from_backlog_wb
13964 ( p_mrp_line_tbl  IN  mrp_line_tbl_type
13965 , x_msg_count OUT NOCOPY NUMBER
13966 
13967 , x_msg_data OUT NOCOPY VARCHAR2
13968 
13969 , x_return_status OUT NOCOPY VARCHAR2)
13970 
13971 IS
13972 /*
13973 l_line_rec                  OE_ORDER_PUB.line_rec_type;
13974 I                           NUMBER;
13975 l_return_status             VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
13976 l_msg_count                 NUMBER := 0;
13977 l_msg_data                  VARCHAR2(2000) := null;
13978 l_control_rec               OE_GLOBALS.control_rec_type;
13979 l_line_tbl                  OE_ORDER_PUB.line_tbl_type;
13980 l_old_line_tbl              OE_ORDER_PUB.line_tbl_type;
13981 l_file_val                  VARCHAR2(80);
13982 */
13983 --
13984 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13985 --
13986 BEGIN
13987    -- (4735196) This api is obsoleted.
13988    --Use OE_SCHEDULE_GRP.Update_Scheduling_Results instead.
13989     IF l_debug_level  > 0 THEN
13990         oe_debug_pub.add(  'This API is not supported ' , 1 ) ;
13991     END IF;
13992    /*
13993      fnd_profile.put('OE_DEBUG_LOG_DIRECTORY','/sqlcom/outbound/dom1151');
13994 
13995     IF l_debug_level  > 0 THEN
13996         oe_debug_pub.add(  'ENTERING UPDATE_RESULTS_FROM_BACKLOG_WB' , 1 ) ;
13997     END IF;
13998     IF l_debug_level  > 0 THEN
13999         oe_debug_pub.add(  'COUNT IS: ' || P_MRP_LINE_TBL.COUNT , 1 ) ;
14000     END IF;
14001     --  Set control flags.
14002 
14003     l_control_rec.controlled_operation := TRUE;
14004     l_control_rec.change_attributes    := TRUE;
14005 
14006     l_control_rec.default_attributes   := TRUE;
14007     l_control_rec.check_security       := TRUE;
14008 
14009     l_control_rec.write_to_DB          := TRUE;
14010     l_control_rec.validate_entity      := TRUE;
14011 
14012     l_control_rec.process              := TRUE;
14013 
14014     --  Instruct API to retain its caches
14015 
14016     l_control_rec.clear_api_cache      := FALSE;
14017     l_control_rec.clear_api_requests   := FALSE;
14018 
14019     FOR I in 1..p_mrp_line_tbl.count LOOP
14020 
14021   IF l_debug_level  > 0 THEN
14022       oe_debug_pub.add(  'RR: LINE ID ' || P_MRP_LINE_TBL ( I ) .LINE_ID , 1 ) ;
14023   END IF;
14024   IF l_debug_level  > 0 THEN
14025       oe_debug_pub.add(  'RR: SCHEDULE_SHIP_DATE ' || P_MRP_LINE_TBL ( I ) .SCHEDULE_SHIP_DATE , 1 ) ;
14026   END IF;
14027 
14028         l_line_rec        := OE_Order_Pub.G_MISS_LINE_REC;
14029         l_line_rec.line_id               := p_mrp_line_tbl(I).line_id;
14030         l_line_rec.schedule_ship_date    :=
14031                                   p_mrp_line_tbl(I).schedule_ship_date;
14032         l_line_rec.schedule_arrival_date :=
14033                                   p_mrp_line_tbl(I).schedule_arrival_date;
14034         l_line_rec.ship_from_org_id      :=
14035                                   p_mrp_line_tbl(I).ship_from_org_id;
14036         l_line_rec.shipping_method_code  :=
14037                                   p_mrp_line_tbl(I).ship_method_code;
14038         l_line_rec.schedule_status_code    :=
14039                                   OESCH_STATUS_SCHEDULED;
14040   -- Start Audit Trail
14041   l_line_rec.change_reason := 'SYSTEM';
14042   --  l_line_rec.change_comments := 'Scheduling Action';
14043   -- End Audit Trail
14044         l_line_rec.visible_demand_flag   := 'Y';
14045         l_line_rec.operation             := OE_GLOBALS.G_OPR_UPDATE;
14046         l_line_tbl(I)                    := l_line_rec;
14047     END LOOP;
14048 
14049 
14050     IF l_debug_level  > 0 THEN
14051         oe_debug_pub.add(  'LINE COUNT IS: ' || L_LINE_TBL.COUNT , 1 ) ;
14052     END IF;
14053     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
14054     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
14055 
14056     IF l_debug_level  > 0 THEN
14057         oe_debug_pub.add(  'BKL: CALLING PROCESS ORDER' , 1 ) ;
14058     END IF;
14059 
14060 
14061     --  Call OE_Order_PVT.Process_order
14062 
14063     OE_Order_PVT.Lines
14064     (p_validation_level    => FND_API.G_VALID_LEVEL_NONE,
14065      p_control_rec         => l_control_rec,
14066      p_x_line_tbl          => l_line_tbl,
14067      p_x_old_line_tbl      => l_old_line_tbl,
14068      x_return_status       => l_return_status);
14069 
14070     IF l_debug_level  > 0 THEN
14071         oe_debug_pub.add(  'BKL: AFTER CALLING PROCESS ORDER' , 1 ) ;
14072     END IF;
14073     IF l_debug_level  > 0 THEN
14074         oe_debug_pub.add(  'L_RETURN_STATUS IS ' || L_RETURN_STATUS , 1 ) ;
14075     END IF;
14076 
14077     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14078         IF l_debug_level  > 0 THEN
14079             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
14080         END IF;
14081         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14082     ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
14083         IF l_debug_level  > 0 THEN
14084             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
14085         END IF;
14086         RAISE FND_API.G_EXC_ERROR;
14087     END IF;
14088 
14089 -- code fix for 3502139
14090 -- call Process_Requests_And_Notify to execute the delayed requests
14091     OE_ORDER_PVT.Process_Requests_And_Notify
14092     (
14093     x_return_status => l_return_status
14094     );
14095 -- code fix for 3502139
14096 
14097 -- Added the following code to fix the bug 3105070
14098 
14099     FOR I in 1..l_line_tbl.count LOOP
14100       IF l_line_tbl(I).source_document_type_id = 10        AND
14101          NOT OE_GLOBALS.EQUAL(l_line_tbl(I).schedule_arrival_date,
14102                           l_old_line_tbl(I).schedule_arrival_date)
14103 
14104       THEN
14105         oe_debug_pub.add(  'PASSING SCHEDULE_ARRIVAL_DATE TO PO ' , 3 ) ;
14106          Update_PO(l_line_tbl(I).schedule_arrival_date,
14107                    l_line_tbl(I).source_document_id,
14108                    l_line_tbl(I).source_document_line_id);
14109         oe_debug_pub.add(  'AFTER PO CALL BACK' , 3 ) ;
14110       END IF;
14111    END LOOP ;
14112 
14113  -- End of the code added to fix the bug 3105070
14114 
14115     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14116     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14117 
14118     x_return_status := l_return_status;
14119     x_msg_count     := l_msg_count;
14120     x_msg_data      := l_msg_data;
14121 
14122     IF l_debug_level  > 0 THEN
14123         oe_debug_pub.add(  'EXITING UPDATE_RESULTS_FROM_BACKLOG_WB' , 1 ) ;
14124     END IF;
14125     OE_DEBUG_PUB.Debug_Off;
14126  */
14127    NULL;
14128    x_msg_data := 'This API is not supported';
14129    RAISE FND_API.G_EXC_ERROR;
14130 EXCEPTION
14131 
14132    WHEN FND_API.G_EXC_ERROR THEN
14133 
14134         x_return_status := FND_API.G_RET_STS_ERROR;
14135         /*
14136         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14137         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14138         */
14139     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14140 
14141         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14142         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14143         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14144 
14145     WHEN OTHERS THEN
14146 
14147         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14148 
14149         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14150         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
14151         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14152         THEN
14153             OE_MSG_PUB.Add_Exc_Msg
14154             (   G_PKG_NAME
14155             ,   'Schedule_line'
14156             );
14157         END IF;
14158 END Update_Results_from_backlog_wb;
14159 /*-----------------------------------------------------+
14160  | Name        :   Post_Forms_Commit                   |
14161  | Parameters  :                                       |
14162  |                                                     |
14163  | Description :   This Procedure is called from       |
14164  |                 OEOXOEFRM.pld POST_FORMS_COMMIT     |
14165  |                 This Procedure was added for        |
14166  |                 Bug: 2097933.                       |
14167  |                 With this procedure we check if     |
14168  |                 there is sufficient Qty for         |
14169  |                 Reservation just before we are      |
14170  |                 Committing the line.                |
14171  |         If there is no sufficient Qty for   |
14172  |         reservation then the Inventory      |
14173  |         populates a pl-sql table. Before    |
14174  |                 commit we check if the pl-sql table |
14175  |         is NOT Null or not.             |
14176  +-----------------------------------------------------*/
14177 
14178 Procedure Post_Forms_Commit
14179 (x_return_status OUT NOCOPY VARCHAR2
14180 
14181 ,x_msg_count OUT NOCOPY NUMBER
14182 
14183 ,x_msg_data OUT NOCOPY VARCHAR2) IS
14184 
14185 l_return_status VARCHAR2(100);
14186 l_msg_count     NUMBER;
14187 l_msg_data      VARCHAR2(500);
14188 l_failed_rsv_temp_tbl INV_RESERVATION_GLOBAL.mtl_failed_rsv_tbl_type;
14189 --
14190 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14191 --
14192 Begin
14193 
14194   IF l_debug_level  > 0 THEN
14195       oe_debug_pub.add(  '*** INSIDE THE POST_FORMS_COMMIT ***' , 1 ) ;
14196   END IF;
14197 
14198   x_return_status := FND_API.G_RET_STS_SUCCESS;
14199 
14200   -- Check for Performed Reservation Start
14201   IF OESCH_PERFORMED_RESERVATION = 'Y' THEN
14202 
14203     IF l_debug_level  > 0 THEN
14204         oe_debug_pub.add(  ' BEFORE CALLING THE INV FOR DO_CHECK_FOR_COMMIT' , 1 ) ;
14205     END IF;
14206     INV_RESERVATION_PVT.Do_Check_For_Commit
14207         (p_api_version_number  => 1.0
14208         ,p_init_msg_lst        => FND_API.G_FALSE
14209         ,x_return_status       => l_return_status
14210         ,x_msg_count           => l_msg_count
14211         ,x_msg_data            => l_msg_data
14212         ,x_failed_rsv_temp_tbl => l_failed_rsv_temp_tbl);
14213 
14214                                            IF l_debug_level  > 0 THEN
14215                                                oe_debug_pub.add(  'AFTER CALLING THE INV FOR DO_CHECK_FOR_COMMIT : ' || L_RETURN_STATUS , 1 ) ;
14216                                            END IF;
14217 
14218   -- We need to find out if the Reservation has failed
14219     IF l_failed_rsv_temp_tbl.count > 0 THEN
14220       IF l_debug_level  > 0 THEN
14221           oe_debug_pub.add(  ' THE RESERVATION PROCESS HAS FAILED ' , 1 ) ;
14222       END IF;
14223       FND_MESSAGE.SET_NAME('ONT','OE_SCH_RSV_FAILURE');
14224       OE_MSG_PUB.Add;
14225     END IF;
14226 
14227     -- Error Handling Start
14228     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14229       IF l_debug_level  > 0 THEN
14230           oe_debug_pub.add(  'INSIDE UNEXPECTED ERROR' , 1 ) ;
14231       END IF;
14232       OE_MSG_PUB.Transfer_Msg_Stack;
14233       l_msg_count   := OE_MSG_PUB.COUNT_MSG;
14234 
14235       FOR I IN 1..l_msg_count LOOP
14236         l_msg_data :=  OE_MSG_PUB.Get(I,'F');
14237 
14238         IF l_debug_level  > 0 THEN
14239             oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14240         END IF;
14241       END LOOP;
14242       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14243 
14244     ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
14245       IF l_debug_level  > 0 THEN
14246           oe_debug_pub.add(  ' INSIDE EXPECTED ERROR' , 1 ) ;
14247       END IF;
14248       OE_MSG_PUB.Transfer_Msg_Stack;
14249       l_msg_count   := OE_MSG_PUB.COUNT_MSG;
14250 
14251       FOR I IN 1..l_msg_count LOOP
14252         l_msg_data :=  OE_MSG_PUB.Get(I,'F');
14253         IF l_debug_level  > 0 THEN
14254             oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14255         END IF;
14256       END LOOP;
14257       RAISE FND_API.G_EXC_ERROR;
14258 
14259     END IF;
14260   --Error Handling End
14261 
14262     OESCH_PERFORMED_RESERVATION := 'N';
14263 
14264   -- Check for Performed Reservation End
14265   END IF;
14266 
14267     --  Get message count and data
14268 
14269     oe_msg_pub.count_and_get
14270     (   p_count                       => x_msg_count
14271     ,   p_data                        => x_msg_data
14272     );
14273 
14274 
14275   IF l_debug_level  > 0 THEN
14276       oe_debug_pub.add(  '*** BEFORE EXITING POST_FORMS_COMMIT ***' , 1 ) ;
14277   END IF;
14278 
14279 EXCEPTION
14280   WHEN FND_API.G_EXC_ERROR THEN
14281  --   OESCH_PERFORMED_RESERVATION := 'N';
14282 
14283     x_return_status := FND_API.G_RET_STS_ERROR;
14284 
14285     --  Get message count and data
14286 
14287     oe_msg_pub.count_and_get
14288      (   p_count                       => x_msg_count
14289      ,   p_data                        => x_msg_data);
14290 
14291   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14292 --    OESCH_PERFORMED_RESERVATION := 'N';
14293     IF OE_MSG_PUB.Check_Msg_Level
14294         (OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14295     THEN
14296       OE_MSG_PUB.Add_Exc_Msg
14297         (G_PKG_NAME , 'Post_Forms_Commit');
14298     END IF;
14299 
14300     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
14301 
14302     --  Get message count and data
14303 
14304     oe_msg_pub.count_and_get
14305     (   p_count                       => x_msg_count
14306     ,   p_data                        => x_msg_data);
14307 
14308 
14309 END Post_Forms_Commit;
14310 
14311 -- added by fabdi 03/May/2001 - For process ATP
14312 /*--------------------------------------------------------------------------
14313 Procedure Name : get_process_query_quantities
14314 Description : This precedure works out nocopy the on_hand_qty and avail_to_reserve
14315 
14316 quanties to display in the same ATP window for process inventory only. The procedure
14317 takes into account grade controlled items and displays inventory result for a particular
14318 grade as well a total sum if grade is null.
14319 This procedure is called from Query_Qty_Tree only
14320 --------------------------------------------------------------------------
14321 -- INVCONV  - not used now     cos of OPM inventory convergence
14322 PROCEDURE get_process_query_quantities
14323   (   p_org_id       IN  NUMBER
14324    ,  p_item_id      IN  NUMBER
14325    ,  p_line_id      IN  NUMBER
14326 , x_on_hand_qty OUT NOCOPY NUMBER
14327 
14328 , x_avail_to_reserve OUT NOCOPY NUMBER
14329 
14330   ) IS
14331 
14332  l_commitedsales_qty     NUMBER;
14333  l_commitedprod_qty     NUMBER;
14334  l_onhand_order_qty     NUMBER;
14335  l_grade_ctl    NUMBER;
14336  l_grade    VARCHAR2(4);
14337 
14338  -- main cursor (with total sum)
14339  CURSOR c_onhand_qty(p_organisation_id number, p_itemid number) IS
14340  SELECT sum(s.onhand_order_qty), sum(s.COMMITTEDSALES_QTY), sum(s.COMMITTEDPROD_QTY)
14341  FROM ic_summ_inv s
14342  WHERE s.item_id = p_itemid AND
14343  s.whse_code = (Select wh.whse_code
14344                 from ic_whse_mst wh
14345                 where wh.MTL_ORGANIZATION_ID = p_organisation_id);
14346 
14347  -- cursor (with single grade sum)
14348  CURSOR c_onhand_qty2(p_organisation_id number, p_itemid number) IS
14349  SELECT s.onhand_order_qty, s.COMMITTEDSALES_QTY, s.COMMITTEDPROD_QTY
14350  FROM ic_summ_inv s
14351  WHERE s.item_id = p_itemid AND
14352  s.whse_code = (Select wh.whse_code
14353                 from ic_whse_mst wh
14354                 where wh.MTL_ORGANIZATION_ID = p_organisation_id) AND
14355  s.qc_grade =  (SELECT preferred_grade
14356                FROM oe_order_lines
14357                WHERE line_id = p_line_id);
14358 
14359 
14360  -- Grade ctl cursor
14361  CURSOR c_grade_ctl(p_item_id number) IS
14362  SELECT grade_ctl
14363  FROM ic_item_mst
14364  WHERE item_id = p_item_id;
14365 
14366  Cursor c_get_grade (p_line_id number) IS
14367  SELECT preferred_grade
14368  FROM oe_order_lines
14369  WHERE line_id = p_line_id;
14370 
14371   --
14372   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14373   --
14374 BEGIN
14375 
14376     IF l_debug_level  > 0 THEN
14377         oe_debug_pub.add(  'INSIDE GET_PROCESS_QUERY_QUANTITIES ' ) ;
14378     END IF;
14379     IF l_debug_level  > 0 THEN
14380         oe_debug_pub.add(  'P_LINE_ID - IN GET_PROCESS_QUERY_QUANTITIES IS: '|| P_LINE_ID ) ;
14381     END IF;
14382 
14383     OPEN c_grade_ctl(p_item_id);
14384     OPEN c_get_grade(p_line_id);
14385     FETCH c_grade_ctl into l_grade_ctl;
14386     FETCH c_get_grade into l_grade;
14387     IF l_debug_level  > 0 THEN
14388         oe_debug_pub.add(  'GRADE OF ITEM IS '|| L_GRADE ) ;
14389     END IF;
14390 
14391      IF (l_grade_ctl > 0 AND l_grade is NOT NULL) THEN
14392         -- Grade ctl item
14393         OPEN c_onhand_qty2(p_org_id, p_item_id);
14394         FETCH c_onhand_qty2 into l_onhand_order_qty, l_commitedsales_qty, l_commitedprod_qty;
14395         if c_onhand_qty2%NOTFOUND then
14396                 CLOSE c_onhand_qty2;
14397                 IF l_debug_level  > 0 THEN
14398                     oe_debug_pub.add(  ' ( 2 ) NO DATA FOUND FOR ITEM '|| P_ITEM_ID || ' ORG_ID '|| P_ORG_ID ) ;
14399                 END IF;
14400                 x_on_hand_qty := 0;
14401                 x_avail_to_reserve := 0;
14402                 IF l_debug_level  > 0 THEN
14403                     oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
14404                 END IF;
14405                 IF l_debug_level  > 0 THEN
14406                     oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
14407                 END IF;
14408                 return;
14409         end if;
14410         CLOSE c_onhand_qty2;
14411 
14412      ELSE
14413         -- non grade ctl item
14414         OPEN c_onhand_qty(p_org_id, p_item_id);
14415         FETCH c_onhand_qty into l_onhand_order_qty, l_commitedsales_qty, l_commitedprod_qty;
14416         if c_onhand_qty%NOTFOUND then
14417                 CLOSE c_onhand_qty;
14418                 IF l_debug_level  > 0 THEN
14419                     oe_debug_pub.add(  ' ( 1 ) NO DATA FOUND FOR ITEM '|| P_ITEM_ID || ' ORG_ID '|| P_ORG_ID ) ;
14420                 END IF;
14421                 x_on_hand_qty := 0;
14422                 x_avail_to_reserve := 0;
14423                 IF l_debug_level  > 0 THEN
14424                     oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
14425                 END IF;
14426                 IF l_debug_level  > 0 THEN
14427                     oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
14428                 END IF;
14429                 return;
14430         end if;
14431         CLOSE c_onhand_qty;
14432      END IF;
14433    CLOSE c_grade_ctl;
14434    CLOSE c_get_grade;
14435 
14436    -- Quantity Calculations
14437    x_on_hand_qty := l_onhand_order_qty;
14438    x_avail_to_reserve :=  l_onhand_order_qty - (l_commitedsales_qty + l_commitedprod_qty);
14439 
14440    IF l_debug_level  > 0 THEN
14441        oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
14442    END IF;
14443    IF l_debug_level  > 0 THEN
14444        oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
14445    END IF;
14446 END get_process_query_quantities; */
14447 -- end fabdi
14448 -- Start  2595661
14449 /*
14450    FUNCTION Name - GET_PICK_STATUS
14451    Description - To get the pick status of a particular line
14452                 This will return true if a single or more line with released_status in ('S','Y','C')
14453 */
14454 FUNCTION Get_Pick_Status (p_line_id IN NUMBER) RETURN BOOLEAN
14455 IS
14456   l_Pick_Status   VARCHAR2(1);
14457 --
14458 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14459 --
14460 BEGIN
14461    SELECT  '1'
14462    INTO  l_Pick_status
14463    FROM  WSH_DELIVERY_DETAILS
14464    WHERE  SOURCE_CODE = 'OE'
14465    AND  SOURCE_LINE_ID = p_line_id
14466    AND  RELEASED_STATUS IN ('S','Y','C');
14467 
14468    RETURN (TRUE);
14469 
14470 EXCEPTION
14471    WHEN NO_DATA_FOUND THEN
14472        RETURN (FALSE);
14473    WHEN TOO_MANY_ROWS THEN
14474        RETURN (TRUE);
14475    WHEN OTHERS THEN
14476       IF l_debug_level  > 0 THEN
14477           oe_debug_pub.ADD('Error message in Get_Pick_Status : '||substr(sqlerrm,1,100),1);
14478 
14479       END IF;
14480       RETURN (FALSE);
14481 END Get_Pick_Status;
14482 
14483 PROCEDURE Do_Unreserve (p_line_rec             IN  OE_ORDER_PUB.Line_Rec_Type
14484                        ,p_quantity_to_unreserve IN  NUMBER
14485                        ,p_quantity2_to_unreserve IN  NUMBER -- INVCONV
14486                        ,p_old_ship_from_org_id   IN  NUMBER DEFAULT NULL -- 5024936
14487                        ,x_return_status         OUT NOCOPY VARCHAR2)
14488 IS
14489   l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
14490   l_rsv_new_rec           inv_reservation_global.mtl_reservation_rec_type;
14491   l_msg_count             NUMBER;
14492   l_msg_data              VARCHAR2(240);
14493   l_rsv_id                NUMBER;
14494   l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
14495   l_count                 NUMBER;
14496   l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
14497   l_qty_to_unreserve      NUMBER;
14498   l_source_code           VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
14499   l_sales_order_id        NUMBER;
14500   l_x_error_code          NUMBER;
14501   l_lock_records          VARCHAR2(1);
14502   l_sort_by_req_date      NUMBER ;
14503   l_buffer                VARCHAR2(2000);
14504 
14505 --
14506 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14507 --
14508 BEGIN
14509 
14510   IF l_debug_level  > 0 THEN
14511       oe_debug_pub.add(  'ENTERING DO_UNRESERVE' , 3 ) ;
14512   END IF;
14513   IF l_debug_level  > 0 THEN
14514       oe_debug_pub.add(  'QUANTITY TO UNRESERVE :' || P_QUANTITY_TO_UNRESERVE , 3 ) ;
14515   END IF;
14516 
14517   -- If the quantity to unreserve is passed and null or missing, we do not
14518   -- need to go throug this procedure.
14519 
14520   -- 2746991
14521   --IF p_quantity_to_unreserve is null OR
14522   IF NVL(p_quantity_to_unreserve,0) = 0 OR
14523      p_quantity_to_unreserve = FND_API.G_MISS_NUM THEN
14524      goto end_of_loop;
14525   END IF;
14526 
14527   IF p_line_rec.source_document_type_id = 10 THEN
14528 
14529      -- This is an internal order line. We need to give
14530      -- a different demand source type for these lines.
14531 
14532      l_rsv_rec.demand_source_type_id        :=
14533           INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD;
14534           -- intenal order
14535   ELSE
14536      l_rsv_rec.demand_source_type_id        :=
14537           INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE;
14538           -- order entry
14539   END IF;
14540 
14541 
14542   -- Get demand_source_header_id from mtl_sales_orders
14543 
14544   l_sales_order_id := Get_mtl_sales_order_id(p_line_rec.HEADER_ID);
14545 
14546   IF l_debug_level  > 0 THEN
14547       oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 3 ) ;
14548   END IF;
14549 
14550   l_rsv_rec.demand_source_header_id  := l_sales_order_id;
14551   l_rsv_rec.demand_source_line_id    := p_line_rec.line_id;
14552   -- 02-jun-2000 mpetrosi added org_id to query_reservation start
14553   --5024936
14554   IF p_old_ship_from_org_id IS NOT NULL THEN
14555      l_rsv_rec.organization_id := p_old_ship_from_org_id;
14556   ELSE
14557      l_rsv_rec.organization_id := p_line_rec.ship_from_org_id;
14558   END IF;
14559   IF l_debug_level  > 0 THEN
14560       oe_debug_pub.add(  'SHIP FROM ORG ' || l_rsv_rec.organization_id , 3 ) ;
14561   END IF;
14562 
14563   inv_reservation_pub.query_reservation
14564   ( p_api_version_number        => 1.0
14565   , p_init_msg_lst              => fnd_api.g_true
14566   , p_query_input               => l_rsv_rec
14567   , p_cancel_order_mode         => INV_RESERVATION_GLOBAL.G_CANCEL_ORDER_YES
14568   , p_lock_records              => l_lock_records
14569   , p_sort_by_req_date          => l_sort_by_req_date
14570   , x_mtl_reservation_tbl       => l_rsv_tbl
14571   , x_mtl_reservation_tbl_count => l_count
14572   , x_error_code                => l_x_error_code
14573   , x_return_status             => x_return_status
14574   , x_msg_count                 => l_msg_count
14575   , x_msg_data                  => l_msg_data);
14576 
14577   IF l_debug_level  > 0 THEN
14578       oe_debug_pub.add(  '3. AFTER QUERY RESERVATION'||X_RETURN_STATUS , 1 ) ;
14579   END IF;
14580   IF l_debug_level  > 0 THEN
14581       oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14582   END IF;
14583   -- Start 2346233
14584   -- l_qty_to_unreserve      := p_quantity_to_unreserve;
14585   IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,l_rsv_tbl(1).primary_uom_code ) THEN
14586      l_qty_to_unreserve :=  INV_CONVERT.INV_UM_CONVERT( item_id       => p_line_rec.inventory_item_id,
14587                                                         precision     => 5,
14588                                                         from_quantity =>p_quantity_to_unreserve,
14589                                                         from_unit     =>p_line_rec.order_quantity_uom,
14590                                                         to_unit       =>l_rsv_tbl(1).primary_uom_code,
14591                                                         from_name     =>NULL,
14592                                                         to_name       =>NULL
14593                                                       );
14594   ELSE
14595      -- Bug 6335352
14596      /* Added below condition specifically for SET processing and when UOM is changed to Primary UOM
14597         Sets are processed using Delayed Requests. By the time delayed request executes, the ordered quantity in line
14598         gets updated, but reservation is still for the original UOM (non-primary). So unreservation in this case
14599         will be done using primary reservation quantity
14600      */
14601      IF l_rsv_tbl(1).primary_reservation_quantity > p_quantity_to_unreserve
14602         AND NOT oe_globals.equal(p_line_rec.order_quantity_uom,l_rsv_tbl(1).reservation_uom_code)
14603      THEN
14604         l_qty_to_unreserve      := l_rsv_tbl(1).primary_reservation_quantity ;
14605      ELSE
14606         l_qty_to_unreserve      := p_quantity_to_unreserve;
14607      END IF;
14608   END IF;
14609   IF l_debug_level  > 0 THEN
14610      oe_debug_pub.add(  'QUANTITY TO UNRESERVE :' || l_QTY_TO_UNRESERVE , 3 ) ;
14611   END IF;
14612   -- End 2346233
14613 
14614 
14615   FOR I IN 1..l_rsv_tbl.COUNT LOOP
14616 
14617     l_rsv_rec := l_rsv_tbl(I);
14618     IF l_debug_level  > 0 THEN
14619         oe_debug_pub.add(  'RESERVED QTY : ' || L_RSV_REC.RESERVATION_QUANTITY , 1 ) ;
14620     END IF;
14621     IF l_debug_level  > 0 THEN
14622         oe_debug_pub.add(  'QTY TO UNRESERVE: ' || L_QTY_TO_UNRESERVE , 1 ) ;
14623     END IF;
14624 
14625     --Start 2346233
14626     /*
14627     IF (l_rsv_rec.reservation_quantity <= l_qty_to_unreserve)
14628     THEN
14629     */
14630     IF (l_rsv_rec.primary_reservation_quantity <= l_qty_to_unreserve) THEN
14631     -- End 2346233
14632 
14633       IF l_debug_level  > 0 THEN
14634           oe_debug_pub.add(  'CALLING INVS DELETE_RESERVATION' , 3 ) ;
14635       END IF;
14636       inv_reservation_pub.delete_reservation
14637       ( p_api_version_number      => 1.0
14638       , p_init_msg_lst            => fnd_api.g_true
14639       , x_return_status           => x_return_status
14640       , x_msg_count               => l_msg_count
14641       , x_msg_data                => l_msg_data
14642       , p_rsv_rec                 => l_rsv_rec
14643       , p_serial_number           => l_dummy_sn
14644       );
14645 
14646       IF l_debug_level  > 0 THEN
14647          oe_debug_pub.add(  'AFTER CALLING INVS DELETE_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
14648       END IF;
14649 
14650       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14651            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14652       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
14653            oe_msg_pub.transfer_msg_stack;
14654            l_msg_count:=OE_MSG_PUB.COUNT_MSG;
14655            FOR I in 1..l_msg_count LOOP
14656                l_msg_data := OE_MSG_PUB.Get(I,'F');
14657                IF l_debug_level  > 0 THEN
14658                    oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14659                END IF;
14660            END LOOP;
14661 
14662            RAISE FND_API.G_EXC_ERROR;
14663       END IF;
14664 
14665       --Start 2346233
14666       /*
14667       l_qty_to_unreserve := l_qty_to_unreserve -
14668                             l_rsv_rec.reservation_quantity;
14669       */
14670       l_qty_to_unreserve := l_qty_to_unreserve -
14671                             l_rsv_rec.primary_reservation_quantity;
14672       -- End 2346233
14673 
14674 
14675       IF (l_qty_to_unreserve <= 0) THEN
14676             goto end_of_loop;
14677       END IF;
14678 
14679     ELSE -- res rec qty > l_qty_to_unreserve
14680       l_rsv_new_rec                              := l_rsv_rec;
14681       -- Start 2346233
14682       /*
14683       l_rsv_new_rec.reservation_quantity         :=
14684 
14685             l_rsv_rec.reservation_quantity - l_qty_to_unreserve ;
14686       l_rsv_new_rec.primary_reservation_quantity := fnd_api.g_miss_num;
14687       */
14688       l_rsv_new_rec.primary_reservation_quantity :=
14689             l_rsv_rec.primary_reservation_quantity - l_qty_to_unreserve ;
14690       l_rsv_new_rec.reservation_quantity := fnd_api.g_miss_num;
14691       -- End 2346233
14692 
14693       IF l_debug_level  > 0 THEN
14694           oe_debug_pub.add(  'OLD QTY : ' || L_RSV_REC.RESERVATION_QUANTITY , 3 ) ;
14695       END IF;
14696       IF l_debug_level  > 0 THEN
14697           oe_debug_pub.add(  'NEW QTY : ' || L_RSV_NEW_REC.RESERVATION_QUANTITY , 3 ) ;
14698       END IF;
14699 
14700      -- INVCONV
14701       /* OPM 14/SEP/00 send process attributes into the reservation
14702       =============================================================*
14703       IF INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_line_rec.ship_from_org_id)    -- OPM 2645605
14704         then */
14705 
14706   --       l_rsv_new_rec.attribute1     := p_line_rec.preferred_grade;
14707 
14708         l_rsv_new_rec.secondary_reservation_quantity   := p_line_rec.ordered_quantity2; -- INVCONV
14709         l_rsv_new_rec.secondary_uom_code               := p_line_rec.ordered_quantity_uom2;
14710  --       l_rsv_new_rec.attribute2     := p_line_rec.ordered_quantity2;
14711   --      l_rsv_new_rec.attribute3     := p_line_rec.ordered_quantity_uom2;
14712 
14713      --  END IF; INVCONV
14714 
14715       /* OPM 14/SEP/00 END
14716       ====================*/
14717 
14718 
14719       IF l_debug_level  > 0 THEN
14720           oe_debug_pub.add(  'CALLING INVS UPDATE_RESERVATION: ' , 3 ) ;
14721       END IF;
14722       inv_reservation_pub.update_reservation
14723       ( p_api_version_number        => 1.0
14724       , p_init_msg_lst              => fnd_api.g_true
14725       , p_original_rsv_rec          => l_rsv_rec
14726       , p_to_rsv_rec                => l_rsv_new_rec
14727       , p_original_serial_number    => l_dummy_sn -- no serial contorl
14728       , p_to_serial_number          => l_dummy_sn -- no serial control
14729       , p_validation_flag           => fnd_api.g_true
14730       , x_return_status             => x_return_status
14731       , x_msg_count                 => l_msg_count
14732       , x_msg_data                  => l_msg_data);
14733 
14734       IF l_debug_level  > 0 THEN
14735           oe_debug_pub.add(  'AFTER INVS UPDATE_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
14736       END IF;
14737 
14738       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14739          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14740       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
14741         oe_msg_pub.transfer_msg_stack;
14742         l_msg_count:=OE_MSG_PUB.COUNT_MSG;
14743         for I in 1..l_msg_count loop
14744             l_msg_data := OE_MSG_PUB.Get(I,'F');
14745             IF l_debug_level  > 0 THEN
14746                 oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14747             END IF;
14748         end loop;
14749         RAISE FND_API.G_EXC_ERROR;
14750       END IF;
14751 
14752       l_qty_to_unreserve := 0;
14753       goto end_of_loop;
14754 
14755     END IF;
14756   END LOOP;
14757 
14758   <<end_of_loop>>
14759   IF l_debug_level  > 0 THEN
14760       oe_debug_pub.add(  'EXITING DO_UNRESERVE' , 3 ) ;
14761   END IF;
14762 
14763 EXCEPTION
14764   WHEN FND_API.G_EXC_ERROR THEN
14765     x_return_status := FND_API.G_RET_STS_ERROR;
14766 
14767   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14768     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14769 
14770   WHEN OTHERS THEN
14771     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14772 
14773     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14774     THEN
14775       OE_MSG_PUB.Add_Exc_Msg
14776       (   G_PKG_NAME
14777       ,   'Do_Unreserve'
14778        );
14779     END IF;
14780 
14781 END Do_Unreserve;
14782 -- End  2595661
14783 -- Pack J
14784 /*--------------------------------------------------------------------------
14785 Procedure Name : Promise_Date_for_Sch_Action
14786 Description    : This API checks Promise date setup flag and based of Order
14787                  date type code and scheduling action sets the promise date
14788                  with schedule ship date/schedule arrival date.
14789 -------------------------------------------------------------------------- */
14790 PROCEDURE Promise_Date_for_Sch_Action
14791 (p_x_line_rec IN OUT NOCOPY OE_ORDER_PUB.Line_Rec_Type,
14792  p_sch_action IN VARCHAR2,
14793  P_header_id  IN NUMBER DEFAULT NULL)
14794 IS
14795   CURSOR order_header_id IS
14796   SELECT header_id
14797   FROM oe_order_lines_all
14798   WHERE line_id = p_x_line_rec.line_id;
14799 
14800   l_promise_date_flag  VARCHAR2(2);
14801   l_order_date_type_code VARCHAR2(15);
14802   l_header_id  NUMBER;
14803 --
14804 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14805 --
14806 
14807 BEGIN
14808    IF l_debug_level  > 0 THEN
14809       oe_debug_pub.add(  'ENTERING PROMISE_DATE_FOR_SCH_ACTION ' , 3 ) ;
14810    END IF;
14811    IF p_header_id IS NULL THEN
14812       -- Heade id not provided.
14813       OPEN order_header_id;
14814       FETCH order_header_id INTO l_header_id;
14815       CLOSE order_header_id;
14816    ELSE
14817       l_header_id := p_header_id;
14818    END IF;
14819    l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_header_id),'SHIP');
14820    l_promise_date_flag := Oe_Sys_Parameters.Value('PROMISE_DATE_FLAG');
14821    IF l_debug_level  > 0 THEN
14822       oe_debug_pub.add(  'PROMISE DATE FLAG: '||l_promise_date_flag , 3 ) ;
14823       oe_debug_pub.add(  'STATUS CODE : '||p_x_line_rec.schedule_status_code , 3 ) ;
14824    END IF;
14825    IF (l_promise_date_flag ='FS'
14826       AND ((p_sch_action = OESCH_ACT_DEMAND
14827        OR p_sch_action = OESCH_ACT_SCHEDULE)
14828       OR (p_sch_action = OESCH_ACT_RESCHEDULE
14829        AND p_x_line_rec.schedule_status_code IS NULL))) -- 4496187
14830      OR (l_promise_date_flag ='S'
14831      AND (p_sch_action = OESCH_ACT_DEMAND
14832       OR p_sch_action = OESCH_ACT_SCHEDULE
14833       OR p_sch_action = OESCH_ACT_REDEMAND
14834       OR p_sch_action = OESCH_ACT_RESCHEDULE))THEN
14835 
14836        IF l_order_date_type_code = 'SHIP' THEN
14837           p_x_line_rec.promise_date := p_x_line_rec.schedule_ship_date;
14838        ELSE
14839           p_x_line_rec.promise_date := p_x_line_rec.schedule_arrival_date;
14840        END IF;
14841    ELSIF l_promise_date_flag ='S'
14842      AND (p_sch_action = OESCH_ACT_UNSCHEDULE
14843       OR p_sch_action = OESCH_ACT_UNDEMAND) THEN --3345776
14844       -- Promise date setup is schedule ship date/ arrival date
14845       -- Clearing the promise date for unscheduling
14846       p_x_line_rec.promise_date := Null;
14847    END IF;
14848    IF l_debug_level  > 0 THEN
14849       oe_debug_pub.add(  'EXITING PROMISE_DATE_FOR_SCH_ACTION ' , 3 ) ;
14850    END IF;
14851 END Promise_Date_for_Sch_Action;
14852 
14853 PROCEDURE Global_atp(p_line_id IN NUMBER)
14854 IS
14855      l_header_id               NUMBER;
14856      l_order_number            NUMBER;
14857      l_line_id                 NUMBER;
14858      l_inventory_item_id       NUMBER;
14859      l_sold_to_org_id          NUMBER;
14860      l_ship_to_org_id          NUMBER;
14861      l_ship_from_org_id        NUMBER;
14862      l_quantity_ordered        NUMBER;
14863      l_uom_code                VARCHAR2(3);
14864      l_requested_ship_date     DATE  := null;
14865      l_requested_arrival_date  DATE  := null;
14866      l_delivery_lead_time      NUMBER;
14867      l_latest_acceptable_date  DATE  := null;
14868      l_freight_carrier         VARCHAR2(30) := null;
14869      l_ship_method             VARCHAR2(30) := null;
14870      l_demand_class            VARCHAR2(30) := null;
14871      l_ship_set_name           VARCHAR2(30) := null;
14872      l_arrival_set_name        VARCHAR2(30) := null;
14873      l_order_date_type_code    VARCHAR2(30) := null;
14874      l_session_id              NUMBER;
14875      l_scenario_id             NUMBER := -1;
14876      l_order_header_id         NUMBER;
14877      l_order_line_id           NUMBER;
14878      l_valid                   BOOLEAN := TRUE;
14879      l_instance_id             NUMBER;
14880      result                    BOOLEAN;
14881      l_project_id              NUMBER;
14882      l_task_id                 NUMBER;
14883      l_project_number          NUMBER;
14884      l_task_number             NUMBER;
14885      l_ship_method_text        VARCHAR2(80);
14886      l_line_number             NUMBER;
14887      l_shipment_number         NUMBER;
14888      l_option_number           NUMBER;
14889      l_promise_date            DATE;
14890      l_request_date            DATE;
14891      l_customer_name           VARCHAR2(50) := null;
14892      l_customer_location       VARCHAR2(40) := null;
14893      l_ship_set_id             NUMBER := null;
14894      l_ship_set_id_1           NUMBER := null;
14895      l_arrival_set_id          NUMBER := null;
14896      l_ato_line_id             NUMBER := null;
14897      l_top_model_line_id       NUMBER := null;
14898      l_smc_flag                VARCHAR2(1) := null;
14899      l_ordered_item            VARCHAR2(2000);
14900      l_return_status           VARCHAR2(1);
14901      l_insert_code             NUMBER;
14902      l_insert_flag             VARCHAR2(240);
14903      p_arrival_set_id          NUMBER;
14904      p_ship_set_id             NUMBER;
14905      p_top_model_line_id       NUMBER;
14906      p_ato_line_id             NUMBER;
14907      l_st_ato_line_id          NUMBER;
14908      l_atp_lead_time           NUMBER := 0;
14909      l_st_atp_lead_time        NUMBER := 0;
14910      l_item_type_code          VARCHAR2(30);
14911      l_ato_model_line_id       NUMBER;
14912      l_conc_line_number        VARCHAR2(30);
14913      l_config_line_id          NUMBER;
14914      l_component_code          VARCHAR2(1000);
14915      l_component_sequence_id   NUMBER;
14916      l_link_to_line_id         NUMBER;
14917 
14918      CURSOR set_lines(p_header_id         IN NUMBER,
14919                       p_line_id           IN NUMBER,
14920                       p_arrival_set_id    IN NUMBER,
14921                       p_ship_set_id       IN NUMBER,
14922                       p_top_model_line_id IN NUMBER,
14923                       p_ato_line_id       IN NUMBER)
14924      IS
14925      SELECT line_id,
14926             header_id,
14927             inventory_item_id,
14928             ordered_item,
14929             sold_to_org_id,
14930             ship_to_org_id,
14931             ship_from_org_id,
14932             demand_class_code,
14933             ordered_quantity,
14934             order_quantity_uom,
14935             latest_acceptable_date,
14936             line_number,
14937             shipment_number,
14938             option_number,
14939             delivery_lead_time,
14940             request_date,
14941             promise_date,
14942             project_id,
14943             task_id,
14944             shipping_method_code,
14945             ship_set_id,
14946             arrival_set_id,
14947             link_to_line_id,
14948             ato_line_id,
14949             item_type_code,
14950             top_model_line_id,
14951             component_sequence_id,
14952             component_code
14953      FROM OE_ORDER_LINES_ALL
14954      where header_id = p_header_id AND
14955           (arrival_set_id = p_arrival_set_id OR
14956            ship_set_id = p_ship_set_id OR
14957            top_model_line_id = p_top_model_line_id OR
14958            ato_line_id = p_ato_line_id) AND
14959            line_id <> p_line_id AND
14960            item_type_code <> 'CONFIG';
14961 
14962 BEGIN
14963 
14964            BEGIN
14965               SELECT header_id,
14966                      inventory_item_id,
14967                      ordered_item,
14968                      sold_to_org_id,
14969                      ship_to_org_id,
14970                      ship_from_org_id,
14971                      demand_class_code,
14972                      ordered_quantity,
14973                      order_quantity_uom,
14974                      latest_acceptable_date,
14975                      line_number,
14976                      shipment_number,
14977                      option_number,
14978                      ship_model_complete_flag,
14979                      top_model_line_id,
14980                      ato_line_id,
14981                      delivery_lead_time,
14982                      request_date,
14983                      promise_date,
14984                      project_id,
14985                      task_id,
14986                      shipping_method_code,
14987                      ship_set_id,
14988                      arrival_set_id,
14989                      item_type_code,
14990                      link_to_line_id,
14991                      component_code,
14992                      component_sequence_id
14993               INTO l_header_id,
14994                    l_inventory_item_id,
14995                    l_ordered_item,
14996                    l_sold_to_org_id,
14997                    l_ship_to_org_id,
14998                    l_ship_from_org_id,
14999                    l_demand_class,
15000                    l_quantity_ordered,
15001                    l_uom_code,
15002                    l_latest_acceptable_date,
15003                    l_line_number,
15004                    l_shipment_number,
15005                    l_option_number,
15006                    l_smc_flag,
15007                    l_top_model_line_id,
15008                    l_ato_line_id,
15009                    l_delivery_lead_time,
15010                    l_request_date,
15011                    l_promise_date,
15012                    l_project_id,
15013                    l_task_id,
15014                    l_ship_method,
15015                    l_ship_set_id,
15016                    l_arrival_set_id,
15017                    l_item_type_code,
15018                    l_link_to_line_id,
15019                    l_component_code,
15020                    l_component_sequence_id
15021               FROM oe_order_lines_all
15022               WHERE line_id = p_line_id;
15023 
15024               l_line_id := p_line_id;
15025 
15026               SELECT ORDER_NUMBER ,ORDER_DATE_TYPE_CODE
15027               INTO   l_order_number,l_order_date_type_code
15028               FROM   oe_order_headers
15029               WHERE  header_id=l_header_id;
15030 
15031               IF l_order_date_type_code = 'ARRIVAL' THEN
15032                  l_requested_arrival_date := l_request_date;
15033                  IF l_requested_arrival_date is null THEN
15034                     l_requested_arrival_date := SYSDATE;
15035                  END IF;
15036                  l_requested_ship_date := null;
15037               ELSE
15038                  l_requested_ship_date := l_request_date;
15039                  IF l_requested_ship_date is null THEN
15040                     l_requested_ship_date := SYSDATE;
15041                  END IF;
15042                  l_requested_arrival_date := null;
15043               END IF;
15044 
15045 
15046            EXCEPTION
15047                WHEN OTHERS THEN
15048                     l_valid := FALSE;
15049            END;
15050 
15051 
15052         IF l_valid THEN
15053 
15054             -- Get the lead time for ATO options
15055             IF l_ato_line_id is not null AND
15056                l_line_id <> l_ato_line_id
15057             THEN
15058 
15059               -- This lines is a ato option or class. Set the atp_lead_time
15060               -- for it.
15061 
15062               IF l_ato_line_id = l_st_ato_line_id THEN
15063                   l_atp_lead_time       := l_st_atp_lead_time;
15064               ELSE
15065                   l_st_atp_lead_time :=
15066                       OE_SCHEDULE_UTIL.Get_Lead_Time
15067                          (p_ato_line_id      => l_ato_line_id,
15068                           p_ship_from_org_id => l_ship_from_org_id);
15069                   l_atp_lead_time  := l_st_atp_lead_time;
15070 
15071                   l_st_ato_line_id := l_ato_line_id;
15072               END IF;
15073             ELSE
15074                l_atp_lead_time          :=0;
15075             END IF;
15076 
15077             -- Get the display values which need to be passed to MRP
15078 
15079             l_project_number    := l_project_id;
15080             l_task_number       := l_task_id;
15081             l_ship_method_text  := l_ship_method;
15082 
15083             IF l_sold_to_org_id is not null
15084             THEN
15085                BEGIN
15086                   SELECT NAME
15087                   INTO l_customer_name
15088                   FROM OE_SOLD_TO_ORGS_V
15089                   WHERE organization_id = l_sold_to_org_id;
15090                EXCEPTION
15091                   WHEN NO_DATA_FOUND THEN
15092                      l_customer_name     := null;
15093                END;
15094             END IF;
15095 
15096             IF l_ship_to_org_id is not null
15097             THEN
15098                BEGIN
15099                   SELECT NAME
15100                   INTO l_customer_location
15101                   FROM OE_SHIP_TO_ORGS_V
15102                   WHERE organization_id = l_ship_to_org_id;
15103                EXCEPTION
15104                   WHEN NO_DATA_FOUND THEN
15105                      l_customer_location := null;
15106                END;
15107             END IF;
15108 
15109             IF l_ship_set_id is not null
15110             THEN
15111                l_ship_set_id_1 := l_ship_set_id;
15112                BEGIN
15113                   SELECT SET_NAME
15114                   INTO l_ship_set_name
15115                   FROM OE_SETS
15116                   WHERE set_id = l_ship_set_id;
15117                EXCEPTION
15118                   WHEN NO_DATA_FOUND THEN
15119                      l_ship_set_name := null;
15120                END;
15121             ELSE
15122                IF nvl(l_smc_flag,'N') = 'Y' THEN
15123                   l_ship_set_id_1 := l_top_model_line_id;
15124                ELSIF l_ato_line_id is not null THEN
15125                  IF  NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
15126                          AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10) THEN
15127                      l_ship_set_id_1 := l_ato_line_id;
15128                  END IF;
15129                END IF;
15130             END IF;
15131 
15132             IF l_arrival_set_id is not null
15133             THEN
15134                BEGIN
15135                   SELECT SET_NAME
15136                   INTO l_arrival_set_name
15137                   FROM OE_SETS
15138                   WHERE set_id = l_arrival_set_id;
15139                EXCEPTION
15140                   WHEN NO_DATA_FOUND THEN
15141                      l_arrival_set_name := null;
15142                END;
15143             END IF;
15144 -- Pack-J changes
15145             IF l_ato_line_id is not null AND
15146             NOT (l_ato_line_id = l_line_id AND
15147                  l_item_type_code in ('STANDARD','OPTION'))
15148             THEN
15149 
15150                  l_ato_model_line_id := l_ato_line_id;
15151 
15152                  BEGIN
15153 
15154                    Select line_id
15155                    Into   l_config_line_id
15156                    From   oe_order_lines_all
15157                    Where  ato_line_id = l_line_id
15158                    And    item_type_code = 'CONFIG';
15159 
15160                  EXCEPTION
15161                    WHEN OTHERS THEN
15162                     l_config_line_id := Null;
15163                  END;
15164             ELSE
15165                l_link_to_line_id   := Null;
15166                l_ato_model_line_id := Null;
15167                l_config_line_id    := Null;
15168             END IF;
15169 
15170             l_conc_line_number :=
15171                    OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(l_line_id);
15172 
15173             l_session_id := Get_Session_Id;
15174 
15175          SELECT instance_id
15176             INTO l_instance_id
15177             FROM mrp_ap_apps_instances;
15178 
15179             l_insert_flag :=  fnd_profile.value('MRP_ATP_CALC_SD');
15180 
15181             IF nvl(l_insert_flag,'N') = 'Y' THEN
15182                l_insert_code   := 1;
15183             ELSE
15184                l_insert_code   := 2;
15185             END IF;
15186 
15187             -- Insert into mrp_atp_schedule_temp table
15188 
15189             INSERT INTO MRP_ATP_SCHEDULE_TEMP
15190             (INVENTORY_ITEM_ID,
15191           SR_INSTANCE_ID,
15192              SOURCE_ORGANIZATION_ID,
15193              CUSTOMER_ID,
15194              CUSTOMER_SITE_ID,
15195              DESTINATION_TIME_ZONE,
15196              QUANTITY_ORDERED,
15197              UOM_CODE,
15198              REQUESTED_SHIP_DATE,
15199              REQUESTED_ARRIVAL_DATE,
15200              LATEST_ACCEPTABLE_DATE,
15201              DELIVERY_LEAD_TIME,
15202              FREIGHT_CARRIER,
15203              INSERT_FLAG,
15204              SHIP_METHOD,
15205              DEMAND_CLASS,
15206              SHIP_SET_NAME,
15207              SHIP_SET_ID,
15208              ARRIVAL_SET_NAME,
15209              ARRIVAL_SET_ID,
15210              OVERRIDE_FLAG,
15211              SESSION_ID,
15212              ORDER_HEADER_ID,
15213              ORDER_LINE_ID,
15214              INVENTORY_ITEM_NAME,
15215              SOURCE_ORGANIZATION_CODE,
15216              ORDER_LINE_NUMBER,
15217              SHIPMENT_NUMBER,
15218              OPTION_NUMBER,
15219              PROMISE_DATE,
15220              CUSTOMER_NAME,
15221              CUSTOMER_LOCATION,
15222              OLD_LINE_SCHEDULE_DATE,
15223              OLD_SOURCE_ORGANIZATION_CODE,
15224              CALLING_MODULE,
15225              ACTION,
15226              STATUS_FLAG,
15227              SCENARIO_ID,
15228              ATP_LEAD_TIME,
15229              ORDER_NUMBER,
15230              OLD_SOURCE_ORGANIZATION_ID,
15231              OLD_DEMAND_CLASS,
15232              PROJECT_ID,
15233              TASK_ID,
15234              PROJECT_NUMBER,
15235              TASK_NUMBER,
15236              SHIP_METHOD_TEXT,
15237              TOP_MODEL_LINE_ID,
15238              ATO_MODEL_LINE_ID,
15239              PARENT_LINE_ID,
15240              VALIDATION_ORG,
15241              COMPONENT_SEQUENCE_ID,
15242              COMPONENT_CODE,
15243              INCLUDED_ITEM_FLAG,
15244              LINE_NUMBER,
15245              CONFIG_ITEM_LINE_ID
15246              )
15247             VALUES
15248             (l_inventory_item_id,
15249              l_instance_id,
15250              l_ship_from_org_id,  --null -- Bug 2913742
15251              l_sold_to_org_id, -- CUSTOMER_ID
15252              l_ship_to_org_id, -- CUSTOMER_SITE_ID
15253              null,  -- DESTINATION_TIME_ZONE
15254              l_quantity_ordered,
15255              l_uom_code,
15256              l_requested_ship_date,
15257              l_requested_arrival_date,
15258              l_latest_acceptable_date,
15259              l_delivery_lead_time,
15260              l_freight_carrier,
15261              l_insert_code,
15262              l_ship_method,
15263              l_demand_class,
15264              l_ship_set_name,
15265              l_ship_set_id_1,
15266              l_arrival_set_name,
15267              l_arrival_set_id,
15268              null, -- OVERRIDE_FLAG
15269              l_session_id,
15270              l_header_id,
15271              l_line_id,
15272              l_ordered_item, -- l_INVENTORY_ITEM_NAME,
15273              null, -- l_SOURCE_ORGANIZATION_CODE,
15274              l_line_number,
15275              l_shipment_number,
15276              l_option_number,
15277              l_promise_date,
15278              l_customer_name,
15279              l_customer_location,
15280              null, -- l_OLD_LINE_SCHEDULE_DATE,
15281              null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
15282              null, -- l_CALLING_MODULE,
15283              100,
15284              4, -- l_STATUS_FLAG,
15285              l_scenario_id,
15286              l_atp_lead_time,
15287              l_order_number,
15288              l_ship_from_org_id,
15289              l_demand_class,
15290              l_project_id,
15291              l_task_id,
15292              l_project_number,
15293              l_task_number,
15294              l_ship_method_text,
15295              l_top_model_line_id,
15296              l_ato_model_line_id,
15297              l_link_to_line_id,
15298              OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID'),
15299              l_component_sequence_id,
15300              l_component_code,
15301              1 , --l_included_item_flag
15302              l_conc_line_number,
15303              l_config_line_id
15304              );
15305 
15306              IF (l_ship_set_id is not null OR
15307                 l_arrival_set_id is not null OR
15308                 nvl(l_smc_flag,'N') = 'Y' OR
15309                 l_ato_line_id is not null) THEN
15310 
15311                 --p_line_id := l_line_id;
15312                 -- IF the line being passed to MRP is in a Ship Set,
15313                 -- or arrival set, we should get the remaining lines
15314                 -- of the set and pass them to MRP too.
15315 
15316                 IF l_arrival_set_id is not null THEN
15317                    p_arrival_set_id    := l_arrival_set_id;
15318                    p_ship_set_id       := null;
15319                    p_top_model_line_id := null;
15320                    p_ato_line_id       := null;
15321 
15322                    OPEN set_lines(p_header_id         => l_header_id,
15323                                   p_line_id           => p_line_id,
15324                                   p_arrival_set_id    => p_arrival_set_id,
15325                                   p_ship_set_id       => p_ship_set_id,
15326                                   p_top_model_line_id => p_top_model_line_id,
15327                                   p_ato_line_id       => p_ato_line_id);
15328 
15329                 ELSIF l_ship_set_id is not null THEN
15330 
15331                    p_arrival_set_id    := null;
15332                    p_ship_set_id       := l_ship_set_id;
15333                    p_top_model_line_id := null;
15334                    p_ato_line_id       := null;
15335 
15336                    OPEN set_lines(p_header_id         => l_header_id,
15337                                   p_line_id           => p_line_id,
15338                                   p_arrival_set_id    => p_arrival_set_id,
15339                                   p_ship_set_id       => p_ship_set_id,
15340                                   p_top_model_line_id => p_top_model_line_id,
15341                                   p_ato_line_id       => p_ato_line_id);
15342 
15343                 ELSIF l_smc_flag is not null THEN
15344                    p_arrival_set_id    := null;
15345                    p_ship_set_id       := null;
15346                    p_top_model_line_id := l_top_model_line_id;
15347                    p_ato_line_id       := null;
15348 
15349                    OPEN set_lines(p_header_id         => l_header_id,
15350                                   p_line_id           => p_line_id,
15351                                   p_arrival_set_id    => p_arrival_set_id,
15352                                   p_ship_set_id       => p_ship_set_id,
15353                                   p_top_model_line_id => p_top_model_line_id,
15354                                   p_ato_line_id       => p_ato_line_id);
15355 
15356                 ELSIF l_ato_line_id is not null THEN
15357                    p_arrival_set_id    := null;
15358                    p_ship_set_id       := null;
15359                    p_top_model_line_id := null;
15360                    p_ato_line_id       := l_ato_line_id;
15361 
15362                    OPEN set_lines(p_header_id         => l_header_id,
15363                                   p_line_id           => p_line_id,
15364                                   p_arrival_set_id    => p_arrival_set_id,
15365                                   p_ship_set_id       => p_ship_set_id,
15366                                   p_top_model_line_id => p_top_model_line_id,
15367                                   p_ato_line_id       => p_ato_line_id);
15368                 END IF;
15369                 LOOP
15370                    FETCH set_lines
15371                    INTO l_line_id,
15372                         l_header_id,
15373                         l_inventory_item_id,
15374                         l_ordered_item,
15375                         l_sold_to_org_id,
15376                         l_ship_to_org_id,
15377                         l_ship_from_org_id,
15378                         l_demand_class,
15379                         l_quantity_ordered,
15380                         l_uom_code,
15381                         l_latest_acceptable_date,
15382                         l_line_number,
15383                         l_shipment_number,
15384                         l_option_number,
15385                         l_delivery_lead_time,
15386                         l_request_date,
15387                         l_promise_date,
15388                         l_project_id,
15389                         l_task_id,
15390                         l_ship_method,
15391                         l_ship_set_id,
15392                         l_arrival_set_id,
15393                         l_link_to_line_id,
15394                         l_ato_line_id, -- 3730998
15395                         l_item_type_code,
15396                         l_top_model_line_id,
15397                         l_component_sequence_id,
15398                         l_component_code;
15399                    EXIT WHEN set_lines%NOTFOUND;
15400 
15401 
15402                    l_project_number    := l_project_id;
15403                    l_task_number       := l_task_id;
15404                    l_ship_method_text  := l_ship_method;
15405 
15406                    IF l_sold_to_org_id is not null
15407                    THEN
15408                       BEGIN
15409                          SELECT NAME
15410                          INTO l_customer_name
15411                          FROM OE_SOLD_TO_ORGS_V
15412                          WHERE organization_id = l_sold_to_org_id;
15413                       EXCEPTION
15414                          WHEN NO_DATA_FOUND THEN
15415                             l_customer_name     := null;
15416                       END;
15417                    END IF;
15418 
15419                    IF l_ship_to_org_id is not null
15420                    THEN
15421                       BEGIN
15422                          SELECT NAME
15423                          INTO l_customer_location
15424                          FROM OE_SHIP_TO_ORGS_V
15425                          WHERE organization_id = l_ship_to_org_id;
15426                       EXCEPTION
15427                          WHEN NO_DATA_FOUND THEN
15428                             l_customer_location := null;
15429                       END;
15430                    END IF;
15431 
15432                    IF l_ship_set_id is not null
15433                    THEN
15434                       BEGIN
15435                          SELECT SET_NAME
15436                          INTO l_ship_set_name
15437                          FROM OE_SETS
15438                          WHERE set_id = l_ship_set_id;
15439                       EXCEPTION
15440                          WHEN NO_DATA_FOUND THEN
15441                             l_ship_set_name := null;
15442                       END;
15443                    END IF;
15444 
15445                    IF l_arrival_set_id is not null
15446                    THEN
15447                       BEGIN
15448                          SELECT SET_NAME
15449                          INTO l_arrival_set_name
15450                          FROM OE_SETS
15451                          WHERE set_id = l_arrival_set_id;
15452                       EXCEPTION
15453                          WHEN NO_DATA_FOUND THEN
15454                             l_arrival_set_name := null;
15455                       END;
15456                    END IF;
15457 
15458                    IF l_order_date_type_code = 'ARRIVAL' THEN
15459                       l_requested_arrival_date := l_request_date;
15460                       IF l_requested_arrival_date is null THEN
15461                          l_requested_arrival_date := SYSDATE;
15462                       END IF;
15463                       l_requested_ship_date := null;
15464                    ELSE
15465                       l_requested_ship_date := l_request_date;
15466                       IF l_requested_ship_date is null THEN
15467                          l_requested_ship_date := SYSDATE;
15468                       END IF;
15469                       l_requested_arrival_date := null;
15470                    END IF;
15471 
15472                    -- Pack-J changes
15473                    IF l_ato_line_id is not null AND
15474                    NOT (l_ato_line_id = l_line_id AND
15475                         l_item_type_code in ('STANDARD','OPTION'))
15476                    THEN
15477 
15478                         l_ato_model_line_id := l_ato_line_id;
15479 
15480                         BEGIN
15481 
15482                           Select line_id
15483                           Into   l_config_line_id
15484                           From   oe_order_lines_all
15485                           Where  ato_line_id = l_line_id
15486                           And    item_type_code = 'CONFIG';
15487 
15488                         EXCEPTION
15489                           WHEN OTHERS THEN
15490                            l_config_line_id := Null;
15491                         END;
15492                    ELSE
15493                       l_link_to_line_id   := Null;
15494                       l_ato_model_line_id := Null;
15495                       l_config_line_id    := Null;
15496                    END IF;
15497 
15498                    l_conc_line_number :=
15499                           OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(l_line_id);
15500 
15501 
15502                    -- Insert into mrp_atp_schedule_temp table
15503 
15504                    INSERT INTO MRP_ATP_SCHEDULE_TEMP
15505                    (INVENTORY_ITEM_ID,
15506                  SR_INSTANCE_ID,
15507                     SOURCE_ORGANIZATION_ID,
15508                     CUSTOMER_ID,
15509                     CUSTOMER_SITE_ID,
15510                     DESTINATION_TIME_ZONE,
15511                     QUANTITY_ORDERED,
15512                     UOM_CODE,
15513                     REQUESTED_SHIP_DATE,
15514                     REQUESTED_ARRIVAL_DATE,
15515                     LATEST_ACCEPTABLE_DATE,
15516                     DELIVERY_LEAD_TIME,
15517                     FREIGHT_CARRIER,
15518                     INSERT_FLAG,
15519                     SHIP_METHOD,
15520                     DEMAND_CLASS,
15521                     SHIP_SET_NAME,
15522                     SHIP_SET_ID,
15523                     ARRIVAL_SET_NAME,
15524                     ARRIVAL_SET_ID,
15525                     OVERRIDE_FLAG,
15526                     SESSION_ID,
15527                     ORDER_HEADER_ID,
15528                     ORDER_LINE_ID,
15529                     INVENTORY_ITEM_NAME,
15530                     SOURCE_ORGANIZATION_CODE,
15531                     ORDER_LINE_NUMBER,
15532                     SHIPMENT_NUMBER,
15533                     OPTION_NUMBER,
15534                     PROMISE_DATE,
15535                     CUSTOMER_NAME,
15536                     CUSTOMER_LOCATION,
15537                     OLD_LINE_SCHEDULE_DATE,
15538                     OLD_SOURCE_ORGANIZATION_CODE,
15539                     CALLING_MODULE,
15540                     ACTION,
15541                     STATUS_FLAG,
15542                     SCENARIO_ID,
15543                     ATP_LEAD_TIME,
15544                     ORDER_NUMBER,
15545                     OLD_SOURCE_ORGANIZATION_ID,
15546                     OLD_DEMAND_CLASS,
15547                     PROJECT_ID,
15548                     TASK_ID,
15549                     PROJECT_NUMBER,
15550                     TASK_NUMBER,
15551                     SHIP_METHOD_TEXT,
15552                     TOP_MODEL_LINE_ID,
15553                     ATO_MODEL_LINE_ID,
15554                     PARENT_LINE_ID,
15555                     VALIDATION_ORG,
15556                     COMPONENT_SEQUENCE_ID,
15557                     COMPONENT_CODE,
15558                     INCLUDED_ITEM_FLAG,
15559                     LINE_NUMBER,
15560                     CONFIG_ITEM_LINE_ID
15561                     )
15562                    VALUES
15563                    (l_inventory_item_id,
15564                     l_instance_id,
15565                     l_ship_from_org_id,  --null -- Bug 2913742
15566                     l_sold_to_org_id, -- CUSTOMER_ID
15567                     l_ship_to_org_id, -- CUSTOMER_SITE_ID
15568                     null,  -- DESTINATION_TIME_ZONE
15569                     l_quantity_ordered,
15570                     l_uom_code,
15571                     l_requested_ship_date,
15572                     l_requested_arrival_date,
15573                     l_latest_acceptable_date,
15574                     l_delivery_lead_time,
15575                     l_freight_carrier,
15576                     l_insert_code,
15577                     l_ship_method,
15578                     l_demand_class,
15579                     l_ship_set_name,
15580                     l_ship_set_id_1,
15581                     l_arrival_set_name,
15582                     l_arrival_set_id,
15583                     null, -- OVERRIDE_FLAG
15584                     l_session_id,
15585                     l_header_id,
15586                     l_line_id,
15587                     l_ordered_item, -- l_INVENTORY_ITEM_NAME,
15588                     null, -- l_SOURCE_ORGANIZATION_CODE,
15589                     l_line_number,
15590                     l_shipment_number,
15591                     l_option_number,
15592                     l_promise_date,
15593                     l_customer_name,
15594                     l_customer_location,
15595                     null, -- l_OLD_LINE_SCHEDULE_DATE,
15596                     null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
15597                     null, -- l_CALLING_MODULE,
15598                     100,
15599                     4, -- l_STATUS_FLAG,
15600                     l_scenario_id,
15601                     l_atp_lead_time,
15602                     l_order_number,
15603                     l_ship_from_org_id,
15604                     l_demand_class,
15605                     l_project_id,
15606                     l_task_id,
15607                     l_project_number,
15608                     l_task_number,
15609                     l_ship_method_text,
15610                     l_top_model_line_id,
15611                     l_ato_model_line_id,
15612                     l_link_to_line_id,
15613                     OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID'),
15614                     l_component_sequence_id,
15615                     l_component_code,
15616                     1 , --l_included_item_flag
15617                     l_conc_line_number,
15618                     l_config_line_id
15619                     );
15620 
15621 
15622                 END LOOP;
15623              END IF;
15624 
15625         END IF; /* l_valid */
15626 
15627 EXCEPTION
15628   WHEN OTHERS THEN
15629 
15630     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
15631     THEN
15632       OE_MSG_PUB.Add_Exc_Msg
15633       (   G_PKG_NAME
15634       ,   'Global_ATP'
15635        );
15636     END IF;
15637 
15638 END Global_ATP;
15639 
15640 
15641 Procedure Cascade_Ship_Set_Attr
15642 ( p_request_rec     IN  OE_Order_Pub.Request_Rec_Type
15643 , x_return_status   OUT NOCOPY /* file.sql.39 change */ VARCHAR2)
15644 IS
15645 
15646 l_line_tbl            OE_ORDER_PUB.line_tbl_type;
15647 l_old_line_tbl        OE_ORDER_PUB.line_tbl_type;
15648 l_control_rec         OE_GLOBALS.control_rec_type;
15649 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
15650 --
15651 l_msg_count                   NUMBER;
15652 l_msg_data                    VARCHAR2(2000);
15653 BEGIN
15654    IF l_debug_level  > 0 THEN
15655       oe_debug_pub.add(  'ENTERING Cascade Set Attr ' , 1 ) ;
15656       oe_debug_pub.add(  'Header_id  ' || p_request_rec.param1 , 2 ) ;
15657       oe_debug_pub.add(  'Ship_Set_id  ' || p_request_rec.param2 , 2 ) ;
15658       oe_debug_pub.add(  'Shipping Method  ' || p_request_rec.param3 , 2 ) ;
15659    END IF;
15660 
15661    x_return_status := FND_API.G_RET_STS_SUCCESS;
15662 
15663      Oe_Config_Schedule_Pvt.Query_Set_Lines
15664         (p_header_id     => p_request_rec.param1,
15665          p_ship_set_id   => p_request_rec.param2,
15666          p_sch_action    => 'UI_ACTION',
15667          x_line_tbl      => l_line_tbl,
15668          x_return_status  => x_return_status);
15669 
15670      l_old_line_tbl := l_line_tbl;
15671 
15672      FOR I IN 1..l_line_tbl.count LOOP
15673 
15674        l_line_tbl(I).shipping_method_code := p_request_rec.param3;
15675        l_line_tbl(I).operation := OE_GLOBALS.G_OPR_UPDATE;
15676 
15677      END LOOP;
15678 
15679     IF l_debug_level  > 0 THEN
15680        oe_debug_pub.add(  'Before calling Process Order from cascade ' , 2 ) ;
15681     END IF;
15682     Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
15683                        p_x_line_tbl      => l_line_tbl,
15684                        p_control_rec     => l_control_rec,
15685                        p_caller          => 'SCH_INTERNAL',
15686                        x_return_status   => x_return_status);
15687 
15688     IF l_debug_level  > 0 THEN
15689       oe_debug_pub.add(  'After calling Po : '  || x_return_status, 2 ) ;
15690     END IF;
15691 
15692     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15693        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15694     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
15695        RAISE FND_API.G_EXC_ERROR;
15696     END IF;
15697 
15698     OE_Order_PVT.Process_Requests_And_Notify
15699     ( p_process_requests        => TRUE
15700     , p_notify                  => FALSE
15701     , p_line_tbl                => l_line_tbl
15702     , p_old_line_tbl            => l_old_line_tbl
15703     , x_return_status           => x_return_status);
15704 
15705     IF l_debug_level  > 0 THEN
15706       oe_debug_pub.add(  'After calling PRN: '  || x_return_status, 2 ) ;
15707     END IF;
15708 
15709     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15710       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15711     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
15712       RAISE FND_API.G_EXC_ERROR;
15713     END IF;
15714 
15715     OE_Set_Util.Update_Set
15716         (p_Set_Id                   => p_request_rec.param2,
15717          p_Shipping_Method_Code     => p_request_rec.param3,
15718          X_Return_Status            => x_return_status,
15719          x_msg_count                => l_msg_count,
15720          x_msg_data                 => l_msg_data
15721         );
15722 
15723    IF l_debug_level  > 0 THEN
15724       oe_debug_pub.add(  'Exiting Cascade Set Attr ' , 1 ) ;
15725    END IF;
15726 
15727 EXCEPTION
15728   WHEN OTHERS THEN
15729 
15730     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
15731     THEN
15732       OE_MSG_PUB.Add_Exc_Msg
15733       (   G_PKG_NAME
15734       ,   'Cascade_Ship_set_attr'
15735        );
15736     END IF;
15737 
15738 END Cascade_Ship_set_attr;
15739 
15740 /* Added the following 2 procedures to fix the bug 6378240  */
15741 
15742 /*---------------------------------------------------------------------
15743 Procedure Name : MRP_ROLLBACK
15744 Description    : Call MRP API to rollback the changes as the changes
15745                  on the line are being rollbacked.
15746                  Added this procedure to fix the bugs 6015417 , 6053872
15747 --------------------------------------------------------------------- */
15748 
15749 Procedure MRP_ROLLBACK
15750 ( p_line_id IN NUMBER
15751  ,p_schedule_action_code IN VARCHAR2
15752  ,x_return_status OUT NOCOPY VARCHAR2)
15753 
15754 IS
15755 l_msg_count               NUMBER;
15756 l_session_id              NUMBER := 0;
15757 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
15758 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
15759 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
15760 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
15761 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
15762 l_mrp_msg_data            VARCHAR2(200);
15763 l_old_line_rec            OE_Order_Pub.Line_Rec_Type;
15764 l_new_line_rec            OE_Order_Pub.Line_Rec_Type;
15765 
15766 --
15767 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
15768 --
15769 BEGIN
15770 
15771    IF l_debug_level  > 0 THEN
15772        oe_debug_pub.add(  'ENTERING CALL MRP ROLLBACK' , 1 ) ;
15773    END IF;
15774 
15775       OE_Line_Util.Query_Row(p_line_id  => p_line_id,
15776                              x_line_rec => l_old_line_rec);
15777       l_old_line_rec.schedule_action_code := p_schedule_action_code ;
15778       -- l_old_line_rec.schedule_action_code := OE_SCHEDULE_UTIL.OESCH_ACT_UNSCHEDULE;
15779    l_new_line_rec := l_old_line_rec;
15780    Load_MRP_request_from_rec
15781        ( p_line_rec              => l_new_line_rec
15782        , p_old_line_rec          => l_old_line_rec
15783        , x_mrp_atp_rec             => l_mrp_atp_rec);
15784 
15785 
15786    IF l_mrp_atp_rec.error_code.count > 0 THEN
15787       l_session_id := Get_Session_Id;
15788 
15789       -- Call ATP
15790 
15791        IF l_debug_level  > 0 THEN
15792            oe_debug_pub.add(  '1. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
15793        END IF;
15794 
15795        MRP_ATP_PUB.Call_ATP
15796               (  p_session_id             =>  l_session_id
15797                , p_atp_rec                =>  l_mrp_atp_rec
15798                , x_atp_rec                =>  l_out_mrp_atp_rec
15799                , x_atp_supply_demand      =>  l_atp_supply_demand
15800                , x_atp_period             =>  l_atp_period
15801                , x_atp_details            =>  l_atp_details
15802                , x_return_status          =>  x_return_status
15803                , x_msg_data               =>  l_mrp_msg_data
15804                , x_msg_count              =>  l_msg_count);
15805 
15806                                               IF l_debug_level  > 0 THEN
15807                                                   oe_debug_pub.add(  '3. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 1 ) ;
15808                                               END IF;
15809 
15810        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15811           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15812        ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
15813              Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
15814                                 p_line_id => p_line_id);
15815              RAISE FND_API.G_EXC_ERROR;
15816        END IF;
15817 
15818        Load_Results_from_rec(p_atp_rec       => l_out_mrp_atp_rec,
15819                              p_x_line_rec    => l_new_line_rec,
15820                              x_return_status => x_return_status);
15821 
15822        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15823           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15824        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
15825           RAISE FND_API.G_EXC_ERROR;
15826        END IF;
15827 
15828    END IF; -- Mrp count.
15829    IF l_debug_level  > 0 THEN
15830        oe_debug_pub.add(  'EXITING CALL MRP ROLLBACK ' , 1 ) ;
15831    END IF;
15832 
15833 
15834 EXCEPTION
15835    WHEN FND_API.G_EXC_ERROR THEN
15836 
15837         x_return_status := FND_API.G_RET_STS_ERROR;
15838 
15839    WHEN OTHERS THEN
15840 
15841         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15842 
15843 /*
15844         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
15845         THEN
15846             OE_MSG_PUB.Add_Exc_Msg
15847             (   G_PKG_NAME,
15848               'MRP_ROLLBACK');
15849         END IF;
15850         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15851 */
15852 END MRP_ROLLBACK;
15853 
15854 Procedure CALL_MRP_ROLLBACK
15855 ( x_return_status OUT NOCOPY VARCHAR2)
15856 
15857 IS
15858 
15859 l_status VARCHAR2(1);
15860 CURSOR C1 is
15861 select line_id , schedule_action_code
15862 from oe_schedule_lines_temp;
15863 
15864 BEGIN
15865 
15866  oe_debug_pub.add(  '6015417,6053872 : in call_mrp_rollback  ');
15867  for rec in C1 loop
15868        oe_debug_pub.add(  '6015417,6053872 : line_id  '||rec.line_id ||'  ' ||rec.schedule_action_code);
15869        if rec.schedule_action_code = 'SCHEDULE' then
15870            MRP_ROLLBACK
15871             ( p_line_id  =>  rec.line_id
15872              ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
15873              ,x_return_status    => l_status);
15874         end if;
15875  end loop;
15876 
15877 EXCEPTION
15878    WHEN FND_API.G_EXC_ERROR THEN
15879 
15880         x_return_status := FND_API.G_RET_STS_ERROR;
15881 
15882    WHEN OTHERS THEN
15883 
15884         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15885 
15886 /*
15887         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
15888         THEN
15889             OE_MSG_PUB.Add_Exc_Msg
15890             (   G_PKG_NAME,
15891               'Call_MRP_ROLLBACK');
15892         END IF;
15893 */
15894 END CALL_MRP_ROLLBACK;
15895 
15896 /* Added the following to fix the bug 6663462 */
15897 
15898 
15899 Procedure DELAYED_SCHEDULE_LINES
15900 ( x_return_status OUT NOCOPY VARCHAR2)
15901 
15902 IS
15903 
15904 l_status VARCHAR2(1);
15905 j        NUMBER;
15906 l_atp_tbl OE_ATP.Atp_Tbl_Type;
15907 l_return_status   VARCHAR2(1);
15908 l_msg_count       NUMBER;
15909 l_msg_data        VARCHAR2(2000);
15910 l_line_tbl                OE_ORDER_PUB.Line_tbl_type;
15911 l_old_line_tbl            OE_ORDER_PUB.Line_tbl_type;
15912 
15913 BEGIN
15914   oe_debug_pub.add(  '6663462 : in schedule_delayed_lines ');
15915   l_line_tbl := OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl;
15916   l_old_line_tbl := l_line_tbl;
15917   for j in 1..l_line_tbl.count LOOP
15918        oe_debug_pub.add(  ' 6663462  : line_id   '|| l_line_tbl(j).line_id );
15919        l_line_tbl(j).operation := OE_GLOBALS.G_OPR_UPDATE; --6715950
15920   end loop;
15921   oe_debug_pub.add(  ' 6663462  : calling process group ' );
15922   IF l_line_tbl.count > 0 THEN
15923 
15924      Oe_Config_Schedule_Pvt.Process_Group
15925        (p_x_line_tbl     => l_line_tbl
15926        ,p_old_line_tbl   => l_old_line_tbl
15927        ,p_caller         => 'UI_ACTION'
15928        ,p_sch_action     => 'SCHEDULE'
15929        ,p_partial        => TRUE
15930        ,x_return_status  => x_return_status);
15931 
15932      OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl.delete;
15933 
15934   END IF;
15935 
15936 EXCEPTION
15937    WHEN FND_API.G_EXC_ERROR THEN
15938 
15939         x_return_status := FND_API.G_RET_STS_ERROR;
15940 
15941    WHEN OTHERS THEN
15942         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15943 
15944 END DELAYED_SCHEDULE_LINES ;
15945 
15946 /*----------------------------------------------------------------------------------
15947 * PROCEDURE IS_ITEM_SUBSTITUTED
15948 * Added for ER 6110708. This API will be used for Item Substituted Validation template.
15949 * ------------------------------------------------------------------------------------*/
15950 PROCEDURE IS_ITEM_SUBSTITUTED
15951 (
15952   p_application_id    IN  NUMBER
15953 , p_entity_short_name   IN  VARCHAR2
15954 , p_validation_entity_short_name  IN  VARCHAR2
15955 , p_validation_tmplt_short_name IN  VARCHAR2
15956 , p_record_set_short_name     IN  VARCHAR2
15957 , p_scope             IN  VARCHAR2
15958 , x_result_out OUT NOCOPY NUMBER
15959 
15960 )
15961 IS
15962 BEGIN
15963 
15964   IF OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED = 'Y' THEN
15965     x_result_out := 1;
15966   ELSE
15967     x_result_out := 0;
15968   END IF;
15969 
15970 END IS_ITEM_SUBSTITUTED;
15971 
15972 /*-----------------------------------------------------------------------------------
15973  * PROCEDURE IS_LINE_PICKED
15974  * Added for ER 6110708. This API will be used for Not Picked Validation template.
15975  * ----------------------------------------------------------------------------------*/
15976 PROCEDURE IS_LINE_PICKED
15977 (
15978   p_application_id    IN  NUMBER
15979 , p_entity_short_name   IN  VARCHAR2
15980 , p_validation_entity_short_name  IN  VARCHAR2
15981 , p_validation_tmplt_short_name IN  VARCHAR2
15982 , p_record_set_short_name     IN  VARCHAR2
15983 , p_scope             IN  VARCHAR2
15984 , x_result_out OUT NOCOPY NUMBER
15985 
15986 )
15987 IS
15988 
15989   CURSOR C_IS_LINE_PICKED
15990   IS
15991   SELECT PICK_STATUS
15992   FROM   WSH_DELIVERY_LINE_STATUS_V
15993   WHERE  SOURCE_CODE = 'OE'
15994   AND    SOURCE_LINE_ID = OE_LINE_SECURITY.g_record.line_id
15995   AND    PICK_STATUS NOT IN ('N', 'R', 'X');
15996 
15997   l_pick_status VARCHAR2(1);
15998 
15999 BEGIN
16000 
16001   OPEN C_IS_LINE_PICKED;
16002   FETCH C_IS_LINE_PICKED into l_pick_status;
16003 
16004   /* If there is atleast one delivery for the current line which is not having the pick status of N,R and X,
16005      that means the delivery has been picked atleast once by Pick Release program. The delivery has gone past the Picking at least once.
16006   */
16007   IF C_IS_LINE_PICKED%FOUND THEN
16008     x_result_out := 1;
16009   ELSE
16010     x_result_out := 0;
16011   END IF;
16012 
16013   CLOSE C_IS_LINE_PICKED;
16014 
16015 END IS_LINE_PICKED;
16016 
16017 /*----------------------------------------------------------------------------------
16018  * PROCEDURE VALIDATE_ITEM_SUBSTITUTION
16019  * Added for ER 6110708. This API will validate the new substitute item before
16020  * calling process order api.
16021  * ---------------------------------------------------------------------------------*/
16022 PROCEDURE VALIDATE_ITEM_SUBSTITUTION
16023 (
16024 p_new_inventory_item_id   IN NUMBER,
16025 p_old_inventory_item_id   IN NUMBER,
16026 p_new_ship_from_org_id    IN NUMBER,
16027 p_old_ship_from_org_id    IN NUMBER,
16028 p_old_shippable_flag      IN VARCHAR2
16029 )
16030 IS
16031   l_shippable_flag  varchar2(1);
16032 BEGIN
16033    BEGIN
16034       SELECT shippable_item_flag
16035       INTO   l_shippable_flag
16036       FROM   MTL_SYSTEM_ITEMS
16037       WHERE  INVENTORY_ITEM_ID = p_new_inventory_item_id
16038       AND    ORGANIZATION_ID = p_new_ship_from_org_id;
16039 
16040       IF l_shippable_flag <> p_old_shippable_flag THEN
16041            oe_debug_pub.add(  'Item substitution cannot happen between shippable and non-shippable items' , 5 ) ;
16042            Fnd_Message.set_name('ONT','OE_SCH_LOOP_SHP_NONSHP');
16043            Oe_Msg_Pub.Add;
16044            OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N';
16045            RAISE FND_API.G_EXC_ERROR;
16046       END IF;
16047    END;
16048 END VALIDATE_ITEM_SUBSTITUTION;
16049 
16050 END OE_SCHEDULE_UTIL;