DBA Data[Home] [Help]

PACKAGE BODY: APPS.OE_SCHEDULE_UTIL

Source


1 PACKAGE BODY OE_SCHEDULE_UTIL AS
2 /* $Header: OEXUSCHB.pls 120.117.12020000.21 2013/05/15 02:15:25 gabhatia 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 G_BINARY_LIMIT         CONSTANT  NUMBER := OE_GLOBALS.G_BINARY_LIMIT; --7827737
12 
13 --ER 2736896
14 G_GOP_AUTO_SPLIT           BOOLEAN := FALSE;
15 G_GOP_AUTO_SPLIT_LINE_ID   NUMBER := 0;
16 --Bug 16614829, add Structure for keeping item,warehouse combination
17 TYPE warehouse_table_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
18 TYPE item_warehouse_type IS RECORD (warehouses  warehouse_table_type);
19 TYPE item_warehouse_table IS TABLE OF item_warehouse_type INDEX BY BINARY_INTEGER;
20 G_ITEM_ORG_TABLE item_warehouse_table;
21 --ER 2736896
22 
23 /*-- 3288805 -- */ -- moved to spec.
24 --G_HEADER_ID        NUMBER       := null;
25 --G_DATE_TYPE        VARCHAR2(30) := null;
26 
27 --G_ATP_TBL          OE_ATP.atp_tbl_type; -- 2434807 - moved to package spec.
28 
29 -- INVCONV below procedure not used now because of OPM inventory convergence
30 /*PROCEDURE get_process_query_quantities
31   (   p_org_id       IN  NUMBER
32    ,  p_item_id      IN  NUMBER
33    ,  p_line_id      IN  NUMBER
34 , x_on_hand_qty OUT NOCOPY NUMBER
35 
36 , x_avail_to_reserve OUT NOCOPY NUMBER
37 
38   ) ; */
39 
40 -- Added for ER 6110708
41 PROCEDURE VALIDATE_ITEM_SUBSTITUTION
42 (
43 p_new_inventory_item_id   IN NUMBER,
44 p_old_inventory_item_id   IN NUMBER,
45 p_new_ship_from_org_id    IN NUMBER,
46 p_old_ship_from_org_id    IN NUMBER,
47 p_old_shippable_flag      IN VARCHAR2
48 );
49 
50 /*---------------------------------------------------------------------
51 Procedure Name : MRP_ROLLBACK (OVERRLOADED)
52 Description    : Call MRP API UNSCHEDULE the order line
53                  which has been demanded in GOP with substitute item
54                  but in OM still has original item
55                  Added this procedure for the ER 2736896
56                  ONLY called from procedure check_split_needed()
57 --------------------------------------------------------------------- */
58 Procedure MRP_ROLLBACK
59 ( p_line_id IN NUMBER
60  ,p_item_id in NUMBER
61  ,p_schedule_action_code IN VARCHAR2
62  ,x_return_status OUT NOCOPY VARCHAR2);
63 
64 /*--------------------------------------------------------
65 PROCEDURE Print_Time
66 
67 --------------------------------------------------------*/
68 
69 PROCEDURE Print_Time(p_msg   IN  VARCHAR2)
70 IS
71   l_time    VARCHAR2(100);
72   --
73   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
74   --
75 BEGIN
76   l_time := to_char (new_time (sysdate, 'PST', 'EST'),
77                                  'DD-MON-YY HH24:MI:SS');
78   IF l_debug_level  > 0 THEN
79       oe_debug_pub.add(  P_MSG || ': '|| L_TIME , 1 ) ;
80   END IF;
81 END Print_Time;
82 
83 --- Start 2434807 ---
84 /*--------------------------------------------------------------------------
85 Procedure Name : Get_Atp_Table_Count
86 Description    : This procedure returns the count of record in g_atp_tbl
87                  along with the contents in the table.
88 --------------------------------------------------------------------------*/
89 PROCEDURE Get_Atp_Table_Count(p_atp_tbl  OUT NOCOPY OE_ATP.Atp_Tbl_Type,
90                               p_atp_tbl_cnt  OUT  NOCOPY NUMBER)
91 IS
92 BEGIN
93    p_atp_tbl_cnt := g_atp_tbl.count;
94    p_atp_tbl := g_atp_tbl;
95 END Get_Atp_Table_Count;
96 --- End 2434807 --
97 
98 
99 /*--------------------------------------------------------------------------
100 Procedure Name : Get_Session_Id
101 Description    : This procedure returns the session_id which will be
102                  passed to MRP's ATP API.
103 --------------------------------------------------------------------------*/
104 FUNCTION Get_Session_Id
105 RETURN number
106 IS
107 --
108 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
109 --
110 BEGIN
111 
112   SELECT mrp_atp_schedule_temp_s.nextval
113   INTO   MRP_SESSION_ID
114   from dual;
115 
116   return MRP_SESSION_ID;
117 EXCEPTION
118   WHEN OTHERS THEN
119     return 0;
120 END Get_Session_Id;
121 
122 /*--------------------------------------------------------------------------
123 Procedure Name : Get_MRP_Session_Id
124 Description    : This procedure returns the MRP_session_id which will be
125                  Used in the pld.
126 --------------------------------------------------------------------------*/
127 FUNCTION Get_MRP_Session_Id
128 RETURN number
129 IS
130 --
131 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
132 --
133 BEGIN
134   return MRP_SESSION_ID;
135 EXCEPTION
136    WHEN OTHERS THEN
137         return 0;
138 END Get_MRP_Session_Id;
139 
140 /*--------------------------------------------------------------------------
141 Function Name : Get_ATP_CHECK_Session_Id
142 Description    : This procedure returns the ATP_CHECK_session_id which will be
143                  Used in the pld.
144 --------------------------------------------------------------------------*/
145 FUNCTION Get_ATP_CHECK_Session_Id
146 RETURN number
147 IS
148 --
149 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
150 --
151 BEGIN
152   return G_ATP_CHECK_SESSION_ID;
153 EXCEPTION
154    WHEN OTHERS THEN
155         return 0;
156 END Get_ATP_CHECK_Session_Id;
157 
158 FUNCTION Validate_ship_method
159 (p_new_ship_method IN VARCHAR2,
160  p_old_ship_method IN VARCHAR2,
161  p_ship_from_org_id IN NUMBER)
162 RETURN BOOLEAN
163 IS
164 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
165 l_count NUMBER := 0;
166 BEGIN
167 
168   IF l_debug_level > 0 THEN
169       oe_debug_pub.add('Entering Validate_ship_method',1);
170       oe_debug_pub.add('p_new_ship_method ' || p_new_ship_method,1);
171       oe_debug_pub.add('p_old_ship_method ' || p_old_ship_method,1);
172   END IF;
173 
174   IF NOT OE_GLOBALS.Equal(p_new_ship_method,
175                           p_old_ship_method)  THEN
176 
177    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110509' THEN
178 
179         SELECT count(*)
180         INTO   l_count
181         FROM   wsh_carrier_services wsh,
182                wsh_org_carrier_services wsh_org
183         WHERE  wsh_org.organization_id    = p_ship_from_org_id
184         AND  wsh.carrier_service_id       = wsh_org.carrier_service_id
185         AND  wsh.ship_method_code         = p_new_ship_method
186         AND  wsh_org.enabled_flag         = 'Y';
187 
188    ELSE
189 
190         SELECT count(*)
191         INTO l_count
192         FROM    wsh_carrier_ship_methods
193         WHERE   ship_method_code = p_new_ship_method
194         AND   organization_id = p_ship_from_org_id;
195 
196    END IF;
197 
198        IF l_debug_level > 0 THEN
199          oe_debug_pub.add('l_count ' || l_count,1);
200        END IF;
201 
202        IF l_count  = 0 THEN
203           RETURN FALSE;
204        END IF;
205 
206  END IF;
207 
208 
209  RETURN TRUE;
210 
211 EXCEPTION
212 
213   WHEN OTHERS THEN
214         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
215         THEN
216             OE_MSG_PUB.Add_Exc_Msg
217             (   G_PKG_NAME
218             ,   'oe_schedule_util.validate_ship_method'
219             );
220         END IF;
221         RETURN FALSE;
222 
223 END Validate_ship_method;
224 -- Pack J
225 /*--------------------------------------------------------------------------
226 PROCEDURE Name : validate_with_LAD
227 Description    : This API will check schedule ship date or schedule arrival date
228                  with latest acceptable date and raise error/warning based on flag
229 -------------------------------------------------------------------------- */
230 PROCEDURE validate_with_LAD
231 ( p_header_id IN NUMBER
232  ,p_latest_acceptable_date IN DATE
233  ,p_schedule_ship_date     IN DATE
234  ,p_schedule_arrival_date  IN DATE
235 )
236 IS
237   l_order_date_type_code  VARCHAR2(20);
238   l_return_status  BOOLEAN := FALSE;
239   l_lad_flag        VARCHAR2(1);
240 --
241 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
242 --
243 
244 BEGIN
245    IF l_debug_level  > 0 THEN
246       oe_debug_pub.add(  'ENTERING VALIDATE_WITH_LAD WITH LAD FLAG ' , 1 ) ;
247    END IF;
248    l_order_date_type_code := NVL(Get_Date_Type(p_header_id),'SHIP');
249 
250    -- To check violation of LAD when parameter set to - Ignore LAD
251    IF l_debug_level  > 0 THEN
252       oe_debug_pub.add(  'CHECKING FOR LAD ' , 1 ) ;
253    END IF;
254    l_lad_flag := Oe_Sys_Parameters.Value('LATEST_ACCEPTABLE_DATE_FLAG');
255    IF l_lad_flag = 'I' THEN
256       IF ((l_order_date_type_code = 'SHIP'
257         AND p_schedule_ship_date
258                             > p_latest_acceptable_date)
259       OR (l_order_date_type_code = 'ARRIVAL'
260         AND p_schedule_arrival_date
261                             > p_latest_acceptable_date)) THEN
262          IF l_debug_level  > 0 THEN
263             oe_debug_pub.add(  'LAD VIOLATED ' , 1 ) ;
264          END IF;
265          Fnd_Message.set_name('ONT','ONT_SCH_LAD_VIOLATE');
266          Oe_Msg_Pub.Add;
267       END IF;
268    /* -- 3349770 this will be only validated when user enters manually.
269    ELSIF l_lad_flag = 'H' THEN
270       IF ((l_order_date_type_code = 'SHIP'
271         AND p_schedule_ship_date
272                            > p_latest_acceptable_date)
273       OR (l_order_date_type_code = 'ARRIVAL'
274         AND p_schedule_arrival_date
275                             > p_latest_acceptable_date)) THEN
276            IF l_debug_level  > 0 THEN
277                oe_debug_pub.add(  'SCHEDULE DATE EXCEEDS LAD ' , 1 ) ;
278            END IF;
279            Fnd_Message.set_name('ONT','ONT_SCH_LAD_SCH_FAILED');
280            Oe_Msg_Pub.Add;
281            RAISE FND_API.G_EXC_ERROR;
282       END IF;
283    */
284    END IF;
285    IF l_debug_level  > 0 THEN
286       oe_debug_pub.add(  'EXITING VALIDATE_WITH_LAD' , 1 ) ;
287    END IF;
288 END validate_with_LAD;
289 -- End Pack J
290 /*--------------------------------------------------------------------------
291 Procedure Name : Insert_Mandatory_Components
292 Description    : This procedure is called from the form side, when the user
293                  clicks on global availability button and the item to check
294                  global availability is an ATO Model. We insert the mandatory
295                  components in MRP_ATP_SCHEDULE_TEMP for global availability.
296 --------------------------------------------------------------------------*/
297 Procedure Insert_Mandatory_Components
298 (p_order_number           IN  NUMBER,
299 p_ato_line_id             IN  NUMBER,
300 p_customer_name           IN  VARCHAR2,
301 p_customer_location       IN  VARCHAR2,
302 p_arrival_set_name        IN  VARCHAR2,
303 p_ship_set_name           IN  VARCHAR2,
304 p_ship_set_id             IN  NUMBER,
305 p_requested_ship_date     IN  DATE,
306 p_requested_arrival_date  IN  DATE,
307 p_session_id              IN  NUMBER,
308 p_instance_id             IN  NUMBER,
309 p_insert_code             IN  NUMBER,
310 x_return_status OUT NOCOPY VARCHAR2
311 
312 )
313 IS
314 l_model_line_rec          OE_ORDER_PUB.line_rec_type;
315 l_model_rec               MRP_ATP_PUB.ATP_Rec_Typ;
316 l_smc_rec                 MRP_ATP_PUB.ATP_Rec_Typ;
317 l_ship_set                VARCHAR2(30);
318 lTableName                VARCHAR2(30);
319 lMessageName              VARCHAR2(30);
320 lErrorMessage             VARCHAR2(2000);
321 l_result                  NUMBER := 1;
322 
323 l_scenario_id             NUMBER := -1;
324 l_line_id                 NUMBER;
325 l_header_id               NUMBER;
326 l_ato_line_id             NUMBER;
327 l_inventory_item_id       NUMBER;
328 l_ordered_item            VARCHAR2(2000);
329 l_sold_to_org_id          NUMBER;
330 l_ship_to_org_id          NUMBER;
331 l_ship_from_org_id        NUMBER;
332 l_quantity_ordered        NUMBER;
333 l_uom_code                VARCHAR2(3);
334 l_latest_acceptable_date  DATE;
335 l_line_number             NUMBER;
336 l_shipment_number         NUMBER;
337 l_option_number           NUMBER;
338 l_delivery_lead_time      NUMBER;
339 l_promise_date            DATE;
340 l_project_id              NUMBER;
341 l_task_id                 NUMBER;
342 l_ship_method             VARCHAR2(30) := null;
343 l_demand_class            VARCHAR2(30) := null;
344 l_ship_set_id             NUMBER;
345 l_arrival_set_id          NUMBER;
346 l_ship_method_text        VARCHAR2(80);
347 l_project_number          NUMBER;
348 l_task_number             NUMBER;
349 l_st_atp_lead_time        NUMBER := 0;
350 l_order_number            NUMBER;
351 --
352 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
353 --
354 BEGIN
355 
356 
357    IF NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
358    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 ) THEN
359 
360       SAVEPOINT insert_mand_comp;
361 
362 --      l_model_line_rec := OE_LINE_UTIL.Query_Row(p_ato_line_id);
363 
364     OE_Line_Util.Query_Row(p_line_id  => p_ato_line_id,
365                                  x_line_rec => l_model_line_rec);
366 
367        l_st_atp_lead_time :=
368           Get_Lead_Time
369             (p_ato_line_id      => l_model_line_rec.ato_line_id,
370              p_ship_from_org_id => l_model_line_rec.ship_from_org_id);
371 
372       l_model_rec.Inventory_Item_Id := MRP_ATP_PUB.number_arr
373                             (l_model_line_rec.Inventory_Item_Id);
374 
375       l_model_rec.Source_Organization_Id := MRP_ATP_PUB.number_arr
376                             (l_model_line_rec.ship_from_org_id);
377 
378       l_model_rec.Identifier := MRP_ATP_PUB.number_arr
379                             (l_model_line_rec.line_id);
380 
381       l_model_rec.Calling_Module := MRP_ATP_PUB.number_arr
382                             (660);
383 
384       l_model_rec.Customer_Id := MRP_ATP_PUB.number_arr
385                             (l_model_line_rec.sold_to_org_id);
386 
387       l_model_rec.Customer_Site_Id := MRP_ATP_PUB.number_arr
388                             (l_model_line_rec.ship_to_org_id);
389 
390       l_model_rec.Destination_Time_Zone := MRP_ATP_PUB.char30_arr
391                             (null);
392 
393       l_model_rec.Quantity_Ordered := MRP_ATP_PUB.number_arr
394                             (l_model_line_rec.ordered_quantity);
395 
396       l_model_rec.Quantity_UOM := MRP_ATP_PUB.char3_arr
397                             (l_model_line_rec.order_quantity_uom);
398 
399       l_model_rec.Earliest_Acceptable_Date := MRP_ATP_PUB.date_arr
400                             (l_model_line_rec.Earliest_Acceptable_Date);
401 
402       l_model_rec.Requested_Ship_Date := MRP_ATP_PUB.date_arr
403                             (l_model_line_rec.request_date);
404 
405       l_model_rec.Requested_Arrival_Date := MRP_ATP_PUB.date_arr
406                             (l_model_line_rec.request_date);
407 
408       l_model_rec.Latest_Acceptable_Date := MRP_ATP_PUB.date_arr
409                             (l_model_line_rec.Latest_Acceptable_Date);
410 
411       l_model_rec.Delivery_Lead_Time := MRP_ATP_PUB.number_arr
412                             (l_model_line_rec.Delivery_Lead_Time);
413       l_model_rec.Atp_lead_Time := MRP_ATP_PUB.number_arr
414                             (l_st_atp_lead_time);
415 
416       l_model_rec.Freight_Carrier := MRP_ATP_PUB.char30_arr
417                             (l_model_line_rec.Freight_Carrier_Code);
418 
419       l_model_rec.Ship_Method := MRP_ATP_PUB.char30_arr
420                             (null);
421 
422       l_model_rec.Demand_Class := MRP_ATP_PUB.char30_arr
423                             (l_model_line_rec.Demand_Class_Code);
424 
425       l_model_rec.Ship_Set_Name := MRP_ATP_PUB.char30_arr
426                             (l_model_line_rec.ship_set_id);
427 
428       l_model_rec.Arrival_Set_Name := MRP_ATP_PUB.char30_arr
429                             (l_model_line_rec.arrival_set_id);
430 
431       l_model_rec.Override_Flag := MRP_ATP_PUB.char1_arr
432                             (null);
433 
434       l_model_rec.Ship_Date := MRP_ATP_PUB.date_arr
435                             (null);
436 
437       l_model_rec.Available_Quantity := MRP_ATP_PUB.number_arr
438                             (null);
439 
440       l_model_rec.Requested_Date_Quantity := MRP_ATP_PUB.number_arr
441                             (null);
442 
443       l_model_rec.Group_Ship_Date := MRP_ATP_PUB.date_arr
444                             (null);
445 
446       l_model_rec.Group_Arrival_Date := MRP_ATP_PUB.date_arr
447                             (null);
448 
449       l_model_rec.Vendor_Id := MRP_ATP_PUB.number_arr
450                             (null);
451 
452       l_model_rec.Vendor_Site_Id := MRP_ATP_PUB.number_arr
453                             (null);
454 
455       l_model_rec.Insert_Flag := MRP_ATP_PUB.number_arr
456                             (null);
457 
458       l_model_rec.Error_Code := MRP_ATP_PUB.number_arr
459                             (null);
460 
461       l_model_rec.Message := MRP_ATP_PUB.char2000_arr
462                             (null);
463 
464       l_model_rec.Action  := MRP_ATP_PUB.number_arr
465                             (null);
466 
467       l_order_number := Get_order_number(l_model_line_rec.header_id);
468 
469       l_model_rec.Order_number  := MRP_ATP_PUB.number_arr
470                             (l_order_number);
471 
472       IF l_debug_level  > 0 THEN
473           oe_debug_pub.add(  '1.. CALLING CTO GET_BOM_MANDATORY_COMPS' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
474       END IF;
475 
476       BEGIN
477       l_result  :=  CTO_CONFIG_ITEM_PK.GET_MANDATORY_COMPONENTS
478                       (p_ship_set           => l_model_rec,
479                        p_organization_id    => null,
480                        p_inventory_item_id  => null,
481                        x_smc_rec            => l_smc_rec,
482                        xErrorMessage        => lErrorMessage,
483                        xMessageName         => lMessageName,
484                        xTableName           => lTableName);
485 
486       IF l_debug_level  > 0 THEN
487           oe_debug_pub.add(  '1. AFTER CALLING CTO API : ' || L_RESULT , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
488       END IF;
489       IF l_debug_level  > 0 THEN
490           oe_debug_pub.add(  'COUNT IS: ' || L_SMC_REC.INVENTORY_ITEM_ID.COUNT , 1 ) ;
491       END IF;
492 
493       EXCEPTION
494          WHEN OTHERS THEN
495               IF l_debug_level  > 0 THEN
496                   oe_debug_pub.add(  'CTO API RETURNED AN UNEXPECTED ERROR' ) ;
497               END IF;
498               l_result := 0;
499       END;
500 
501       IF l_result = 1 AND
502          l_smc_rec.Identifier.count >= 1 THEN
503 
504                               IF l_debug_level  > 0 THEN
505                                   oe_debug_pub.add(  'SMC COUNT IS : ' || L_SMC_REC.IDENTIFIER.COUNT , 1 ) ;
506                               END IF;
507 
508          FOR J IN 1..l_smc_rec.Identifier.count LOOP
509              l_line_id                 := l_smc_rec.Identifier(J);
510              l_header_id               := l_model_line_rec.header_id;
511              l_ato_line_id             := l_model_line_rec.ato_line_id;
512              l_inventory_item_id       := l_smc_rec.Inventory_Item_Id(J);
513              l_ordered_item            := null;
514              l_sold_to_org_id          := l_model_line_rec.sold_to_org_id;
515              l_ship_to_org_id          := l_model_line_rec.ship_to_org_id;
516              l_ship_from_org_id        := l_model_line_rec.ship_from_org_id;
517              l_demand_class            := l_model_line_rec.demand_class_code;
518              l_quantity_ordered        := l_smc_rec.Quantity_Ordered(J);
519              l_uom_code                := l_smc_rec.Quantity_UOM(J);
520              l_latest_acceptable_date  :=
521                                  l_model_line_rec.latest_acceptable_date;
522              l_line_number             := l_model_line_rec.line_number;
523              l_shipment_number         := l_model_line_rec.line_number;
524              l_option_number           := l_model_line_rec.option_number;
525              l_delivery_lead_time      := l_model_line_rec.delivery_lead_time;
526              l_promise_date            := l_model_line_rec.promise_date;
527              l_project_id              := l_model_line_rec.project_id;
528              l_task_id                 := l_model_line_rec.task_id;
529              l_ship_method             := l_model_line_rec.shipping_method_code;
530              l_arrival_set_id          := l_model_line_rec.arrival_set_id;
531 
532              l_ship_method_text        := l_ship_method;
533              l_project_number          := l_project_id;
534              l_task_number             := l_task_id;
535 
536              IF l_inventory_item_id is not null AND
537                 l_ship_from_org_id is not null
538              THEN
539                 BEGIN
540 
541                   SELECT concatenated_segments
542                   INTO  l_ordered_item
543                   FROM  mtl_system_items_vl
544                   WHERE inventory_item_id = l_inventory_item_id
545                   AND organization_id = l_ship_from_org_id;
546 
547                 EXCEPTION
548                   WHEN OTHERS THEN
549                      null;
550                 END;
551 
552              END IF;
553 
554 
555             INSERT INTO MRP_ATP_SCHEDULE_TEMP
556             (INVENTORY_ITEM_ID,
557           SR_INSTANCE_ID,
558              SOURCE_ORGANIZATION_ID,
559              CUSTOMER_ID,
560              CUSTOMER_SITE_ID,
561              DESTINATION_TIME_ZONE,
562              QUANTITY_ORDERED,
563              UOM_CODE,
564              REQUESTED_SHIP_DATE,
565              REQUESTED_ARRIVAL_DATE,
566              LATEST_ACCEPTABLE_DATE,
567              DELIVERY_LEAD_TIME,
568              FREIGHT_CARRIER,
569              INSERT_FLAG,
570              SHIP_METHOD,
571              DEMAND_CLASS,
572              SHIP_SET_NAME,
573              SHIP_SET_ID,
574              ARRIVAL_SET_NAME,
575              ARRIVAL_SET_ID,
576              ATP_LEAD_TIME,
577              OVERRIDE_FLAG,
578              SESSION_ID,
579              ORDER_HEADER_ID,
580              ORDER_LINE_ID,
581              INVENTORY_ITEM_NAME,
582              SOURCE_ORGANIZATION_CODE,
583              ORDER_LINE_NUMBER,
584              SHIPMENT_NUMBER,
585              OPTION_NUMBER,
586              PROMISE_DATE,
587              CUSTOMER_NAME,
588              CUSTOMER_LOCATION,
589              OLD_LINE_SCHEDULE_DATE,
590              OLD_SOURCE_ORGANIZATION_CODE,
591              CALLING_MODULE,
592              ACTION,
593              STATUS_FLAG,
594              SCENARIO_ID,
595              ORDER_NUMBER,
596              OLD_SOURCE_ORGANIZATION_ID,
597              OLD_DEMAND_CLASS,
598              PROJECT_ID,
599              TASK_ID,
600              PROJECT_NUMBER,
601              TASK_NUMBER,
602              SHIP_METHOD_TEXT
603              )
604             VALUES
605             (l_inventory_item_id,
606              p_instance_id,
607              null,
608              l_sold_to_org_id, -- CUSTOMER_ID
609              l_ship_to_org_id, -- CUSTOMER_SITE_ID
610              null,  -- DESTINATION_TIME_ZONE
611              l_quantity_ordered,
612              l_uom_code,
613              p_requested_ship_date,
614              p_requested_arrival_date,
615              l_latest_acceptable_date,
616              l_delivery_lead_time,
617              null, -- FREIGHT_CARRIER,
618              p_insert_code,
619              l_ship_method,
620              l_demand_class,
621              p_ship_set_name,
622              p_ship_set_id,
623              p_arrival_set_name,
624              l_arrival_set_id,
625              l_st_atp_lead_time,
626              null, -- OVERRIDE_FLAG
627              p_session_id,
628              l_header_id,
629              l_line_id,
630              l_ordered_item, -- l_INVENTORY_ITEM_NAME,
631              null, -- l_SOURCE_ORGANIZATION_CODE,
632              l_line_number,
633              l_shipment_number,
634              l_option_number,
635              l_promise_date,
636              p_customer_name,
637              p_customer_location,
638              null, -- l_OLD_LINE_SCHEDULE_DATE,
639              null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
640              null, -- l_CALLING_MODULE,
641              100,
642              4, -- l_STATUS_FLAG,
643              l_scenario_id,
644              p_order_number,
645              l_ship_from_org_id,
646              l_demand_class,
647              l_project_id,
648              l_task_id,
649              l_project_number,
650              l_task_number,
651              l_ship_method_text
652              );
653 
654          END LOOP;
655       END IF;
656 
657    END IF; -- GOP Code control
658    x_return_status := FND_API.G_RET_STS_SUCCESS;
659 
660 EXCEPTION
661     WHEN OTHERS THEN
662         ROLLBACK TO SAVEPOINT insert_mand_comp;
663         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
664 
665 END Insert_Mandatory_Components;
666 
667 /*--------------------------------------------------------------------------
668 Procedure Name : Update_PO
669 Description    : This procedure is called whenever there is a change to
670                  schedule_ship_date on an internal order. PO has a callback
671                  we need to call to notify them of this change.
672 --------------------------------------------------------------------------*/
673 
674 Procedure Update_PO(p_schedule_ship_date       IN DATE,
675                     p_source_document_id       IN VARCHAR2,
676                     p_source_document_line_id  IN VARCHAR2)
677 IS
678 po_result    BOOLEAN;
679 --
680 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
681 --
682 BEGIN
683 
684   IF l_debug_level  > 0 THEN
685       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.UPDATE_PO' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
686   END IF;
687 
688   -- Call po if internal req and quantity is changed
689 
690   IF p_source_document_line_id IS NOT NULL THEN
691 
692        IF l_debug_level  > 0 THEN
693            oe_debug_pub.add(  'DATE ' || P_SCHEDULE_SHIP_DATE , 2 ) ;
694        END IF;
695 
696        po_result := po_supply.po_req_supply(
697                        p_docid         => p_source_document_id,
698                        p_lineid        => p_source_document_line_id,
699                        p_shipid        => p_source_document_line_id,
700                        p_action        => 'Update_Req_Line_Date',
701                        p_recreate_flag => FALSE,
702                        p_qty           => null,
703                        p_receipt_date  => p_schedule_ship_date);
704   END IF;
705 
706   IF l_debug_level  > 0 THEN
707       oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.UPDATE_PO' , 0.5 ) ;   -- debug level changed to 0.5 for bug 13435459
708   END IF;
709 
710 EXCEPTION
711     WHEN OTHERS THEN
712          IF l_debug_level  > 0 THEN
713              oe_debug_pub.add(  'EXCEPTION IN UPDATE_PO' , 2 ) ;
714          END IF;
715 END Update_PO;
716 
717 /*---------------------------------------------------------------------
718 Function Name : Within_Rsv_Time_Fence
719 Description   : The function returns:
720                 TRUE:  If the Schedule_Ship_Date is within the
721                        time fence of the system date. The time fence
722                        is defined in the profile option
723                        ONT_RESERVATION_TIME_FENCE.
724                 FALSE: If the Schedule_Ship_Date is note within the
725                        time fence of the system date.
726                 The date part of the dates (and not the time) are compared
727                 to return the value.
728 
729                 If schedule_date - SYSDATE < reservation_time_fence
730                 we will return TRUE, else will return FALSE
731 --------------------------------------------------------------------- */
732 Function Within_Rsv_Time_Fence(p_schedule_ship_date IN DATE,
733                 p_org_id IN NUMBER) --4689197
734 RETURN BOOLEAN
735 IS
736   l_rsv_time_fence_profile VARCHAR2(30);
737   l_rsv_time_fence         NUMBER;
738   l_time_to_ship           NUMBER;
739   l_sysdate                DATE;
740   l_schedule_ship_date     DATE;
741   --
742   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
743   --
744 BEGIN
745   l_rsv_time_fence_profile :=
746          Oe_Sys_Parameters.Value('ONT_RESERVATION_TIME_FENCE', p_org_id);
747 
748   BEGIN
749     l_rsv_time_fence := to_number(l_rsv_time_fence_profile);
750   EXCEPTION
751     WHEN OTHERS THEN
752       IF l_debug_level  > 0 THEN
753           oe_debug_pub.add(  'IGNORING RESERVATION TIME FENCE' , 1 ) ;
754       END IF;
755       l_rsv_time_fence := null;
756   END;
757 
758   l_sysdate            := trunc(SYSDATE);
759   l_schedule_ship_date := trunc(p_schedule_ship_date);
760 
761   l_time_to_ship := to_number(l_schedule_ship_date -l_sysdate);
762 
763   IF l_debug_level  > 0 THEN
764       oe_debug_pub.add(  'L_TIME_TO_SHIP ' || L_TIME_TO_SHIP , 1 ) ;
765   END IF;
766   IF l_debug_level  > 0 THEN
767       oe_debug_pub.add(  'L_RSV_TIME_FENCE ' || L_RSV_TIME_FENCE , 1 ) ;
768   END IF;
769 
770 /* Commented the following code to fix the bug 3109349
771 
772   IF l_time_to_ship < 0 THEN
773     -- We don't know what this means. Schedule ship date is already
774     -- past due. So we will not reserve any inventory for this line.
775      RETURN FALSE;
776   ELSIF l_time_to_ship <= l_rsv_time_fence THEN
777 */
778 
779   IF l_time_to_ship <= l_rsv_time_fence THEN
780     RETURN TRUE;
781   ELSE
782     RETURN FALSE;
783   END IF;
784 
785 END Within_Rsv_Time_Fence;
786 /*---------------------------------------------------------------------
787 Function Name : SMC_OVERRIDDEN
788 Description   : This function retuns true if one of the line in a smc model
789                 is overridden. This will be used in procedure load_mrp_request.
790 
791 -----------------------------------------------------------------------*/
792 Function SMC_OVERRIDDEN(p_top_model_line_id IN NUMBER)
793 RETURN BOOLEAN
794 IS
795  l_overridden VARCHAR2(1) := 'N';
796 BEGIN
797 
798    SELECT 'Y'
799    INTO   l_overridden
800    FROM   oe_order_lines_all
801    WHERE  top_model_line_id = p_top_model_line_id
802    AND    open_flag = 'Y'
803    AND    override_atp_date_code = 'Y'
804    AND    rownum < 2;
805 
806    IF l_overridden = 'Y' THEN
807       RETURN TRUE;
808    ELSE
809      RETURN FALSE;
810    END IF;
811 
812 EXCEPTION
813  WHEN OTHERS THEN
814 
815    RETURN FALSE;
816 END SMC_OVERRIDDEN;
817 /*---------------------------------------------------------------------
818 Function Name : SET_OVERRIDDEN
819 Description   : This function retuns true if one of the line in a set
820                 is overridden. This will be used in log_set_request procedure.
821 
822 -----------------------------------------------------------------------*/
823 Function SET_OVERRIDDEN(p_header_id      IN NUMBER
824                        ,p_ship_set_id    IN NUMBER
825                        ,p_arrival_set_id IN NUMBER)
826 RETURN BOOLEAN
827 IS
828  l_overridden VARCHAR2(1) := 'N';
829 BEGIN
830 
831 
832 
833    SELECT 'Y'
834    INTO   l_overridden
835    FROM   oe_order_lines_all
836    WHERE  header_id = p_header_id
837    AND    (ship_set_id = p_ship_set_id
838    OR     arrival_set_id = p_arrival_set_id)
839    AND    override_atp_date_code = 'Y'
840    AND    rownum < 2;
841 
842    IF l_overridden = 'Y' THEN
843       RETURN TRUE;
844    ELSE
845      RETURN FALSE;
846    END IF;
847 
848 EXCEPTION
849  WHEN OTHERS THEN
850 
851    RETURN FALSE;
852 END SET_OVERRIDDEN;
853 /*---------------------------------------------------------------------
854 Procedure Name : Reserve_line
855 Description    : This API calls Inventory's APIs to reserve.
856 --------------------------------------------------------------------- */
857 
858 Procedure Reserve_line
859 ( p_line_rec              IN  OE_ORDER_PUB.Line_Rec_Type
860  ,p_quantity_to_reserve   IN  NUMBER
861  ,p_quantity2_to_reserve   IN  NUMBER  DEFAULT NULL -- INVCONV
862  ,p_rsv_update            IN  BOOLEAN DEFAULT FALSE
863 ,x_return_status OUT NOCOPY VARCHAR2)
864 
865 IS
866 l_msg_count           NUMBER;
867 l_msg_data            VARCHAR2(2000);
868 l_reservation_rec     inv_reservation_global.mtl_reservation_rec_type;
869 l_quantity_reserved   NUMBER;
870 l_quantity2_reserved   NUMBER;  -- INVCONV
871 l_rsv_id              NUMBER;
872 l_reservable_type     NUMBER;
873 l_dummy_sn            inv_reservation_global.serial_number_tbl_type;
874 l_buffer              VARCHAR2(2000);
875 
876 l_substitute_flag BOOLEAN; -- Added for ER 6110708
877 
878 -- subinventory
879 l_revision_code NUMBER;
880 l_lot_code      NUMBER;
881 l_serial_code   NUMBER;
882 l_do_partial_reservation  VARCHAR2(1) := FND_API.G_FALSE;
883 --
884 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
885 --
886 BEGIN
887    IF l_debug_level  > 0 THEN
888        oe_debug_pub.add(  'ENTERING RESERVE LINE :' || P_QUANTITY_TO_RESERVE , 1 ) ;
889          oe_debug_pub.add(  'ENTERING RESERVE LINE qty2 :' || P_QUANTITY2_TO_RESERVE , 1 ) ;
890    END IF;
891    x_return_status := FND_API.G_RET_STS_SUCCESS;
892 
893    SELECT RESERVABLE_TYPE
894    INTO   l_reservable_type
895    FROM   MTL_SYSTEM_ITEMS
896    WHERE  INVENTORY_ITEM_ID = p_line_rec.inventory_item_id
897    AND    ORGANIZATION_ID = p_line_rec.ship_from_org_id;
898 
899    IF l_reservable_type = 1 THEN
900 
901        --Bug 13082802
902        IF l_debug_level  > 0 THEN
903            oe_debug_pub.add(  'BEFORE FULFILLED QTY CHECK' , 2 ) ;
904        END IF;
905 
906        IF (p_line_rec.fulfilled_quantity is not null)
907           AND (p_line_rec.fulfilled_quantity <> FND_API.G_MISS_NUM) THEN
908 
909          -- The line is Fulfilled. Cannot reserve it
910          IF l_debug_level  > 0 THEN
911             oe_debug_pub.add(  'LINE FULFILLED, CANNOT RESERVE LINE. FULFILLED QTY:' || p_line_rec.fulfilled_quantity , 1 ) ;
912          END IF;
913 
914          FND_MESSAGE.SET_NAME('ONT','OE_RSV_LINE_FULFILLED');
915          OE_MSG_PUB.Add;
916 
917          RAISE FND_API.G_EXC_ERROR;
918 
919        END IF;
920        --Bug 13082802
921 
922        IF l_debug_level  > 0 THEN
923            oe_debug_pub.add(  'L_QTY_TO_RESERVE : ' || P_QUANTITY_TO_RESERVE , 3 ) ;
924            oe_debug_pub.add(  'L_QTY2_TO_RESERVE : ' || p_quantity2_to_reserve , 3 ) ;
925        END IF;
926        --newsub check if item is under lot/revision/serial control
927        IF  p_line_rec.subinventory is not null
928        AND p_line_rec.subinventory <> FND_API.G_MISS_CHAR THEN
929            BEGIN
930               SELECT revision_qty_control_code, lot_control_code,
931                      serial_number_control_code
932               INTO   l_revision_code, l_lot_code,
933                      l_serial_code
934               FROM   mtl_system_items
935               WHERE  inventory_item_id = p_line_rec.inventory_item_id
936               AND    organization_id   = p_line_rec.ship_from_org_id;
937 
938            EXCEPTION
939                WHEN OTHERS THEN
940                 x_return_status := FND_API.G_RET_STS_ERROR;
941                 fnd_message.set_name('ONT', 'OE_INVALID_ITEM_WHSE');
942                 OE_MSG_PUB.Add;
943            END;
944 
945            IF l_revision_code = 2
946            OR l_lot_code = 2
947            THEN
948               -- 2 == YES
949               FND_MESSAGE.SET_NAME('ONT', 'OE_SUBINV_NOT_ALLOWED');
950               OE_MSG_PUB.Add;
951               IF  p_line_rec.Schedule_action_code =  OESCH_ACT_RESERVE THEN
952                  x_return_status := FND_API.G_RET_STS_ERROR;
953                  RAISE FND_API.G_EXC_ERROR;
954               ELSE
955                  x_return_status     := FND_API.G_RET_STS_SUCCESS;
956                  IF l_debug_level  > 0 THEN
957                      oe_debug_pub.add(  'BEFORE RETURN' , 3 ) ;
958                  END IF;
959                  RETURN;
960               END IF;
961 
962            END IF;
963        END IF;
964        --end newsub
965 
966 
967 
968 
969        Load_INV_Request
970        ( p_line_rec              => p_line_rec
971        , p_quantity_to_reserve   => p_quantity_to_reserve
972        , p_quantity2_to_reserve   => p_quantity2_to_reserve -- INVCONV
973        , x_reservation_rec       => l_reservation_rec);
974 
975        -- Call INV with action = RESERVE
976 
977        IF l_debug_level  > 0 THEN
978            oe_debug_pub.add(  'CALLING INVS CREATE_RESERVATION' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
979        END IF;
980        -- Pack J
981        -- Check for partial reservation flag
982        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
983          AND Oe_Sys_Parameters.Value('PARTIAL_RESERVATION_FLAG') = 'Y' THEN
984          l_do_partial_reservation := FND_API.G_TRUE;
985        END IF;
986        l_quantity2_reserved :=NULL;
987 
988        /* Added the below code for ER 6110708 */
989        IF OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED = 'Y' THEN
990          l_substitute_flag := TRUE;
991        ELSE
992          l_substitute_flag := FALSE;
993        END IF;
994        /* End of code changes for ER 6110708 */
995 
996         IF l_debug_level  > 0 THEN
997          oe_debug_pub.add(  'before  CREATE RESERVATION ',  1 ) ;
998          oe_debug_pub.add(  ' qty2 = ' || l_reservation_rec.secondary_reservation_quantity , 1 ) ;
999          oe_debug_pub.add(  ' uom2 = ' || l_reservation_rec.secondary_uom_code , 1 ) ;
1000 
1001 
1002          END IF;
1003 
1004        -- p_partial_rsv_exists will be TRUE if reservation get increased
1005        inv_reservation_pub.create_reservation
1006        (
1007         p_api_version_number        => 1.0
1008       , p_init_msg_lst              => FND_API.G_TRUE
1009       , x_return_status             => x_return_status
1010       , x_msg_count                 => l_msg_count
1011       , x_msg_data                  => l_msg_data
1012       , p_rsv_rec                   => l_reservation_rec
1013       , p_serial_number             => l_dummy_sn
1014       , x_serial_number             => l_dummy_sn
1015       , p_partial_reservation_flag  => l_do_partial_reservation  --FND_API.G_FALSE --Pack J
1016       , p_force_reservation_flag    => FND_API.G_FALSE
1017       , p_validation_flag           => FND_API.G_TRUE
1018       , x_quantity_reserved         => l_quantity_reserved
1019       , x_secondary_quantity_reserved  => l_quantity2_reserved -- INVCONV
1020       , x_reservation_id            => l_rsv_id
1021       , p_partial_rsv_exists        => p_rsv_update
1022       , p_substitute_flag           => l_substitute_flag  -- Added for ER 6110708
1023       );
1024 
1025       IF l_debug_level  > 0 THEN
1026          oe_debug_pub.add(  '2. AFTER CALLING CREATE RESERVATION' || X_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
1027       END IF;
1028       IF l_debug_level  > 0 THEN
1029           oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
1030       END IF;
1031 
1032       -- Bug No:2097933
1033       -- If the Reservation was succesfull we set
1034       -- the package variable to "Y".
1035       IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN
1036          OESCH_PERFORMED_RESERVATION := 'Y';
1037       END IF;
1038 
1039       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1040          -- 4091185
1041          -- Messages are being handled in exceptions block
1042          -- oe_msg_pub.transfer_msg_stack;
1043          -- l_msg_count:=OE_MSG_PUB.COUNT_MSG;
1044          -- for I in 1..l_msg_count loop
1045          --     l_msg_data := OE_MSG_PUB.Get(I,'F');
1046          --     IF l_debug_level  > 0 THEN
1047          --         oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
1048          --     END IF;
1049          -- end loop;
1050          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1051       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
1052           -- 4091185
1053           oe_msg_pub.transfer_msg_stack(p_type => 'WARNING');
1054           l_msg_count:=OE_MSG_PUB.COUNT_MSG;
1055           for I in 1..l_msg_count loop
1056               l_msg_data := OE_MSG_PUB.Get(I,'F');
1057               IF l_debug_level  > 0 THEN
1058                   oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
1059               END IF;
1060           end loop;
1061           IF p_line_rec.Schedule_action_code = OESCH_ACT_RESERVE THEN
1062              RAISE FND_API.G_EXC_ERROR;
1063           ELSE
1064              x_return_status := FND_API.G_RET_STS_SUCCESS;
1065              IF l_debug_level  > 0 THEN
1066                  oe_debug_pub.add(  'UNABLE TO RESERVE' , 2 ) ;
1067              END IF;
1068           END IF;
1069 
1070        END IF;
1071 
1072    END IF;
1073 
1074      IF l_debug_level  > 0 THEN
1075          oe_debug_pub.add(  'AFTER CALLING INVS CREATE_RESERVATION' || X_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
1076      END IF;
1077 
1078 EXCEPTION
1079   WHEN FND_API.G_EXC_ERROR THEN
1080     x_return_status := FND_API.G_RET_STS_ERROR;
1081 
1082   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1083     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1084 
1085      oe_msg_pub.transfer_msg_stack;
1086      l_msg_count:=OE_MSG_PUB.COUNT_MSG;
1087      for I in 1..l_msg_count loop
1088         l_msg_data := OE_MSG_PUB.Get(I,'F');
1089         -- 4091185
1090         -- oe_msg_pub.transfer_msg_stack has already added messages.
1091         -- oe_msg_pub.add_text(l_msg_data);
1092         IF l_debug_level  > 0 THEN
1093             oe_debug_pub.add(  'INV : ' || L_MSG_DATA , 2 ) ;
1094         END IF;
1095      end loop;
1096 
1097   WHEN OTHERS THEN
1098     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1099 
1100     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1101     THEN
1102       OE_MSG_PUB.Add_Exc_Msg
1103       (   G_PKG_NAME
1104       ,   'Reserve_line'
1105        );
1106     END IF;
1107 
1108 END Reserve_Line;
1109 /*---------------------------------------------------------------------
1110 Procedure Name : Unreserve_Line
1111 Description    : This API calls Inventory's APIs to Unreserve. It first
1112                  queries the reservation records, and then calls
1113                  delete_reservations until the p_quantity_to_unreserve
1114                  is satisfied.
1115 --------------------------------------------------------------------- */
1116 
1117 Procedure Unreserve_Line
1118 ( p_line_rec              IN  OE_ORDER_PUB.Line_Rec_Type
1119  ,p_old_ship_from_org_id IN NUMBER DEFAULT NULL -- 6628134
1120  ,p_quantity_to_unreserve IN  NUMBER
1121  ,p_quantity2_to_unreserve IN  NUMBER  DEFAULT NULL -- INVCONV
1122 ,x_return_status OUT NOCOPY VARCHAR2)
1123 
1124 IS
1125   l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
1126   l_rsv_new_rec           inv_reservation_global.mtl_reservation_rec_type;
1127   l_msg_count             NUMBER;
1128   l_msg_data              VARCHAR2(240);
1129   l_rsv_id                NUMBER;
1130   l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
1131   l_count                 NUMBER;
1132   l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
1133   l_qty_to_unreserve      NUMBER;
1134   l_source_code           VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
1135   l_sales_order_id        NUMBER;
1136   l_x_error_code          NUMBER;
1137   l_lock_records          VARCHAR2(1);
1138   l_sort_by_req_date      NUMBER ;
1139   l_buffer                VARCHAR2(2000);
1140 
1141    l_quantity2_to_UNreserve NUMBER; -- INVCONV
1142 
1143 --
1144 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1145 --
1146 BEGIN
1147 
1148   IF l_debug_level  > 0 THEN
1149       oe_debug_pub.add(  'ENTERING UNRESERVE LINE' , 3 ) ;
1150   END IF;
1151   IF l_debug_level  > 0 THEN
1152       oe_debug_pub.add(  'QUANTITY TO UNRESERVE : ' || P_QUANTITY_TO_UNRESERVE , 3 ) ;
1153             oe_debug_pub.add(  'QUANTITY2 TO UNRESERVE : ' || P_QUANTITY2_TO_UNRESERVE , 3 ) ;
1154   END IF;
1155 
1156   /* Bug 6335352
1157      OM will handle Resevrations both before and after Shipping Interface is done.
1158   IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y' THEN
1159      IF l_debug_level  > 0 THEN
1160          oe_debug_pub.add(  'LINE HAS BEEN SHIPPING INTERFACED' , 3 ) ;
1161      END IF;
1162      goto end_of_loop;
1163   END IF;
1164   */
1165   -- Start 2595661
1166   --  All codes except shipping interface check have been moved to Procedure Do_Unreserve
1167 
1168   l_quantity2_to_UNreserve := p_quantity2_to_UNreserve; -- INVCONV
1169   IF p_quantity2_to_UNreserve = 0 -- INVCONV
1170         then
1171           l_quantity2_to_UNreserve := null;
1172   END IF;
1173 
1174   Do_Unreserve
1175         ( p_line_rec               => p_line_rec
1176         , p_quantity_to_unreserve  => p_quantity_to_unreserve
1177         , p_quantity2_to_unreserve  => l_quantity2_to_unreserve -- INVCONV
1178         , p_old_ship_from_org_id    => p_old_ship_from_org_id -- 6628134
1179         , x_return_status          => x_return_status);
1180   -- End 2595661
1181 
1182   <<end_of_loop>>
1183 
1184   IF l_debug_level  > 0 THEN
1185       oe_debug_pub.add(  'EXITING UNRESERVE_LINES' , 3 ) ;
1186   END IF;
1187 
1188 EXCEPTION
1189   WHEN FND_API.G_EXC_ERROR THEN
1190     x_return_status := FND_API.G_RET_STS_ERROR;
1191 
1192   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1193     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1194 
1195   WHEN OTHERS THEN
1196     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1197 
1198     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1199     THEN
1200       OE_MSG_PUB.Add_Exc_Msg
1201       (   G_PKG_NAME
1202       ,   'Unreserve_line'
1203        );
1204     END IF;
1205 
1206 END Unreserve_Line;
1207 
1208 
1209 
1210 /* ---------------------------------------------------------------
1211 FUNCTION  : Valid_Set_Addition
1212 Description: This function is called to make sure the model/kit which is
1213              getting added to set should not have lines overridden for
1214              differenr dates.
1215  ---------------------------------------------------------------*/
1216 FUNCTION Valid_Set_Addition(p_top_model_line_id IN NUMBER ,
1217                             p_set_type          IN VARCHAR2)
1218 RETURN BOOLEAN
1219 IS
1220 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1221 l_count       NUMBER := 0;
1222 BEGIN
1223 
1224    IF l_debug_level  > 0 THEN
1225        oe_debug_pub.add(  'p_top_model_line_id :' || p_top_model_line_id , 2 ) ;
1226        oe_debug_pub.add(  'p_set_type :' || P_SET_TYPE , 2 ) ;
1227    END IF;
1228 
1229 
1230    IF p_set_type = 'SHIP_SET' THEN
1231 
1232      SELECT count(a.line_id)
1233      INTO   l_count
1234      FROM oe_order_lines_all a,
1235           oe_order_lines_all b
1236      WHERE a.top_model_line_id = p_top_model_line_id
1237      AND a.top_model_line_id = b.top_model_line_id
1238      AND a.override_atp_date_code = 'Y'
1239      AND b.override_atp_date_code = 'Y'
1240      AND a.schedule_ship_date <> b.schedule_ship_date;
1241 
1242    ELSIF p_set_type = 'ARRIVAL_SET' THEN
1243 
1244      SELECT count(a.line_id)
1245      INTO   l_count
1246      FROM oe_order_lines_all a,
1247           oe_order_lines_all b
1248      WHERE a.top_model_line_id = p_top_model_line_id
1249      AND a.top_model_line_id = b.top_model_line_id
1250      AND a.override_atp_date_code = 'Y'
1251      AND b.override_atp_date_code = 'Y'
1252      AND a.schedule_arrival_date <> b.schedule_arrival_date;
1253 
1254    END IF;
1255 
1256    IF l_debug_level  > 0 THEN
1257     oe_debug_pub.add('count: ' || l_count,2);
1258    END IF;
1259    IF l_count = 0 THEN
1260 
1261       RETURN TRUE;
1262 
1263    ELSE
1264 
1265       RETURN FALSE;
1266 
1267    END IF;
1268 EXCEPTION
1269 
1270   WHEN OTHERS THEN
1271 
1272    IF l_debug_level  > 0 THEN
1273     oe_debug_pub.add('return false from Valid_Set_Addition');
1274    END IF;
1275     RETURN FALSE;
1276 
1277 END Valid_Set_Addition;
1278 /* ---------------------------------------------------------------
1279 FUNCTION  : Set_Attr_Matched
1280 Description: This function is called to compare set and line record
1281              for set attributes. This will help in avoiding additional call
1282              to MRP if the line is scheduled for a same set attributes.
1283  ---------------------------------------------------------------*/
1284 FUNCTION Set_Attr_Matched(p_set_ship_from_org_id IN NUMBER ,
1285                           p_line_ship_from_org_id IN NUMBER,
1286                           p_set_ship_to_org_id IN NUMBER,
1287                           p_line_ship_to_org_id IN NUMBER,
1288                           p_set_schedule_ship_date IN DATE,
1289                           p_line_schedule_ship_date IN DATE,
1290                           p_set_arrival_date IN DATE,
1291                           p_line_arrival_date IN DATE,
1292                           p_set_shipping_method_code IN VARCHAR2,
1293                           p_line_shipping_method_code IN VARCHAR2,
1294                           p_set_type IN VARCHAR2)
1295 RETURN BOOLEAN
1296 IS
1297 
1298 --
1299 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1300 --
1301 BEGIN
1302 
1303    IF l_debug_level  > 0 THEN
1304        oe_debug_pub.add(  'P_SET_TYPE :' || P_SET_TYPE , 2 ) ;
1305        oe_debug_pub.add(  'P_LINE_SHIP_FROM_ORG_ID :' || P_LINE_SHIP_FROM_ORG_ID , 2 ) ;
1306        oe_debug_pub.add(  'P_SET_SHIP_FROM_ORG_ID :' || P_SET_SHIP_FROM_ORG_ID , 2 ) ;
1307        oe_debug_pub.add(  'P_LINE_SHIP_TO_ORG_ID :' || P_LINE_SHIP_TO_ORG_ID , 2 ) ;
1308        oe_debug_pub.add(  'P_SET_SHIP_TO_ORG_ID :' || P_SET_SHIP_TO_ORG_ID , 2 ) ;
1309        oe_debug_pub.add(  'P_LINE_SCHEDULE_SHIP_DATE :' || P_LINE_SCHEDULE_SHIP_DATE , 2 ) ;
1310        oe_debug_pub.add(  'P_SET_SCHEDULE_SHIP_DATE :' || P_SET_SCHEDULE_SHIP_DATE , 2 ) ;
1311        oe_debug_pub.add(  'P_LINE_ARRIVAL_DATE :' || P_LINE_ARRIVAL_DATE , 2 ) ;
1312        oe_debug_pub.add(  'P_SET_ARRIVAL_DATE :' || P_SET_ARRIVAL_DATE , 2 ) ;
1313    END IF;
1314 
1315    IF (p_set_type = 'SHIP_SET' AND
1316        p_line_ship_from_org_id  = p_set_ship_from_org_id   AND
1317        p_line_ship_to_org_id     = p_set_Ship_To_Org_Id     AND
1318        p_line_schedule_ship_date = p_set_schedule_ship_date) THEN
1319 
1320 /* Modified the above line and added followinf 7 lines to fix the bug 3393033 */
1321       IF NVL(fnd_profile.value('ONT_SHIP_METHOD_FOR_SHIP_SET'),'N') = 'Y' THEN
1322        -- 3878494
1323        IF  OE_GLOBALS.equal(p_line_shipping_method_code,p_set_shipping_method_code) THEN
1324            RETURN TRUE;
1325        END IF;
1326       ELSE -- profile
1327        RETURN TRUE;
1328       END IF;
1329 
1330 /* Modified the following line to fix the bug 3393033 */
1331    ELSIF (p_set_type = 'ARRIVAL_SET' AND
1332        p_line_ship_to_org_id     = p_set_ship_to_org_id     AND
1333        p_line_arrival_date       = p_set_arrival_date) THEN
1334 
1335        RETURN TRUE;
1336 
1337    END IF;
1338 
1339    IF l_debug_level  > 0 THEN
1340        oe_debug_pub.add(  'EXITING CAMPARE ATTR' , 3 ) ;
1341    END IF;
1342    RETURN FALSE;
1343 
1344 EXCEPTION
1345 
1346   WHEN OTHERS THEN
1347 
1348    IF l_debug_level  > 0 THEN
1349     oe_debug_pub.add('return false from debug');
1350    END IF;
1351     RETURN FALSE;
1352 
1353 END Set_Attr_Matched;
1354 
1355 -- 4026758
1356 /* ----------------------------------------------------------------------
1357 Procedure Log_Delete_Set_Request
1358 Set_id will be deleted
1359 1. When the line gets removed from the set.
1360 2. When the line gets deleted, which is part of the set.
1361 3. When line gets cancelled.
1362 -------------------------------------------------------------------------*/
1363 Procedure Log_Delete_Set_Request
1364 (p_header_id IN NUMBER,
1365  p_line_id   IN NUMBER,
1366  p_set_id    IN NUMBER,
1367  x_return_status OUT NOCOPY VARCHAR2)
1368 
1369 IS
1370 l_set_type   VARCHAR2(30);
1371 --
1372 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1373 --
1374 BEGIN
1375 
1376    IF l_debug_level  > 0 THEN
1377      oe_debug_pub.add(  'ENTERING LOG DELETE SET REQUEST' || P_SET_ID , 1 );
1378    END IF;
1379 
1380    SELECT set_type
1381    INTO l_set_type
1382    FROM oe_sets
1383    WHERE  set_id = p_set_id;
1384 
1385    OE_delayed_requests_Pvt.log_request(
1386    p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
1387    p_entity_id              => p_line_id,
1388    p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
1389    p_requesting_entity_id   => p_line_id,
1390    p_request_type           => OE_GLOBALS.G_DELETE_SET,
1391    p_param1                 => p_set_id,
1392    p_param2                 => p_header_id,
1393    p_param3                 => l_set_type,
1394    x_return_status          => x_return_status);
1395    IF l_debug_level  > 0 THEN
1396       oe_debug_pub.add(  'EXITING LOG DELETE SET REQUEST '||x_return_status , 1 ) ;
1397    END IF;
1398 
1399 EXCEPTION
1400    WHEN FND_API.G_EXC_ERROR THEN
1401 
1402         x_return_status := FND_API.G_RET_STS_ERROR;
1403 
1404    WHEN OTHERS THEN
1405 
1406         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1407 
1408         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1409         THEN
1410             OE_MSG_PUB.Add_Exc_Msg
1411             (   G_PKG_NAME,
1412               'Log_Delete_Set_Request');
1413         END IF;
1414 END Log_Delete_Set_Request;
1415 
1416 /* ----------------------------------------------------------------------
1417 Procedure Log_Set_Request
1418 1. Set will populate set id when set name is populated by user.
1419 2. log delayed request when a line is added to set.
1420 3. Log delayed request when scheduling related attribute changed on
1421    on the line which belong to set.
1422 3. Execute all delayed requests at once.
1423 
1424  Need to take care logging delyed request when action is passed on the
1425  line. Action can be passed by scheduling workflow or order import call.
1426 
1427   If the line is part of ato model/line  or SMC  model then log group request.
1428   Other wise log a schedule or reschedule delayed request.
1429 
1430 -------------------------------------------------------------------------*/
1431 Procedure Log_Set_Request
1432 (p_line_rec       IN OE_ORDER_PUB.Line_rec_type,
1433  p_old_line_rec   IN OE_ORDER_PUB.Line_rec_type,
1434  p_sch_action     IN VARCHAR2,
1435  p_caller         IN VARCHAR2,
1436 x_return_status OUT NOCOPY VARCHAR2)
1437 
1438 IS
1439 l_group_request        BOOLEAN;
1440 l_action               VARCHAR2(30);
1441 l_schedule_ship_date    DATE := Null;
1442 l_schedule_arrival_date DATE := Null;
1443 l_param1               NUMBER;
1444 l_ship_to_org_id       NUMBER := Null;
1445 l_ship_from_org_id     NUMBER := Null;
1446 l_entity_type          VARCHAR2(30);
1447 
1448 l_set_schedule_ship_date    DATE;
1449 l_set_schedule_arrival_date DATE;
1450 l_set_ship_to_org_id        NUMBER;
1451 l_set_ship_from_org_id      NUMBER;
1452 l_set_status                VARCHAR2(1);
1453 l_set_type                  VARCHAR2(30);
1454 l_matched                   BOOLEAN := FALSE;
1455 l_param12                   VARCHAR2(1);
1456 l_shipping_method_code      VARCHAR2(30);
1457 /* Added the following 1 line to fix the bug 2740480 */
1458 l_push_logic                VARCHAR2(1) := 'N';
1459 /* Added the following 1 line to fix the bug 3393033 */
1460 l_set_shipping_method_code      VARCHAR2(30);
1461 
1462 -- 2391781
1463 l_cascade_line_id           NUMBER := NULL;
1464 l_line_id                   NUMBER := NULL;
1465 
1466 l_order_date_type_code      VARCHAR2(30);
1467 --
1468 --3314157
1469 l_operation         VARCHAR2(30);
1470 --3344843
1471 l_diff_res_qty              NUMBER;
1472 l_qty_to_reserve            NUMBER;
1473 l_qty_to_unreserve          NUMBER;
1474 l_index                     NUMBER;
1475 l_line_id_mod               NUMBER;  --7827737
1476 --
1477 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
1478 --
1479 BEGIN
1480 
1481  -- If set does not exists, the the first time the lines are getting added
1482  -- to set. Do a group schedule. Other wise log a delayed request based on
1483  -- ato or smc.
1484    IF l_debug_level  > 0 THEN
1485        oe_debug_pub.add(  'ENTERING LOG SET REQUEST' || P_LINE_REC.SHIP_SET_ID , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
1486    END IF;
1487  BEGIN
1488 
1489 
1490 /* Removed ship_method_code from the following select , to fix the bug 2916814 */
1491 
1492   Select schedule_ship_date,ship_to_org_id,ship_from_org_id,
1493          schedule_arrival_date,shipping_method_code,
1494          set_status, set_type
1495   Into   l_schedule_ship_date,l_ship_to_org_id,l_ship_from_org_id,
1496          l_schedule_arrival_date,l_shipping_method_code,
1497          l_set_status, l_set_type
1498   From   oe_sets
1499   Where  set_id = nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id);
1500 
1501    IF l_debug_level  > 0 THEN
1502        oe_debug_pub.add(  'SET SCHEDULE DATE'|| L_SCHEDULE_SHIP_DATE , 1 ) ;
1503    END IF;
1504  EXCEPTION
1505    WHEN OTHERS THEN
1506      l_schedule_ship_date := null;
1507  END;
1508 
1509  l_set_schedule_ship_date    := l_schedule_ship_date;
1510  l_set_schedule_arrival_date := l_schedule_arrival_date;
1511  l_set_ship_to_org_id        := l_ship_to_org_id;
1512  l_set_ship_from_org_id      := l_ship_from_org_id;
1513  l_set_shipping_method_code  := l_shipping_method_code;
1514 
1515  IF l_schedule_ship_date is null
1516  AND NVL(fnd_profile.value('ONT_SET_FOR_EACH_LINE'),'N') = 'Y'
1517  AND  p_line_rec.schedule_status_code is null
1518  AND  l_set_status = 'T'  THEN
1519 
1520   l_action :=  OESCH_ACT_SCHEDULE;
1521 
1522     -- 4188166
1523     -- Log request if no request logged for the set id. Also remember ship_to_org_id
1524     -- and ship_from_org_id for  cascading
1525     IF NOT OE_Delayed_Requests_PVT.Check_for_Request
1526     (   p_entity_code  =>OE_GLOBALS.G_ENTITY_LINE
1527     ,   p_entity_id    =>nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id)
1528     ,   p_request_type =>OE_GLOBALS.G_GROUP_SET)  THEN
1529 
1530       OE_delayed_requests_Pvt.log_request(
1531       p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
1532       p_entity_id              => nvl(p_line_rec.ship_set_id,p_line_rec.arrival_set_id),
1533       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
1534       p_requesting_entity_id   => p_line_rec.line_id,
1535       p_request_type           => OE_GLOBALS.G_GROUP_SET,
1536       p_param1                 => l_set_type,
1537       p_param2                 => p_line_rec.header_id,
1538       p_param3                 => p_line_rec.line_id,
1539       p_param4                 => p_line_rec.top_model_line_id,
1540       x_return_status          => x_return_status);
1541    END IF;
1542 
1543 
1544   GOTO END_PROCESS;
1545 
1546  END IF;
1547 
1548  IF l_schedule_ship_date is null
1549  OR p_line_rec.schedule_action_code is not null
1550  THEN
1551   --  Log a group request.
1552 /*
1553    IF p_line_rec.schedule_status_code is not null THEN --2369951
1554 
1555      oe_debug_pub.add('1 Setting schedule line',2);
1556 
1557      l_schedule_ship_date    := p_line_rec.schedule_ship_date;
1558      l_schedule_arrival_date := p_line_rec.schedule_arrival_date;
1559      l_ship_from_org_id      := p_line_rec.ship_from_org_id;
1560      l_ship_to_org_id        := p_line_rec.ship_to_org_id;
1561 
1562      l_action := OESCH_ACT_RESCHEDULE;
1563 
1564    ELSE
1565      oe_debug_pub.add('1 Setting group schedule',2);
1566      l_group_request := TRUE;
1567      l_action := OESCH_ACT_SCHEDULE;
1568    END IF;
1569 */
1570    IF l_debug_level  > 0 THEN
1571        oe_debug_pub.add(  '1 SETTING GROUP SCHEDULE' , 2 ) ;
1572    END IF;
1573    l_group_request := TRUE;
1574    l_action := OESCH_ACT_RESCHEDULE;
1575 
1576 
1577 
1578  ELSIF OE_GLOBALS.Equal(p_line_rec.ship_set_id,
1579                         p_old_line_rec.ship_set_id)
1580  AND   OE_GLOBALS.Equal(p_line_rec.arrival_set_id,
1581                         p_old_line_rec.arrival_set_id)
1582  AND   p_line_rec.schedule_status_code is not null
1583  THEN
1584 
1585    -- some attribute changed in the line which belongs to set.
1586    -- Log group request.
1587     IF l_debug_level  > 0 THEN
1588         oe_debug_pub.add(  '2 SETTING GROUP SCHEDULE' , 2 ) ;
1589     END IF;
1590     l_group_request := TRUE;
1591     l_action := OESCH_ACT_RESCHEDULE;
1592     l_order_date_type_code :=
1593             NVL(Get_Date_Type(p_line_rec.header_id), 'SHIP'); -- 2920081
1594 
1595      --User has changed some attribute on the set.
1596      --Log a group request and send the changed information to
1597      --deleyed request.
1598 
1599 
1600     IF l_ship_from_org_id is not null THEN
1601        IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
1602                                p_old_line_rec.ship_from_org_id)
1603        THEN
1604           l_ship_from_org_id := p_line_rec.ship_from_org_id;
1605        END IF;
1606     END IF;
1607 
1608     IF l_ship_to_org_id is not null THEN
1609        IF NOT OE_GLOBALS.Equal(p_line_rec.ship_to_org_id,
1610                                p_old_line_rec.ship_to_org_id)
1611        THEN
1612           l_ship_to_org_id := p_line_rec.ship_to_org_id;
1613        END IF;
1614     END IF;
1615 
1616     IF l_schedule_ship_date is not null THEN
1617 
1618        IF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1619                                p_old_line_rec.request_date)
1620        AND NOT SET_OVERRIDDEN(p_header_id => p_line_rec.header_id
1621                              ,p_ship_set_id => p_line_rec.ship_set_id
1622                              ,p_arrival_set_id => p_line_rec.arrival_set_id)
1623        THEN
1624           IF l_order_date_type_code ='SHIP'
1625           THEN -- 2920081
1626              --Bug 6057897
1627              --Request date change should be considered for doing re-scheudling of the set based on the parameter
1628              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1629              THEN
1630                 l_schedule_ship_date := p_line_rec.request_date;
1631              END IF;
1632           ELSE
1633              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1634              THEN
1635                 l_schedule_arrival_date := p_line_rec.request_date;
1636              END IF;
1637           END IF;
1638          -- l_schedule_ship_date := p_line_rec.request_date;
1639        END IF;
1640 
1641        IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
1642                                p_old_line_rec.schedule_ship_date)
1643        THEN
1644           l_schedule_ship_date := p_line_rec.schedule_ship_date;
1645        END IF;
1646     END IF;
1647 
1648     IF l_schedule_arrival_date is not null THEN
1649 
1650        IF p_line_rec.arrival_set_id is not null
1651        AND NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1652                                 p_old_line_rec.request_date)
1653        AND NOT SET_OVERRIDDEN(p_header_id      => p_line_rec.header_id
1654                              ,p_ship_set_id    => p_line_rec.ship_set_id
1655                              ,p_arrival_set_id => p_line_rec.arrival_set_id)
1656        THEN
1657           IF l_order_date_type_code ='SHIP'
1658           THEN -- 2920081
1659              --Bug 6057897
1660              --Request date change should be considered for doing re-scheudling of the set based on the parameter
1661              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1662              THEN
1663                 l_schedule_ship_date := p_line_rec.request_date;
1664              END IF;
1665           ELSE
1666              IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
1667              THEN
1668                 l_schedule_arrival_date := p_line_rec.request_date;
1669              END IF;
1670           END IF;
1671         --l_schedule_arrival_date := p_line_rec.request_date;
1672        END IF;
1673 
1674        IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
1675                                p_old_line_rec.schedule_arrival_date)
1676        THEN
1677           l_schedule_arrival_date := p_line_rec.schedule_arrival_date;
1678        END IF;
1679     END IF;
1680 
1681     IF NOT OE_GLOBALS.Equal(p_line_rec.shipping_method_code ,
1682                                p_old_line_rec.shipping_method_code )
1683     THEN
1684           l_shipping_method_code := p_line_rec.shipping_method_code ;
1685     END IF;
1686 
1687 
1688     -- 2391781,2787962
1689     -- Warehouse change only for arrival set
1690 
1691     IF  (NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
1692                               p_old_line_rec.ship_from_org_id) AND
1693          (NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y' OR
1694           p_line_rec.ato_line_id is not null) AND
1695           p_line_rec.arrival_set_id is not null)
1696     OR  NOT OE_GLOBALS.Equal(p_line_rec.request_date,
1697                              p_old_line_rec.request_date)
1698     THEN
1699 
1700        IF  NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y'
1701        OR  NVL(p_line_rec.top_model_line_id ,-99) = p_line_rec.line_id
1702        THEN
1703 
1704           l_cascade_line_id := p_line_rec.top_model_line_id;
1705           IF l_debug_level  > 0 THEN
1706            oe_debug_pub.add('Going to Log Cascade warehouse for SMC Model',2);
1707           END IF;
1708 
1709        ELSIF p_line_rec.ato_line_id is not null
1710        AND   NOT(p_line_rec.ato_line_id = p_line_rec.line_id
1711        AND   p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
1712                                            OE_GLOBALS.G_ITEM_OPTION,
1713 					   OE_GLOBALS.G_ITEM_INCLUDED)) --9775352
1714        THEN
1715 
1716           IF l_debug_level  > 0 THEN
1717            oe_debug_pub.add('Going to Log Cascade warehouse for ato Model',2);
1718           END IF;
1719 
1720           l_cascade_line_id := p_line_rec.ato_line_id;
1721 
1722        ELSIF p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CLASS THEN
1723           l_cascade_line_id := p_line_rec.line_id;
1724        END IF;
1725     END IF;
1726     -- 2391781, 2787962
1727 
1728 
1729  ELSE
1730 
1731   --   Log a request based on the type of the line.
1732 
1733 
1734     IF  ((p_line_rec.ato_line_id is not null AND
1735          NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
1736               p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
1737                                             OE_GLOBALS.G_ITEM_OPTION,
1738 					    OE_GLOBALS.G_ITEM_INCLUDED))) --9775352
1739     OR  (p_line_rec.line_id = p_line_rec.top_model_line_id))
1740     AND  NOT (OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
1741          AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 )
1742     THEN
1743 
1744     IF l_debug_level  > 0 THEN
1745         oe_debug_pub.add(  '3 SETTING GROUP SCHEDULE' , 2 ) ;
1746     END IF;
1747 
1748 /* Added the following 1 line to fix the bug 2740480 */
1749      l_push_logic := 'Y';
1750 
1751      l_group_request := TRUE;
1752      l_action := OESCH_ACT_RESCHEDULE;
1753      -- Scheduling action would be reschedule.
1754 
1755     ELSE
1756 
1757     IF l_debug_level  > 0 THEN
1758         oe_debug_pub.add(  '4 SCHEDULE LINE' , 2 ) ;
1759     END IF;
1760      /*changes for bug 6719457*/
1761       IF l_debug_level  > 0 THEN
1762         oe_debug_pub.add(  'Checking if Group_Schedule delayed request already exists for the line' , 2 ) ;
1763     END IF;
1764 
1765       IF OE_Delayed_Requests_PVT.Check_for_Request
1766      (   p_entity_code  =>OE_GLOBALS.G_ENTITY_ALL
1767      ,   p_entity_id    =>p_line_rec.line_id
1768      ,   p_request_type =>OE_GLOBALS.G_GROUP_SCHEDULE)  THEN
1769 
1770         l_group_request := TRUE;
1771      ELSE
1772           l_group_request := FALSE;
1773      END IF;
1774 
1775  /*Changes for bug 6719457*/
1776 
1777      -- scheduling action would be schedule or re-schedule
1778      -- based on the scheduling status.
1779 
1780      IF p_line_rec.schedule_status_code is not null THEN
1781         l_action := OESCH_ACT_RESCHEDULE;
1782      ELSE
1783         l_action := OESCH_ACT_SCHEDULE;
1784      END IF;
1785 
1786      -- scheduling action would be schedule or re-schedule
1787      -- based on the scheduling status.
1788 
1789     END IF;
1790 
1791  END IF;
1792 
1793  IF p_line_rec.arrival_set_id is not null THEN
1794     l_param1      := p_line_rec.arrival_set_id;
1795     l_entity_type := OESCH_ENTITY_ARRIVAL_SET;
1796  ELSE
1797      l_param1      := p_line_rec.ship_set_id;
1798      l_entity_type := OESCH_ENTITY_SHIP_SET;
1799  END IF;
1800 
1801 
1802  -- Store the old inventory item to pass it to
1803  -- MRP during rescheduling the scheduled item.
1804 
1805  IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
1806                          p_old_line_rec.inventory_item_id)
1807  AND p_line_rec.schedule_status_code IS NOT NULL THEN
1808 
1809   l_line_id_mod := MOD(p_line_rec.line_id,G_BINARY_LIMIT); --7827737
1810  -- OE_Item_Tbl(p_line_rec.line_id).line_id --7827737
1811   --                  := p_line_rec.line_id;
1812 
1813   OE_Item_Tbl(l_line_id_mod).line_id  --7827737
1814                    := p_line_rec.line_id;
1815 
1816   --OE_Item_Tbl(p_line_rec.line_id).inventory_item_id  --7827737
1817   --                  := p_old_line_rec.inventory_item_id;
1818 
1819   OE_Item_Tbl(l_line_id_mod).inventory_item_id --7827737
1820                    := p_old_line_rec.inventory_item_id;
1821  END IF;
1822  -- 3384975
1823  l_param12 := 'N';
1824 
1825  -- set the variable 12 when scheduling attributes changed on a
1826  -- scheduled line.
1827  --- 3344843 Also set when reserved quantity changed
1828 
1829  IF  (p_line_rec.schedule_status_code is NOT NULL
1830   AND (Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1831                                 p_old_line_rec => p_old_line_rec)
1832    OR  p_line_rec.ordered_quantity <> p_old_line_rec.ordered_quantity
1833    OR  ( p_line_rec.ordered_quantity2 <> p_old_line_rec.ordered_quantity2
1834           AND  Nvl(p_line_rec.fulfillment_base ,'P') ='S'
1835         )-- bug 16782621
1836    OR  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1837                              p_old_line_rec.override_atp_date_code)
1838    OR NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1839                            p_old_line_rec.reserved_quantity)
1840       ))
1841    OR  p_line_rec.schedule_action_code is not null
1842  THEN
1843 
1844     IF l_debug_level  > 0 THEN
1845        oe_debug_pub.add(' Schedule Action Code '||p_line_rec.schedule_action_code,1);
1846        oe_debug_pub.add(  'SET L PARAM 12' , 4 ) ;
1847     END IF;
1848     l_param12 := 'Y';
1849 
1850  END IF;
1851 
1852 
1853  IF l_group_request THEN
1854 
1855     IF l_debug_level  > 0 THEN
1856         oe_debug_pub.add(  'BEFORE LOGGING G_GROUP_SCHEDULE' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
1857     END IF;
1858     -- Log group set. which will execute a group schedule.
1859    /* -- 3384975 moved out of l_group_request check
1860     l_param12 := 'N';
1861 
1862     -- set the variable 12 when scheduling attributes changed on a
1863     -- scheduled line.
1864     --- 3344843 Also set when reserved quantity changed
1865 
1866     IF  p_line_rec.schedule_status_code is NOT NULL
1867     AND (Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1868                                     p_old_line_rec => p_old_line_rec)
1869     OR  p_line_rec.ordered_quantity <> p_old_line_rec.ordered_quantity
1870     OR  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1871                              p_old_line_rec.override_atp_date_code)
1872     OR NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1873                            p_old_line_rec.reserved_quantity))
1874     THEN
1875 
1876        IF l_debug_level  > 0 THEN
1877            oe_debug_pub.add(  'SET L PARAM 12' , 4 ) ;
1878        END IF;
1879        l_param12 := 'Y';
1880 
1881     END IF;
1882    */
1883     --for bug 3314157
1884     IF OE_QUOTE_UTIL.G_COMPLETE_NEG='Y'
1885     THEN
1886        l_operation:=OE_GLOBALS.G_OPR_CREATE;
1887     ELSE
1888        l_operation:= p_line_rec.operation;
1889     END IF;
1890     -- 3586151 OE_GLOBALS.G_ENTITY_LINE is replaced by OE_GLOBALS.G_ENTITY_ALL for p_entity_code to execute at the end.
1891     OE_delayed_requests_Pvt.log_request
1892      (p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,  --OE_GLOBALS.G_ENTITY_LINE,
1893       p_entity_id              => p_line_rec.line_id,
1894       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
1895       p_requesting_entity_id   => p_line_rec.line_id,
1896       p_request_type           => OE_GLOBALS.G_GROUP_SCHEDULE,
1897       p_param1                 => l_param1,
1898       p_param2                 => p_line_rec.header_id,
1899       p_param3                 => l_action,
1900       p_param4                 => p_old_line_rec.ship_from_org_id,
1901       p_param5                 => p_old_line_rec.ship_to_org_id,
1902       p_date_param1            => p_old_line_rec.schedule_ship_date,
1903       p_date_param2            => p_old_line_rec.schedule_arrival_date,
1904       p_date_param3            => p_old_line_rec.request_date,
1905       p_date_param4            => l_schedule_ship_date,
1906       p_date_param5            => l_schedule_arrival_date,
1907       p_param6                 => p_old_line_rec.ship_set_id,
1908       p_param7                 => p_old_line_rec.arrival_set_id,
1909       p_param8                 => l_entity_type,
1910       p_param9                 => l_ship_to_org_id,
1911       p_param10                => l_ship_from_org_id,
1912       p_param11                => nvl(l_shipping_method_code,p_line_rec.shipping_method_code),
1913 /* removed param11 to fix the bug 2916814 */
1914       p_param12                => l_param12,
1915 /* Added the following 1 line to fix the bug 2740480 */
1916       p_param13                => l_push_logic,
1917     /*REplaced  p_line_rec.operation with l_operation as a fix for bug 3314157*/
1918       p_param14                => l_operation,
1919       x_return_status          => x_return_status);
1920 
1921 
1922    -- 2391781
1923    IF l_cascade_line_id IS NOT NULL
1924    THEN
1925       IF l_debug_level  > 0 THEN
1926          oe_debug_pub.add(  'Logging cascade warehouse / Request date ' , 4 ) ;
1927       END IF;
1928       OE_delayed_requests_Pvt.log_request
1929          (p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
1930           p_entity_id              => l_cascade_line_id,
1931           p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
1932           p_requesting_entity_id   => l_cascade_line_id,
1933           p_request_type           => OE_GLOBALS.G_CASCADE_SCH_ATTRBS,
1934           p_param1                 => NVL(p_line_rec.arrival_set_id,
1935                                           p_line_rec.ship_set_id),
1936           p_param2                 => p_line_rec.ship_from_org_id,
1937           p_date_param1            => p_line_rec.request_date,
1938           x_return_status          => x_return_status);
1939 
1940 
1941    END IF;
1942    -- 2391781
1943 
1944 
1945 
1946  ELSE
1947 
1948     -- Log schedule or reschedule based on the action.
1949 
1950     -- See of the line is scheduled and getting added to set with
1951     -- same scheduling and set attributes, avoid logging the request.
1952     -- 3384975 Override_atp_date_code check added
1953 
1954     IF p_line_rec.schedule_status_code IS NOT NULL AND
1955     NOT Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
1956                                    p_old_line_rec => p_old_line_rec)
1957     AND     OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
1958                              p_old_line_rec.override_atp_date_code)
1959     AND p_line_rec.ordered_quantity = p_old_line_rec.ordered_quantity
1960     AND (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD OR
1961         nvl(p_line_rec.model_remnant_flag,'N') = 'Y') THEN
1962 
1963         IF l_debug_level  > 0 THEN
1964             oe_debug_pub.add(  'ARRIVAL_SET_ID : ' || P_LINE_REC.ARRIVAL_SET_ID || ':' || P_LINE_REC.SHIP_SET_ID , 2 ) ;
1965         END IF;
1966 
1967         IF l_debug_level  > 0 THEN
1968             oe_debug_pub.add(  'OLD SHIP DATE ' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 2 ) ;
1969         END IF;
1970         --- 3344843 Put to old set if set attribs match and there is no change in reserve quantity.
1971         IF Set_Attr_Matched
1972            (p_set_ship_from_org_id    => l_set_ship_from_org_id ,
1973             p_line_ship_from_org_id   => p_line_rec.ship_from_org_id,
1974             p_set_ship_to_org_id      => l_set_ship_to_org_id ,
1975             p_line_ship_to_org_id     => p_line_rec.ship_to_org_id ,
1976             p_set_schedule_ship_date  => l_set_schedule_ship_date ,
1977             p_line_schedule_ship_date => p_line_rec.schedule_ship_date,
1978             p_set_arrival_date        => l_set_schedule_arrival_date,
1979             p_line_arrival_date       => p_line_rec.schedule_arrival_date,
1980             p_line_shipping_method_code => p_line_rec.shipping_method_code ,
1981             p_set_shipping_method_code => l_set_shipping_method_code ,
1982             p_set_type                => l_entity_type)
1983           AND OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
1984            p_old_line_rec.reserved_quantity) THEN
1985 
1986              IF l_debug_level  > 0 THEN
1987                  oe_debug_pub.add(  'ONLY SCHEDULED LINE IS GETTING INTO OLD SET' , 2 ) ;
1988              END IF;
1989              l_matched := TRUE;
1990              -- Since we are not logging delayed request, we will update the
1991              -- ship method if it does not match.
1992 
1993 /* Commented the following code to fix the bug 2916814
1994 
1995             IF NOT OE_GLOBALS.EQUAL(p_line_rec.shipping_method_code,
1996                                     l_shipping_method_code) THEN
1997 
1998                BEGIN
1999 
2000                 Update oe_order_lines_all
2001                 Set shipping_method_code = l_shipping_method_code
2002                 where header_id = p_line_rec.header_id
2003                 and line_id = p_line_rec.line_id;
2004               EXCEPTION
2005                 WHEN OTHERS THEN
2006                      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2007 
2008                END;
2009             END IF;
2010 
2011     */
2012 
2013         END IF; -- compare.
2014 
2015     END IF;  -- not null
2016 
2017     IF NOT l_matched THEN
2018     IF l_debug_level  > 0 THEN
2019         oe_debug_pub.add(  'BEFORE LOGGING G_SCHEDULE_LINE' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
2020     END IF;
2021 
2022      l_line_id := p_line_rec.line_id;
2023 
2024      IF (OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
2025      AND MSC_ATP_GLOBAL.GET_APS_VERSION = 10 ) THEN
2026 
2027          l_line_id := Nvl(p_line_rec.ato_line_id,p_line_rec.line_id);
2028      END IF;
2029 
2030      OE_delayed_requests_Pvt.log_request
2031      (p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
2032       p_entity_id              => l_line_id,
2033       p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
2034       p_requesting_entity_id   => p_line_rec.line_id,
2035       p_request_type           => OE_GLOBALS.G_SCHEDULE_LINE,
2036       p_param1                 => l_param1,
2037       p_param2                 => p_line_rec.header_id,
2038       p_param3                 => l_action,
2039       p_param4                 => p_old_line_rec.ship_from_org_id,
2040       p_param5                 => p_old_line_rec.ship_to_org_id,
2041       p_param6                 => p_old_line_rec.ship_set_id,
2042       p_param7                 => p_old_line_rec.arrival_set_id,
2043       p_param8                 => l_entity_type,
2044       p_param9                 => l_ship_to_org_id,
2045       p_param10                => l_ship_from_org_id,
2046       p_param11                => l_set_shipping_method_code,
2047       p_param12                => l_param12,
2048 /* commented the above line to fix the bug 2916814 */
2049       p_date_param1            => p_old_line_rec.schedule_ship_date,
2050       p_date_param2            => p_old_line_rec.schedule_arrival_date,
2051       p_date_param3            => p_old_line_rec.request_date,
2052       p_date_param4            => l_schedule_ship_date,
2053       p_date_param5            => l_schedule_arrival_date,
2054       x_return_status          => x_return_status);
2055 
2056     END IF;
2057 
2058 
2059  END IF;
2060     --3344843 Storing the change in reserve quantity
2061     l_diff_res_qty := nvl(p_line_rec.reserved_quantity, 0) -
2062                     nvl(p_old_line_rec.reserved_quantity, 0);
2063 -- INVCONV may need changes here for sets
2064 
2065     IF l_debug_level  > 0 THEN
2066       oe_debug_pub.add(  'RES QTY DIFF '|| L_DIFF_RES_QTY , 1 ) ;
2067     END IF;
2068     IF l_diff_res_qty <> 0 THEN
2069        IF l_diff_res_qty > 0 THEN
2070           l_qty_to_reserve   := l_diff_res_qty;
2071           l_qty_to_unreserve := null;
2072        ELSIF l_diff_res_qty < 0 THEN
2073           l_qty_to_unreserve := 0 - l_diff_res_qty;
2074        END IF;
2075 
2076 
2077        l_qty_to_reserve
2078          := NVL(p_old_line_rec.reserved_quantity,0) + l_diff_res_qty;
2079        l_qty_to_unreserve
2080          := p_old_line_rec.reserved_quantity;
2081        --l_index          := p_line_rec.line_id;  --7827737
2082         l_index        :=MOD(p_line_rec.line_id,G_BINARY_LIMIT);--7827737
2083        IF l_qty_to_reserve is not NULL OR
2084              l_qty_to_unreserve  is not NULL THEN
2085 
2086           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).entity_id
2087                            := p_line_rec.top_model_line_id;
2088           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).line_id
2089                            := p_line_rec.line_id;
2090           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).qty_to_reserve
2091                            := l_qty_to_reserve;
2092           Oe_Config_Schedule_Pvt.OE_Reservations_Tbl(l_index).qty_to_unreserve
2093                            := l_qty_to_unreserve;
2094        END IF;
2095     END IF;
2096 
2097  <<END_PROCESS>>
2098 
2099  IF l_debug_level  > 0 THEN
2100     oe_debug_pub.add(  'EXITING LOG SET REQUEST' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
2101  END IF;
2102 
2103  --If the scheduling action is not null then system should execute the delyed request.
2104 
2105 EXCEPTION
2106    WHEN FND_API.G_EXC_ERROR THEN
2107 
2108         x_return_status := FND_API.G_RET_STS_ERROR;
2109 
2110    WHEN OTHERS THEN
2111 
2112         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2113 
2114         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2115         THEN
2116             OE_MSG_PUB.Add_Exc_Msg
2117             (   G_PKG_NAME,
2118               'Log_Set_Request');
2119         END IF;
2120 END Log_Set_Request;
2121 
2122 /*-----------------------------------------------------------------------------
2123 Procedure Name : Schedule_Attribute_Changed
2124 Description    : This function returns TRUE is scheduling attribute is changed
2125                  on a line. This is required for rescheduling.
2126 ----------------------------------------------------------------------------- */
2127 
2128 
2129 FUNCTION Schedule_Attribute_Changed
2130 ( p_line_rec     IN Oe_Order_Pub.line_rec_type
2131 , p_old_line_rec IN Oe_Order_Pub.line_rec_type)
2132 RETURN BOOLEAN
2133 IS
2134 --
2135 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2136 --
2137 BEGIN
2138 
2139     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIP_FROM_ORG_ID,
2140                            p_old_line_rec.SHIP_FROM_ORG_ID)
2141     THEN
2142        RETURN TRUE;
2143     END IF;
2144 
2145     IF NOT OE_GLOBALS.Equal(p_line_rec.SUBINVENTORY,
2146                            p_old_line_rec.SUBINVENTORY)
2147     THEN
2148        RETURN TRUE;
2149     END IF;
2150 
2151     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIP_TO_ORG_ID,
2152                            p_old_line_rec.SHIP_TO_ORG_ID)
2153     THEN
2154        RETURN TRUE;
2155     END IF;
2156 
2157     IF NOT OE_GLOBALS.Equal(p_line_rec.DEMAND_CLASS_CODE,
2158                            p_old_line_rec.DEMAND_CLASS_CODE)
2159     THEN
2160        RETURN TRUE;
2161     END IF;
2162 
2163     IF NOT OE_GLOBALS.Equal(p_line_rec.SCHEDULE_SHIP_DATE,
2164                            p_old_line_rec.SCHEDULE_SHIP_DATE)
2165     THEN
2166        RETURN TRUE;
2167     END IF;
2168 
2169     IF NOT OE_GLOBALS.Equal(p_line_rec.SCHEDULE_ARRIVAL_DATE,
2170                            p_old_line_rec.SCHEDULE_ARRIVAL_DATE)
2171     THEN
2172        RETURN TRUE;
2173     END IF;
2174 
2175     IF NOT OE_GLOBALS.Equal(p_line_rec.SHIPPING_METHOD_CODE,
2176                            p_old_line_rec.SHIPPING_METHOD_CODE)
2177     THEN
2178        RETURN TRUE;
2179     END IF;
2180 
2181     IF NOT OE_GLOBALS.Equal(p_line_rec.REQUEST_DATE,
2182                            p_old_line_rec.REQUEST_DATE)
2183     THEN
2184        RETURN TRUE;
2185     END IF;
2186 
2187     IF NOT OE_GLOBALS.Equal(p_line_rec.DELIVERY_LEAD_TIME,
2188                            p_old_line_rec.DELIVERY_LEAD_TIME)
2189     THEN
2190        RETURN TRUE;
2191     END IF;
2192 
2193 
2194     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
2195                             p_old_line_rec.inventory_item_id)
2196     THEN
2197        RETURN TRUE;
2198     END IF;
2199 
2200     IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,
2201                             p_old_line_rec.order_quantity_uom)
2202     THEN
2203        RETURN TRUE;
2204     END IF;
2205 
2206     IF NOT OE_GLOBALS.Equal(p_line_rec.sold_to_org_id,
2207                             p_old_line_rec.sold_to_org_id)
2208     THEN
2209        RETURN TRUE;
2210     END IF;
2211 
2212 
2213     RETURN FALSE;
2214     IF l_debug_level  > 0 THEN
2215         oe_debug_pub.add(  'RETURNING FALSE ' , 3 ) ;
2216     END IF;
2217 
2218 EXCEPTION
2219 
2220    WHEN OTHERS THEN
2221 
2222         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2223         THEN
2224             OE_MSG_PUB.Add_Exc_Msg
2225             (   G_PKG_NAME,
2226               'Schedule_Attribute_Changed');
2227         END IF;
2228 END Schedule_Attribute_Changed;
2229 
2230 /*-----------------------------------------------------------------------
2231 -- BUG 1955004
2232 Procedure Name : Inactive_Demand_Scheduling
2233 Description    : This procedure is invoked when it is desired to bypass
2234                  the call to ATP for SCHEDULING.  It is primarily written
2235                  for the new Scheduling Levels of FOUR and FIVE, which are
2236                  'Inactive Demand with Reservations' and 'Inactive Demand
2237                  without Reservations'.  This procedure will act on four
2238                  fields - schedule_ship_date, schedule_arrival_date,
2239                  visible_demand_flag, and schedule_status_code.
2240 ------------------------------------------------------------------------*/
2241 PROCEDURE Inactive_Demand_Scheduling
2242 ( p_x_old_line_rec  IN OE_ORDER_PUB.line_rec_type
2243 , p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type
2244 , p_sch_action      IN VARCHAR2 := NULL
2245 , x_return_status OUT NOCOPY VARCHAR2
2246 
2247 ) IS
2248 
2249 l_sch_action           VARCHAR2(30) := p_sch_action;
2250 l_order_date_type_code VARCHAR2(30);
2251 
2252 l_return_status   VARCHAR2(1);  -- Added for IR ISO Tracking bug 7667702
2253 --
2254 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2255 --
2256 BEGIN
2257 
2258   IF l_debug_level  > 0 THEN
2259       oe_debug_pub.add(  'ENTERING INACTIVE_DEMAND_SCHEDULING' , 1 ) ;
2260   END IF;
2261   X_return_status := FND_API.G_RET_STS_SUCCESS;
2262 
2263   IF l_sch_action is NULL THEN
2264     L_sch_action := p_x_line_rec.schedule_action_code;
2265   END IF;
2266 
2267   l_order_date_type_code :=
2268             NVL(Get_Date_Type(p_x_line_rec.header_id), 'SHIP');
2269 
2270 
2271   IF l_sch_action = OESCH_ACT_SCHEDULE OR
2272      l_sch_action = OESCH_ACT_RESCHEDULE OR
2273      l_sch_action = OESCH_ACT_REDEMAND OR
2274      l_sch_action = OESCH_ACT_DEMAND OR
2275      (l_sch_action = OESCH_ACT_RESERVE AND
2276       p_x_line_rec.schedule_status_code IS NULL) THEN
2277     -- set the data accordingly.
2278 
2279     IF l_debug_level  > 0 THEN
2280         oe_debug_pub.add(  'INACTIVE DEMAND - GOING TO SCHEDULE' , 1 ) ;
2281     END IF;
2282 
2283     IF l_order_date_type_code = 'SHIP' THEN
2284       IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
2285                               p_x_old_line_rec.schedule_ship_date) AND
2286          p_x_line_rec.schedule_ship_date IS NOT NULL AND
2287          p_x_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE THEN
2288          -- If the user provides a ship_date, or changes the existing, use it
2289          -- DOO Sch Integration
2290          IF NVL(p_x_line_rec.bypass_sch_flag,'N') ='N'
2291 	    OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2292             p_x_line_rec.schedule_arrival_date := p_x_line_rec.schedule_ship_date;
2293          END IF;
2294 
2295       ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.request_date,
2296                                  p_x_old_line_rec.request_date) AND
2297          p_x_line_rec.request_date IS NOT NULL AND
2298          p_x_line_rec.request_date <> FND_API.G_MISS_DATE THEN
2299            -- if the user changed request date, use it
2300            -- DOO Sch Integration
2301            IF NVL(p_x_line_rec.bypass_sch_flag,'N') ='N'
2302 	     OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2303              p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2304              p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2305            END IF;
2306 
2307       ELSE
2308         --  dates have not changed, so use whichever is not null, ship first
2309          -- DOO Sch Integration
2310          IF NVL(p_x_line_rec.bypass_sch_flag,'N') ='N'
2311 	  OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2312             IF p_x_line_rec.schedule_ship_date IS NOT NULL THEN
2313               p_x_line_rec.schedule_arrival_date := p_x_line_rec.schedule_ship_date;
2314             ELSE
2315               p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2316               p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2317             END IF;
2318           END IF;
2319       END IF;
2320 
2321     ELSE -- Arrival
2322 
2323       IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_arrival_date,
2324                               p_x_old_line_rec.schedule_arrival_date) AND
2325          p_x_line_rec.schedule_arrival_date IS NOT NULL AND
2326          p_x_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE THEN
2327            -- If the user provides a arrival_date, or changes the existing, use it
2328            -- DOO Sch Integration
2329            IF NVL(p_x_line_rec.bypass_sch_flag,'N') ='N'
2330 	   OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2331              p_x_line_rec.schedule_ship_date := p_x_line_rec.schedule_arrival_date;
2332            END IF;
2333 
2334       ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.request_date,
2335                                  p_x_old_line_rec.request_date) AND
2336          p_x_line_rec.request_date IS NOT NULL AND
2337          p_x_line_rec.request_date <> FND_API.G_MISS_DATE THEN
2338            -- if the user changed request date, use it
2339             -- DOO Sch Integration
2340             IF NVL(p_x_line_rec.bypass_sch_flag,'N') ='N'
2341 	    OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2342 
2343               p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2344               p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2345             END IF;
2346 
2347       ELSE
2348         --  dates have not changed, so use whichever is not null, ship first
2349         -- DOO Sch Integration
2350         IF Nvl(p_x_line_rec.bypass_sch_flag, 'N') = 'N'
2351 	OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR THEN --14043008
2352           IF p_x_line_rec.schedule_arrival_date IS NOT NULL THEN
2353             -- it is arrival date type, so use the arrival date provided to set ship
2354             p_x_line_rec.schedule_ship_date := p_x_line_rec.schedule_arrival_date;
2355           ELSE
2356             -- if user does not provide a date, use request date
2357             p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
2358             p_x_line_rec.schedule_arrival_date := p_x_line_rec.request_date;
2359           END IF;
2360         END IF;
2361        END IF;
2362     END IF;
2363 
2364     -- we want this line scheduled, but not visible for demand
2365     p_x_line_rec.visible_demand_flag := 'N';
2366     p_x_line_rec.schedule_status_code := OESCH_STATUS_SCHEDULED;
2367     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
2368        -- Promise date setup
2369        Promise_Date_for_Sch_Action
2370             (p_x_line_rec => p_x_line_rec
2371             ,p_sch_action => l_sch_action
2372             ,P_header_id  => p_x_line_rec.header_id);
2373     END IF;
2374 
2375   ELSIF l_sch_action = OESCH_ACT_UNSCHEDULE OR
2376         L_sch_action = OESCH_ACT_UNDEMAND THEN
2377     -- we are unscheduling
2378 
2379     IF l_debug_level  > 0 THEN
2380         oe_debug_pub.add(  'INACTIVE DEMAND - GOING TO UNSCHEDULE' , 1 ) ;
2381     END IF;
2382 
2383     P_x_line_rec.schedule_ship_date := NULL;
2384     P_x_line_rec.schedule_arrival_date := NULL;
2385     p_x_line_rec.visible_demand_flag := NULL;
2386     p_x_line_rec.schedule_status_code := NULL;
2387 
2388     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
2389        -- Promise date setup
2390        Promise_Date_for_Sch_Action
2391             (p_x_line_rec => p_x_line_rec
2392             ,p_sch_action => l_sch_action
2393             ,P_header_id  => p_x_line_rec.header_id);
2394     END IF;
2395   ELSE
2396   -- We should not have any other actions here.
2397   -- If we do, it is an error.
2398     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2399 
2400   END IF;
2401 
2402 /* IR ISO Tracking bug 7667702: IR ISO Change Management Begins */
2403 
2404 -- This code is hooked for IR ISO project, where if the schedule ship/
2405 -- arrival date is changed and MRP is not installed then a delayed request is
2406 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
2407 -- API from Purchasing, responsible for conditionally updating the Need By
2408 -- Date column in internal requisition line. This will be done based on
2409 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
2410 
2411 -- For details on IR ISO CMS project, please refer to FOL >
2412 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
2413 
2414 
2415     IF (p_x_line_rec.order_source_id = 10) AND
2416        (
2417         ((p_x_old_line_rec.schedule_ship_date IS NOT NULL) AND
2418           NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,p_x_old_line_rec.schedule_ship_date))
2419        OR
2420         ((p_x_old_line_rec.schedule_arrival_date IS NOT NULL) AND
2421           NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_arrival_date,p_x_old_line_rec.schedule_arrival_date))
2422        )  THEN
2423 
2424        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
2425          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
2426 
2427          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
2428 
2429            IF l_debug_level > 0 THEN
2430              oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change');
2431            END IF;
2432 
2433            -- Log a delayed request to update the change in Schedule Ship Date to
2434            -- Requisition Line. This request will be logged only if the change is
2435            -- not initiated from Requesting Organization, and it is not a case of
2436            -- Internal Sales Order Full Cancellation. It will even not be logged
2437            -- Purchasing profile option does not allow update of Need By Date when
2438            -- Schedule Ship Date changes on internal sales order line
2439 
2440            OE_delayed_requests_Pvt.log_request
2441            ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
2442            , p_entity_id              => p_x_line_rec.line_id
2443            , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
2444            , p_requesting_entity_id   => p_x_line_rec.line_id
2445            , p_request_unique_key1    => p_x_line_rec.header_id  -- Order Hdr_id
2446            , p_request_unique_key2    => p_x_line_rec.source_document_id -- Req Hdr_id
2447            , p_request_unique_key3    => p_x_line_rec.source_document_line_id -- Req Line_id
2448            , p_date_param1            => p_x_line_rec.schedule_arrival_date
2449            -- Note: p_date_param1 is used for both Schedule_Ship_Date and
2450            -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
2451            -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
2452            -- it can expect change with respect to Ship or Arrival date. Thus, will
2453            -- not raise any issues.
2454            , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
2455            , x_return_status          => l_return_status
2456            );
2457 
2458            IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2459              RAISE FND_API.G_EXC_ERROR;
2460            END IF;
2461 
2462          ELSE
2463            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
2464              IF l_debug_level > 0 THEN
2465                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
2466              END IF;
2467 
2468              OE_SCHEDULE_UTIL.Update_PO(p_x_line_rec.schedule_arrival_date,
2469                 p_x_line_rec.source_document_id,
2470                 p_x_line_rec.source_document_line_id);
2471            END IF;
2472          END IF;
2473 
2474        END IF;
2475      END IF; -- Order_Source_id
2476 
2477 /* ============================= */
2478 /* IR ISO Change Management Ends */
2479 
2480 EXCEPTION
2481 
2482    WHEN FND_API.G_EXC_ERROR THEN -- Added for IR ISO Tracking bug 7667702
2483       x_return_status :=  FND_API.G_RET_STS_ERROR;
2484 
2485   WHEN OTHERS THEN
2486 
2487     X_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2488 
2489     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2490     THEN
2491 
2492       OE_MSG_PUB.Add_Exc_Msg
2493       (G_PKG_NAME,
2494        'Inactive_Demand_Scheduling');
2495     END IF;
2496 
2497     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2498 
2499 END Inactive_Demand_Scheduling;
2500 -- END 1955004
2501 
2502 /*-----------------------------------------------------------------------
2503 Procedure Name : Initialize_mrp_record
2504 Description    : This procedure create l_count records each for each table
2505                  in the record of tables of MRP's p_atp_rec.
2506 ------------------------------------------------------------------------*/
2507 Procedure Initialize_mrp_record
2508 ( p_x_atp_rec IN  OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ
2509  ,l_count     IN  NUMBER)
2510 IS
2511  l_return_status           VARCHAR2(1);
2512  --
2513  l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2514  --
2515 BEGIN
2516   IF l_debug_level  > 0 THEN
2517       oe_debug_pub.add(  'EXTENDING THE TABLE BY ' || L_COUNT , 5 ) ;
2518   END IF;
2519 /*
2520   p_x_atp_rec.Inventory_Item_Id.extend(l_count);
2521   p_x_atp_rec.Source_Organization_Id.extend(l_count);
2522   p_x_atp_rec.Identifier.extend(l_count);
2523   p_x_atp_rec.Order_Number.extend(l_count);
2524   p_x_atp_rec.Calling_Module.extend(l_count);
2525   p_x_atp_rec.Customer_Id.extend(l_count);
2526   p_x_atp_rec.Customer_Site_Id.extend(l_count);
2527   p_x_atp_rec.Destination_Time_Zone.extend(l_count);
2528   p_x_atp_rec.Quantity_Ordered.extend(l_count);
2529   p_x_atp_rec.Quantity_UOM.extend(l_count);
2530   p_x_atp_rec.Requested_Ship_Date.extend(l_count);
2531   p_x_atp_rec.Requested_Arrival_Date.extend(l_count);
2532   p_x_atp_rec.Earliest_Acceptable_Date.extend(l_count);
2533   p_x_atp_rec.Latest_Acceptable_Date.extend(l_count);
2534   p_x_atp_rec.Delivery_Lead_Time.extend(l_count);
2535   p_x_atp_rec.Atp_Lead_Time.extend(l_count);
2536   p_x_atp_rec.Freight_Carrier.extend(l_count);
2537   p_x_atp_rec.Ship_Method.extend(l_count);
2538   p_x_atp_rec.Demand_Class.extend(l_count);
2539   p_x_atp_rec.Ship_Set_Name.extend(l_count);
2540   p_x_atp_rec.Arrival_Set_Name.extend(l_count);
2541   p_x_atp_rec.Override_Flag.extend(l_count);
2542   p_x_atp_rec.Action.extend(l_count);
2543   p_x_atp_rec.ship_date.extend(l_count);
2544   p_x_atp_rec.Available_Quantity.extend(l_count);
2545   p_x_atp_rec.Requested_Date_Quantity.extend(l_count);
2546   p_x_atp_rec.Group_Ship_Date.extend(l_count);
2547   p_x_atp_rec.Group_Arrival_Date.extend(l_count);
2548   p_x_atp_rec.Vendor_Id.extend(l_count);
2549   p_x_atp_rec.Vendor_Site_Id.extend(l_count);
2550   p_x_atp_rec.Insert_Flag.extend(l_count);
2551   p_x_atp_rec.Error_Code.extend(l_count);
2552   p_x_atp_rec.Message.extend(l_count);
2553   p_x_atp_rec.Old_Source_Organization_Id.extend(l_count);
2554   p_x_atp_rec.Old_Demand_Class.extend(l_count);
2555   p_x_atp_rec.oe_flag.extend(l_count);
2556   -- Added below attributes to fix bug 1912138.
2557   p_x_atp_rec.ato_delete_flag.extend(l_count);
2558   p_x_atp_rec.attribute_01.extend(l_count);
2559   p_x_atp_rec.attribute_05.extend(l_count);
2560   p_x_atp_rec.substitution_typ_code.extend(l_count);
2561   p_x_atp_rec.old_inventory_item_id.extend(l_count);
2562 */
2563 
2564   IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
2565   AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
2566 
2567    MSC_ATP_GLOBAL.EXTEND_ATP
2568     (p_atp_tab       => p_x_atp_rec,
2569      p_index         => l_count,
2570      x_return_status => l_return_status);
2571 
2572   ELSE
2573 
2574     MSC_SATP_FUNC.Extend_ATP
2575     (p_atp_tab       => p_x_atp_rec,
2576      p_index         => l_count,
2577      x_return_status => l_return_status);
2578 
2579   END IF;
2580 
2581 
2582 EXCEPTION
2583 
2584    WHEN OTHERS THEN
2585 
2586         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2587         THEN
2588             OE_MSG_PUB.Add_Exc_Msg
2589             (   G_PKG_NAME,
2590               'Initialize_mrp_record');
2591         END IF;
2592 END Initialize_mrp_record;
2593 
2594 /*
2595 Scheduling Enhancements ER 2736896
2596 Checks whether a split is needed based on the MRP record returned
2597 */
2598 PROCEDURE check_split_needed(p_x_line_rec       IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
2599                              p_atp_rec          IN  MRP_ATP_PUB.ATP_Rec_Typ,
2600                              p_index            IN  NUMBER,
2601                              p_split_line_rec       OUT NOCOPY OE_ORDER_PUB.line_rec_type,
2602                              p_line_action      IN             VARCHAR2,
2603                              p_config_exists    IN    VARCHAR2,
2604                              p_om_auto_split    IN  VARCHAR2,
2605                              p_order_date_type_code IN VARCHAR2,
2606                              x_return_status        OUT NOCOPY VARCHAR2
2607                             )
2608 IS
2609 
2610    l_bckup_request_tbl OE_Order_PUB.Request_Tbl_Type;
2611    l_atp_tbl           OE_ATP.atp_tbl_type;
2612    l_sch_recursion     VARCHAR2(30);
2613 
2614    l_split_line_tbl    OE_ORDER_PUB.Line_Tbl_Type;
2615    l_update_line_tbl    OE_ORDER_PUB.Line_Tbl_Type;
2616    l_control_rec				OE_GLOBALS.Control_Rec_Type;
2617    l_header_out_rec            OE_ORDER_PUB.Header_Rec_Type;
2618    l_line_out_tbl              OE_ORDER_PUB.Line_Tbl_Type;
2619    l_line_adj_out_tbl          OE_ORDER_PUB.Line_Adj_Tbl_Type;
2620    l_header_adj_out_tbl        OE_Order_PUB.Header_Adj_Tbl_Type;
2621    l_header_scredit_out_tbl    OE_Order_PUB.Header_Scredit_Tbl_Type;
2622    l_line_scredit_out_tbl      OE_Order_PUB.Line_Scredit_Tbl_Type;
2623    l_action_request_out_tbl    OE_Order_PUB.Request_Tbl_Type;
2624    l_header_price_att_tbl		OE_ORDER_PUB.Header_Price_Att_Tbl_Type;
2625    l_header_adj_assoc_tbl		OE_ORDER_PUB.Header_Adj_Assoc_Tbl_Type;
2626    l_header_adj_att_tbl		OE_ORDER_PUB.Header_Adj_Att_Tbl_Type;
2627    l_line_price_att_tbl		OE_ORDER_PUB.Line_Price_Att_Tbl_Type;
2628    l_line_adj_assoc_tbl		OE_ORDER_PUB.Line_Adj_Assoc_Tbl_Type;
2629    l_line_adj_att_tbl			OE_ORDER_PUB.Line_Adj_Att_Tbl_Type;
2630    l_lot_serial_tbl        	OE_Order_PUB.Lot_Serial_Tbl_Type;
2631    l_msg_count					NUMBER;
2632    l_msg_data					VARCHAR2(2000);
2633    l_header_payment_out_tbl        OE_Order_PUB.Header_Payment_Tbl_Type;
2634    l_line_payment_out_tbl          OE_Order_PUB.Line_Payment_Tbl_Type;
2635 
2636    l_service_exists varchar2(30);
2637 
2638    l_substitute_flag  BOOLEAN := FALSE;
2639 
2640    l_debug_level       CONSTANT NUMBER := oe_debug_pub.g_debug_level;
2641 
2642    l_mrp_rollback_schedule number := 0;
2643 
2644    l_revert_substitution BOOLEAN := FALSE;
2645    l_requested_date_quantity NUMBER := 0;
2646    l_latest_acceptable_date DATE;
2647 
2648    J NUMBER := p_index;
2649    l_return_status     VARCHAR2(1);
2650 
2651    --For MRP ROLLBACK logic
2652    --Only fetch new line ID
2653    CURSOR c_mrp_rollback(c_line_id number) is
2654    select line_id , schedule_action_code
2655    from   oe_schedule_lines_temp
2656    where  line_id = c_line_id;
2657 BEGIN
2658 
2659    x_return_status := FND_API.G_RET_STS_SUCCESS;
2660    SAVEPOINT SPLIT_SCHEDULING;
2661 
2662    --Take a backup of current delayed request table
2663    --so that in case of any error just set the global table to the backup table
2664    --since in case or error we do not want any of the delayed requests
2665    --logged during split to be executed
2666    l_bckup_request_tbl := OE_Delayed_Requests_PVT.g_delayed_requests;
2667 
2668    l_latest_acceptable_date := NVL(p_x_line_rec.latest_acceptable_date,p_x_line_rec.request_date);
2669 
2670    IF l_debug_level  > 0 THEN
2671       oe_debug_pub.add(  'check_split_needed() inside check_split_needed()' , 1 ) ;
2672 	  oe_debug_pub.add(  'check_split_needed() p_om_auto_split:' || p_om_auto_split , 1 ) ;
2673       oe_debug_pub.add(  'check_split_needed() l_latest_acceptable_date:' || l_latest_acceptable_date , 1 ) ;
2674       oe_debug_pub.add(  'check_split_needed() requested_ship_date:'  || p_atp_rec.requested_ship_date(J) , 1 ) ;
2675       oe_debug_pub.add(  'check_split_needed() requested_arrival_date:'  || p_atp_rec.requested_arrival_date(J) , 1 ) ;
2676       oe_debug_pub.add(  'check_split_needed() quantity_ordered:'  || p_atp_rec.quantity_ordered(J) , 1 ) ;
2677       oe_debug_pub.add(  'check_split_needed() ATP order uom:'  || p_atp_rec.quantity_uom(J) , 1 ) ;
2678       oe_debug_pub.add(  'check_split_needed() order line UOM:'  || p_x_line_rec.order_quantity_uom , 1 ) ;
2679       oe_debug_pub.add(  'check_split_needed() ship_date:'  || p_atp_rec.ship_date(J) , 1 ) ;
2680 	  oe_debug_pub.add(  'check_split_needed() arrival_date:'  || p_atp_rec.arrival_date(J) , 1 ) ;
2681       oe_debug_pub.add(  'check_split_needed() available_quantity:'  || p_atp_rec.available_quantity(J) , 1 ) ;
2682       oe_debug_pub.add(  'check_split_needed() requested_date_quantity:'  || p_atp_rec.requested_date_quantity(J) , 1 ) ;
2683       oe_debug_pub.add(  'check_split_needed() source_organization_id:'  || p_atp_rec.source_organization_id(J) , 1 ) ;
2684    END IF;
2685 
2686    IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id THEN
2687       l_substitute_flag := TRUE;
2688 
2689       IF l_debug_level  > 0 THEN
2690           oe_debug_pub.add(  'SCH: ITEM HAS BEEN SUBSTITUTED' , 1 ) ;
2691 		  oe_debug_pub.add(  'check_split_needed() inventory_item_id:'  || p_atp_rec.inventory_item_id(J) , 1 ) ;
2692 		  oe_debug_pub.add(  'check_split_needed() inventory_item_name:'  || p_atp_rec.inventory_item_name(J) , 1 ) ;
2693           oe_debug_pub.add(  'check_split_needed() req_item_detail_flag:'  || p_atp_rec.req_item_detail_flag(J) , 1 ) ;
2694           oe_debug_pub.add(  'check_split_needed() request_item_id:'  || p_atp_rec.request_item_id(J) , 1 ) ;
2695 		  oe_debug_pub.add(  'check_split_needed() request_item_name:'  || p_atp_rec.request_item_name(J) , 1 ) ;
2696           oe_debug_pub.add(  'check_split_needed() req_item_available_date:'  || p_atp_rec.req_item_available_date(J) , 1 ) ;
2697           oe_debug_pub.add(  'check_split_needed() req_item_req_date_qty:'  || p_atp_rec.req_item_req_date_qty(J) , 1 ) ;
2698           oe_debug_pub.add(  'check_split_needed() req_item_available_date_qty:'  || p_atp_rec.req_item_available_date_qty(J) , 1 ) ;
2699       END IF;
2700       IF l_debug_level  > 0 THEN
2701           oe_debug_pub.add(  'NEW INVENTORY ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
2702       END IF;
2703 
2704       IF p_om_auto_split = 'Q' AND nvl(p_atp_rec.req_item_req_date_qty(J),0) > 0
2705          AND nvl(p_atp_rec.req_item_req_date_qty(J),0) < p_atp_rec.quantity_ordered(J)
2706       THEN
2707 
2708          l_substitute_flag := FALSE;
2709 		 l_revert_substitution := TRUE;
2710          IF l_debug_level  > 0 THEN
2711           oe_debug_pub.add(  'check_split_needed() Substitution case, but revert substitution', 1 ) ;
2712          END IF;
2713       END IF;
2714 
2715    END IF;
2716 
2717    IF NOT l_substitute_flag THEN
2718 
2719    --No substitution, check if requested_date_quantity has a value less than available_quantity
2720      IF NOT l_revert_substitution THEN
2721 
2722         IF l_debug_level  > 0 THEN
2723            oe_debug_pub.add(  'Split the line:' || p_atp_rec.requested_date_quantity(J) , 1 ) ;
2724         END IF;
2725 
2726         l_requested_date_quantity := p_atp_rec.requested_date_quantity(J);
2727 
2728     --clear the placed demand, if scheduling was successful
2729     IF NOT (p_atp_rec.error_code(J) <> 0 AND
2730             p_atp_rec.error_code(J) <> -99  AND
2731             p_atp_rec.error_code(J) <> 150 )
2732 	THEN
2733         MRP_ROLLBACK
2734             ( p_line_id  =>  p_x_line_rec.line_id
2735              ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
2736              ,x_return_status    => l_return_status);
2737     END IF;
2738 
2739      ELSE --revert the substitution in GOP
2740 
2741         l_requested_date_quantity := p_atp_rec.req_item_req_date_qty(J); --qty available for requested item on requested date
2742 
2743         --call overloaded MRP_ROLLBACK API
2744         MRP_ROLLBACK
2745             ( p_line_id  =>  p_x_line_rec.line_id
2746              ,p_item_id  =>  p_atp_rec.inventory_item_id(J)
2747              ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
2748              ,x_return_status    => l_return_status);
2749 
2750 	 END IF;
2751 
2752     l_split_line_tbl(1) := OE_ORDER_PUB.G_MISS_LINE_REC;
2753     l_split_line_tbl(1).line_id := p_x_line_rec.line_id;
2754     --l_split_line_tbl(1).line_set_id := l_line_set_id;
2755 
2756 	--check if UOM has changed in ATP
2757     IF p_x_line_rec.order_quantity_uom <> p_atp_rec.quantity_uom(J) THEN
2758        IF l_debug_level  > 0 THEN
2759           oe_debug_pub.add(  'check_split_needed() UOM has changed, convert' , 1 ) ;
2760        END IF;
2761 
2762        l_split_line_tbl(1).ordered_quantity := INV_CONVERT.INV_UM_CONVERT(
2763                                                        p_x_line_rec.inventory_item_id -- INVCONV
2764                                                       ,5
2765                                                       ,l_requested_date_quantity
2766                                                       ,p_atp_rec.quantity_uom(J)
2767                                                       ,p_x_line_rec.order_quantity_uom
2768                                                       ,NULL -- From uom name
2769                                                       ,NULL -- To uom name
2770                                                       );
2771 	ELSE
2772        l_split_line_tbl(1).ordered_quantity := l_requested_date_quantity; --p_atp_rec.requested_date_quantity(J);
2773 	END IF;
2774 
2775     IF p_x_line_rec.ordered_quantity2 is not null
2776     AND p_x_line_rec.ordered_quantity2 <> FND_API.G_MISS_NUM
2777 	THEN
2778        l_split_line_tbl(1).ordered_quantity2 := round(p_x_line_rec.ordered_quantity2 *
2779                                               (l_split_line_tbl(1).ordered_quantity/p_x_line_rec.ordered_quantity),5) ;
2780     END IF;
2781 
2782     l_split_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
2783     l_split_line_tbl(1).change_reason := 'SYSTEM';
2784 
2785     IF l_debug_level  > 0 THEN
2786         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(1) : ordered_quantity ' || l_split_line_tbl(1).ordered_quantity , 1 ) ;
2787         oe_debug_pub.add(  'check_split_needed() oe_split_util.g_sch_recursion ' || oe_split_util.g_sch_recursion , 1 ) ;
2788     END IF;
2789 
2790 	--Schedule the first line during split
2791     IF p_order_date_type_code = 'SHIP' THEN
2792        l_split_line_tbl(1).schedule_ship_date := nvl(p_x_line_rec.schedule_ship_date,p_atp_rec.requested_ship_date(J));
2793     ELSE
2794        l_split_line_tbl(1).schedule_arrival_date := nvl(p_x_line_rec.schedule_arrival_date,p_atp_rec.requested_arrival_date(J));
2795     END IF;
2796 
2797     l_split_line_tbl(1).split_action_code := 'SPLIT';
2798     l_split_line_tbl(1).split_by := 'USER';
2799 
2800     l_split_line_tbl(2) := OE_ORDER_PUB.G_MISS_LINE_REC;
2801     l_split_line_tbl(2).ordered_quantity := p_x_line_rec.ordered_quantity - l_split_line_tbl(1).ordered_quantity ;
2802     IF p_x_line_rec.ordered_quantity2 is not null
2803     AND p_x_line_rec.ordered_quantity2 <> FND_API.G_MISS_NUM
2804     THEN
2805        l_split_line_tbl(2).ordered_quantity2 := round((p_x_line_rec.ordered_quantity2 - l_split_line_tbl(1).ordered_quantity2),5) ;
2806     END IF;
2807     l_split_line_tbl(2).operation := OE_GLOBALS.G_OPR_CREATE;
2808     --l_split_line_tbl(2).split_action_code := 'SPLIT';
2809     l_split_line_tbl(2).split_from_line_id := p_x_line_rec.line_id;
2810     l_split_line_tbl(2).split_by := 'USER';
2811     l_split_line_tbl(2).change_reason := 'SYSTEM';
2812 
2813     IF NOT l_revert_substitution THEN
2814        l_split_line_tbl(2).schedule_ship_date := p_atp_rec.requested_ship_date(J);
2815        l_split_line_tbl(2).schedule_arrival_date := p_atp_rec.requested_arrival_date(J);
2816     ELSE
2817        l_split_line_tbl(2).schedule_ship_date := p_atp_rec.req_item_available_date(J);
2818        l_split_line_tbl(2).schedule_arrival_date := p_atp_rec.req_item_available_date(J);
2819     END IF;
2820 
2821     IF l_debug_level  > 0 THEN
2822        oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(2) : ordered_quantity ' || l_split_line_tbl(2).ordered_quantity , 1 ) ;
2823     END IF;
2824 
2825     l_sch_recursion := oe_split_util.g_sch_recursion;
2826 
2827     oe_split_util.g_sch_recursion := 'FALSE';
2828     G_GOP_AUTO_SPLIT := TRUE;
2829     G_GOP_AUTO_SPLIT_LINE_ID := p_x_line_rec.line_id;
2830 
2831     l_control_rec.validate_entity		:= FALSE;
2832     l_control_rec.check_security		:= TRUE;
2833     l_control_rec.process               := TRUE;
2834     l_control_rec.controlled_operation  := TRUE;
2835 
2836     IF l_debug_level  > 0 THEN
2837         oe_debug_pub.add(  'check_split_needed() calling process order to split - same item ' , 1 ) ;
2838     END IF;
2839 
2840     OE_Order_PVT.Process_Order
2841     (
2842         p_api_version_number        => 1.0
2843     ,    x_return_status                => l_return_status
2844     ,    x_msg_count                    => l_msg_count
2845     ,    x_msg_data                    => l_msg_data
2846     ,    p_x_line_tbl                => l_split_line_tbl
2847     ,    p_control_rec                => l_control_rec
2848     ,    p_x_header_rec                => l_header_out_rec
2849     ,    p_x_header_adj_tbl            => l_header_adj_out_tbl
2850     ,    p_x_header_scredit_tbl        => l_header_scredit_out_tbl
2851     ,    p_x_header_payment_tbl        => l_header_payment_out_tbl
2852     ,    p_x_line_adj_tbl            => l_line_adj_out_tbl
2853     ,    p_x_line_scredit_tbl        => l_line_scredit_out_tbl
2854     ,    p_x_line_payment_tbl        => l_line_payment_out_tbl
2855     ,    p_x_action_request_tbl        => l_action_request_out_tbl
2856     ,    p_x_lot_serial_tbl            => l_lot_serial_tbl
2857     ,    p_x_header_price_att_tbl    => l_header_price_att_tbl
2858     ,    p_x_header_adj_att_tbl        => l_header_adj_att_tbl
2859     ,    p_x_header_adj_assoc_tbl    => l_header_adj_assoc_tbl
2860     ,    p_x_line_price_att_tbl        => l_line_price_att_tbl
2861     ,    p_x_line_adj_att_tbl        => l_line_adj_att_tbl
2862     ,    p_x_line_adj_assoc_tbl        => l_line_adj_assoc_tbl
2863     );
2864 
2865       IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2866           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2867       ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
2868           RAISE FND_API.G_EXC_ERROR;
2869       END IF;
2870 
2871     IF l_debug_level  > 0 THEN
2872         oe_debug_pub.add(  'check_split_needed() returned from process order to split - same item ' || l_return_status  , 1 ) ;
2873         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(1) : SSD ' || l_split_line_tbl(1).schedule_ship_date , 1 ) ;
2874         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(1) : SAD ' || l_split_line_tbl(1).schedule_arrival_date , 1 ) ;
2875     END IF;
2876 
2877     IF l_debug_level  > 0 THEN
2878         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(2) : SSD ' || l_split_line_tbl(2).schedule_ship_date , 1 ) ;
2879         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(2) : SAD ' || l_split_line_tbl(2).schedule_arrival_date , 1 ) ;
2880     END IF;
2881 
2882 	--Call PO API to schedule the split line 2 with correct SSD/SAD
2883     l_update_line_tbl(1) := OE_ORDER_PUB.G_MISS_LINE_REC;
2884     l_update_line_tbl(1).line_id := l_split_line_tbl(2).line_id;
2885     l_update_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
2886     l_update_line_tbl(1).change_reason := 'SYSTEM';
2887     l_update_line_tbl(1).ship_from_org_id   := Nvl(p_x_line_rec.ship_from_org_id, p_atp_rec.source_organization_id(J));
2888 	l_update_line_tbl(1).schedule_action_code := OE_Schedule_Util.OESCH_ACT_SCHEDULE;
2889 
2890 
2891     OE_Order_PVT.Process_Order
2892     (
2893         p_api_version_number        => 1.0
2894     ,    x_return_status                => l_return_status
2895     ,    x_msg_count                    => l_msg_count
2896     ,    x_msg_data                    => l_msg_data
2897     ,    p_x_line_tbl                => l_update_line_tbl
2898     ,    p_control_rec                => l_control_rec
2899     ,    p_x_header_rec                => l_header_out_rec
2900     ,    p_x_header_adj_tbl            => l_header_adj_out_tbl
2901     ,    p_x_header_scredit_tbl        => l_header_scredit_out_tbl
2902     ,    p_x_header_payment_tbl        => l_header_payment_out_tbl
2903     ,    p_x_line_adj_tbl            => l_line_adj_out_tbl
2904     ,    p_x_line_scredit_tbl        => l_line_scredit_out_tbl
2905     ,    p_x_line_payment_tbl        => l_line_payment_out_tbl
2906     ,    p_x_action_request_tbl        => l_action_request_out_tbl
2907     ,    p_x_lot_serial_tbl            => l_lot_serial_tbl
2908     ,    p_x_header_price_att_tbl    => l_header_price_att_tbl
2909     ,    p_x_header_adj_att_tbl        => l_header_adj_att_tbl
2910     ,    p_x_header_adj_assoc_tbl    => l_header_adj_assoc_tbl
2911     ,    p_x_line_price_att_tbl        => l_line_price_att_tbl
2912     ,    p_x_line_adj_att_tbl        => l_line_adj_att_tbl
2913     ,    p_x_line_adj_assoc_tbl        => l_line_adj_assoc_tbl
2914     );
2915 
2916     IF l_debug_level  > 0 THEN
2917         oe_debug_pub.add(  'check_split_needed() same item : returned from process order to schedule new line ' || l_return_status  , 1 ) ;
2918         oe_debug_pub.add(  'check_split_needed() l_update_line_tbl(1) : SSD ' || l_update_line_tbl(1).schedule_ship_date , 1 ) ;
2919         oe_debug_pub.add(  'check_split_needed() l_update_line_tbl(1) : SAD ' || l_update_line_tbl(1).schedule_arrival_date , 1 ) ;
2920     END IF;
2921 
2922 	/* DO NOT RAISE ERROR
2923     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2924 	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2925 	ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
2926 		  RAISE FND_API.G_EXC_ERROR;
2927 	END IF;
2928 	*/
2929 
2930      --query the row again
2931 	 oe_line_util.query_row
2932 		(p_line_id	=> l_split_line_tbl(1).line_id
2933 		,x_line_rec	=> l_split_line_tbl(1)
2934 		);
2935 
2936 	oe_split_util.g_sch_recursion := l_sch_recursion ;
2937 	G_GOP_AUTO_SPLIT := FALSE;
2938 	G_GOP_AUTO_SPLIT_LINE_ID := 0;
2939     p_split_line_rec := l_split_line_tbl(1);
2940 
2941   ELSE
2942 
2943     IF l_debug_level  > 0 THEN
2944         oe_debug_pub.add(  'check_split_needed() Item Substitution case ' , 1 ) ;
2945     END IF;
2946 
2947     IF (nvl(p_atp_rec.req_item_req_date_qty(J),0) = 0 AND p_om_auto_split = 'P')
2948 	   OR p_om_auto_split in ('Q','F')
2949 	THEN
2950 		IF l_debug_level  > 0 THEN
2951           oe_debug_pub.add(  ' No split needed.'  , 1 ) ;
2952         END IF;
2953 	   RETURN;
2954 	END IF;
2955 
2956 	--Check if the line has service
2957     BEGIN
2958     Select 'EXISTS' into
2959        l_service_exists
2960        from oe_order_lines_all
2961        where service_reference_line_id = p_x_line_rec.line_id;
2962 		IF l_debug_level  > 0 THEN
2963           oe_debug_pub.add(  'The line has a service line split. Do not split.'  , 1 ) ;
2964         END IF;
2965 	   RETURN;
2966     EXCEPTION
2967         when no_data_found then
2968     	    null;
2969     End ;
2970 
2971 	IF  ( NVL(p_x_line_rec.booked_flag,'N') = 'N' OR
2972             ( NVL(p_x_line_rec.booked_flag,'N') = 'Y' and
2973               NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_x_line_rec.ship_from_org_id) and
2974               NVL(p_x_line_rec.reserved_quantity, 0) = 0  -- ER 6110708 Do not allow item substitutions for Booked lines if line is reserved
2975             )
2976         ) -- Modified for ER 6110708, allow item substitution for Booked Lines also, but do not allow for OPM after Booking
2977      AND  p_x_line_rec.line_set_id is null
2978 	 THEN
2979 	    IF l_debug_level  > 0 THEN
2980           oe_debug_pub.add(  'Booked flag and line set id checks, continue split'  , 1 ) ;
2981         END IF;
2982 	 ELSE
2983 	    IF l_debug_level  > 0 THEN
2984           oe_debug_pub.add(  'Booked flag and line set id checks, prevent substitution split'  , 1 ) ;
2985         END IF;
2986         RETURN;
2987 	 END IF;
2988 
2989 	--Before split check if the substitution is valid
2990     -- Added below call for ER 6110708
2991     IF nvl(p_x_line_rec.booked_flag, 'N') = 'Y' THEN
2992       VALIDATE_ITEM_SUBSTITUTION
2993       (
2994         p_new_inventory_item_id => p_atp_rec.inventory_item_id(J),
2995         p_old_inventory_item_id => p_x_line_rec.inventory_item_id,
2996         p_old_ship_from_org_id => p_x_line_rec.ship_from_org_id,
2997         p_new_ship_from_org_id => p_atp_rec.Source_Organization_Id(J),
2998         p_old_shippable_flag => p_x_line_rec.shippable_flag
2999       );
3000     END IF;
3001 
3002     --call overloaded MRP_ROLLBACK API to release sub item demand in ATP
3003     MRP_ROLLBACK
3004         ( p_line_id  =>  p_x_line_rec.line_id
3005          ,p_item_id  =>  p_atp_rec.inventory_item_id(J)
3006          ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
3007          ,x_return_status    => l_return_status);
3008 
3009     l_split_line_tbl(1) := OE_ORDER_PUB.G_MISS_LINE_REC;
3010     l_split_line_tbl(1).line_id := p_x_line_rec.line_id;
3011 	--check if UOM has changed in ATP
3012     IF p_x_line_rec.order_quantity_uom <> p_atp_rec.quantity_uom(J) THEN
3013        IF l_debug_level  > 0 THEN
3014           oe_debug_pub.add(  'check_split_needed() UOM has changed, convert' , 1 ) ;
3015        END IF;
3016 
3017        l_split_line_tbl(1).ordered_quantity := INV_CONVERT.INV_UM_CONVERT(
3018                                                        p_x_line_rec.inventory_item_id -- INVCONV
3019                                                       ,5
3020                                                       ,p_atp_rec.req_item_req_date_qty(J)
3021                                                       ,p_atp_rec.quantity_uom(J)
3022                                                       ,p_x_line_rec.order_quantity_uom
3023                                                       ,NULL -- From uom name
3024                                                       ,NULL -- To uom name
3025                                                       );
3026 	ELSE
3027        l_split_line_tbl(1).ordered_quantity := p_atp_rec.req_item_req_date_qty(J);
3028 	END IF;
3029 
3030 	l_split_line_tbl(1).order_quantity_uom := p_x_line_rec.order_quantity_uom;
3031     IF p_x_line_rec.ordered_quantity2 is not null
3032 	AND p_x_line_rec.ordered_quantity2 <> FND_API.G_MISS_NUM
3033 	THEN
3034 	  l_split_line_tbl(1).ordered_quantity2 := round(p_x_line_rec.ordered_quantity2 *
3035                                                (l_split_line_tbl(1).ordered_quantity/p_x_line_rec.ordered_quantity),5) ;
3036 	END IF;
3037     l_split_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
3038     l_split_line_tbl(1).ship_from_org_id   := Nvl(p_x_line_rec.ship_from_org_id, p_atp_rec.source_organization_id(J));
3039     l_split_line_tbl(1).inventory_item_id := p_x_line_rec.inventory_item_id ; --set the original item
3040     l_split_line_tbl(1).change_reason := 'SYSTEM';
3041     IF p_order_date_type_code = 'SHIP' THEN
3042        l_split_line_tbl(1).schedule_ship_date := p_atp_rec.requested_ship_date(J);
3043     ELSE
3044        l_split_line_tbl(1).schedule_arrival_date := p_atp_rec.requested_arrival_date(J);
3045     END IF;
3046 
3047     l_split_line_tbl(2) := OE_ORDER_PUB.G_MISS_LINE_REC;
3048     l_split_line_tbl(2) := p_x_line_rec;
3049     l_split_line_tbl(2).line_id := FND_API.G_MISS_NUM;
3050     l_split_line_tbl(2).split_from_line_id := p_x_line_rec.line_id;
3051     l_split_line_tbl(2).ordered_quantity := p_x_line_rec.ordered_quantity - l_split_line_tbl(1).ordered_quantity ;
3052     --l_split_line_tbl(2).change_reason := 'SYSTEM';
3053     IF p_x_line_rec.ordered_quantity2 is not null
3054 	AND p_x_line_rec.ordered_quantity2 <> FND_API.G_MISS_NUM
3055 	THEN
3056        l_split_line_tbl(2).ordered_quantity2 := round((p_x_line_rec.ordered_quantity2 - l_split_line_tbl(1).ordered_quantity2),5) ;
3057 	END IF;
3058     l_split_line_tbl(2).operation := OE_GLOBALS.G_OPR_CREATE;
3059 
3060     OE_SPLIT_UTIL.Default_Attributes(l_split_line_tbl(2),p_x_line_rec);
3061 
3062 	l_split_line_tbl(2).order_quantity_uom := p_x_line_rec.order_quantity_uom;
3063     l_split_line_tbl(2).split_from_line_id := NULL;
3064     l_split_line_tbl(2).line_set_id := NULL;
3065     --l_split_line_tbl(2).inventory_item_id  := p_atp_rec.inventory_item_id(J); --Retain the same inv ID
3066     IF p_order_date_type_code = 'SHIP' THEN
3067 	   l_split_line_tbl(2).schedule_ship_date := p_atp_rec.requested_ship_date(J);
3068 	   l_split_line_tbl(2).schedule_arrival_date := null;
3069 	ELSE
3070        l_split_line_tbl(2).schedule_ship_date := null;
3071        l_split_line_tbl(2).schedule_arrival_date := p_atp_rec.requested_arrival_date(J);
3072 	END IF;
3073 	l_split_line_tbl(2).schedule_status_code := null;
3074 	l_split_line_tbl(2).schedule_action_code := null;
3075 	l_split_line_tbl(2).reserved_quantity := null;
3076 	l_split_line_tbl(2).reserved_quantity2 := null;
3077     l_split_line_tbl(2).ship_from_org_id   := Nvl(p_x_line_rec.ship_from_org_id, p_atp_rec.source_organization_id(J));
3078 	l_split_line_tbl(2).line_number := FND_API.G_MISS_NUM;
3079 	l_split_line_tbl(2).shipment_number := FND_API.G_MISS_NUM;
3080 	l_split_line_tbl(2).orig_sys_line_ref := FND_API.G_MISS_CHAR;
3081 
3082     IF l_debug_level  > 0 THEN
3083         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(1) : ordered_quantity ' || l_split_line_tbl(1).ordered_quantity , 1 ) ;
3084         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(2) : ordered_quantity ' || l_split_line_tbl(2).ordered_quantity , 1 ) ;
3085         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(1) : schedule_action_code ' || l_split_line_tbl(1).schedule_action_code , 1 ) ;
3086         oe_debug_pub.add(  'check_split_needed() l_split_line_tbl(2) : schedule_action_code ' || l_split_line_tbl(2).schedule_action_code , 1 ) ;
3087         oe_debug_pub.add(  'check_split_needed() oe_split_util.g_sch_recursion ' || oe_split_util.g_sch_recursion , 1 ) ;
3088     END IF;
3089 /*
3090 	--set the visible demand flag
3091     IF p_atp_rec.attribute_05.COUNT > 0 THEN
3092   	 IF p_atp_rec.attribute_05(J) IS NULL THEN
3093   		IF p_config_exists = 'N' THEN
3094   		   p_x_line_rec.visible_demand_flag   := 'Y';
3095   		ELSE
3096   		   IF l_debug_level  > 0 THEN
3097   			   oe_debug_pub.add(  'INSIDE CONFIG EXISTS' , 3 ) ;
3098   		   END IF;
3099   		END IF;
3100 
3101   	 ELSIF p_atp_rec.attribute_05(J) = 'N' THEN
3102   	   p_x_line_rec.visible_demand_flag   := 'N';
3103   	 ELSIF p_atp_rec.attribute_05(J) = 'Y' THEN
3104   	   p_x_line_rec.visible_demand_flag   := 'Y';
3105   	 END IF;
3106     ELSE
3107   	 IF p_config_exists = 'N' THEN
3108   	  p_x_line_rec.visible_demand_flag   := 'Y';
3109   	 ELSE
3110   	  IF l_debug_level  > 0 THEN
3111   		  oe_debug_pub.add(  'CONFIG EXISTS' , 3 ) ;
3112   	  END IF;
3113   	 END IF;
3114     END IF;
3115 
3116 	--update the order line so that in the DB it is scheduled
3117 	--doing the update later so that the default_attributes above
3118 	--takes the item_id from the original line
3119     update oe_order_lines_all
3120     SET  schedule_ship_date =  p_atp_rec.ship_date(J),
3121     schedule_arrival_date = p_atp_rec.arrival_date(J),
3122     schedule_status_code = 'SCHEDULED',
3123 	ship_from_org_id = p_atp_rec.Source_Organization_Id(J),
3124 	visible_demand_flag = p_x_line_rec.visible_demand_flag,
3125     delivery_lead_time = p_atp_rec.delivery_lead_time(J),
3126     mfg_lead_time = p_atp_rec.atp_lead_time(J),
3127     shipping_method_code = nvl(p_atp_rec.ship_method(J),shipping_method_code),
3128     Original_Inventory_Item_Id = p_x_line_rec.Inventory_Item_id,
3129     Original_item_identifier_Type = p_x_line_rec.item_identifier_type,
3130     Original_ordered_item_id = p_x_line_rec.ordered_item_id ,
3131     Original_ordered_item  = p_x_line_rec.ordered_item,
3132 	inventory_item_id = p_atp_rec.inventory_item_id(J),
3133     item_identifier_type = 'INT'
3134     WHERE line_id = p_x_line_rec.line_id;
3135 */
3136     --l_sch_recursion := oe_split_util.g_sch_recursion;
3137 
3138     --oe_split_util.g_sch_recursion := 'FALSE';
3139 
3140     l_control_rec.validate_entity		:= TRUE; --REMOVE
3141     l_control_rec.check_security		:= TRUE; --REMOVE
3142     l_control_rec.process               := TRUE;
3143     l_control_rec.controlled_operation  := TRUE;
3144 
3145 	/*
3146     l_control_rec.controlled_operation := TRUE;
3147     l_control_rec.change_attributes    := TRUE;
3148 
3149     l_control_rec.clear_dependents     := TRUE;
3150     l_control_rec.default_attributes   := TRUE;
3151     l_control_rec.check_security       := TRUE;
3152 
3153     l_control_rec.write_to_DB          := TRUE;
3154     l_control_rec.validate_entity      := TRUE;
3155     l_control_rec.process              := FALSE;
3156     */
3157     --  Instruct API to retain its caches
3158 
3159     l_control_rec.clear_api_cache      := FALSE;
3160     l_control_rec.clear_api_requests   := FALSE;
3161 
3162 	G_GOP_AUTO_SPLIT := TRUE;
3163 	G_GOP_AUTO_SPLIT_LINE_ID := p_x_line_rec.line_id;
3164 
3165 	--Set this so that even after booking item can be substituted.
3166 	OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y'; --REMOVE
3167 
3168     OE_Order_PVT.Process_Order
3169     (
3170         p_api_version_number        => 1.0
3171     ,    x_return_status                => l_return_status
3172     ,    x_msg_count                    => l_msg_count
3173     ,    x_msg_data                    => l_msg_data
3174     ,    p_x_line_tbl                => l_split_line_tbl
3175     ,    p_control_rec                => l_control_rec
3176     ,    p_x_header_rec                => l_header_out_rec
3177     ,    p_x_header_adj_tbl            => l_header_adj_out_tbl
3178     ,    p_x_header_scredit_tbl        => l_header_scredit_out_tbl
3179     ,    p_x_header_payment_tbl        => l_header_payment_out_tbl
3180     ,    p_x_line_adj_tbl            => l_line_adj_out_tbl
3181     ,    p_x_line_scredit_tbl        => l_line_scredit_out_tbl
3182     ,    p_x_line_payment_tbl        => l_line_payment_out_tbl
3183     ,    p_x_action_request_tbl        => l_action_request_out_tbl
3184     ,    p_x_lot_serial_tbl            => l_lot_serial_tbl
3185     ,    p_x_header_price_att_tbl    => l_header_price_att_tbl
3186     ,    p_x_header_adj_att_tbl        => l_header_adj_att_tbl
3187     ,    p_x_header_adj_assoc_tbl    => l_header_adj_assoc_tbl
3188     ,    p_x_line_price_att_tbl        => l_line_price_att_tbl
3189     ,    p_x_line_adj_att_tbl        => l_line_adj_att_tbl
3190     ,    p_x_line_adj_assoc_tbl        => l_line_adj_assoc_tbl
3191     );
3192 
3193     --oe_split_util.g_sch_recursion := l_sch_recursion ;
3194 
3195     IF l_debug_level  > 0 THEN
3196         oe_debug_pub.add(  'check_split_needed() substitute item : returned from process order to split - subs item : same item split ' || l_return_status  , 1 ) ;
3197 		oe_debug_pub.add(  'check_split_needed() substitute item : new line Id : ' || l_split_line_tbl(2).line_id , 1 ) ;
3198     END IF;
3199 
3200     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3201 	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3202 	ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
3203 		  RAISE FND_API.G_EXC_ERROR;
3204 	END IF;
3205 
3206 /*
3207 	--Call PO API to update newly created line with new inventory item id
3208     l_update_line_tbl(1) := OE_ORDER_PUB.G_MISS_LINE_REC;
3209     l_update_line_tbl(1).line_id := l_split_line_tbl(2).line_id;
3210     l_update_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
3211     l_update_line_tbl(1).change_reason := 'SYSTEM';
3212     l_update_line_tbl(1).ship_from_org_id   := Nvl(p_x_line_rec.ship_from_org_id, p_atp_rec.source_organization_id(J));
3213     l_update_line_tbl(1).inventory_item_id := p_atp_rec.inventory_item_id(J) ; --substitute item
3214 	IF p_order_date_type_code = 'SHIP' THEN
3215 	   l_update_line_tbl(1).schedule_ship_date := p_atp_rec.requested_ship_date(J);
3216 	ELSE
3217 	   l_update_line_tbl(1).schedule_arrival_date := p_atp_rec.requested_arrival_date(J);
3218     END IF;
3219 
3220 	OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y';
3221 
3222     OE_Order_PVT.Process_Order
3223     (
3224         p_api_version_number        => 1.0
3225     ,    x_return_status                => l_return_status
3226     ,    x_msg_count                    => l_msg_count
3227     ,    x_msg_data                    => l_msg_data
3228     ,    p_x_line_tbl                => l_update_line_tbl
3229     ,    p_control_rec                => l_control_rec
3230     ,    p_x_header_rec                => l_header_out_rec
3231     ,    p_x_header_adj_tbl            => l_header_adj_out_tbl
3232     ,    p_x_header_scredit_tbl        => l_header_scredit_out_tbl
3233     ,    p_x_header_payment_tbl        => l_header_payment_out_tbl
3234     ,    p_x_line_adj_tbl            => l_line_adj_out_tbl
3235     ,    p_x_line_scredit_tbl        => l_line_scredit_out_tbl
3236     ,    p_x_line_payment_tbl        => l_line_payment_out_tbl
3237     ,    p_x_action_request_tbl        => l_action_request_out_tbl
3238     ,    p_x_lot_serial_tbl            => l_lot_serial_tbl
3239     ,    p_x_header_price_att_tbl    => l_header_price_att_tbl
3240     ,    p_x_header_adj_att_tbl        => l_header_adj_att_tbl
3241     ,    p_x_header_adj_assoc_tbl    => l_header_adj_assoc_tbl
3242     ,    p_x_line_price_att_tbl        => l_line_price_att_tbl
3243     ,    p_x_line_adj_att_tbl        => l_line_adj_att_tbl
3244     ,    p_x_line_adj_assoc_tbl        => l_line_adj_assoc_tbl
3245     );
3246 
3247     IF l_debug_level  > 0 THEN
3248         oe_debug_pub.add(  'check_split_needed() substitute item : returned from process order to update to substitute item ' || l_return_status  , 1 ) ;
3249     END IF;
3250 
3251     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3252 	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3253 	ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
3254 		  RAISE FND_API.G_EXC_ERROR;
3255 	END IF;
3256 */
3257 	G_GOP_AUTO_SPLIT := FALSE;
3258 	G_GOP_AUTO_SPLIT_LINE_ID := 0;
3259 	OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N'; --REMOVE
3260 /*
3261     FND_MESSAGE.SET_NAME('ONT','OE_SCH_ITEM_CHANGE');
3262     FND_MESSAGE.SET_TOKEN('OLDITEM',p_x_line_rec.ordered_item);
3263     FND_MESSAGE.SET_TOKEN('NEWITEM', p_atp_rec.inventory_item_name(J));
3264     OE_MSG_PUB.Add;
3265 */
3266     p_split_line_rec := l_split_line_tbl(1);
3267 
3268 
3269   END IF;
3270 
3271   /*
3272   IF l_debug_level  > 0 THEN
3273 	  for l_rec in (select * from oe_schedule_lines_temp)
3274 	  LOOP
3275 	    oe_debug_pub.add('check_split_needed() GLOBAL TEMP TABLE : ' || l_rec.line_id || ' : ' || l_rec.schedule_action_code,1);
3276 	  END LOOP;
3277   END IF;
3278   */
3279 EXCEPTION
3280 
3281    WHEN OTHERS THEN
3282       --Do not raise error, Rollback and set the delayed request table
3283       --x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3284       IF l_debug_level  > 0 THEN
3285          oe_debug_pub.add(  'check_split_needed() exception in check_split_needed() : ' || SQLERRM , 1 ) ;
3286       END IF;
3287 
3288 	  --MRP ROLLBACK LOGIC
3289 	  --CALL mrp_rollback() TO UNSCHEDULE all lines with SCHEDULE MODE in temp table
3290 	  IF l_split_line_tbl.count > 1 AND l_split_line_tbl(2).line_id IS NOT NULL
3291 	  AND l_split_line_tbl(2).line_id <> FND_API.G_MISS_NUM  THEN
3292 	  BEGIN
3293         FOR rec in c_mrp_rollback(l_split_line_tbl(2).line_id)
3294         LOOP
3295          IF l_debug_level  > 0 THEN
3296             oe_debug_pub.add(  'check_split_needed() MRP ROLLBACK for  : line_id  '||rec.line_id ||'  ' ||rec.schedule_action_code);
3297          END IF;
3298 
3299          IF rec.schedule_action_code = 'SCHEDULE' THEN
3300              MRP_ROLLBACK
3301               ( p_line_id  =>  rec.line_id
3302                ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
3303                ,x_return_status    => l_return_status);
3304                --8731703
3305                --IF OE_SCH_CONC_REQUESTS.g_conc_program = 'Y' THEN
3306                  -- OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(rec.line_id) := 'N';
3307                --END IF;
3308 			EXIT; --only once is enough.
3309          END IF;
3310         END LOOP;
3311       EXCEPTION
3312       WHEN OTHERS THEN
3313         NULL;
3314       END;
3315 	  END IF;
3316 
3317       ROLLBACK TO SPLIT_SCHEDULING;
3318 
3319 	  --CALL mrp_rollback() TO REDEMAND LINE 1
3320 	  --If for line 1, there is SCHEDULE action, then UNSCHEDULE
3321 	  --Else if for line 1, there is REDEMAND, then REDAMD again
3322 
3323       BEGIN
3324          IF l_debug_level  > 0 THEN
3325             oe_debug_pub.add(  'check_split_needed() MRP ROLLBACK for  : line_id  '||p_x_line_rec.line_id || ' : UNSCHEDULE');
3326          END IF;
3327         --REDEMAND THE ORIGINAL LINE
3328         FOR rec in c_mrp_rollback(p_x_line_rec.line_id)
3329         LOOP
3330          IF l_debug_level  > 0 THEN
3331             oe_debug_pub.add(  'check_split_needed() MRP ROLLBACK for  : line_id  '||rec.line_id ||'  ' ||rec.schedule_action_code);
3332          END IF;
3333 
3334          IF rec.schedule_action_code = 'SCHEDULE' THEN
3335              MRP_ROLLBACK
3336               ( p_line_id  =>  rec.line_id
3337                ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
3338                ,x_return_status    => l_return_status);
3339                --8731703
3340                IF OE_SCH_CONC_REQUESTS.g_conc_program = 'Y' THEN
3341                   OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(rec.line_id) := 'N';
3342                END IF;
3343 			l_mrp_rollback_schedule := 1;
3344 			EXIT; --only once is enough.
3345          END IF;
3346         END LOOP;
3347 
3348       EXCEPTION
3349       WHEN OTHERS THEN
3350         NULL;
3351       END;
3352 
3353 	  IF l_mrp_rollback_schedule = 0  THEN --REDEMAND ONLY IF THERE IS NO SCHEDULE ACTION(previous block)
3354       BEGIN
3355          IF l_debug_level  > 0 THEN
3356             oe_debug_pub.add(  'check_split_needed() MRP ROLLBACK for  : line_id  '||p_x_line_rec.line_id || ' : REDEMAND');
3357          END IF;
3358         --REDEMAND THE ORIGINAL LINE
3359         FOR rec in c_mrp_rollback(p_x_line_rec.line_id)
3360         LOOP
3361          IF l_debug_level  > 0 THEN
3362             oe_debug_pub.add(  'check_split_needed() MRP ROLLBACK for  : line_id  '||rec.line_id ||'  ' ||rec.schedule_action_code);
3363          END IF;
3364 
3365          IF rec.schedule_action_code = 'REDEMAND' THEN
3366              MRP_ROLLBACK
3367               ( p_line_id  =>  rec.line_id
3368                ,p_schedule_action_code  =>  'REDEMAND'
3369                ,x_return_status    => l_return_status);
3370                --8731703
3371                IF OE_SCH_CONC_REQUESTS.g_conc_program = 'Y' THEN
3372                   OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(rec.line_id) := 'N';
3373                END IF;
3374 			EXIT; --only once is enough.
3375          END IF;
3376         END LOOP;
3377 
3378       EXCEPTION
3379       WHEN OTHERS THEN
3380         NULL;
3381       END;
3382       END IF;
3383 
3384       G_GOP_AUTO_SPLIT := FALSE;
3385 	  G_GOP_AUTO_SPLIT_LINE_ID := 0;
3386 	  OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N'; --REMOVE
3387 
3388       OE_Delayed_Requests_PVT.g_delayed_requests := l_bckup_request_tbl;
3389       x_return_status :=  FND_API.G_RET_STS_ERROR;
3390 	  --RAISE FND_API.G_RET_STS_ERROR;
3391 END check_split_needed;
3392 
3393 /*--------------------------------------------------------------------
3394 Procedure Name : Load_MRP_request
3395 Description    :
3396   This procedure loads the MRP record or tables to be passed
3397   to MRP's API from the OM's table of records of order lines.
3398 
3399   If line line to be passed to MRP is an ATO model, we call
3400   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
3401   components, and we pass them along with the ATO model to MRP.
3402 -------------------------------------------------------------------- */
3403 Procedure Load_MRP_request
3404 ( p_line_rec           IN  OE_ORDER_PUB.line_rec_type
3405  ,p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type
3406  ,p_sch_action         IN  VARCHAR2 := NULL
3407  ,p_mrp_calc_sd        IN  VARCHAR2
3408  ,p_type_code          IN  VARCHAR2
3409  ,p_order_number       IN  NUMBER
3410  ,p_partial_set        IN  BOOLEAN := FALSE
3411  ,p_config_line_id     IN  NUMBER  := NULL
3412  ,p_part_of_set        IN  VARCHAR2 DEFAULT 'N' -- 4405004
3413  ,p_index              IN OUT NOCOPY NUMBER
3414  ,x_atp_rec            IN OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
3415 IS
3416   I                   NUMBER := p_index;
3417   l_insert_flag       NUMBER;
3418   l_oe_flag           VARCHAR2(1);
3419   l_inv_ctp           VARCHAR2(240);
3420   l_explode           BOOLEAN;
3421   l_st_atp_lead_time  NUMBER;
3422   l_st_ato_line_id    NUMBER;
3423   l_ship_set          VARCHAR2(30);
3424   l_arrival_set       VARCHAR2(30);
3425   l_action_code       VARCHAR2(30);
3426   l_action            NUMBER;
3427   l_organization_id   NUMBER;
3428   l_inventory_item_id NUMBER;
3429   l_result            NUMBER := 1;
3430 
3431   l_model_rec         MRP_ATP_PUB.ATP_Rec_Typ;
3432   l_smc_rec           MRP_ATP_PUB.ATP_Rec_Typ;
3433 
3434   lTableName          VARCHAR2(30);
3435   lMessageName        VARCHAR2(30);
3436   lErrorMessage       VARCHAR2(2000);
3437   l_line_id_mod       NUMBER ; --7827737
3438 
3439   l_model_override_atp_date_code VARCHAR2(30);
3440   --
3441   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
3442   --
3443 Begin
3444 
3445   IF l_debug_level  > 0 THEN
3446       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.LOAD_MRP_REQUEST' , 1 ) ;
3447   END IF;
3448   IF l_debug_level  > 0 THEN
3449       oe_debug_pub.add('------------Load MRP Table-----------',1);
3450   END IF;
3451 
3452     IF l_action_code <> OESCH_ACT_ATP_CHECK THEN
3453        G_ATP_CHECK_Session_Id := Null;
3454     END IF;
3455 
3456     IF p_sch_action IS NULL THEN
3457         l_action_code := p_line_rec.schedule_action_code;
3458     ELSE
3459         l_action_code := p_sch_action;
3460     END IF;
3461 
3462     IF nvl(p_mrp_calc_sd,'N') = 'Y' THEN
3463       l_insert_flag   := 1;
3464     ELSE
3465       l_insert_flag   := 0;
3466     END IF;
3467 
3468     x_atp_rec.atp_lead_time(I)   := 0;
3469 
3470     IF l_debug_level  > 0 THEN
3471        oe_debug_pub.add('Line Id              : ' || p_line_rec.line_id,3);
3472     END IF;
3473     IF l_debug_level  > 0 THEN
3474        oe_debug_pub.add('Schedule Action      : ' || l_action_code,3);
3475     END IF;
3476 
3477 /*
3478     IF p_line_rec.source_document_type_id = 10 THEN
3479       oe_debug_pub.add('It is an internal order ',3);
3480       l_oe_flag := 'Y';
3481 
3482       IF (p_line_rec.schedule_ship_date IS NOT NULL AND
3483           p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE ) OR
3484          (p_line_rec.schedule_arrival_date IS NOT NULL AND
3485           p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE ) THEN
3486 
3487           oe_debug_pub.add('No changes to date as it has been passed',3);
3488       ELSE
3489           oe_debug_pub.add('Pass the request date as arrival date',3);
3490 
3491           x_atp_rec.Requested_ship_Date(I)  := null;
3492           x_atp_rec.Requested_arrival_Date(I) := p_line_rec.request_date;
3493 
3494       END IF;
3495 
3496       x_atp_rec.attribute_01(I) := p_line_rec.source_document_id;
3497 
3498     ELSE
3499 
3500       oe_debug_pub.add('It is not an internal order ',3);
3501       l_oe_flag := 'N';
3502 
3503     END IF;
3504 
3505     x_atp_rec.oe_flag(I) := l_oe_flag;
3506     oe_debug_pub.add('OE Flag is : '||x_atp_rec.oe_flag(I),3);
3507 */
3508 
3509     IF p_line_rec.arrival_set_id is null
3510     THEN
3511       l_arrival_set := p_line_rec.arrival_set;
3512     ELSE
3513       l_arrival_set := nvl(p_line_rec.arrival_set,to_char(p_line_rec.arrival_set_id));
3514     END IF;
3515 
3516     IF  p_line_rec.ship_set_id is null
3517     THEN
3518       l_ship_set := p_line_rec.ship_set;
3519     ELSE
3520       l_ship_set := nvl(p_line_rec.ship_set,to_char(p_line_rec.ship_set_id));
3521     END IF;
3522 
3523     IF l_arrival_set = FND_API.G_MISS_CHAR THEN
3524        l_arrival_set := Null;
3525     END IF;
3526     IF l_ship_set = FND_API.G_MISS_CHAR THEN
3527        l_ship_set := Null;
3528     END IF;
3529     IF l_debug_level  > 0 THEN
3530         oe_debug_pub.add(  'SHIP_SET : ' || L_SHIP_SET , 3 ) ;
3531     END IF;
3532     IF l_debug_level  > 0 THEN
3533         oe_debug_pub.add(  'ARRIVAL SET : ' || L_ARRIVAL_SET , 3 ) ;
3534     END IF;
3535 
3536 
3537 /*      IF (p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
3538             p_line_rec.ship_from_org_id := null;
3539         END IF;
3540         IF (p_old_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
3541             p_old_line_rec.ship_from_org_id := null;
3542         END IF;
3543 */
3544 /*
3545 
3546         IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
3547                                 p_old_line_rec.ship_from_org_id) OR
3548                (p_line_rec.re_source_flag = 'N')
3549 */
3550 
3551     x_atp_rec.Inventory_Item_Id(I)         := p_line_rec.inventory_item_id;
3552 
3553     IF (p_line_rec.ship_from_org_id IS NOT NULL) THEN
3554       x_atp_rec.Source_Organization_Id(I)
3555                       := p_line_rec.ship_from_org_id;
3556       IF l_debug_level  > 0 THEN
3557           oe_debug_pub.add(  'SHIP FROM : ' || P_LINE_REC.SHIP_FROM_ORG_ID , 3 ) ;
3558       END IF;
3559 
3560     ELSE
3561       x_atp_rec.Source_Organization_Id(I) := null;
3562       IF l_debug_level  > 0 THEN
3563           oe_debug_pub.add(  'SHIP FROM IS NULL ' , 3 ) ;
3564       END IF;
3565     END IF;
3566 
3567     x_atp_rec.Identifier(I)                := p_line_rec.line_id;
3568     x_atp_rec.Order_Number(I)              := p_order_number;
3569     x_atp_rec.Calling_Module(I)            := 660;
3570     x_atp_rec.Customer_Id(I)               := p_line_rec.sold_to_org_id;
3571     x_atp_rec.Customer_Site_Id(I)          := p_line_rec.ship_to_org_id;
3572     x_atp_rec.Destination_Time_Zone(I)     := p_line_rec.item_type_code;
3573     x_atp_rec.Quantity_Ordered(I)          := p_line_rec.ordered_quantity;
3574     x_atp_rec.Quantity_UOM(I)              := p_line_rec.order_quantity_uom;
3575     x_atp_rec.Earliest_Acceptable_Date(I)  := null;
3576 
3577     IF l_debug_level  > 0 THEN
3578         oe_debug_pub.add(  'A1 : ' || P_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
3579     END IF;
3580     IF l_debug_level  > 0 THEN
3581         oe_debug_pub.add(  'A2 : ' || P_OLD_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
3582     END IF;
3583 
3584     -- Start 2691579 --
3585     IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
3586        IF  (p_line_rec.operation = OE_GLOBALS.G_OPR_DELETE)
3587        AND (p_line_rec.ato_line_id IS NOT NULL AND
3588        NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
3589             p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
3590                                           OE_GLOBALS.G_ITEM_OPTION,
3591 					  OE_GLOBALS.G_ITEM_INCLUDED))) THEN --9775352
3592           x_atp_rec.ato_delete_flag(I) := 'Y';
3593           IF l_debug_level  > 0 THEN
3594              oe_debug_pub.add(  'ATO DELETE FLAG : '
3595                              || x_atp_rec.ato_delete_flag(I) , 1 ) ;
3596           END IF;
3597        END IF;
3598     END IF;
3599     -- End 2691579 --
3600 
3601 /* Commented the following condition to fix the bug 2823868 */
3602 /* The problem is that if someone has set order date type as arrival
3603    and the puts the line in a ship set , then p_line_rec.ship_set_id will be not null
3604    so the schedule_ship_date will be passed to the atp record, where as arrival_date should have
3605    passed to the arrival record */
3606 /*
3607     IF p_line_rec.arrival_set_id is not null THEN
3608 
3609       IF l_debug_level  > 0 THEN
3610           oe_debug_pub.add(  'T1' , 1 ) ;
3611       END IF;
3612       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
3613          x_atp_rec.Requested_Arrival_Date(I) :=
3614                                  p_line_rec.request_date;
3615 
3616       ELSE
3617         x_atp_rec.Requested_Arrival_Date(I) :=
3618                                p_line_rec.schedule_arrival_date;
3619       END IF;
3620       x_atp_rec.Requested_Ship_Date(I)    := null;
3621 
3622     ELSIF  p_line_rec.ship_set_id is not null THEN
3623 
3624       IF l_debug_level  > 0 THEN
3625           oe_debug_pub.add(  'T2' , 1 ) ;
3626       END IF;
3627       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
3628          x_atp_rec.Requested_Ship_Date(I)    :=
3629                                p_line_rec.request_date;
3630       ELSE
3631          x_atp_rec.Requested_Ship_Date(I)    :=
3632                                p_line_rec.schedule_ship_date;
3633       END IF;
3634       x_atp_rec.Requested_Arrival_Date(I) := null;
3635 */
3636 
3637     IF (p_type_code = 'ARRIVAL') THEN
3638 
3639       -- If user changes schedule_arrival_date then schedule based
3640       -- on the arrival_date. Otherwise look for the change in request date.
3641       -- If user changed request date, schedule based on the request
3642       -- date. Otherwise if the scheduling is happening because of
3643       -- some other changes, use nvl on arrival_date and request_dates.
3644 
3645       IF l_debug_level  > 0 THEN
3646           oe_debug_pub.add(  'T3' , 1 ) ;
3647       END IF;
3648 
3649       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
3650          x_atp_rec.Requested_Arrival_Date(I) :=
3651                                  p_line_rec.request_date;
3652 
3653       ELSE
3654 
3655        IF  p_line_rec.schedule_status_code is not null
3656        AND p_line_rec.ship_model_complete_flag = 'Y'
3657        AND smc_overridden(p_line_rec.top_model_line_id) THEN
3658 
3659            x_atp_rec.Requested_Arrival_Date(I) :=
3660                      p_line_rec.schedule_arrival_date;
3661 
3662        ELSE -- not overridden.
3663 
3664         IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
3665                                 p_old_line_rec.schedule_arrival_date) AND
3666            p_line_rec.schedule_arrival_date IS NOT NULL AND
3667            p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE
3668         THEN
3669            x_atp_rec.Requested_Arrival_Date(I) :=
3670                      p_line_rec.schedule_arrival_date;
3671 
3672         ELSIF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
3673                                  p_old_line_rec.request_date) AND
3674             p_line_rec.request_date IS NOT NULL AND
3675             p_line_rec.request_date <> FND_API.G_MISS_DATE AND
3676             nvl(p_line_rec.override_atp_date_code,'N') = 'N' AND
3677             --Bug 6057897
3678             --Added the below condition to prevent rescheduling based on request date, if scheduling is triggered due to
3679             --some other changes. For example change in Order quantity will trigger scheduling, but request date change
3680             --should not be honoured.
3681             NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
3682         THEN
3683             x_atp_rec.Requested_Arrival_Date(I) :=
3684                        p_line_rec.request_date;
3685         ELSE
3686             x_atp_rec.Requested_Arrival_Date(I) :=
3687              nvl(p_line_rec.schedule_arrival_date,p_line_rec.request_date);
3688         END IF;
3689        END IF; --  overridden
3690       END IF; --atp
3691 
3692       x_atp_rec.Requested_Ship_Date(I)    := null;
3693       IF l_debug_level  > 0 THEN
3694          oe_debug_pub.add(  'REQ ARR DATE : ' ||X_ATP_REC.REQUESTED_ARRIVAL_DATE ( I ) , 3 ) ;
3695       END IF;
3696 
3697     ELSE
3698       -- If user changes schedule_ship_date then schedule based
3699       -- on the ship_date. Otherwise look for the change in request date.
3700       -- If user changed request date, schedule based on the request
3701       -- date. Otherwise if the scheduling is happening because of
3702       -- some other changes, use nvl on schedule_ship and request_dates.
3703 
3704       IF l_debug_level  > 0 THEN
3705           oe_debug_pub.add(  'T4' , 1 ) ;
3706       END IF;
3707       IF p_line_rec.schedule_action_code = OESCH_ACT_ATP_CHECK THEN
3708          x_atp_rec.Requested_Ship_Date(I)    :=
3709                                p_line_rec.request_date;
3710       ELSE
3711        IF  p_line_rec.schedule_status_code is not null
3712        AND p_line_rec.ship_model_complete_flag = 'Y'
3713        AND smc_overridden(p_line_rec.top_model_line_id) THEN
3714 
3715            x_atp_rec.Requested_Ship_Date(I) :=
3716                           p_line_rec.schedule_ship_date;
3717        ELSE -- Not overridden
3718 
3719         IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
3720                                 p_old_line_rec.schedule_ship_date) AND
3721            p_line_rec.schedule_ship_date IS NOT NULL AND
3722            p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE
3723         THEN
3724           x_atp_rec.Requested_Ship_Date(I) :=
3725                           p_line_rec.schedule_ship_date;
3726 
3727         ELSIF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
3728                                    p_old_line_rec.request_date) AND
3729               p_line_rec.request_date IS NOT NULL AND
3730               p_line_rec.request_date <> FND_API.G_MISS_DATE AND
3731               nvl(p_line_rec.override_atp_date_code,'N') = 'N' AND
3732               --Bug 6057897
3733               NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y'
3734         THEN
3735           x_atp_rec.Requested_Ship_Date(I) :=
3736                         p_line_rec.request_date;
3737         ELSE
3738           x_atp_rec.Requested_Ship_Date(I)    :=
3739            nvl(p_line_rec.schedule_ship_date,p_line_rec.request_date);
3740 
3741         END IF; -- sch ship date changed.
3742        END IF; -- Overridden
3743       END IF; --Atp
3744 
3745       x_atp_rec.Requested_Arrival_Date(I)  := null;
3746                                      IF l_debug_level  > 0 THEN
3747                                          oe_debug_pub.add(  'REQ SHIP DATE : ' || X_ATP_REC.REQUESTED_SHIP_DATE ( I ) , 3 ) ;
3748                                      END IF;
3749 
3750     END IF;
3751 
3752 
3753     IF p_partial_set THEN
3754 
3755       -- If the line is part of a set and we are rescheduling it
3756       -- just by itself, we should not let MRP change the date.
3757       -- Thus we will pass null Latest_Acceptable_Date
3758 
3759       x_atp_rec.Latest_Acceptable_Date(I)  := null;
3760     ELSE
3761 
3762       x_atp_rec.Latest_Acceptable_Date(I)  :=
3763                        p_line_rec.latest_acceptable_date;
3764     END IF;
3765 
3766                             IF l_debug_level  > 0 THEN
3767                                 oe_debug_pub.add(  'LATEST ACCEPTABLE DATE :' || X_ATP_REC.LATEST_ACCEPTABLE_DATE ( I ) , 1 ) ;
3768                             END IF;
3769 
3770     x_atp_rec.Delivery_Lead_Time(I)     := Null;
3771     --x_atp_rec.Delivery_Lead_Time(I)     := p_line_rec.delivery_lead_time;
3772                             IF l_debug_level  > 0 THEN
3773                                 oe_debug_pub.add(  'DELIVERYLEAD TIME :' || X_ATP_REC.DELIVERY_LEAD_TIME ( I ) , 1 ) ;
3774                             END IF;
3775     x_atp_rec.Freight_Carrier(I)        := null;
3776     x_atp_rec.Ship_Method(I)            := p_line_rec.shipping_method_code;
3777     x_atp_rec.Demand_Class(I)           := p_line_rec.demand_class_code;
3778     x_atp_rec.Ship_Set_Name(I)          := l_ship_set;
3779     x_atp_rec.Arrival_Set_Name(I)       := l_arrival_set;
3780     -- 4405004
3781     x_atp_rec.part_of_set(I)            := p_part_of_set;
3782     IF l_debug_level  > 0 THEN
3783        oe_debug_pub.add(  'PART OF SET FLAG :' || X_ATP_REC.part_of_set( I ) , 1 ) ;
3784     END IF;
3785 
3786     IF G_OVERRIDE_FLAG = 'Y' THEN
3787       x_atp_rec.Override_Flag(I)     := 'Y';
3788     ELSE
3789       IF l_action_code <> OESCH_ACT_ATP_CHECK THEN
3790         x_atp_rec.Override_Flag(I)     := p_line_rec.override_atp_date_code;
3791       END IF;
3792       IF l_debug_level  > 0 THEN
3793           oe_debug_pub.add(  'OVERRIDE_FLAG :' || X_ATP_REC.OVERRIDE_FLAG ( I ) , 1 ) ;
3794       END IF;
3795     END IF;
3796 
3797     x_atp_rec.Ship_Date(I)              := null;
3798     x_atp_rec.Available_Quantity(I)     := null;
3799     x_atp_rec.Requested_Date_Quantity(I) := null;
3800     x_atp_rec.Group_Ship_Date(I)        := null;
3801     x_atp_rec.Group_Arrival_Date(I)     := null;
3802     x_atp_rec.Vendor_Id(I)              := null;
3803     x_atp_rec.Vendor_Site_Id(I)         := null;
3804     x_atp_rec.Insert_Flag(I)            := l_insert_flag;
3805     IF l_debug_level  > 0 THEN
3806         oe_debug_pub.add(  'INSERT FLAG IN ATP_REC : '||X_ATP_REC.INSERT_FLAG ( I ) , 3 ) ;
3807     END IF;
3808     x_atp_rec.Error_Code(I)             := null;
3809     x_atp_rec.Message(I)                := null;
3810 
3811 
3812     IF p_line_rec.source_document_type_id = 10 THEN
3813       IF l_debug_level  > 0 THEN
3814           oe_debug_pub.add(  'IT IS AN INTERNAL ORDER ' , 3 ) ;
3815       END IF;
3816       l_oe_flag := 'Y';
3817 
3818       IF (p_line_rec.schedule_ship_date IS NOT NULL AND
3819           p_line_rec.schedule_ship_date <> FND_API.G_MISS_DATE ) OR
3820          (p_line_rec.schedule_arrival_date IS NOT NULL AND
3821           p_line_rec.schedule_arrival_date <> FND_API.G_MISS_DATE ) THEN
3822 
3823           IF l_debug_level  > 0 THEN
3824               oe_debug_pub.add(  'NO CHANGES TO DATE AS IT HAS BEEN PASSED' , 3 ) ;
3825           END IF;
3826       ELSE
3827           IF l_debug_level  > 0 THEN
3828               oe_debug_pub.add(  'PASS THE REQUEST DATE AS ARRIVAL DATE' , 3 ) ;
3829           END IF;
3830 
3831           x_atp_rec.Requested_ship_Date(I)  := null;
3832           x_atp_rec.Requested_arrival_Date(I) := p_line_rec.request_date;
3833 
3834       END IF;
3835 
3836       x_atp_rec.attribute_01(I) := p_line_rec.source_document_id;
3837 
3838     ELSE
3839 
3840       IF l_debug_level  > 0 THEN
3841           oe_debug_pub.add(  'IT IS NOT AN INTERNAL ORDER ' , 3 ) ;
3842       END IF;
3843       l_oe_flag := 'N';
3844 
3845     END IF;
3846 
3847     x_atp_rec.oe_flag(I) := l_oe_flag;
3848     IF l_debug_level  > 0 THEN
3849         oe_debug_pub.add(  'OE FLAG IS : '||X_ATP_REC.OE_FLAG ( I ) , 3 ) ;
3850     END IF;
3851 
3852                              IF l_debug_level  > 0 THEN
3853                                  oe_debug_pub.add(  'REQUEST SHIP DATE : ' || TO_CHAR ( X_ATP_REC.REQUESTED_SHIP_DATE ( I ) , 'DD-MON-RR:HH:MM:SS' ) , 3 ) ;
3854                              END IF;
3855                              IF l_debug_level  > 0 THEN
3856                                  oe_debug_pub.add(  'REQUEST ARRIVAL DATE : ' || TO_CHAR ( X_ATP_REC.REQUESTED_ARRIVAL_DATE ( I ) , 'DD-MON-RR:HH:MM:SS' ) , 3 ) ;
3857                              END IF;
3858 
3859     IF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_ATP_CHECK) THEN
3860       x_atp_rec.Action(I)                    := 100;
3861       IF l_debug_level  > 0 THEN
3862           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
3863       END IF;
3864     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_DEMAND) OR
3865           (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_SCHEDULE)
3866     THEN
3867       x_atp_rec.Action(I)                    := 110;
3868       IF l_debug_level  > 0 THEN
3869           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
3870       END IF;
3871     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_REDEMAND) OR
3872           (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE)
3873     THEN
3874       x_atp_rec.Action(I)                     := 120;
3875       x_atp_rec.Old_Source_Organization_Id(I) :=
3876                           p_old_line_rec.ship_from_org_id;
3877       x_atp_rec.Old_Demand_Class(I)           :=
3878                           p_old_line_rec.demand_class_code;
3879       IF l_debug_level  > 0 THEN
3880           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
3881       END IF;
3882     ELSIF (l_action_code = OE_SCHEDULE_UTIL.OESCH_ACT_UNDEMAND)
3883     THEN
3884       x_atp_rec.Action(I)                    := 120;
3885       x_atp_rec.Quantity_Ordered(I)          := 0;
3886       x_atp_rec.Old_Source_Organization_Id(I) :=
3887                             p_old_line_rec.ship_from_org_id;
3888       x_atp_rec.Old_Demand_Class(I)           :=
3889                             p_old_line_rec.demand_class_code;
3890 
3891       /*L.G. OPM bug 1828340 jul 19,01*/
3892       IF l_debug_level  > 0 THEN
3893           oe_debug_pub.add(  'MRP ACTION: ' || X_ATP_REC.ACTION ( I ) , 3 ) ;
3894       END IF;
3895       -- Bug3361870 (commenting this piece of code. Not required)
3896      /*  IF INV_GMI_RSV_BRANCH.Process_Branch   -- INVCONV - delete this
3897          (p_organization_id => p_old_line_rec.ship_from_org_id)
3898       THEN
3899         Update oe_order_lines_all
3900         Set ordered_quantity = 0,
3901             ordered_quantity2 = 0
3902         Where line_id=p_old_line_rec.line_id;
3903       END IF; */
3904     END IF; -- action=**
3905 
3906 IF OE_SCH_CONC_REQUESTS.g_conc_program = 'Y' THEN -- 9108353
3907     -- Schords (R12 Project #6403)
3908     IF p_line_rec.ship_model_complete_flag = 'N' AND
3909        p_line_rec.top_model_line_id IS NOT NULL AND
3910        (p_line_rec.ato_line_id IS NULL OR
3911         p_line_rec.ato_line_id <> p_line_rec.top_model_line_id) AND
3912        p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_INCLUDED THEN
3913 
3914        l_line_id_mod := MOD(p_line_rec.line_id,G_BINARY_LIMIT); --7827737
3915        --IF NOT OE_SCH_CONC_REQUESTS.included_processed(p_line_rec.line_id) THEN
3916        IF NOT OE_SCH_CONC_REQUESTS.included_processed(l_line_id_mod) THEN -- 9108353
3917           IF l_debug_level  > 0 THEN
3918              oe_debug_pub.add(  'INCLUDED PROCESSED : ' || p_line_rec.line_id, 3 ) ;
3919           END IF;
3920           --5166476
3921 
3922           --OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_line_rec.line_id) := 'Y';
3923           OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(l_line_id_mod) :='Y';--7827737
3924           --OE_SCH_CONC_REQUESTS.g_process_records := OE_SCH_CONC_REQUESTS.g_process_records + 1;
3925        END IF;
3926     ELSE
3927        l_line_id_mod := MOD(p_line_rec.line_id,G_BINARY_LIMIT); --7827737
3928        IF OE_SCH_CONC_REQUESTS.g_recorded = 'N' THEN -- 5166476
3929           --5166476
3930 
3931           --OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_line_rec.line_id) := 'Y';
3932           OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(l_line_id_mod) := 'Y';--7827737
3933           --OE_SCH_CONC_REQUESTS.g_process_records
3934           --       := OE_SCH_CONC_REQUESTS.g_process_records + 1;
3935        END IF;
3936     END IF;
3937 END IF; -- 9108353
3938 
3939     -- storing in local var to assing action to ato mandatory components
3940     -- to fix bug 1947539.
3941 
3942     l_action := x_atp_rec.Action(I);
3943     x_atp_rec.atp_lead_time(I)   := 0;
3944     IF p_line_rec.ato_line_id is not null AND
3945        p_line_rec.line_id <> p_line_rec.ato_line_id
3946     THEN
3947 
3948       -- This lines is a ato option or class.
3949       -- Set the atp_lead_time for it.
3950 
3951       IF p_line_rec.ato_line_id = l_st_ato_line_id
3952       THEN
3953         x_atp_rec.atp_lead_time(I)   := l_st_atp_lead_time;
3954         IF l_debug_level  > 0 THEN
3955             oe_debug_pub.add(  'ATO LEAD TIME IS ' || L_ST_ATP_LEAD_TIME , 3 ) ;
3956         END IF;
3957       ELSE
3958         IF l_debug_level  > 0 THEN
3959             oe_debug_pub.add(  'CALLING GET_LEAD_TIME' , 3 ) ;
3960         END IF;
3961         l_st_atp_lead_time :=
3962                      Get_Lead_Time
3963                      (p_ato_line_id      => p_line_rec.ato_line_id,
3964                       p_ship_from_org_id => p_line_rec.ship_from_org_id);
3965 
3966         IF l_debug_level  > 0 THEN
3967             oe_debug_pub.add(  'AFTER CALLING GET_LEAD_TIME' , 3 ) ;
3968         END IF;
3969         IF l_debug_level  > 0 THEN
3970             oe_debug_pub.add(  'LEAD TIME: ' || L_ST_ATP_LEAD_TIME , 3 ) ;
3971         END IF;
3972 
3973         x_atp_rec.atp_lead_time(I)   := l_st_atp_lead_time;
3974         l_st_ato_line_id := p_line_rec.ato_line_id;
3975       END IF;
3976     END IF;
3977 
3978     -- Item Substitution Code.
3979 
3980    -- Sunbstitution will be supported only for
3981    -- Standard not ATO item.
3982    -- Before booking
3983    -- Non internal order item
3984    -- Not on split line
3985 
3986     IF l_debug_level  > 0 THEN
3987         oe_debug_pub.add(  'ITEM_TYPE_CODE :' || P_LINE_REC.ITEM_TYPE_CODE , 1 ) ;
3988     END IF;
3989     IF l_debug_level  > 0 THEN
3990         oe_debug_pub.add(  'ATO_LINE_ID :' || P_LINE_REC.ATO_LINE_ID , 1 ) ;
3991     END IF;
3992     IF l_debug_level  > 0 THEN
3993         oe_debug_pub.add(  'BOOKED_FLAG :' || P_LINE_REC.BOOKED_FLAG , 1 ) ;
3994     END IF;
3995     IF l_debug_level  > 0 THEN
3996         oe_debug_pub.add(  'LINE_SET_ID :' || P_LINE_REC.LINE_SET_ID , 1 ) ;
3997     END IF;
3998     IF l_debug_level  > 0 THEN
3999         oe_debug_pub.add(  'SOURCE_DOCUMENT_TYPE_ID :' || P_LINE_REC.SOURCE_DOCUMENT_TYPE_ID , 1 ) ;
4000     END IF;
4001 
4002     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
4003                             p_old_line_rec.inventory_item_id)
4004     AND p_line_rec.schedule_status_code is NOT NULL THEN
4005 
4006      l_line_id_mod := MOD(p_line_rec.line_id,G_BINARY_LIMIT); --7827737
4007 
4008      --IF OE_Item_Tbl.EXISTS(p_line_rec.line_id) THEN
4009      IF OE_Item_Tbl.EXISTS(l_line_id_mod) THEN    --7827737
4010         x_atp_rec.old_inventory_item_id(I) :=
4011                      OE_Item_Tbl(l_line_id_mod).inventory_item_id;   --7827737
4012                      --OE_Item_Tbl(p_line_rec.line_id).inventory_item_id;
4013         --OE_Item_Tbl.DELETE(p_line_rec.line_id);
4014         OE_Item_Tbl.DELETE(l_line_id_mod);  --7827737
4015      ELSE
4016         x_atp_rec.old_inventory_item_id(I) :=
4017                                       p_old_line_rec.inventory_item_id;
4018     END IF;
4019      IF l_debug_level  > 0 THEN
4020          oe_debug_pub.add(  'OLD_ITEM ID :' || X_ATP_REC.OLD_INVENTORY_ITEM_ID ( I ) , 1 ) ;
4021      END IF;
4022     END IF;
4023 
4024     IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
4025 
4026     --  Bug fix 2331427
4027      IF   p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD
4028      AND  p_line_rec.ato_line_id is null
4029      AND  ( NVL(p_line_rec.booked_flag,'N') = 'N' OR
4030             ( NVL(p_line_rec.booked_flag,'N') = 'Y' and
4031               NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_line_rec.ship_from_org_id) and
4032               NVL(p_line_rec.reserved_quantity, 0) = 0  -- ER 6110708 Do not allow item substitutions for Booked lines if line is reserved
4033             )
4034           ) -- Modified for ER 6110708, allow item substitution for Booked Lines also, but do not allow for OPM after Booking
4035      AND  p_line_rec.line_set_id is null
4036      AND  nvl(p_line_rec.source_document_type_id,-99) <> 10
4037      THEN
4038         --  Set substution code to 1 when OM can allow substitution on th
4039         -- line. For atp check get the values from MRP to show the substitute
4040         -- item details by setting req_item_detail_flag to 1.
4041 
4042         IF l_debug_level  > 0 THEN
4043             oe_debug_pub.add(  'LINE IS MARKED FOR SUBSTITUTION' , 1 ) ;
4044         END IF;
4045          x_atp_rec.substitution_typ_code(I) := 1;
4046                   IF l_action_code = OESCH_ACT_ATP_CHECK OR
4047             NVL(OE_SYS_PARAMETERS.value('AUTO_SPLIT_AT_SCHEDULING'),'N') <> 'N' --ER 2736896
4048          THEN
4049             x_atp_rec.req_item_detail_flag(I) := 1;
4050          ELSE
4051            x_atp_rec.req_item_detail_flag(I) := 2;
4052          END IF;
4053 
4054      ELSE
4055         IF l_debug_level  > 0 THEN
4056             oe_debug_pub.add(  'LINE IS NOT MARKED FOR SUBSTITUTION' , 1 ) ;
4057         END IF;
4058          x_atp_rec.substitution_typ_code(I) := 4;
4059          x_atp_rec.req_item_detail_flag(I) := 2;
4060 
4061      END IF;
4062 
4063     END IF;
4064 
4065     IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
4066     AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
4067 
4068       IF l_debug_level  > 0 THEN
4069        Oe_debug_pub.add('Do not explode SMC records',2);
4070       END IF;
4071 
4072       x_atp_rec.Included_item_flag(I)  := 1;
4073       x_atp_rec.top_model_line_id(I)   := p_line_rec.top_model_line_id;
4074       x_atp_rec.ato_model_line_id(I)   := p_line_rec.ato_line_id;
4075       x_atp_rec.parent_line_id(I)      := p_line_rec.link_to_line_id;
4076       x_atp_rec.validation_org(I)      :=
4077                   OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
4078       x_atp_rec.component_code(I)      :=  p_line_rec.component_code;
4079       x_atp_rec.component_sequence_id(I) := p_line_rec.component_sequence_id;
4080       x_atp_rec.line_number(I) :=
4081           OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(p_line_rec.line_id);
4082 
4083 
4084       IF(p_line_rec.ato_line_id IS NOT NULL AND
4085       NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
4086            p_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
4087                                          OE_GLOBALS.G_ITEM_OPTION,
4088 					 OE_GLOBALS.G_ITEM_INCLUDED))) THEN --9775352
4089 
4090          x_atp_rec.config_item_line_id(I) := p_config_line_id ;
4091 
4092       END IF;
4093 
4094     ELSE
4095 
4096     l_inv_ctp :=  fnd_profile.value('INV_CTP');
4097 
4098     IF l_debug_level  > 0 THEN
4099         oe_debug_pub.add(  'INV_CTP : '||L_INV_CTP , 3 ) ;
4100     END IF;
4101 
4102 
4103     IF p_line_rec.ato_line_id = p_line_rec.line_id AND
4104        (p_line_rec.item_type_code in ('MODEL','CLASS') OR
4105        (p_line_rec.item_type_code in ('STANDARD','OPTION','INCLUDED') AND --9775352
4106         l_inv_ctp = '5')) THEN
4107 
4108        l_explode := TRUE;
4109        -- Added this code to fix bug 1998613.
4110        IF p_line_rec.schedule_status_code is not null
4111           AND nvl(p_line_rec.ordered_quantity,0) <
4112                   p_old_line_rec.ordered_quantity
4113           AND p_old_line_rec.reserved_quantity > 0
4114           AND NOT Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
4115                                              p_old_line_rec => p_old_line_rec)
4116           THEN
4117 
4118             IF l_debug_level  > 0 THEN
4119              oe_debug_pub.add('ONLY ORDERED QTY GOT REDUCED,NO EXPLOSION',3);
4120             END IF;
4121              l_explode := FALSE;
4122 
4123        END IF;
4124        IF l_explode THEN
4125     -- If the line scheduled is an ATO Model, call ATO's API
4126     -- to get the Standard Mandatory Components
4127 
4128         IF l_debug_level  > 0 THEN
4129             oe_debug_pub.add('ATO ITEM TYPE: '||P_LINE_REC.ITEM_TYPE_CODE,3);
4130         END IF;
4131 
4132         IF  p_line_rec.item_type_code = 'STANDARD' AND
4133             x_atp_rec.ship_set_name(I) is NULL THEN
4134 
4135             IF l_debug_level  > 0 THEN
4136                 oe_debug_pub.add(  'ASSIGNING SHIP SET FOR ATO ITEM ' , 3 ) ;
4137             END IF;
4138             x_atp_rec.Ship_Set_Name(I)          := p_line_rec.ato_line_id;
4139 
4140         END IF;
4141 
4142         IF p_line_rec.item_type_code = 'STANDARD' THEN
4143 
4144            IF l_debug_level  > 0 THEN
4145                oe_debug_pub.add(  'ASSIGNING WAREHOUSE AND ITEM ' , 3 ) ;
4146            END IF;
4147            l_organization_id := p_line_rec.ship_from_org_id;
4148            l_inventory_item_id := p_line_rec.inventory_item_id;
4149            IF l_debug_level  > 0 THEN
4150               oe_debug_pub.add('WAREHOUSE/ITEM : '||L_ORGANIZATION_ID||'/' ||L_INVENTORY_ITEM_ID,3 ) ;
4151            END IF;
4152 
4153         ELSE
4154 
4155           l_organization_id := NULL;
4156         l_inventory_item_id := NULL;
4157           IF l_debug_level  > 0 THEN
4158              oe_debug_pub.add('WAREHOUSE/ITEM : '||L_ORGANIZATION_ID||'/' ||L_INVENTORY_ITEM_ID,3);
4159            END IF;
4160 
4161   END IF;
4162 
4163         --Load Model Rec to pass to ATO's API
4164         --Load Model Rec to pass to ATO's API
4165 
4166         l_model_rec.Inventory_Item_Id := MRP_ATP_PUB.number_arr
4167                             (x_atp_rec.Inventory_Item_Id(I));
4168 
4169         l_model_rec.Source_Organization_Id := MRP_ATP_PUB.number_arr
4170                             (x_atp_rec.Source_Organization_Id(I));
4171 
4172         l_model_rec.Identifier := MRP_ATP_PUB.number_arr
4173                             (x_atp_rec.Identifier(I));
4174 
4175         l_model_rec.Calling_Module := MRP_ATP_PUB.number_arr
4176                             (x_atp_rec.Calling_Module(I));
4177 
4178         l_model_rec.Customer_Id := MRP_ATP_PUB.number_arr
4179                             (x_atp_rec.Customer_Id(I));
4180 
4181         l_model_rec.Customer_Site_Id := MRP_ATP_PUB.number_arr
4182                             (x_atp_rec.Customer_Site_Id(I));
4183 
4184         l_model_rec.Destination_Time_Zone := MRP_ATP_PUB.char30_arr
4185                             (x_atp_rec.Destination_Time_Zone(I));
4186 
4187         l_model_rec.Quantity_Ordered := MRP_ATP_PUB.number_arr
4188                             (x_atp_rec.Quantity_Ordered(I));
4189 
4190         l_model_rec.Quantity_UOM := MRP_ATP_PUB.char3_arr
4191                             (x_atp_rec.Quantity_UOM(I));
4192 
4193         l_model_rec.Earliest_Acceptable_Date := MRP_ATP_PUB.date_arr
4194                             (x_atp_rec.Earliest_Acceptable_Date(I));
4195 
4196         l_model_rec.Requested_Ship_Date := MRP_ATP_PUB.date_arr
4197                             (x_atp_rec.Requested_Ship_Date(I));
4198 
4199         l_model_rec.Requested_Arrival_Date := MRP_ATP_PUB.date_arr
4200                             (x_atp_rec.Requested_Arrival_Date(I));
4201 
4202         l_model_rec.Latest_Acceptable_Date := MRP_ATP_PUB.date_arr
4203                             (x_atp_rec.Latest_Acceptable_Date(I));
4204 
4205         l_model_rec.Delivery_Lead_Time := MRP_ATP_PUB.number_arr
4206                             (x_atp_rec.Delivery_Lead_Time(I));
4207 
4208         l_model_rec.Atp_lead_Time := MRP_ATP_PUB.number_arr
4209                             (x_atp_rec.Atp_lead_Time(I));
4210 
4211         l_model_rec.Freight_Carrier := MRP_ATP_PUB.char30_arr
4212                             (x_atp_rec.Freight_Carrier(I));
4213 
4214         l_model_rec.Ship_Method := MRP_ATP_PUB.char30_arr
4215                             (x_atp_rec.Ship_Method(I));
4216 
4217         l_model_rec.Demand_Class := MRP_ATP_PUB.char30_arr
4218                             (x_atp_rec.Demand_Class(I));
4219 
4220         l_model_rec.Ship_Set_Name := MRP_ATP_PUB.char30_arr
4221                             (x_atp_rec.Ship_Set_Name(I));
4222 
4223         l_model_rec.Arrival_Set_Name := MRP_ATP_PUB.char30_arr
4224                             (x_atp_rec.Arrival_Set_Name(I));
4225 
4226         l_model_rec.Override_Flag := MRP_ATP_PUB.char1_arr
4227                             (x_atp_rec.Override_Flag(I));
4228 
4229         l_model_rec.Ship_Date := MRP_ATP_PUB.date_arr
4230                             (x_atp_rec.Ship_Date(I));
4231 
4232         l_model_rec.Available_Quantity := MRP_ATP_PUB.number_arr
4233                             (x_atp_rec.Available_Quantity(I));
4234 
4235         l_model_rec.Requested_Date_Quantity := MRP_ATP_PUB.number_arr
4236                             (x_atp_rec.Requested_Date_Quantity(I));
4237 
4238         l_model_rec.Group_Ship_Date := MRP_ATP_PUB.date_arr
4239                             (x_atp_rec.Group_Ship_Date(I));
4240 
4241         l_model_rec.Group_Arrival_Date := MRP_ATP_PUB.date_arr
4242                             (x_atp_rec.Group_Arrival_Date(I));
4243 
4244         l_model_rec.Vendor_Id := MRP_ATP_PUB.number_arr
4245                             (x_atp_rec.Vendor_Id(I));
4246 
4247         l_model_rec.Vendor_Site_Id := MRP_ATP_PUB.number_arr
4248                             (x_atp_rec.Vendor_Site_Id(I));
4249 
4250         l_model_rec.Insert_Flag := MRP_ATP_PUB.number_arr
4251                             (x_atp_rec.Insert_Flag(I));
4252 
4253         l_model_rec.Error_Code := MRP_ATP_PUB.number_arr
4254                             (x_atp_rec.Error_Code(I));
4255 
4256         l_model_rec.Message := MRP_ATP_PUB.char2000_arr
4257                             (x_atp_rec.Message(I));
4258 
4259         l_model_rec.Action  := MRP_ATP_PUB.number_arr
4260                             (x_atp_rec.action(I));
4261 
4262         l_model_rec.order_number  := MRP_ATP_PUB.number_arr
4263                             (x_atp_rec.order_number(I));
4264 
4265         IF x_atp_rec.Old_Source_Organization_Id.Exists(I) THEN
4266            l_model_rec.Old_Source_Organization_Id := MRP_ATP_PUB.number_arr
4267                                (x_atp_rec.Old_Source_Organization_Id(I));
4268         END IF;
4269 
4270         IF x_atp_rec.Old_Demand_Class.Exists(I) THEN
4271            l_model_rec.Old_Demand_Class  :=
4272                          MRP_ATP_PUB.char30_arr(x_atp_rec.Old_Demand_Class(I));
4273         END IF;
4274 
4275         BEGIN
4276              IF l_debug_level  > 0 THEN
4277                  oe_debug_pub.add(  '2.. CALLING CTO GET_BOM_MANDATORY_COMPS' , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
4278              END IF;
4279 
4280              l_result := CTO_CONFIG_ITEM_PK.GET_MANDATORY_COMPONENTS
4281                          (p_ship_set           => l_model_rec,
4282                           p_organization_id    => l_organization_id,
4283                           p_inventory_item_id  => l_inventory_item_id,
4284                           x_smc_rec            => l_smc_rec,
4285                           xErrorMessage        => lErrorMessage,
4286                           xMessageName         => lMessageName,
4287                           xTableName           => lTableName);
4288 
4289              IF l_debug_level  > 0 THEN
4290                  oe_debug_pub.add(  '2..AFTER CALLING CTO API : ' || L_RESULT , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
4291              END IF;
4292 
4293         EXCEPTION
4294             WHEN OTHERS THEN
4295                 IF l_debug_level  > 0 THEN
4296                     oe_debug_pub.add(  'CTO API RETURNED AN UNEXPECTED ERROR' ) ;
4297                 END IF;
4298                 l_result := 0;
4299         END;
4300 
4301         IF l_result = 1 AND
4302            l_smc_rec.Identifier.count >= 1 THEN
4303                               IF l_debug_level  > 0 THEN
4304                                   oe_debug_pub.add(  'SMC COUNT IS : ' || L_SMC_REC.IDENTIFIER.COUNT , 1 ) ;
4305                               END IF;
4306 
4307            Initialize_mrp_record(p_x_atp_rec => x_atp_rec,
4308                                  l_count   => l_smc_rec.Identifier.count);
4309 
4310            FOR J IN 1..l_smc_rec.Identifier.count LOOP
4311             I := I + 1;
4312             -- Added atp_lead_time, order Number to fix bug 1560461.
4313             x_atp_rec.atp_lead_time(I)   := 0;
4314             x_atp_rec.oe_flag(I) := l_oe_flag;
4315 
4316            -- As part of the bug fix 2910899, OM will indicate and remember the
4317            -- Standard Madatory record positions using vendor_name. This will be
4318            -- used in the load_results procedure to bypass the SMC records.
4319 
4320             x_atp_rec.vendor_name(I) := 'SMC';
4321             IF l_debug_level  > 0 THEN
4322                 oe_debug_pub.add(  'OE FLAG IS : '||X_ATP_REC.OE_FLAG ( I ) , 3 ) ;
4323                  oe_debug_pub.add(  'SMC  : '|| I || ' iden :' ||
4324                                 l_smc_rec.Identifier(J) , 3 ) ;
4325             END IF;
4326 
4327             x_atp_rec.Inventory_Item_Id(I)      := l_smc_rec.Inventory_Item_Id(J);
4328             x_atp_rec.Source_Organization_Id(I) :=
4329                                        l_smc_rec.Source_Organization_Id(J);
4330 
4331             x_atp_rec.Identifier(I)             := l_smc_rec.Identifier(J);
4332             x_atp_rec.Order_Number(I)           := p_order_number;
4333             x_atp_rec.Calling_Module(I)         := l_smc_rec.Calling_Module(J);
4334             x_atp_rec.Customer_Id(I)            := l_smc_rec.Customer_Id(J);
4335             x_atp_rec.Customer_site_Id(I)       := l_smc_rec.Customer_site_Id(J);
4336             x_atp_rec.Destination_Time_Zone(I)  :=
4337                                        l_smc_rec.Destination_Time_Zone(J);
4338             x_atp_rec.Quantity_Ordered(I)       := l_smc_rec.Quantity_Ordered(J);
4339             x_atp_rec.Quantity_UOM(I)           := l_smc_rec.Quantity_UOM(J);
4340             x_atp_rec.Earliest_Acceptable_Date(I) :=
4341                                        l_smc_rec.Earliest_Acceptable_Date(J);
4342             x_atp_rec.Requested_Ship_Date(I)    :=
4343                                        l_smc_rec.Requested_Ship_Date(J);
4344             x_atp_rec.Requested_Arrival_Date(I) :=
4345                                        l_smc_rec.Requested_Arrival_Date(J);
4346             x_atp_rec.Latest_Acceptable_Date(I) :=
4347                                        l_smc_rec.Latest_Acceptable_Date(J);
4348             x_atp_rec.Delivery_Lead_Time(I)     :=
4349                                        l_smc_rec.Delivery_Lead_Time(J);
4350             x_atp_rec.Freight_Carrier(I)        :=
4351                                        l_smc_rec.Freight_Carrier(J);
4352             x_atp_rec.Ship_Method(I)            :=
4353                                        l_smc_rec.Ship_Method(J);
4354             x_atp_rec.Demand_Class(I)           :=
4355                                        l_smc_rec.Demand_Class(J);
4356             x_atp_rec.Ship_Set_Name(I)          :=
4357                                        l_smc_rec.Ship_Set_Name(J);
4358             x_atp_rec.Arrival_Set_Name(I)       :=
4359                                        l_smc_rec.Arrival_Set_Name(J);
4360             x_atp_rec.Override_Flag(I)          :=
4361                                        l_smc_rec.Override_Flag(J);
4362             x_atp_rec.Ship_Date(I)              :=
4363                                        l_smc_rec.Ship_Date(J);
4364             x_atp_rec.Available_Quantity(I)     :=
4365                                        l_smc_rec.Available_Quantity(J);
4366             x_atp_rec.Requested_Date_Quantity(I):=
4367                                        l_smc_rec.Requested_Date_Quantity(J);
4368             x_atp_rec.Group_Ship_Date(I)        :=
4369                                        l_smc_rec.Group_Ship_Date(J);
4370             x_atp_rec.Group_Arrival_Date(I)     :=
4371                                        l_smc_rec.Group_Arrival_Date(J);
4372             x_atp_rec.Vendor_Id(I)              :=
4373                                        l_smc_rec.Vendor_Id(J);
4374             x_atp_rec.Vendor_Site_Id(I)         :=
4375                                        l_smc_rec.Vendor_Site_Id(J);
4376             x_atp_rec.Insert_Flag(I)            :=
4377                                        l_smc_rec.Insert_Flag(J);
4378             x_atp_rec.atp_lead_time(I)           :=
4379                                        l_smc_rec.atp_lead_time(J);
4380             x_atp_rec.Error_Code(I)  := l_smc_rec.Error_Code(J);
4381             x_atp_rec.Message(I)     := l_smc_rec.Message(J);
4382             x_atp_rec.Action(I)      := l_action;
4383 
4384             x_atp_rec.Old_Source_Organization_Id(I) :=
4385                           l_smc_rec.Old_Source_Organization_Id(J);
4386             x_atp_rec.Old_Demand_Class(I)           :=
4387                           l_smc_rec.Old_Demand_Class(J);
4388 
4389             -- Schords (R12 Project #6403)
4390             OE_SCH_CONC_REQUESTS.g_process_records := OE_SCH_CONC_REQUESTS.g_process_records + 1;
4391             IF l_debug_level  > 0 THEN
4392                oe_debug_pub.add(  'INCLUDED TO PROCESSED : ' , 3 ) ;
4393             END IF;
4394 
4395            END LOOP;
4396         END IF; -- Identifier count is greater than 1.
4397        END IF; -- l_explode.
4398     END IF;  -- Need to explode smc.
4399 
4400     END IF; -- Code control
4401     P_index := I;
4402 
4403 
4404 EXCEPTION
4405    WHEN FND_API.G_EXC_ERROR THEN
4406 
4407      Raise FND_API.G_EXC_ERROR;
4408    WHEN OTHERS THEN
4409 
4410         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4411         THEN
4412             OE_MSG_PUB.Add_Exc_Msg
4413             (   G_PKG_NAME,
4414               'Load_MRP_request');
4415         END IF;
4416 
4417 End Load_MRP_request;
4418 
4419 /*--------------------------------------------------------------------
4420 Procedure Name : Load_MRP_request_from_rec
4421 Description    :
4422   This procedure loads the MRP record or tables to be passed
4423   to MRP's API from the OM's table of records of order lines.
4424 
4425   If line line to be passed to MRP is an ATO model, we call
4426   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
4427   components, and we pass them along with the ATO model to MRP.
4428 -------------------------------------------------------------------- */
4429 Procedure Load_MRP_request_from_rec
4430 ( p_line_rec           IN  OE_ORDER_PUB.Line_rec_Type
4431  ,p_old_line_rec       IN  OE_ORDER_PUB.Line_rec_Type
4432  ,p_sch_action         IN  VARCHAR2 := NULL
4433 ,x_mrp_atp_rec OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
4434 
4435 IS
4436   l_mrp_calc_sd       VARCHAR2(240);
4437   l_type_code         VARCHAR2(30);
4438   l_order_number      NUMBER;
4439   l_index             NUMBER := 1;
4440   --
4441   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
4442   --
4443 BEGIN
4444 
4445   l_mrp_calc_sd :=  fnd_profile.value('MRP_ATP_CALC_SD');
4446 
4447   IF l_debug_level  > 0 THEN
4448       oe_debug_pub.add(  'MRP_ATP_CALC_SD : '||L_MRP_CALC_SD , 3 ) ;
4449   END IF;
4450 
4451   l_type_code    := Get_Date_Type(p_line_rec.header_id);
4452   l_order_number := Get_Order_Number(p_line_rec.header_id);
4453 
4454 
4455     IF p_line_rec.item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
4456 
4457        Initialize_mrp_record
4458        (p_x_atp_rec => x_mrp_atp_rec,
4459         l_count     => 1);
4460 
4461 
4462       Load_MRP_request
4463       ( p_line_rec       => p_line_rec
4464        ,p_old_line_rec   => p_old_line_rec
4465        ,p_sch_action     => p_sch_action
4466        ,p_mrp_calc_sd    => l_mrp_calc_sd
4467        ,p_type_code      => l_type_code
4468        ,p_order_number   => l_order_number
4469        ,p_index          => l_index
4470        ,x_atp_rec        => x_mrp_atp_rec);
4471 
4472     END IF;
4473 
4474 
4475 EXCEPTION
4476    WHEN FND_API.G_EXC_ERROR THEN
4477 
4478      Raise FND_API.G_EXC_ERROR;
4479 
4480    WHEN OTHERS THEN
4481 
4482         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4483         THEN
4484             OE_MSG_PUB.Add_Exc_Msg
4485             (   G_PKG_NAME,
4486               'Log_Set_Request');
4487         END IF;
4488 
4489 END Load_MRP_request_from_rec;
4490 
4491 /*--------------------------------------------------------------------
4492 Procedure Name : Load_MRP_request_from_tbl
4493 Description    :
4494   This procedure loads the MRP record or tables to be passed
4495   to MRP's API from the OM's table of records of order lines.
4496 
4497   If line line to be passed to MRP is an ATO model, we call
4498   CTO's GET_MANDATORY_COMPONENTS API to get the mandatory
4499   components, and we pass them along with the ATO model to MRP.
4500 
4501 -------------------------------------------------------------------- */
4502 Procedure Load_MRP_request_from_tbl
4503 ( p_line_tbl           IN  OE_ORDER_PUB.Line_Tbl_Type
4504  ,p_old_line_tbl       IN  OE_ORDER_PUB.Line_Tbl_Type
4505  ,p_partial_set        IN  BOOLEAN := FALSE
4506  ,p_sch_action         IN  VARCHAR2 := NULL
4507  ,p_part_of_set        IN  VARCHAR2 DEFAULT 'N' -- 4405004
4508 ,x_mrp_atp_rec OUT NOCOPY MRP_ATP_PUB.ATP_Rec_Typ)
4509 
4510 IS
4511   l_mrp_calc_sd       VARCHAR2(240);
4512   l_type_code         VARCHAR2(30);
4513   l_order_number      NUMBER;
4514   l_index             NUMBER := 0;
4515   l_config_count      NUMBER := 0;
4516   l_config_line_id    NUMBER;
4517   l_ato_line_id       NUMBER := -999;
4518 
4519   -- BUG 1955004
4520   l_inactive_demand_count NUMBER:= 0;
4521   l_scheduling_level_code VARCHAR2(30);
4522   l_line_id_mod       NUMBER;   --7827737
4523 
4524   --
4525   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
4526   --
4527 BEGIN
4528 
4529   IF l_debug_level  > 0 THEN
4530       oe_debug_pub.add(  'ENTERING LOAD_MRP_REQUEST_FROM_TBL' , 1 ) ;
4531   END IF;
4532   l_mrp_calc_sd :=  fnd_profile.value('MRP_ATP_CALC_SD');
4533 
4534   IF l_debug_level  > 0 THEN
4535       oe_debug_pub.add(  'MRP_ATP_CALC_SD : '||L_MRP_CALC_SD , 3 ) ;
4536   END IF;
4537 
4538   IF l_debug_level  > 0 THEN
4539       oe_debug_pub.add(  'COUNT OF NEW ' || P_LINE_TBL.COUNT , 1 ) ;
4540   END IF;
4541   IF l_debug_level  > 0 THEN
4542       oe_debug_pub.add(  'COUNT OF NEW ' || P_OLD_LINE_TBL.COUNT , 1 ) ;
4543   END IF;
4544   IF l_debug_level  > 0 THEN
4545       oe_debug_pub.add(  'FIRST ' || P_LINE_TBL.FIRST ) ;
4546   END IF;
4547   IF l_debug_level  > 0 THEN
4548       oe_debug_pub.add(  'LAST ' || P_LINE_TBL.LAST ) ;
4549   END IF;
4550 
4551   l_type_code    := Get_Date_Type(p_line_tbl(1).header_id);
4552   l_order_number := Get_Order_Number(p_line_tbl(1).header_id);
4553 
4554   -- When config line is created on the model, we should not extend the
4555   -- ato table for config line, since we do not pass config line to MRP.
4556 
4557   IF p_sch_action = OESCH_ACT_RESCHEDULE OR
4558      p_sch_action = OESCH_ACT_UNSCHEDULE OR
4559      p_sch_action = OESCH_ACT_ATP_CHECK THEN
4560      FOR  cnt IN 1..p_line_tbl.count LOOP
4561 
4562         IF p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_CONFIG THEN
4563 
4564           l_config_count := l_config_count + 1;
4565 
4566         END IF;
4567 
4568      END LOOP;
4569   END IF;
4570 
4571    --BUG 1955004
4572   IF p_sch_action <> OESCH_ACT_ATP_CHECK THEN
4573   -- loop through the records to identify which need to bypass scheduling
4574 
4575     FOR I in 1..p_line_tbl.count LOOP
4576 
4577       IF l_debug_level  > 0 THEN
4578           oe_debug_pub.add(  'HEADER_ID IS : ' || P_LINE_TBL ( I ) .HEADER_ID ) ;
4579       END IF;
4580       IF l_debug_level  > 0 THEN
4581           oe_debug_pub.add(  'LINE_TYPE_ID IS : ' || P_LINE_TBL ( I ) .LINE_TYPE_ID ) ;
4582       END IF;
4583 
4584       l_scheduling_level_code := Get_Scheduling_Level(p_line_tbl(I).header_id,
4585                                                    P_line_tbl(I).line_type_id);
4586 
4587       IF l_scheduling_level_code IS NULL THEN
4588          l_scheduling_level_code := SCH_LEVEL_THREE;
4589       END IF;
4590       IF l_debug_level  > 0 THEN
4591           oe_debug_pub.add(  'SCHEDULING LEVEL IS : ' || L_SCHEDULING_LEVEL_CODE ) ;
4592       END IF;
4593 
4594       --3763015
4595       IF l_scheduling_level_code = SCH_LEVEL_FOUR OR
4596          l_scheduling_level_code = SCH_LEVEL_FIVE  OR
4597          NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y' OR
4598           Nvl(p_line_tbl(i).bypass_sch_flag, 'N') = 'Y' THEN  -- for DOO Integration
4599 
4600          IF l_debug_level  > 0 THEN
4601              oe_debug_pub.add(  'INACTIVE DEMAND LINE , LINE_ID = ' || P_LINE_TBL.LAST ) ;
4602          END IF;
4603 
4604          l_line_id_mod :=MOD(p_line_tbl(I).line_id,G_BINARY_LIMIT); --7827737
4605 
4606          --OE_inactive_demand_tbl(p_line_tbl(I).line_id).line_id:=
4607          --                       P_line_tbl(I).line_id;
4608          OE_inactive_demand_tbl(l_line_id_mod).line_id:=
4609                                  P_line_tbl(I).line_id;              --7827737
4610          --OE_inactive_demand_tbl(p_line_tbl(I).line_id).scheduling_level_code:=
4611          --                       l_scheduling_level_code;
4612          OE_inactive_demand_tbl(l_line_id_mod).scheduling_level_code:=
4613                            l_scheduling_level_code;              --7827737
4614 
4615          IF l_debug_level  > 0 THEN
4616              oe_debug_pub.add(  'INACTIVE DEMAND LINE' ) ;
4617          END IF;
4618                         IF l_debug_level  > 0 THEN
4619                            -- oe_debug_pub.add(  'LINE_ID = ' || OE_INACTIVE_DEMAND_TBL ( P_LINE_TBL ( I ) .LINE_ID ) .LINE_ID ) ;
4620                             oe_debug_pub.add(  'LINE_ID = ' || OE_INACTIVE_DEMAND_TBL (L_LINE_ID_MOD ) .LINE_ID ); --7827737;
4621                         END IF;
4622                         IF l_debug_level  > 0 THEN
4623                             --oe_debug_pub.add(  'SCHEDULING_LEVEL_CODE =' || OE_INACTIVE_DEMAND_TBL ( P_LINE_TBL ( I ) .LINE_ID ) .SCHEDULING_LEVEL_CODE ) ;
4624                             oe_debug_pub.add(  'SCHEDULING_LEVEL_CODE =' || OE_INACTIVE_DEMAND_TBL (L_LINE_ID_MOD ) .SCHEDULING_LEVEL_CODE ) ; --7827737
4625                         END IF;
4626 
4627          l_inactive_demand_count := l_inactive_demand_count + 1;
4628 
4629        END IF;
4630 
4631     END LOOP;
4632   END IF;
4633   --END 1955004
4634 
4635   Initialize_mrp_record
4636   (p_x_atp_rec  => x_mrp_atp_rec,
4637    l_count      => p_line_tbl.count - l_config_count - l_inactive_demand_count);  -- 1955004 added last minus
4638 
4639   FOR  cnt IN 1..p_line_tbl.count LOOP
4640 
4641     IF p_line_tbl(cnt).item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
4642 
4643        --1955004
4644       --IF OE_inactive_demand_tbl.EXISTS(p_line_tbl(cnt).line_id) THEN
4645        IF  OE_inactive_demand_tbl.EXISTS(MOD(p_line_tbl(cnt).line_id,G_BINARY_LIMIT))
4646         THEN --7827737
4647           IF l_debug_level  > 0 THEN
4648               oe_debug_pub.add(  'SKIPPING LOAD_MRP_REQUEST BECAUSE INACTIVE DEMAND LINE' ) ;
4649           END IF;
4650         NULL; -- skip this record
4651       ELSE
4652       --END 1955004
4653 
4654       IF l_config_count > 0
4655       AND p_line_tbl(cnt).ato_line_id is not null
4656       AND p_line_tbl(cnt).ato_line_id <> l_ato_line_id
4657       AND NOT(p_line_tbl(cnt).ato_line_id = p_line_tbl(cnt).line_id
4658           AND (p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_STANDARD
4659           OR   p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_OPTION
4660 	  OR p_line_tbl(cnt).item_type_code = OE_GLOBALS.G_ITEM_INCLUDED)) --9775352
4661       THEN
4662 
4663           l_ato_line_id := p_line_tbl(cnt).ato_line_id;
4664 
4665 
4666           BEGIN
4667 
4668             Select line_id
4669             Into   l_config_line_id
4670             From   oe_order_lines_all
4671             Where  ato_line_id = p_line_tbl(cnt).ato_line_id
4672             And    item_type_code = 'CONFIG';
4673 
4674           EXCEPTION
4675 
4676             WHEN Others THEN
4677 
4678               l_config_line_id := Null;
4679 
4680           END;
4681 
4682       END IF;
4683 
4684       l_index := l_index + 1;
4685 
4686       Load_MRP_request
4687       ( p_line_rec       => p_line_tbl(cnt)
4688        ,p_old_line_rec   => p_old_line_tbl(cnt)
4689        ,p_sch_action     => p_sch_action
4690        ,p_mrp_calc_sd    => l_mrp_calc_sd
4691        ,p_type_code      => l_type_code
4692        ,p_order_number   => l_order_number
4693        ,p_partial_set    => p_partial_set
4694        ,p_config_line_id => l_config_line_id
4695        ,p_part_of_set    => p_part_of_set --4405004
4696        ,p_index          => l_index
4697        ,x_atp_rec        => x_mrp_atp_rec);
4698 
4699       END IF; -- 1955004 check for inactive_demand
4700     END IF;
4701 
4702   END LOOP;
4703 
4704   IF l_debug_level  > 0 THEN
4705       oe_debug_pub.add(  'EXITING LOAD_MRP_REQUEST_FROM_TBL' , 1 ) ;
4706   END IF;
4707 
4708 EXCEPTION
4709    WHEN FND_API.G_EXC_ERROR THEN
4710      Raise FND_API.G_EXC_ERROR;
4711 
4712    WHEN OTHERS THEN
4713 
4714         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4715         THEN
4716             OE_MSG_PUB.Add_Exc_Msg
4717             (   G_PKG_NAME,
4718               'Load_MRP_request_from_tbl');
4719         END IF;
4720 END Load_MRP_request_from_tbl;
4721 
4722 /*--------------------------------------------------------------------------
4723 Procedure Name : Load_Results_from_rec
4724 Description    : This API loads the results from MRP's ATP_REC_TYPE to
4725                  OM's order line. It also populates OM's ATP Table which
4726                  is used to display the ATP results on the client side.
4727                  We ignore the mandatory components which we passed to MRP
4728                  while loading the results.
4729 -------------------------------------------------------------------------- */
4730 Procedure Load_Results_from_rec
4731 ( p_atp_rec         IN  MRP_ATP_PUB.ATP_Rec_Typ
4732 , p_x_line_rec      IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type
4733 , p_index           IN  NUMBER := 1
4734 , p_sch_action      IN  VARCHAR2 := NULL
4735 , p_config_exists   IN  VARCHAR2 := 'N'
4736 , p_partial_set     IN BOOLEAN := FALSE
4737 , x_return_status OUT NOCOPY VARCHAR2)
4738 
4739 IS
4740 J                  NUMBER := p_index;
4741 atp_count          NUMBER := 1;
4742 l_msg_count        NUMBER;
4743 l_msg_data         VARCHAR2(2000);
4744 l_explanation      VARCHAR2(80);
4745 l_type_code        VARCHAR2(30);
4746 l_ship_set_name    VARCHAR2(30);
4747 l_arrival_set_name VARCHAR2(30);
4748 l_arrival_date     DATE := NULL;
4749 l_sch_action       VARCHAR2(30) := p_sch_action;
4750 l_organization_id  NUMBER;
4751 l_inventory_item   VARCHAR2(2000);
4752 l_order_date_type_code VARCHAR2(30);
4753 l_return_status   VARCHAR2(1);  -- Added for IR ISO CMS Project
4754 
4755 --Scheduling Enhancement ER 2736896
4756 --l_atp_flag             VARCHAR2(1);
4757 l_om_auto_split        VARCHAR2(1);
4758 l_split_line_rec       OE_ORDER_PUB.line_rec_type;
4759 l_split_eligible       BOOLEAN := FALSE;
4760 --Scheduling Enhancement ER 2736896
4761 --
4762 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
4763 --
4764 l_old_ship_from_org_id  number; -- Added for ER 6110708
4765 BEGIN
4766 
4767   IF l_debug_level  > 0 THEN
4768       oe_debug_pub.add(  '2. ENTERING LOAD_RESULTS_FROM_REC' , 1 ) ;
4769   END IF;
4770   x_return_status :=  FND_API.G_RET_STS_SUCCESS;
4771 
4772   IF l_sch_action is NULL THEN
4773      l_sch_action := p_x_line_rec.schedule_action_code;
4774   END IF;
4775 
4776   atp_count := g_atp_tbl.count + 1;
4777   IF l_debug_level  > 0 THEN
4778      oe_debug_pub.add('-----------------Loading MRP Result---------------',1);
4779      oe_debug_pub.add( 'MRP COUNT IS ' || P_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
4780      oe_debug_pub.add(  'SCHEDULE ACTION CODE ' || L_SCH_ACTION , 1 ) ;
4781   END IF;
4782 
4783      -- Check for the MRP data. If MRP is not returning any data, then
4784      -- raise an error.
4785 
4786      IF p_atp_rec.error_code.count = 0 THEN
4787 
4788         IF l_debug_level  > 0 THEN
4789             oe_debug_pub.add(  'MRP HAS RETURNED ANY DATA' , 1 ) ;
4790         END IF;
4791 
4792         FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
4793         OE_MSG_PUB.Add;
4794         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4795 
4796      END IF;
4797      -- 4504197
4798      l_order_date_type_code :=
4799             NVL(Get_Date_Type(p_x_line_rec.header_id), 'SHIP');
4800 
4801     --Scheduling Enhancements ER 2736896
4802     l_om_auto_split := NVL(OE_SYS_PARAMETERS.VALUE('AUTO_SPLIT_AT_SCHEDULING'),'N') ;
4803 
4804     IF l_debug_level  > 0 THEN
4805        oe_debug_pub.add('l_om_auto_split ' || l_om_auto_split);
4806        oe_debug_pub.add('order_source_id ' || p_x_line_rec.order_source_id);
4807        oe_debug_pub.add('source_type_code ' || p_x_line_rec.source_type_code);
4808        oe_debug_pub.add('l_sch_action ' || l_sch_action);
4809        oe_debug_pub.add('item_type_code ' || p_x_line_rec.item_type_code);
4810        oe_debug_pub.add('top_model_line_id ' || p_x_line_rec.top_model_line_id);
4811        oe_debug_pub.add('ship_set_id ' || p_x_line_rec.ship_set_id);
4812        oe_debug_pub.add('arrival_set_id ' || p_x_line_rec.arrival_set_id);
4813        oe_debug_pub.add('split_action_code ' || p_x_line_rec.split_action_code);
4814        oe_debug_pub.add('split_from_line_id ' || p_x_line_rec.split_from_line_id);
4815        oe_debug_pub.add('operation ' || p_x_line_rec.operation);
4816        oe_debug_pub.add('req date qty ' || p_atp_rec.requested_date_quantity(J));
4817        oe_debug_pub.add('ordered qty ' || p_atp_rec.quantity_ordered(J) );
4818        oe_debug_pub.add('override flag ' || p_atp_rec.override_flag(J));
4819     END IF;
4820 
4821     IF l_om_auto_split <> 'N'  --New profile
4822     AND p_x_line_rec.order_source_id <> 10 --exluded internal order lines
4823     AND p_x_line_rec.source_type_code <> 'EXTERNAL' --external lines are not qualified
4824     AND l_sch_action = OESCH_ACT_SCHEDULE --only for schedule
4825     AND p_x_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD --only for standard item
4826     AND (p_x_line_rec.top_model_line_id IS NULL
4827           OR p_x_line_rec.top_model_line_id = FND_API.G_MISS_NUM
4828         ) --models are excluded
4829     AND (p_x_line_rec.split_action_code <> 'SPLIT' --Do not auto split during a Split
4830          AND NOT (p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE AND p_x_line_rec.split_from_line_id is not null ) --for new line during split
4831         )
4832     AND p_x_line_rec.line_category_code <>  'RETURN' --exclude return lines
4833     AND p_x_line_rec.ship_set_id is null
4834     AND p_x_line_rec.arrival_set_id is null
4835     AND nvl(p_atp_rec.override_flag(J),'N') = 'N' --override flag on ATP rec should be N
4836     AND NOT G_GOP_AUTO_SPLIT --do not auto split if scheduling as a part of auto split
4837 	AND NOT (G_ITEM_ORG_TABLE.EXISTS(p_x_line_rec.inventory_item_id) --Bug 16614829 same item warehouse should have been split
4838              AND G_ITEM_ORG_TABLE(p_x_line_rec.inventory_item_id).warehouses.EXISTS(p_atp_rec.source_organization_id(J)))
4839     THEN
4840        --item substitution case, for full substitution do not split
4841        IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id THEN
4842 	      IF l_om_auto_split IN ('P','Q') AND nvl(p_atp_rec.req_item_req_date_qty(J),0) > 0 THEN
4843              l_split_eligible := TRUE; --mark the line as eligible to split
4844 		  END IF;
4845        ELSE --not an item substitution case. Check for request date quantity
4846           IF nvl(p_atp_rec.requested_date_quantity(J),0) < nvl(p_atp_rec.quantity_ordered(J),0) AND
4847 		  nvl(p_atp_rec.requested_date_quantity(J),0) > 0  THEN
4848              l_split_eligible := TRUE; --mark the line as eligible to split
4849           END IF;
4850        END IF;
4851 
4852     END IF;
4853     --Scheduling Enhancements ER 2736896
4854 
4855      -- 4535580 Start
4856      IF p_atp_rec.error_code(J) <> -99  THEN --5673809
4857       --  p_atp_rec.error_code(J) <> 150  THEN
4858 
4859         -- Populate the inventory_item_id from atp record so that, if there is any
4860         -- substitution, then substitution item will come into inventory_item_id
4861         g_atp_tbl(atp_count).request_item_id     := p_x_line_rec.inventory_item_id;
4862         g_atp_tbl(atp_count).inventory_item_id   := p_atp_rec.inventory_item_id(J);
4863         g_atp_tbl(atp_count).ordered_quantity    := p_x_line_rec.ordered_quantity;
4864         g_atp_tbl(atp_count).order_quantity_uom  := p_x_line_rec.order_quantity_uom;
4865         g_atp_tbl(atp_count).request_date        := p_x_line_rec.request_date;
4866         g_atp_tbl(atp_count).ship_from_org_id    :=
4867                     p_atp_rec.Source_Organization_Id(J);
4868         g_atp_tbl(atp_count).subinventory_code := p_x_line_rec.subinventory; --11777419
4869         g_atp_tbl(atp_count).qty_on_request_date :=
4870                     p_atp_rec.Requested_Date_Quantity(J);
4871         g_atp_tbl(atp_count).ordered_qty_Available_Date :=
4872                     p_atp_rec.Ship_Date(J);
4873         g_atp_tbl(atp_count).qty_on_available_date  :=
4874                     p_atp_rec.Available_Quantity(J);
4875         --4504197
4876         IF l_order_date_type_code = 'SHIP' THEN
4877            g_atp_tbl(atp_count).group_available_date  :=
4878                     p_atp_rec.group_ship_date(J);
4879         ELSIF p_atp_rec.group_arrival_date(J) is not null THEN
4880            g_atp_tbl(atp_count).group_available_date  :=
4881                     p_atp_rec.group_arrival_date(J);
4882         END IF;
4883 
4884         -- Display Values
4885         g_atp_tbl(atp_count).line_id         := p_x_line_rec.line_id;
4886         g_atp_tbl(atp_count).header_id       := p_x_line_rec.header_id;
4887         g_atp_tbl(atp_count).line_number     := p_x_line_rec.line_number;
4888         g_atp_tbl(atp_count).shipment_number := p_x_line_rec.shipment_number;
4889         g_atp_tbl(atp_count).option_number   := p_x_line_rec.option_number;
4890         g_atp_tbl(atp_count).component_number := p_x_line_rec.component_number;
4891         g_atp_tbl(atp_count).item_input      := p_x_line_rec.ordered_item;
4892         g_atp_tbl(atp_count).error_message   := l_explanation;
4893         --4772886
4894         g_atp_tbl(atp_count).org_id := p_x_line_rec.org_id;
4895 
4896         IF p_x_line_rec.ship_set_id is not null THEN
4897            BEGIN
4898               SELECT SET_NAME
4899               INTO l_ship_set_name
4900               FROM OE_SETS
4901               WHERE set_id = p_x_line_rec.ship_set_id;
4902            EXCEPTION
4903               WHEN NO_DATA_FOUND THEN
4904                  l_ship_set_name := null;
4905            END;
4906         END IF;
4907         IF p_x_line_rec.arrival_set_id is not null THEN
4908            BEGIN
4909               SELECT SET_NAME
4910               INTO l_arrival_set_name
4911               FROM OE_SETS
4912               WHERE set_id = p_x_line_rec.arrival_set_id;
4913            EXCEPTION
4914               WHEN NO_DATA_FOUND THEN
4915                  l_arrival_set_name := null;
4916            END;
4917         END IF;
4918 
4919         g_atp_tbl(atp_count).ship_set    := l_ship_set_name;
4920         g_atp_tbl(atp_count).arrival_set := l_arrival_set_name;
4921 
4922         IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id  THEN
4923 
4924            IF l_debug_level  > 0 THEN
4925               oe_debug_pub.add(  'SUBSTITUTION OCCURED' , 1 ) ;
4926               oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
4927               oe_debug_pub.add(  'ORIG ITEM :' || P_ATP_REC.REQUEST_ITEM_ID ( J ) , 1 ) ;
4928               oe_debug_pub.add(  'ORIG NAME :' || P_ATP_REC.REQUEST_ITEM_NAME ( J ) , 1 ) ;
4929               oe_debug_pub.add(  'ORIG REQ_ITEM_REQ_DATE_QTY :'|| P_ATP_REC.REQ_ITEM_REQ_DATE_QTY ( J ) , 1 ) ;
4930               oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE_QTY :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
4931               oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
4932 
4933               oe_debug_pub.add(  'SUB QTY_ON_REQUEST_DATE :'|| P_ATP_REC.REQUESTED_DATE_QUANTITY ( J ) , 1 ) ;
4934               oe_debug_pub.add(  'SUB ORDERED_QTY_AVAILABLE_DATE :' || P_ATP_REC.SHIP_DATE ( J ) , 1 ) ;
4935               oe_debug_pub.add(  'SUB QTY_ON_AVAILABLE_DATE :' || P_ATP_REC.AVAILABLE_QUANTITY ( J ) , 1 ) ;
4936            END IF;
4937 
4938               --g_atp_tbl(atp_count).request_item_id
4939                --                    := p_atp_rec.request_item_id(J);
4940            g_atp_tbl(atp_count).Request_item_name
4941                                    := p_atp_rec.request_item_name(J);
4942            g_atp_tbl(atp_count).req_item_req_date_qty
4943                                    := p_atp_rec.req_item_req_date_qty(J);
4944            g_atp_tbl(atp_count).req_item_available_date_qty
4945                                    := p_atp_rec.req_item_available_date_qty(J);
4946            g_atp_tbl(atp_count).req_item_available_date
4947                                    := p_atp_rec.req_item_available_date(J);
4948            g_atp_tbl(atp_count).substitute_flag := 'Y';
4949            g_atp_tbl(atp_count).substitute_item_name
4950                                     := p_atp_rec.inventory_item_name(J);
4951         ELSE
4952            g_atp_tbl(atp_count).substitute_flag := 'N';
4953         END IF;
4954 
4955         l_organization_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
4956            --parameter change made below to fix bug 2819093
4957         OE_ID_TO_VALUE.Ordered_Item
4958             (p_Item_Identifier_type  => p_x_line_rec.item_identifier_type
4959             ,p_inventory_item_id     => p_x_line_rec.inventory_item_id
4960             ,p_organization_id       => l_organization_id
4961             ,p_ordered_item_id       => p_x_line_rec.ordered_item_id
4962             ,p_sold_to_org_id        => p_x_line_rec.sold_to_org_id
4963             ,p_ordered_item          => p_x_line_rec.ordered_item
4964             ,x_ordered_item          => g_atp_tbl(atp_count).Ordered_item_name
4965             ,x_inventory_item        => l_inventory_item);
4966 
4967         OE_ID_TO_VALUE.Ordered_Item
4968             (p_Item_Identifier_type => 'INT'
4969             ,p_inventory_item_id    => p_atp_rec.inventory_item_id(J)
4970             ,p_organization_id      => l_organization_id
4971             ,p_ordered_item_id      => Null
4972             ,p_sold_to_org_id       => Null
4973             ,p_ordered_item         => Null
4974             ,x_ordered_item         => g_atp_tbl(atp_count).Substitute_item_name
4975             ,x_inventory_item       => l_inventory_item);
4976 
4977      END IF;
4978      -- 4535580 End
4979 
4980      IF p_atp_rec.error_code(J) <> 0 AND
4981         l_sch_action <>   OESCH_ACT_ATP_CHECK AND
4982         p_atp_rec.error_code(J) <> -99  AND -- Multi org changes.
4983         p_atp_rec.error_code(J) <> 150 AND -- to fix bug 1880166
4984         --Scheduling Enhancements ER 2736896, Do not raise error
4985         NOT ( p_atp_rec.error_code(J) IN (52,53) AND l_split_eligible)
4986      THEN
4987 
4988         -- Schords (R12 Project #6403)
4989         --5166476
4990         IF OE_SCH_CONC_REQUESTS.g_recorded = 'N' THEN
4991            --OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(p_x_line_rec.line_id) := 'N';
4992           OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(MOD(p_x_line_rec.line_id,G_BINARY_LIMIT)):='N';--7827737
4993            OE_SCH_CONC_REQUESTS.g_recorded :='Y';
4994         END IF;
4995 
4996         IF l_debug_level  > 0 THEN
4997             oe_debug_pub.add(  'ERROR FROM MRP: ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
4998         END IF;
4999         IF p_atp_rec.error_code(J) = 80 THEN
5000 
5001              FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
5002              OE_MSG_PUB.Add;
5003 
5004         ELSE
5005 
5006             IF l_debug_level  > 0 THEN
5007                 oe_debug_pub.add(  'SCHEDULING FAILED' , 1 ) ;
5008             END IF;
5009             IF l_debug_level  > 0 THEN
5010                 oe_debug_pub.add(  P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
5011             END IF;
5012 
5013             OE_MSG_PUB.set_msg_context(
5014              p_entity_code                 => 'LINE'
5015              ,p_entity_id                  => p_x_line_rec.line_id
5016              ,p_header_id                  => p_x_line_rec.header_id
5017              ,p_line_id                    => p_x_line_rec.line_id
5018              ,p_order_source_id            => p_x_line_rec.order_source_id
5019              ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
5020              ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
5021              ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
5022              ,p_change_sequence            => p_x_line_rec.change_sequence
5023              ,p_source_document_type_id    => p_x_line_rec.source_document_type_id
5024              ,p_source_document_id         => p_x_line_rec.source_document_id
5025              ,p_source_document_line_id    => p_x_line_rec.source_document_line_id );
5026 
5027             l_explanation := null;
5028 
5029             select meaning
5030             into l_explanation
5031             from mfg_lookups where
5032             lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
5033             and lookup_code = p_atp_rec.error_code(J) ;
5034             --4535580
5035             g_atp_tbl(atp_count).error_message   := l_explanation;
5036 
5037             IF p_atp_rec.error_code(J) = 19 THEN
5038              -- This error code is given for those lines which are
5039              -- in a group and whose scheduling failed due to some other lines.
5040              -- We do not want to give this out as a message.
5041              null;
5042             ELSIF OESCH_PERFORM_GRP_SCHEDULING = 'N'  THEN
5043 
5044              -- Flag OESCH_PERFORM_GRP_SCHEDULING is set to 'N' when
5045              -- scheduling is called from delayed request to schedule
5046              -- a line being inserted into a set. If there is an error,
5047              -- we will be trying to schedule the whole set again, so
5048              -- we should not display this error message.
5049              null;
5050             -- Commenting Code to fix the Bug-2487471
5051             /*  ELSIF p_atp_rec.Ship_Set_Name(J) is not null OR
5052                   p_atp_rec.Arrival_Set_Name(J) is not null THEN
5053 
5054              -- This line belongs to a scheduling group. We do not want
5055              -- to give out individual messages for each line. We will store
5056              -- them in atp_tbl which can be displayed by the user.
5057              null;
5058            */
5059             ELSIF p_partial_set THEN
5060                -- This call is made by schedule_set_lines procedure.
5061                -- No need to set the message here. Caller will take care
5062                -- of setting the message in this scenario.
5063                Null;
5064             ELSE
5065               IF l_debug_level  > 0 THEN
5066                   oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
5067               END IF;
5068               -- 4558018
5069               IF  p_atp_rec.Ship_Date(J) IS NOT NULL THEN
5070                  FND_MESSAGE.SET_NAME('ONT','ONT_SCH_FAILED_WITH_DATE');
5071                  FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
5072                  FND_MESSAGE.SET_TOKEN('NEXT_DATE',p_atp_rec.Ship_Date(J));
5073 
5074               ELSE
5075                  FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
5076                  FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
5077               END IF;
5078               OE_MSG_PUB.Add;
5079             END IF;
5080         END IF; -- 80
5081 
5082         IF l_debug_level  > 0 THEN
5083             oe_debug_pub.add(  'SETTING ERROR' , 1 ) ;
5084         END IF;
5085         x_return_status := FND_API.G_RET_STS_ERROR;
5086 
5087 
5088 
5089      ELSE
5090 
5091         IF l_debug_level  > 0 THEN
5092             oe_debug_pub.add(  'LOADING ATP RECORD' , 1 ) ;
5093             oe_debug_pub.add(  P_ATP_REC.SOURCE_ORGANIZATION_ID ( 1 ) , 1 ) ;
5094 
5095             oe_debug_pub.add(  'ERROR CODE : ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
5096         END IF;
5097         -- Muti org changes.
5098       IF (p_atp_rec.error_code(J) <> -99 ) THEN
5099 
5100         IF l_debug_level  > 0 THEN
5101             oe_debug_pub.add(  '3. ERROR CODE : ' || P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
5102             oe_debug_pub.add(  '3. J : ' || J , 3 ) ;
5103             oe_debug_pub.add(  '3. IDENTIFIER : ' || P_ATP_REC.IDENTIFIER ( J ) , 1 ) ;
5104             oe_debug_pub.add(  '3. ITEM : ' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
5105         END IF;
5106 
5107          IF l_debug_level  > 0 THEN
5108              oe_debug_pub.add(  '3.REQUEST SHIP DATE :' || TO_CHAR ( P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5109               oe_debug_pub.add(  '3.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 1 ) ;
5110               oe_debug_pub.add(  '3.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5111               oe_debug_pub.add(  '3.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5112               oe_debug_pub.add(  '3.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
5113               oe_debug_pub.add(  '3.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
5114               oe_debug_pub.add(  '3.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 1 ) ;
5115               oe_debug_pub.add(  '3.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 1 ) ;
5116          END IF;
5117 
5118         IF l_sch_action = OESCH_ACT_ATP_CHECK THEN
5119 
5120            l_explanation := null;
5121 
5122            IF (p_atp_rec.error_code(J) <> 0) THEN
5123 
5124               BEGIN
5125                  select meaning
5126                  into l_explanation
5127                  from mfg_lookups where
5128                  lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
5129                  and lookup_code = p_atp_rec.error_code(J) ;
5130 
5131                  g_atp_tbl(atp_count).error_message   := l_explanation;
5132                  IF l_debug_level  > 0 THEN
5133                      oe_debug_pub.add(  'EXPLANATION IS : ' || L_EXPLANATION , 1 ) ;
5134                  END IF;
5135 
5136               /*   IF p_atp_rec.error_code(J) = 150 THEN -- to fix bug 1880166.
5137                     OE_MSG_PUB.add_text(l_explanation);
5138                  END IF;
5139               */ -- 2393433.
5140               EXCEPTION
5141                  WHEN OTHERS THEN
5142                    Null;
5143               END;
5144 
5145            END IF;
5146 
5147            -- Populate the inventory_item_id from atp record so that, if there is any
5148            -- substitution, then substitution item will come into inventory_item_id
5149            g_atp_tbl(atp_count).request_item_id     := p_x_line_rec.inventory_item_id;
5150            g_atp_tbl(atp_count).inventory_item_id   := p_atp_rec.inventory_item_id(J);
5151            g_atp_tbl(atp_count).ordered_quantity    := p_x_line_rec.ordered_quantity;
5152            g_atp_tbl(atp_count).order_quantity_uom  := p_x_line_rec.order_quantity_uom;
5153            g_atp_tbl(atp_count).request_date        := p_x_line_rec.request_date;
5154            g_atp_tbl(atp_count).ship_from_org_id    :=
5155                     p_atp_rec.Source_Organization_Id(J);
5156            g_atp_tbl(atp_count).subinventory_code := p_x_line_rec.subinventory; --11777419
5157            g_atp_tbl(atp_count).qty_on_request_date :=
5158                     p_atp_rec.Requested_Date_Quantity(J);
5159            g_atp_tbl(atp_count).ordered_qty_Available_Date :=
5160                     p_atp_rec.Ship_Date(J);
5161            g_atp_tbl(atp_count).qty_on_available_date  :=
5162                     p_atp_rec.Available_Quantity(J);
5163            --4504197
5164            IF l_order_date_type_code = 'SHIP' THEN
5165               g_atp_tbl(atp_count).group_available_date  :=
5166                     p_atp_rec.group_ship_date(J);
5167            ELSIF p_atp_rec.group_arrival_date(J) is not null THEN
5168              g_atp_tbl(atp_count).group_available_date  :=
5169                     p_atp_rec.group_arrival_date(J);
5170            END IF;
5171 
5172            -- Display Values
5173            g_atp_tbl(atp_count).line_id         := p_x_line_rec.line_id;
5174            g_atp_tbl(atp_count).header_id       := p_x_line_rec.header_id;
5175            g_atp_tbl(atp_count).line_number     := p_x_line_rec.line_number;
5176            g_atp_tbl(atp_count).shipment_number := p_x_line_rec.shipment_number;
5177            g_atp_tbl(atp_count).option_number   := p_x_line_rec.option_number;
5178            g_atp_tbl(atp_count).component_number := p_x_line_rec.component_number;
5179            g_atp_tbl(atp_count).item_input      := p_x_line_rec.ordered_item;
5180            g_atp_tbl(atp_count).error_message   := l_explanation;
5181            --4772886
5182            g_atp_tbl(atp_count).org_id  := p_x_line_rec.org_id;
5183 
5184            IF p_x_line_rec.ship_set_id is not null THEN
5185              BEGIN
5186                 SELECT SET_NAME
5187                 INTO l_ship_set_name
5188                 FROM OE_SETS
5189                 WHERE set_id = p_x_line_rec.ship_set_id;
5190              EXCEPTION
5191                 WHEN NO_DATA_FOUND THEN
5192                   l_ship_set_name := null;
5193              END;
5194            END IF;
5195 
5196            IF p_x_line_rec.arrival_set_id is not null THEN
5197              BEGIN
5198                 SELECT SET_NAME
5199                 INTO l_arrival_set_name
5200                 FROM OE_SETS
5201                 WHERE set_id = p_x_line_rec.arrival_set_id;
5202              EXCEPTION
5203                 WHEN NO_DATA_FOUND THEN
5204                   l_arrival_set_name := null;
5205              END;
5206            END IF;
5207 
5208            g_atp_tbl(atp_count).ship_set    := l_ship_set_name;
5209            g_atp_tbl(atp_count).arrival_set := l_arrival_set_name;
5210 
5211            IF p_atp_rec.inventory_item_id(J) <> p_x_line_rec.inventory_item_id
5212            THEN
5213 
5214               IF l_debug_level  > 0 THEN
5215                   oe_debug_pub.add(  'SUBSTITUTION OCCURED' , 1 ) ;
5216                   oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
5217                   oe_debug_pub.add(  'ORIG ITEM :' || P_ATP_REC.REQUEST_ITEM_ID ( J ) , 1 ) ;
5218                   oe_debug_pub.add(  'ORIG NAME :' || P_ATP_REC.REQUEST_ITEM_NAME ( J ) , 1 ) ;
5219                   oe_debug_pub.add(  'ORIG REQ_ITEM_REQ_DATE_QTY :'|| P_ATP_REC.REQ_ITEM_REQ_DATE_QTY ( J ) , 1 ) ;
5220                   oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE_QTY :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
5221                   oe_debug_pub.add(  'ORIG REQ_ITEM_AVAILABLE_DATE :' || P_ATP_REC.REQ_ITEM_AVAILABLE_DATE ( J ) , 1 ) ;
5222 
5223                   oe_debug_pub.add(  'SUB QTY_ON_REQUEST_DATE :'|| P_ATP_REC.REQUESTED_DATE_QUANTITY ( J ) , 1 ) ;
5224                   oe_debug_pub.add(  'SUB ORDERED_QTY_AVAILABLE_DATE :' || P_ATP_REC.SHIP_DATE ( J ) , 1 ) ;
5225                   oe_debug_pub.add(  'SUB QTY_ON_AVAILABLE_DATE :' || P_ATP_REC.AVAILABLE_QUANTITY ( J ) , 1 ) ;
5226               END IF;
5227 
5228 
5229               --g_atp_tbl(atp_count).request_item_id
5230                --                    := p_atp_rec.request_item_id(J);
5231               g_atp_tbl(atp_count).Request_item_name
5232                                    := p_atp_rec.request_item_name(J);
5233               g_atp_tbl(atp_count).req_item_req_date_qty
5234                                    := p_atp_rec.req_item_req_date_qty(J);
5235               g_atp_tbl(atp_count).req_item_available_date_qty
5236                                    := p_atp_rec.req_item_available_date_qty(J);
5237               g_atp_tbl(atp_count).req_item_available_date
5238                                    := p_atp_rec.req_item_available_date(J);
5239               g_atp_tbl(atp_count).substitute_flag := 'Y';
5240 
5241               g_atp_tbl(atp_count).substitute_item_name
5242                                       := p_atp_rec.inventory_item_name(J);
5243            ELSE
5244              g_atp_tbl(atp_count).substitute_flag := 'N';
5245            END IF;
5246 
5247            l_organization_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
5248            --parameter change made below to fix bug 2819093
5249            OE_ID_TO_VALUE.Ordered_Item
5250             (p_Item_Identifier_type  => p_x_line_rec.item_identifier_type
5251             ,p_inventory_item_id     => p_x_line_rec.inventory_item_id
5252             ,p_organization_id       => l_organization_id
5253             ,p_ordered_item_id       => p_x_line_rec.ordered_item_id
5254             ,p_sold_to_org_id        => p_x_line_rec.sold_to_org_id
5255             ,p_ordered_item          => p_x_line_rec.ordered_item
5256             ,x_ordered_item          => g_atp_tbl(atp_count).Ordered_item_name
5257             ,x_inventory_item        => l_inventory_item);
5258 
5259            OE_ID_TO_VALUE.Ordered_Item
5260             (p_Item_Identifier_type => 'INT'
5261             ,p_inventory_item_id    => p_atp_rec.inventory_item_id(J)
5262             ,p_organization_id      => l_organization_id
5263             ,p_ordered_item_id      => Null
5264             ,p_sold_to_org_id       => Null
5265             ,p_ordered_item         => Null
5266             ,x_ordered_item         => g_atp_tbl(atp_count).Substitute_item_name
5267             ,x_inventory_item       => l_inventory_item);
5268 
5269 
5270 
5271         END IF; -- Check for ATP.
5272       END IF; --Check for -99.
5273 
5274       IF x_return_status = FND_API.G_RET_STS_SUCCESS AND
5275          l_sch_action <> OESCH_ACT_ATP_CHECK
5276       THEN
5277         -- code fix for 3502139
5278         OE_MSG_PUB.set_msg_context(
5279          p_entity_code                 => 'LINE'
5280          ,p_entity_id                  => p_x_line_rec.line_id
5281          ,p_header_id                  => p_x_line_rec.header_id
5282          ,p_line_id                    => p_x_line_rec.line_id
5283          ,p_order_source_id            => p_x_line_rec.order_source_id
5284          ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
5285          ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
5286          ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
5287          ,p_change_sequence            => p_x_line_rec.change_sequence
5288          ,p_source_document_type_id    => p_x_line_rec.source_document_type_id
5289          ,p_source_document_id         => p_x_line_rec.source_document_id
5290          ,p_source_document_line_id    => p_x_line_rec.source_document_line_id );
5291         -- code fix for 3502139
5292         --8731703 : Code moved from Call_MRP_ATP
5293          IF l_debug_level  > 0 THEN
5294            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);
5295          END IF;
5296          BEGIN
5297             insert into oe_schedule_lines_temp
5298                    (LINE_ID ,
5299                     SCHEDULE_ACTION_CODE)
5300               values(p_x_line_rec.line_id,p_x_line_rec.schedule_action_code);
5301          EXCEPTION
5302             WHEN OTHERS THEN
5303               IF l_debug_level  > 0 THEN
5304                   oe_debug_pub.add(  '6378240 : INSERT ERROR ');
5305               END IF;
5306          END;
5307          --  8731703 end
5308 
5309         --Scheduling Enhancements ER 2736896
5310         IF l_split_eligible THEN
5311            check_split_needed(p_x_line_rec,
5312                              p_atp_rec,
5313                              J,
5314                              l_split_line_rec,
5315                              l_sch_action,
5316                              p_config_exists,
5317                              l_om_auto_split,
5318                              l_order_date_type_code,
5319                              l_return_status  );
5320 
5321            IF l_debug_level  > 0 THEN
5322              oe_debug_pub.add( 'Back from check_split_needed, status:' || l_return_status , 1 ) ;
5323            END IF;
5324 
5325            IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5326               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5327            ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
5328                  RAISE FND_API.G_EXC_ERROR;
5329            END IF;
5330 
5331            IF l_split_line_rec.line_id is not null AND l_split_line_rec.line_id <> FND_API.G_MISS_NUM THEN
5332              IF l_debug_level  > 0 THEN
5333                  oe_debug_pub.add(  'Auto split has happened : new qty : ' || l_split_line_rec.ordered_quantity , 1 ) ;
5334 				 oe_debug_pub.add(  'Auto split has happened : RSV qty : ' || l_split_line_rec.reserved_quantity , 1 ) ;
5335 				 oe_debug_pub.add(  'Auto split has happened : RSV qty2 : ' || l_split_line_rec.reserved_quantity2 , 1 ) ;
5336                  oe_debug_pub.add(  'original qty : ' || p_x_line_rec.ordered_quantity , 1 ) ;
5337 				 oe_debug_pub.add(  'original RSV qty : ' || p_x_line_rec.reserved_quantity , 1 ) ;
5338 				 oe_debug_pub.add(  'original RSV qty2 : ' || p_x_line_rec.reserved_quantity2 , 1 ) ;
5339              END IF;
5340 
5341 			 IF p_x_line_rec.reserved_quantity is not null AND p_x_line_rec.reserved_quantity <> FND_API.G_MISS_NUM
5342 			 AND p_x_line_rec.reserved_quantity > l_split_line_rec.ordered_quantity
5343 			 THEN
5344 			    p_x_line_rec.reserved_quantity := l_split_line_rec.ordered_quantity ;
5345 			    p_x_line_rec.reserved_quantity2 := l_split_line_rec.ordered_quantity2 ;
5346 			    IF l_debug_level  > 0 THEN
5347 				    oe_debug_pub.add(  'new RSV qty : ' || p_x_line_rec.reserved_quantity , 1 ) ;
5348 				    oe_debug_pub.add(  'new RSV qty2 : ' || p_x_line_rec.reserved_quantity2 , 1 ) ;
5349                 END IF;
5350 			 END IF;
5351 
5352 			 l_split_line_rec.reserved_quantity := p_x_line_rec.reserved_quantity ;
5353 			 l_split_line_rec.reserved_quantity2 := p_x_line_rec.reserved_quantity2 ;
5354 
5355              --Bug 16614829, Add the item/warehouse combination to global table
5356              G_ITEM_ORG_TABLE(p_x_line_rec.inventory_item_id).warehouses(p_atp_rec.source_organization_id(J)) := 1;
5357 
5358              --This assignment needs to be checked if we can directly do this or only
5359              --assign scheduling related attributes.
5360              p_x_line_rec := l_split_line_rec;
5361 
5362              RETURN;
5363            END IF;
5364         ELSE
5365           IF l_debug_level  > 0 THEN
5366             oe_debug_pub.add(  'Not eligible for scheduling split' , 1 ) ;
5367           END IF;
5368         END IF;
5369       --Scheduling Enhancements ER 2736896
5370 
5371         IF l_sch_action = OESCH_ACT_DEMAND
5372         OR l_sch_action = OESCH_ACT_SCHEDULE
5373         THEN
5374           IF l_debug_level  > 0 THEN
5375               oe_debug_pub.add(  'LOADING RESULTS OF SCHEDULE' , 1 ) ;
5376               oe_debug_pub.add(  '1.REQUEST SHIP DATE :' || TO_CHAR ( P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5377              oe_debug_pub.add(  '1.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 1 ) ;
5378              oe_debug_pub.add(  '1.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5379              oe_debug_pub.add(  '1.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 1 ) ;
5380              oe_debug_pub.add(  '1.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 1 ) ;
5381              oe_debug_pub.add(  '1.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 1 ) ;
5382              oe_debug_pub.add(  '1.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 1 ) ;
5383           END IF;
5384 
5385           l_old_ship_from_org_id := p_x_line_rec.ship_from_org_id; -- Added for ER 6110708
5386 
5387           p_x_line_rec.ship_from_org_id      :=
5388                                 p_atp_rec.Source_Organization_Id(J);
5389 
5390           -- If the item subtitution occurs on the line then populate
5391           -- new inventory item from atp record and also populate
5392           -- Original inventory items.
5393 
5394           IF p_atp_rec.inventory_item_id(J)  <>  p_x_line_rec.inventory_item_id THEN
5395 
5396              IF l_debug_level  > 0 THEN
5397                  oe_debug_pub.add(  'SCH: ITEM HAS BEEN SUBSTITUTED' , 1 ) ;
5398              END IF;
5399              IF l_debug_level  > 0 THEN
5400                  oe_debug_pub.add(  'NEW INVENTORY ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
5401              END IF;
5402 
5403              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ITEM_CHANGE');
5404              FND_MESSAGE.SET_TOKEN('OLDITEM',p_x_line_rec.ordered_item);
5405              FND_MESSAGE.SET_TOKEN('NEWITEM', p_atp_rec.inventory_item_name(J));
5406              OE_MSG_PUB.Add;
5407 
5408              -- Added below call for ER 6110708
5409              IF nvl(p_x_line_rec.booked_flag, 'N') = 'Y' THEN
5410                VALIDATE_ITEM_SUBSTITUTION
5411                (
5412                  p_new_inventory_item_id => p_atp_rec.inventory_item_id(J),
5413                  p_old_inventory_item_id => p_x_line_rec.inventory_item_id,
5414                  p_old_ship_from_org_id => l_old_ship_from_org_id,
5415                  p_new_ship_from_org_id => p_atp_rec.Source_Organization_Id(J),
5416                  p_old_shippable_flag => p_x_line_rec.shippable_flag
5417                );
5418              END IF;
5419 
5420              IF  p_x_line_rec.Original_Inventory_Item_Id is null
5421              THEN
5422                 p_x_line_rec.Original_Inventory_Item_Id
5423                              := p_x_line_rec.Inventory_Item_id;
5424                 p_x_line_rec.Original_item_identifier_Type
5425                              := p_x_line_rec.item_identifier_type;
5426                 p_x_line_rec.Original_ordered_item_id
5427                              := p_x_line_rec.ordered_item_id;
5428                 p_x_line_rec.Original_ordered_item
5429                              := p_x_line_rec.ordered_item;
5430 
5431              END IF;
5432              IF l_debug_level  > 0 THEN
5433                  oe_debug_pub.add(  'ORIGINAL ITEM :' || P_X_LINE_REC.INVENTORY_ITEM_ID , 2 ) ;
5434              END IF;
5435              IF l_debug_level  > 0 THEN
5436                  oe_debug_pub.add(  'SUB ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 2 ) ;
5437              END IF;
5438              p_x_line_rec.inventory_item_id
5439                              := p_atp_rec.inventory_item_id(J);
5440              p_x_line_rec.item_identifier_type := 'INT';
5441 
5442              -- This variable is to track that the Item is being Substituted by Scheduling and not being changed manully by user.
5443              OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y';  -- Added for ER 6110708.
5444           END IF; -- inv changed.
5445 
5446           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
5447 
5448 
5449              IF p_atp_rec.group_ship_date(J) IS NOT NULL
5450              THEN
5451                 p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
5452              ELSE
5453                 p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
5454              END IF;
5455 
5456              IF p_atp_rec.group_arrival_date(J) IS NOT NULL THEN
5457                 p_x_line_rec.schedule_arrival_date := p_atp_rec.group_arrival_date(J);
5458              ELSE
5459                 p_x_line_rec.schedule_arrival_date := p_atp_rec.arrival_date(J);
5460 
5461              END IF;
5462 
5463           ELSE
5464             p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
5465 
5466             p_x_line_rec.schedule_arrival_date  :=
5467                                     p_atp_rec.ship_date(J) +
5468                                     nvl(p_atp_rec.delivery_lead_time(J),0);
5469 
5470             IF p_atp_rec.group_arrival_date(J) IS NOT NULL
5471             THEN
5472               p_x_line_rec.schedule_arrival_date :=
5473                                       p_atp_rec.group_arrival_date(J);
5474               p_x_line_rec.schedule_ship_date :=
5475                   p_x_line_rec.schedule_arrival_date -
5476                   nvl(p_atp_rec.delivery_lead_time(J),0);
5477 
5478             END IF;
5479 
5480             IF p_atp_rec.group_ship_date(J) IS NOT NULL
5481             THEN
5482               p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
5483               p_x_line_rec.schedule_arrival_date  :=
5484                                       p_x_line_rec.schedule_ship_date +
5485                                       nvl(p_atp_rec.delivery_lead_time(J),0);
5486 
5487             END IF;
5488           END IF;
5489           IF p_atp_rec.ship_method(J) IS NOT NULL THEN
5490              p_x_line_rec.shipping_method_code  := p_atp_rec.ship_method(J);
5491           END IF;
5492 
5493           p_x_line_rec.delivery_lead_time  := p_atp_rec.delivery_lead_time(J);
5494           p_x_line_rec.mfg_lead_time       := p_atp_rec.atp_lead_time(J);
5495           p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
5496 
5497           IF l_debug_level  > 0 THEN
5498               oe_debug_pub.add(  'BEFORE ATTRIBUTE 05' , 5 ) ;
5499           END IF;
5500 
5501         -- bug fix 1965182/1925326
5502           IF p_atp_rec.attribute_05.COUNT > 0 THEN
5503              IF p_atp_rec.attribute_05(J) IS NULL THEN
5504                 IF p_config_exists = 'N' THEN
5505                    p_x_line_rec.visible_demand_flag   := 'Y';
5506                 ELSE
5507                    IF l_debug_level  > 0 THEN
5508                        oe_debug_pub.add(  'INSIDE CONFIG EXISTS' , 3 ) ;
5509                    END IF;
5510                 END IF;
5511 
5512              ELSIF p_atp_rec.attribute_05(J) = 'N' THEN
5513                p_x_line_rec.visible_demand_flag   := 'N';
5514              ELSIF p_atp_rec.attribute_05(J) = 'Y' THEN
5515                p_x_line_rec.visible_demand_flag   := 'Y';
5516              END IF;
5517           ELSE
5518              IF p_config_exists = 'N' THEN
5519               p_x_line_rec.visible_demand_flag   := 'Y';
5520              ELSE
5521               IF l_debug_level  > 0 THEN
5522                   oe_debug_pub.add(  'CONFIG EXISTS' , 3 ) ;
5523               END IF;
5524              END IF;
5525           END IF;
5526           IF l_debug_level  > 0 THEN
5527               oe_debug_pub.add(  'AFTER ATTRIBUTE 05' , 5 ) ;
5528           END IF;
5529 
5530           -- We had set the ship_set and arrival_set (which are value
5531           -- fields) to the set name values for calling MRP purpose.
5532           -- Setting these back to null since sets defaulting logic
5533           -- gets fired if these values are populated.
5534 
5535           IF  p_x_line_rec.ship_set_id IS NOT NULL
5536           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
5537              p_x_line_rec.ship_set     := null;
5538           END IF;
5539 
5540           IF  p_x_line_rec.arrival_set_id IS NOT NULL
5541           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
5542              p_x_line_rec.arrival_set  := null;
5543           END IF;
5544 
5545           -- Pack J
5546           -- Promise Date setup with Schedule date
5547           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
5548              -- Check for LAD violation
5549              Validate_with_LAD
5550                 ( p_header_id              => p_x_line_rec.header_id
5551                  ,p_latest_acceptable_date => p_x_line_rec.latest_acceptable_date
5552                  ,p_schedule_ship_date     => p_x_line_rec.schedule_ship_date
5553                  ,p_schedule_arrival_date  => p_x_line_rec.schedule_arrival_date);
5554              -- Promise Date setup
5555              Promise_Date_for_Sch_Action
5556                   (p_x_line_rec => p_x_line_rec
5557                   ,p_sch_action => l_sch_action
5558                   ,P_header_id  => p_x_line_rec.header_id);
5559 
5560              -- Firm Demand Flag.
5561              IF  nvl(p_x_line_rec.firm_demand_flag,'N') = 'N'
5562              AND Oe_Sys_Parameters.Value('FIRM_DEMAND_EVENTS') = 'SCHEDULE' THEN
5563                  p_x_line_rec.firm_demand_flag := 'Y';
5564 
5565              END IF;
5566           END IF;
5567 
5568           -- Adding code to trap if mrp is returning success and not
5569           -- returning correct data to OM.
5570           IF p_x_line_rec.schedule_ship_date is null
5571           OR p_x_line_rec.schedule_arrival_date is null THEN
5572 
5573              IF l_debug_level  > 0 THEN
5574                  oe_debug_pub.add(  'SCH: MRP HAS NOT RETURNING VALID SHIP DATE' , 2 ) ;
5575                  oe_debug_pub.add(  'SCH: Shedule ship date ' ||
5576                                                p_x_line_rec.schedule_ship_date , 2 ) ;
5577                  oe_debug_pub.add(  'SCH: Schedule Arr Date' ||
5578                                                p_x_line_rec.schedule_arrival_date , 2 ) ;
5579              END IF;
5580              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
5581              OE_MSG_PUB.Add;
5582              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5583           END IF;
5584 
5585         ELSIF l_sch_action = OESCH_ACT_REDEMAND OR
5586               l_sch_action = OESCH_ACT_RESCHEDULE
5587         THEN
5588           IF l_debug_level  > 0 THEN
5589              oe_debug_pub.add(  'LOAD THE RESULT OF RESCHEDULE' , 3 ) ;
5590              oe_debug_pub.add(  '2.REQUEST SHIP DATE :' || P_ATP_REC.REQUESTED_SHIP_DATE ( J ) , 3 ) ;
5591              oe_debug_pub.add(  '2.REQUEST ARRIVAL DATE :' || P_ATP_REC.REQUESTED_ARRIVAL_DATE ( J ) , 3 ) ;
5592              oe_debug_pub.add(  '2.SHIP DATE :' || TO_CHAR ( P_ATP_REC.SHIP_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 3 ) ;
5593              oe_debug_pub.add(  '2.ARRIVAL DATE :' || TO_CHAR ( P_ATP_REC.ARRIVAL_DATE ( J ) , 'DD-MON-RR:HH:MI:SS' ) , 3 ) ;
5594              oe_debug_pub.add(  '2.LEAD TIME :' || P_ATP_REC.DELIVERY_LEAD_TIME ( J ) , 3 ) ;
5595              oe_debug_pub.add(  '2.GROUP SHIP DATE :' || P_ATP_REC.GROUP_SHIP_DATE ( J ) , 3 ) ;
5596              oe_debug_pub.add(  '2.GROUP ARRIVAL DATE :' || P_ATP_REC.GROUP_ARRIVAL_DATE ( J ) , 3 ) ;
5597           END IF;
5598 
5599           l_old_ship_from_org_id := p_x_line_rec.ship_from_org_id; -- Added for ER 6110708
5600 
5601           p_x_line_rec.ship_from_org_id :=
5602                                   p_atp_rec.Source_Organization_Id(J);
5603 
5604 
5605           -- If the item subtitution occurs on the line then populate
5606           -- new inventory item from atp record and also populate
5607           -- Original inventory items.
5608 
5609           IF p_atp_rec.inventory_item_id(J)  <>  p_x_line_rec.inventory_item_id
5610           THEN
5611 
5612              IF l_debug_level  > 0 THEN
5613                  oe_debug_pub.add(  'RSCH: ITEM HAS BEEN SUBSTITUTED' , 1 ) ;
5614                  oe_debug_pub.add(  'RSCH NEW INVENTORY ITEM :' || P_ATP_REC.INVENTORY_ITEM_ID ( J ) , 1 ) ;
5615              END IF;
5616 
5617              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ITEM_CHANGE');
5618              FND_MESSAGE.SET_TOKEN('OLDITEM',p_x_line_rec.ordered_item);
5619              FND_MESSAGE.SET_TOKEN('NEWITEM', p_atp_rec.inventory_item_name(J));
5620              OE_MSG_PUB.Add;
5621 
5622              -- Added below call for ER 6110708
5623              IF nvl(p_x_line_rec.booked_flag, 'N') = 'Y' THEN
5624                VALIDATE_ITEM_SUBSTITUTION
5625                (
5626                  p_new_inventory_item_id => p_atp_rec.inventory_item_id(J),
5627                  p_old_inventory_item_id => p_x_line_rec.inventory_item_id,
5628                  p_old_ship_from_org_id => l_old_ship_from_org_id,
5629                  p_new_ship_from_org_id => p_atp_rec.Source_Organization_Id(J),
5630                  p_old_shippable_flag => p_x_line_rec.shippable_flag
5631                );
5632              END IF;
5633 
5634              IF  p_x_line_rec.Original_Inventory_Item_Id is null
5635              THEN
5636                 p_x_line_rec.Original_Inventory_Item_Id
5637                              := p_x_line_rec.Inventory_Item_id;
5638                 p_x_line_rec.Original_item_identifier_Type
5639                              := p_x_line_rec.item_identifier_type;
5640                 p_x_line_rec.Original_ordered_item_id
5641                              := p_x_line_rec.ordered_item_id;
5642                 p_x_line_rec.Original_ordered_item
5643                              := p_x_line_rec.ordered_item;
5644 
5645              END IF;
5646              p_x_line_rec.inventory_item_id
5647                              := p_atp_rec.inventory_item_id(J);
5648              p_x_line_rec.item_identifier_type := 'INT';
5649 
5650              -- This variable is to track that the Item is being Substituted by Scheduling and not being changed manully by user.
5651              OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'Y';  -- Added for ER 6110708.
5652           END IF; -- inv changed.
5653 
5654 
5655           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
5656 
5657 
5658              IF p_atp_rec.group_ship_date(J) IS NOT NULL
5659              THEN
5660                 p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
5661              ELSE
5662                 p_x_line_rec.schedule_ship_date  := p_atp_rec.ship_date(J);
5663              END IF;
5664 
5665              IF p_atp_rec.group_arrival_date(J) IS NOT NULL THEN
5666                 p_x_line_rec.schedule_arrival_date := p_atp_rec.group_arrival_date(J);
5667              ELSE
5668                 p_x_line_rec.schedule_arrival_date := p_atp_rec.arrival_date(J);
5669 
5670              END IF;
5671           ELSE
5672              p_x_line_rec.schedule_ship_date := p_atp_rec.ship_date(J);
5673 
5674              p_x_line_rec.schedule_arrival_date  :=
5675                                   p_atp_rec.ship_date(J) +
5676                                   nvl(p_atp_rec.delivery_lead_time(J),0);
5677 
5678 
5679              IF p_atp_rec.group_arrival_date(J) IS NOT NULL
5680              THEN
5681                p_x_line_rec.schedule_arrival_date :=
5682                                        p_atp_rec.group_arrival_date(J);
5683                p_x_line_rec.schedule_ship_date :=
5684                    p_x_line_rec.schedule_arrival_date -
5685                    nvl(p_atp_rec.delivery_lead_time(J),0);
5686 
5687              END IF;
5688 
5689              IF p_atp_rec.group_ship_date(J) IS NOT NULL
5690              THEN
5691                p_x_line_rec.schedule_ship_date := p_atp_rec.group_ship_date(J);
5692                p_x_line_rec.schedule_arrival_date  :=
5693                                     p_x_line_rec.schedule_ship_date +
5694                                     nvl(p_atp_rec.delivery_lead_time(J),0);
5695 
5696              END IF;
5697 
5698           END IF;
5699 
5700           IF p_atp_rec.ship_method(J) IS NOT NULL THEN
5701              p_x_line_rec.shipping_method_code  := p_atp_rec.ship_method(J);
5702           END IF;
5703 
5704           p_x_line_rec.delivery_lead_time  := p_atp_rec.delivery_lead_time(J);
5705           p_x_line_rec.mfg_lead_time       := p_atp_rec.atp_lead_time(J);
5706 
5707           -- When a new option is added to scheduled SMC/SET OM will
5708           -- call MRP with action re-schedule. So, for the new line we need to
5709           -- assign the following values.
5710 
5711           -- Status assignment will be done after promise date setup call
5712           --p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
5713 
5714           IF l_debug_level  > 0 THEN
5715               oe_debug_pub.add(  'RSCH BEFORE ATTRIBUTE 05' , 5 ) ;
5716           END IF;
5717         -- bug fix 1965182/1925326
5718           IF p_atp_rec.attribute_05.COUNT > 0 THEN
5719              IF p_atp_rec.attribute_05(J) IS NULL THEN
5720                 IF p_config_exists = 'N' THEN
5721                    p_x_line_rec.visible_demand_flag   := 'Y';
5722                 ELSE
5723                    IF l_debug_level  > 0 THEN
5724                        oe_debug_pub.add(  'INSIDE CONFIG EXISTS' , 3 ) ;
5725                    END IF;
5726                 END IF;
5727 
5728              ELSIF p_atp_rec.attribute_05(J) = 'N' THEN
5729                p_x_line_rec.visible_demand_flag   := 'N';
5730              ELSIF p_atp_rec.attribute_05(J) = 'Y' THEN
5731                p_x_line_rec.visible_demand_flag   := 'Y';
5732              END IF;
5733           ELSE
5734              IF p_config_exists = 'N' THEN
5735               p_x_line_rec.visible_demand_flag   := 'Y';
5736              ELSE
5737               IF l_debug_level  > 0 THEN
5738                   oe_debug_pub.add(  'CONFIG EXISTS' , 3 ) ;
5739               END IF;
5740              END IF;
5741           END IF;
5742           IF l_debug_level  > 0 THEN
5743               oe_debug_pub.add(  'RSCH AFTER ATTRIBUTE 05' , 5 ) ;
5744           END IF;
5745 
5746           IF (p_x_line_rec.ordered_quantity = 0)
5747           THEN
5748             -- Bug 2101332. On a cancelled line, keeping
5749             -- warehouse is not a harm.
5750             -- p_x_line_rec.ship_from_org_id   := null;
5751             p_x_line_rec.schedule_ship_date    := null;
5752             p_x_line_rec.schedule_arrival_date := null;
5753             p_x_line_rec.schedule_status_code  := null;
5754             p_x_line_rec.visible_demand_flag   := 'N';
5755             p_x_line_rec.override_atp_date_code := null;
5756           END IF;
5757 
5758           -- We had set the ship_set and arrival_set (which are value
5759           -- fields) to the set name values for calling MRP purpose.
5760           -- Setting these back to null since sets defaulting logic
5761           -- gets fired if these values are populated.
5762 
5763           IF  p_x_line_rec.ship_set_id IS NOT NULL
5764           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
5765               p_x_line_rec.ship_set     := null;
5766           END IF;
5767 
5768           IF  p_x_line_rec.arrival_set_id IS NOT NULL
5769           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
5770               p_x_line_rec.arrival_set  := null;
5771           END IF;
5772 
5773           IF p_x_line_rec.top_model_line_id = p_x_line_rec.line_id THEN
5774 
5775                            IF l_debug_level  > 0 THEN
5776                                oe_debug_pub.add(  'STORE ARRIVAL_DATE ' || P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 2 ) ;
5777                            END IF;
5778              l_arrival_date := p_x_line_rec.schedule_arrival_date;
5779 
5780           END IF;
5781           -- Pack J
5782           -- Promise Date setup with Schedule date
5783           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
5784              -- Check for LAD violation
5785              Validate_with_LAD
5786                 ( p_header_id              => p_x_line_rec.header_id
5787                  ,p_latest_acceptable_date => p_x_line_rec.latest_acceptable_date
5788                  ,p_schedule_ship_date     => p_x_line_rec.schedule_ship_date
5789                  ,p_schedule_arrival_date  => p_x_line_rec.schedule_arrival_date);
5790              -- Promise date setup
5791              Promise_Date_for_Sch_Action
5792                   (p_x_line_rec => p_x_line_rec
5793                   ,p_sch_action => l_sch_action
5794                   ,P_header_id  => p_x_line_rec.header_id);
5795 
5796              -- Firm Demand Flag.
5797              IF  nvl(p_x_line_rec.firm_demand_flag,'N') = 'N'
5798              AND Oe_Sys_Parameters.Value('FIRM_DEMAND_EVENTS') = 'SCHEDULE' THEN
5799                  p_x_line_rec.firm_demand_flag := 'Y';
5800 
5801              END IF;
5802           END IF;
5803           IF p_x_line_rec.ordered_quantity > 0 THEN
5804              p_x_line_rec.schedule_status_code  := OESCH_STATUS_SCHEDULED;
5805           END IF;
5806 
5807           IF l_debug_level  > 0 THEN
5808              oe_debug_pub.add(  'RSCH: PROMISE DATE '||p_x_line_rec.promise_date , 2 ) ;
5809           END IF;
5810 
5811 
5812           -- Adding code to trap if mrp is returning success and not
5813           -- returning correct data to OM.
5814           IF (p_x_line_rec.schedule_ship_date is null
5815           OR  p_x_line_rec.schedule_arrival_date is null)
5816           AND p_x_line_rec.ordered_quantity > 0 THEN
5817 
5818              IF l_debug_level  > 0 THEN
5819                  oe_debug_pub.add(  'RSCH: MRP HAS NOT RETURNING VALID DATE' , 2 ) ;
5820              END IF;
5821              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
5822              OE_MSG_PUB.Add;
5823              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5824           END IF;
5825 
5826         ELSIF l_sch_action = OESCH_ACT_UNDEMAND
5827         THEN
5828           IF l_debug_level  > 0 THEN
5829              oe_debug_pub.add(  'RR2:Load the results of undemand: '
5830                                             || P_X_LINE_REC.RE_SOURCE_FLAG , 1 ) ;
5831           END IF;
5832           --  Commented for bug 7692055
5833           -- We will not clear ship_from_org
5834           /*
5835           --bug 2921202
5836           if p_x_line_rec.ordered_quantity > 0 then
5837            IF p_x_line_rec.re_source_flag='Y' or
5838              p_x_line_rec.re_source_flag is null THEN
5839                IF l_debug_level  > 0 THEN
5840                    oe_debug_pub.add(  'SETTING SHIP FROM TO NULL' , 1 ) ;
5841                END IF;
5842                p_x_line_rec.ship_from_org_id      := null;
5843            END IF;
5844           end if;
5845           */
5846           p_x_line_rec.schedule_ship_date    := null;
5847           p_x_line_rec.schedule_arrival_date := null;
5848           p_x_line_rec.schedule_status_code  := null;
5849           p_x_line_rec.visible_demand_flag   := null;
5850 
5851           -- We had set the ship_set and arrival_set (which are value
5852           -- fields) to the set name values for calling MRP purpose.
5853           -- Setting these back to null since sets defaulting logic
5854           -- gets fired if these values are populated.
5855 
5856           IF  p_x_line_rec.ship_set_id IS NOT NULL
5857           AND p_x_line_rec.ship_set_id <> FND_API.G_MISS_NUM THEN
5858               p_x_line_rec.ship_set     := null;
5859           END IF;
5860 
5861           IF  p_x_line_rec.arrival_set_id IS NOT NULL
5862           AND p_x_line_rec.arrival_set_id <> FND_API.G_MISS_NUM THEN
5863               p_x_line_rec.arrival_set  := null;
5864           END IF;
5865 
5866           -- BUG 1282873
5867           IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
5868 
5869             -- Unscheduling a line will also clear the Override Atp flag
5870              p_x_line_rec.override_atp_date_code := Null;
5871           END IF;
5872           -- END 1282873
5873           -- 3345776
5874           IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
5875              -- Promise date setup
5876              Promise_Date_for_Sch_Action
5877                   (p_x_line_rec => p_x_line_rec
5878                   ,p_sch_action => l_sch_action
5879                   ,P_header_id  => p_x_line_rec.header_id);
5880           END IF;
5881 
5882         END IF;
5883 
5884 /*** Moved the below code for bug #8205242 from procedure Load_Results_From_Tbl
5885      to procedure Load_Results_from_Rec ***/
5886 
5887 /* 7576948: IR ISO Change Management Begins */
5888 
5889 -- This code is hooked for IR ISO project, where if the schedule ship
5890 -- date is changed as a result of MRP call then a delayed request is
5891 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
5892 -- API from Purchasing, responsible for conditionally updating the Need By
5893 -- Date column in internal requisition line. This will be done based on
5894 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
5895 
5896 -- For details on IR ISO CMS project, please refer to FOL >
5897 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
5898 
5899     IF l_debug_level > 0 THEN
5900       oe_debug_pub.add(' Source Type for the order line is : '||p_x_line_rec.order_source_id,5);
5901       oe_debug_pub.add(' Line_id : '||p_x_line_rec.line_id,5);
5902       oe_debug_pub.add(' Header_id : '||p_x_line_rec.header_id,5);
5903       oe_debug_pub.add(' Source Document id : '||p_x_line_rec.source_document_id,5);
5904       oe_debug_pub.add(' Src Doc Line_id : '||p_x_line_rec.source_document_line_id,5);
5905       oe_debug_pub.add(' Schedule Arrival Date : '||p_x_line_rec.schedule_arrival_date,5);
5906     END IF;
5907 
5908     IF p_x_line_rec.order_source_id = 10 THEN
5909        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
5910          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
5911          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
5912          -- Modified for IR ISO Tracking bug 7667702
5913 
5914          IF l_debug_level > 0 THEN
5915            oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change',5);
5916          END IF;
5917 
5918          -- Log a delayed request to update the change in Schedule Ship Date to
5919          -- Requisition Line. This request will be logged only if the change is
5920          -- not initiated from Requesting Organization, and it is not a case of
5921          -- Internal Sales Order Full Cancellation. It will even not be logged
5922          -- Purchasing profile option does not allow update of Need By Date when
5923          -- Schedule Ship Date changes on internal sales order line
5924 
5925          OE_delayed_requests_Pvt.log_request
5926          ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
5927          , p_entity_id              => p_x_line_rec.line_id
5928          , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
5929          , p_requesting_entity_id   => p_x_line_rec.line_id
5930          , p_request_unique_key1    => p_x_line_rec.header_id  -- Order Hdr_id
5931          , p_request_unique_key2    => p_x_line_rec.source_document_id -- Req Hdr_id
5932          , p_request_unique_key3    => p_x_line_rec.source_document_line_id -- Req Line_id
5933          , p_date_param1            => p_x_line_rec.schedule_arrival_date -- schedule_ship_date
5934 -- Note: p_date_param1 is used for both Schedule_Ship_Date and
5935 -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
5936 -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
5937 -- it can expect change with respect to Ship or Arrival date. Thus, will
5938 -- not raise any issues.
5939          , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
5940          , x_return_status          => l_return_status
5941          );
5942 
5943          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5944            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5945          ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
5946            RAISE FND_API.G_EXC_ERROR;
5947          END IF;
5948 
5949          ELSE -- Added for IR ISO Tracking bug 7667702
5950            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
5951              IF l_debug_level > 0 THEN
5952                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
5953              END IF;
5954 
5955              OE_SCHEDULE_UTIL.Update_PO(p_x_line_rec.schedule_arrival_date,
5956                 p_x_line_rec.source_document_id,
5957                 p_x_line_rec.source_document_line_id);
5958            END IF;
5959          END IF;
5960 
5961        END IF;
5962      END IF; -- Order_Source_id
5963 
5964 /* ============================= */
5965 /* IR ISO Change Management Ends */
5966 
5967 
5968       END IF; -- Return Status.
5969      END IF; -- Main If;
5970 
5971   IF l_debug_level  > 0 THEN
5972       oe_debug_pub.add(  'VALUE OF THE INDEX ' || J , 1 ) ;
5973   END IF;
5974   IF l_debug_level  > 0 THEN
5975       oe_debug_pub.add(  'EXITING LOAD_RESULTS_FROM_REC' || X_RETURN_STATUS , 1 ) ;
5976   END IF;
5977 
5978 EXCEPTION
5979    WHEN FND_API.G_EXC_ERROR THEN
5980      x_return_status :=  FND_API.G_RET_STS_ERROR;
5981   WHEN OTHERS THEN
5982       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5983       THEN
5984             OE_MSG_PUB.Add_Exc_Msg
5985             (   G_PKG_NAME
5986             ,   'Load_Results_from_rec'
5987             );
5988       END IF;
5989       IF l_debug_level  > 0 THEN
5990           oe_debug_pub.add(  'UNEXPECTED ERROR IN LOAD_RESULTS_FROM_REC' , 1 ) ;
5991       END IF;
5992       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5993 END Load_Results_from_rec;
5994 
5995 
5996 /*--------------------------------------------------------------------------
5997 Procedure Name : Load_Results_from_Tbl
5998 Description    : This API loads the results from MRP's ATP_REC_TYPE to
5999                  OM's order line. It also populates OM's ATP Table which
6000                  is used to display the ATP results on the client side.
6001                  We ignore the mandatory components which we passed to MRP
6002                  while loading the results.
6003 
6004                  Added parameter p_old_line_tbl to support bug 1955004
6005                  for call to Inactive_Demand_Scheduling()
6006 -------------------------------------------------------------------------- */
6007 Procedure Load_Results_from_tbl
6008 ( p_atp_rec         IN MRP_ATP_PUB.ATP_Rec_Typ
6009 , p_old_line_tbl    IN OE_ORDER_PUB.line_tbl_type
6010 , p_x_line_tbl      IN OUT NOCOPY OE_ORDER_PUB.line_tbl_type
6011 , p_sch_action      IN VARCHAR2 := NULL
6012 , p_partial         IN BOOLEAN := FALSE
6013 , p_partial_set     IN BOOLEAN := FALSE
6014 , x_return_status OUT NOCOPY VARCHAR2)
6015 
6016 IS
6017 J                 NUMBER := 1;
6018 K                 NUMBER := 1;
6019 l_sch_action      VARCHAR2(30) := p_sch_action;
6020 l_old_ato_line_id NUMBER := -99;
6021 l_old_set_id      VARCHAR2(30) := -99;
6022 l_new_set_id      VARCHAR2(30) := -99;
6023 l_config_exists   VARCHAR2(1) := 'N';
6024 l_line_id_mod     NUMBER ;    --7827737;
6025 
6026 
6027 l_return_status   VARCHAR2(1);  -- Added for IR ISO CMS Project
6028 --
6029 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6030 --
6031 l_raise_error     BOOLEAN := FALSE;
6032 BEGIN
6033   IF l_debug_level  > 0 THEN
6034       oe_debug_pub.add(  'ENTERING LOAD_RESULTS_FROM_TBL' , 1 ) ;
6035   END IF;
6036   x_return_status :=  FND_API.G_RET_STS_SUCCESS;
6037 
6038   IF l_sch_action is NULL THEN
6039      l_sch_action := p_x_line_tbl(1).schedule_action_code;
6040   END IF;
6041 
6042   IF l_debug_level  > 0 THEN
6043      oe_debug_pub.add('-----------------Loading MRP Result From Tbl---------------',1);
6044       oe_debug_pub.add(  'Loading mrp results' , 1 ) ;
6045       oe_debug_pub.add(  'Mrp count is ' || P_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
6046       oe_debug_pub.add(  'Line count is ' || P_X_LINE_TBL.COUNT , 1 ) ;
6047       oe_debug_pub.add(  'Scheduling action ' || L_SCH_ACTION , 1 ) ;
6048   END IF;
6049 
6050   g_atp_tbl.delete;
6051 
6052   FOR I in 1..p_x_line_tbl.count LOOP
6053    l_line_id_mod := MOD(p_x_line_tbl(I).line_id,G_BINARY_LIMIT); --7827737
6054    l_config_exists := 'N'; --Bug 13777961: Re-initialize the variable
6055 
6056     -- BUG 1955004
6057     --IF OE_inactive_demand_tbl.EXISTS(p_x_line_tbl(I).line_id) THEN
6058     IF OE_inactive_demand_tbl.EXISTS(l_line_id_mod) THEN  --7827737
6059 
6060        -- we know this is line has an inactive demand scheduling level
6061 
6062         IF l_debug_level  > 0 THEN
6063             oe_debug_pub.add(  'CALLING INACTIVE_DEMAND_SCHEDULING FROM LOAD_RESULTS_FROM_TBL' , 1 ) ;
6064         END IF;
6065         Inactive_demand_scheduling(p_x_old_line_rec => p_old_line_tbl(I)
6066                                   ,p_x_line_rec => p_x_line_tbl(I)
6067                                   ,p_sch_action =>  L_SCH_ACTION  --14097050
6068                                   ,x_return_status => x_return_status);
6069 
6070         --OE_inactive_demand_tbl.DELETE(p_x_line_tbl(I).line_id);
6071         OE_inactive_demand_tbl.DELETE(l_line_id_mod);  --7827737
6072         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6073           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6074         END IF;
6075 
6076     ELSE
6077      -- END 1955004
6078 
6079      IF p_x_line_tbl(I).item_type_code = OE_GLOBALS.G_ITEM_CONFIG THEN
6080 
6081         -- The config item might be a part of the table since query
6082         -- of  group of lines returns back the config item too. But
6083         -- we did not pass this item to MRP (in load_mrp_request). Thus
6084         -- we will bypass this record out here too.
6085 
6086         -- Since we don't pass config line to MRP we need populate schedule date
6087         -- on config from Model line. This is to fix bug1576412.
6088 
6089         -- We need populate the model record scheduling values
6090         -- on the config line.
6091 
6092         K := 1;
6093         WHILE K <= p_atp_rec.error_code.count LOOP
6094 
6095 
6096          -- code has been changed to fix bug 2314594.
6097          IF p_x_line_tbl(I).ato_line_id = p_atp_rec.Identifier(K) THEN
6098             EXIT;
6099          END IF;
6100          K := K +1;
6101 
6102         END LOOP;
6103 
6104         IF l_sch_action =  OESCH_ACT_RESCHEDULE
6105           AND NVL(p_x_line_tbl(I).ordered_quantity,0) > 0 THEN -- 3907522
6106 
6107         -- Modified this part to fix bug 1900085.
6108            IF l_debug_level  > 0 THEN
6109                oe_debug_pub.add(  'UPDATING CONFIG LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
6110            END IF;
6111 
6112            IF p_atp_rec.group_ship_date(k) IS NOT NULL
6113            THEN
6114               p_x_line_tbl(I).schedule_ship_date := p_atp_rec.group_ship_date(k);
6115            ELSE
6116               p_x_line_tbl(I).schedule_ship_date  := p_atp_rec.ship_date(k);
6117            END IF;
6118 
6119            IF p_atp_rec.group_arrival_date(k) IS NOT NULL THEN
6120               p_x_line_tbl(I).schedule_arrival_date := p_atp_rec.group_arrival_date(k);
6121            ELSE
6122               p_x_line_tbl(I).schedule_arrival_date := p_atp_rec.arrival_date(k);
6123 
6124            END IF;
6125 
6126            p_x_line_tbl(I).delivery_lead_time := p_atp_rec.delivery_lead_time(k);
6127 
6128           IF l_debug_level  > 0 THEN
6129              oe_debug_pub.add(  'Config schedule ' || P_X_LINE_TBL(I).SCHEDULE_SHIP_DATE , 2 ) ;
6130              oe_debug_pub.add(  'Config arrival ' || P_X_LINE_TBL(I).SCHEDULE_ARRIVAL_DATE , 2 ) ;
6131           END IF;
6132         END IF;
6133 
6134         --4052648
6135         IF l_sch_action =  OESCH_ACT_UNSCHEDULE THEN
6136            IF l_debug_level  > 0 THEN
6137               oe_debug_pub.add(  'CLEARING SCHEDULE INFORMATION FOR CONFIG LINE ' , 2 ) ;
6138            END IF;
6139            p_x_line_tbl(I).schedule_ship_date    := null;
6140            p_x_line_tbl(I).schedule_arrival_date := null;
6141            p_x_line_tbl(I).schedule_status_code  := null;
6142            p_x_line_tbl(I).visible_demand_flag   := null;
6143 
6144            IF  p_x_line_tbl(I).ship_set_id IS NOT NULL
6145            AND p_x_line_tbl(I).ship_set_id <> FND_API.G_MISS_NUM THEN
6146               p_x_line_tbl(I).ship_set     := null;
6147            END IF;
6148 
6149            IF  p_x_line_tbl(I).arrival_set_id IS NOT NULL
6150            AND p_x_line_tbl(I).arrival_set_id <> FND_API.G_MISS_NUM THEN
6151               p_x_line_tbl(I).arrival_set  := null;
6152            END IF;
6153 
6154            -- Unscheduling a line will also clear the Override Atp flag
6155            p_x_line_tbl(I).override_atp_date_code := Null;
6156         END IF;
6157 
6158      ELSE
6159 
6160       IF p_x_line_tbl(I).ato_line_id is not null
6161       AND p_x_line_tbl(I).ato_line_id <> l_old_ato_line_id
6162       THEN
6163 
6164         l_old_ato_line_id := p_x_line_tbl(I).ato_line_id;
6165         IF l_debug_level  > 0 THEN
6166            oe_debug_pub.add(  'Check for config on line ' || P_X_LINE_TBL ( I ) .ATO_LINE_ID , 1 ) ;
6167         END IF;
6168         BEGIN
6169 
6170           Select 'Y'
6171           Into   l_config_exists
6172           From   oe_order_lines_all
6173           Where  header_id = p_x_line_tbl(I).header_id
6174           And    ato_line_id = p_x_line_tbl(I).ato_line_id
6175           And    item_type_code = OE_GLOBALS.G_ITEM_CONFIG;
6176 
6177         EXCEPTION
6178               WHEN OTHERS THEN
6179                IF l_debug_level  > 0 THEN
6180                    oe_debug_pub.add(  'NO CONFIG EXISTS FOR ATO ' , 1 ) ;
6181                END IF;
6182                l_config_exists := 'N';
6183         END;
6184 
6185       END IF;
6186 
6187       IF l_debug_level  > 0 THEN
6188           oe_debug_pub.add(  'Load results line_id ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
6189           oe_debug_pub.add(  'Index ' || J , 1 ) ;
6190       END IF;
6191 
6192 
6193       IF  l_sch_action = OESCH_ACT_ATP_CHECK
6194       AND OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
6195       AND MSC_ATP_GLOBAL.GET_APS_VERSION = 10
6196       AND p_x_line_tbl(I).ato_line_id is not null
6197       AND p_x_line_tbl(I).ato_line_id <> p_x_line_tbl(I).line_id  THEN
6198 
6199         IF l_debug_level  > 0 THEN
6200           oe_debug_pub.add(  'Do not call load_results for options/calsss');
6201         END IF;
6202 
6203       ELSE
6204 
6205         Load_Results_from_rec(p_atp_rec       => p_atp_rec,
6206                               p_x_line_rec    => p_x_line_tbl(I),
6207                               p_sch_action    => l_sch_action,
6208                               p_index         => J,
6209                               p_config_exists => l_config_exists,
6210                               p_partial_set   => p_partial_set,
6211                               x_return_status => x_return_status);
6212 
6213         IF l_debug_level  > 0 THEN
6214           oe_debug_pub.add(  'After call to load_results_from_rec' || J  || x_return_status, 1 ) ;
6215         END IF;
6216 
6217         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6218            IF l_debug_level  > 0 THEN
6219                oe_debug_pub.add(  'UNEXP ERROR LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
6220            END IF;
6221            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6222         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6223         -- This code is required, if user performing scheduling action
6224         -- from header or using multi selected lines, system has to
6225         -- commit those lines which went through scheduling fine.
6226         -- Ignore other records.
6227 
6228            l_new_set_id := NVL(p_atp_rec.ship_set_name(J),
6229                               NVL(p_atp_rec.arrival_set_name(J),-99));
6230 
6231            IF l_old_set_id <> l_new_set_id
6232 --           AND p_atp_rec.error_code(J) = 19
6233            AND NOT p_partial_set  THEN
6234 
6235             IF l_debug_level  > 0 THEN
6236                 oe_debug_pub.add(  'Before setting message for: ' || P_ATP_REC.ERROR_CODE(J) , 2 ) ;
6237             END IF;
6238 
6239              l_old_set_id := l_new_set_id;
6240              FND_MESSAGE.SET_NAME('ONT','OE_SCH_GROUP_MEMBER_FAILED');
6241              OE_MSG_PUB.Add;
6242 
6243 
6244            END IF;
6245            IF p_partial THEN
6246               IF l_debug_level  > 0 THEN
6247                   oe_debug_pub.add(  'SET OPERATION TO NONE' , 1 ) ;
6248               END IF;
6249               p_x_line_tbl(I).operation := OE_GLOBALS.G_OPR_NONE;
6250               x_return_status := FND_API.G_RET_STS_SUCCESS;
6251            ELSE
6252               IF l_debug_level  > 0 THEN
6253                   oe_debug_pub.add(  'EXP ERROR LINE ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
6254               END IF;
6255               l_raise_error := TRUE;
6256               --RAISE FND_API.G_EXC_ERROR;
6257            END IF;
6258         END IF;
6259       END IF; -- GOP Code
6260      END IF;
6261 
6262 
6263      -- As part of the bug fix 2910899, OM will indicate and remember the
6264      -- Standard Madatory record positions using vendor_name. This will be
6265      -- used in the load_results procedure to bypass the SMC records.
6266      -- Increment and skip the smc records.
6267 
6268      IF I < p_x_line_tbl.count AND
6269         p_x_line_tbl(I).item_type_code <> 'CONFIG' AND
6270         J < p_atp_rec.Identifier.count  THEN
6271          J := J + 1;
6272          IF (nvl(p_atp_rec.vendor_name(J),'N') = 'SMC')
6273          THEN
6274 
6275             WHILE (nvl(p_atp_rec.vendor_name(J),'N') = 'SMC')
6276             LOOP
6277               IF l_debug_level  > 0 THEN
6278               oe_debug_pub.add(  'SMC  : '|| J  , 3 ) ;
6279               END IF;
6280                J := J + 1;
6281          IF p_atp_rec.identifier.count < J THEN
6282          GOTO END_ATP_WHILE;
6283          END IF;
6284             END LOOP;
6285 
6286       << END_ATP_WHILE >>
6287       NULL;
6288 
6289          END IF;
6290      END IF;
6291      -- End of increment and skip.
6292 
6293   IF l_debug_level  > 0 THEN
6294       oe_debug_pub.add(  'OPERATION : ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
6295   END IF;
6296 
6297     END IF; -- for new IF statement for BUG 1955004
6298 
6299 /* 7576948: IR ISO Change Management Begins */
6300 
6301 /* Commented for bug #8205242
6302 
6303 -- This code is hooked for IR ISO project, where if the schedule ship
6304 -- date is changed as a result of MRP call then a delayed request is
6305 -- logged to call the PO_RCO_Validation_GRP.Update_ReqChange_from_SO
6306 -- API from Purchasing, responsible for conditionally updating the Need By
6307 -- Date column in internal requisition line. This will be done based on
6308 -- PO profile 'POR: Sync up Need by date on IR with OM' set to YES
6309 
6310 -- For details on IR ISO CMS project, please refer to FOL >
6311 -- OM Development > OM GM > 12.1.1 > TDD > IR_ISO_CMS_TDD.doc
6312 
6313 
6314     IF (p_x_line_tbl(i).order_source_id = 10) AND
6315        (
6316         ((p_old_line_tbl(i).schedule_ship_date IS NOT NULL) AND
6317           NOT OE_GLOBALS.Equal(p_x_line_tbl(i).schedule_ship_date,p_old_line_tbl(i).schedule_ship_date))
6318        OR
6319         ((p_old_line_tbl(i).schedule_arrival_date IS NOT NULL) AND
6320           NOT OE_GLOBALS.Equal(p_x_line_tbl(i).schedule_arrival_date,p_old_line_tbl(i).schedule_arrival_date))
6321        )  THEN
6322 
6323        IF NOT OE_Internal_Requisition_Pvt.G_Update_ISO_From_Req
6324          AND NOT OE_SALES_CAN_UTIL.G_IR_ISO_HDR_CANCEL THEN
6325          -- AND FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
6326          IF FND_PROFILE.VALUE('POR_SYNC_NEEDBYDATE_OM') = 'YES' THEN
6327          -- Modified for IR ISO Tracking bug 7667702
6328 
6329          IF l_debug_level > 0 THEN
6330            oe_debug_pub.add(' Logging G_UPDATE_REQUISITION delayed request for date change');
6331          END IF;
6332 
6333          -- Log a delayed request to update the change in Schedule Ship Date to
6334          -- Requisition Line. This request will be logged only if the change is
6335          -- not initiated from Requesting Organization, and it is not a case of
6336          -- Internal Sales Order Full Cancellation. It will even not be logged
6337          -- Purchasing profile option does not allow update of Need By Date when
6338          -- Schedule Ship Date changes on internal sales order line
6339 
6340          OE_delayed_requests_Pvt.log_request
6341          ( p_entity_code            => OE_GLOBALS.G_ENTITY_LINE
6342          , p_entity_id              => p_x_line_tbl(i).line_id
6343          , p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE
6344          , p_requesting_entity_id   => p_x_line_tbl(i).line_id
6345          , p_request_unique_key1    => p_x_line_tbl(i).header_id  -- Order Hdr_id
6346          , p_request_unique_key2    => p_x_line_tbl(i).source_document_id -- Req Hdr_id
6347          , p_request_unique_key3    => p_x_line_tbl(i).source_document_line_id -- Req Line_id
6348          , p_date_param1            => p_x_line_tbl(i).schedule_arrival_date -- schedule_ship_date
6349 -- Note: p_date_param1 is used for both Schedule_Ship_Date and
6350 -- Schedule_Arrival_Date, as while executing G_UPDATE_REQUISITION delayed
6351 -- request via OE_Process_Requisition_Pvt.Update_Internal_Requisition,
6352 -- it can expect change with respect to Ship or Arrival date. Thus, will
6353 -- not raise any issues.
6354          , p_request_type           => OE_GLOBALS.G_UPDATE_REQUISITION
6355          , x_return_status          => l_return_status
6356          );
6357 
6358          IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6359            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6360          ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
6361            RAISE FND_API.G_EXC_ERROR;
6362          END IF;
6363 
6364          ELSE -- Added for IR ISO Tracking bug 7667702
6365            IF NOT OE_Schedule_GRP.G_ISO_Planning_Update THEN
6366              IF l_debug_level > 0 THEN
6367                oe_debug_pub.add(' Need By Date is not allowed to update. Updating MTL_Supply only',5);
6368              END IF;
6369 
6370              OE_SCHEDULE_UTIL.Update_PO(p_x_line_tbl(i).schedule_arrival_date,
6371                 p_x_line_tbl(i).source_document_id,
6372                 p_x_line_tbl(i).source_document_line_id);
6373            END IF;
6374          END IF;
6375 
6376        END IF;
6377      END IF; -- Order_Source_id
6378 
6379 */
6380 
6381 /* ============================= */
6382 /* IR ISO Change Management Ends */
6383 
6384 
6385   END LOOP;
6386 
6387   --ER 2736896, Bug 16614829
6388   --Do not delete the global item/warehouse table during auto split
6389   IF NOT G_GOP_AUTO_SPLIT THEN
6390      G_ITEM_ORG_TABLE.DELETE;
6391   END IF;
6392 
6393   IF l_raise_error THEN
6394 
6395      RAISE FND_API.G_EXC_ERROR;
6396 
6397   END IF;
6398 
6399   IF l_debug_level  > 0 THEN
6400       oe_debug_pub.add(  'EXITING LOAD_RESULTS_FROM_TBL' , 1 ) ;
6401   END IF;
6402 
6403 EXCEPTION
6404    WHEN FND_API.G_EXC_ERROR THEN
6405 
6406       x_return_status :=  FND_API.G_RET_STS_ERROR;
6407   WHEN OTHERS THEN
6408       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6409       THEN
6410             OE_MSG_PUB.Add_Exc_Msg
6411             (   G_PKG_NAME
6412             ,   'Load_Results_from_tbl'
6413             );
6414       END IF;
6415       IF l_debug_level  > 0 THEN
6416           oe_debug_pub.add(  'UNEXPECTED ERROR IN LOAD_RESULTS_FROM_TBL' , 1 ) ;
6417       END IF;
6418       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6419 END Load_Results_from_tbl;
6420 
6421 Procedure Display_Sch_Errors
6422 ( p_atp_rec         IN  MRP_ATP_PUB.ATP_Rec_Typ
6423 , p_line_tbl        IN  OE_ORDER_PUB.line_tbl_type
6424                         := OE_ORDER_PUB.G_MISS_LINE_TBL
6425 , p_line_id         IN  NUMBER DEFAULT NULL)
6426 
6427 IS
6428 
6429 J                 NUMBER := 1;
6430 l_old_set_id      Varchar2(30) := -99;
6431 l_new_set_id      Varchar2(30) := -99;
6432 l_explanation     VARCHAR2(240);
6433 l_order_source_id          NUMBER;
6434 l_orig_sys_document_ref    VARCHAR2(50);
6435 l_orig_sys_line_ref        VARCHAR2(50);
6436 l_orig_sys_shipment_ref    VARCHAR2(50);
6437 l_change_sequence          VARCHAR2(50);
6438 l_source_document_id       NUMBER;
6439 l_source_document_line_id  NUMBER;
6440 l_source_document_type_id  NUMBER;
6441 l_header_id                NUMBER;
6442 
6443 --
6444 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6445 --
6446 BEGIN
6447 
6448     IF l_debug_level  > 0 THEN
6449         oe_debug_pub.add(  'ENTERING DISPLAY_SCH_ERRORS' , 1 ) ;
6450     END IF;
6451 
6452 IF p_atp_rec.error_code.count > 0 THEN
6453 
6454  IF p_line_id is not null THEN
6455 
6456 
6457     IF l_debug_level  > 0 THEN
6458         oe_debug_pub.add(  'SCHEDULING FAILED WITH ERROR CODE: ' || P_LINE_ID ) ;
6459     END IF;
6460 
6461     IF p_line_id <> FND_API.G_MISS_NUM THEN
6462        select order_source_id, orig_sys_document_ref, orig_sys_line_ref,
6463               orig_sys_shipment_ref, change_sequence, source_document_id,
6464               source_document_line_id, source_document_type_id, header_id
6465        into l_order_source_id, l_orig_sys_document_ref, l_orig_sys_line_ref,
6466             l_orig_sys_shipment_ref, l_change_sequence, l_source_document_id,
6467             l_source_document_line_id, l_source_document_type_id, l_header_id
6468        from oe_order_lines
6469        where line_id = p_line_id;
6470      END IF;
6471 
6472      OE_MSG_PUB.set_msg_context(
6473        p_entity_code                => 'LINE'
6474       ,p_entity_id                  => p_line_id
6475       ,p_header_id                  => l_header_id
6476       ,p_line_id                    => p_line_id
6477       ,p_orig_sys_document_ref      => l_orig_sys_document_ref
6478       ,p_orig_sys_document_line_ref => l_orig_sys_line_ref
6479       ,p_orig_sys_shipment_ref      => l_orig_sys_shipment_ref
6480       ,p_change_sequence            => l_change_sequence
6481       ,p_source_document_id         => l_source_document_id
6482       ,p_source_document_line_id    => l_source_document_line_id
6483       ,p_order_source_id            => l_order_source_id
6484       ,p_source_document_type_id    => l_source_document_type_id);
6485 
6486      IF p_atp_rec.error_code(J) = 80 THEN
6487 
6488         FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
6489         OE_MSG_PUB.Add;
6490         l_explanation := null;
6491 
6492      ELSIF p_atp_rec.error_code(J) <> 0 THEN
6493 
6494         select meaning
6495         into l_explanation
6496         from mfg_lookups where
6497         lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
6498         and lookup_code = p_atp_rec.error_code(J) ;
6499 
6500         IF l_debug_level  > 0 THEN
6501             oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
6502         END IF;
6503         FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
6504         FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
6505         OE_MSG_PUB.Add;
6506 
6507       ELSE
6508 
6509         FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATP_ERROR');
6510         OE_MSG_PUB.Add;
6511       END IF;
6512 
6513 
6514  ELSE
6515 
6516   FOR I in 1..p_line_tbl.count LOOP
6517 
6518    IF p_line_tbl(I).Item_type_code <> 'CONFIG' THEN
6519 
6520      l_new_set_id := NVL(p_atp_rec.ship_set_name(J),
6521                      NVL(p_atp_rec.arrival_set_name(J),-99));
6522 
6523      IF l_old_set_id <> l_new_set_id
6524      AND p_atp_rec.error_code(J) = 19 THEN
6525 
6526       IF l_debug_level  > 0 THEN
6527          oe_debug_pub.add(  'Before setting message for: ' || P_ATP_REC.ERROR_CODE ( J ) , 2 ) ;
6528       END IF;
6529 
6530       l_old_set_id := l_new_set_id;
6531       FND_MESSAGE.SET_NAME('ONT','OE_SCH_GROUP_MEMBER_FAILED');
6532       OE_MSG_PUB.Add;
6533 
6534      ELSIF p_atp_rec.error_code(J) <> 0
6535      AND   p_atp_rec.error_code(J) <> 19 THEN
6536 
6537       IF l_debug_level  > 0 THEN
6538           oe_debug_pub.add(  'SCHEDULING FAILED WITH ERROR CODE:'||P_ATP_REC.ERROR_CODE ( J ) , 1 ) ;
6539       END IF;
6540 
6541 
6542           OE_MSG_PUB.set_msg_context(
6543            p_entity_code                => 'LINE'
6544           ,p_entity_id                  => p_line_tbl(I).line_id
6545           ,p_header_id                  => p_line_tbl(I).header_id
6546           ,p_line_id                    => p_line_tbl(I).line_id
6547           ,p_order_source_id            => p_line_tbl(I).order_source_id
6548           ,p_orig_sys_document_ref      => p_line_tbl(I).orig_sys_document_ref
6549           ,p_orig_sys_document_line_ref => p_line_tbl(I).orig_sys_line_ref
6550           ,p_orig_sys_shipment_ref      => p_line_tbl(I).orig_sys_shipment_ref
6551           ,p_change_sequence            => p_line_tbl(I).change_sequence
6552           ,p_source_document_type_id    => p_line_tbl(I).source_document_type_id
6553           ,p_source_document_id         => p_line_tbl(I).source_document_id
6554           ,p_source_document_line_id    => p_line_tbl(I).source_document_line_id);
6555 
6556           IF p_atp_rec.error_code(J) = 80 THEN
6557 
6558             FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_SOURCE');
6559             OE_MSG_PUB.Add;
6560             l_explanation := null;
6561 
6562           ELSE
6563             select meaning
6564             into l_explanation
6565             from mfg_lookups where
6566             lookup_type = 'MTL_DEMAND_INTERFACE_ERRORS'
6567             and lookup_code = p_atp_rec.error_code(J) ;
6568 
6569             IF l_debug_level  > 0 THEN
6570                 oe_debug_pub.add(  'ADDING MESSAGE TO THE STACK' , 1 ) ;
6571             END IF;
6572             FND_MESSAGE.SET_NAME('ONT','OE_SCH_OE_ORDER_FAILED');
6573             FND_MESSAGE.SET_TOKEN('EXPLANATION',l_explanation);
6574             OE_MSG_PUB.Add;
6575 
6576 
6577           END IF;
6578       END IF;
6579 
6580    -- Increment and skip the smc records.
6581       IF I < p_line_tbl.count AND
6582          p_line_tbl(I).item_type_code <> 'CONFIG' AND
6583          J < p_atp_rec.Identifier.count  THEN
6584 
6585           J := J + 1;
6586           IF (p_line_tbl(I).line_id = p_atp_rec.Identifier(J))
6587           THEN
6588 
6589             WHILE (p_atp_rec.Identifier(J) = p_line_tbl(I).line_id)
6590             LOOP
6591                 J := J + 1;
6592               IF p_atp_rec.identifier.count < J THEN
6593                 GOTO END_ATP_WHILE;
6594               END IF;
6595             END LOOP;
6596 
6597           << END_ATP_WHILE >>
6598           NULL;
6599 
6600          END IF;
6601       END IF;
6602     END IF;
6603   END LOOP;
6604  END IF; -- p_line
6605 
6606 END IF;
6607 
6608 End Display_Sch_Errors;
6609 
6610 /*---------------------------------------------------------------------
6611 Procedure Name : Call_MRP_ATP
6612 Description    : Create and call MRP API.
6613 --------------------------------------------------------------------- */
6614 
6615 Procedure Call_MRP_ATP
6616 ( p_x_line_rec       IN OUT NOCOPY OE_ORDER_PUB.Line_Rec_Type
6617  ,p_old_line_rec     IN OE_ORDER_PUB.Line_Rec_Type
6618 ,x_return_status OUT NOCOPY VARCHAR2)
6619 
6620 IS
6621 l_msg_count               NUMBER;
6622 l_session_id              NUMBER := 0;
6623 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
6624 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
6625 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
6626 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
6627 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
6628 l_mrp_msg_data            VARCHAR2(200);
6629 
6630 --
6631 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6632 --
6633 BEGIN
6634 
6635    IF l_debug_level  > 0 THEN
6636        oe_debug_pub.add(  'ENTERING CALL MRP ATP' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
6637    END IF;
6638 
6639 -- BUG 1955004
6640  -- 3763015
6641  IF (sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
6642     sch_cached_sch_level_code = SCH_LEVEL_FIVE OR
6643     NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y')  OR
6644     Nvl(p_x_line_rec.bypass_sch_flag, 'N') = 'Y'  THEN -- DOO Integration
6645     -- this is an inactive demand line.
6646 
6647    IF l_debug_level  > 0 THEN
6648        oe_debug_pub.add(  'THIS IS A SINGLE INACTIVE DEMAND LINE' , 1 ) ;
6649    END IF;
6650 
6651    Inactive_Demand_Scheduling(p_x_old_line_rec => p_old_line_rec
6652                             ,p_x_line_rec     => p_x_line_rec
6653                             ,x_return_status  => x_return_status);
6654 
6655    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6656           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6657    ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
6658           Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
6659                              p_line_id => p_x_line_rec.line_id);
6660           RAISE FND_API.G_EXC_ERROR;
6661    END IF;
6662 
6663  ELSE
6664 
6665    IF l_debug_level  > 0 THEN
6666        oe_debug_pub.add(  'ELSE CALL FOR CALL MRP ATP' , 1 ) ;
6667    END IF;
6668 -- END 1955004
6669 
6670    Load_MRP_request_from_rec
6671        ( p_line_rec              => p_x_line_rec
6672        , p_old_line_rec          => p_old_line_rec
6673        , x_mrp_atp_rec             => l_mrp_atp_rec);
6674 
6675 
6676    IF l_mrp_atp_rec.error_code.count > 0 THEN
6677       l_session_id := Get_Session_Id;
6678 
6679       -- Call ATP
6680 
6681        IF l_debug_level  > 0 THEN
6682            oe_debug_pub.add(  '1. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
6683        END IF;
6684       /* 8731703: Code moved to Load_Results_From_rec
6685       -- Added the following 10  lines to fix the bug 6378240
6686        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);
6687      BEGIN
6688        insert into oe_schedule_lines_temp
6689                    (LINE_ID ,
6690                     SCHEDULE_ACTION_CODE)
6691               values(p_x_line_rec.line_id,p_x_line_rec.schedule_action_code);
6692      EXCEPTION
6693          WHEN OTHERS THEN
6694                   oe_debug_pub.add(  '6378240 : INSERT ERROR ');
6695      END;
6696      */
6697 
6698 
6699        MRP_ATP_PUB.Call_ATP
6700               (  p_session_id             =>  l_session_id
6701                , p_atp_rec                =>  l_mrp_atp_rec
6702                , x_atp_rec                =>  l_out_mrp_atp_rec
6703                , x_atp_supply_demand      =>  l_atp_supply_demand
6704                , x_atp_period             =>  l_atp_period
6705                , x_atp_details            =>  l_atp_details
6706                , x_return_status          =>  x_return_status
6707                , x_msg_data               =>  l_mrp_msg_data
6708                , x_msg_count              =>  l_msg_count);
6709 
6710       IF l_debug_level  > 0 THEN
6711          oe_debug_pub.add(  '3. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
6712       END IF;
6713 
6714        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6715           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6716        ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
6717           IF p_x_line_rec.schedule_action_code <> OESCH_ACT_ATP_CHECK THEN
6718              Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
6719                                 p_line_id => p_x_line_rec.line_id);
6720              RAISE FND_API.G_EXC_ERROR;
6721           END IF;
6722        END IF;
6723 
6724        Load_Results_from_rec(p_atp_rec       => l_out_mrp_atp_rec,
6725                              p_x_line_rec    => p_x_line_rec,
6726                              x_return_status => x_return_status);
6727 
6728        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6729           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6730        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
6731           RAISE FND_API.G_EXC_ERROR;
6732        END IF;
6733 
6734    END IF; -- Mrp count.
6735    IF l_debug_level  > 0 THEN
6736        oe_debug_pub.add(  'EXITING CALL ATP' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
6737    END IF;
6738 
6739   END IF;  -- for new IF statement added for 1955004
6740 
6741 EXCEPTION
6742    WHEN FND_API.G_EXC_ERROR THEN
6743 
6744         x_return_status := FND_API.G_RET_STS_ERROR;
6745 
6746    WHEN OTHERS THEN
6747 
6748         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6749 
6750         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6751         THEN
6752             OE_MSG_PUB.Add_Exc_Msg
6753             (   G_PKG_NAME,
6754               'Call_MRP_ATP');
6755         END IF;
6756         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6757 END Call_MRP_ATP;
6758 PROCEDURE Cascade_Override_atp
6759  (p_line_rec IN     OE_ORDER_PUB.line_rec_type)
6760 IS
6761 --
6762 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6763 --
6764 BEGIN
6765 
6766    IF l_debug_level  > 0 THEN
6767        oe_debug_pub.add(  'ENTERING CASCADE_OVERRIDE_ATP ' , 1 ) ;
6768    END IF;
6769 
6770     IF   p_line_rec.ato_line_id IS NOT NULL AND
6771     NOT (p_line_rec.ato_line_id = p_line_rec.line_id AND
6772          p_line_rec.item_type_code = 'OPTION') THEN
6773 
6774        UPDATE OE_ORDER_LINES_ALL
6775        SET    override_atp_date_code = Null
6776        WHERE  header_id = p_line_rec.header_id
6777        AND    ato_line_id = p_line_rec.ato_line_id;
6778 
6779     ELSIF  p_line_rec.item_type_code = 'CLASS'
6780        OR  p_line_rec.item_type_code = 'MODEL'
6781        OR  p_line_rec.item_type_code = 'KIT'
6782     THEN
6783 
6784        UPDATE OE_ORDER_LINES_ALL
6785        SET    override_atp_date_code = Null
6786        WHERE  header_id = p_line_rec.header_id
6787        AND    link_to_line_id  = p_line_rec.line_id
6788        AND    item_type_code = 'INCLUDED';
6789 
6790     END IF;
6791 
6792    IF l_debug_level  > 0 THEN
6793        oe_debug_pub.add(  'EXITING CASCADE_OVERRIDE_ATP ' , 1 ) ;
6794    END IF;
6795 EXCEPTION
6796 
6797    WHEN OTHERS THEN
6798 
6799         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6800         THEN
6801             OE_MSG_PUB.Add_Exc_Msg
6802             (   G_PKG_NAME,
6803               'Cascade Override Atp');
6804         END IF;
6805         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6806 END Cascade_Override_atp;
6807 /*---------------------------------------------------------------------
6808 Procedure Name : Query_Qty_Tree
6809 Description    : Queries the On-Hand and Available to Reserve
6810                  quantites by calling INV's
6811                  inv_quantity_tree_pub.query_quantities.
6812                  The quantities are given at the highest level (Item, Org
6813                  combination).
6814 --------------------------------------------------------------------- */
6815 Procedure Query_Qty_Tree(p_org_id            IN NUMBER,
6816                          p_item_id           IN NUMBER,
6817                          p_line_id           IN NUMBER DEFAULT NULL,
6818                          p_sch_date          IN DATE DEFAULT NULL,
6819 --                         p_grade_code        IN VARCHAR2 DEFAULT NULL, -- INVCONV NOT NEEDED NOW
6820                          x_on_hand_qty OUT NOCOPY NUMBER,
6821                          x_avail_to_reserve OUT NOCOPY NUMBER,
6822                          x_on_hand_qty2 OUT NOCOPY NUMBER, -- INVCONV
6823                          x_avail_to_reserve2 OUT NOCOPY NUMBER ,-- INVCONV
6824                          p_subinventory_code  IN VARCHAR2 DEFAULT NULL --11777419
6825                          )
6826 
6827 IS
6828   l_return_status           VARCHAR2(1);
6829   l_msg_count               NUMBER;
6830   l_msg_data                VARCHAR2(2000);
6831   l_qoh                     NUMBER;
6832   l_rqoh                    NUMBER;
6833   l_qr                      NUMBER;
6834   l_qs                      NUMBER;
6835   l_att                     NUMBER;
6836   l_atr                     NUMBER;
6837 
6838   l_sqoh                     NUMBER; -- INVCONV
6839   l_srqoh                    NUMBER; -- INVCONV
6840   l_sqr                      NUMBER; -- INVCONV
6841   l_sqs                      NUMBER; -- INVCONV
6842   l_satt                     NUMBER; -- INVCONV
6843   l_satr                     NUMBER; -- INVCONV
6844 
6845 
6846   l_msg_index               number;
6847   l_lot_control_flag        BOOLEAN;
6848   l_lot_control_code        NUMBER;
6849   l_org_id                  NUMBER;
6850 
6851 
6852   -- added by fabdi 03/May/2001
6853 --  l_process_flag      VARCHAR2(1) := FND_API.G_FALSE; -- INVCONV
6854 --  l_ic_item_mst_rec         GMI_RESERVATION_UTIL.ic_item_mst_rec; -- INVCONV
6855   -- end fabdi
6856 
6857 --
6858 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
6859 --
6860 BEGIN
6861 
6862   IF l_debug_level  > 0 THEN
6863       oe_debug_pub.add(  'ENTERING QUERY_QTY_TREE ' , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
6864   END IF;
6865   IF l_debug_level  > 0 THEN
6866       oe_debug_pub.add(  'ORG IS : ' || P_ORG_ID , 1 ) ;
6867   END IF;
6868   IF l_debug_level  > 0 THEN
6869       oe_debug_pub.add(  'ITEM IS : ' || P_ITEM_ID , 1 ) ;
6870       oe_debug_pub.add(  'Sub inventory IS : ' || p_subinventory_code , 1 ) ; --11777419
6871   END IF;
6872   -- added by fabdi 03/May/2001
6873 /*  IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_org_id) -- INVCONV remove OPM stuff
6874   THEN
6875   l_process_flag := FND_API.G_FALSE;
6876   ELSE
6877   l_process_flag := FND_API.G_TRUE;
6878   END IF;
6879 
6880   IF l_process_flag = FND_API.G_TRUE
6881   THEN
6882 
6883         GMI_RESERVATION_UTIL.Get_OPM_item_from_Apps
6884         ( p_organization_id          =>  p_org_id
6885         , p_inventory_item_id        =>  p_item_id
6886         , x_ic_item_mst_rec          =>  l_ic_item_mst_rec
6887         , x_return_status            =>  l_return_status
6888         , x_msg_count                =>  l_msg_count
6889         , x_msg_data                 =>  l_msg_data);
6890 
6891         IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)
6892         THEN
6893            FND_MESSAGE.SET_NAME('GMI','GMI_ERROR');
6894            FND_MESSAGE.SET_TOKEN('BY_PROC',
6895                             'GMI_Reservation_Util.Get_OPM_item_from_Apps');
6896            FND_MESSAGE.SET_TOKEN('WHERE','OE_SCHEDULE_UTIL');
6897            RAISE FND_API.G_EXC_ERROR;
6898         END IF;
6899         get_process_query_quantities ( p_org_id => p_org_id,
6900                                        p_item_id =>  l_ic_item_mst_rec.item_id,
6901                                        p_line_id => p_line_id,
6902                                        x_on_hand_qty => l_qoh,
6903                                        x_avail_to_reserve => l_atr
6904                                       );
6905 
6906   -- end fabdi
6907   ELSE   */
6908 
6909   BEGIN
6910      -- Added code to fix bug 2111470
6911     IF p_org_id is null THEN
6912        l_org_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
6913     END IF;
6914 
6915     SELECT msi.lot_control_code
6916     INTO   l_lot_control_code
6917     FROM   mtl_system_items msi
6918     WHERE  msi.inventory_item_id = p_item_id
6919     AND    msi.organization_id   = nvl(p_org_id,l_org_id);
6920 
6921     IF l_lot_control_code = 2 THEN
6922        l_lot_control_flag := TRUE;
6923     ELSE
6924        l_lot_control_flag := FALSE;
6925     END IF;
6926 
6927   EXCEPTION
6928    WHEN OTHERS THEN
6929    l_lot_control_flag := FALSE;
6930   END;
6931 
6932   -- Bug 2259553.
6933   --inv_quantity_tree_pvt.clear_quantity_cache;
6934     inv_quantity_tree_pvt.mark_all_for_refresh
6935   (  p_api_version_number  => 1.0
6936    , p_init_msg_lst        => FND_API.G_TRUE
6937    , x_return_status       => l_return_status
6938    , x_msg_count           => l_msg_count
6939    , x_msg_data            => l_msg_data
6940    );
6941 
6942   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6943          oe_msg_pub.transfer_msg_stack;
6944          l_msg_count:=OE_MSG_PUB.COUNT_MSG;
6945          for I in 1..l_msg_count loop
6946              l_msg_data := OE_MSG_PUB.Get(I,'F');
6947              IF l_debug_level  > 0 THEN
6948                  oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
6949              END IF;
6950          end loop;
6951          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6952   ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
6953           oe_msg_pub.transfer_msg_stack;
6954           l_msg_count:=OE_MSG_PUB.COUNT_MSG;
6955           for I in 1..l_msg_count loop
6956               l_msg_data := OE_MSG_PUB.Get(I,'F');
6957               IF l_debug_level  > 0 THEN
6958                   oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
6959               END IF;
6960           end loop;
6961           RAISE FND_API.G_EXC_ERROR;
6962   END IF;
6963 
6964 
6965   inv_quantity_tree_pub.query_quantities
6966     (  p_api_version_number      => 1.0
6967      , x_return_status           => l_return_status
6968      , x_msg_count               => l_msg_count
6969      , x_msg_data                => l_msg_data
6970      , p_organization_id         => p_org_id
6971      , p_inventory_item_id       => p_item_id
6972      , p_tree_mode               => 2
6973      , p_is_revision_control     => false
6974      , p_is_lot_control          => l_lot_control_flag
6975      , p_lot_expiration_date     => nvl(p_sch_date,sysdate)
6976      , p_is_serial_control       => false
6977      , p_grade_code              => NUll  -- INVCONV      NOT NEEDED NOW
6978      , p_revision                => null
6979      , p_lot_number              => null
6980      , p_subinventory_code       => p_subinventory_code    --null --11777419
6981      , p_locator_id              => null
6982      , x_qoh                     => l_qoh
6983      , x_rqoh                    => l_rqoh
6984      , x_qr                      => l_qr
6985      , x_qs                      => l_qs
6986      , x_att                     => l_att
6987      , x_atr                     => l_atr
6988      , x_sqoh                    => l_sqoh        -- INVCONV
6989      , x_srqoh                   => l_srqoh       -- INVCONV
6990      , x_sqr                     => l_sqr         -- INVCONV
6991      , x_sqs                     => l_sqs         -- INVCONV
6992      , x_satt                    => l_satt        -- INVCONV
6993      , x_satr                    => l_satr        -- INVCONV
6994      );
6995 
6996   IF l_debug_level  > 0 THEN
6997       oe_debug_pub.add(  'AFTER CALLING QUERY_QUANTITIES' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
6998   END IF;
6999  -- END IF; INVCONV
7000   IF l_debug_level  > 0 THEN
7001       oe_debug_pub.add(  'RR: L_QOH ' || L_QOH , 1 ) ;
7002   END IF;
7003   IF l_debug_level  > 0 THEN
7004       oe_debug_pub.add(  'RR: L_QOH ' || L_ATR , 1 ) ;
7005   END IF;
7006 
7007   x_on_hand_qty      := l_qoh;
7008   x_avail_to_reserve := l_atr;
7009 
7010   x_on_hand_qty2      := l_sqoh; -- INVCONV
7011   x_avail_to_reserve2 := l_satr; -- INVCONV
7012 
7013   IF l_debug_level  > 0 THEN
7014       oe_debug_pub.add(  'EXITING QUERY_QTY_TREE ' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
7015   END IF;
7016 
7017 EXCEPTION
7018 
7019    WHEN OTHERS THEN
7020 
7021         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7022         THEN
7023             OE_MSG_PUB.Add_Exc_Msg
7024             (   G_PKG_NAME,
7025               'Query_Qty_Tree');
7026         END IF;
7027         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7028 END Query_Qty_Tree;
7029 /*---------------------------------------------------------------------
7030 Procedure Name : Action ATP
7031 Description    : This procedure is called to perform atp_check on a single
7032                  line
7033 --------------------------------------------------------------------- */
7034 Procedure Action_ATP
7035 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
7036  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
7037 x_return_status OUT NOCOPY VARCHAR2)
7038 
7039 IS
7040 l_msg_count               NUMBER;
7041 l_mrp_msg_data            VARCHAR2(2000);
7042 --l_session_id              NUMBER := 0;
7043 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
7044 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
7045 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
7046 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
7047 l_on_hand_qty             NUMBER;
7048 l_avail_to_reserve        NUMBER;
7049 l_on_hand_qty2             NUMBER; -- INVCONV
7050 l_avail_to_reserve2        NUMBER; -- INVCONV
7051 
7052 --ER#14763609
7053 l_fulfillment_base            oe_order_lines_all.fulfillment_base%type;
7054 l_secondary_uom_code          mtl_system_items_b.secondary_uom_code%type;
7055 l_primary_uom_code            mtl_system_items_b.primary_uom_code%type;
7056 l_on_hand_ord_qty             NUMBER ;
7057 l_avail_to_reserve_ord        NUMBER ;
7058 l_sub_secondary_uom_code      mtl_system_items_b.secondary_uom_code%type;
7059 l_sub_primary_uom_code        mtl_system_items_b.primary_uom_code%type;
7060 l_sub_on_hand_ord_qty         NUMBER ;
7061 l_sub_avail_to_reserve_ord    NUMBER ;
7062 --End ER#14763609
7063 
7064 -- l_process_flag            VARCHAR2(1) := FND_API.G_FALSE;
7065 --
7066 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level; --INVCONV
7067 --
7068 BEGIN
7069 
7070    IF l_debug_level  > 0 THEN
7071        oe_debug_pub.add(  'ENTERING ACTION ATP ' || P_X_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
7072    END IF;
7073 
7074      -- Call MRP API
7075 
7076    Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
7077                 p_old_line_rec  => p_old_line_rec,
7078                 x_return_status => x_return_status);
7079 
7080    IF l_debug_level  > 0 THEN
7081        oe_debug_pub.add(  'AFTER CALLING MRP API: ' || X_RETURN_STATUS , 2 ) ;
7082    END IF;
7083 
7084    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7085       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7086    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7087       RAISE FND_API.G_EXC_ERROR;
7088    END IF;
7089 
7090   /* Load_Mrp_Request_From_Rec
7091       ( p_line_rec        => p_x_line_rec
7092       , p_old_line_rec    => p_old_line_rec
7093       , x_mrp_atp_rec     => l_mrp_atp_rec);
7094 
7095    IF l_mrp_atp_rec.error_code.count > 0 THEN
7096 
7097      l_session_id := Get_Session_Id;
7098 
7099      G_ATP_CHECK_session_id := l_session_id;
7100      -- Call ATP
7101 
7102      IF l_debug_level > 0 THEN
7103       oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,0.5);  -- debug level changed to 0.5 for bug 13435459
7104      END IF;
7105 
7106      MRP_ATP_PUB.Call_ATP
7107          ( p_session_id          =>  l_session_id
7108           , p_atp_rec            =>  l_mrp_atp_rec
7109           , x_atp_rec            =>  l_mrp_atp_rec
7110           , x_atp_supply_demand  =>  l_atp_supply_demand
7111           , x_atp_period         =>  l_atp_period
7112           , x_atp_details        =>  l_atp_details
7113           , x_return_status      =>  x_return_status
7114           , x_msg_data           =>  l_mrp_msg_data
7115           , x_msg_count          =>  l_msg_count);
7116 
7117      IF l_debug_level > 0 THEN
7118        oe_debug_pub.add('5. After Calling MRP_ATP_PUB.Call_ATP' ||
7119                                               x_return_status,0.5);   -- debug level changed to 0.5 for bug 13435459
7120      END IF;
7121      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7122         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7123      END IF;
7124 
7125      Load_Results_from_rec(p_atp_rec        => l_mrp_atp_rec,
7126                            p_x_line_rec     => p_x_line_rec,
7127                            x_return_status  => x_return_status);
7128 
7129      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7130             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7131      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7132             RAISE FND_API.G_EXC_ERROR;
7133      END IF;
7134 
7135    END IF; -- Mrp count.
7136   */
7137      -- We also need to pass back on-hand qty and available_to_reserve
7138      -- qties while performing ATP. Getting these values from inventory.
7139 
7140      FOR K IN 1..g_atp_tbl.count LOOP
7141         IF l_debug_level  > 0 THEN
7142             oe_debug_pub.add(  'CALLING QUERY_QTY_TREE' , 1 ) ;
7143         END IF;
7144         Query_Qty_Tree(p_org_id     => g_atp_tbl(K).ship_from_org_id,
7145                        p_item_id    => g_atp_tbl(K).inventory_item_id,
7146                        p_line_id    => g_atp_tbl(K).line_id,
7147                        p_sch_date   => nvl(g_atp_tbl(K).group_available_date,
7148                                        g_atp_tbl(K).ordered_qty_Available_Date),
7149                        x_on_hand_qty      => l_on_hand_qty,
7150                        x_avail_to_reserve => l_avail_to_reserve,
7151                        x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
7152                        x_avail_to_reserve2 => l_avail_to_reserve2, -- INVCONV
7153                        p_subinventory_code => g_atp_tbl(K).subinventory_code --11777419
7154                       );
7155 
7156        /*  --  added by fabdi 03/May/2001  NOT NEEDED NOW FOR OPM INVENTORY CONVERGENCE INVCONV
7157         IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id
7158                                            => g_atp_tbl(K).ship_from_org_id)
7159         THEN
7160              l_process_flag := FND_API.G_FALSE;
7161         ELSE
7162              l_process_flag := FND_API.G_TRUE;
7163         END IF;
7164 
7165         IF l_process_flag = FND_API.G_TRUE
7166         THEN
7167           g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
7168           g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
7169           g_atp_tbl(K).QTY_ON_REQUEST_DATE  := l_avail_to_reserve;
7170                                                  -- Available field in ATP
7171 
7172           IF l_debug_level  > 0 THEN
7173               oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
7174           END IF;
7175           IF l_debug_level  > 0 THEN
7176               oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
7177           END IF;
7178           IF l_debug_level  > 0 THEN
7179               oe_debug_pub.add(  'AVAILABLE ' || L_AVAIL_TO_RESERVE ) ;
7180           END IF;
7181         ELSE   */  --  INVCONV
7182 
7183     --ER#14763609 Start
7184          IF l_debug_level  > 0 THEN
7185             oe_debug_pub.add(  '1. Fetching primary and secondary UOM' , 1 ) ;
7186          END IF;
7187 
7188 	 SELECT secondary_uom_code ,primary_uom_code
7189           INTO l_secondary_uom_code ,l_primary_uom_code
7190           FROM mtl_system_items_b
7191           WHERE inventory_item_id = g_atp_tbl(K).inventory_item_id
7192           AND organization_id   = g_atp_tbl(K).ship_from_org_id
7193           AND  ROWNUM=1;
7194 
7195          IF l_debug_level  > 0 THEN
7196             oe_debug_pub.add(  '1. l_primary_uom_code / l_secondary_uom_code ' || l_primary_uom_code || ' / ' || l_secondary_uom_code , 1 ) ;
7197          END IF;
7198 
7199          l_fulfillment_base:= NVL(p_x_line_rec.fulfillment_base,'P') ;
7200 
7201          IF l_debug_level  > 0 THEN
7202             oe_debug_pub.add(  '1. fulfillment_base: '|| l_fulfillment_base , 1 ) ;
7203          END IF;
7204     --ER#14763609 End
7205 
7206           IF l_debug_level  > 0 THEN
7207               oe_debug_pub.add(  'SUBSTITUTE_FLAG :' || G_ATP_TBL ( K ) .SUBSTITUTE_FLAG , 1 ) ;
7208           END IF;
7209           IF g_atp_tbl(K).substitute_flag = 'N' THEN
7210              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
7211              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
7212              IF l_debug_level  > 0 THEN
7213                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
7214                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
7215              END IF;
7216 --ER#14763609 Start
7217              IF l_fulfillment_base ='P' THEN
7218                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7219                                      (
7220                                      g_atp_tbl(K).inventory_item_id,
7221                                      l_primary_uom_code,
7222                                      g_atp_tbl(K).order_quantity_uom,
7223                                      l_on_hand_qty
7224                                      );
7225 
7226                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7227                                          (
7228                                          g_atp_tbl(K).inventory_item_id,
7229                                          l_primary_uom_code,
7230                                          g_atp_tbl(K).order_quantity_uom,
7231                                          l_avail_to_reserve
7232                                        );
7233                 ELSE
7234                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7235                                      (
7236                                      g_atp_tbl(K).inventory_item_id,
7237                                      l_secondary_uom_code,
7238                                      g_atp_tbl(K).order_quantity_uom,
7239                                      l_on_hand_qty2
7240                                      );
7241 
7242                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7243                                          (
7244                                          g_atp_tbl(K).inventory_item_id,
7245                                          l_secondary_uom_code,
7246                                          g_atp_tbl(K).order_quantity_uom,
7247                                          l_avail_to_reserve2
7248                                        );
7249                 END IF ;
7250 
7251 	     g_atp_tbl(K).on_hand_qty_orduom          := l_on_hand_ord_qty;
7252              g_atp_tbl(K).available_to_reserve_orduom := l_avail_to_reserve_ord;
7253              g_atp_tbl(K).primary_uom                 := l_primary_uom_code;
7254 
7255 	     IF l_debug_level  > 0 THEN
7256                  oe_debug_pub.add(  'l_on_hand_ord_qty: ' || l_on_hand_ord_qty ) ;
7257                  oe_debug_pub.add(  'l_avail_to_reserve_ord: ' || l_avail_to_reserve_ord ) ;
7258                  oe_debug_pub.add(  'l_primary_uom_code: ' || l_primary_uom_code ) ;
7259              END IF;
7260 
7261 --ER#14763609 End
7262 
7263 
7264           ELSE  --If substitute then
7265              g_atp_tbl(K).sub_on_hand_qty          := l_on_hand_qty;
7266              g_atp_tbl(K).sub_available_to_reserve := l_avail_to_reserve;
7267              IF l_debug_level  > 0 THEN
7268                  oe_debug_pub.add(  'SUB L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
7269                  oe_debug_pub.add(  'SUB L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
7270              END IF;
7271 --ER#14763609 Start
7272              IF l_fulfillment_base ='P' THEN
7273                 l_sub_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7274                                          (
7275                                          g_atp_tbl(K).inventory_item_id,
7276                                          l_primary_uom_code,
7277                                          g_atp_tbl(K).order_quantity_uom,
7278                                          l_on_hand_qty
7279                                          );
7280 
7281                l_sub_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7282                                          (
7283                                          g_atp_tbl(K).inventory_item_id,
7284                                          l_primary_uom_code,
7285                                          g_atp_tbl(K).order_quantity_uom,
7286                                          l_avail_to_reserve
7287                                        );
7288                 ELSE
7289                 l_sub_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7290                                      (
7291                                      g_atp_tbl(K).inventory_item_id,
7292                                      l_secondary_uom_code,
7293                                      g_atp_tbl(K).order_quantity_uom,
7294                                      l_on_hand_qty2
7295                                      );
7296 
7297                l_sub_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7298                                          (
7299                                          g_atp_tbl(K).inventory_item_id,
7300                                          l_secondary_uom_code,
7301                                          g_atp_tbl(K).order_quantity_uom,
7302                                          l_avail_to_reserve2
7303                                        );
7304                 END IF ;
7305 
7306 	     g_atp_tbl(K).sub_on_hand_qty_orduom          := l_sub_on_hand_ord_qty;
7307              g_atp_tbl(K).sub_avail_to_reserve_orduom     := l_sub_avail_to_reserve_ord;
7308              g_atp_tbl(K).sub_primary_uom                 := l_primary_uom_code;
7309 
7310 	     IF l_debug_level  > 0 THEN
7311                  oe_debug_pub.add(  'l_sub_on_hand_ord_qty: ' || l_sub_on_hand_ord_qty ) ;
7312                  oe_debug_pub.add(  'l_sub_avail_to_reserve_ord: ' || l_sub_avail_to_reserve_ord ) ;
7313                  oe_debug_pub.add(  'l_primary_uom_code: ' || l_primary_uom_code ) ;
7314              END IF;
7315 
7316 --ER#14763609 End
7317              Query_Qty_Tree
7318                   (p_org_id     => g_atp_tbl(K).ship_from_org_id,
7319                    p_item_id    => g_atp_tbl(K).request_item_id,
7320                    p_line_id    => g_atp_tbl(K).line_id,
7321                    p_sch_date   => g_atp_tbl(K).req_item_available_date,
7322                    x_on_hand_qty      => l_on_hand_qty,
7323                    x_avail_to_reserve => l_avail_to_reserve,
7324                    x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
7325                    x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
7326                    );
7327              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
7328              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
7329              IF l_debug_level  > 0 THEN
7330                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
7331                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
7332              END IF;
7333 
7334 
7335 --ER#14763609 Start
7336          IF l_debug_level  > 0 THEN
7337             oe_debug_pub.add(  '1.1 Fetching primary and secondary UOM' , 1 ) ;
7338          END IF;
7339 
7340 	 SELECT secondary_uom_code ,primary_uom_code
7341           INTO l_secondary_uom_code ,l_primary_uom_code
7342           FROM mtl_system_items_b
7343           WHERE inventory_item_id = g_atp_tbl(K).request_item_id
7344           AND organization_id   = g_atp_tbl(K).ship_from_org_id
7345           AND  ROWNUM=1;
7346          IF l_debug_level  > 0 THEN
7347             oe_debug_pub.add(  '1.1 l_primary_uom_code / l_secondary_uom_code ' || l_primary_uom_code || ' / ' || l_secondary_uom_code , 1 ) ;
7348          END IF;
7349 
7350              IF l_fulfillment_base ='P' THEN
7351                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7352                                      (
7353                                      g_atp_tbl(K).request_item_id,
7354                                      l_primary_uom_code,
7355                                      g_atp_tbl(K).order_quantity_uom,
7356                                      l_on_hand_qty
7357                                      );
7358 
7359                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7360                                          (
7361                                          g_atp_tbl(K).request_item_id,
7362                                          l_primary_uom_code,
7363                                          g_atp_tbl(K).order_quantity_uom,
7364                                          l_avail_to_reserve
7365                                        );
7366                 ELSE
7367                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
7368                                      (
7369                                      g_atp_tbl(K).request_item_id,
7370                                      l_secondary_uom_code,
7371                                      g_atp_tbl(K).order_quantity_uom,
7372                                      l_on_hand_qty2
7373                                      );
7374 
7375                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
7376                                          (
7377                                          g_atp_tbl(K).request_item_id,
7378                                          l_secondary_uom_code,
7379                                          g_atp_tbl(K).order_quantity_uom,
7380                                          l_avail_to_reserve2
7381                                        );
7382                 END IF ;
7383 
7384 	     g_atp_tbl(K).on_hand_qty_orduom          := l_on_hand_ord_qty;
7385              g_atp_tbl(K).available_to_reserve_orduom := l_avail_to_reserve_ord;
7386              g_atp_tbl(K).primary_uom                 := l_primary_uom_code;
7387 
7388 	     IF l_debug_level  > 0 THEN
7389                  oe_debug_pub.add(  'l_on_hand_ord_qty: ' || l_on_hand_ord_qty ) ;
7390                  oe_debug_pub.add(  'l_avail_to_reserve_ord: ' || l_avail_to_reserve_ord ) ;
7391                  oe_debug_pub.add(  'l_primary_uom_code: ' || l_primary_uom_code ) ;
7392              END IF;
7393 
7394 --ER#14763609 End
7395 	 END IF; -- g_atp_tbl(K).substitute_flag = 'N'
7396         -- END IF; -- INVCONV
7397         -- end fabdi
7398 
7399      END LOOP;
7400 
7401     IF l_debug_level  > 0 THEN
7402         oe_debug_pub.add(  'EXITING ACTION ATP' || X_RETURN_STATUS , 1 ) ;
7403     END IF;
7404 
7405 EXCEPTION
7406    WHEN FND_API.G_EXC_ERROR THEN
7407 
7408         x_return_status := FND_API.G_RET_STS_ERROR;
7409         RAISE FND_API.G_EXC_ERROR;
7410    WHEN OTHERS THEN
7411 
7412         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7413 
7414         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7415         THEN
7416             OE_MSG_PUB.Add_Exc_Msg
7417             (   G_PKG_NAME,
7418               'Action_ATP');
7419         END IF;
7420         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7421 END Action_ATP;
7422 /*---------------------------------------------------------------------
7423 Procedure Name : Action_Schedule
7424 Description    : This procedure is called from Process_Request proecudure
7425                  to perform the action of SCHEDULE or RESERVE on the line.
7426 --------------------------------------------------------------------- */
7427 Procedure Action_Schedule
7428 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
7429  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
7430  p_sch_action     IN  VARCHAR2,
7431  p_qty_to_reserve IN  NUMBER := Null,
7432  x_return_status OUT NOCOPY VARCHAR2)
7433 
7434 IS
7435 l_msg_count               NUMBER;
7436 --l_session_id              NUMBER := 0;
7437 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
7438 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
7439 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
7440 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
7441 l_mrp_msg_data            VARCHAR2(200);
7442 l_old_reserved_quantity   NUMBER;
7443 l_qty_to_reserve          NUMBER;
7444 
7445 l_old_reserved_quantity2   NUMBER; -- INVCONV
7446 l_qty2_to_reserve          NUMBER := null;  -- INVCONV
7447 
7448 l_p_qty2_to_reserve          NUMBER := null;  -- INVCONV
7449 
7450 --
7451 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7452 --
7453 BEGIN
7454  IF l_debug_level  > 0 THEN
7455      oe_debug_pub.add(  'ENTERING ACTION SCHEDULE' , 1 ) ;
7456  END IF;
7457 
7458 l_p_qty2_to_reserve := p_x_line_rec.reserved_quantity2; -- Bug#8501046
7459 
7460   IF (p_x_line_rec.schedule_status_code is null)
7461   THEN
7462 
7463     -- The line is not scheduled, so go ahead and schedule the line
7464     -- Create MRP record from the line record with the action of schedule
7465     -- The result of the request should be in x_request_rec
7466 
7467     -- Setting the action to schedule since first we need to schedule the line.
7468     -- Will reset the action to what is actually was after calling MRP.
7469 
7470     IF l_debug_level  > 0 THEN
7471         oe_debug_pub.add(  'SCHEDULING FROM ACTION SCHEDULE' , 1 ) ;
7472     END IF;
7473     p_x_line_rec.schedule_action_code := OESCH_ACT_SCHEDULE;
7474 
7475 /*
7476     Load_MRP_Request_from_rec
7477           ( p_line_rec              => p_x_line_rec
7478           , p_old_line_rec          => p_old_line_rec
7479           , x_mrp_atp_rec               => l_mrp_atp_rec);
7480 
7481    oe_debug_pub.add('Action Schedule Count is ' ||
7482                        l_mrp_atp_rec.error_code.count,1);
7483 
7484    -- We are adding this so that we will not call MRP when
7485    -- table count is 0.
7486 
7487    IF l_mrp_atp_rec.error_code.count > 0 THEN
7488 
7489 
7490     l_session_id := Get_Session_Id;
7491 
7492    IF l_debug_level > 0 THEn
7493     oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,0.5);   -- debug level changed to 0.5 for bug 13435459
7494    END IF;
7495 
7496     MRP_ATP_PUB.Call_ATP
7497             (  p_session_id             =>  l_session_id
7498              , p_atp_rec                =>  l_mrp_atp_rec
7499              , x_atp_rec                =>  l_mrp_atp_rec
7500              , x_atp_supply_demand      =>  l_atp_supply_demand
7501              , x_atp_period             =>  l_atp_period
7502              , x_atp_details            =>  l_atp_details
7503              , x_return_status          =>  x_return_status
7504              , x_msg_data               =>  l_mrp_msg_data
7505              , x_msg_count              =>  l_msg_count);
7506 
7507    IF l_debug_level>0 THEN
7508      oe_debug_pub.add('1. After Calling MRP_ATP_PUB.Call_ATP' ||
7509                                               x_return_status,0.5);  -- debug level changed to 0.5 for bug 13435459
7510    END IF;
7511     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7512             oe_debug_pub.add('Error is' || l_mrp_msg_data,1);
7513             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7514     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7515           --Display_Sch_Errors;
7516           Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
7517                              p_line_id => p_x_line_rec.line_id);
7518           RAISE FND_API.G_EXC_ERROR;
7519     END IF;
7520 
7521     Load_Results_from_rec
7522                 (p_atp_rec         => l_mrp_atp_rec,
7523                  p_x_line_rec      => p_x_line_rec,
7524                  x_return_status   => x_return_status);
7525 
7526     END IF; -- Check for MRP count.
7527 */
7528 
7529 
7530 
7531     Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
7532                 p_old_line_rec  => p_old_line_rec,
7533                 x_return_status => x_return_status);
7534 
7535     IF l_debug_level  > 0 THEN
7536         oe_debug_pub.add(  'X_RETURN_STATUS ' || X_RETURN_STATUS , 1 ) ;
7537     END IF;
7538     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7539             IF l_debug_level  > 0 THEN
7540                 oe_debug_pub.add(  'UNEXPECTED ERROR FROM ' , 1 ) ;
7541             END IF;
7542             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7543     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7544             IF l_debug_level  > 0 THEN
7545                 oe_debug_pub.add(  'RR: L2' , 1 ) ;
7546             END IF;
7547             RAISE FND_API.G_EXC_ERROR;
7548     END IF;
7549 
7550     -- Reloading p_x_line_rec from l_out_line_tbl since the record
7551     -- in the table is the one which is demanded.
7552 
7553     IF l_debug_level  > 0 THEN
7554         oe_debug_pub.add(  'RR: L3' , 1 ) ;
7555     END IF;
7556     p_x_line_rec.schedule_action_code := p_sch_action;
7557 
7558   END IF; /* If schedule_status_code is null */
7559 
7560   IF l_debug_level  > 0 THEN
7561       oe_debug_pub.add(  'RR: L4' , 1 ) ;
7562   END IF;
7563   IF l_debug_level  > 0 THEN
7564       oe_debug_pub.add(  'SCH_CACHED_SCH_LEVEL_CODE ' || SCH_CACHED_SCH_LEVEL_CODE , 1 ) ;
7565   END IF;
7566 
7567   IF ((p_sch_action = OESCH_ACT_RESERVE)
7568       OR (p_sch_action = OESCH_ACT_SCHEDULE AND
7569          (sch_cached_sch_level_code = SCH_LEVEL_THREE OR
7570           -- BUG 1955004
7571           sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
7572           -- END 1955004
7573           sch_cached_sch_level_code is null) AND
7574           Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id)) OR --4689197
7575      (p_qty_to_reserve is not null))
7576 	 AND (NOT G_GOP_AUTO_SPLIT  OR G_GOP_AUTO_SPLIT_LINE_ID <> P_X_LINE_REC.LINE_ID ) --ER 2736896. Do not reserve here.
7577   THEN
7578 
7579     IF l_debug_level  > 0 THEN
7580         oe_debug_pub.add(  'SCHEDULING IN ACTION_SCHEDULE' , 1 ) ;
7581     END IF;
7582 
7583     -- Assigning reserved_quantity to 0 if MISS_NUM, to fix the bug 1384831
7584 
7585     l_old_reserved_quantity := p_old_line_rec.reserved_quantity;
7586     l_old_reserved_quantity2 := nvl(p_old_line_rec.reserved_quantity2,0); -- INVCONV
7587 
7588     IF p_old_line_rec.reserved_quantity = FND_API.G_MISS_NUM THEN
7589 
7590        l_old_reserved_quantity := 0;
7591 
7592     END IF;
7593         IF p_old_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM THEN -- INVCONV
7594        l_old_reserved_quantity2 := 0;
7595     END IF;
7596 
7597     IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
7598     THEN
7599       -- Create INV record from the line to reserve
7600 
7601       IF p_qty_to_reserve is null THEN
7602          l_qty_to_reserve := p_x_line_rec.ordered_quantity -
7603                            nvl(l_old_reserved_quantity,0);
7604 
7605 
7606       ELSE
7607          l_qty_to_reserve := p_qty_to_reserve;
7608       END IF;
7609 
7610       IF l_p_qty2_to_reserve is null or
7611              l_p_qty2_to_reserve = FND_API.G_MISS_NUM THEN -- INVCONV
7612       -- KYH BUG 4245418 BEGIN
7613       -- =====================
7614       -- l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0)  -      -- need to test if this falls over
7615       --                   nvl(l_old_reserved_quantity2,0);
7616       -- It is dangerous to compute secondary quantity to reserve based on
7617       -- ordered_quantity2 minus reserved_quantity2 as above.
7618       -- This is because ordered_quantity 2 always reflects a standard conversion from ordered_quantity
7619       -- whereas reserved_quantity2 may be the result of one or more lot specific calculations
7620       -- Combining values from these different conversion rates may not give the correct result.
7621       -- Better to compute the secondary to reserve by converting l_qty_to_reserve
7622          IF p_x_line_rec.ordered_quantity_uom2 is not null and
7623                         p_x_line_rec.ordered_quantity_uom2 <> FND_API.G_MISS_CHAR THEN
7624            -- Only invoke the conversion for dual tracked items
7625            --Bug#8501046,Checked the scenario using default and non-default both types for lot-specific conversion as well and the formula works fine.
7626  	            l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0)- nvl(l_old_reserved_quantity2,0);       --bug 7675494
7627  	            /*IF l_debug_level  > 0 THEN
7628              oe_debug_pub.add(  'DUAL Tracked quantity so convert the qty to reserve ' || l_qty_to_reserve , 1 ) ;
7629            END IF;
7630            l_qty2_to_reserve    := inv_convert.inv_um_convert(
7631                                    item_id                      => p_x_line_rec.inventory_item_id
7632                                  , lot_number                   => NULL
7633                                  , organization_id              => p_x_line_rec.ship_from_org_id
7634                                  , PRECISION                    => 5
7635                                  , from_quantity                => l_qty_to_reserve
7636                                  , from_unit                    => p_x_line_rec.order_quantity_uom
7637                                  , to_unit                      => p_x_line_rec.ordered_quantity_uom2
7638                                  , from_name                    => NULL -- from uom name
7639                                  , to_name                      => NULL -- to uom name
7640                                  );
7641            IF l_debug_level  > 0 THEN
7642              oe_debug_pub.add(  'After UOM conversion the secondary to reserve is  ' || l_qty2_to_reserve , 1 ) ;
7643            END IF;
7644 
7645            IF l_qty2_to_reserve = -99999 THEN
7646              -- conversion failed
7647              FND_MESSAGE.SET_NAME('INV','INV_NO_CONVERSION_ERR'); -- INVCONV
7648              OE_MSG_PUB.ADD;
7649              IF l_debug_level  > 0 THEN
7650                oe_debug_pub.add(  'ERROR on UOM conversion to Secondary UOM which is '||p_x_line_rec.ordered_quantity_uom2 , 1 ) ;
7651              END IF;
7652              RAISE FND_API.G_EXC_ERROR;
7653            END IF; */
7654          END IF;
7655       -- KYH BUG 4245418 END
7656       ELSE
7657          l_qty2_to_reserve := l_p_qty2_to_reserve;
7658       END IF;
7659       IF l_qty2_to_reserve = 0 -- INVCONV
7660          THEN
7661                     l_qty2_to_reserve := NULL;
7662       END IF;
7663 
7664 
7665 --      IF l_qty_to_reserve > 0 THEN-- bug 16782621
7666     IF (l_qty_to_reserve > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')-- bug 16782621
7667       OR (l_qty2_to_reserve > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
7668     THEN
7669 
7670 
7671          -- Since we are calling schedule line from post write.
7672          -- line is posted to db. we do not need to skip the code.
7673      --    IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE THEN
7674             -- We cannot create a reservation currently if the line is
7675             -- being created (since the lines is yet not in the database,
7676             -- and there is a validation done with this line id when we
7677             -- call INV API.). We will populate the reserved quantity on
7678             -- the line record, and in the post-write procedure (in OEXULINB),
7679             -- we will perform the reservation.
7680 
7681             --l_out_line_rec.schedule_status_code := OESCH_STATUS_SCHEDULED;
7682       --        p_x_line_rec.reserved_quantity    := l_qty_to_reserve;
7683 
7684        --  ELSE
7685              IF l_debug_level  > 0 THEN
7686            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
7687                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
7688        END IF;
7689 
7690 
7691              Reserve_Line
7692               ( p_line_rec              => p_x_line_rec
7693               , p_quantity_to_reserve   => l_qty_to_reserve
7694               , p_quantity2_to_reserve   => l_qty2_to_reserve -- INVCONV
7695               , x_return_status         => x_return_status);
7696 
7697         -- END IF; -- Operation on the line is create or not
7698 
7699       ELSE
7700 
7701          p_x_line_rec.reserved_quantity :=
7702                                   p_old_line_rec.reserved_quantity;
7703 
7704            p_x_line_rec.reserved_quantity2 :=
7705                                   p_old_line_rec.reserved_quantity2; -- INVCONV
7706 
7707            IF l_debug_level  > 0 THEN
7708                 oe_debug_pub.add(  'p_old_line_rec.reserved_quantity ' || p_old_line_rec.reserved_quantity , 2 ) ;
7709                      oe_debug_pub.add(  'p_old_line_rec.reserved_quantity2 ' || p_old_line_rec.reserved_quantity2 , 2 ) ;
7710        END IF;
7711 
7712       END IF; -- l_qty_to_reserve
7713     END IF; -- If shippable Flag = Y
7714 
7715   END IF; /* If reservation needs to be performed */
7716 
7717 
7718     IF l_debug_level  > 0 THEN
7719         oe_debug_pub.add(  'EXITING ACTION SCHEDULE: ' || X_RETURN_STATUS , 1 ) ;
7720     END IF;
7721 
7722 EXCEPTION
7723    WHEN FND_API.G_EXC_ERROR THEN
7724 
7725         x_return_status := FND_API.G_RET_STS_ERROR;
7726 
7727    WHEN OTHERS THEN
7728 
7729         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7730 
7731         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7732         THEN
7733             OE_MSG_PUB.Add_Exc_Msg
7734             (   G_PKG_NAME,
7735               'Action_Schedule');
7736         END IF;
7737 
7738 END Action_Schedule;
7739 /*---------------------------------------------------------------------
7740 Procedure Name : Action_UnSchedule
7741 Description    : This procedure is called from Process_Request proecudure
7742                  to perform the action of UNSCHEDULE or UNRESERVE on the line.
7743 --------------------------------------------------------------------- */
7744 
7745 Procedure Action_UnSchedule
7746 (p_old_line_rec  IN  OE_ORDER_PUB.line_rec_type,
7747  p_sch_action    IN  VARCHAR2,
7748  p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
7749 x_return_status OUT NOCOPY VARCHAR2)
7750 
7751 IS
7752 
7753 l_msg_count               NUMBER;
7754 --l_msg_data                VARCHAR2(2000);
7755 --l_session_id              NUMBER := 0;
7756 --l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
7757 --l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
7758 --l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
7759 --l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
7760 l_mrp_msg_data            VARCHAR2(200);
7761 l_qty_to_unreserve        NUMBER;
7762 l_qty2_to_unreserve        NUMBER; -- INVCONV
7763 --
7764 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7765 --
7766 BEGIN
7767 
7768      IF l_debug_level  > 0 THEN
7769          oe_debug_pub.add(  'ENTERING ACTION_UNSCHEDULE ' , 1 ) ;
7770      END IF;
7771 
7772      -- shipping_interfaced_flag
7773      IF (p_old_line_rec.reserved_quantity is not null AND
7774          p_old_line_rec.reserved_quantity <> FND_API.G_MISS_NUM)
7775      THEN
7776 
7777         -- Call INV API to delete the reservations on  the line.
7778 
7779         l_qty_to_unreserve := p_old_line_rec.reserved_quantity;
7780         l_qty2_to_unreserve := p_old_line_rec.reserved_quantity2; -- INCONV
7781         IF l_qty2_to_unreserve = 0 -- INVCONV
7782          THEN
7783           l_qty2_to_unreserve := NULL;
7784         END IF;
7785 
7786         Unreserve_Line
7787             ( p_line_rec               => p_old_line_rec
7788             , p_quantity_to_unreserve  => l_qty_to_unreserve
7789             , p_quantity2_to_unreserve  => l_qty2_to_unreserve
7790             , x_return_status          => x_return_status);
7791 
7792         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7793              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7794         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7795              RAISE FND_API.G_EXC_ERROR;
7796         END IF;
7797 
7798      END IF;
7799 
7800      -- If the action was unreserve, we do not need to unschedule the line.
7801      -- Thus we will check for this condition before unscheduling.
7802 
7803      IF p_sch_action <> OESCH_ACT_UNRESERVE THEN
7804 
7805        -- Create MRP record with action of UNDEMAND.
7806 
7807        p_x_line_rec.schedule_action_code := OESCH_ACT_UNDEMAND;
7808 
7809  /*
7810       Load_MRP_Request_from_rec
7811           ( p_line_rec              => p_x_line_rec
7812           , p_old_line_rec          => p_old_line_rec
7813           , x_mrp_atp_rec             => l_mrp_atp_rec);
7814 
7815       IF l_mrp_atp_rec.error_code.count > 0 THEN
7816 
7817         l_session_id := Get_Session_Id;
7818 
7819         -- Call ATP
7820        IF l_debug_level > 0 THEn
7821         oe_debug_pub.add('1. Calling MRP API with session id '||l_session_id,0.5);  -- debug level changed to 0.5 for bug 13435459
7822        END IF;
7823 
7824         MRP_ATP_PUB.Call_ATP
7825           ( p_session_id             =>  l_session_id
7826           , p_atp_rec                =>  l_mrp_atp_rec
7827           , x_atp_rec                =>  l_mrp_atp_rec
7828           , x_atp_supply_demand      =>  l_atp_supply_demand
7829           , x_atp_period             =>  l_atp_period
7830           , x_atp_details            =>  l_atp_details
7831           , x_return_status          =>  x_return_status
7832           , x_msg_data               =>  l_mrp_msg_data
7833           , x_msg_count              =>  l_msg_count);
7834        IF l_debug_level > 0 THEN
7835          oe_debug_pub.add('2. After Calling MRP_ATP_PUB.Call_ATP' ||
7836                                               x_return_status,0.5);   -- debug level changed to 0.5 for bug 13435459
7837        END IF;
7838 
7839         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7840            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7841         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7842            --Display_Sch_Errors;
7843            Display_sch_errors(p_atp_rec => l_mrp_atp_rec,
7844                               p_line_id => p_x_line_rec.line_id);
7845            RAISE FND_API.G_EXC_ERROR;
7846         END IF;
7847 
7848         Load_Results_from_rec
7849                     (p_atp_rec         => l_mrp_atp_rec,
7850                      p_x_line_rec      => p_x_line_rec,
7851                      x_return_status   => x_return_status);
7852 
7853         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7854                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7855         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7856                 RAISE FND_API.G_EXC_ERROR;
7857         END IF;
7858 
7859       END IF; -- MRP Count.
7860 
7861  */
7862 
7863 
7864         Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
7865                      p_old_line_rec  => p_old_line_rec,
7866                      x_return_status => x_return_status);
7867 
7868        IF l_debug_level  > 0 THEN
7869            oe_debug_pub.add(  'AFTER CALLING MRP API: ' || X_RETURN_STATUS , 2 ) ;
7870        END IF;
7871 
7872         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7873                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7874         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
7875                 RAISE FND_API.G_EXC_ERROR;
7876         END IF;
7877 
7878     END IF;
7879 
7880     -- If the action performed is UNSCHEDULE, then the reserved quantity is now     -- null.
7881     p_x_line_rec.reserved_quantity    := null;
7882     p_x_line_rec.reserved_quantity2   := null; -- INVCONV
7883 
7884 
7885   IF l_debug_level  > 0 THEN
7886       oe_debug_pub.add(  'EXITING ACTION_UNSCHEDULE ' , 1 ) ;
7887   END IF;
7888 EXCEPTION
7889    WHEN FND_API.G_EXC_ERROR THEN
7890 
7891         x_return_status := FND_API.G_RET_STS_ERROR;
7892 
7893    WHEN OTHERS THEN
7894 
7895         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7896 
7897         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7898         THEN
7899             OE_MSG_PUB.Add_Exc_Msg
7900             (   G_PKG_NAME,
7901               'Action_UnSchedule');
7902         END IF;
7903 
7904 END Action_UnSchedule;
7905 
7906 /*---------------------------------------------------------------------
7907 Procedure Name : Action_Undemand
7908 Description    : This procedure is called from SCHEDULE LINE proecudure
7909                  to perform the UNDEMAD on the line when an item is changed.
7910 --------------------------------------------------------------------- */
7911 
7912 Procedure Action_Undemand
7913 (p_old_line_rec  IN  OE_ORDER_PUB.line_rec_type,
7914 x_return_status OUT NOCOPY VARCHAR2)
7915 
7916 
7917 IS
7918 l_msg_count               NUMBER;
7919 l_msg_data                VARCHAR2(2000);
7920 l_session_id              NUMBER := 0;
7921 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
7922 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
7923 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
7924 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
7925 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
7926 -- Bug 1955004
7927 l_scheduling_level_code   VARCHAR2(30);
7928 
7929 --
7930 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
7931 --
7932 BEGIN
7933 
7934       IF l_debug_level  > 0 THEN
7935           oe_debug_pub.add(  'ENTERING ACTION_UNDEMAND' , 1 ) ;
7936       END IF;
7937 
7938    --BUG 1955004
7939    IF l_debug_level  > 0 THEN
7940        oe_debug_pub.add(  'CHECKING SCHEDULING LEVEL...' , 1 ) ;
7941    END IF;
7942    l_scheduling_level_code := Get_Scheduling_Level(p_old_line_rec.header_id,
7943                                                    p_old_line_rec.line_type_id);
7944 
7945    IF l_debug_level  > 0 THEN
7946        oe_debug_pub.add(  'L_SCHEDULING_LEVEL_CODE : ' || L_SCHEDULING_LEVEL_CODE , 1 ) ;
7947    END IF;
7948    -- 3763015
7949    IF (l_scheduling_level_code = SCH_LEVEL_FOUR OR
7950     l_scheduling_level_code = SCH_LEVEL_FIVE OR
7951     NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y') OR
7952     Nvl(p_old_line_rec.bypass_sch_flag, 'N') = 'Y' THEN -- DOO Integration
7953     -- this is an inactive demand line.
7954 
7955       IF l_debug_level  > 0 THEN
7956           oe_debug_pub.add(  'IT IS AN INACTIVE DEMAND LINE' , 1 ) ;
7957       END IF;
7958       NULL;
7959    ELSE
7960    --END 1955004
7961 
7962       IF l_debug_level  > 0 THEN
7963           oe_debug_pub.add(  'STD ACTION_UNDEMAND ACTION' , 1 ) ;
7964       END IF;
7965 
7966       -- Create MRP record with action of UNDEMAND.
7967 
7968       Load_MRP_Request_from_rec
7969           ( p_line_rec      => p_old_line_rec
7970           , p_old_line_rec  => p_old_line_rec
7971           , p_sch_action    => OESCH_ACT_UNDEMAND
7972           , x_mrp_atp_rec   => l_mrp_atp_rec);
7973 
7974       IF l_mrp_atp_rec.error_code.count > 0 THEN
7975 
7976         l_session_id := Get_Session_Id;
7977 
7978         -- Call ATP
7979 
7980         IF l_debug_level  > 0 THEN
7981             oe_debug_pub.add(  '4. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 1 ) ;
7982         END IF;
7983 
7984         MRP_ATP_PUB.Call_ATP
7985           ( p_session_id             =>  l_session_id
7986           , p_atp_rec                =>  l_mrp_atp_rec
7987           , x_atp_rec                =>  l_out_mrp_atp_rec
7988           , x_atp_supply_demand      =>  l_atp_supply_demand
7989           , x_atp_period             =>  l_atp_period
7990           , x_atp_details            =>  l_atp_details
7991           , x_return_status          =>  x_return_status
7992           , x_msg_data               =>  l_msg_data
7993           , x_msg_count              =>  l_msg_count);
7994 
7995                                               IF l_debug_level  > 0 THEN
7996                                                   oe_debug_pub.add(  '4. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 1 ) ;
7997                                               END IF;
7998 
7999         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8000            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8001         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8002            --Display_Sch_Errors;
8003            Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
8004                               p_line_id => p_old_line_rec.line_id);
8005            RAISE FND_API.G_EXC_ERROR;
8006         END IF;
8007       END IF; -- MRP count.
8008      END IF;  -- For new IF statement for 1955004
8009 
8010       IF l_debug_level  > 0 THEN
8011           oe_debug_pub.add(  'EXITING ACTION_UNDEMAND' , 1 ) ;
8012       END IF;
8013 
8014 EXCEPTION
8015 
8016    WHEN FND_API.G_EXC_ERROR THEN
8017 
8018         x_return_status := FND_API.G_RET_STS_ERROR;
8019 
8020    WHEN OTHERS THEN
8021 
8022         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8023 
8024         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8025         THEN
8026             OE_MSG_PUB.Add_Exc_Msg
8027             (   G_PKG_NAME,
8028               'Action_Undemand');
8029         END IF;
8030 
8031 END Action_Undemand;
8032 
8033 /*---------------------------------------------------------------------
8034 Procedure Name : Action_Reserve
8035 Description    : This procedure is called from Process_Request proecudure
8036 --------------------------------------------------------------------- */
8037 Procedure Action_Reserve
8038 (p_x_line_rec     IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
8039  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
8040 x_return_status OUT NOCOPY VARCHAR2)
8041 
8042 IS
8043 l_old_reserved_qty     NUMBER :=nvl(p_old_line_rec.reserved_quantity,0);
8044 l_changed_reserved_qty NUMBER;
8045 l_qty_to_reserve       NUMBER;
8046 --
8047 l_old_reserved_qty2     NUMBER :=nvl(p_old_line_rec.reserved_quantity2,0); -- INVCONV
8048 l_changed_reserved_qty2 NUMBER; -- INVCONV
8049 l_qty2_to_reserve       NUMBER; -- INVCONV
8050 
8051 
8052 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
8053 --
8054 BEGIN
8055 
8056  IF l_debug_level  > 0 THEN
8057      oe_debug_pub.add(  'ENTERING ACTION RESERVE ' ) ;
8058  END IF;
8059  x_return_status := FND_API.G_RET_STS_SUCCESS;
8060 
8061  -- When only subinventory changed on the line. Need Scheduling
8062  -- Will pass line action as reserve. We will unreserve and reserve
8063  -- the line for new subinventory.
8064 
8065  -- shipping_interfaced_flag.
8066   -- 4653097
8067  IF (NOT OE_GLOBALS.Equal(p_x_line_rec.subinventory,
8068                          p_old_line_rec.subinventory)
8069    OR NOT OE_GLOBALS.Equal(p_x_line_rec.project_id,
8070                          p_old_line_rec.project_id)
8071    OR NOT OE_GLOBALS.Equal(p_x_line_rec.task_id,
8072                          p_old_line_rec.task_id))
8073  AND l_old_reserved_qty > 0
8074  AND NVL(p_x_line_rec.shipping_interfaced_flag,'N') = 'N'
8075  THEN
8076 
8077     IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
8078        then
8079      l_qty2_to_reserve := NULL;
8080 
8081     END IF;
8082 
8083     Unreserve_Line
8084     (p_line_rec              => p_old_line_rec,
8085      p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
8086      p_quantity2_to_unreserve => l_qty2_to_reserve, -- INVCONV
8087      x_return_status         => x_return_status);
8088 
8089      l_old_reserved_qty := 0;
8090      l_old_reserved_qty2 := 0; -- INVCONV
8091      -- Start Added for bug 2470416
8092      -- To set the Cascading_Request_Logged to True
8093      -- which will trigger the form to requery
8094 
8095      OE_GLOBALS.G_CASCADING_REQUEST_LOGGED :=TRUE;
8096      -- End Added for bug 2470416
8097 
8098     IF l_debug_level  > 0 THEN
8099         oe_debug_pub.add(  'AFTER DELETING THE RESERVATION' , 3 ) ;
8100     END IF;
8101  END IF;
8102 
8103  IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
8104  THEN
8105     -- 3239619 / 3362461 / 3456052 Convert the old reserve qty as per new UOM if not same.
8106     IF NOT OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom)
8107       AND l_old_reserved_qty > 0 THEN
8108     l_old_reserved_qty := INV_CONVERT.INV_UM_CONVERT(item_id   =>p_old_line_rec.inventory_item_id,
8109                                       precision    =>5,
8110                                       from_quantity=>l_old_reserved_qty,
8111                                       from_unit    => p_old_line_rec.order_quantity_uom,
8112                                       to_unit      => p_x_line_rec.order_quantity_uom,
8113                                       from_name    => NULL,
8114                                       to_name      => NULL
8115                                       );
8116     END IF;
8117     l_changed_reserved_qty   := l_old_reserved_qty -
8118                                 nvl(p_x_line_rec.reserved_quantity,0);
8119     l_changed_reserved_qty2   := nvl(l_old_reserved_qty2, 0)  -
8120                                 nvl(p_x_line_rec.reserved_quantity2,0); -- INVCONV
8121     IF l_changed_reserved_qty2 = 0 -- INVCONV
8122      THEN
8123         l_changed_reserved_qty2 := NULL;
8124     END IF;
8125 
8126     -- shipping_interfaced_flag
8127   --  IF l_changed_reserved_qty > 0 THEN--modified for bug 16782621
8128     IF (l_changed_reserved_qty > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')-- bug 16782621
8129       OR (l_changed_reserved_qty2 > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8130     THEN
8131        IF l_debug_level  > 0 THEN
8132            oe_debug_pub.add(  'RESERVED QUANTITY HAS DECREASED' , 1 ) ;
8133        END IF;
8134 
8135        -- No need to pass old record. Since this is a change
8136        -- due to quantity.
8137        -- Start 2595661
8138        IF (nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'Y'
8139            AND NOT Get_Pick_Status(p_x_line_rec.line_id) ) THEN
8140 
8141           Do_Unreserve
8142                 ( p_line_rec               => p_x_line_rec
8143                 , p_quantity_to_unreserve  => l_changed_reserved_qty
8144                 , p_quantity2_to_unreserve  => l_changed_reserved_qty2 -- INVCONV
8145                 , p_old_ship_from_org_id    => p_old_line_rec.ship_from_org_id --5024936
8146                 , x_return_status          => x_return_status);
8147        ELSE
8148        -- End 2595661
8149 
8150           Unreserve_Line
8151                ( p_line_rec              => p_x_line_rec
8152                , p_quantity_to_unreserve => l_changed_reserved_qty
8153                , p_quantity2_to_unreserve => l_changed_reserved_qty2 -- INVCONV
8154                , x_return_status         => x_return_status);
8155        END IF;  --2595661
8156 
8157        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8158           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8159        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8160           RAISE FND_API.G_EXC_ERROR;
8161        END IF;
8162 
8163 
8164  --   ELSIF l_changed_reserved_qty < 0 THEN  --modfied for ---- bug 16782621 issue1
8165     ELSIF (l_changed_reserved_qty < 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8166       OR (l_changed_reserved_qty2 < 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8167     THEN
8168        IF l_debug_level  > 0 THEN
8169            oe_debug_pub.add(  'RESERVED QUANTITY HAS INCREASED' , 1 ) ;
8170        END IF;
8171 
8172        -- Make sure reservation qty should not be more than
8173        -- Ordered qty.
8174 
8175        IF p_x_line_rec.reserved_quantity > p_x_line_rec.ordered_quantity
8176        THEN
8177 
8178           l_qty_to_reserve := p_x_line_rec.ordered_quantity -
8179                               l_old_reserved_qty;
8180 
8181        ELSE
8182 
8183           l_qty_to_reserve := p_x_line_rec.reserved_quantity -
8184                              l_old_reserved_qty;
8185 
8186        END IF;
8187        IF p_x_line_rec.reserved_quantity2 > p_x_line_rec.ordered_quantity2 -- INVCONV
8188        THEN
8189 
8190           l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0) -
8191                               nvl(l_old_reserved_qty2, 0);
8192 
8193        ELSE
8194 
8195           l_qty2_to_reserve := nvl(p_x_line_rec.reserved_quantity2, 0) -
8196                              nvl(l_old_reserved_qty2, 0) ;
8197 
8198        END IF;
8199        IF l_qty2_to_reserve = 0 -- INVCONV
8200          THEN
8201           l_qty2_to_reserve := null;
8202        end if;
8203 
8204 
8205        IF l_debug_level  > 0 THEN
8206            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
8207            oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
8208        END IF;
8209 
8210 --       IF l_qty_to_reserve > 0 -- bug 16782621
8211         IF ( l_qty_to_reserve > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')-- bug 16782621
8212          OR ( l_qty2_to_reserve > 0 AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8213        THEN
8214 
8215 
8216 
8217 
8218           Reserve_Line
8219           ( p_line_rec             => p_x_line_rec
8220           , p_quantity_to_reserve  => l_qty_to_reserve
8221           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
8222           , p_rsv_update           => TRUE  -- Going to increase reservation
8223           , x_return_Status        => x_return_status);
8224 
8225        END IF;
8226 
8227        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8228           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8229        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8230           RAISE FND_API.G_EXC_ERROR;
8231        END IF;
8232 
8233     END IF; -- end of reserved_quantity change code
8234 
8235  END IF; -- Check for shippable flag.
8236  IF l_debug_level  > 0 THEN
8237      oe_debug_pub.add(  'EXITING ACTION RESERVE ' ) ;
8238  END IF;
8239 
8240 EXCEPTION
8241    WHEN FND_API.G_EXC_ERROR THEN
8242 
8243         x_return_status := FND_API.G_RET_STS_ERROR;
8244 
8245    WHEN OTHERS THEN
8246 
8247         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8248 
8249         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8250         THEN
8251             OE_MSG_PUB.Add_Exc_Msg
8252             (   G_PKG_NAME,
8253               'Action_Reserve');
8254         END IF;
8255 END Action_Reserve;
8256 
8257 /*---------------------------------------------------------------------
8258 Procedure Name : Action_Reschedule
8259 Description    : This procedure is called from Process_Request proecudure
8260 --------------------------------------------------------------------- */
8261 Procedure Action_Reschedule
8262 (p_x_line_rec     IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
8263  p_old_line_rec   IN  OE_ORDER_PUB.line_rec_type,
8264 x_return_status OUT NOCOPY VARCHAR2,
8265 
8266 x_reserve_later OUT NOCOPY VARCHAR2)
8267 
8268 IS
8269 l_re_reserve_flag      VARCHAR2(1) := 'Y';
8270 l_old_reserved_qty     NUMBER := nvl(p_old_line_rec.reserved_quantity,0);
8271 l_qty_to_reserve       NUMBER;
8272 l_changed_reserved_qty NUMBER;
8273 l_qty_to_unreserve     NUMBER;
8274 -- INVCONV
8275 l_old_reserved_qty2     NUMBER := nvl(p_old_line_rec.reserved_quantity2,0);
8276 l_qty2_to_reserve       NUMBER;
8277 l_changed_reserved_qty2 NUMBER;
8278 l_qty2_to_unreserve     NUMBER;
8279 
8280 
8281 
8282 l_reservation_rec      inv_reservation_global.mtl_reservation_rec_type;
8283 l_rsv_tbl              inv_reservation_global.mtl_reservation_tbl_type;
8284 l_sales_order_id       NUMBER;
8285 l_x_error_code         NUMBER;
8286 l_lock_records         VARCHAR2(1);
8287 l_sort_by_req_date     NUMBER ;
8288 l_count                NUMBER;
8289 l_msg_count            NUMBER;
8290 l_msg_data             VARCHAR2(2000);
8291 l_dummy_sn             inv_reservation_global.serial_number_tbl_type;
8292 l_buffer               VARCHAR2(2000);
8293 l_rsv_update           BOOLEAN :=FALSE;
8294 --
8295 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
8296 --
8297 /*OPM BEGIN Bug 2794760
8298 ====================*/
8299 -- l_item_rec         OE_ORDER_CACHE.item_rec_type; -- OPM  -- INVCONV Not needed now because of OPM inventory convergence
8300 /*OPM End Bug 2794760
8301 ====================*/
8302 BEGIN
8303     x_return_status := FND_API.G_RET_STS_SUCCESS;
8304     x_reserve_later := 'N';
8305     IF l_debug_level  > 0 THEN
8306         oe_debug_pub.add(  'ENTERING ACTION_RESCHEDULE ' || P_X_LINE_REC.OPEN_FLAG , 1 ) ;
8307         oe_debug_pub.add(  'ENTERING ACTION_RESCHEDULE p_x_line_rec.fulfillment_base ' || P_X_LINE_REC.fulfillment_base , 1 ) ;
8308     END IF;
8309 
8310     IF (p_old_line_rec.reserved_quantity is not null)
8311     THEN
8312 
8313       -- Added this part of code to fix bug 1797109.
8314       -- Unreserve only when one of the below mentioned, attrubutes
8315       -- changes, in other case simply re-schedule the line.
8316       --During the fix l_re_reserve_flag is introduced.
8317 
8318 -- shipping_interfaced_flag
8319 --      l_re_reserve_flag := 'N';
8320       --4653097
8321       IF
8322          -- Bug 6335352
8323          -- Commenting the below line
8324          -- nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'N' AND
8325         (NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
8326                                 p_old_line_rec.inventory_item_id)
8327         OR NOT OE_GLOBALS.Equal(p_x_line_rec.subinventory,
8328                                 p_old_line_rec.subinventory)
8329         OR NOT OE_GLOBALS.Equal(p_x_line_rec.order_quantity_uom,
8330                                 p_old_line_rec.order_quantity_uom)
8331         OR NOT OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id,
8332                                 p_old_line_rec.ship_from_org_id)
8333         OR NOT OE_GLOBALS.Equal(p_x_line_rec.project_id,
8334                                 p_old_line_rec.project_id)
8335         OR NOT OE_GLOBALS.Equal(p_x_line_rec.task_id,
8336                                 p_old_line_rec.task_id)) THEN
8337 
8338  --         l_re_reserve_flag := 'Y';
8339           -- Call INV to delete the old reservations
8340           l_qty2_to_unreserve := p_old_line_rec.reserved_quantity2 ; --bug 16434168
8341           IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
8342            THEN
8343               l_qty2_to_unreserve := NULL;
8344           END IF;
8345 
8346           Unreserve_Line
8347              (p_line_rec              => p_old_line_rec,
8348               p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
8349               p_quantity2_to_unreserve => l_qty2_to_unreserve , -- INVCONV
8350               x_return_status         => x_return_status);
8351           l_old_reserved_qty := 0;
8352           l_old_reserved_qty2 := 0; -- INVCONV
8353       END IF;
8354     END IF;
8355 
8356 
8357     -- If the scheduling is happening due to inventory item change.
8358     -- We should call MRP twice. First time we should call the with
8359     -- Undemand for old item. Second call would be redemand.
8360 
8361     IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
8362                             p_old_line_rec.inventory_item_id)
8363     THEN
8364 
8365         Action_undemand(p_old_line_rec  => p_old_line_rec,
8366                         x_return_status => x_return_status);
8367 
8368         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8369            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8370         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8371            RAISE FND_API.G_EXC_ERROR;
8372         END IF;
8373     END IF;
8374 
8375 
8376      -- If the rescheduling is happening for order change then
8377      -- Undmand the line, else redemand.
8378 
8379      IF p_x_line_rec.ordered_quantity > 0 THEN
8380         p_x_line_rec.schedule_action_code := OESCH_ACT_REDEMAND;
8381      ELSE
8382         p_x_line_rec.schedule_action_code := OESCH_ACT_UNDEMAND;
8383      END IF;
8384 
8385      Call_MRP_ATP(p_x_line_rec    => p_x_line_rec,
8386                   p_old_line_rec  => p_old_line_rec,
8387                   x_return_status => x_return_status);
8388 
8389      --4161641
8390      IF l_debug_level  > 0 THEN
8391         oe_debug_pub.add(  'X_RETURN_STATUS ' || X_RETURN_STATUS , 1 ) ;
8392      END IF;
8393      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8394         IF l_debug_level  > 0 THEN
8395            oe_debug_pub.add(  'UNEXPECTED ERROR FROM MRP CALL ' , 1 ) ;
8396         END IF;
8397         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8398      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8399         IF l_debug_level  > 0 THEN
8400            oe_debug_pub.add(  'ACTION RESCHEDULE : ERROR FROM MRP CALL' , 1 ) ;
8401         END IF;
8402         RAISE FND_API.G_EXC_ERROR;
8403      END IF;
8404 
8405      -- Item has been substituted by MRP. Remove any old reservation
8406      -- on old item. Since reservation cannot be made without posting
8407      -- the new item to db postpone the reservation on new item to
8408      -- process_request.
8409 
8410      IF nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'N'
8411      AND NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
8412                               p_old_line_rec.inventory_item_id)
8413      THEN
8414 
8415        IF l_debug_level  > 0 THEN
8416            oe_debug_pub.add(  'SUB: REMOVE THE RESERVATION ON OLD LINE' , 1 ) ;
8417        END IF;
8418 
8419        IF l_old_reserved_qty > 0 THEN
8420           l_qty2_to_unreserve := p_old_line_rec.reserved_quantity2 ; --bug 16434168
8421           IF p_old_line_rec.reserved_quantity2 = 0 -- INVCONV
8422            THEN
8423               l_qty2_to_unreserve := NULL;
8424           END IF;
8425 
8426 
8427           Unreserve_Line
8428              (p_line_rec              => p_old_line_rec,
8429               p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
8430               p_quantity2_to_unreserve => l_qty2_to_unreserve, -- INVCONV
8431               x_return_status         => x_return_status);
8432 
8433           l_old_reserved_qty := 0;
8434           l_old_reserved_qty2 := 0; -- INVCONV
8435        END IF;
8436 
8437        IF  (p_x_line_rec.reserved_quantity is NOT NULL OR
8438             Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id)) --4689197
8439        AND (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
8440             -- BUG 1955004
8441             sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
8442             -- END 1955004
8443             sch_cached_sch_level_code is null)
8444        AND  l_old_reserved_qty = 0
8445        THEN
8446 
8447          IF l_debug_level  > 0 THEN
8448              oe_debug_pub.add(  'SUB: SETTING RESERVE LATER FLAG' , 3 ) ;
8449          END IF;
8450          x_reserve_later := 'Y';
8451        END IF;
8452 
8453        RETURN;
8454      END IF;
8455 
8456      IF nvl(p_x_line_rec.shippable_flag,'N') = 'Y'
8457      THEN
8458 
8459        IF NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
8460                                p_old_line_rec.schedule_ship_date)
8461           AND OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id, -- 5178175
8462                                p_old_line_rec.ship_from_org_id)
8463        AND l_old_reserved_qty > 0
8464        AND p_x_line_rec.ordered_quantity > 0
8465        THEN
8466 
8467 
8468          l_reservation_rec.reservation_id := fnd_api.g_miss_num;
8469 
8470          l_sales_order_id
8471                      := Get_mtl_sales_order_id(p_old_line_rec.header_id);
8472          l_reservation_rec.demand_source_header_id  := l_sales_order_id;
8473          l_reservation_rec.demand_source_line_id    := p_old_line_rec.line_id;
8474          l_reservation_rec.organization_id  := p_old_line_rec.ship_from_org_id;
8475 
8476 
8477          IF l_debug_level  > 0 THEN
8478              oe_debug_pub.add(  'RSCH: CALLING INVS QUERY_RESERVATION ' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
8479          END IF;
8480          inv_reservation_pub.query_reservation
8481            ( p_api_version_number       => 1.0
8482            , p_init_msg_lst              => fnd_api.g_true
8483            , x_return_status             => x_return_status
8484            , x_msg_count                 => l_msg_count
8485            , x_msg_data                  => l_msg_data
8486            , p_query_input               => l_reservation_rec
8487            , x_mtl_reservation_tbl       => l_rsv_tbl
8488            , x_mtl_reservation_tbl_count => l_count
8489            , x_error_code                => l_x_error_code
8490            , p_lock_records              => l_lock_records
8491            , p_sort_by_req_date          => l_sort_by_req_date
8492            );
8493 
8494           IF l_debug_level  > 0 THEN
8495              oe_debug_pub.add(  'AFTER CALLING INVS QUERY_RESERVATION: ' || X_RETURN_STATUS , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
8496           END IF;
8497 
8498          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8499              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8500          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8501              RAISE FND_API.G_EXC_ERROR;
8502          END IF;
8503 
8504                                        IF l_debug_level  > 0 THEN
8505                                            oe_debug_pub.add(  'RESERVATION RECORD COUNT IS: ' || L_RSV_TBL.COUNT , 1 ) ;
8506                                        END IF;
8507 
8508          -- Let's get the total reserved_quantity
8509          FOR K IN 1..l_rsv_tbl.count LOOP
8510 
8511           l_reservation_rec := l_rsv_tbl(K);
8512           l_reservation_rec.requirement_date := p_x_line_rec.schedule_ship_date;
8513 
8514           IF l_debug_level  > 0 THEN
8515               oe_debug_pub.add(  'RSCH: CALLING INVS UPDATE RESERVATION ' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
8516           END IF;
8517           inv_reservation_pub.update_reservation
8518             ( p_api_version_number        => 1.0
8519             , p_init_msg_lst              => fnd_api.g_true
8520             , x_return_status             => x_return_status
8521             , x_msg_count                 => l_msg_count
8522             , x_msg_data                  => l_msg_data
8523             , p_original_rsv_rec          => l_rsv_tbl(k)
8524             , p_to_rsv_rec                => l_reservation_rec
8525             , p_original_serial_number    => l_dummy_sn -- no serial contorl
8526             , p_to_serial_number          => l_dummy_sn -- no serial control
8527             , p_validation_flag           => fnd_api.g_true
8528             , p_over_reservation_flag     => 2 -- 4715544
8529             );
8530 
8531            IF l_debug_level  > 0 THEN
8532              oe_debug_pub.add(  'AFTER CALLING INVS UPDATE_RESERVATION: ' || X_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
8533            END IF;
8534 
8535           IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8536                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8537           ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8538                  IF l_msg_data is not null THEN
8539                     fnd_message.set_encoded(l_msg_data);
8540                     l_buffer := fnd_message.get;
8541                     oe_msg_pub.add_text(p_message_text => l_buffer);
8542                     IF l_debug_level  > 0 THEN
8543                         oe_debug_pub.add(  'ERROR : '|| L_BUFFER , 1 ) ;
8544                     END IF;
8545                   END IF;
8546                   RAISE FND_API.G_EXC_ERROR;
8547           END IF;
8548          END LOOP;
8549 
8550        END IF; -- Ship date has changed
8551 
8552           -- End code for bug 2126165.
8553 
8554         -- Inv code expect the item to be available in database before
8555         -- making reservation. If the reservation is being made due to
8556         -- change in inventory_item_id, we will move the reservation
8557         -- call to post_write. -- 1913263.
8558 
8559         -- pre-write scheduling call has been moved to post write.
8560         -- we can perform reservation right here for item change.
8561 
8562     /*    IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
8563                                 p_old_line_rec.inventory_item_id)
8564         THEN
8565 
8566              oe_debug_pub.add('No re-reserve due to item change',1);
8567              RETURN;
8568         END IF;
8569   */
8570         -- 4316272
8571         IF  (nvl(p_x_line_rec.reserved_quantity, 0) > 0 OR
8572              Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date,
8573 				   p_x_line_rec.org_id))
8574         AND (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
8575             -- BUG 1955004
8576             sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
8577             -- END 1955004
8578              sch_cached_sch_level_code is null)
8579         AND  l_old_reserved_qty = 0
8580         THEN
8581 
8582        IF l_debug_level  > 0 THEN
8583            oe_debug_pub.add(  'RESERVING IN ACTION_RESCHEDULE' ) ;
8584        END IF;
8585 
8586           -- Old reservation does not exists on the line.
8587           -- create a new reservation.
8588           -- (Post Pack J)Create Reservation if within reservation time fence
8589           -- 4316272 Removed redundant condition.
8590           IF (nvl(p_x_line_rec.reserved_quantity,0) >
8591                  p_x_line_rec.ordered_quantity)
8592           OR nvl(p_x_line_rec.reserved_quantity,0) = 0
8593           OR (nvl(p_x_line_rec.reserved_quantity,0) <
8594                      p_x_line_rec.ordered_quantity
8595             AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG',p_x_line_rec.org_id) = 'Y') THEN
8596              l_qty_to_reserve := p_x_line_rec.ordered_quantity;
8597 
8598           ELSE
8599              l_qty_to_reserve := p_x_line_rec.reserved_quantity;
8600           END IF;
8601 
8602          -- Bug 6335352
8603          -- Added the below code
8604          IF NOT OE_GLOBALS.EQUAL(p_x_line_rec.order_quantity_uom, p_old_line_rec.order_quantity_uom) THEN
8605            IF p_x_line_rec.ordered_quantity > p_x_line_rec.reserved_quantity AND
8606               Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) THEN
8607                 l_qty_to_reserve := p_x_line_rec.ordered_quantity;
8608            ELSIF p_x_line_rec.ordered_quantity < p_x_line_rec.reserved_quantity THEN
8609                l_qty_to_reserve := p_x_line_rec.ordered_quantity;
8610            END IF;
8611          END IF;
8612 
8613       IF (nvl(p_x_line_rec.reserved_quantity2,0) >   -- INVCONV
8614                  nvl(p_x_line_rec.ordered_quantity2, 0) )
8615           OR nvl(p_x_line_rec.reserved_quantity2,0) = 0
8616           --OR (OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
8617           OR Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) --4689197
8618           THEN
8619              l_qty2_to_reserve := nvl(p_x_line_rec.ordered_quantity2, 0);
8620           ELSE
8621              l_qty2_to_reserve := nvl(p_x_line_rec.reserved_quantity2, 0);
8622           END IF;
8623           IF l_qty2_to_reserve = 0 -- INVCONV
8624             THEN
8625             l_qty2_to_reserve := NULL;
8626           END IF;
8627 
8628           IF l_debug_level  > 0 THEN
8629            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
8630            oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
8631           END IF;
8632 
8633 
8634           Reserve_Line
8635           ( p_line_rec             => p_x_line_rec
8636           , p_quantity_to_reserve  => l_qty_to_reserve
8637           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
8638           , x_return_Status        => x_return_status);
8639 
8640 
8641         ELSIF l_old_reserved_qty > 0 THEN
8642 
8643            --If ordered qty is not changed on the line, take care of the
8644            --reservation changes.
8645 
8646           IF  OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
8647                                p_old_line_rec.ordered_quantity)
8648           AND NOT OE_GLOBALS.Equal(p_x_line_rec.reserved_quantity,
8649                                    l_old_reserved_qty)
8650           THEN
8651           IF l_debug_level  > 0 THEN
8652            oe_debug_pub.add(  'reserved qty has changed during rescheduling process. ' , 2 ) ;
8653           END IF;
8654 	     Action_reserve(p_x_line_rec    => p_x_line_rec,
8655                             p_old_line_rec  => p_old_line_rec,
8656                             x_return_status => x_return_status);
8657 
8658 -- start bug 16782621
8659           ELSIF  OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity2,
8660                                   p_old_line_rec.ordered_quantity2)
8661             AND NOT OE_GLOBALS.Equal(p_x_line_rec.reserved_quantity2,
8662                                      l_old_reserved_qty2)
8663             AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S'
8664           THEN
8665           IF l_debug_level  > 0 THEN
8666            oe_debug_pub.add(  'reserved qty2 has changed during rescheduling process. ' , 2 ) ;
8667           END IF;
8668              Action_reserve(p_x_line_rec    => p_x_line_rec,
8669                             p_old_line_rec  => p_old_line_rec,
8670                             x_return_status => x_return_status);
8671 --end bug 16782621
8672           ELSE -- Ordered qty has changed on the line.
8673 
8674 
8675            l_changed_reserved_qty   := l_old_reserved_qty -
8676                                        p_x_line_rec.reserved_quantity;
8677            l_changed_reserved_qty2   := nvl(l_old_reserved_qty2, 0) -   -- INVCONV
8678                                        nvl(p_x_line_rec.reserved_quantity2,0);
8679            IF l_changed_reserved_qty2 = 0 -- INVCONV
8680             THEN
8681                  l_changed_reserved_qty2 := NULL;
8682            END IF;
8683 
8684 --           IF  p_x_line_rec.ordered_quantity  >=  p_x_line_rec.reserved_quantity -- bug 16782621
8685            IF ( p_x_line_rec.ordered_quantity  >=  p_x_line_rec.reserved_quantity
8686 	               AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8687              OR( p_x_line_rec.ordered_quantity2  >=  p_x_line_rec.reserved_quantity2
8688 	               AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8689 	   THEN
8690             -- Ordered qty is greater than res qty. Take care of reservation
8691             -- changes if any.
8692 
8693 --            IF l_changed_reserved_qty > 0 THEN  -- bug 16782621
8694             IF (l_changed_reserved_qty > 0  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8695 	     OR (l_changed_reserved_qty2 > 0  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8696 	    THEN
8697 
8698                IF l_debug_level  > 0 THEN
8699                    oe_debug_pub.add(  'RESERVED QUANTITY HAS DECREASED' , 1 ) ;
8700                END IF;
8701 
8702                 -- No need to pass old record. Since this is a change
8703                 -- due to quantity.
8704                 Unreserve_Line
8705                 ( p_line_rec              => p_x_line_rec
8706                 , p_quantity_to_unreserve => l_changed_reserved_qty
8707                 , p_quantity2_to_unreserve => l_changed_reserved_qty2 -- INVCONV
8708                 , x_return_status         => x_return_status);
8709 
8710                 IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8711                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8712                 ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8713                    RAISE FND_API.G_EXC_ERROR;
8714                 END IF;
8715             /* -- Moved to the last part of If-elsif check
8716             -- Pack J
8717             -- Call reserve_line if partial flag is set to 'Yes' and
8718             --ordered quantity is greater than reserved quantity.
8719             ELSIF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
8720                  --AND OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
8721                  --                  p_old_line_rec.ordered_quantity)
8722                  AND p_x_line_rec.ordered_quantity >NVL(p_x_line_rec.reserved_quantity,0)
8723                  AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
8724                  AND Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date) THEN
8725                    l_qty_to_reserve :=
8726                       p_x_line_rec.ordered_quantity - NVL(p_x_line_rec.reserved_quantity,0);
8727                    l_qty2_to_reserve :=
8728                       nvl(p_x_line_rec.ordered_quantity2, 0) - NVL(p_x_line_rec.reserved_quantity2,0); -- INVCONV
8729                  -- Setting rsv_update flag to TRUE if reservation exists
8730                  IF NVL(p_x_line_rec.reserved_quantity,0) > 0 THEN
8731                  -- Going to update the reservation
8732                     l_rsv_update := TRUE;
8733                  END IF;
8734                  Reserve_Line
8735                   ( p_line_rec              => p_x_line_rec
8736                   , p_quantity_to_reserve   => l_qty_to_reserve
8737                   , p_quantity2_to_reserve   => l_qty2_to_reserve -- INVCONV
8738                   , p_rsv_update            => l_rsv_update
8739                   , x_return_status         => x_return_status);
8740             */
8741 
8742   --          ELSIF l_changed_reserved_qty < 0  -- bug 16782621
8743             ELSIF ( l_changed_reserved_qty < 0	  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8744 	       OR ( l_changed_reserved_qty2 < 0	  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8745 	    THEN
8746 
8747               IF l_debug_level  > 0 THEN
8748                   oe_debug_pub.add(  'RESERVED QUANTITY HAS INCREASED' , 1 ) ;
8749               END IF;
8750 
8751               l_qty_to_reserve := p_x_line_rec.reserved_quantity -
8752                                   l_old_reserved_qty;
8753 
8754               l_qty2_to_reserve := NVL(p_x_line_rec.reserved_quantity2,0) -   -- INVCONV
8755                                   nvl(l_old_reserved_qty2, 0);
8756 
8757               IF l_qty2_to_reserve = 0 THEN -- INVCONV
8758                 l_qty2_to_reserve := null;
8759               END IF;
8760               IF l_debug_level  > 0 THEN
8761                 oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
8762                 oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
8763               END IF;
8764 
8765 
8766               Reserve_Line
8767               ( p_line_rec             => p_x_line_rec
8768               , p_quantity_to_reserve  => l_qty_to_reserve
8769               , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
8770               , p_rsv_update           => TRUE
8771               , x_return_Status        => x_return_status);
8772 
8773               IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8774                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8775               ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8776                   RAISE FND_API.G_EXC_ERROR;
8777               END IF;
8778 
8779             /* -- Begin Bug 2794760    -- INVCONV take out OPM code
8780 --removed commented code , refer  OEXUSCHB.pls 120.117.12020000.19
8781             -- END Bug 2794760   */
8782 
8783             -- Post Pack J
8784             -- Call reserve_line if ordered quantity is greater than reserved quantity.
8785             ELSIF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
8786                  --AND OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
8787                  --                  p_old_line_rec.ordered_quantity)
8788 --                 AND p_x_line_rec.ordered_quantity >NVL(p_x_line_rec.reserved_quantity,0)--commented for bug 16782621
8789                  --AND OE_SYS_PARAMETERS.value('PARTIAL_RESERVATION_FLAG') = 'Y'
8790                  AND ( (p_x_line_rec.ordered_quantity >NVL(p_x_line_rec.reserved_quantity,0) --added for bug 16782621
8791                             AND Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8792                       OR (p_x_line_rec.ordered_quantity2 >NVL(p_x_line_rec.reserved_quantity2,0)
8793                             AND Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8794                       )
8795                  AND Within_Rsv_Time_Fence(p_x_line_rec.schedule_ship_date, p_x_line_rec.org_id) THEN --4689197
8796                    l_qty_to_reserve :=
8797                       p_x_line_rec.ordered_quantity - NVL(p_x_line_rec.reserved_quantity,0);
8798                 -- KYH BUG 4245418 BEGIN
8799                 -- =====================
8800                 -- l_qty2_to_reserve :=   -- INVCONV
8801                 --    nvl(p_x_line_rec.ordered_quantity2, 0) - NVL(p_x_line_rec.reserved_quantity2,0);
8802                 -- It is dangerous to compute secondary quantity to reserve based on
8803                 -- ordered_quantity2 minus reserved_quantity2 as above.
8804                 -- This is because ordered_quantity2 always reflects a standard conversion from ordered_quantity
8805                 -- whereas reserved_quantity2 may be the result of one or more lot specific calculations
8806                 -- Combining values from these different conversion rates may not give the correct result.
8807                 -- Better to compute the secondary to reserve by converting l_qty_to_reserve
8808                    IF p_x_line_rec.ordered_quantity_uom2 is not null and
8809                      p_x_line_rec.ordered_quantity_uom2 <> FND_API.G_MISS_CHAR THEN
8810                      -- Only invoke the conversion for dual tracked items
8811                      IF l_debug_level  > 0 THEN
8812                        oe_debug_pub.add(  'DUAL Tracked quantity so convert the qty to reserve ' || l_qty_to_reserve , 1 ) ;
8813                      END IF;
8814                      IF Nvl(p_x_line_rec.fulfillment_base ,'P') ='P' THEN  --added for bug 16782621
8815                       l_qty2_to_reserve    := inv_convert.inv_um_convert(
8816                                           item_id                      => p_x_line_rec.inventory_item_id
8817                                         , lot_number                   => NULL
8818                                         , organization_id              => p_x_line_rec.ship_from_org_id
8819                                         , PRECISION                    => 5
8820                                         , from_quantity                => l_qty_to_reserve
8821                                         , from_unit                    => p_x_line_rec.order_quantity_uom
8822                                         , to_unit                      => p_x_line_rec.ordered_quantity_uom2
8823                                         , from_name                    => NULL -- from uom name
8824                                         , to_name                      => NULL -- to uom name
8825                                         );
8826                        IF l_debug_level  > 0 THEN
8827                          oe_debug_pub.add(  'After UOM conversion the secondary to reserve is  ' || l_qty2_to_reserve , 1 ) ;
8828                        END IF;
8829 
8830                      IF l_qty2_to_reserve = -99999 THEN
8831                        -- conversion failed
8832                        FND_MESSAGE.SET_NAME('INV','INV_NO_CONVERSION_ERR'); -- INVCONV
8833                        OE_MSG_PUB.ADD;
8834                        IF l_debug_level  > 0 THEN
8835                          oe_debug_pub.add(  'ERROR on UOM conversion to Secondary UOM which is '||p_x_line_rec.ordered_quantity_uom2 , 1 ) ;
8836                        END IF;
8837                        RAISE FND_API.G_EXC_ERROR;
8838                      END IF;
8839 
8840 		     ELSE --when Nvl(p_x_line_rec.fulfillment_base ,'P') ='S'
8841                       l_qty2_to_reserve :=nvl(p_x_line_rec.ordered_quantity2, 0) - NVL(p_x_line_rec.reserved_quantity2,0);
8842                      END IF;
8843 
8844 		   END IF;--p_x_line_rec.ordered_quantity_uom2 is not null
8845                 -- KYH BUG 4245418 END
8846 
8847                IF l_debug_level  > 0 THEN
8848                 oe_debug_pub.add(  'p_x_line_rec.ordered_quantity ' || p_x_line_rec.ordered_quantity , 2 ) ;
8849                 oe_debug_pub.add(  'p_x_line_rec.reserved_quantity ' || p_x_line_rec.reserved_quantity , 2 ) ;
8850                 oe_debug_pub.add(  'p_x_line_rec.ordered_quantity2 ' || p_x_line_rec.ordered_quantity2 , 2 ) ;
8851                 oe_debug_pub.add(  'p_x_line_rec.reserved_quantity2 ' || p_x_line_rec.reserved_quantity2 , 2 ) ;
8852                END IF;
8853 
8854 
8855                    IF l_qty2_to_reserve = 0 THEN -- INVCONV
8856                             l_qty2_to_reserve := null;
8857                    END IF;
8858 
8859 
8860                  -- Setting rsv_update flag to TRUE if reservation exists
8861                  IF NVL(p_x_line_rec.reserved_quantity,0) > 0 THEN
8862                  -- Going to update the reservation
8863                     l_rsv_update := TRUE;
8864                  END IF;
8865 
8866                  IF l_debug_level  > 0 THEN
8867                     oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
8868                     oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
8869                  END IF;
8870 
8871 
8872 
8873                  Reserve_Line
8874                   ( p_line_rec              => p_x_line_rec
8875                   , p_quantity_to_reserve   => l_qty_to_reserve
8876                   , p_quantity2_to_reserve   => l_qty2_to_reserve
8877                   , p_rsv_update            => l_rsv_update
8878                   , x_return_status         => x_return_status);
8879 
8880             END IF; -- end of reserved_quantity change code
8881 
8882            ELSE
8883 
8884             -- Order qty is less than res qty. That means, we are
8885             -- need to unreserve the extra qty. However, that extra res
8886             -- may not exists in reservation table.
8887             -- For example Orderes qty is getting updated as 10 to 9
8888             -- and reservation qty from 9 to 12. In this case we do not
8889             -- need any res changes. To deal this, check the old reservation
8890             -- with the new ordered qty.
8891 
8892             IF l_debug_level  > 0 THEN
8893                 oe_debug_pub.add(  'ORDERED QTY IS GREATER THAN RES QTY' , 1 ) ;
8894             END IF;
8895 /*modified below if for bug16782621
8896            IF NOT OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity,
8897                         p_old_line_rec.ordered_quantity) THEN --for bug 12378904
8898 */           IF ( NOT OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity, -- bug16782621
8899                                        p_old_line_rec.ordered_quantity)
8900                    AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')
8901               OR( NOT OE_GLOBALS.Equal(p_x_line_rec.ordered_quantity2,
8902                                        p_old_line_rec.ordered_quantity2)
8903                    AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8904              THEN
8905             IF l_debug_level  > 0 THEN
8906                 oe_debug_pub.add(  'OLD Ordered QTY Not Equal to New Ordered QTY' , 1 ) ;
8907             END IF;
8908 
8909 
8910 	    l_qty_to_unreserve := l_old_reserved_qty -
8911                                   p_x_line_rec.ordered_quantity;
8912 
8913             l_qty2_to_unreserve := nvl(l_old_reserved_qty2, 0) -   -- INVCONV
8914                                   nvl(p_x_line_rec.ordered_quantity2, 0);
8915             IF l_qty2_to_unreserve = 0 THEN -- INVCONV
8916                 l_qty2_to_unreserve := null;
8917             end if;
8918 --            IF l_qty_to_unreserve > 0 THEN ---- bug16782621
8919                /* Unreserve_Line
8920                ( p_line_rec              => p_x_line_rec
8921                , p_quantity_to_unreserve => l_qty_to_unreserve
8922                , p_quantity2_to_unreserve => l_qty2_to_unreserve -- INVCONV
8923                , x_return_status         => x_return_status);
8924                */
8925            IF  (l_qty_to_unreserve > 0  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='P')-- bug16782621
8926 	     OR (l_qty2_to_unreserve > 0  AND  Nvl(p_x_line_rec.fulfillment_base ,'P') ='S')
8927 	    THEN
8928               -- Start 13906710
8929               IF (nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'Y'
8930                   AND NOT Get_Pick_Status(p_x_line_rec.line_id) ) THEN
8931               --unreserve the line if it's not picked
8932                   Do_Unreserve
8933                         ( p_line_rec               => p_x_line_rec
8934                         , p_quantity_to_unreserve  => l_qty_to_unreserve --l_changed_reserved_qty Bug 14110600
8935                         , p_quantity2_to_unreserve  => l_qty2_to_unreserve --l_changed_reserved_qty2-- INVCONV
8936                         , p_old_ship_from_org_id    =>p_old_line_rec.ship_from_org_id
8937                         , x_return_status          => x_return_status);
8938 
8939               ELSIF nvl(p_x_line_rec.shipping_interfaced_flag,'N') = 'N' THEN
8940               --unreserve the line only if it's not interfaced to WSH
8941                   Unreserve_Line
8942                       ( p_line_rec              => p_x_line_rec
8943                       , p_quantity_to_unreserve => l_qty_to_unreserve --l_changed_reserved_qty Bug 14110600
8944                       , p_quantity2_to_unreserve => l_qty2_to_unreserve --l_changed_reserved_qty2 --INVCONV
8945                       , x_return_status         => x_return_status);
8946               END IF;
8947               --End 13906710
8948 
8949                IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
8950                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8951                ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
8952                   RAISE FND_API.G_EXC_ERROR;
8953                END IF;
8954 
8955             END IF; -- reserved_quantity same, ordered_quantity changed
8956            END IF ; --for bug 12378904
8957            END IF; -- Order qty > reserve qty.
8958           END IF; -- Order qty and res qty changes.
8959         END IF; -- Need reservation changes.
8960      END IF; -- Shippable flag.
8961     IF l_debug_level  > 0 THEN
8962         oe_debug_pub.add(  'EXITING ACTION_RESCHEDULE ' , 1 ) ;
8963     END IF;
8964 EXCEPTION
8965    WHEN FND_API.G_EXC_ERROR THEN
8966 
8967         x_return_status := FND_API.G_RET_STS_ERROR;
8968 
8969    WHEN OTHERS THEN
8970 
8971         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8972 
8973         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8974         THEN
8975             OE_MSG_PUB.Add_Exc_Msg
8976             (   G_PKG_NAME,
8977               'Action_Reschedule');
8978         END IF;
8979 END Action_Reschedule;
8980 
8981 /*-----------------------------------------------------------------------------
8982 Procedure Name : Load_INV_Request
8983 Description    : This procedure loads the INV's record structure which
8984                  we will pass to INV for reservation purpose.
8985                  We need to pass to INV the idenfier for OM demand.
8986                  We pass the constant INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE
8987                  for all OM Order Lines except Internal Orders.
8988                  For Internal Orders we pass
8989                  INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD
8990                  as the identifier.
8991 ----------------------------------------------------------------------------- */
8992 Procedure Load_INV_Request
8993 ( p_line_rec                 IN  Oe_Order_Pub.Line_Rec_Type
8994 , p_quantity_to_reserve      IN  NUMBER
8995 , p_quantity2_to_reserve      IN  NUMBER DEFAULT NULL
8996 , x_reservation_rec OUT NOCOPY Inv_Reservation_Global.Mtl_Reservation_Rec_Type)
8997 
8998 IS
8999 l_source_code          VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
9000 l_sales_order_id       NUMBER;
9001 l_subinventory         VARCHAR2(10);
9002 --
9003 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9004 
9005 l_quantity2_to_reserve NUMBER; --INVCONV
9006 --
9007 BEGIN
9008    IF l_debug_level  > 0 THEN
9009        oe_debug_pub.add(  'ENTERING LOAD INV REQUEST' , 1 ) ;
9010    END IF;
9011 
9012    x_reservation_rec                      := null;
9013    x_reservation_rec.reservation_id       := fnd_api.g_miss_num; -- cannot know
9014    x_reservation_rec.requirement_date     := p_line_rec.schedule_ship_date;
9015    x_reservation_rec.organization_id      := p_line_rec.ship_from_org_id;
9016    x_reservation_rec.inventory_item_id    := p_line_rec.inventory_item_id;
9017 
9018    IF p_line_rec.source_document_type_id = 10 THEN
9019 
9020       -- This is an internal order line. We need to give
9021       -- a different demand sourc2e type for these lines.
9022 
9023       x_reservation_rec.demand_source_type_id    :=
9024               INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD;
9025                                                  -- intenal order
9026 
9027    ELSE
9028 
9029       x_reservation_rec.demand_source_type_id    :=
9030              INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE; -- order entry
9031 
9032    END IF;
9033 
9034 
9035      -- Get demand_source_header_id from mtl_sales_orders
9036 
9037      l_sales_order_id := Get_mtl_sales_order_id(p_line_rec.HEADER_ID);
9038 
9039      IF l_debug_level  > 0 THEN
9040          oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 1 ) ;
9041      END IF;
9042 
9043      IF p_line_rec.subinventory = FND_API.G_MISS_CHAR THEN
9044         l_subinventory := NULL;
9045      ELSE
9046         l_subinventory := p_line_rec.subinventory;
9047      END IF;
9048 
9049      x_reservation_rec.demand_source_header_id := l_sales_order_id;
9050      x_reservation_rec.demand_source_line_id   := p_line_rec.line_id;
9051      x_reservation_rec.reservation_uom_code    := p_line_rec.order_quantity_uom;
9052      x_reservation_rec.reservation_quantity    := p_quantity_to_reserve;
9053      x_reservation_rec.supply_source_type_id   :=
9054              INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INV;
9055      x_reservation_rec.subinventory_code       := l_subinventory;
9056      /* OPM 02/JUN/00 send process attributes into the reservation -- INVCONV - no longer using attributes
9057      =============================================================
9058      x_reservation_rec.attribute1  := p_line_rec.preferred_grade;
9059      x_reservation_rec.secondary_reservation_quantity   := p_line_rec.ordered_quantity2;
9060      x_reservation_rec.secondary_uom_code    := p_line_rec.ordered_quantity_uom2;
9061      x_reservation_rec.attribute3  := p_line_rec.ordered_quantity_uom2;
9062 
9063       OPM 02/JUN/00 END
9064     ====================*/
9065     --- diff between  x_reservation_rec.reservation_quantity    and x_reservation_rec.primary_reservation_quantity
9066     -- INVCONV
9067 
9068       IF p_quantity2_to_reserve = 0 -- INVCONV
9069         then
9070          l_quantity2_to_reserve := null;
9071        ELSE
9072 
9073         l_quantity2_to_reserve := p_quantity2_to_reserve;
9074        END IF;
9075 
9076 
9077 
9078      x_reservation_rec.secondary_reservation_quantity   := l_quantity2_to_reserve;
9079      x_reservation_rec.secondary_uom_code               := p_line_rec.ordered_quantity_uom2;
9080      --4653097
9081      IF p_line_rec.project_id IS NOT NULL THEN
9082         x_reservation_rec.project_id := p_line_rec.project_id;
9083      END IF;
9084      IF p_line_rec.task_id IS NOT NULL THEN
9085         x_reservation_rec.task_id := p_line_rec.task_id;
9086      END IF;
9087 
9088      IF l_debug_level  > 0 THEN
9089          oe_debug_pub.add(  'p_quantity2_to_reserve = ' || p_quantity2_to_reserve , 1 ) ;
9090          oe_debug_pub.add(  'p_line_rec.ordered_quantity_uom2 = ' || p_line_rec.ordered_quantity_uom2 , 1 ) ;
9091      END IF;
9092 
9093 
9094    IF l_debug_level  > 0 THEN
9095        oe_debug_pub.add(  'EXITING LOAD INV REQUEST' , 1 ) ;
9096    END IF;
9097 EXCEPTION
9098 
9099    WHEN NO_DATA_FOUND THEN
9100        IF l_debug_level  > 0 THEN
9101            oe_debug_pub.add(  'WHEN OTHERS OF LOAD INV REQUEST' , 1 ) ;
9102        END IF;
9103        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9104 
9105 END Load_INV_Request;
9106 
9107 PROCEDURE Set_Auto_sch_flag_for_batch(p_header_id IN NUMBER)
9108 IS
9109 --
9110 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9111 --
9112 BEGIN
9113                                         IF l_debug_level  > 0 THEN
9114                                             oe_debug_pub.add(  'ENTERING PROCEDURE SET_AUTO_SCH_FLAG_FOR_BATCH: ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
9115                                         END IF;
9116 
9117     IF OESCH_AUTO_SCHEDULE_PROFILE = 'Y' THEN
9118        Set_Auto_Sch_Flag('Y');
9119     ELSE
9120      IF l_debug_level  > 0 THEN
9121          oe_debug_pub.add(  'LOAD HEADER : ' || P_HEADER_ID , 2 ) ;
9122      END IF;
9123      OE_Order_Cache.Load_Order_Header(p_header_id);
9124      OE_ORDER_CACHE.Load_order_type(OE_ORDER_CACHE.g_header_rec.order_type_id);
9125                      IF l_debug_level  > 0 THEN
9126                          oe_debug_pub.add(  'ORDER TYPE ID : ' || OE_ORDER_CACHE.G_ORDER_TYPE_REC.ORDER_TYPE_ID , 2 ) ;
9127                      END IF;
9128      IF nvl(OE_ORDER_CACHE.g_order_type_rec.auto_scheduling_flag,'N') = 'Y' THEN
9129        Set_Auto_Sch_Flag('Y');
9130      ELSE
9131        Set_Auto_Sch_Flag('N');
9132      END IF;
9133     END IF;
9134 
9135                                 IF l_debug_level  > 0 THEN
9136                                     oe_debug_pub.add(  'EXITING PROCEDURE SET_AUTO_SCH_FLAG_FOR_BATCH: ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
9137                                 END IF;
9138 END Set_Auto_sch_flag_for_batch;
9139 FUNCTION Get_Scheduling_Level( p_header_id IN NUMBER,
9140                                p_line_type_id IN NUMBER)
9141 RETURN VARCHAR2
9142 IS
9143 l_scheduling_level_code  VARCHAR2(30) := null;
9144 l_line_type              VARCHAR2(80) := null;
9145 l_order_type             VARCHAR2(80) := null;
9146 --
9147 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9148 --
9149 BEGIN
9150 
9151   IF l_debug_level  > 0 THEN
9152       oe_debug_pub.add(  'ENTERING GET_SCHEDULING_LEVEL: ' || P_HEADER_ID||'/'||P_LINE_TYPE_ID , 1 ) ;
9153   END IF;
9154 
9155   IF p_line_type_id = sch_cached_line_type_id
9156   THEN
9157 
9158      sch_cached_sch_level_code := sch_cached_sch_level_code_line;
9159      RETURN sch_cached_sch_level_code;
9160 
9161   END IF;
9162 
9163   IF p_line_type_id IS NOT NULL THEN
9164    SELECT name, scheduling_level_code
9165    INTO   l_line_type,l_scheduling_level_code
9166    FROM   oe_transaction_types
9167    WHERE  transaction_type_id = p_line_type_id AND
9168             transaction_type_code = 'LINE';
9169   END IF;
9170 
9171   IF  l_scheduling_level_code IS NOT NULL THEN
9172 
9173       sch_cached_line_type_id   := p_line_type_id;
9174       sch_cached_sch_level_code := l_scheduling_level_code;
9175       sch_cached_sch_level_code_line := l_scheduling_level_code;
9176       sch_cached_line_type      := l_line_type;
9177       RETURN l_scheduling_level_code;
9178 
9179   END IF;
9180 
9181   IF p_header_id = sch_cached_header_id
9182   THEN
9183 
9184      sch_cached_sch_level_code := sch_cached_sch_level_code_head;
9185      RETURN sch_cached_sch_level_code;
9186 
9187   END IF;
9188   SELECT /* MOAC_SQL_CHANGE*/ name, scheduling_level_code
9189   INTO   l_order_type,l_scheduling_level_code
9190   FROM   oe_order_types_v ot, oe_order_headers_all h
9191   WHERE  h.header_id     = p_header_id AND
9192          h.order_type_id =  ot.order_type_id;
9193 
9194   sch_cached_header_id      := p_header_id;
9195   sch_cached_sch_level_code := l_scheduling_level_code;
9196   sch_cached_sch_level_code_head := l_scheduling_level_code;
9197   sch_cached_order_type     := l_order_type;
9198 
9199   IF l_debug_level  > 0 THEN
9200       oe_debug_pub.add(  'EXITING GET_SCHEDULING_LEVEL' , 1 ) ;
9201   END IF;
9202   RETURN l_scheduling_level_code;
9203 
9204 EXCEPTION
9205    WHEN NO_DATA_FOUND THEN
9206         IF l_debug_level  > 0 THEN
9207             oe_debug_pub.add(  'NO DATA FOUND GET_SCHEDULING_LEVEL' , 1 ) ;
9208         END IF;
9209         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9210 
9211 END Get_Scheduling_Level;
9212 
9213 
9214 /*---------------------------------------------------------------
9215 Handle_External_Lines
9216 ----------------------------------------------------------------*/
9217 PROCEDURE Handle_External_Lines
9218 ( p_x_line_rec  IN OUT NOCOPY   OE_ORDER_PUB.line_rec_type)
9219 IS
9220 
9221   l_line_tbl              OE_ORDER_PUB.line_tbl_type;
9222   l_old_line_tbl          OE_ORDER_PUB.line_tbl_type;
9223   l_line_rec              OE_ORDER_PUB.line_rec_type
9224                           := OE_Order_Pub.G_MISS_LINE_REC;
9225   l_control_rec           OE_GLOBALS.control_rec_type;
9226   l_index                 NUMBER;
9227   l_return_status         VARCHAR2(1);
9228 
9229   CURSOR ato_options IS
9230   SELECT line_id
9231   FROM   oe_order_lines
9232   WHERE  top_model_line_id = p_x_line_rec.top_model_line_id
9233   AND    ato_line_id       = p_x_line_rec.ato_line_id
9234   AND    open_flag = 'Y'
9235   AND    source_type_code = 'EXTERNAL';
9236 
9237 --
9238 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9239 --
9240 BEGIN
9241 
9242   IF l_debug_level  > 0 THEN
9243       oe_debug_pub.add(  'UPDATE SHIP DATE , EXTERNAL '|| P_X_LINE_REC.LINE_ID , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
9244   END IF;
9245 
9246   IF p_x_line_rec.ship_from_org_id is NULL THEN
9247     FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATO_WHSE_REQD');
9248     OE_MSG_PUB.Add;
9249     RAISE FND_API.G_EXC_ERROR;
9250   END IF;
9251 
9252   IF  p_x_line_rec.schedule_ship_date is NULL THEN
9253     p_x_line_rec.schedule_ship_date := p_x_line_rec.request_date;
9254   END IF;
9255 
9256   IF l_debug_level  > 0 THEN
9257       oe_debug_pub.add(  'SHIP DATE '|| P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
9258   END IF;
9259 
9260   l_index := 0;
9261   l_line_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
9262 
9263   IF p_x_line_rec.ato_line_id is NULL OR
9264      p_x_line_rec.top_model_line_id is NULL
9265   THEN
9266 
9267     l_index := l_index + 1;
9268 
9269     l_line_rec.line_id            := p_x_line_rec.line_id;
9270     l_line_rec.schedule_ship_date := p_x_line_rec.schedule_ship_date;
9271 
9272     l_line_tbl(l_index) := l_line_rec;
9273   ELSE
9274 
9275     FOR ato_rec in ato_options
9276     LOOP
9277 
9278       l_index := l_index + 1;
9279       l_line_rec.line_id            := ato_rec.line_id;
9280       l_line_rec.schedule_ship_date := p_x_line_rec.schedule_ship_date;
9281 
9282       l_line_tbl(l_index) := l_line_rec;
9283 
9284     END LOOP;
9285 
9286     IF l_debug_level  > 0 THEN
9287         oe_debug_pub.add(  'ATO EXTERNAL '|| P_X_LINE_REC.ATO_LINE_ID , 1 ) ;
9288     END IF;
9289   END IF;
9290 
9291 
9292   OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
9293   OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
9294 
9295   IF l_debug_level  > 0 THEN
9296       oe_debug_pub.add(  'CALLING PROCESS ORDER' , 1 ) ;
9297   END IF;
9298 
9299   OE_Order_PVT.Lines
9300   ( p_validation_level            => FND_API.G_VALID_LEVEL_NONE
9301    ,p_control_rec                 => l_control_rec
9302    ,p_x_line_tbl                  => l_line_tbl
9303    ,p_x_old_line_tbl              => l_old_line_tbl
9304    ,x_return_status               => l_return_status);
9305 
9306    IF l_debug_level  > 0 THEN
9307        oe_debug_pub.add(  'SCH RETURN_STATUS IS ' || L_RETURN_STATUS , 1 ) ;
9308    END IF;
9309 
9310    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9311      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9312    ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
9313      RAISE FND_API.G_EXC_ERROR;
9314    END IF;
9315 
9316    OE_Order_PVT.Process_Requests_And_Notify
9317    ( p_process_requests    => TRUE
9318     ,p_notify              => TRUE
9319     ,p_line_tbl            => l_line_tbl
9320     ,p_old_line_tbl        => l_old_line_tbl
9321     ,x_return_status       => l_return_status);
9322 
9323    IF l_debug_level  > 0 THEN
9324        oe_debug_pub.add(  '1 SCH RETURN_STATUS IS ' || L_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
9325    END IF;
9326 
9327    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9328      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9329    ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
9330      RAISE FND_API.G_EXC_ERROR;
9331    END IF;
9332 
9333    OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
9334    IF l_debug_level  > 0 THEN
9335        oe_debug_pub.add(  'UI CASCADE FLAG SET TO TRUE' , 4 ) ;
9336    END IF;
9337 
9338    OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
9339    OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
9340 
9341 EXCEPTION
9342   WHEN OTHERS THEN
9343     IF l_debug_level  > 0 THEN
9344         oe_debug_pub.add(  'HANDLE_EXTERNAL_LINES ERROR '|| SQLERRM , 1 ) ;
9345     END IF;
9346     RAISE;
9347 END Handle_External_Lines;
9348 
9349 
9350 /*----------------------------------------------------------------
9351 PROCEDURE Schedule_line
9352 Description:
9353   This procedure will be called from
9354    oe_line_util.pre_write
9355    **
9356    **
9357 
9358   Parameter:
9359   p_caller         => Internal/External.
9360 
9361   If the call is being made by process order then it is called as
9362   internal. In all other cases it is external.
9363 
9364   If the value is set to True that means (oe_line_util) it is a
9365   recursive call and pass appropriate value to process and notify.
9366 
9367   This will be set to external from workflow and grp call. If it is set
9368   external execute request then and there.
9369 
9370   The procedure checks if we need to perform any scheduling action
9371   and then branches the code acoording to the type of sch request.
9372 
9373   Added new call to Handle_External_Lines for config dropshipments.
9374 -----------------------------------------------------------------*/
9375 Procedure Schedule_line
9376 ( p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type,
9377   p_x_line_rec         IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
9378   p_caller             IN  VARCHAR2 := SCH_INTERNAL,
9379 x_return_status OUT NOCOPY VARCHAR2 )
9380 
9381 IS
9382   l_old_line_rec         OE_ORDER_PUB.line_rec_type;
9383   l_sales_order_id       NUMBER;
9384   l_need_sch             BOOLEAN;
9385   l_line_action          VARCHAR2(30) := Null;
9386   l_auto_sch             VARCHAR2(1)  := Null;
9387   l_old_rsv_qty          NUMBER;
9388   l_old_rsv_qty2          NUMBER; -- INVCONV
9389   l_index                NUMBER;
9390   --
9391   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
9392   l_auto_schedule_sets   VARCHAR2(1):='Y' ; --4241385
9393   l_set_id               NUMBER ;	    --4241385
9394   l_set_exists           BOOLEAN;	    --4241385
9395   l_set_scheduled        BOOLEAN ;	    --4241385
9396   --
9397 
9398 
9399 BEGIN
9400 
9401    Print_Time('Entering ** OE_SCHEDULE_UTIL.Schedule_Line ');
9402    IF l_debug_level  > 0 THEN
9403       oe_debug_pub.add('Entering ** OE_SCHEDULE_UTIL.Schedule_Line ',0.5);  -- Added for bug 13435459
9404       oe_debug_pub.add('---- Old Record ---- ',1);
9405       OE_DEBUG_PUB.ADD ( 'LINE ID : '||P_OLD_LINE_REC.LINE_ID , 1 ) ;
9406       oe_debug_pub.add(  'ATO LINE ID : '||P_OLD_LINE_REC.ATO_LINE_ID , 1 ) ;
9407       oe_debug_pub.add(  'ORDERED QTY : '||P_OLD_LINE_REC.ORDERED_QUANTITY , 1 ) ;
9408       oe_debug_pub.add(  'SHIP FROM : '||P_OLD_LINE_REC.SHIP_FROM_ORG_ID , 1 ) ;
9409       oe_debug_pub.add(  'SUBINVENTORY : '||P_OLD_LINE_REC.SUBINVENTORY , 1 ) ;
9410       oe_debug_pub.add(  'SCH SHIP DATE: '||P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
9411       oe_debug_pub.add(  'SCH ARR DATE : '||P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
9412       oe_debug_pub.add(  'SHIP SET ID : '||P_OLD_LINE_REC.SHIP_SET_ID , 1 ) ;
9413       oe_debug_pub.add(  'ARR SET ID : '||P_OLD_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
9414       oe_debug_pub.add(  'ACTION : '||P_OLD_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
9415       oe_debug_pub.add(  'STATUS : '||P_OLD_LINE_REC.SCHEDULE_STATUS_CODE , 1 ) ;
9416       oe_debug_pub.add(  'RES QUANTITY : '||P_OLD_LINE_REC.RESERVED_QUANTITY , 1 ) ;
9417       oe_debug_pub.add(  'RES QUANTITY2 : '||P_OLD_LINE_REC.RESERVED_QUANTITY2 , 1 ) ;
9418       oe_debug_pub.add(  'OVERRIDE ATP : '||P_OLD_LINE_REC.OVERRIDE_ATP_DATE_CODE , 1 ) ;
9419       oe_debug_pub.add(  ' ' , 1 ) ;
9420       oe_debug_pub.add('---- New Record ----',1);
9421       OE_DEBUG_PUB.ADD ( 'LINE ID : '||P_X_LINE_REC.LINE_ID , 1 ) ;
9422       oe_debug_pub.add(  'ATO LINE ID : '||P_X_LINE_REC.ATO_LINE_ID , 1 ) ;
9423       oe_debug_pub.add(  'ORDERED QTY : '||P_X_LINE_REC.ORDERED_QUANTITY , 1 ) ;
9424       oe_debug_pub.add(  'SHIP FROM : '||P_X_LINE_REC.SHIP_FROM_ORG_ID , 1 ) ;
9425       oe_debug_pub.add(  'SUBINVENTORY : '||P_X_LINE_REC.SUBINVENTORY , 1 ) ;
9426       oe_debug_pub.add(  'SCH SHIP DATE: '||P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
9427       oe_debug_pub.add(  'SCH ARR DATE : '||P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
9428       oe_debug_pub.add(  'SHIP SET ID : '||P_X_LINE_REC.SHIP_SET_ID , 1 ) ;
9429       oe_debug_pub.add(  'ARR SET ID : '||P_X_LINE_REC.ARRIVAL_SET_ID , 1 ) ;
9430       oe_debug_pub.add(  'ACTION : '||P_X_LINE_REC.SCHEDULE_ACTION_CODE , 1 ) ;
9431       oe_debug_pub.add(  'STATUS : '||P_X_LINE_REC.SCHEDULE_STATUS_CODE , 1 ) ;
9432       oe_debug_pub.add(  'RES QTY : '||P_X_LINE_REC.RESERVED_QUANTITY , 1 ) ;
9433       oe_debug_pub.add(  'RES QTY2 : '||P_X_LINE_REC.RESERVED_QUANTITY2 , 1 ) ;
9434       oe_debug_pub.add(  'OPERATION : '||P_X_LINE_REC.OPERATION , 1 ) ;
9435       oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG : '||OESCH_AUTO_SCH_FLAG , 1 ) ;
9436       oe_debug_pub.add(  'OVERRIDE ATP : '||P_X_LINE_REC.OVERRIDE_ATP_DATE_CODE , 1 ) ;
9437       oe_debug_pub.add(  ' ' , 1 ) ;
9438    END IF;
9439 
9440    x_return_status := FND_API.G_RET_STS_SUCCESS;
9441 
9442    l_old_line_rec := p_old_line_rec;
9443 
9444    IF p_x_line_rec.ship_from_org_id = FND_API.G_MISS_NUM THEN
9445       p_x_line_rec.ship_from_org_id := null;
9446    END IF;
9447    IF  p_old_line_rec.ship_from_org_id = FND_API.G_MISS_NUM THEN
9448       l_old_line_rec.ship_from_org_id := null;
9449    END IF;
9450 
9451 
9452    IF p_x_line_rec.schedule_status_code is not null THEN
9453 
9454       l_sales_order_id := Get_mtl_sales_order_id(p_x_line_rec.HEADER_ID);
9455 
9456       IF l_old_line_rec.reserved_quantity is null THEN
9457          IF l_debug_level  > 0 THEN
9458             oe_debug_pub.add(  'RR1: L_OLD_LINE_REC.RESERVED_QUANTITY IS NULL' , 1 ) ;
9459          END IF;
9460       ELSIF l_old_line_rec.reserved_quantity = FND_API.G_MISS_NUM THEN
9461          IF l_debug_level  > 0 THEN
9462             oe_debug_pub.add(  'RR2: L_OLD_LINE_REC.RESERVED_QUANTITY IS MISSING' , 1 ) ;
9463          END IF;
9464       END IF;
9465 
9466       --l_old_line_rec.reserved_quantity :=
9467       --     OE_LINE_UTIL.Get_Reserved_Quantity
9468       --     (p_header_id   => l_sales_order_id,
9469       --      p_line_id     => p_x_line_rec.line_id,
9470       --      p_org_id      => p_x_line_rec.ship_from_org_id);
9471 
9472       -- INVCONV
9473 
9474       IF l_old_line_rec.reserved_quantity2 is null THEN
9475          IF l_debug_level  > 0 THEN
9476             oe_debug_pub.add(  'RR1: L_OLD_LINE_REC.RESERVED_QUANTITY2 IS NULL' , 1 ) ;
9477          END IF;
9478       ELSIF l_old_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM THEN
9479          IF l_debug_level  > 0 THEN
9480             oe_debug_pub.add(  'RR2: L_OLD_LINE_REC.RESERVED_QUANTITY2 IS MISSING' , 1 ) ;
9481          END IF;
9482       END IF;
9483 
9484       --l_old_line_rec.reserved_quantity2 :=
9485       --    OE_LINE_UTIL.Get_Reserved_Quantity2
9486       --    (p_header_id   => l_sales_order_id,
9487       --     p_line_id     => p_x_line_rec.line_id,
9488       --     p_org_id      => p_x_line_rec.ship_from_org_id);
9489 
9490       -- INVCONV - MERGED CALLS     FOR OE_LINE_UTIL.Get_Reserved_Quantity and OE_LINE_UTIL.Get_Reserved_Quantity2
9491 
9492       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
9493                                            ,p_line_id   => p_x_line_rec.line_id
9494                                            --Bug 6335352  ,p_org_id    => p_x_line_rec.ship_from_org_id
9495                                            ,p_org_id    => l_old_line_rec.ship_from_org_id
9496                                            ,p_order_quantity_uom => l_old_line_rec.order_quantity_uom
9497                                            ,p_inventory_item_id  => l_old_line_rec.inventory_item_id   --bug 16434168
9498                                            ,x_reserved_quantity =>  l_old_line_rec.reserved_quantity
9499                                            ,x_reserved_quantity2 => l_old_line_rec.reserved_quantity2
9500                                            );
9501 
9502 
9503    ELSE
9504       l_old_line_rec.reserved_quantity := null;
9505       l_old_line_rec.reserved_quantity2 := null; -- INVCONV
9506    END IF;
9507 
9508 
9509    IF l_old_line_rec.reserved_quantity = 0
9510    THEN
9511       -- Currently setting the reserved quantity to null if it is zero.
9512       l_old_line_rec.reserved_quantity := null;
9513    END IF;
9514 
9515    IF l_old_line_rec.reserved_quantity2 = 0 -- INVCONV
9516    THEN
9517       -- Currently setting the reserved quantity2 to null if it is zero.
9518       l_old_line_rec.reserved_quantity2 := null;
9519    END IF;
9520 
9521    --By Achaubey for Bug 3239619/3362461/3456052
9522    -- Old reserve qty quired based on old UOM. Convert it if old UOM is not same as new UOM
9523    --Bug 6335352
9524    --This conversion is not required now, as we are passing old_line_rec.order_quantity_uon to Get_Reserved_Quantities API
9525    /*
9526    IF l_old_line_rec.reserved_quantity > 0  THEN
9527       l_old_rsv_qty := l_old_line_rec.reserved_quantity; -- This qty is as per old UOM
9528       IF NOT OE_GLOBALS.Equal(l_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom) THEN
9529          l_old_rsv_qty := INV_CONVERT.INV_UM_CONVERT(item_id      =>l_old_line_rec.inventory_item_id,
9530                                                      precision    =>5,
9531                                                      from_quantity=>l_old_line_rec.reserved_quantity,
9532                                                      from_unit    => l_old_line_rec.order_quantity_uom,    --old uom
9533                                                      to_unit      => p_x_line_rec.order_quantity_uom,  --new uom
9534                                                      from_name    => NULL,
9535                                                      to_name      => NULL
9536                                                      );
9537       END IF;
9538    END IF;
9539    */
9540 
9541   -- Bug 6335352 starts
9542   IF p_x_line_rec.reserved_quantity = FND_API.G_MISS_NUM
9543   THEN
9544      -- Converting missing to old value
9545      IF NOT OE_GLOBALS.Equal(l_old_line_rec.order_quantity_uom,p_x_line_rec.order_quantity_uom)
9546      AND nvl(l_old_line_rec.reserved_quantity, 0) > 0
9547      THEN
9548               p_x_line_rec.reserved_quantity := INV_CONVERT.INV_UM_CONVERT
9549                                                 (item_id =>l_old_line_rec.inventory_item_id,
9550                                                  precision =>5,
9551                                                  from_quantity=>l_old_line_rec.reserved_quantity,
9552                                                  from_unit => l_old_line_rec.order_quantity_uom,    --old uom
9553                                                  to_unit => p_x_line_rec.order_quantity_uom,  --new uom
9554                                                  from_name => NULL,
9555                                                  to_name => NULL
9556                                                 );
9557      ELSE
9558        -- p_x_line_rec.reserved_quantity := l_old_rsv_qty;
9559        p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
9560      END IF;
9561   END IF;
9562   -- Bug 6335352 ends
9563 
9564    IF l_old_line_rec.reserved_quantity2 > 0  THEN -- INVCONV
9565       l_old_rsv_qty2 := l_old_line_rec.reserved_quantity2; -- INVCONV
9566    END IF;
9567 
9568    IF p_x_line_rec.reserved_quantity = FND_API.G_MISS_NUM
9569    THEN
9570       -- Converting missing to old value
9571       p_x_line_rec.reserved_quantity := l_old_rsv_qty;
9572       -- p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
9573    END IF;
9574 
9575    IF p_x_line_rec.reserved_quantity2 = FND_API.G_MISS_NUM -- INVCONV
9576    THEN
9577       -- Converting missing to old value
9578       p_x_line_rec.reserved_quantity2 := l_old_rsv_qty2;
9579       -- p_x_line_rec.reserved_quantity := l_old_line_rec.reserved_quantity;
9580    END IF;
9581 
9582 
9583    IF l_debug_level  > 0 THEN
9584       oe_debug_pub.add(  'OLD RES QTY :' || L_OLD_LINE_REC.RESERVED_QUANTITY , 1 ) ;
9585    END IF;
9586    IF l_debug_level  > 0 THEN
9587       oe_debug_pub.add(  'NEW RES QTY :' || P_X_LINE_REC.RESERVED_QUANTITY , 1 ) ;
9588    END IF;
9589 
9590    ------------ reserved qty and miss num handling done ----------
9591 
9592    IF p_x_line_rec.source_type_code = 'EXTERNAL' AND
9593       p_x_line_rec.schedule_ship_date is NOT NULL AND
9594       NOT OE_GLOBALS.Equal(p_x_line_rec.schedule_ship_date,
9595                            p_old_line_rec.schedule_ship_date) AND
9596       p_x_line_rec.ato_line_id is NOT NULL
9597    THEN
9598       Handle_External_Lines
9599          (p_x_line_rec   => p_x_line_rec);
9600    END IF;
9601 
9602    -- Item substitution code
9603    -- Clear original attributes when item is changed by user
9604    -- on a scheduled line. On an unscheduled substituted item
9605    -- original item will cleared in pre-write code.
9606 
9607    IF  NOT OE_GLOBALS.Equal(p_x_line_rec.Inventory_Item_Id,
9608                             p_old_line_rec.Inventory_Item_Id)
9609       AND p_x_line_rec.schedule_status_code is not null
9610       AND p_x_line_rec.item_relationship_type is null
9611    THEN
9612       oe_debug_pub.add('SL: clearing out the original item fields') ;
9613       p_x_line_rec.Original_Inventory_Item_Id    := Null;
9614       p_x_line_rec.Original_item_identifier_Type := Null;
9615       p_x_line_rec.Original_ordered_item_id      := Null;
9616       p_x_line_rec.Original_ordered_item         := Null;
9617 
9618 
9619    END IF;
9620 
9621    -- Set auto scheduling flag for batch calls.
9622 
9623 
9624    IF OE_GLOBALS.G_UI_FLAG THEN
9625 
9626       IF l_debug_level  > 0 THEN
9627          oe_debug_pub.add(  'UI IS SET' , 1 ) ;
9628       END IF;
9629    ELSE
9630 
9631       IF l_debug_level  > 0 THEN
9632          oe_debug_pub.add(  'UI IS NOT SET' , 1 ) ;
9633       END IF;
9634    END IF;
9635 
9636    IF l_debug_level  > 0 THEN
9637       oe_debug_pub.add(  'SOURCE :' || P_X_LINE_REC.SOURCE_DOCUMENT_TYPE_ID , 1 ) ;
9638    END IF;
9639    IF l_debug_level  > 0 THEN
9640       oe_debug_pub.add(  'OPR :' || P_X_LINE_REC.OPERATION , 1 ) ;
9641    END IF;
9642 
9643    IF  NOT OE_GLOBALS.G_UI_FLAG
9644       AND NOT (nvl(p_x_line_rec.source_document_type_id,-99) = 2)
9645       AND OE_CONFIG_UTIL.G_CONFIG_UI_USED = 'N'
9646       -- QUOTING change - set auto scheduling flag for complete negotiation step
9647       AND (p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
9648            OR OE_Quote_Util.G_COMPLETE_NEG = 'Y'
9649            )
9650    THEN --2998550 added check for G_CONFIG_UI_USED to set flag for batch
9651 
9652       IF l_debug_level  > 0 THEN
9653          oe_debug_pub.add(  'BEFORE SETTING AUTO SCHEDULING FLAG FOR BATCH' , 1 ) ;
9654       END IF;
9655 
9656       Set_Auto_sch_flag_for_batch(p_x_line_rec.header_id);
9657 
9658    ELSE
9659       IF l_debug_level  > 0 THEN
9660          oe_debug_pub.add(  'ELSE SETTING AUTO SCHEDULING FLAG FOR' , 1 ) ;
9661       END IF;
9662    END IF;
9663 
9664    -- 4026758
9665    IF (NOT OE_GLOBALS.Equal(p_x_line_rec.ship_set_id,
9666                             p_old_line_rec.ship_set_id)
9667        AND p_old_line_rec.ship_set_id IS NOT NULL)
9668       OR (NOT OE_GLOBALS.Equal(p_x_line_rec.arrival_set_id,
9669                                p_old_line_rec.arrival_set_id)
9670           AND p_old_line_rec.arrival_set_id IS NOT NULL)
9671       OR ( ( p_x_line_rec.arrival_set_id is not null
9672              OR p_x_line_rec.ship_set_id is not null)
9673            AND p_x_line_rec.ordered_quantity = 0) THEN
9674       -- Line is being removed from  set.
9675 
9676 
9677    --bug5631508
9678     --Record the set history before deleting it
9679 
9680     OE_AUDIT_HISTORY_PVT.RECORD_SET_HISTORY(p_header_id   => p_x_line_rec.header_id,
9681 					    p_line_id     => p_x_line_rec.line_id,
9682 					    p_set_id      => nvl(l_old_line_rec.ship_set_id,l_old_line_rec.arrival_set_id),
9683 					    x_return_status => x_return_status);
9684 
9685     IF l_debug_level  > 0 THEN
9686          oe_debug_pub.add(  'AFTER Inserting data in OE_SETS_HISTORY table ' || x_return_status , 1 ) ;
9687     END IF;
9688 
9689       Log_Delete_Set_Request
9690          (p_header_id   => p_x_line_rec.header_id,
9691           p_line_id     => p_x_line_rec.line_id,
9692           p_set_id      => nvl(l_old_line_rec.ship_set_id,l_old_line_rec.arrival_set_id),
9693           x_return_status => x_return_status);
9694       IF l_debug_level  > 0 THEN
9695          oe_debug_pub.add(  'AFTER LOGGING DELETE SETS DELAYED REQUEST IN SCHEDULE LINE' || X_RETURN_STATUS , 1 ) ;
9696       END IF;
9697    END IF;
9698 
9699    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
9700       AND   NOT OE_GLOBALS.Equal(l_old_line_rec.firm_demand_flag,
9701                                  p_x_line_rec.firm_demand_flag)
9702       AND  p_x_line_rec.operation = 'UPDATE' THEN
9703 
9704       IF p_x_line_rec.ship_model_complete_flag = 'Y'
9705       THEN
9706          Update oe_order_lines_all
9707             Set firm_demand_flag = p_x_line_rec.firm_demand_flag
9708             Where top_model_line_id = p_x_line_rec.top_model_line_id;
9709 
9710          OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
9711       ELSIF p_x_line_rec.ato_line_id is not null
9712             AND   NOT(p_x_line_rec.ato_line_id =p_x_line_rec.line_id
9713                       AND   p_x_line_rec.item_type_code IN (OE_GLOBALS.G_ITEM_STANDARD,
9714                                                             OE_GLOBALS.G_ITEM_OPTION,
9715 							    OE_GLOBALS.G_ITEM_INCLUDED)) --9775352
9716       THEN
9717 
9718          Update oe_order_lines_all
9719             Set firm_demand_flag = p_x_line_rec.firm_demand_flag
9720             Where ato_line_id = p_x_line_rec.ato_line_id;
9721 
9722          OE_GLOBALS.G_CASCADING_REQUEST_LOGGED := TRUE;
9723       END IF;
9724    END IF; -- Code
9725 
9726 
9727    l_need_sch :=  Need_Scheduling(p_line_rec         => p_x_line_rec,
9728                                   p_old_line_rec     => l_old_line_rec,
9729                                   x_line_action      => l_line_action,
9730                                   x_auto_sch         => l_auto_sch);
9731 
9732    IF not(l_need_sch) THEN
9733 
9734       IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_SHIP_METHOD_FLAG'),'Y')  = 'N'
9735          AND NOT OE_GLOBALS.Equal(p_x_line_rec.shipping_method_code,
9736                                   l_old_line_rec.shipping_method_code)
9737          AND fnd_profile.value('ONT_SHIP_METHOD_FOR_SHIP_SET') = 'Y'
9738       THEN
9739 
9740          IF l_debug_level  > 0 THEN
9741             oe_debug_pub.add(  'SHIPPING_METHOD CHANGED , CASCADE' , 4 ) ;
9742          END IF;
9743 
9744          oe_delayed_requests_pvt.log_request(
9745                                              p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
9746                                              p_entity_id              => p_x_line_rec.line_id,
9747                                              p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
9748                                              p_requesting_entity_id   => p_x_line_rec.line_id,
9749                                              p_request_type           => OE_GLOBALS.G_CASCADE_SHIP_SET_ATTR,
9750                                              p_param1                 => p_x_line_rec.header_id,
9751                                              p_param2                 => p_x_line_rec.ship_set_id,
9752                                              p_param3                 => p_x_line_rec.shipping_method_code,
9753                                              x_return_status          => x_return_status);
9754 
9755 
9756       END IF;
9757 
9758       IF l_debug_level  > 0 THEN
9759          oe_debug_pub.add(  'SCHEDULING NOT REQUIRED' , 1 ) ;
9760       END IF;
9761       RETURN;
9762    END IF;
9763 
9764    IF l_debug_level  > 0 THEN
9765       oe_debug_pub.add(  'SCHEDULING LINE_ACTION :' || L_LINE_ACTION , 1 ) ;
9766    END IF;
9767 
9768    -- 5223953 - Versioning and reason not required for system-driven changes.
9769    IF p_caller = SCH_EXTERNAL THEN
9770       p_x_line_rec.change_reason := 'SYSTEM';
9771    END IF;
9772 
9773    ------------ need scheduling and resource flag done ----------
9774 
9775    <<RE_VALIDATE>>
9776 
9777    IF l_debug_level  > 0 THEN
9778       oe_debug_pub.add(  'CALLING OE_SCHEDULE_UTIL.VALIDATE LINE' , 1 ) ;
9779    END IF;
9780 
9781    Validate_Line(p_line_rec      => p_x_line_rec,
9782                  p_old_line_rec  => l_old_line_rec,
9783                  p_sch_action    => l_line_action,
9784                  p_caller        => SCH_INTERNAL,
9785                  x_return_status => x_return_status);
9786 
9787    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9788       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9789    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
9790 
9791       -- When header preference is set and if line is failing
9792       -- in validation clear the line from set and do not fail the call
9793       -- so that line can be saved. -- 2404401.
9794       IF (p_x_line_rec.ship_set_id is not null
9795           OR p_x_line_rec.arrival_set_id is not null)
9796          AND p_x_line_rec.operation = oe_globals.g_opr_create
9797          AND (p_x_line_rec.top_model_line_id = p_x_line_rec.line_id
9798               OR   p_x_line_rec.top_model_line_id IS NULL) THEN
9799 
9800          OE_Order_Cache.Load_Order_Header(p_x_line_rec.header_id);
9801          IF OE_ORDER_CACHE.g_header_rec.customer_preference_set_code = 'SHIP' OR
9802             OE_ORDER_CACHE.g_header_rec.customer_preference_set_code = 'ARRIVAL'
9803          THEN
9804 
9805             IF l_debug_level  > 0 THEN
9806                oe_debug_pub.add(  'VALIDATION FAILED FOR SET' , 2 ) ;
9807             END IF;
9808             BEGIN
9809 
9810                OE_ORDER_UTIL.Update_Global_Picture
9811                   (p_Upd_New_Rec_If_Exists => False,
9812                    p_old_line_rec  => p_old_line_rec,
9813                    p_line_rec      => p_x_line_rec,
9814                    p_line_id       => p_x_line_rec.line_id,
9815                    x_index         => l_index,
9816                    x_return_status => x_return_status);
9817 
9818                IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9819                   IF l_debug_level  > 0 THEN
9820                      oe_debug_pub.add(  'After update global pic: UNEXP ERRORED OUT' , 1 ) ;
9821                   END IF;
9822                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9823                ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
9824                   IF l_debug_level  > 0 THEN
9825                      oe_debug_pub.add(  'After update global pic : ERRORED OUT' , 1 ) ;
9826                   END IF;
9827                   RAISE FND_API.G_EXC_ERROR;
9828                END IF;
9829 
9830                IF l_index is not NULL THEN
9831                   --update Global Picture directly
9832                   OE_ORDER_UTIL.g_line_tbl(l_index).ship_set_id := Null;
9833                   OE_ORDER_UTIL.g_line_tbl(l_index).arrival_set_id  := Null;
9834                   OE_ORDER_UTIL.g_line_tbl(l_index).ship_set := Null;
9835                   OE_ORDER_UTIL.g_line_tbl(l_index).arrival_set := Null;
9836                   IF l_debug_level  > 0 THEN
9837                      oe_debug_pub.add(  'GLOBAL SHIP_set is : ' || OE_ORDER_UTIL.G_LINE_TBL (L_INDEX).line_id , 1 ) ;
9838                   END IF;
9839                END IF; /*l_index is not null check*/
9840 
9841                Update oe_order_lines_all
9842                   Set arrival_set_id = Null,
9843                   ship_set_id    = Null
9844                   Where Line_id = p_x_line_rec.line_id;
9845 
9846                p_x_line_rec.arrival_set_id := Null;
9847                p_x_line_rec.ship_set_id := Null;
9848 
9849                -- Change the status since we do not want to fail
9850                -- line to get saved .
9851                x_return_status := FND_API.G_RET_STS_SUCCESS;
9852 
9853                IF nvl(p_x_line_rec.override_atp_date_code,'N') = 'Y'
9854                   OR p_x_line_rec.schedule_ship_date is not null
9855                   OR p_x_line_rec.schedule_arrival_date is not null THEN
9856 
9857                   IF l_debug_level  > 0 THEN
9858                      oe_debug_pub.add(  'VALIDATION FAILED re-validate' , 2 ) ;
9859                   END IF;
9860 
9861                   GOTO RE_VALIDATE;
9862 
9863                END IF;
9864             END;
9865          END IF; -- preference
9866 
9867       --12888703
9868       ELSIF (p_x_line_rec.ship_set_id is not null
9869           OR p_x_line_rec.arrival_set_id is not null)
9870          AND p_x_line_rec.operation = oe_globals.g_opr_update
9871          AND NVL(oe_sys_parameters.Value('ONT_AUTO_SCH_SETS',p_x_line_rec.org_id),'Y') = 'N' THEN
9872          x_return_status := FND_API.G_RET_STS_SUCCESS;
9873          oe_schedule_util.OESCH_SET_SCHEDULING := 'N';
9874         IF l_debug_level  > 0 THEN
9875             oe_debug_pub.add(  'Add Line to set without schedule as Auto Schedule Set is set to No' , 1 ) ;
9876          END IF;
9877 
9878       END IF; -- ship set id
9879       IF l_auto_sch = 'Y' THEN
9880          IF l_debug_level  > 0 THEN
9881             oe_debug_pub.add(  'VALIDATION FAILED WHILE AUTO SCH' , 1 ) ;
9882          END IF;
9883          -- Change the status since we do not want to fail
9884          -- line to get saved .
9885          x_return_status := FND_API.G_RET_STS_SUCCESS;
9886       END IF;
9887       RETURN;
9888    END IF;
9889 
9890 
9891    ------------ here starts the branch based on groups ----------
9892    -- If rescheduling happening due change of the inventory item,
9893    -- if it is belong to group undemand the item before logging a
9894    -- request.After logging a request, we will not have visibility
9895    -- to old data.
9896 
9897    IF  l_line_action = OESCH_ACT_RESCHEDULE
9898       AND NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
9899                                p_old_line_rec.inventory_item_id)
9900       AND (p_x_line_rec.ship_set_id is NOT NULL OR
9901            p_x_line_rec.arrival_set_id is NOT NULL OR
9902               (nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N' AND
9903                p_x_line_rec.top_model_line_id is NOT NULL))
9904    THEN
9905 
9906       IF l_old_line_rec.reserved_quantity is not null
9907       THEN
9908          -- Call INV API to delete the reservations on  the line.
9909          -- shipping_interfaced_flag
9910          Unreserve_Line
9911          ( p_line_rec               => l_old_line_rec
9912            , p_quantity_to_unreserve  => l_old_line_rec.reserved_quantity
9913            , p_quantity2_to_unreserve  => l_old_line_rec.reserved_quantity2 -- INVCONV
9914            , x_return_status          => x_return_status);
9915 
9916          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9917             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9918          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
9919             RAISE FND_API.G_EXC_ERROR;
9920          END IF;
9921       END IF;
9922 
9923       Action_Undemand(p_old_line_rec   => l_old_line_rec,
9924                       x_return_status  => x_return_status);
9925 
9926 
9927    END IF; -- Undemand.
9928 
9929    -- Breaking the link. Since, if line belong to set may not require to
9930    -- log a sets request, might require group_scheduling since they are belong
9931    -- to Model.
9932 
9933    IF nvl(p_x_line_rec.override_atp_date_code,'N') = 'N' AND
9934       nvl(p_old_line_rec.override_atp_date_code,'N') = 'Y'
9935       AND p_x_line_rec.top_model_line_id is not null THEN
9936 
9937       Cascade_override_atp(p_line_rec => p_x_line_rec);
9938 
9939    END IF;
9940 
9941    IF (p_x_line_rec.ship_set_id is NOT NULL OR
9942        p_x_line_rec.arrival_set_id is NOT NULL)
9943       AND p_x_line_rec.ordered_quantity > 0
9944       AND l_line_action <> OESCH_ACT_RESERVE  THEN -- remnant??
9945 
9946       /* 4241385 start of auto schedule sets ER changes */
9947 
9948        IF l_debug_level  > 0 THEN
9949          oe_debug_pub.add(  'Get the set details' , 1 ) ;
9950        END IF;
9951            IF p_x_line_rec.ship_set_id is NOT NULL THEN
9952 
9953 	      l_set_id:= p_x_line_rec.ship_set_id ;
9954 	   ELSIF p_x_line_rec.arrival_set_id is NOT NULL THEN
9955 
9956 	      l_set_id:= p_x_line_rec.arrival_set_id ;
9957 	   END IF ;
9958 
9959            get_set_details(p_set_id =>l_set_id
9960 	                 ,x_set_exists=>l_set_exists
9961 			 ,x_set_scheduled=>l_set_scheduled);
9962            l_auto_schedule_sets := NVL(oe_sys_parameters.Value('ONT_AUTO_SCH_SETS',p_x_line_rec.org_id),'Y');
9963            IF l_set_exists THEN
9964 
9965 	      IF l_set_scheduled
9966 	      OR Get_Auto_Sch_Flag='Y'
9967 	      OR l_auto_schedule_sets='Y'
9968 	      OR (p_x_line_rec.schedule_ship_Date IS NOT NULL )
9969 	      OR (p_x_line_rec.schedule_arrival_Date IS NOT NULL )
9970 	      OR (p_x_line_rec.reserved_quantity>0 )
9971 	      OR (P_X_LINE_REC.SCHEDULE_ACTION_CODE = 'SCHEDULE')
9972               OR (NVL(P_X_LINE_REC.BOOKED_FLAG,'N')='Y' --10088102
9973 	        AND l_auto_schedule_sets='Y') -- 12642790
9974 	      THEN
9975 		  IF l_debug_level  > 0 THEN
9976                     oe_debug_pub.add(  'Set exists and is scheduled.' , 1 ) ;
9977                   END IF;
9978 	             Log_Set_Request
9979 		       (p_line_rec      => p_x_line_rec,
9980 		        p_old_line_rec  => l_old_line_rec,
9981 		        p_sch_action    => l_line_action,
9982 		        p_caller        => p_caller,
9983 		        x_return_status => x_return_status);
9984 		     IF l_auto_schedule_sets = 'N' THEN --13958294
9985                      --10064449: Delayed request need to be executed now.
9986                      OE_DELAYED_REQUESTS_PVT.Process_Request_for_Entity
9987                         (p_entity_code   => OE_GLOBALS.G_ENTITY_ALL --OE_GLOBALS.G_ENTITY_LINE  -- 12642790
9988                         ,p_delete        => FND_API.G_TRUE
9989                         ,x_return_status => x_return_status );
9990 
9991 
9992                      IF l_debug_level  > 0 THEN
9993                         oe_debug_pub.add(  'After calling request for entity ' || X_RETURN_STATUS , 1 ) ;
9994                      END IF;
9995 
9996                      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
9997                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9998                      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
9999                         RAISE FND_API.G_EXC_ERROR;
10000                      END IF;
10001 
10002                      OE_Order_PVT.Process_Requests_And_Notify
10003                        ( x_return_status       => x_return_status);
10004 
10005                      IF l_debug_level  > 0 THEN
10006                         oe_debug_pub.add(  '1 RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
10007                      END IF;
10008 
10009                      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
10010                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10011                      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
10012                         RAISE FND_API.G_EXC_ERROR;
10013                      END IF;
10014 
10015 
10016                     --End 10064449
10017 		  END IF ; --13958294
10018 	          IF l_debug_level  > 0 THEN
10019 		    oe_debug_pub.add(  'AFTER LOGGING SETS DELAYED REQUEST ' || X_RETURN_STATUS , 1 ) ;
10020 	          END IF;
10021 	      ELSE --set is not scheduled. Do not log request.
10022 		  IF l_debug_level  > 0 THEN
10023                     oe_debug_pub.add(  'scheduling is not required. So Skip' , 1 ) ;
10024                   END IF;
10025 	      END IF ; --set scheduled.
10026 
10027 	   ELSE --set does not exist. That means new set.
10028               IF l_debug_level  > 0 THEN
10029                 oe_debug_pub.add('set does not exist.get the system parameter value' ,1 ) ;
10030               END IF;
10031 
10032 	      IF l_auto_schedule_sets = 'Y' --parameter is yes
10033               OR Get_Auto_Sch_Flag='Y'
10034 	      OR (p_x_line_rec.schedule_ship_Date IS NOT NULL )
10035 	      OR (p_x_line_rec.schedule_arrival_Date  IS NOT NULL )
10036 	      OR (p_x_line_rec.reserved_quantity>0 )
10037 	      OR (P_X_LINE_REC.SCHEDULE_ACTION_CODE = 'SCHEDULE')
10038 	      THEN --4241385 tools ->Auto schedule is yes
10039 	          IF l_debug_level  > 0 THEN
10040 		    oe_debug_pub.add(  'NEW SCHEDULING SETS RELATED' , 1 ) ;
10041 		  END IF;
10042 		     Log_Set_Request
10043 		      (p_line_rec      => p_x_line_rec,
10044 		       p_old_line_rec  => l_old_line_rec,
10045 		       p_sch_action    => l_line_action,
10046 		       p_caller        => p_caller,
10047 		       x_return_status => x_return_status);
10048 		  IF l_debug_level  > 0 THEN
10049 		    oe_debug_pub.add(  'AFTER LOGGING SETS DELAYED REQUEST ' || X_RETURN_STATUS , 1 ) ;
10050 		  END IF;
10051 	       ELSE --auto schedule is not required.
10052 		  IF l_debug_level  > 0 THEN
10053 		    oe_debug_pub.add(  'Need not log delayed request.' ) ;
10054 		  END IF;
10055 	       END IF ;
10056 
10057 	   END IF ;--set exists check.
10058 /*End of Auto Schedule Sets ER Changes */
10059 
10060    -- ELSIF nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N'
10061    --Begin bug fix for bug#6153528
10062     ELSIF ( nvl(p_x_line_rec.model_remnant_flag, 'N') = 'N' or
10063           ( nvl(p_x_line_rec.model_remnant_flag, 'N') = 'Y' and p_x_line_rec.ato_line_id is not null)
10064           )
10065    --End bug fix for bug#6153528
10066          AND   p_x_line_rec.top_model_line_id is NOT NULL
10067          AND   l_line_action <> OESCH_ACT_RESERVE
10068    THEN
10069 
10070 
10071       IF l_debug_level  > 0 THEN
10072          oe_debug_pub.add(  'NEW SCHEDULING CONFIG RELATED' , 1 ) ;
10073       END IF;
10074 
10075       OE_CONFIG_SCHEDULE_PVT.Log_Config_Sch_Request
10076          ( p_line_rec       => p_x_line_rec
10077            ,p_old_line_rec   => l_old_line_rec
10078            ,p_sch_action     => l_line_action
10079            ,p_caller         => p_caller
10080            ,x_return_status  => x_return_status);
10081 
10082    ELSE
10083       IF l_debug_level  > 0 THEN
10084          oe_debug_pub.add(  'NEW SCHEDULING STANDARD LINE ' , 1 ) ;
10085       END IF;
10086 
10087       Process_request( p_old_line_rec   => l_old_line_rec,
10088                        p_caller         => p_caller,
10089                        p_x_line_rec     => p_x_line_rec,
10090                        p_sch_action     => l_line_action,
10091                        x_return_status  => x_return_status);
10092    END IF; -- Group, parent and independent
10093 
10094 
10095    IF l_debug_level  > 0 THEN
10096       oe_debug_pub.add(  'SCH SHIP DATE ' || P_X_LINE_REC.SCHEDULE_SHIP_DATE , 5 ) ;
10097    END IF;
10098    IF l_debug_level  > 0 THEN
10099       oe_debug_pub.add(  'SCH ARR DATE ' || P_X_LINE_REC.SCHEDULE_ARRIVAL_DATE , 5 ) ;
10100    END IF;
10101    IF l_debug_level  > 0 THEN
10102       oe_debug_pub.add(  'SHIP_FROM ' || P_X_LINE_REC.SHIP_FROM_ORG_ID , 5 ) ;
10103    END IF;
10104 
10105    Print_Time('Exiting schedule_line');
10106    IF l_debug_level > 0 THEN
10107      oe_debug_pub.add('Exiting schedule_line',0.5);  -- Added debug for bug 13435459
10108    END IF;
10109 EXCEPTION
10110    WHEN FND_API.G_EXC_ERROR THEN
10111 
10112       IF l_debug_level  > 0 THEN
10113          oe_debug_pub.add(  'G_EXC_ERROR IN SCHEDULE_LINE' , 1 ) ;
10114       END IF;
10115       x_return_status := FND_API.G_RET_STS_ERROR;
10116 
10117    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10118 
10119       IF l_debug_level  > 0 THEN
10120          oe_debug_pub.add(  'G_EXC__UNEXP_ERROR IN SCHEDULE_LINE' , 1 ) ;
10121       END IF;
10122       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10123 
10124    WHEN OTHERS THEN
10125 
10126       IF l_debug_level  > 0 THEN
10127          oe_debug_pub.add(  'OTHERS IN SCHEDULE_LINE'|| SQLERRM , 1 ) ;
10128       END IF;
10129       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10130 
10131       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
10132       THEN
10133          OE_MSG_PUB.Add_Exc_Msg
10134             (   G_PKG_NAME
10135                 ,'Schedule_line');
10136       END IF;
10137 
10138 END Schedule_line;
10139 
10140 
10141 /*---------------------------------------------------------------------
10142 Function Name  : Need_Scheduling
10143 Description    : This API will return to the calling process if scheduling
10144                  needs to be performed on the line or not.
10145 
10146 The output param x_line_action,
10147   can be used as a guideline to decide
10148   the scheduling action on a line.
10149   NOTE: 1) The value of x_line_action should be ignored if this
10150            function returns false.
10151         2) More than 1 attributes can have a change, however this
10152            procedure returns TRUE as soon as first in the code
10153            is found to be changed.
10154 
10155 For a line which is not scheduled, entering following
10156 attributes will result in scheduling:
10157   schedule_ship_date
10158   schedue_arrival_date
10159   reserved_quantity
10160 
10161 
10162 Cases which need more explaination have comments inside code.
10163 -- We do not schedule service lines.
10164 -- We do not schedule OTA lines. (Changed as per bug 7392538)
10165 -- we will not do scheduling for lines with source_type=EXTERNAL
10166 -- We should not perform scheduling if the line is already scheduled
10167 --------------------------------------------------------------------- */
10168 FUNCTION Need_Scheduling
10169 (p_line_rec           IN OE_ORDER_PUB.line_rec_type,
10170  p_old_line_rec       IN OE_ORDER_PUB.line_rec_type,
10171 x_line_action OUT NOCOPY VARCHAR2,
10172 
10173 x_auto_sch OUT NOCOPY VARCHAR2
10174 
10175  )
10176 RETURN BOOLEAN
10177 IS
10178   l_order_date_type_code   VARCHAR2(30):='';
10179   l_request_date_flag      VARCHAR2(1) := 'Y';
10180   l_shipping_method_flag   VARCHAR2(1) := 'Y';
10181 /* 6663462 Added the following 3 variables for the Delayed Scheduling feature */
10182   l_delayed_schedule       VARCHAR2(1) := NVL(fnd_profile.value('ONT_DELAY_SCHEDULING'),'N') ;
10183   l_index                  NUMBER := 0;
10184   l_return_status          VARCHAR2(1);
10185   l_auto_schedule_sets     VARCHAR2(1):='Y'; --4241385
10186   l_set_exists             BOOLEAN ;	     --4241385
10187   l_set_scheduled          BOOLEAN ;	     --4241385
10188 
10189 
10190 
10191 --
10192 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
10193 --
10194 BEGIN
10195  --4241385. get the sys param value.
10196  l_auto_schedule_sets := nvl(oe_sys_parameters.Value('ONT_AUTO_SCH_SETS',p_line_rec.org_id),'Y');
10197    IF l_debug_level  > 0 THEN
10198        oe_debug_pub.add(  'ENTERING NEED SCHEDULING' , 1 ) ;
10199    END IF;
10200 
10201    x_line_action := Null;
10202    x_auto_sch    := 'N';
10203 
10204    -- QUOTING changes - return FALSE for lines in negotiation phase.
10205    IF p_line_rec.transaction_phase_code = 'N' THEN
10206        RETURN FALSE;
10207    END IF;
10208 
10209    IF (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_SERVICE
10210       OR NVL(p_line_rec.subscription_enable_flag,'N')='Y' ) THEN --sol_ord_er #16014165
10211        RETURN FALSE;
10212    END IF;
10213 
10214    IF l_debug_level  > 0 THEN
10215        oe_debug_pub.add(  'N1 NOT A SERVICE LINE' , 1 ) ;
10216    END IF;
10217    IF p_line_rec.line_category_code =  'RETURN' THEN
10218        RETURN FALSE;
10219    END IF;
10220 
10221    IF l_debug_level  > 0 THEN
10222        oe_debug_pub.add(  'N01 NOT A RETURN LINE' , 1 ) ;
10223    END IF;
10224  -- bug 7392538 changes start here
10225  -- Issue Discription
10226  -- The function call to OE_OTA_UTIL.Is_OTA_Line has been
10227  -- commented out due to bug 7385681. The issue here was that
10228  -- if a class line had UOM in ENR or EVT then this function
10229  -- call did not allow the class to be scheduled. Hence the
10230  -- workflow of any included item inside this class always left
10231  -- the LINE_SCHEDULING activity in the workflow with the result
10232  -- as INCOMPLETE and got stuck.
10233  -- The new behavior replicated the manual scheduling behavior.
10234 /*
10235    IF OE_OTA_UTIL.Is_OTA_Line(p_line_rec.order_quantity_uom)
10236    THEN
10237       RETURN FALSE;
10238    END IF;
10239 
10240    IF l_debug_level  > 0 THEN
10241        oe_debug_pub.add(  'N2 NOT A OTA ITEM' , 1 ) ;
10242    END IF;
10243  */
10244 
10245    -- If a config item is deleted, we do not need to call scheduling.
10246    -- Config Item can be deleted only through delink API. While delinking,
10247    -- CTO team takes care of updating the demand picture for the
10248    -- configuration. If a config item is getting created,
10249    -- we do not need to call scheduling.
10250 
10251    IF p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CONFIG AND
10252       (p_line_rec.operation = OE_GLOBALS.G_OPR_DELETE OR
10253        p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE )
10254    THEN
10255       RETURN FALSE;
10256    END IF;
10257 
10258    IF l_debug_level  > 0 THEN
10259        oe_debug_pub.add(  'N3 NOT CONFIG WITH DELETE AND CREATE OPR' , 1 ) ;
10260    END IF;
10261 
10262    -- Check to see if this line is a new line which has been created
10263    -- due to the split action. If yes, then do not schedule it, since we
10264    -- have already scheduled the line before.
10265 
10266    IF (p_line_rec.split_from_line_id is not null) AND
10267       (p_line_rec.split_from_line_id <> FND_API.G_MISS_NUM) AND
10268       (p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE)
10269    THEN
10270       IF l_debug_level  > 0 THEN
10271           oe_debug_pub.add(  'THIS IS A NEW LINE CREATED THRU SPLIT' , 1 ) ;
10272       END IF;
10273       RETURN FALSE;
10274    END IF;
10275 
10276    IF l_debug_level  > 0 THEN
10277        oe_debug_pub.add(  'N4 NOT A SPLIT LINE' , 1 ) ;
10278    END IF;
10279 
10280    -- Check to see if this line is the one which is getting split.
10281    -- If it is, then return FALSE, since this line is already rescheduled.
10282    IF (p_line_rec.split_action_code = 'SPLIT') THEN
10283        IF  (p_line_rec.schedule_status_code is not null) AND
10284            (p_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE)
10285        THEN
10286            IF l_debug_level  > 0 THEN
10287                oe_debug_pub.add(  'THIS LINE IS BEING SPLIT' , 1 ) ;
10288            END IF;
10289            RETURN FALSE;
10290        END IF;
10291    END IF;
10292 
10293 /*
10294    IF (p_line_rec.split_action_code = 'SPLIT') THEN
10295       oe_debug_pub.add('This is a split action',1);
10296       RETURN FALSE;
10297    END IF;
10298 */
10299    IF l_debug_level  > 0 THEN
10300        oe_debug_pub.add(  'N5 NOT A SPLIT PARENT' , 1 ) ;
10301    END IF;
10302 
10303 
10304    IF  p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_EXTERNAL
10305    AND p_line_rec.schedule_status_code is null
10306    THEN
10307         IF l_debug_level  > 0 THEN
10308             oe_debug_pub.add(  'UNSCHEDULED EXTERNAL LINE' , 1 ) ;
10309         END IF;
10310         RETURN FALSE;
10311    END IF;
10312 
10313    IF l_debug_level  > 0 THEN
10314        oe_debug_pub.add(  'N6 NOT A DROP SHIP LINE' , 1 ) ;
10315    END IF;
10316 
10317    If (p_line_rec.schedule_status_code is null) AND
10318       ((p_line_rec.schedule_action_code = OESCH_ACT_UNSCHEDULE) OR
10319       (p_line_rec.schedule_action_code = OESCH_ACT_UNDEMAND) OR
10320       (p_line_rec.schedule_action_code = OESCH_ACT_UNRESERVE))
10321    THEN
10322        IF l_debug_level  > 0 THEN
10323            oe_debug_pub.add(  'INVALID SCH ACTION ' , 1 ) ;
10324        END IF;
10325        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
10326        OE_MSG_PUB.Add;
10327        RETURN FALSE;
10328    END IF;
10329 
10330    IF l_debug_level  > 0 THEN
10331        oe_debug_pub.add(  'N7 NOT AN INVALID SCH ACTION' , 1 ) ;
10332    END IF;
10333 
10334    x_line_action := OESCH_ACT_SCHEDULE;
10335 
10336    IF p_line_rec.schedule_status_code is null
10337    THEN
10338 
10339      IF  p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_INTERNAL
10340      AND p_old_line_rec.source_type_code =  OE_GLOBALS.G_SOURCE_EXTERNAL
10341      AND p_line_rec.schedule_ship_date IS NOT NULL THEN
10342 
10343         IF l_debug_level  > 0 THEN
10344             oe_debug_pub.add(  'EXTERNAL -> INTERNAL WITH SHIP DATE' , 4 ) ;
10345         END IF;
10346         RETURN TRUE;
10347 
10348      END IF;
10349 
10350 
10351      IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
10352                              p_old_line_rec.schedule_ship_date)
10353      THEN
10354         IF l_debug_level  > 0 THEN
10355             oe_debug_pub.add(  'SCH_SHIP_DATE IS CHANGED , SCHEDULE' , 4 ) ;
10356         END IF;
10357         RETURN TRUE;
10358      END IF;
10359 
10360      IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
10361                              p_old_line_rec.schedule_arrival_date)
10362      THEN
10363         IF l_debug_level  > 0 THEN
10364             oe_debug_pub.add(  'SCH_ARR_DATE IS CHANGED , SCHEDULE' , 4 ) ;
10365         END IF;
10366         RETURN TRUE;
10367      END IF;
10368      --14634073: Customer has changed request date after entering schedule ship/Arrival date
10369      IF (p_line_rec.schedule_arrival_date IS NOT NULL OR
10370            p_line_rec.schedule_ship_date IS NOT NULL) AND
10371         NOT OE_GLOBALS.Equal(p_line_rec.request_date,
10372                             p_old_line_rec.request_date) THEN
10373         IF l_debug_level  > 0 THEN
10374             oe_debug_pub.add(  'REQUEST DATE IS ALSO CHANGED , UPDATE SCHEDULE' , 4 ) ;
10375         END IF;
10376         RETURN TRUE;
10377      END IF;
10378 
10379      IF l_debug_level  > 0 THEN
10380          oe_debug_pub.add(  'NEW RESERVED QTY' || P_LINE_REC.RESERVED_QUANTITY ) ;
10381      END IF;
10382      IF l_debug_level  > 0 THEN
10383          oe_debug_pub.add(  'OLD RESERVED QTY' || P_OLD_LINE_REC.RESERVED_QUANTITY ) ;
10384      END IF;
10385 
10386      IF NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
10387                              p_old_line_rec.reserved_quantity)
10388      THEN
10389         IF l_debug_level  > 0 THEN
10390             oe_debug_pub.add(  'RESERVED QTY ENTERED , SCHEDULE' , 4 ) ;
10391         END IF;
10392         RETURN TRUE;
10393      END IF;
10394 
10395      IF  NOT OE_GLOBALS.Equal(p_line_rec.ship_set_id,
10396                               p_old_line_rec.ship_set_id)
10397      AND p_line_rec.ship_set_id IS NOT NULL
10398      THEN
10399         --4241385
10400         get_set_details(p_set_id =>p_line_rec.ship_set_id
10401 	                 ,x_set_exists=>l_set_exists
10402 			 ,x_set_scheduled=>l_set_scheduled);
10403 
10404         IF l_auto_schedule_sets = 'Y'
10405 	OR l_set_scheduled
10406         OR (NVL(p_line_rec.booked_flag,'N')='Y'  AND  l_auto_schedule_sets = 'Y') THEN --10088102 --12888703
10407         --4241385
10408 	    IF l_debug_level  > 0 THEN
10409               oe_debug_pub.add(  'LINE IS BEING ADDED TO SHIP SET' , 4 ) ;
10410             END IF;
10411             RETURN TRUE;
10412 	END IF ;  --4241385
10413      END IF;
10414 
10415      IF  NOT OE_GLOBALS.Equal(p_line_rec.arrival_Set_id,
10416                               p_old_line_rec.arrival_Set_id)
10417      AND  p_line_rec.arrival_set_id IS NOT NULL
10418      THEN
10419          --4241385
10420         get_set_details(p_set_id =>p_line_rec.arrival_set_id
10421 	                 ,x_set_exists=>l_set_exists
10422 			 ,x_set_scheduled=>l_set_scheduled);
10423 
10424         IF l_auto_schedule_sets = 'Y'
10425 	   OR l_set_scheduled
10426             OR (NVL(p_line_rec.booked_flag,'N')='Y' AND  l_auto_schedule_sets = 'Y') THEN --10088102 --12888703
10427          --4241385
10428             IF l_debug_level  > 0 THEN
10429               oe_debug_pub.add(  'LINE IS BEING ADDED TO ARRIVAL SET' , 4 ) ;
10430             END IF;
10431             RETURN TRUE;
10432 	END IF ;  --4241385
10433      END IF;
10434 
10435   END IF;
10436 
10437 
10438    IF l_debug_level  > 0 THEN
10439        oe_debug_pub.add(p_line_rec.booked_flag,1);
10440        oe_debug_pub.add(  'N8' , 1 ) ;
10441    END IF;
10442 
10443    -- QUOTING changes - trigger auto-scheduling if call is from
10444    -- complete negotiation
10445    IF ((p_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
10446         OR OE_Quote_Util.G_COMPLETE_NEG = 'Y')
10447        AND OESCH_AUTO_SCH_FLAG = 'Y') THEN
10448 
10449          IF (p_line_rec.top_model_line_id is null OR
10450              p_line_rec.top_model_line_id = FND_API.G_MISS_NUM) AND
10451              p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_STANDARD
10452          THEN
10453             IF l_debug_level  > 0 THEN
10454                 oe_debug_pub.add(  'AUTO SCHEDULE IS TRUE' , 4 ) ;
10455                 oe_debug_pub.add(  '6663462 : Delay Scheduling : ' || l_delayed_schedule , 4 ) ;
10456 
10457             END IF;
10458             --8728176 : log request if OM: Bypass ATP is set to 'No'
10459             IF l_delayed_schedule = 'N' OR
10460                NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') ='Y' THEN
10461               x_auto_sch := 'Y';
10462               RETURN TRUE;
10463             ELSE
10464                 OE_delayed_requests_Pvt.log_request(
10465                  p_entity_code            => OE_GLOBALS.G_ENTITY_ALL,
10466                  p_entity_id              => p_line_rec.header_id,
10467                  p_requesting_entity_code => OE_GLOBALS.G_ENTITY_ALL,
10468                  p_requesting_entity_id   => p_line_rec.header_id,
10469                  p_request_type           => OE_GLOBALS.G_DELAYED_SCHEDULE,
10470                  x_return_status          => l_return_status);
10471                  l_index := OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl.count;
10472                  OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl(l_index+1) := p_line_rec;
10473                  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 ) ;
10474             END IF;
10475 
10476          END IF;
10477    END IF;
10478 
10479    IF l_debug_level  > 0 THEN
10480        oe_debug_pub.add(  'N9 AFTER AUTO SCHEDULE CHECK' , 1 ) ;
10481    END IF;
10482 
10483    IF  p_line_rec.schedule_status_code is NULL AND
10484        p_line_rec.schedule_action_code is NULL AND
10485        OESCH_AUTO_SCH_FLAG = 'N'
10486    THEN
10487       x_line_action := Null;
10488       RETURN FALSE;
10489    END IF;
10490 
10491    IF l_debug_level  > 0 THEN
10492        oe_debug_pub.add(  'N10' , 1 ) ;
10493    END IF;
10494 
10495    IF p_line_rec.schedule_status_code = OESCH_STATUS_SCHEDULED AND
10496       p_line_rec.schedule_action_code = OESCH_ACT_SCHEDULE
10497    THEN
10498       x_line_action := Null;
10499       RETURN FALSE;
10500    END IF;
10501 
10502    IF l_debug_level  > 0 THEN
10503        oe_debug_pub.add(  'N11' , 1 ) ;
10504    END IF;
10505 
10506    IF (p_line_rec.schedule_action_code is not null)
10507    THEN
10508       x_line_action := p_line_rec.schedule_action_code;
10509       IF l_debug_level  > 0 THEN
10510           oe_debug_pub.add(  'SCH ACTION CODE '|| P_LINE_REC.SCHEDULE_ACTION_CODE , 4 ) ;
10511       END IF;
10512       RETURN TRUE;
10513    END IF;
10514 
10515    IF l_debug_level  > 0 THEN
10516        oe_debug_pub.add(  'N12 AFTER ACTION CHECK' , 1 ) ;
10517    END IF;
10518    -------------- Starts already scheduled line part ------
10519 
10520    -- We should avoid calling scheduling when user changes values
10521    -- of the below attributes on the unscheduled lines. The code
10522    -- below is valid only for scheduled lines.
10523 
10524   IF p_line_rec.schedule_status_code is NOT NULL THEN
10525 
10526     x_line_action := OESCH_ACT_RESCHEDULE;
10527 
10528       -- Bug 12735226
10529       IF  p_line_rec.source_type_code =  OE_GLOBALS.G_SOURCE_EXTERNAL
10530       THEN
10531         x_line_action := OESCH_ACT_UNSCHEDULE;
10532         IF l_debug_level  > 0 THEN
10533            oe_debug_pub.add(  'Source type  CHANGED , UNSCHEDULE' , 4 ) ;
10534         END IF;
10535         RETURN TRUE;
10536       END IF;
10537       -- Bug 12735226
10538 
10539     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
10540                             p_old_line_rec.schedule_ship_date)
10541     THEN
10542       -- On a scheduled line, if user is clearing schedule_ship_date
10543       -- treat is as inschedule.
10544       IF  p_line_rec.schedule_ship_date is null
10545 --      AND p_line_rec.ship_set_id is null
10546 --      AND p_line_rec.arrival_set_id is null
10547       THEN
10548          x_line_action := OESCH_ACT_UNSCHEDULE;
10549       END IF;
10550       IF l_debug_level  > 0 THEN
10551           oe_debug_pub.add(  'SCH_SHIP_DATE CHANGED , RESCHEDULE' , 4 ) ;
10552       END IF;
10553       RETURN TRUE;
10554     END IF;
10555 
10556     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
10557                             p_old_line_rec.schedule_arrival_date)
10558     THEN
10559       IF l_debug_level  > 0 THEN
10560           oe_debug_pub.add(  'SCH_ARR_DATE CHANGED , RESCHEDULE' , 4 ) ;
10561       END IF;
10562       IF p_line_rec.schedule_arrival_date is null
10563       AND p_line_rec.ship_set_id is null
10564       AND p_line_rec.arrival_set_id is null THEN
10565          x_line_action := OESCH_ACT_UNSCHEDULE;
10566       END IF;
10567       RETURN TRUE;
10568     END IF;
10569 
10570     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_from_org_id,
10571                             p_old_line_rec.ship_from_org_id)
10572     THEN
10573        IF l_debug_level  > 0 THEN
10574            oe_debug_pub.add(  'SHIP_FROM_ORG CHANGED , RESCHEDULE' , 4 ) ;
10575        END IF;
10576        RETURN TRUE;
10577     END IF;
10578 
10579 
10580     IF NOT OE_GLOBALS.Equal(p_line_rec.ordered_quantity,
10581                             p_old_line_rec.ordered_quantity)
10582     THEN
10583        IF l_debug_level  > 0 THEN
10584            oe_debug_pub.add(  'ORD QTY CHANGED , RESCHEDULE' , 4 ) ;
10585        END IF;
10586        RETURN TRUE;
10587     END IF;
10588 
10589 --start bug 16782621
10590     IF NOT OE_GLOBALS.Equal(p_line_rec.ordered_quantity2,
10591                             p_old_line_rec.ordered_quantity2) AND ( Nvl(p_line_rec.fulfillment_base ,'P') ='S')
10592     THEN
10593        IF l_debug_level  > 0 THEN
10594            oe_debug_pub.add(  'ORD QTY2 CHANGED for FB=S, RESCHEDULE' , 4 ) ;
10595        END IF;
10596        RETURN TRUE;
10597     END IF;
10598 --end bug 16782621
10599 
10600     IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,
10601                             p_old_line_rec.order_quantity_uom)
10602     THEN
10603        IF l_debug_level  > 0 THEN
10604            oe_debug_pub.add(  'ORDER QTY UOM CHANGED , RESCHEDULE' , 4 ) ;
10605        END IF;
10606        RETURN TRUE;
10607     END IF;
10608 
10609     IF NOT OE_GLOBALS.Equal(p_line_rec.request_date,
10610                             p_old_line_rec.request_date)
10611     THEN
10612        -- Pack J
10613        --- Return True only if Request date parameter value is set to 'Yes'
10614        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
10615           l_request_date_flag :=
10616                     NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y');
10617        END IF;
10618 
10619        -- This code has been added to avoid re-scheduling the line when request date is changed
10620        -- on a scheduled and overridden line. -- 3524314
10621 
10622        IF OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
10623                            p_old_line_rec.override_atp_date_code)
10624        AND nvl(p_line_rec.override_atp_date_code,'N') = 'Y' THEN
10625           l_request_date_flag := 'N';
10626        END IF;
10627 
10628        IF l_request_date_flag = 'Y' THEN
10629 
10630           IF l_debug_level  > 0 THEN
10631              oe_debug_pub.add(  'REQ_DATE CHANGED , RESCHEDULE' , 4 ) ;
10632           END IF;
10633           RETURN TRUE;
10634        END IF;
10635 
10636     END IF;
10637 
10638     IF NOT OE_GLOBALS.Equal(p_line_rec.shipping_method_code,
10639                             p_old_line_rec.shipping_method_code)
10640     THEN
10641        -- Pack J
10642        -- Return True if Ship method parameter value set to 'Yes'.
10643        IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
10644          l_shipping_method_flag :=
10645                         NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_SHIP_METHOD_FLAG'),'Y');
10646        END IF;
10647        IF l_shipping_method_flag = 'Y' THEN
10648 
10649           IF l_debug_level  > 0 THEN
10650              oe_debug_pub.add(  'SHIPPING_METHOD CHANGED , RESCHEDULE' , 4 ) ;
10651           END IF;
10652           RETURN TRUE;
10653        END IF;
10654 
10655     END IF;
10656 
10657     IF NOT OE_GLOBALS.Equal(p_line_rec.delivery_lead_time,
10658                             p_old_line_rec.delivery_lead_time)
10659     THEN
10660       BEGIN
10661         select order_date_type_code
10662         into l_order_date_type_code
10663         from oe_order_headers_all
10664         where header_id =  p_line_rec.header_id;
10665 
10666         IF l_order_date_type_code  = 'ARRIVAL' THEN
10667            IF l_debug_level  > 0 THEN
10668                oe_debug_pub.add(  'DEL LEAD TIME CHANGED , RESCHEDULE' , 4 ) ;
10669            END IF;
10670            RETURN TRUE;
10671         END IF;
10672       EXCEPTION
10673         WHEN OTHERS THEN
10674           NULL;
10675       END;
10676     END IF;
10677 
10678     IF NOT OE_GLOBALS.Equal(p_line_rec.demand_class_code,
10679                             p_old_line_rec.demand_class_code)
10680     THEN
10681        IF l_debug_level  > 0 THEN
10682            oe_debug_pub.add(  'DEMAND_CLASS CHANGED , RESCHEDULE' , 4 ) ;
10683        END IF;
10684        RETURN TRUE;
10685     END IF;
10686 
10687     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_to_org_id,
10688                             p_old_line_rec.ship_to_org_id)
10689     THEN
10690        IF l_debug_level  > 0 THEN
10691            oe_debug_pub.add(  'SHIP_TO_ORG CHANGED , RESCHEDULE' , 4 ) ;
10692        END IF;
10693        RETURN TRUE;
10694     END IF;
10695 
10696     IF NOT OE_GLOBALS.Equal(p_line_rec.sold_to_org_id,
10697                            p_old_line_rec.sold_to_org_id)
10698     THEN
10699        IF l_debug_level  > 0 THEN
10700            oe_debug_pub.add(  'SOLD_TO_ORG CHANGED , RESCHEDULE' , 4 ) ;
10701        END IF;
10702        RETURN TRUE;
10703     END IF;
10704 
10705     IF NOT OE_GLOBALS.Equal(p_line_rec.inventory_item_id,
10706                              p_old_line_rec.inventory_item_id)
10707     THEN
10708         IF l_debug_level  > 0 THEN
10709             oe_debug_pub.add(  'INV ITEM ID CHANGED , RESCHEDULE' , 4 ) ;
10710         END IF;
10711         RETURN TRUE;
10712     END IF;
10713 
10714     IF NOT OE_GLOBALS.Equal(p_line_rec.ship_set_id,
10715                              p_old_line_rec.ship_set_id)
10716        AND p_line_rec.ship_set_id IS NOT NULL
10717     THEN
10718 
10719         IF l_debug_level  > 0 THEN
10720             oe_debug_pub.add(  'LINE IS BEING ADDED TO SHIP SET' , 4 ) ;
10721         END IF;
10722         RETURN TRUE;
10723     END IF;
10724 
10725     IF NOT OE_GLOBALS.Equal(p_line_rec.arrival_set_id,
10726                              p_old_line_rec.arrival_set_id)
10727        AND p_line_rec.arrival_set_id IS NOT NULL
10728     THEN
10729 
10730         IF l_debug_level  > 0 THEN
10731             oe_debug_pub.add(  'LINE IS BEING ADDED TO ARRIVAL SET' , 4 ) ;
10732         END IF;
10733         RETURN TRUE;
10734     END IF;
10735 
10736       -- BUG 1282873
10737     IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
10738       IF  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
10739                                p_old_line_rec.override_atp_date_code) THEN
10740          -- This line was previously scheduled with the Override ATP Flag set
10741          -- but it is now not set.  Must re-schedule the line
10742          IF l_debug_level  > 0 THEN
10743              oe_debug_pub.add(  'OVERRIDE ATP UNCHECKED , RESCHEDULE' , 1 ) ;
10744          END IF;
10745          Return TRUE;
10746       END IF;
10747     END IF;
10748      -- END 1282873
10749 
10750     -- Changing the source type on a scheduled line.
10751     -- We should unschedule the line
10752     IF p_line_rec.source_type_code = OE_GLOBALS.G_SOURCE_EXTERNAL AND
10753        NOT OE_GLOBALS.Equal(p_line_rec.source_type_code,
10754                             p_old_line_rec.source_type_code)
10755     THEN
10756         IF l_debug_level  > 0 THEN
10757             oe_debug_pub.add(  'SOURCE TYPE MADE EXTERNAL , UNSCHEDULE' , 4 ) ;
10758         END IF;
10759         x_line_action := OESCH_ACT_UNSCHEDULE;
10760         RETURN TRUE;
10761     END IF;
10762 
10763     IF NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity,
10764                             p_old_line_rec.reserved_quantity)
10765     THEN
10766         IF l_debug_level  > 0 THEN
10767             oe_debug_pub.add(  'RESERVED QTY CHANGED , RESERVE' , 4 ) ;
10768         END IF;
10769         x_line_action := OESCH_ACT_RESERVE;
10770         RETURN TRUE;
10771     END IF;
10772 
10773 --start bug 16782621
10774     IF NOT OE_GLOBALS.Equal(p_line_rec.reserved_quantity2,
10775                             p_old_line_rec.reserved_quantity2)	   AND ( Nvl(p_line_rec.fulfillment_base ,'P') ='S')
10776     THEN
10777         IF l_debug_level  > 0 THEN
10778             oe_debug_pub.add(  'RESERVED QTY2 CHANGED FOR FB= S , RESERVE' , 4 ) ;
10779         END IF;
10780         x_line_action := OESCH_ACT_RESERVE;
10781         RETURN TRUE;
10782     END IF;
10783 --end bug 16782621
10784 
10785     -- subinventory changes require only the change
10786     --reservation. If reservation does not exists for the
10787     --line no action required here.
10788 
10789     -- Do not move this code
10790     IF  NOT OE_GLOBALS.Equal(p_line_rec.subinventory,
10791                              p_old_line_rec.subinventory)
10792     AND p_old_line_rec.reserved_quantity > 0
10793     THEN
10794        IF l_debug_level  > 0 THEN
10795            oe_debug_pub.add(  'SUBINVENTORY CHANGED , RESCHEDULE' , 4 ) ;
10796        END IF;
10797        x_line_action := OESCH_ACT_RESERVE;
10798        RETURN TRUE;
10799     END IF;
10800     --4653097:Start
10801     IF  NOT OE_GLOBALS.Equal(p_line_rec.project_id,
10802                              p_old_line_rec.project_id)
10803     AND p_old_line_rec.reserved_quantity > 0
10804     THEN
10805        IF l_debug_level  > 0 THEN
10806            oe_debug_pub.add(  'PROJECT CHANGED , RESCHEDULE' , 4 ) ;
10807        END IF;
10808        x_line_action := OESCH_ACT_RESERVE;
10809        RETURN TRUE;
10810     END IF;
10811 
10812     IF  NOT OE_GLOBALS.Equal(p_line_rec.task_id,
10813                              p_old_line_rec.task_id)
10814     AND p_old_line_rec.reserved_quantity > 0
10815     THEN
10816        IF l_debug_level  > 0 THEN
10817            oe_debug_pub.add(  'TASK CHANGED , RESCHEDULE' , 4 ) ;
10818        END IF;
10819        x_line_action := OESCH_ACT_RESERVE;
10820        RETURN TRUE;
10821     END IF;
10822     --4653097 :End
10823   END IF; -- Check for schedule_status_code not NULL.
10824 
10825   IF l_debug_level  > 0 THEN
10826       oe_debug_pub.add(  'N13' , 1 ) ;
10827   END IF;
10828   RETURN FALSE;
10829 
10830 
10831 EXCEPTION
10832 
10833   WHEN OTHERS THEN
10834     x_line_action := Null;
10835 
10836     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
10837     THEN
10838       OE_MSG_PUB.Add_Exc_Msg
10839       (   G_PKG_NAME
10840       ,   'Need_Scheduling'
10841        );
10842     END IF;
10843 
10844 END Need_Scheduling;
10845 
10846 
10847 /*---------------------------------------------------------------------
10848 Procedure Name : Validate_Line
10849 Description    : Validates a line before scheduling.
10850                  It will make sure the required attributes are the
10851                  there on the line.
10852                  Only standard lines can be scheduled.Service lines
10853                  return lines cannot be scheduled.
10854                  IF the profile OE:Schedule Line on Hold is set to 'Y'
10855                  we will perform scheduling on lines on hold. If it is
10856                  set to 'N', we will not perform scheduling.
10857 --------------------------------------------------------------------- */
10858 Procedure Validate_Line
10859 (p_line_rec      IN OE_ORDER_PUB.Line_Rec_Type,
10860  p_old_line_rec  IN OE_ORDER_PUB.Line_Rec_Type,
10861  p_sch_action    IN VARCHAR2,
10862  p_caller        IN VARCHAR2 := SCH_EXTERNAL,
10863 x_return_status OUT NOCOPY VARCHAR2)
10864 
10865 IS
10866   l_msg_count              NUMBER;
10867   l_msg_data               VARCHAR2(2000);
10868   l_result                 Varchar2(30);
10869   l_scheduling_level_code  VARCHAR2(30) := NULL;
10870   l_type_code              VARCHAR2(30);
10871   l_out_return_status      VARCHAR2(1)  := FND_API.G_RET_STS_SUCCESS;
10872   l_org_id                 NUMBER;
10873   l_bill_seq_id            NUMBER;
10874   l_make_buy               NUMBER;
10875   l_config_id              NUMBER;
10876   l_org_code               VARCHAR2(30);
10877   l_order_date_type_code   VARCHAR2(30) := null; -- Bug-2371760
10878   l_auth_to_override_atp   VARCHAR2(3) := NULL; -- BUG 1282873
10879   l_override               NUMBER;  -- BUG 1282873
10880   l_set_rec                OE_ORDER_CACHE.set_rec_type;
10881   l_found                  VARCHAR2(1) :='N';  --Bug 2746497
10882   l_overridden             VARCHAR2(1) :='N';  --Bug 2716220
10883 --
10884 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
10885 --
10886 BEGIN
10887 
10888                                    IF l_debug_level  > 0 THEN
10889                                        oe_debug_pub.add(  '..ENTERING OE_SCHEDULE_UTIL.VALIDATE_LINE :' || P_SCH_ACTION , 1 ) ;
10890                                    END IF;
10891    x_return_status  := FND_API.G_RET_STS_SUCCESS;
10892 
10893    -- If the quantity on the line is missing or null and if
10894    -- the user is trying to performing scheduling, it is an error
10895 
10896    IF ((p_old_line_rec.ordered_quantity is null OR
10897         p_old_line_rec.ordered_quantity = FND_API.G_MISS_NUM) AND
10898          (p_line_rec.ordered_quantity is null OR
10899           p_line_rec.ordered_quantity = FND_API.G_MISS_NUM)) THEN
10900 
10901              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_QUANTITY');
10902              OE_MSG_PUB.Add;
10903              IF l_debug_level  > 0 THEN
10904                  oe_debug_pub.add(  'E1' , 1 ) ;
10905              END IF;
10906              x_return_status := FND_API.G_RET_STS_ERROR;
10907    END IF;
10908 
10909    -- If the quantity on the line is zero(which is different from
10910    -- missing)  and if the user is trying to performing scheduling,
10911    -- it is an error
10912 
10913    IF (((p_old_line_rec.ordered_quantity is null OR
10914          p_old_line_rec.ordered_quantity = FND_API.G_MISS_NUM OR
10915          p_old_line_rec.ordered_quantity = 0) AND
10916          p_line_rec.ordered_quantity = 0) AND
10917          (nvl(p_line_rec.cancelled_flag,'N') = 'N')) THEN
10918 
10919          IF  p_sch_action is not null
10920          AND p_caller = SCH_INTERNAL  THEN
10921 
10922              FND_MESSAGE.SET_NAME('ONT','OE_SCH_ZERO_QTY');
10923              OE_MSG_PUB.Add;
10924 
10925              IF l_debug_level  > 0 THEN
10926                  oe_debug_pub.add(  'E2' , 1 ) ;
10927              END IF;
10928              x_return_status := FND_API.G_RET_STS_ERROR;
10929          END IF;
10930 
10931 
10932    END IF;
10933 
10934    IF l_debug_level  > 0 THEN
10935        oe_debug_pub.add(  'BEFORE CANCEL CHECK ' || X_RETURN_STATUS , 2 ) ;
10936    END IF;
10937    -- If the line is cancelled, scheduling is not allowed.
10938 
10939    IF (p_line_rec.cancelled_flag = 'Y' AND
10940        p_caller = SCH_INTERNAL) THEN
10941 
10942           IF p_line_rec.schedule_action_code is not null THEN
10943 
10944              -- The line is cancelled. Cannot perform scheduling
10945              -- on it.
10946 
10947              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_FULLY_CANCELLED');
10948              OE_MSG_PUB.Add;
10949 
10950              IF l_debug_level  > 0 THEN
10951                  oe_debug_pub.add(  'E3' , 1 ) ;
10952              END IF;
10953              x_return_status := FND_API.G_RET_STS_ERROR;
10954 
10955          END IF;
10956    END IF;
10957 
10958    -- If the line is shipped, scheduling is not allowed.
10959 
10960    IF l_debug_level  > 0 THEN
10961        oe_debug_pub.add(  'BEFORE SHIPPIED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
10962    END IF;
10963    IF (p_line_rec.shipped_quantity is not null) AND
10964         (p_line_rec.shipped_quantity <> FND_API.G_MISS_NUM) THEN
10965 
10966          IF p_sch_action is not null THEN
10967 
10968              -- The line is cancelled. Cannot perform scheduling
10969              -- on it.
10970 
10971              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_SHIPPED');
10972              OE_MSG_PUB.Add;
10973 
10974              IF l_debug_level  > 0 THEN
10975                  oe_debug_pub.add(  'E4' , 1 ) ;
10976              END IF;
10977              x_return_status := FND_API.G_RET_STS_ERROR;
10978 
10979          END IF;
10980    END IF;
10981 /* --Commenting for Bug 13082802
10982 IF l_debug_level  > 0 THEN
10983        oe_debug_pub.add(  'BEFORE FULFILLED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
10984    END IF;
10985 --Added for bug 6873122
10986    IF (p_line_rec.fulfilled_quantity is not null) AND
10987         (p_line_rec.fulfilled_quantity <> FND_API.G_MISS_NUM) THEN
10988 
10989          IF p_sch_action is not null THEN
10990 
10991              -- The line is Fulfilled. Cannot perform scheduling
10992              -- on it.
10993 
10994              FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_FULFILLED');
10995              OE_MSG_PUB.Add;
10996 
10997              IF l_debug_level  > 0 THEN
10998                  oe_debug_pub.add(  'E4.1' , 1 ) ;
10999              END IF;
11000              x_return_status := FND_API.G_RET_STS_ERROR;
11001 
11002          END IF;
11003    END IF;
11004 --Added for bug 6873122
11005 --Commenting for Bug 13082802 */
11006    IF l_debug_level  > 0 THEN
11007        oe_debug_pub.add(  'BEFORE RESERVED QTY CHECK ' || X_RETURN_STATUS , 2 ) ;
11008    END IF;
11009    -- Check to see if the reserved quantity is changed and is more
11010    -- than the ordered quantity. This should not be allowed.
11011    IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity,
11012                              p_line_rec.reserved_quantity)
11013    THEN
11014         -- Reserved Quantity has changed
11015        IF (p_line_rec.ordered_quantity < p_line_rec.reserved_quantity)
11016        AND OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom, p_line_rec.order_quantity_uom) --Bug 6335352
11017        THEN
11018 
11019          FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
11020          OE_MSG_PUB.Add;
11021 
11022          IF l_debug_level  > 0 THEN
11023              oe_debug_pub.add(  'E5' , 1 ) ;
11024          END IF;
11025          x_return_status := FND_API.G_RET_STS_ERROR;
11026        END IF;
11027 
11028        -- after changing reserved qty, trying to unschedule or unreserve
11029        -- dose not make sense.
11030        IF (p_sch_action = OESCH_ACT_UNSCHEDULE OR
11031            p_sch_action = OESCH_ACT_UNRESERVE) AND
11032            (p_line_rec.reserved_quantity is not null) THEN
11033 
11034            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_QTY_CHG_NOT_ALLOWED');
11035            OE_MSG_PUB.Add;
11036            x_return_status := FND_API.G_RET_STS_ERROR;
11037            IF l_debug_level  > 0 THEN
11038                oe_debug_pub.add(  'E6' , 1 ) ;
11039            END IF;
11040        END IF;
11041    END IF;
11042 
11043 IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity2, -- INVCONV
11044                              p_line_rec.reserved_quantity2)
11045    THEN
11046         -- Reserved Quantity2 has changed
11047        IF (p_line_rec.ordered_quantity2 < p_line_rec.reserved_quantity2)
11048        THEN
11049 
11050          FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
11051          OE_MSG_PUB.Add;
11052 
11053          IF l_debug_level  > 0 THEN
11054              oe_debug_pub.add(  'E5a' , 1 ) ;
11055          END IF;
11056          x_return_status := FND_API.G_RET_STS_ERROR;
11057        END IF;
11058 
11059        -- after changing reserved qty, trying to unschedule or unreserve
11060        -- dose not make sense.
11061        IF (p_sch_action = OESCH_ACT_UNSCHEDULE OR
11062            p_sch_action = OESCH_ACT_UNRESERVE) AND
11063            (p_line_rec.reserved_quantity2 is not null) THEN
11064 
11065            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_QTY_CHG_NOT_ALLOWED');
11066            OE_MSG_PUB.Add;
11067            x_return_status := FND_API.G_RET_STS_ERROR;
11068            IF l_debug_level  > 0 THEN
11069                oe_debug_pub.add(  'E6A' , 1 ) ;
11070            END IF;
11071        END IF;
11072    END IF;
11073 
11074 
11075 
11076    -- Check to see if the ordered quantity and reserved quantity
11077    -- both have changed and if the ordered quantity is less than
11078    -- the reserved quantity. This should not be allowed.
11079 
11080    IF NOT OE_GLOBALS.Equal(p_old_line_rec.ordered_quantity,
11081                            p_line_rec.ordered_quantity)
11082    THEN
11083         -- Ordered Quantity has changed
11084        IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity,
11085                                p_line_rec.reserved_quantity)
11086           AND OE_GLOBALS.Equal(p_old_line_rec.order_quantity_uom, p_line_rec.order_quantity_uom) -- Bug 6335352
11087        THEN
11088          IF (p_line_rec.ordered_quantity < p_line_rec.reserved_quantity)
11089          THEN
11090 
11091            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
11092            OE_MSG_PUB.Add;
11093 
11094            x_return_status := FND_API.G_RET_STS_ERROR;
11095            IF l_debug_level  > 0 THEN
11096                oe_debug_pub.add(  'E7' , 1 ) ;
11097            END IF;
11098          END IF;
11099        END IF;
11100    END IF;
11101 
11102     IF NOT OE_GLOBALS.Equal(p_old_line_rec.ordered_quantity2, -- INVCONV
11103                            p_line_rec.ordered_quantity2)
11104    THEN
11105         -- Ordered Quantity has changed
11106        IF NOT OE_GLOBALS.Equal(p_old_line_rec.reserved_quantity2,
11107                                p_line_rec.reserved_quantity2)
11108        THEN
11109          IF (p_line_rec.ordered_quantity2 < p_line_rec.reserved_quantity2)
11110          THEN
11111 
11112            FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_MORE_ORD_QTY');
11113            OE_MSG_PUB.Add;
11114 
11115            x_return_status := FND_API.G_RET_STS_ERROR;
11116            IF l_debug_level  > 0 THEN
11117                oe_debug_pub.add(  'E7A' , 1 ) ;
11118            END IF;
11119          END IF;
11120        END IF;
11121    END IF;
11122 
11123 
11124    -- If the order quantity uom on the line is missing or null
11125    -- and if the user is trying to performing scheduling,
11126    -- it is an error
11127 
11128    IF (p_line_rec.order_quantity_uom is null OR
11129        p_line_rec.order_quantity_uom = FND_API.G_MISS_CHAR) THEN
11130 
11131              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_UOM');
11132              OE_MSG_PUB.Add;
11133 
11134              x_return_status := FND_API.G_RET_STS_ERROR;
11135              IF l_debug_level  > 0 THEN
11136                  oe_debug_pub.add(  'E8' , 1 ) ;
11137              END IF;
11138    END IF;
11139 
11140    -- If the item on the line is missing or null and if the user
11141    -- is trying to performing scheduling, it is an error
11142 
11143    IF l_debug_level  > 0 THEN
11144        oe_debug_pub.add(  'CHECKING THE ITEM....' , 1 ) ;
11145    END IF;
11146 
11147    IF (p_line_rec.inventory_item_id is null OR
11148        p_line_rec.inventory_item_id = FND_API.G_MISS_NUM) THEN
11149 
11150              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_ITEM');
11151              OE_MSG_PUB.Add;
11152 
11153              IF l_debug_level  > 0 THEN
11154                  oe_debug_pub.add(  'E9' , 1 ) ;
11155              END IF;
11156              x_return_status := FND_API.G_RET_STS_ERROR;
11157    END IF;
11158 
11159    -- If the request_date on the line is missing or null and
11160    -- if the user is trying to performing scheduling,
11161    -- it is an error
11162 
11163    IF l_debug_level  > 0 THEN
11164        oe_debug_pub.add(  'CHECKING THE REQUEST DATE....' , 1 ) ;
11165    END IF;
11166    IF (p_line_rec.request_date is null OR
11167           p_line_rec.request_date = FND_API.G_MISS_DATE) THEN
11168 
11169              FND_MESSAGE.SET_NAME('ONT','OE_SCH_MISSING_REQUEST_DATE');
11170              OE_MSG_PUB.Add;
11171              IF l_debug_level  > 0 THEN
11172                  oe_debug_pub.add(  'E10' , 1 ) ;
11173              END IF;
11174              x_return_status := FND_API.G_RET_STS_ERROR;
11175    END IF;
11176    -- 3763015
11177    IF NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y' OR
11178          Nvl(p_line_rec.bypass_sch_flag, 'N') = 'Y' THEN --- DOO Integration Changes
11179 
11180       -- An item with this profile set  MUST have a warehouse
11181       IF l_debug_level  > 0 THEN
11182          oe_debug_pub.add(  'CHECKING THAT LINE HAS A WAREHOUSE...' , 1 ) ;
11183       END IF;
11184       IF (p_line_rec.ship_from_org_id is null OR
11185           p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
11186           FND_MESSAGE.SET_NAME('ONT','ONT_SCH_BYPASS_MISS_WSH');
11187           OE_MSG_PUB.Add;
11188 
11189          IF l_debug_level  > 0 THEN
11190             oe_debug_pub.add(  'ERROR BYPASS' , 1 ) ;
11191          END IF;
11192          x_return_status := FND_API.G_RET_STS_ERROR;
11193       END IF;
11194    END IF;
11195 
11196    -- If the line belongs to a set, you cannot unschedule the line
11197    IF l_debug_level  > 0 THEN
11198        oe_debug_pub.add(  'CHECKING FOR SET VALIDATIONS....' , 1 ) ;
11199    END IF;
11200    IF ((p_line_rec.ship_set_id is not null AND
11201         p_line_rec.ship_set_id <> FND_API.G_MISS_NUM) OR
11202        (p_line_rec.arrival_set_id is not null AND
11203         p_line_rec.arrival_set_id <> FND_API.G_MISS_NUM)) AND
11204          (p_sch_action = OESCH_ACT_UNDEMAND OR
11205           p_sch_action = OESCH_ACT_UNSCHEDULE)
11206           THEN
11207 
11208              FND_MESSAGE.SET_NAME('ONT','OE_SCH_CANNOT_UNSCH_SET');
11209              OE_MSG_PUB.Add;
11210              IF l_debug_level  > 0 THEN
11211                  oe_debug_pub.add(  'E11' , 1 ) ;
11212              END IF;
11213              x_return_status := FND_API.G_RET_STS_ERROR;
11214    END IF;
11215 
11216 
11217    -- Bug 2434132.
11218    -- Reducing the qty should be allowed and not increasing the qty.
11219    IF l_debug_level  > 0 THEN
11220        oe_debug_pub.add(  'CHECKING FOR HOLDS....'||p_sch_action , 1 ) ;
11221    END IF;
11222    IF  Oe_Sys_Parameters.Value('ONT_SCHEDULE_LINE_ON_HOLD') = 'N'
11223    AND (p_sch_action = OESCH_ACT_SCHEDULE OR
11224         p_sch_action = OESCH_ACT_RESERVE OR
11225         (p_sch_action = OESCH_ACT_RESCHEDULE AND
11226          p_line_rec.schedule_status_code is Null) OR
11227        (p_line_rec.schedule_status_code is not null AND
11228         Schedule_Attribute_Changed(p_line_rec     => p_line_rec,
11229                                    p_old_line_rec => p_old_line_rec)) OR
11230        (p_line_rec.schedule_status_code is not null AND
11231         p_line_rec.ordered_quantity > p_old_line_rec.ordered_quantity))
11232 
11233    THEN
11234 
11235         -- Since the profile is set to NO, we should not schedule
11236         -- the line if the line is on hold.
11237 
11238         IF l_debug_level  > 0 THEN
11239             oe_debug_pub.add(  'CALLING CHECK HOLDS' , 1 ) ;
11240         END IF;
11241 
11242         OE_Holds_PUB.Check_Holds
11243                  (   p_api_version       => 1.0
11244                  ,   p_init_msg_list     => FND_API.G_FALSE
11245                  ,   p_commit            => FND_API.G_FALSE
11246                  ,   p_validation_level  => FND_API.G_VALID_LEVEL_FULL
11247                  ,   x_return_status     => l_out_return_status
11248                  ,   x_msg_count         => l_msg_count
11249                  ,   x_msg_data          => l_msg_data
11250                  ,   p_line_id           => p_line_rec.line_id
11251                  ,   p_header_id           => p_line_rec.header_id
11252                  ,   p_hold_id           => NULL
11253                  ,   p_entity_code       => NULL
11254                  ,   p_entity_id         => NULL
11255                  ,   x_result_out        => l_result
11256                  );
11257 
11258         IF l_debug_level  > 0 THEN
11259             oe_debug_pub.add(  'AFTER CALLING CHECK HOLDS: ' || L_OUT_RETURN_STATUS , 1 ) ;
11260         END IF;
11261 
11262 
11263         IF (l_out_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
11264             IF l_out_return_status = FND_API.G_RET_STS_ERROR THEN
11265                RAISE FND_API.G_EXC_ERROR;
11266             ELSE
11267                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
11268             END IF;
11269         END IF;
11270 
11271         IF (l_result = FND_API.G_TRUE) THEN
11272             FND_MESSAGE.SET_NAME('ONT','OE_SCH_LINE_ON_HOLD');
11273             OE_MSG_PUB.Add;
11274             x_return_status := FND_API.G_RET_STS_ERROR;
11275         END IF;
11276    END IF;
11277 
11278    -- Check to see what scheduling level is allowed to be performed
11279    -- on this line. If the action requested is not allowed for the
11280    -- scheduling action, error out.
11281 
11282    IF l_debug_level  > 0 THEN
11283        oe_debug_pub.add(  'CHECKING SCHEDULING LEVEL...' , 1 ) ;
11284    END IF;
11285    l_scheduling_level_code := Get_Scheduling_Level(p_line_rec.header_id,
11286                                                    p_line_rec.line_type_id);
11287 
11288    IF l_debug_level  > 0 THEN
11289        oe_debug_pub.add(  'L_SCHEDULING_LEVEL_CODE : ' || L_SCHEDULING_LEVEL_CODE , 1 ) ;
11290    END IF;
11291 
11292 --BUG 1955004
11293    -- Checking if transaction type is using a
11294    --Repair Item Shipping Scheduling Level
11295    IF l_debug_level  > 0 THEN
11296        oe_debug_pub.add(  'CHECKING IF LINE HAS REPAIR ITEM SHIPPING SCHEDULING LEVEL...' , 1 ) ;
11297    END IF;
11298 
11299    IF (l_scheduling_level_code = SCH_LEVEL_FOUR
11300        OR l_scheduling_level_code = SCH_LEVEL_FIVE) THEN
11301 
11302      -- An item with this scheduling level MUST have a warehouse
11303     IF l_debug_level  > 0 THEN
11304         oe_debug_pub.add(  'CHECKING THAT LINE HAS A WAREHOUSE...' , 1 ) ;
11305     END IF;
11306     IF (p_line_rec.ship_from_org_id is null OR
11307         p_line_rec.ship_from_org_id = FND_API.G_MISS_NUM) THEN
11308         FND_MESSAGE.SET_NAME('ONT','OE_SCH_INACTIVE_MISS_WSH');
11309         FND_MESSAGE.SET_TOKEN('LTYPE',
11310                        nvl(sch_cached_line_type,'0'));
11311         OE_MSG_PUB.Add;
11312 
11313         IF l_debug_level  > 0 THEN
11314             oe_debug_pub.add(  'E11.1' , 1 ) ;
11315         END IF;
11316         x_return_status := FND_API.G_RET_STS_ERROR;
11317     END IF;
11318 
11319      -- An item with this schedling level MUST be a Standard
11320      -- Item, and NOT be a part of an ATO
11321      IF l_debug_level  > 0 THEN
11322          oe_debug_pub.add(  'CHECKING THAT IT IS A STANDARD ITEM...' , 1 ) ;
11323      END IF;
11324      IF (p_line_rec.item_type_code <> 'STANDARD'
11325          OR p_line_rec.ato_line_id is not null) THEN
11326 
11327         FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_INACTIVE_STD_ONLY');
11328         FND_MESSAGE.SET_TOKEN('LTYPE',
11329                        nvl(sch_cached_line_type,'0'));
11330         OE_MSG_PUB.Add;
11331 
11332         IF l_debug_level  > 0 THEN
11333             oe_debug_pub.add(  'E11.2' , 1 ) ;
11334         END IF;
11335         X_return_status := FND_API.G_RET_STS_ERROR;
11336       END IF;
11337 
11338       -- An item with this scheduling level MUST NOT be part
11339       -- of ANY set
11340       IF l_debug_level  > 0 THEN
11341           oe_debug_pub.add(  'CHECKING FOR NO SETS...' , 1 ) ;
11342       END IF;
11343       /* Commenting for Standalone project.
11344       -- We will now allow standard lines into sets.
11345       IF (p_line_rec.ship_set_id is not null OR
11346           P_line_rec.arrival_set_id is not null)THEN
11347 
11348         FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_INACTIVE_STD_ONLY');
11349         FND_MESSAGE.SET_TOKEN('LTYPE',
11350                        nvl(sch_cached_line_type,'0'));
11351         OE_MSG_PUB.Add;
11352 
11353         IF l_debug_level  > 0 THEN
11354             oe_debug_pub.add(  'E11.3' , 1 ) ;
11355         END IF;
11356         X_return_status := FND_API.G_RET_STS_ERROR;
11357       END IF;
11358       */
11359    END IF;
11360 -- END 1955004
11361 
11362    -- 3763015
11363    IF (l_scheduling_level_code is not null
11364    AND l_scheduling_level_code <> SCH_LEVEL_THREE)
11365    OR NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y'
11366    OR Nvl(p_line_rec.bypass_sch_flag, 'N') = 'Y' THEN -- DOO Integration related.
11367         IF l_scheduling_level_code = SCH_LEVEL_ONE THEN
11368            IF p_sch_action = OESCH_ACT_SCHEDULE OR
11369               p_sch_action = OESCH_ACT_RESERVE OR
11370              (p_line_rec.schedule_status_code is  null AND
11371              (p_line_rec.schedule_ship_date is NOT NULL OR
11372               p_line_rec.schedule_arrival_date is NOT NULL))
11373             THEN
11374 
11375               FND_MESSAGE.SET_NAME('ONT','OE_SCH_ACTION_NOT_ALLOWED');
11376               FND_MESSAGE.SET_TOKEN('ACTION',
11377                        nvl(p_sch_action,OESCH_ACT_SCHEDULE));
11378               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
11379                        nvl(sch_cached_line_type,sch_cached_order_type));
11380               OE_MSG_PUB.Add;
11381               IF l_debug_level  > 0 THEN
11382                   oe_debug_pub.add(  'E12' , 1 ) ;
11383               END IF;
11384               x_return_status := FND_API.G_RET_STS_ERROR;
11385            END IF;
11386         ELSE
11387            IF l_scheduling_level_code = SCH_LEVEL_TWO OR
11388               -- BUG 1955004
11389               l_scheduling_level_code = SCH_LEVEL_FIVE
11390               -- Level Five and Two cannot have Reservations performed
11391               -- END 1955004
11392            THEN
11393            -- 2766876
11394            IF (NVL(p_line_rec.reserved_quantity,0) > 0
11395            AND p_sch_action = OESCH_ACT_SCHEDULE)
11396            OR p_sch_action = OESCH_ACT_RESERVE THEN
11397               FND_MESSAGE.SET_NAME('ONT','OE_SCH_ACTION_NOT_ALLOWED');
11398               FND_MESSAGE.SET_TOKEN('ACTION',
11399                         nvl(p_sch_action,OESCH_ACT_RESERVE));
11400               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
11401                         nvl(sch_cached_line_type,sch_cached_order_type));
11402               OE_MSG_PUB.Add;
11403               IF l_debug_level  > 0 THEN
11404                   oe_debug_pub.add(  'E13' , 1 ) ;
11405               END IF;
11406               x_return_status := FND_API.G_RET_STS_ERROR;
11407            END IF;
11408         END IF;
11409            --BUG 1955004
11410           -- 3763015
11411           IF l_scheduling_level_code = SCH_LEVEL_FOUR OR
11412              l_scheduling_level_code = SCH_LEVEL_FIVE OR
11413              NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'Y'  OR
11414              Nvl(p_line_rec.bypass_sch_flag, 'N') = 'Y'  -- DOO Integration.
11415           THEN
11416             IF p_sch_action = OESCH_ACT_ATP_CHECK THEN
11417             -- levels Four and Five CANNOT have ATP Performed
11418               FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_ACTION_NOT_ALLOWED');
11419               FND_MESSAGE.SET_TOKEN('ACTION',
11420                           (nvl(p_sch_action, OESCH_ACT_ATP_CHECK)));
11421               FND_MESSAGE.SET_TOKEN('ORDER_TYPE',
11422                         nvl(sch_cached_line_type,sch_cached_order_type));
11423               OE_MSG_PUB.Add;
11424               IF l_debug_level  > 0 THEN
11425                   oe_debug_pub.add('p_line_rec.bypass_sch_flag: ' ||
11426                                      Nvl(p_line_rec.bypass_sch_flag, 'N'), 1);
11427                                               -- Debug during DOO Integration.
11428                   oe_debug_pub.add(  'E13.1' , 1 ) ;
11429               END IF;
11430               X_return_status := FND_API.G_RET_STS_ERROR;
11431             END IF;
11432           END IF;
11433         --END 1955004
11434        END IF;
11435    END IF;
11436 
11437    -- For DOO Integration (Bug 11896152)
11438    IF Nvl(p_line_rec.bypass_sch_flag, 'N') = 'Y'  AND
11439                             NOT Oe_Genesis_Util.G_INCOMING_FROM_DOO
11440    THEN
11441       DECLARE
11442         l_explanation VARCHAR2(80);
11443       BEGIN
11444         SELECT  meaning INTO l_explanation
11445         FROM    oe_lookups
11446         WHERE   lookup_type = 'SCH_FAIL_REASONS'
11447         AND     lookup_code = 'EXT_SCH_DOO';
11448 
11449         Fnd_Message.Set_Name('ONT', 'OE_SCH_OE_ORDER_FAILED');
11450         Fnd_Message.Set_Token('EXPLANATION', l_explanation);
11451         Oe_Msg_Pub.ADD;
11452 
11453         l_explanation := NULL;
11454       EXCEPTION
11455         WHEN OTHERS THEN
11456           IF l_debug_level > 0 THEN
11457             oe_debug_pub.ADD('E13.2 Exception section....',1);
11458           END IF;
11459       END;
11460 
11461      IF l_debug_level > 0 THEN
11462        Oe_Debug_Pub.Add ( 'E13.2', 1);
11463      END IF;
11464 
11465      x_return_status := Fnd_Api.G_Ret_Sts_Error;
11466 
11467    END IF;
11468    -- End: bug 11896152
11469 
11470    IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y'
11471    AND (((p_sch_action  = OESCH_ACT_RESERVE OR
11472           p_sch_action  = OESCH_ACT_RESCHEDULE
11473 	  )
11474 	  AND
11475           ( (p_old_line_rec.reserved_quantity >nvl(p_line_rec.reserved_quantity,0)
11476 	     AND   Nvl(p_line_rec.fulfillment_base ,'P') ='P')
11477            OR  ( p_old_line_rec.reserved_quantity2 >nvl(p_line_rec.reserved_quantity2,0)
11478              AND   Nvl(p_line_rec.fulfillment_base ,'P') ='S') -- bug 16782621
11479           )
11480 	  AND
11481           Get_Pick_Status(p_line_rec.line_id)
11482 	 )
11483         OR
11484          (p_sch_action  = OESCH_ACT_UNRESERVE AND
11485           Get_Pick_Status(p_line_rec.line_id)
11486 	  )
11487        )
11488    THEN  -- 2595661
11489 
11490        -- Reservation qty cannot be reduced when line is
11491        -- interfaced to wsh. Give a message here tell the user we are
11492        -- not unreserving. Added code here to fix bug 2038201.
11493 
11494        FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNRSV_NOT_ALLOWED');
11495        OE_MSG_PUB.Add;
11496        IF l_debug_level  > 0 THEN
11497            oe_debug_pub.add(  'E14' , 1 ) ;
11498        END IF;
11499        x_return_status := FND_API.G_RET_STS_ERROR;
11500 
11501    END IF;
11502 
11503    -- following 2 checks are only for ato's
11504 
11505    IF p_line_rec.ato_line_id is not null AND
11506       NOT(p_line_rec.ato_line_id = p_line_rec.line_id AND
11507           p_line_rec.item_type_code IN ( OE_GLOBALS.G_ITEM_OPTION,
11508                                          OE_GLOBALS.G_ITEM_STANDARD,
11509 					 OE_GLOBALS.G_ITEM_INCLUDED)) --9775352
11510    THEN
11511 
11512      IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
11513    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 THEN
11514 
11515        IF l_debug_level  > 0 THEN
11516         oe_debug_pub.add('Warehouse Validation is not rqd',1);
11517        END IF;
11518      ELSE
11519 
11520       IF p_line_rec.ship_from_org_id is NULL AND
11521         p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_SCHEDULE
11522       THEN
11523 
11524        FND_MESSAGE.SET_NAME('ONT','OE_SCH_ATO_WHSE_REQD');
11525        OE_MSG_PUB.Add;
11526        IF l_debug_level  > 0 THEN
11527            oe_debug_pub.add(  'E15' , 1 ) ;
11528        END IF;
11529        x_return_status := FND_API.G_RET_STS_ERROR;
11530 
11531       END IF;
11532      END IF; -- Gop code level
11533      IF (p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
11534      OR  p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_UNRESERVE)
11535      AND p_line_rec.schedule_status_code is null
11536      THEN
11537 
11538        FND_MESSAGE.SET_NAME('ONT','OE_SCH_RES_NO_CONFIG');
11539        OE_MSG_PUB.Add;
11540        IF l_debug_level  > 0 THEN
11541            oe_debug_pub.add(  'E16' , 1 ) ;
11542        END IF;
11543        -- code fix for 3300528
11544        -- no need for returning error in this case
11545        --IF nvl(p_line_rec.ship_model_complete_flag,'N') = 'N' THEN
11546        --   x_return_status := FND_API.G_RET_STS_ERROR;
11547        --END IF;
11548        -- code fix for 3300528
11549      END IF;
11550      /* 4171389:  Reservations cannot be placed for config items if order
11551       * is not booked.
11552       */
11553      IF p_sch_action  = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
11554         AND p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CONFIG
11555         AND p_line_rec.booked_flag <> 'Y'
11556      THEN
11557         FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
11558         OE_MSG_PUB.Add;
11559         IF l_debug_level  > 0 THEN
11560            oe_debug_pub.add(  'E16.0' , 1 );
11561         END IF;
11562         x_return_status := FND_API.G_RET_STS_ERROR;
11563      END IF;
11564    -- code fix for 3300528
11565    -- a message has to be given when the user tries to reserve PTO models and classes
11566    ELSIF nvl(p_line_rec.shippable_flag,'N') = 'N'
11567      AND  ( p_sch_action = OE_SCHEDULE_UTIL.OESCH_ACT_RESERVE
11568        OR p_sch_action = OE_SCHEDULE_UTIL.OESCH_ACT_UNRESERVE)
11569        AND p_line_rec.schedule_status_code IS NULL
11570    THEN
11571      FND_MESSAGE.SET_NAME('ONT','ONT_SCH_NOT_RESERVABLE');
11572      OE_MSG_PUB.Add;
11573      IF l_debug_level  > 0 THEN
11574        oe_debug_pub.add(  'E16.1' , 1 ) ;
11575      END IF;
11576      -- code fix for 3300528
11577    END IF; -- part of ato
11578 
11579    -- Added for 3451987
11580    IF NVL(FND_PROFILE.VALUE('ONT_SCH_ATO_ITEM_WO_BOM'), 'N') = 'N' THEN
11581       -- Added this part of validation to fix bug 2051855
11582       IF p_line_rec.ato_line_id = p_line_rec.line_id
11583          AND p_line_rec.item_type_code in ('STANDARD','OPTION','INCLUDED') --9775352
11584          AND  fnd_profile.value('INV_CTP') = '5'THEN
11585 
11586          l_org_id := OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID');
11587 
11588          -- Added code to fix bug 2156268
11589          BEGIN
11590 
11591             SELECT planning_make_buy_code
11592             INTO   l_make_buy
11593             FROM   mtl_system_items
11594             WHERE  inventory_item_id = p_line_rec.inventory_item_id
11595             AND    ORGANIZATION_ID = nvl(p_line_rec.ship_from_org_id,
11596                                             l_org_id);
11597 
11598          EXCEPTION
11599             WHEN NO_DATA_FOUND THEN
11600                l_make_buy := 1;
11601          END;
11602 
11603          IF l_debug_level  > 0 THEN
11604             oe_debug_pub.add(  'L_MAKE_BUY' || L_MAKE_BUY , 2 );
11605          END IF;
11606 
11607          IF nvl(l_make_buy,1) <> 2 THEN
11608             BEGIN
11609                -- Modified code to fix bug 2307501.
11610                SELECT BILL_SEQUENCE_ID
11611                INTO   l_bill_seq_id
11612                FROM   BOM_BILL_OF_MATERIALS
11613                WHERE  ORGANIZATION_ID = nvl(p_line_rec.ship_from_org_id,
11614                                             l_org_id)
11615                AND    ASSEMBLY_ITEM_ID = p_line_rec.inventory_item_id
11616                AND    ALTERNATE_BOM_DESIGNATOR IS NULL;
11617 
11618             EXCEPTION
11619                WHEN NO_DATA_FOUND THEN
11620                   IF l_debug_level  > 0 THEN
11621                      oe_debug_pub.add(  'NO BILL IS DEFINED' , 2 ) ;
11622                   END IF;
11623                   IF l_debug_level  > 0 THEN
11624                      oe_debug_pub.add(  'E17' , 1 ) ;
11625                   END IF;
11626                   FND_MESSAGE.SET_NAME('ONT','OE_BOM_NO_BILL_IN_SHP_ORG');
11627                   FND_MESSAGE.SET_TOKEN('ITEM',p_line_rec.ordered_item);
11628 
11629                   -- Bug 2367743  Start
11630                   Select ORGANIZATION_CODE
11631                   Into   l_org_code
11632                   From   Inv_Organization_Info_v   --ORG_ORGANIZATION_DEFINITIONS
11633                   Where  ORGANIZATION_ID = NVL(p_line_rec.ship_from_org_id,l_org_id);
11634                   IF l_debug_level  > 0 THEN
11635                      oe_debug_pub.add(  'ORGANIZATION CODE:'||L_ORG_CODE , 2 ) ;
11636                   END IF;
11637                   FND_MESSAGE.SET_TOKEN('ORG',l_org_code);
11638                   -- Bug 2367743 End
11639 
11640                   OE_MSG_PUB.Add;
11641                   x_return_status := FND_API.G_RET_STS_ERROR;
11642 
11643                WHEN OTHERS THEN
11644                   Null;
11645             END;
11646          END IF;
11647       END IF;
11648    END IF; -- ONT_SCH_ATO_ITEM_WO_BOM = 'N'
11649 
11650 /* Duplicate validation to E14. Bug 2312341
11651    IF  nvl(p_line_rec.shipping_interfaced_flag, 'N') = 'Y'
11652    AND (p_sch_action = OESCH_ACT_RESERVE
11653    OR   p_sch_action = OESCH_ACT_RESCHEDULE)
11654    AND p_old_line_rec.reserved_quantity >
11655        p_line_rec.reserved_quantity
11656    THEN
11657 
11658       -- Added code here to fix bug 2038201.
11659       oe_debug_pub.add('E18',1);
11660       FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNRSV_NOT_ALLOWED');
11661       OE_MSG_PUB.Add;
11662       x_return_status := FND_API.G_RET_STS_ERROR;
11663 
11664    END IF;
11665 */
11666    IF (p_sch_action = OESCH_ACT_UNSCHEDULE)
11667    AND (p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_MODEL OR
11668         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_OPTION OR
11669         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_CLASS OR
11670         p_line_rec.item_type_code = OE_GLOBALS.G_ITEM_INCLUDED)
11671    AND  NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
11672    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10 )
11673 
11674    THEN
11675      -- This action is not allowed on an ATO configuration if the config
11676      -- item is created.
11677 
11678      BEGIN
11679         SELECT line_Id
11680         INTO   l_config_id
11681         FROM   OE_ORDER_LINES_ALL
11682         WHERE  header_id = p_line_rec.header_id
11683         AND   (ato_line_id =p_line_rec.ato_line_id OR
11684               (top_model_line_id = p_line_rec.top_model_line_id AND
11685                Ship_model_complete_flag = 'Y'))
11686         AND    item_type_code = 'CONFIG';
11687 
11688       IF l_debug_level  > 0 THEN
11689           oe_debug_pub.add(  'E19' , 1 ) ;
11690       END IF;
11691       FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNSCH_CONFIG_EXISTS');
11692       OE_MSG_PUB.Add;
11693       x_return_status := FND_API.G_RET_STS_ERROR;
11694 
11695      EXCEPTION
11696         WHEN NO_DATA_FOUND THEN
11697              null;
11698         WHEN TOO_MANY_ROWS THEN
11699           IF l_debug_level  > 0 THEN
11700               oe_debug_pub.add(  'E19 TOO MANY ROWS' , 1 ) ;
11701           END IF;
11702           FND_MESSAGE.SET_NAME('ONT','OE_SCH_UNSCH_CONFIG_EXISTS');
11703           OE_MSG_PUB.Add;
11704           x_return_status := FND_API.G_RET_STS_ERROR;
11705 
11706      END;
11707 
11708    END IF; /* If action was unschedule */
11709 
11710  -- Start Bug-2371760
11711  -- Change in Schedule Ship/Arrival Date is not allowed
11712  -- when Order Date Type is specified.
11713 /*
11714  OE_DEBUG_PUB.Add('Checking for Order date Type - Arrival');
11715 
11716  IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_ship_date,
11717                             p_old_line_rec.schedule_ship_date)
11718     THEN
11719        -- If the Order Type is ARRIVAL, the user is not
11720        -- allowed to change the schedule ship date
11721 
11722        l_order_date_type_code    := Get_Date_Type(p_line_rec.header_id);
11723 
11724        IF nvl(l_order_date_type_code,'SHIP') = 'ARRIVAL' THEN
11725 
11726           FND_MESSAGE.SET_NAME('ONT','OE_SCH_INV_SHP_DATE');
11727           OE_MSG_PUB.Add;
11728 
11729           OE_DEBUG_PUB.Add('E20 Order date Type - Arrival');
11730           x_return_status := FND_API.G_RET_STS_ERROR;
11731 
11732        END IF;
11733 
11734 
11735     END IF;
11736 
11737    OE_DEBUG_PUB.Add('Checking for Order date Type - Ship');
11738 
11739     IF NOT OE_GLOBALS.Equal(p_line_rec.schedule_arrival_date,
11740                             p_old_line_rec.schedule_arrival_date)
11741     THEN
11742 
11743        -- If the Order Type is SHIP (or null), the user is not
11744        -- allowed to change the schedule arrival date
11745 
11746        l_order_date_type_code    := Get_Date_Type(p_line_rec.header_id);
11747 
11748        IF nvl(l_order_date_type_code,'SHIP') = 'SHIP' THEN
11749 
11750           FND_MESSAGE.SET_NAME('ONT','OE_SCH_INV_ARR_DATE');
11751           OE_MSG_PUB.Add;
11752 
11753           OE_DEBUG_PUB.Add('E21 Order date Type - Ship');
11754 
11755           x_return_status := FND_API.G_RET_STS_ERROR;
11756 
11757        END IF;
11758    END IF;
11759 
11760  -- End Bug-2371760
11761  */
11762    -- BUG 1282873
11763    IF OE_CODE_CONTROL.Get_Code_Release_Level >= '110509' THEN
11764      l_auth_to_override_atp :=
11765          NVL(FND_PROFILE.VALUE('ONT_OVERRIDE_ATP'), 'N');
11766 
11767     IF l_debug_level  > 0 THEN
11768         oe_debug_pub.add(  'ONT_OVERRIDE_ATP' || L_AUTH_TO_OVERRIDE_ATP , 3 ) ;
11769     END IF;
11770     IF l_auth_to_override_atp = 'N' THEN
11771 
11772        IF  NOT OE_GLOBALS.Equal(p_line_rec.override_atp_date_code,
11773                                 p_old_line_rec.override_atp_date_code)
11774        THEN
11775 
11776           -- only authorized users have authority to update the
11777           -- Overide ATP Field
11778           FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH');
11779           OE_MSG_PUB.Add;
11780           IF l_debug_level  > 0 THEN
11781               oe_debug_pub.add(  'E22 INVALID AUTOTIZE' ) ;
11782           END IF;
11783                             IF l_debug_level  > 0 THEN
11784                                 oe_debug_pub.add(  'USER DOES NOT HAVE AUTHORITY TO CHANGE THE OVERRIDE ATP FIELD' , 1 ) ;
11785                             END IF;
11786           x_return_status := FND_API.G_RET_STS_ERROR;
11787 
11788        END IF;
11789 
11790 
11791        IF p_line_rec.schedule_status_code IS NOT NULL THEN
11792 
11793           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' AND
11794              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
11795                                         ,p_old_line_rec => p_old_line_rec) OR
11796           NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
11797 
11798           THEN
11799 
11800            -- only authorized users have authority to update scheduling attributes
11801            -- on an overridden line
11802              FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_MOD');
11803              OE_MSG_PUB.Add;
11804                   IF l_debug_level  > 0 THEN
11805                       oe_debug_pub.add(  'E23 USER DOES NOT HAVE AUTHORITY TO UPDATE SCHEDULE ATTRIBUTES' , 1 ) ;
11806                   END IF;
11807              X_return_status := FND_API.G_RET_STS_ERROR;
11808 
11809           END IF;
11810 
11811           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' AND
11812              (p_sch_action = OESCH_ACT_UNDEMAND OR
11813               P_sch_action = OESCH_ACT_UNSCHEDULE) THEN
11814              -- Only Authorized users have authority to unschedule an overriddenline
11815              FND_MESSAGE.SET_NAME('ONT','OE_SCH_OVER_ATP_NO_AUTH_UNCHK');
11816              OE_MSG_PUB.Add;
11817              IF l_debug_level  > 0 THEN
11818                 oe_debug_pub.add(  'E24 GENERAL USER CANNOT UNSCHEDULE AN OVERRIDDEN LINE' , 1 ) ;
11819              END IF;
11820              X_return_status := FND_API.G_RET_STS_ERROR;
11821 
11822           END IF;
11823           -- Start 2746497
11824           IF NVL(p_line_rec.override_atp_date_code, 'N') = 'N' AND
11825              NVL(p_line_rec.ship_model_complete_flag,'N') = 'Y' AND
11826              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
11827                                        ,p_old_line_rec => p_old_line_rec) OR
11828              NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
11829           THEN
11830                BEGIN
11831                       SELECT  'Y' INTO l_found
11832                       FROM   oe_order_lines
11833                       WHERE  top_model_line_id = p_line_rec.top_model_line_id
11834                       AND override_atp_date_code = 'Y'
11835                       AND rownum <2;
11836                EXCEPTION
11837                    WHEN OTHERS THEN
11838                         NULL;
11839                END;
11840                IF l_found = 'Y' THEN
11841                   FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_MOD');
11842                   OE_MSG_PUB.Add;
11843                   X_return_status := FND_API.G_RET_STS_ERROR;
11844              END IF;
11845           END IF;
11846           -- End 2746497
11847 
11848 
11849           IF (p_line_rec.ship_set_id IS NOT NULL OR
11850              p_line_rec.arrival_set_id IS NOT NULL) AND
11851              (Schedule_Attribute_Changed(p_line_rec => p_line_rec
11852                                        ,p_old_line_rec => p_old_line_rec) OR
11853              NVL(p_line_rec.ordered_quantity, 0) > p_old_line_rec.ordered_quantity)
11854           THEN
11855 
11856              l_override := 0;
11857 
11858 
11859              SELECT count('x')
11860              INTO   l_override
11861              FROM   oe_order_lines_all
11862              WHERE  header_id = p_line_rec.header_id
11863              AND    (ship_set_id = p_line_rec.ship_set_id
11864              OR     arrival_set_id = p_line_rec.arrival_set_id)
11865              AND    override_atp_date_code = 'Y';
11866 
11867              IF l_override > 0 THEN
11868 
11869                -- only authorized users have authority to update scheduling
11870                -- attributes in a set with an overridden line
11871                FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
11872                OE_MSG_PUB.Add;
11873                IF l_debug_level  > 0 THEN
11874                    oe_debug_pub.add(  'E25 USER DOES NOT HAVE AUTHORITY TO UPDATE SCHEDULE ATTRIBUTES ON A SET WITH AN OVERRIDDEN LINE' , 1 ) ;
11875                END IF;
11876                X_return_status := FND_API.G_RET_STS_ERROR;
11877 
11878              END IF; -- l_override > 0
11879           END IF; -- ship and arrival not null
11880        END IF;  -- schedule_status_code IS NOT NULL
11881     END IF; -- overide_atp = N
11882 
11883     IF ((p_line_rec.ship_set_id IS NOT NULL AND
11884         NOT oe_globals.equal(p_line_rec.ship_set_id,
11885                              p_old_line_rec.ship_set_id))  OR
11886        (p_line_rec.arrival_set_id IS NOT NULL AND
11887         NOT oe_globals.equal(p_line_rec.arrival_set_id,
11888                             p_old_line_rec.arrival_set_id))) THEN
11889 
11890        l_set_rec := OE_ORDER_CACHE.Load_Set
11891                   (nvl(p_line_rec.arrival_set_id,p_line_rec.ship_set_id));
11892        IF NVL(p_line_rec.override_atp_date_code, 'N') = 'Y' THEN
11893 
11894         IF  l_set_rec.ship_from_org_id is not null
11895         AND NOT Set_Attr_Matched
11896            (p_set_ship_from_org_id      => l_set_rec.ship_from_org_id ,
11897             p_line_ship_from_org_id     => p_line_rec.ship_from_org_id,
11898             p_set_ship_to_org_id        => l_set_rec.ship_to_org_id ,
11899             p_line_ship_to_org_id       => p_line_rec.ship_to_org_id ,
11900             p_set_schedule_ship_date    => l_set_rec.schedule_ship_date ,
11901             p_line_schedule_ship_date   => p_line_rec.schedule_ship_date,
11902             p_set_arrival_date          => l_set_rec.schedule_arrival_date,
11903             p_line_arrival_date         => p_line_rec.schedule_arrival_date,
11904             p_set_shipping_method_code  => l_set_rec.shipping_method_code ,
11905             p_line_shipping_method_code => p_line_rec.shipping_method_code,
11906             p_set_type                  => l_set_rec.set_type) THEN
11907 
11908           -- General users can only add new lines to the set provided the new
11909           -- lines can be scheduled for the schedule ship date of the set AND
11910           -- Authorized users cannot add an overridden line if the schedule
11911           -- ship date soes not match the set
11912 
11913           FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
11914           OE_MSG_PUB.Add;
11915           IF l_debug_level  > 0 THEN
11916               oe_debug_pub.add(  'E26 UNABLE TO ADD THE LINE TO THE SET' , 1 ) ;
11917           END IF;
11918           x_return_status := FND_API.G_RET_STS_ERROR;
11919 
11920         -- 2716220
11921         ELSIF p_line_rec.ship_set_id is not null
11922 --          AND p_line_rec.schedule_status_code is not null THEN
11923         THEN
11924            BEGIN
11925               SELECT 'Y'
11926               INTO   l_overridden
11927               FROM   oe_order_lines_all
11928               WHERE  header_id = p_line_rec.header_id
11929               AND    line_id <> p_line_rec.line_id
11930               AND    ship_set_id = p_line_rec.ship_set_id
11931               AND    override_atp_date_code = 'Y'
11932               AND    schedule_ship_date <> p_line_rec.schedule_ship_date
11933               AND    rownum < 2;
11934            EXCEPTION
11935               WHEN OTHERS THEN
11936                  NULL;
11937            END;
11938         ELSIF p_line_rec.arrival_set_id is not null
11939  --         AND p_line_rec.schedule_status_code is not null THEN
11940         THEN
11941            BEGIN
11942               SELECT 'Y'
11943               INTO   l_overridden
11944               FROM   oe_order_lines_all
11945               WHERE  header_id = p_line_rec.header_id
11946               AND    line_id <> p_line_rec.line_id
11947               AND    arrival_set_id = p_line_rec.arrival_set_id
11948               AND    override_atp_date_code = 'Y'
11949               AND    schedule_arrival_date <> p_line_rec.schedule_arrival_date
11950               AND    rownum < 2;
11951            EXCEPTION
11952               WHEN OTHERS THEN
11953                  NULL;
11954            END;
11955 
11956         END IF; -- ship from
11957         IF l_overridden = 'Y' THEN
11958         --3517527 set_status check commented
11959         --  IF l_set_rec.set_status <> 'T' THEN
11960            FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
11961            OE_MSG_PUB.Add;
11962         --  END IF;
11963            IF l_debug_level  > 0 THEN
11964               oe_debug_pub.add(  'E27 UNABLE TO ADD THE line TO THE SET' , 1 ) ;
11965            END IF;
11966            x_return_status := FND_API.G_RET_STS_ERROR;
11967         END IF;
11968 
11969        END IF; -- override
11970 
11971         -- If a non smc model or kit has lines that are overridden for
11972         -- different dates, stop adding the model to set.
11973 
11974        IF p_line_rec.top_model_line_id = p_line_rec.line_id
11975        AND nvl(p_line_rec.ship_model_complete_flag,'N') = 'N'
11976        AND p_line_rec.ato_line_id is null THEN
11977 
11978           IF NOT Valid_Set_Addition
11979              (p_top_model_line_id  => p_line_rec.top_model_line_id,
11980               p_set_type           => l_set_rec.set_type) THEN
11981 
11982            FND_MESSAGE.SET_NAME('ONT', 'OE_SCH_OVER_ATP_NO_AUTH_SET');
11983            OE_MSG_PUB.Add;
11984            IF l_debug_level  > 0 THEN
11985                oe_debug_pub.add(  'E28 UNABLE TO ADD THE Model TO THE SET' , 1 ) ;
11986            END IF;
11987            x_return_status := FND_API.G_RET_STS_ERROR;
11988 
11989           END IF; -- valid
11990        END IF; -- top model
11991     END IF; -- ship set.
11992 
11993   END IF; -- pack I check
11994   --END 1282873
11995   -- Pack J
11996   -- Honoring Latest Acceptable Date
11997   -- 3940632 : Dates truncated prior to comparison.
11998   IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510' THEN
11999 
12000       l_order_date_type_code := NVL(Get_Date_Type(p_line_rec.header_id), 'SHIP');
12001       IF  (p_sch_action = OESCH_ACT_SCHEDULE
12002           OR (p_sch_action = OESCH_ACT_RESERVE
12003              AND p_line_rec.schedule_status_code IS NULL)
12004           OR p_sch_action = OESCH_ACT_RESCHEDULE)
12005          AND OE_SYS_PARAMETERS.value ('LATEST_ACCEPTABLE_DATE_FLAG') = 'H'
12006          AND NVL(p_line_rec.override_atp_date_code, 'N') = 'N'
12007 
12008  --Bug 6400995
12009          --Honoring of LAD should be done only when updating/creating lines and not while cancelling
12010          AND nvl(p_line_rec.cancelled_flag,'N') = 'N'
12011 
12012      THEN
12013 
12014          IF trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date))
12015                                                        < trunc(p_line_rec.request_date) THEN
12016             IF l_debug_level  > 0 THEN
12017                oe_debug_pub.add ('E29 Request date exceeds Latest Acceptable Date',1);
12018             END IF;
12019             FND_MESSAGE.SET_NAME('ONT','ONT_SCH_REQUEST_EXCEED_LAD');
12020             OE_MSG_PUB.Add;
12021             x_return_status := FND_API.G_RET_STS_ERROR;
12022          ELSIF ((l_order_date_type_code = 'SHIP'
12023             AND trunc(NVL(p_line_rec.schedule_ship_date,p_line_rec.request_date))
12024                            >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))
12025           OR (l_order_date_type_code = 'ARRIVAL'
12026             AND trunc(NVL(p_line_rec.schedule_arrival_date, p_line_rec.request_date))
12027                           >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))) THEN
12028            IF l_debug_level  > 0 THEN
12029               oe_debug_pub.add('E30 Schedule date exceeds Latest Acceptable Date',1);
12030            END IF;
12031            FND_MESSAGE.SET_NAME('ONT','ONT_SCH_LAD_SCH_FAILED');
12032            OE_MSG_PUB.Add;
12033            x_return_status := FND_API.G_RET_STS_ERROR;
12034          END IF;
12035          -- 3894212
12036          IF ((p_line_rec.ship_set_id IS NOT NULL AND
12037            NOT oe_globals.equal(p_line_rec.ship_set_id,
12038                              p_old_line_rec.ship_set_id))  OR
12039             (p_line_rec.arrival_set_id IS NOT NULL AND
12040            NOT oe_globals.equal(p_line_rec.arrival_set_id,
12041                             p_old_line_rec.arrival_set_id))) THEN
12042 
12043             l_set_rec := OE_ORDER_CACHE.Load_Set
12044                   (nvl(p_line_rec.arrival_set_id,p_line_rec.ship_set_id));
12045 
12046             IF ((l_order_date_type_code = 'SHIP'
12047                AND trunc(l_set_rec.schedule_ship_date)
12048                            >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date)))
12049                OR (l_order_date_type_code = 'ARRIVAL'
12050                AND trunc(l_set_rec.schedule_arrival_date)
12051                           >  trunc(NVL(p_line_rec.latest_acceptable_date,p_line_rec.request_date))))
12052                AND l_set_rec.schedule_arrival_date IS NOT NULL
12053                AND l_set_rec.schedule_ship_date IS NOT NULL THEN
12054                IF l_debug_level  > 0 THEN
12055                   oe_debug_pub.add('E30.1 Schedule date exceeds Latest Acceptable Date',1);
12056                END IF;
12057                FND_MESSAGE.SET_NAME('ONT','ONT_SCH_LAD_SCH_FAILED');
12058                OE_MSG_PUB.Add;
12059                x_return_status := FND_API.G_RET_STS_ERROR;
12060             END IF;
12061          END IF;
12062          --
12063       END IF;
12064    END IF;
12065  -- End Pack J
12066     -- 3288838 --
12067     -- 3361753 subinventory check added
12068    l_found := 'N';
12069    --4653097
12070    IF nvl(p_line_rec.shipping_interfaced_flag,'N') = 'Y'
12071     AND p_sch_action = OESCH_ACT_RESERVE
12072     AND OE_GLOBALS.Equal(p_old_line_rec.subinventory,
12073                          p_line_rec.subinventory)
12074     AND OE_GLOBALS.Equal(p_old_line_rec.project_id,
12075                          p_line_rec.project_id)
12076     AND OE_GLOBALS.Equal(p_old_line_rec.task_id,
12077                          p_line_rec.task_id) THEN
12078       BEGIN
12079          IF l_debug_level > 0 THEN
12080             OE_DEBUG_PUB.Add('Before checking for Staged/Closed deliveries', 1);
12081          END IF;
12082 
12083          SELECT 'Y'
12084          INTO   l_found
12085          FROM   WSH_DELIVERY_DETAILS
12086          WHERE  SOURCE_LINE_ID = p_line_rec.line_id
12087          AND    SOURCE_CODE = 'OE'
12088          AND    RELEASED_STATUS IN ('Y', 'C');
12089 
12090          IF l_debug_level > 0 THEN
12091             OE_DEBUG_PUB.Add('Staged/Closed deliveries exist for the line', 3);
12092          END IF;
12093       EXCEPTION
12094          WHEN NO_DATA_FOUND THEN
12095             NULL;
12096          WHEN TOO_MANY_ROWS THEN
12097             l_found :='Y';
12098       END;
12099       IF l_found = 'Y' THEN
12100          IF l_debug_level  > 0 THEN
12101             oe_debug_pub.add('E31 Reservation(s) cannot be created.  The line has been pick confirmed/staged',1);
12102          END IF;
12103          FND_MESSAGE.SET_NAME('ONT','ONT_SCH_RSV_FAILURE_STAGED');
12104          OE_MSG_PUB.Add;
12105          x_return_status := FND_API.G_RET_STS_ERROR;
12106       END IF;
12107    END IF;
12108 
12109   IF l_debug_level  > 0 THEN
12110      oe_debug_pub.add(  '..EXITING OE_SCHEDULE_UTIL.VALIDATE_LINE WITH ' || X_RETURN_STATUS , 1 ) ;
12111   END IF;
12112 
12113 
12114 EXCEPTION
12115    WHEN FND_API.G_EXC_ERROR THEN
12116 
12117         x_return_status := FND_API.G_RET_STS_ERROR;
12118 
12119    WHEN OTHERS THEN
12120 
12121         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12122 
12123         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12124         THEN
12125             OE_MSG_PUB.Add_Exc_Msg
12126             (   G_PKG_NAME,
12127               'Validate_Line');
12128         END IF;
12129 END Validate_Line;
12130 /*---------------------------------------------------------------------
12131 Procedure Name : Process_Request
12132 Description    : This procedure is called from the Schedule_Line procedure
12133                  to schedule a SINGLE LINE (a set or a parent line  is scheduled
12134                  in different procedure). The single line could be a part
12135                  of the set which is getting scheduled independently
12136                  (because there was not change in the set related attribute),
12137                  or it could be just a simple standard line which does not
12138                  belong to any set.
12139 
12140 --------------------------------------------------------------------- */
12141 Procedure Process_request(p_old_line_rec  IN OE_ORDER_PUB.line_rec_type,
12142                           p_x_line_rec    IN OUT NOCOPY OE_ORDER_PUB.line_rec_type,
12143                           p_caller        IN VARCHAR2,
12144                           p_sch_action    IN VARCHAR2,
12145 x_return_status OUT NOCOPY VARCHAR2)
12146 
12147 IS
12148 l_update_flag    VARCHAR2(1):= FND_API.G_TRUE;
12149 l_orig_line_rec  OE_ORDER_PUB.line_rec_type;
12150 l_reserve_later  VARCHAR2(1) := 'N';
12151 l_qty_to_reserve NUMBER;
12152 l_qty2_to_reserve NUMBER; -- INVCONV
12153 --Bug 12641867
12154 l_shipping_interfaced_flag VARCHAR2(1);
12155 l_firm_demand_flag   VARCHAR2(1);
12156 l_lock_control   NUMBER;
12157 --End Bug 12641867
12158 
12159 --
12160 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12161 --
12162 l_return_status       VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS; --Bug 5343902
12163 BEGIN
12164 
12165   IF l_debug_level  > 0 THEN
12166       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.PROCESS_REQUEST' , 1 ) ;
12167   END IF;
12168   IF l_debug_level  > 0 THEN
12169       oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG : ' || OESCH_AUTO_SCH_FLAG , 1 ) ;
12170   END IF;
12171 
12172 -- need to retain orig record to call process order.
12173 
12174   l_orig_line_rec := p_x_line_rec;
12175   x_return_status   := FND_API.G_RET_STS_SUCCESS;
12176 
12177   IF p_sch_action  = OESCH_ACT_SCHEDULE THEN
12178 
12179     -- Based on the p_sch_action  and schedule_status_code call action_schedule.
12180 
12181      IF l_debug_level  > 0 THEN
12182          oe_debug_pub.add(  'NEW' || P_X_LINE_REC.SCHEDULE_SHIP_DATE ) ;
12183      END IF;
12184      IF l_debug_level  > 0 THEN
12185          oe_debug_pub.add(  'OLD' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE ) ;
12186      END IF;
12187 
12188      IF l_debug_level  > 0 THEN
12189          oe_debug_pub.add(  'OLD AD ' || P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
12190      END IF;
12191      IF l_debug_level  > 0 THEN
12192          oe_debug_pub.add(  'NEW AD ' || P_OLD_LINE_REC.SCHEDULE_ARRIVAL_DATE , 1 ) ;
12193      END IF;
12194 
12195      /* Added code for bug 5343902 */
12196     IF nvl(p_x_line_rec.model_remnant_flag, 'N')  = 'Y' AND
12197        p_x_line_rec.item_type_code in ('KIT', 'CLASS','MODEL')  AND
12198        p_x_line_rec.explosion_date IS NULL
12199     THEN
12200       oe_debug_pub.add('Remnant ' || p_x_line_rec.item_type_code || ', Calling Process_Included_Items', 5);
12201 
12202       l_return_status := OE_CONFIG_UTIL.Process_Included_Items
12203                          (p_line_id   => p_x_line_rec.line_id,
12204                           p_freeze    => FALSE);
12205 
12206       IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12207          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12208       ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
12209          RAISE FND_API.G_EXC_ERROR;
12210       END IF;
12211 
12212        oe_debug_pub.add('After calling Process_Included_Items', 5);
12213     END IF;
12214     /* End of code changes for bug 5343902 */
12215 
12216      Action_Schedule(p_x_line_rec     => p_x_line_rec,
12217                      p_old_line_rec   => p_old_line_rec,
12218                      p_sch_action     => OESCH_ACT_SCHEDULE,
12219                      p_qty_to_reserve => p_x_line_rec.reserved_quantity,
12220                      x_return_status  => x_return_status);
12221 
12222      IF l_debug_level  > 0 THEN
12223          oe_debug_pub.add(  'AFTER ACTION SCHEDULE : ' || X_RETURN_STATUS , 1 ) ;
12224      END IF;
12225 
12226      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12227           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12228      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12229 
12230 /* Added two extra conditions in the following if condition to fix the Bug 2894100 */
12231 
12232          IF  OESCH_AUTO_SCH_FLAG = 'Y'
12233          AND p_x_line_rec.schedule_ship_date is null
12234          AND p_x_line_rec.schedule_arrival_date is null
12235          -- QUOTING changes - check for complete negotiation also
12236          -- as it can trigger auto-scheduling
12237          AND (p_x_line_rec.operation = OE_GLOBALS.G_OPR_CREATE
12238               OR OE_Quote_Util.G_COMPLETE_NEG = 'Y'
12239              )
12240          THEN
12241          -- We donot want to error out the insert if autoscheduling
12242          -- failed. So we will return success. We also do not want to
12243          -- do any update, so we will set the l_update_flag to FALSE.
12244 
12245             l_update_flag     := FND_API.G_FALSE;
12246             x_return_status   := FND_API.G_RET_STS_SUCCESS;
12247 
12248          ELSE
12249 
12250             RAISE FND_API.G_EXC_ERROR;
12251 
12252          END IF;
12253      END IF;
12254 
12255   ELSIF p_sch_action  = OESCH_ACT_RESCHEDULE THEN
12256 
12257 IF NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
12258                             p_old_line_rec.inventory_item_id)
12259      AND p_x_line_rec.source_type_code = 'EXTERNAL' THEN -- 7139462
12260 
12261         p_x_line_rec.schedule_action_code := OESCH_ACT_UNSCHEDULE;
12262 
12263         Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
12264                         p_old_line_rec  => p_old_line_rec,
12265                         p_sch_action    => OESCH_ACT_UNSCHEDULE,
12266                         x_return_status => x_return_status);
12267         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12268            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12269         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12270            RAISE FND_API.G_EXC_ERROR;
12271         END IF;
12272  ELSE
12273        Action_Reschedule(p_x_line_rec    => p_x_line_rec,
12274                          p_old_line_rec  => p_old_line_rec,
12275                          x_return_status => x_return_status,
12276                          x_reserve_later => l_reserve_later);
12277 
12278         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12279            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12280         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12281            RAISE FND_API.G_EXC_ERROR;
12282         END IF;
12283 END IF;
12284 
12285   ELSIF p_sch_action  = OESCH_ACT_RESERVE THEN
12286 
12287      -- If some one has passes action as reserve on a
12288      -- unscheduled line
12289      IF p_x_line_rec.schedule_status_code IS NULL THEN
12290 
12291         Action_Schedule(p_x_line_rec     => p_x_line_rec,
12292                         p_old_line_rec   => p_old_line_rec,
12293                         p_sch_action     => OESCH_ACT_RESERVE,
12294                         p_qty_to_reserve => p_x_line_rec.reserved_quantity,
12295                         x_return_status  => x_return_status);
12296 
12297         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12298            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12299         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12300            RAISE FND_API.G_EXC_ERROR;
12301         END IF;
12302 
12303      ELSE
12304 
12305         Action_reserve(p_x_line_rec    => p_x_line_rec,
12306                        p_old_line_rec  => p_old_line_rec,
12307                        x_return_status => x_return_status);
12308 
12309         l_update_flag := FND_API.G_FALSE;
12310 
12311         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12312            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12313         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12314            RAISE FND_API.G_EXC_ERROR;
12315         END IF;
12316 
12317 
12318      END IF;
12319 
12320 
12321   ELSIF p_sch_action   = OESCH_ACT_UNRESERVE
12322   THEN
12323 
12324     -- Setting update flag to false, so that schedule_line does not
12325     -- process_order as unreserving does not cause any line attributes
12326     -- to change.
12327 
12328     l_update_flag := FND_API.G_FALSE;
12329 
12330     -- We should never use this action unless the
12331     -- old_status is reserved .
12332 
12333      IF (p_old_line_rec.reserved_quantity is not null AND
12334          p_old_line_rec.reserved_quantity <> FND_API.G_MISS_NUM)
12335      THEN
12336     -- shipping_interfaced_flag
12337        Unreserve_Line
12338         (p_line_rec              => p_old_line_rec,
12339          p_quantity_to_unreserve => p_old_line_rec.reserved_quantity,
12340          p_quantity2_to_unreserve => p_old_line_rec.reserved_quantity2, -- INVCONV
12341          x_return_status         => x_return_status);
12342 
12343         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12344            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12345         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12346            RAISE FND_API.G_EXC_ERROR;
12347         END IF;
12348 
12349      ELSE
12350        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
12351        OE_MSG_PUB.Add;
12352        l_update_flag := FND_API.G_FALSE;
12353      END IF;
12354 
12355 
12356   -- schedule_action_code -->  OESCH_ACT_UNDEMAND
12357 
12358   ELSIF p_sch_action  = OESCH_ACT_UNDEMAND
12359   THEN
12360      IF p_x_line_rec.Schedule_status_code IS NOT NULL THEN
12361 
12362         Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
12363                           p_old_line_rec  => p_old_line_rec,
12364                           p_sch_action    => OESCH_ACT_UNDEMAND,
12365                           x_return_status => x_return_status);
12366 
12367         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12368            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12369         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12370            RAISE FND_API.G_EXC_ERROR;
12371         END IF;
12372 
12373      ELSE
12374        FND_MESSAGE.SET_NAME('ONT','OE_SCH_NO_ACTION_DONE_NO_EXP');
12375        OE_MSG_PUB.Add;
12376        l_update_flag := FND_API.G_FALSE;
12377      END IF;
12378 
12379   ELSIF p_sch_action  = OESCH_ACT_UNSCHEDULE THEN
12380      -- When action is passed as unschedule.
12381      -- Or if the line's source type is being changed from INTERNAL to
12382      -- EXTERNAL, and the old line was scheduled, we need to unschedule it.
12383 
12384       p_x_line_rec.schedule_action_code := OESCH_ACT_UNSCHEDULE;
12385       Action_UnSchedule(p_x_line_rec    => p_x_line_rec,
12386                         p_old_line_rec  => p_old_line_rec,
12387                         p_sch_action    => OESCH_ACT_UNSCHEDULE,
12388                         x_return_status => x_return_status);
12389 
12390       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12391          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12392       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12393          RAISE FND_API.G_EXC_ERROR;
12394       END IF;
12395 
12396   END IF; -- Main If.
12397 
12398   IF l_update_flag = FND_API.G_TRUE
12399   THEN
12400 /*
12401      Call_Process_Order(p_old_line_rec    => p_old_line_rec,
12402                         p_x_line_rec      => p_x_line_rec,
12403                         p_write_to_db     => p_write_to_db,
12404                         p_caller          => p_caller,
12405                         x_return_status   => x_return_status);
12406 */
12407 
12408      --Bug 12641867
12409      IF p_x_line_rec.ato_line_id IS NOT NULL
12410       AND p_x_line_rec.item_type_code = 'STANDARD'
12411       AND nvl(p_x_line_rec.booked_flag, 'N') = 'Y'
12412       AND p_sch_action  IN (OESCH_ACT_RESCHEDULE,OESCH_ACT_RESERVE) THEN
12413 
12414         SELECT shipping_interfaced_flag,firm_demand_flag,lock_control
12415         INTO l_shipping_interfaced_flag,l_firm_demand_flag,l_lock_control
12416         FROM OE_ORDER_LINES_ALL
12417         WHERE LINE_ID=p_x_line_rec.line_id;
12418 
12419         p_x_line_rec.shipping_interfaced_flag := l_shipping_interfaced_flag;
12420         p_x_line_rec.firm_demand_flag := l_firm_demand_flag;
12421         p_x_line_rec.lock_control := l_lock_control;
12422 
12423      END IF;
12424      --End Bug 12641867
12425 
12426      Process_Line (p_old_line_rec    => l_orig_line_rec,
12427                    p_x_line_rec      => p_x_line_rec,
12428                    p_caller          => p_caller,
12429                    x_return_status   => x_return_status);
12430 
12431                                         IF l_debug_level  > 0 THEN
12432                                             oe_debug_pub.add(  'AFTER CALLING CALL_PROCESS_ORDER :' || X_RETURN_STATUS , 1 ) ;
12433                                         END IF;
12434 
12435       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12436          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12437       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
12438          RAISE FND_API.G_EXC_ERROR;
12439       END IF;
12440 
12441       IF l_reserve_later = 'Y'
12442       AND (NOT G_GOP_AUTO_SPLIT  OR G_GOP_AUTO_SPLIT_LINE_ID <> P_X_LINE_REC.LINE_ID ) THEN --ER 2736896. Do not reserve here.
12443                                  IF l_debug_level  > 0 THEN
12444                                      oe_debug_pub.add(  'RESERVE THE NEW ITEM: ' || P_X_LINE_REC.INVENTORY_ITEM_ID , 1 ) ;
12445                                  END IF;
12446         IF p_x_line_rec.shippable_flag = 'Y' THEN
12447 
12448 
12449           IF (nvl(p_x_line_rec.reserved_quantity,0) >
12450                  p_x_line_rec.ordered_quantity)
12451           OR nvl(p_x_line_rec.reserved_quantity,0) = 0
12452           THEN
12453              l_qty_to_reserve := p_x_line_rec.ordered_quantity;
12454           ELSE
12455              l_qty_to_reserve := p_x_line_rec.reserved_quantity;
12456           END IF;
12457 
12458                     IF (nvl(p_x_line_rec.reserved_quantity2,0) >   -- INVCONV
12459                  nvl(p_x_line_rec.ordered_quantity2,0) )
12460           OR nvl(p_x_line_rec.reserved_quantity2,0) = 0
12461           THEN
12462              l_qty2_to_reserve := p_x_line_rec.ordered_quantity2;
12463           ELSE
12464              l_qty2_to_reserve := p_x_line_rec.reserved_quantity2;
12465           END IF;
12466 
12467           IF l_qty2_to_reserve = 0 THEN -- INVCONV
12468                 l_qty2_to_reserve := null;
12469           end if;
12470 
12471           IF l_debug_level  > 0 THEN
12472            oe_debug_pub.add(  'QTY TO RESERVE ' || L_QTY_TO_RESERVE , 2 ) ;
12473                      oe_debug_pub.add(  'QTY2 TO RESERVE ' || L_QTY2_TO_RESERVE , 2 ) ;
12474        END IF;
12475 
12476 
12477           Reserve_Line
12478           ( p_line_rec             => p_x_line_rec
12479           , p_quantity_to_reserve  => l_qty_to_reserve
12480           , p_quantity2_to_reserve  => l_qty2_to_reserve -- INVCONV
12481           , x_return_Status        => x_return_status);
12482 
12483 
12484         END IF; -- Shippable flag
12485       END IF; -- reserve later
12486 
12487   END IF; -- update flag
12488 
12489   -- If schedule date has change, we need to call PO callback function
12490   -- to indicate the change.
12491 
12492   -- IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE AND --3888871
12493   IF p_x_line_rec.source_document_type_id = 10 AND --3888871
12494      NOT OE_GLOBALS.EQUAL(p_x_line_rec.schedule_arrival_date,
12495                           p_old_line_rec.schedule_arrival_date)
12496   THEN
12497      IF l_debug_level  > 0 THEN
12498         oe_debug_pub.add(  'PASSING SCHEDULE_ARRIVAL_DATE TO PO ' , 3 ) ;
12499      END IF;
12500      Update_PO(p_x_line_rec.schedule_arrival_date,
12501                p_x_line_rec.source_document_id,
12502                p_x_line_rec.source_document_line_id);
12503      IF l_debug_level  > 0 THEN
12504          oe_debug_pub.add(  'AFTER PO CALL BACK' , 3 ) ;
12505      END IF;
12506   END IF;
12507 
12508   IF l_debug_level  > 0 THEN
12509      oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.PROCESS_REQUEST: ' || X_RETURN_STATUS , 1 ) ;
12510   END IF;
12511 
12512 
12513 EXCEPTION
12514   WHEN FND_API.G_EXC_ERROR THEN
12515     x_return_status := FND_API.G_RET_STS_ERROR;
12516 
12517   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12518     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12519 
12520   WHEN OTHERS THEN
12521     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12522 
12523     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12524     THEN
12525       OE_MSG_PUB.Add_Exc_Msg
12526       (   G_PKG_NAME
12527       ,   'Process_request'
12528        );
12529     END IF;
12530 
12531 END Process_request;
12532 
12533 /*-----------------------------------------------------------------------------
12534 Procedure Name : Get_Lead_Time
12535 Description    : This function returns the manufacturing lead team for ATO
12536                  Options and Classes. While performing ATP, and scheduling
12537                  for an ATO configuration, we just don't have to check
12538 the availability of the items, we also need to find out nocopy
12539 
12540                  the amount of time it takes to build those items.
12541                  This procedure gives the time it takes to build the ATO.
12542                  It is standard formula which is used. The value is derived
12543                  from the ATO model. Thus all options for a given model
12544                  will have the same Lead Time.
12545 ----------------------------------------------------------------------------- */
12546 FUNCTION Get_Lead_Time
12547 ( p_ato_line_id      IN NUMBER
12548 , p_ship_from_org_id IN NUMBER)
12549 RETURN NUMBER
12550 IS
12551 l_model_ordered_quantity  NUMBER := 0;
12552 l_model_order_qty_uom     NUMBER := 0;
12553 primary_model_qty         NUMBER := 0;
12554 st_lead_time              NUMBER := 0;
12555 db_full_lead_time         NUMBER := 0;
12556 db_fixed_lead_time        NUMBER := 0;
12557 db_variable_lead_time     NUMBER := 0;
12558 db_primary_uom_code       VARCHAR2(3);
12559 db_model_item_id          NUMBER := 0;
12560 db_line_unit_code         VARCHAR2(3);
12561 --
12562 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12563 --
12564 BEGIN
12565 
12566   IF l_debug_level  > 0 THEN
12567       oe_debug_pub.add(  'ENTERING GET_LEAD_TIME' , 1 ) ;
12568   END IF;
12569   IF l_debug_level  > 0 THEN
12570       oe_debug_pub.add(  'ATO LINE IS ' || P_ATO_LINE_ID , 1 ) ;
12571   END IF;
12572   IF l_debug_level  > 0 THEN
12573       oe_debug_pub.add(  'SHIP FROM IS ' || P_SHIP_FROM_ORG_ID , 1 ) ;
12574   END IF;
12575 
12576   SELECT     NVL ( MSI.FULL_LEAD_TIME , 0 )
12577              , NVL ( MSI.FIXED_LEAD_TIME , 0 )
12578              , NVL ( MSI.VARIABLE_LEAD_TIME , 0 )
12579              , MSI.PRIMARY_UOM_CODE
12580              , NVL ( OL.INVENTORY_ITEM_ID , 0 )
12581              , OL.order_quantity_uom
12582              , OL.ordered_quantity
12583   INTO       db_full_lead_time
12584              , db_fixed_lead_time
12585              , db_variable_lead_time
12586              , db_primary_uom_code
12587              , db_model_item_id
12588              , db_line_unit_code
12589              , primary_model_qty
12590   FROM    MTL_SYSTEM_ITEMS MSI
12591           , OE_ORDER_LINES OL
12592   WHERE   MSI.INVENTORY_ITEM_ID  = OL.INVENTORY_ITEM_ID
12593   AND     MSI.ORGANIZATION_ID    = p_ship_from_org_id
12594   AND     OL.LINE_ID             = p_ato_line_id ;
12595 
12596 
12597   -- Get the model quantity in primary UOM
12598 
12599   -- Set the Lead time
12600 
12601   st_lead_time :=  ceil( nvl(db_fixed_lead_time,0) + nvl(db_variable_lead_time,0)
12602                          * nvl(primary_model_qty,0));
12603 
12604   IF nvl(db_full_lead_time,0) > nvl(st_lead_time,0) THEN
12605      st_lead_time := ceil(db_full_lead_time);
12606   END IF;
12607 
12608   RETURN st_lead_time;
12609 EXCEPTION
12610    WHEN NO_DATA_FOUND THEN
12611         RETURN 0;
12612    WHEN OTHERS THEN
12613         RETURN 0;
12614 END Get_Lead_Time;
12615 
12616 
12617 /*-----------------------------------------------------------------------------
12618 Procedure Name : Get_Date_Type
12619 Description    : This procedure returns the date type of the order.
12620                  The date type could be SHIP or ARRIVAl or null. Null
12621                  value is treated at SHIP in the scheduling code.
12622 -----------------------------------------------------------------------------*/
12623 
12624 FUNCTION Get_Date_Type
12625 ( p_header_id      IN NUMBER)
12626 RETURN VARCHAR2
12627 IS
12628 l_order_date_type_code   VARCHAR2(30) := null;
12629 --
12630 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12631 --
12632 BEGIN
12633 
12634    IF p_header_id <> nvl(G_HEADER_ID,0) THEN
12635        BEGIN
12636           SELECT order_date_type_code
12637           INTO   l_order_date_type_code
12638           FROM   oe_order_headers
12639           WHERE  header_id = p_header_id;
12640 
12641           G_HEADER_ID := p_header_id;
12642           G_DATE_TYPE := l_order_date_type_code;
12643        EXCEPTION
12644           WHEN OTHERS THEN
12645                RETURN null;
12646        END;
12647    ELSE
12648        l_order_date_type_code := G_DATE_TYPE;
12649    END IF;
12650 
12651    RETURN l_order_date_type_code;
12652 
12653 EXCEPTION
12654    WHEN NO_DATA_FOUND THEN
12655         RETURN NULL;
12656 END Get_Date_Type;
12657 
12658 /*--------------------------------------------------------------------------
12659 Procedure Name : Get_Order_Number
12660 Description    : This procedure returns the order_number from the header
12661         record, which we will pass to the MRP API.
12662 --------------------------------------------------------------------------*/
12663 FUNCTION Get_Order_Number(p_header_id in number)
12664 RETURN NUMBER
12665 IS
12666 l_order_number NUMBER;
12667 --
12668 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12669 --
12670 BEGIN
12671 
12672   IF l_debug_level  > 0 THEN
12673       oe_debug_pub.add(  'ENTERING GET_ORDER_NUMBER: ' || P_HEADER_ID , 1 ) ;
12674   END IF;
12675 
12676   IF p_header_id is not null AND p_header_id  <> FND_API.G_MISS_NUM
12677   THEN
12678      BEGIN
12679         select order_number
12680         into l_order_number
12681         from oe_order_headers
12682         where header_id = p_header_id;
12683      EXCEPTION
12684         WHEN OTHERS THEN
12685              RETURN null;
12686      END;
12687   END IF;
12688 
12689   IF l_debug_level  > 0 THEN
12690       oe_debug_pub.add(  'ORDER NUMBER : ' || L_ORDER_NUMBER ) ;
12691   END IF;
12692   RETURN l_order_number;
12693 EXCEPTION
12694    WHEN OTHERS THEN
12695         RETURN null;
12696 END Get_Order_Number;
12697 
12698 /*--------------------------------------------------------------------------
12699 Procedure Name : Get_mtl_sales_order_id
12700 Description    : This funtion returns the SALES_ORDER_ID (frm mtl_sales_orders)
12701                  for a given heeader_id.
12702                  Every header in oe_order_headers_all will have a record
12703                  in MTL_SALES_ORDERS. The unique key to get the sales_order_id
12704                  from mtl_sales_orders is
12705                  Order_Number
12706                  Order_Type (in base language)
12707                  OM:Source Code profile option (stored as ont_source_code).
12708 
12709                  The above values are stored in a flex in MTL_SALES_ORDERS.
12710                  SEGMENT1 : stores the order number
12711                  SEGMENT2 : stores the order type
12712                  SEGMENT3 : stores the ont_source_code value
12713 
12714 -------------------------------------------------------------------------- */
12715 FUNCTION Get_mtl_sales_order_id(p_header_id IN NUMBER,
12716                                 p_order_type_id IN NUMBER DEFAULT NULL)
12717 RETURN NUMBER
12718 IS
12719 l_source_code              VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
12720 l_sales_order_id           NUMBER := 0;
12721 l_order_type_name          VARCHAR2(80);
12722 l_order_type_id            NUMBER;
12723 --
12724 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12725 --
12726 BEGIN
12727 
12728    IF l_debug_level  > 0 THEN
12729        oe_debug_pub.add(  'ENTERING GET_MTL_SALES_ORDER_ID' , 2 ) ;
12730    END IF;
12731    -- 3748723
12732    IF p_order_type_id IS NULL THEN
12733 
12734       IF OE_Order_Cache.g_header_rec.order_type_id IS NOT NULL
12735       AND OE_Order_Cache.g_header_rec.header_id = p_header_id THEN  --Bug4683211
12736 
12737          IF l_debug_level  > 0 THEN
12738             oe_debug_pub.add(  'From Header Cache ' , 2 ) ;
12739          END IF;
12740          l_order_type_id := OE_Order_Cache.g_header_rec.order_type_id;
12741       ELSE
12742 
12743          BEGIN
12744             SELECT order_type_id
12745             INTO   l_order_type_id
12746             FROM   oe_order_headers_all
12747             WHERE  header_id = p_header_id;
12748          EXCEPTION
12749             WHEN OTHERS THEN
12750                RAISE;
12751          END;
12752       END IF;
12753    ELSE
12754       l_order_type_id:=p_order_type_id;
12755    END IF;
12756 
12757    IF l_debug_level  > 0 THEN
12758        oe_debug_pub.add(  'ORDER TYPE ID :' || L_ORDER_TYPE_ID , 2 ) ;
12759    END IF;
12760 
12761    --3748723
12762    IF l_order_type_id = oe_schedule_util.sch_cached_mtl_order_type_id AND
12763       oe_schedule_util.sch_cached_mtl_order_type_name IS NOT NULL THEN
12764       IF l_debug_level  > 0 THEN
12765          oe_debug_pub.add(  'From order_type_id_Cache ' , 2 ) ;
12766       END IF;
12767       l_order_type_name := oe_schedule_util.sch_cached_mtl_order_type_name;
12768    ELSE
12769       BEGIN
12770          SELECT NAME
12771          INTO   l_order_type_name
12772          FROM   OE_TRANSACTION_TYPES_TL
12773          WHERE  TRANSACTION_TYPE_ID = l_order_type_id
12774          AND    language = (select language_code
12775                             from fnd_languages
12776                             where installed_flag = 'B');
12777          oe_schedule_util.sch_cached_mtl_order_type_name := l_order_type_name;
12778  --Begin Bug#6719001
12779          oe_schedule_util.sch_cached_mtl_order_type_id := l_order_type_id;
12780          --End Bug#6719001
12781       EXCEPTION
12782          WHEN NO_DATA_FOUND THEN
12783             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12784       END;
12785    END IF;
12786    IF l_debug_level  > 0 THEN
12787        oe_debug_pub.add(  'ORDER TYPE: ' || L_ORDER_TYPE_NAME , 2 ) ;
12788    END IF;
12789    IF l_debug_level  > 0 THEN
12790        oe_debug_pub.add(  'SOURCE CODE: ' || L_SOURCE_CODE , 2 ) ;
12791    END IF;
12792 
12793    --3748723
12794    IF l_order_type_name = oe_schedule_util.sch_cached_mtl_order_type_name AND
12795       l_source_code = oe_schedule_util.sch_cached_mtl_source_code AND
12796       p_header_id = oe_schedule_util.sch_cached_mtl_header_id THEN
12797 
12798       IF l_debug_level  > 0 THEN
12799          oe_debug_pub.add(  'CACHED SALES_ORDER_ID ' || oe_schedule_util.sch_cached_mtl_sales_order_id , 2 ) ;
12800       END IF;
12801 
12802       RETURN oe_schedule_util.sch_cached_mtl_sales_order_id;
12803    ELSE
12804       SELECT S.SALES_ORDER_ID
12805       INTO l_sales_order_id
12806       FROM MTL_SALES_ORDERS S,
12807            OE_ORDER_HEADERS_ALL H
12808       WHERE S.SEGMENT1 = TO_CHAR(H.ORDER_NUMBER)
12809       AND S.SEGMENT2 = l_order_type_name
12810       AND S.SEGMENT3 = l_source_code
12811       AND H.HEADER_ID = p_header_id;
12812       --3748723
12813       oe_schedule_util.sch_cached_mtl_header_id := p_header_id;
12814       oe_schedule_util.sch_cached_mtl_source_code :=l_source_code;
12815       oe_schedule_util.sch_cached_mtl_sales_order_id := l_sales_order_id;
12816       oe_schedule_util.sch_cached_mtl_order_type_id := l_order_type_id;
12817    END IF;
12818 
12819    IF l_debug_level  > 0 THEN
12820        oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 2 ) ;
12821    END IF;
12822 
12823    RETURN l_sales_order_id;
12824 EXCEPTION
12825     WHEN NO_DATA_FOUND THEN
12826        IF l_debug_level  > 0 THEN
12827            oe_debug_pub.add(  '2. L_SALES_ORDER_ID IS 0' , 2 ) ;
12828        END IF;
12829        RETURN 0;
12830     WHEN OTHERS THEN
12831        IF l_debug_level  > 0 THEN
12832            oe_debug_pub.add(  '2. L_SALES_ORDER_ID IS 0' , 2 ) ;
12833        END IF;
12834        RETURN 0;
12835 END Get_mtl_sales_order_id;
12836 /*--------------------------------------------------------------------------
12837 Procedure Name : Insert_Into_Mtl_Sales_Orders
12838 Description    : This API creates a record in MTL_SALES_ORDERS for a given
12839                  order header.
12840                  Every header in oe_order_headers_all will have a record
12841                  in MTL_SALES_ORDERS. The unique key to get the sales_order_id
12842                  from mtl_sales_orders is
12843                  Order_Number
12844                  Order_Type (in base language)
12845                  OM:Source Code profile option (stored as ont_source_code).
12846 
12847                  The above values are stored in a flex in MTL_SALES_ORDERS.
12848                  SEGMENT1 : stores the order number
12849                  SEGMENT2 : stores the order type
12850                  SEGMENT3 : stores the ont_source_code value
12851 
12852 -------------------------------------------------------------------------- */
12853 Procedure Insert_Into_Mtl_Sales_Orders
12854 ( p_header_rec       IN  OE_ORDER_PUB.header_rec_type)
12855 IS
12856 l_order_type_name          VARCHAR2(80);
12857 l_source_code              VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
12858 l_sales_order_id           NUMBER;
12859 l_msg_data                 VARCHAR2(2000);
12860 l_msg_count                NUMBER;
12861 l_return_status            VARCHAR2(1);
12862 --
12863 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12864 --
12865 BEGIN
12866 
12867   IF l_debug_level  > 0 THEN
12868       oe_debug_pub.add(  'ENTERING INSERT_INTO_MTL_SALES_ORDERS' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
12869   END IF;
12870 
12871   BEGIN
12872   -- Fix for bug#1078323: the order type name should be selected in
12873   -- the base language
12874      SELECT NAME
12875      INTO l_order_type_name
12876      FROM OE_TRANSACTION_TYPES_TL
12877      WHERE TRANSACTION_TYPE_ID = p_header_rec.order_type_id
12878      AND language = (select language_code
12879                      from fnd_languages
12880                      where installed_flag = 'B');
12881   EXCEPTION
12882      WHEN NO_DATA_FOUND THEN
12883        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12884   END;
12885 
12886   IF l_debug_level  > 0 THEN
12887       oe_debug_pub.add(  'CALLING INVS CREATE_SALESORDER' , 1 ) ;
12888   END IF;
12889   IF l_debug_level  > 0 THEN
12890       oe_debug_pub.add(  'ORDER TYPE: ' || L_ORDER_TYPE_NAME , 1 ) ;
12891   END IF;
12892   IF l_debug_level  > 0 THEN
12893       oe_debug_pub.add(  'SOURCE CODE: ' || L_SOURCE_CODE , 1 ) ;
12894   END IF;
12895   IF l_debug_level  > 0 THEN
12896       oe_debug_pub.add(  'ORDER NUMBER : ' || P_HEADER_REC.ORDER_NUMBER , 1 ) ;
12897   END IF;
12898 
12899   inv_salesorder.create_salesorder
12900       ( p_api_version_number        => 1.0,
12901         p_segment1                  => p_header_rec.order_number,
12902         p_segment2                  => l_order_type_name,
12903         p_segment3                  => l_source_code,
12904         p_validation_date           => p_header_rec.creation_date,
12905         x_salesorder_id             => l_sales_order_id,
12906         x_message_data              => l_msg_data,
12907         x_message_count             => l_msg_count,
12908         x_return_status             => l_return_status);
12909 
12910 
12911   IF l_debug_level  > 0 THEN
12912       oe_debug_pub.add(  'L_MSG_COUNT ' || L_MSG_COUNT , 1 ) ;
12913   END IF;
12914   IF l_debug_level  > 0 THEN
12915       oe_debug_pub.add(  'RETURN STATUS ' || L_RETURN_STATUS , 1 ) ;
12916   END IF;
12917 
12918   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12919        oe_msg_pub.transfer_msg_stack;
12920        l_msg_count:=OE_MSG_PUB.COUNT_MSG;
12921        FOR I in 1..l_msg_count LOOP
12922           l_msg_data := OE_MSG_PUB.Get(I,'F');
12923           IF l_debug_level  > 0 THEN
12924               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
12925           END IF;
12926        END LOOP;
12927 
12928        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12929   ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
12930        oe_msg_pub.transfer_msg_stack;
12931        l_msg_count:=OE_MSG_PUB.COUNT_MSG;
12932        FOR I in 1..l_msg_count LOOP
12933           l_msg_data := OE_MSG_PUB.Get(I,'F');
12934           IF l_debug_level  > 0 THEN
12935               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
12936           END IF;
12937        END LOOP;
12938        RAISE FND_API.G_EXC_ERROR;
12939   END IF;
12940 
12941   IF l_debug_level  > 0 THEN
12942       oe_debug_pub.add(  'EXITING INSERT_INTO_MTL_SALES_ORDERS' , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
12943   END IF;
12944 
12945 EXCEPTION
12946 
12947     WHEN OTHERS THEN
12948 
12949         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
12950         THEN
12951             OE_MSG_PUB.Add_Exc_Msg
12952             (   G_PKG_NAME
12953             ,   'Insert_Into_Mtl_Sales_Orders'
12954             );
12955         END IF;
12956 
12957         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12958 
12959 END Insert_Into_Mtl_Sales_Orders;
12960 
12961 
12962 /*---------------------------------------------------------------------
12963 Procedure Name : call_process_order
12964 Description    : This process is called after scheduling is performed
12965                  on the line and the result needs to be verified and/or
12966                  updated to the database.
12967 --------------------------------------------------------------------- */
12968 Procedure call_process_order
12969 ( p_x_old_line_tbl      IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
12970 , p_x_line_tbl          IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
12971 , p_control_rec         IN  OE_GLOBALS.control_rec_type
12972 , p_caller              IN  VARCHAR2
12973 , x_return_status OUT NOCOPY VARCHAR2)
12974 
12975 IS
12976 is_set_recursion        VARCHAR2(1) := 'Y';
12977 --
12978 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
12979 --
12980 BEGIN
12981 
12982     IF l_debug_level  > 0 THEN
12983         oe_debug_pub.add(  'ENTERING CALL_PROCESS_ORDER' , 1 ) ;
12984     END IF;
12985 
12986     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
12987     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
12988 
12989     IF l_debug_level  > 0 THEN
12990         oe_debug_pub.add(  'CALLING PROCESS ORDER' , 1 ) ;
12991     END IF;
12992 
12993      -- Set global set recursive flag
12994      -- The global flag to supress the sets logic to fire in
12995      -- get set id api in lines
12996     IF NOT oe_set_util.g_set_recursive_flag  THEN
12997        is_set_recursion := 'N';
12998        oe_set_util.g_set_recursive_flag := TRUE;
12999     END IF;
13000 
13001     --  Call OE_Order_PVT.Process_order
13002 
13003     OE_Order_PVT.Lines
13004     (p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
13005      p_control_rec                 => p_control_rec,
13006      p_x_line_tbl                  => p_x_line_tbl,
13007      p_x_old_line_tbl              => p_x_old_line_tbl,
13008      x_return_status               => x_return_status);
13009 
13010     -- unset global set recursive flag
13011     -- The global flag to supress the sets logic to
13012     -- fire in get set id api in lines
13013 
13014     IF is_set_recursion  = 'N' THEN
13015        is_set_recursion := 'Y';
13016        oe_set_util.g_set_recursive_flag := FALSE;
13017     END IF;
13018 
13019     IF l_debug_level  > 0 THEN
13020         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS ORDER' , 1 ) ;
13021     END IF;
13022     IF l_debug_level  > 0 THEN
13023         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
13024     END IF;
13025 
13026     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13027         IF l_debug_level  > 0 THEN
13028             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13029         END IF;
13030         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13031     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13032         IF l_debug_level  > 0 THEN
13033             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13034         END IF;
13035         RAISE FND_API.G_EXC_ERROR;
13036     END IF;
13037 
13038     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13039     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13040 
13041      -- Resetting the variable after the Process Order API Call.
13042      OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N';  -- Added for ER 6110708
13043 
13044     IF l_debug_level  > 0 THEN
13045         oe_debug_pub.add(  'EXITING CALL PROCESS ORDER' , 1 ) ;
13046     END IF;
13047 EXCEPTION
13048 
13049    WHEN FND_API.G_EXC_ERROR THEN
13050 
13051         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13052         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13053         x_return_status := FND_API.G_RET_STS_ERROR;
13054 
13055     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13056 
13057         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13058         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13059         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13060 
13061     WHEN OTHERS THEN
13062 
13063         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13064         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13065         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13066 
13067         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13068         THEN
13069             OE_MSG_PUB.Add_Exc_Msg
13070             (   G_PKG_NAME
13071             ,   'Call_Process_Order'
13072             );
13073         END IF;
13074 END call_process_order;
13075 
13076 /*---------------------------------------------------------------------
13077 Procedure Name : Process_Group_of_Lines
13078 Description    : This process is called after scheduling is performed
13079                  on the line and the result needs to be verified and/or
13080                  updated to the database.
13081 --------------------------------------------------------------------- */
13082 Procedure Process_Group_of_Lines
13083 ( p_x_old_line_tbl      IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
13084 , p_x_line_tbl          IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type
13085 , p_caller              IN  VARCHAR2
13086 , x_return_status OUT NOCOPY VARCHAR2)
13087 
13088 IS
13089 l_process_requests BOOLEAN;
13090 I                  NUMBER;
13091 l_index            NUMBER;
13092 --
13093 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13094 --
13095 BEGIN
13096 
13097     IF l_debug_level  > 0 THEN
13098         oe_debug_pub.add(  'ENTERING PROCESS_GROUP_OF_LINES' , 1 ) ;
13099     END IF;
13100     x_return_status :=  FND_API.G_RET_STS_SUCCESS;
13101     SAVEPOINT Process_Group_of_Lines;
13102     I := p_x_line_tbl.FIRST;
13103     WHILE I IS NOT NULL LOOP
13104      BEGIN
13105       IF l_debug_level  > 0 THEN
13106           oe_debug_pub.add(  'OPERATION : ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
13107       END IF;
13108       IF p_x_line_tbl(I).operation <> OE_GLOBALS.G_OPR_NONE
13109       THEN
13110          Process_Line (p_old_line_rec    => p_x_old_line_tbl(I),
13111                        p_x_line_rec      => p_x_line_tbl(I),
13112                        p_caller          => p_caller,
13113                        p_call_prn        => FALSE,
13114                        x_return_status   => x_return_status);
13115 
13116          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13117             IF l_debug_level  > 0 THEN
13118                 oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13119             END IF;
13120             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13121          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13122            IF l_debug_level  > 0 THEN
13123                oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13124            END IF;
13125            RAISE FND_API.G_EXC_ERROR;
13126          END IF;
13127 
13128          IF p_x_line_tbl(I).top_model_line_id = p_x_line_tbl(I).line_id
13129          THEN
13130            IF l_debug_level  > 0 THEN
13131                oe_debug_pub.add(  'CLEAR THE CACHED TOP MODEL RECORD' , 1 ) ;
13132            END IF;
13133            IF l_debug_level  > 0 THEN
13134                oe_debug_pub.add(  'MODEL LINE: '|| P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
13135            END IF;
13136            OE_Order_Cache.Clear_Top_Model_Line(p_key => p_x_line_tbl(I).line_id);
13137          END IF;
13138 
13139       ELSIF  nvl(p_x_line_tbl(I).open_flag,'Y') = 'N' THEN
13140 
13141 
13142          IF l_debug_level  > 0 THEN
13143              oe_debug_pub.add(  'BEFORE DOING DIRECT UPDATE IN GROUP' , 1 ) ;
13144          END IF;
13145 
13146          p_x_line_tbl(I).last_update_date := SYSDATE;
13147          p_x_line_tbl(I).last_updated_by := FND_GLOBAL.USER_ID;
13148          p_x_line_tbl(I).last_update_login := FND_GLOBAL.LOGIN_ID;
13149          p_x_line_tbl(I).lock_control := p_x_line_tbl(I).lock_control + 1;
13150 
13151    -- added for notification framework
13152    --check code release level first. Notification framework is at Pack H level
13153       IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
13154         -- calling notification framework to get index position
13155         OE_ORDER_UTIL.Update_Global_Picture(p_Upd_New_Rec_If_Exists =>False,
13156                     p_old_line_rec => p_x_old_line_tbl(I),
13157                     p_line_rec =>p_x_line_tbl(I),
13158                     p_line_id => p_x_line_tbl(I).line_id,
13159                     x_index => l_index,
13160                     x_return_status => x_return_status);
13161           IF l_debug_level  > 0 THEN
13162               oe_debug_pub.add(  'UPDATE_GLOBAL RETURN STATUS FROM OE_SCHEDULE_UTIL.PROCESS_GROUP_OF_LINE IS: ' || X_RETURN_STATUS ) ;
13163           END IF;
13164           IF l_debug_level  > 0 THEN
13165               oe_debug_pub.add(  'GLOBAL PICTURE INDEX IS: ' || L_INDEX , 1 ) ;
13166           END IF;
13167 
13168          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13169               IF l_debug_level  > 0 THEN
13170                   oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13171               END IF;
13172            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13173          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13174              IF l_debug_level  > 0 THEN
13175                  oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13176              END IF;
13177               RAISE FND_API.G_EXC_ERROR;
13178           END IF;
13179       IF l_index is not NULL THEN
13180           --update Global Picture directly
13181            OE_ORDER_UTIL.g_line_tbl(l_index).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
13182            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_ship_date := p_x_line_tbl(I).schedule_ship_date;
13183            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_arrival_date := p_x_line_tbl(I).schedule_arrival_date;
13184            OE_ORDER_UTIL.g_line_tbl(l_index).delivery_lead_time    := p_x_line_tbl(I).delivery_lead_time;
13185            OE_ORDER_UTIL.g_line_tbl(l_index).mfg_lead_time         := p_x_line_tbl(I).mfg_lead_time;
13186            OE_ORDER_UTIL.g_line_tbl(l_index).shipping_method_code   := p_x_line_tbl(I).shipping_method_code;
13187            OE_ORDER_UTIL.g_line_tbl(l_index).schedule_status_code  := p_x_line_tbl(I).schedule_status_code;
13188            OE_ORDER_UTIL.g_line_tbl(l_index).visible_demand_flag    := p_x_line_tbl(I).visible_demand_flag;
13189            OE_ORDER_UTIL.g_line_tbl(l_index).latest_acceptable_date := p_x_line_tbl(I).latest_acceptable_date;
13190            OE_ORDER_UTIL.g_line_tbl(l_index).last_update_date       := p_x_line_tbl(I).last_update_date;
13191            OE_ORDER_UTIL.g_line_tbl(l_index).last_updated_by        := p_x_line_tbl(I).last_updated_by;
13192            OE_ORDER_UTIL.g_line_tbl(l_index).last_update_login      := p_x_line_tbl(I).last_update_login;
13193            OE_ORDER_UTIL.g_line_tbl(l_index).lock_control           := p_x_line_tbl(I).lock_control;
13194            IF l_debug_level  > 0 THEN
13195                oe_debug_pub.add(  'GLOBAL SHIP_FROM_ORG_ID IS: ' || OE_ORDER_UTIL.G_LINE_TBL ( L_INDEX ) .SHIP_FROM_ORG_ID , 1 ) ;
13196            END IF;
13197         END IF; /*l_index is not null check*/
13198       END IF;  /*code_release_level*/
13199 
13200          UPDATE OE_ORDER_LINES
13201          SET
13202           ship_from_org_id           = p_x_line_tbl(I).ship_from_org_id
13203          ,schedule_ship_date         = p_x_line_tbl(I).schedule_ship_date
13204          ,schedule_arrival_date      = p_x_line_tbl(I).schedule_arrival_date
13205          ,delivery_lead_time         = p_x_line_tbl(I).delivery_lead_time
13206          ,mfg_lead_time              = p_x_line_tbl(I).mfg_lead_time
13207          ,shipping_method_code       = p_x_line_tbl(I).shipping_method_code
13208          ,schedule_status_code       = p_x_line_tbl(I).schedule_status_code
13209          ,visible_demand_flag        = p_x_line_tbl(I).visible_demand_flag
13210          ,latest_acceptable_date     = p_x_line_tbl(I).latest_acceptable_date
13211          ,Original_Inventory_Item_Id
13212                               = p_x_line_tbl(I).Original_Inventory_Item_Id
13213          ,Original_item_identifier_Type
13214                               = p_x_line_tbl(I).Original_item_identifier_Type
13215          ,Original_ordered_item_id   = p_x_line_tbl(I).Original_ordered_item_id
13216          ,Original_ordered_item      = p_x_line_tbl(I).Original_ordered_item
13217          ,last_update_date           = p_x_line_tbl(I).last_update_date
13218          ,last_updated_by            = p_x_line_tbl(I).last_updated_by
13219          ,last_update_login          = p_x_line_tbl(I).last_update_login
13220          ,lock_control               = p_x_line_tbl(I).lock_control
13221          WHERE LINE_ID = p_x_line_tbl(I).line_id;
13222 
13223          IF l_debug_level  > 0 THEN
13224              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE' , 1 ) ;
13225          END IF;
13226       END IF;
13227 
13228      END;
13229      I := p_x_line_tbl.NEXT(I);
13230     END LOOP;
13231 
13232    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
13233     IF p_caller  = SCH_INTERNAL THEN
13234         l_process_requests := FALSE;
13235     ELSE
13236         l_process_requests := TRUE;
13237     END IF;
13238 
13239     OE_Order_PVT.Process_Requests_And_Notify
13240     ( p_process_requests        => l_process_requests
13241     , p_notify                  => FALSE
13242     , p_line_tbl                => p_x_line_tbl
13243     , p_old_line_tbl            => p_x_old_line_tbl
13244     , x_return_status           => x_return_status
13245     );
13246 
13247     IF l_debug_level  > 0 THEN
13248         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST' , 1 ) ;
13249     END IF;
13250     IF l_debug_level  > 0 THEN
13251         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
13252     END IF;
13253 
13254     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13255         IF l_debug_level  > 0 THEN
13256             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13257         END IF;
13258         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13259     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13260         IF l_debug_level  > 0 THEN
13261             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13262         END IF;
13263         RAISE FND_API.G_EXC_ERROR;
13264     END IF;
13265 
13266  ELSE  /*pre pack H*/
13267      -- Do not process delayed requests if this was a recursive
13268      -- call (e.g. from oe_line_util.pre_write_process)
13269 
13270     IF p_caller  = SCH_INTERNAL THEN
13271         l_process_requests := FALSE;
13272     ELSE
13273         l_process_requests := TRUE;
13274     END IF;
13275 
13276     OE_Order_PVT.Process_Requests_And_Notify
13277     ( p_process_requests        => l_process_requests
13278     , p_notify                  => TRUE
13279     , p_line_tbl                => p_x_line_tbl
13280     , p_old_line_tbl            => p_x_old_line_tbl
13281     , x_return_status           => x_return_status
13282     );
13283 
13284     IF l_debug_level  > 0 THEN
13285         oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST' , 1 ) ;
13286     END IF;
13287     IF l_debug_level  > 0 THEN
13288         oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
13289     END IF;
13290 
13291     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13292         IF l_debug_level  > 0 THEN
13293             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13294         END IF;
13295         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13296     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13297         IF l_debug_level  > 0 THEN
13298             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13299         END IF;
13300         RAISE FND_API.G_EXC_ERROR;
13301     END IF;
13302  END IF; /*code_release_level*/
13303 
13304 
13305     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13306     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13307     IF l_debug_level  > 0 THEN
13308         oe_debug_pub.add(  'PROCESS GROUP ' || OESCH_PERFORM_SCHEDULING , 1 ) ;
13309     END IF;
13310     IF l_debug_level  > 0 THEN
13311         oe_debug_pub.add(  'EXITING PROCESS GROUP OF LINES' , 1 ) ;
13312     END IF;
13313 EXCEPTION
13314 
13315    WHEN FND_API.G_EXC_ERROR THEN
13316 
13317         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13318         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13319         x_return_status := FND_API.G_RET_STS_ERROR;
13320         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
13321 
13322     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13323 
13324         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13325         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13326         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13327         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
13328 
13329     WHEN OTHERS THEN
13330 
13331         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13332         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13333         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13334         ROLLBACK TO SAVEPOINT Process_Group_of_lines;
13335 
13336         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13337         THEN
13338             OE_MSG_PUB.Add_Exc_Msg
13339             (   G_PKG_NAME
13340             ,   'Call_Process_Order'
13341             );
13342         END IF;
13343 END Process_Group_of_Lines;
13344 
13345 
13346 /*---------------------------------------------------------------------
13347 Procedure Name : Process_Line
13348 Description    : This process is called after scheduling is performed
13349                  on the line and the result needs to be verified and/or
13350                  updated to the database.
13351 
13352 New code :  If the warehouse is changed on the line due to scheduling
13353             we will follow older approch. If warehouse is not changed ont
13354             the line then call security based on the operation. Call new API
13355             for clear-dep and defaulting. Based on  the output from the new api
13356             either call process order or do a direct update.
13357             p_call_prn is call process_request and notify.
13358 --------------------------------------------------------------------- */
13359 Procedure Process_Line
13360 ( p_old_line_rec        IN  OE_ORDER_PUB.line_rec_type
13361 , p_x_line_rec          IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type
13362 , p_caller              IN  VARCHAR2
13363 , p_call_prn            IN  BOOLEAN := TRUE
13364 , x_return_status OUT NOCOPY VARCHAR2)
13365 
13366 IS
13367 l_process_requests      BOOLEAN;
13368 l_line_tbl              OE_ORDER_PUB.line_tbl_type;
13369 l_old_line_tbl          OE_ORDER_PUB.line_tbl_type;
13370 l_sec_result            NUMBER;
13371 l_order_type_id         NUMBER := OE_Order_Cache.g_header_rec.order_type_id;
13372 l_control_rec           OE_GLOBALS.control_rec_type;
13373 l_src_attr_tbl          OE_GLOBALS.NUMBER_Tbl_Type;
13374 l_index                 NUMBER;
13375 --
13376 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13377 --
13378 BEGIN
13379 
13380     x_return_status :=  FND_API.G_RET_STS_SUCCESS;
13381     IF l_debug_level  > 0 THEN
13382         oe_debug_pub.add(  'ENTERING PROCESS_LINE' , 1 ) ;
13383     END IF;
13384     IF l_debug_level  > 0 THEN
13385         oe_debug_pub.add(  'P_CALLER ' || P_CALLER , 1 ) ;
13386     END IF;
13387 
13388     /* Start Audit Trail */
13389     --Added is_mass_change condition for bug 4911340
13390     IF NOT OE_GLOBALS.G_UI_FLAG AND OE_MASS_CHANGE_PVT.IS_MASS_CHANGE = 'F' THEN
13391       p_x_line_rec.change_reason := 'SYSTEM';
13392     END IF;
13393 
13394    -- p_x_line_rec.change_comments := 'Scheduling Action';
13395     /* End Audit Trail */
13396 
13397    -- When warehouse is changed on the line then call process_order.
13398 
13399    IF nvl(p_x_line_rec.open_flag,'Y') = 'N' THEN
13400 
13401                                    IF l_debug_level  > 0 THEN
13402                                        oe_debug_pub.add(  'DIRECT UPDATE FOR A CANCELLED LINE' || P_X_LINE_REC.LINE_ID , 1 ) ;
13403                                    END IF;
13404 
13405          p_x_line_rec.last_update_date := SYSDATE;
13406          p_x_line_rec.last_updated_by := FND_GLOBAL.USER_ID;
13407          p_x_line_rec.last_update_login := FND_GLOBAL.LOGIN_ID;
13408          p_x_line_rec.lock_control := p_x_line_rec.lock_control + 1;
13409 
13410          UPDATE OE_ORDER_LINES
13411          SET
13412           ship_from_org_id           = p_x_line_rec.ship_from_org_id
13413          ,schedule_ship_date         = p_x_line_rec.schedule_ship_date
13414          ,schedule_arrival_date      = p_x_line_rec.schedule_arrival_date
13415          ,delivery_lead_time         = p_x_line_rec.delivery_lead_time
13416          ,mfg_lead_time              = p_x_line_rec.mfg_lead_time
13417          ,shipping_method_code       = p_x_line_rec.shipping_method_code
13418          ,schedule_status_code       = p_x_line_rec.schedule_status_code
13419          ,visible_demand_flag        = p_x_line_rec.visible_demand_flag
13420          ,Original_Inventory_Item_Id = p_x_line_rec.Original_Inventory_Item_Id
13421          ,Original_item_identifier_Type
13422                                  = p_x_line_rec.Original_item_identifier_Type
13423          ,Original_ordered_item_id   = p_x_line_rec.Original_ordered_item_id
13424          ,Original_ordered_item      = p_x_line_rec.Original_ordered_item
13425          ,latest_acceptable_date     = p_x_line_rec.latest_acceptable_date
13426          ,override_atp_date_code     = p_x_line_rec.override_atp_date_code
13427          ,firm_demand_flag           = p_x_line_rec.firm_demand_flag
13428          ,earliest_ship_date         = p_x_line_rec.earliest_ship_date
13429          ,last_update_date           = p_x_line_rec.last_update_date
13430          ,last_updated_by            = p_x_line_rec.last_updated_by
13431          ,last_update_login          = p_x_line_rec.last_update_login
13432          ,lock_control               = p_x_line_rec.lock_control
13433          WHERE LINE_ID = p_x_line_rec.line_id;
13434 
13435          IF l_debug_level  > 0 THEN
13436              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE ON A CANCELLED LINE' , 1 ) ;
13437          END IF;
13438 
13439    ELSIF NOT OE_GLOBALS.Equal(p_x_line_rec.ship_from_org_id,
13440                            p_old_line_rec.ship_from_org_id)
13441    OR    NOT OE_GLOBALS.Equal(p_x_line_rec.ship_to_org_id,
13442                            p_old_line_rec.ship_to_org_id)
13443    OR    NOT OE_GLOBALS.Equal(p_x_line_rec.inventory_item_id,
13444                            p_old_line_rec.inventory_item_id)
13445    THEN
13446 
13447     IF l_debug_level  > 0 THEN
13448         oe_debug_pub.add(  'WAREHOUSE/SHIP TO HAS CHANGED ON THE LINE CALLING PO' , 1 ) ;
13449     END IF;
13450 
13451     l_control_rec.controlled_operation := TRUE;
13452     l_control_rec.change_attributes    := TRUE;
13453 
13454     l_control_rec.clear_dependents     := TRUE;
13455     l_control_rec.default_attributes   := TRUE;
13456     l_control_rec.check_security       := TRUE;
13457 
13458     l_control_rec.write_to_DB          := TRUE;
13459     l_control_rec.validate_entity      := TRUE;
13460     l_control_rec.process              := FALSE;
13461 
13462     --  Instruct API to retain its caches
13463 
13464     l_control_rec.clear_api_cache      := FALSE;
13465     l_control_rec.clear_api_requests   := FALSE;
13466 
13467     l_old_line_tbl(1) := p_old_line_rec;
13468     l_line_tbl(1)     := p_x_line_rec;
13469 
13470     -- We are doing this since we are calling the po from post write.
13471 
13472     l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
13473 
13474     Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
13475                        p_x_line_tbl      => l_line_tbl,
13476                        p_control_rec     => l_control_rec,
13477                        p_caller          => p_caller,
13478                        x_return_status   => x_return_status);
13479 
13480     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13481        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13482     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13483        RAISE FND_API.G_EXC_ERROR;
13484     END IF;
13485 
13486    ELSE -- warehouse is not changed on the line.
13487 
13488       IF p_x_line_rec.operation = OE_GLOBALS.G_OPR_UPDATE THEN
13489 
13490         IF l_debug_level  > 0 THEN
13491             oe_debug_pub.add(  'IT IS UPDATE , CALL SECURITY' ) ;
13492         END IF;
13493 
13494         IF p_caller = 'INTERNAL' THEN      -- 5999034
13495            p_x_line_rec.change_reason := 'SYSTEM';
13496         END IF;
13497 
13498 
13499         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
13500 
13501         OE_Line_Security.Attributes
13502                 ( p_line_rec        => p_x_line_rec
13503                 , p_old_line_rec    => p_old_line_rec
13504                 , x_result          => l_sec_result
13505                 , x_return_status   => x_return_status
13506                 );
13507 
13508         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13509            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13510         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13511            RAISE FND_API.G_EXC_ERROR;
13512         END IF;
13513 
13514         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13515         IF l_debug_level  > 0 THEN
13516             oe_debug_pub.add(  'AFTER SECURITY CALL' || X_RETURN_STATUS , 1 ) ;
13517         END IF;
13518 
13519          -- if operation on any attribute is constrained
13520         IF l_sec_result = OE_PC_GLOBALS.YES THEN
13521            IF l_debug_level  > 0 THEN
13522                oe_debug_pub.add(  'CONSTRAINT FOUND' , 4 ) ;
13523            END IF;
13524            RAISE FND_API.G_EXC_ERROR;
13525         END IF;
13526 
13527       END IF; -- operation update.
13528 
13529       --Value of the G_ATTR_UPDATED_BY_DEF will be set in defaulting
13530       -- We will re-set the value, before calling.
13531       OE_GLOBALS.G_ATTR_UPDATED_BY_DEF := 'N';
13532 
13533       IF l_debug_level  > 0 THEN
13534           oe_debug_pub.add(  'OLD SHIP :' || P_OLD_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
13535       END IF;
13536       IF l_debug_level  > 0 THEN
13537           oe_debug_pub.add(  'NEW SHIP :' || P_X_LINE_REC.SCHEDULE_SHIP_DATE , 1 ) ;
13538       END IF;
13539 
13540      IF NOT OE_GLOBALS.Equal(trunc(p_x_line_rec.schedule_ship_date),     -- 5999034
13541                               trunc(p_old_line_rec.schedule_ship_date))
13542 
13543       THEN
13544 
13545        IF l_debug_level  > 0 THEN
13546            oe_debug_pub.add(  'SHIP_DATE HAS CHANGED ON THE LINE' , 1 ) ;
13547        END IF;
13548        l_src_attr_tbl(1) := OE_LINE_UTIL.G_SCHEDULE_SHIP_DATE;
13549 
13550        OE_Line_Util_Ext.Clear_Dep_And_Default
13551           ( p_src_attr_tbl    => l_src_attr_tbl,
13552             p_x_line_rec      => p_x_line_rec,
13553             p_old_line_rec    => p_old_line_rec);
13554        IF l_debug_level  > 0 THEN
13555            oe_debug_pub.add(  'AFTER CALLING CLEAR_DEP_AND_DEFAULT' , 1 ) ;
13556        END IF;
13557 
13558       END IF;
13559       IF l_debug_level  > 0 THEN
13560           oe_debug_pub.add(  'DIRECT/PO ' || OE_GLOBALS.G_ATTR_UPDATED_BY_DEF , 1 ) ;
13561       END IF;
13562       IF OE_GLOBALS.G_ATTR_UPDATED_BY_DEF = 'Y' THEN
13563 
13564 
13565          l_control_rec.controlled_operation := TRUE;
13566          l_control_rec.change_attributes    := TRUE;
13567 
13568          l_control_rec.clear_dependents     := FALSE;
13569          l_control_rec.default_attributes   := FALSE;
13570          l_control_rec.check_security       := FALSE;
13571 
13572          l_control_rec.write_to_DB          := TRUE;
13573          l_control_rec.validate_entity      := TRUE;
13574          l_control_rec.process              := FALSE;
13575 
13576          --  Instruct API to retain its caches
13577 
13578          l_control_rec.clear_api_cache      := FALSE;
13579          l_control_rec.clear_api_requests   := FALSE;
13580 
13581          l_old_line_tbl(1) := p_old_line_rec;
13582          l_line_tbl(1)     := p_x_line_rec;
13583 
13584          -- We are doing this since we are calling the po from post write.
13585 
13586          l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
13587 
13588          Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
13589                             p_x_line_tbl      => l_line_tbl,
13590                             p_control_rec     => l_control_rec,
13591                             p_caller          => p_caller,
13592                             x_return_status   => x_return_status);
13593 
13594          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13595             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13596          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13597             RAISE FND_API.G_EXC_ERROR;
13598          END IF;
13599 
13600       ELSE
13601          IF l_debug_level  > 0 THEN
13602              oe_debug_pub.add(  ' DEFAULTING HAS NOT CHANGED ANY THING' , 1 ) ;
13603          END IF;
13604          IF l_debug_level  > 0 THEN
13605              oe_debug_pub.add(  'BEFORE CALLING LOG_SCHEDULING REQUESTS' , 1 ) ;
13606          END IF;
13607 
13608          OE_MSG_PUB.set_msg_context
13609          (p_entity_code                => 'LINE'
13610          ,p_entity_id                  => p_x_line_rec.line_id
13611          ,p_header_id                  => p_x_line_rec.header_id
13612          ,p_line_id                    => p_x_line_rec.line_id
13613          ,p_orig_sys_document_ref      => p_x_line_rec.orig_sys_document_ref
13614          ,p_orig_sys_document_line_ref => p_x_line_rec.orig_sys_line_ref
13615          ,p_orig_sys_shipment_ref      => p_x_line_rec.orig_sys_shipment_ref
13616          ,p_change_sequence            => p_x_line_rec.change_sequence
13617          ,p_source_document_id         => p_x_line_rec.source_document_id
13618          ,p_source_document_line_id    => p_x_line_rec.source_document_line_id
13619          ,p_order_source_id            => p_x_line_rec.order_source_id
13620          ,p_source_document_type_id    => p_x_line_rec.source_document_type_id);
13621 
13622          OE_LINE_UTIL.Log_Scheduling_Requests
13623          (p_x_line_rec    => p_x_line_rec
13624          ,p_old_line_rec  => p_old_line_rec
13625          ,p_caller        => p_caller
13626          ,p_order_type_id => l_order_type_id
13627          ,x_return_status => x_return_status);
13628 
13629          IF l_debug_level  > 0 THEN
13630              oe_debug_pub.add(  'AFTER CALLING LOG_SCHEDULING REQUESTS' || X_RETURN_STATUS , 1 ) ;
13631          END IF;
13632 
13633          IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13634             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13635          ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13636             RAISE FND_API.G_EXC_ERROR;
13637          END IF;
13638 
13639          IF l_debug_level  > 0 THEN
13640              oe_debug_pub.add(  'BEFORE DOING DIRECT UPDATE' , 1 ) ;
13641          END IF;
13642          IF NOT validate_ship_method (p_x_line_rec.shipping_method_code,
13643                                   p_old_line_rec.shipping_method_code,
13644                                   p_x_line_rec.ship_from_org_id) THEN
13645 
13646            p_x_line_rec.shipping_method_code := Null;
13647            p_x_line_rec.freight_carrier_code := Null;
13648          END IF;
13649 
13650          -- Start 2806483
13651          IF (p_x_line_rec.shipping_method_code IS NOT NULL
13652          AND p_x_line_rec.shipping_method_code <> FND_API.G_MISS_CHAR)
13653          AND NOT OE_GLOBALS.EQUAL(p_x_line_rec.shipping_method_code
13654                                  ,p_old_line_rec.shipping_method_code)
13655          THEN
13656 
13657             p_x_line_rec.freight_carrier_code :=
13658                     OE_Default_Line.Get_Freight_Carrier(p_line_rec => p_x_line_rec,
13659                                                         p_old_line_rec => p_old_line_rec);
13660          END IF;
13661          -- End 2806483
13662 
13663          p_x_line_rec.last_update_date := SYSDATE;
13664          p_x_line_rec.last_updated_by := FND_GLOBAL.USER_ID;
13665          p_x_line_rec.last_update_login := FND_GLOBAL.LOGIN_ID;
13666          p_x_line_rec.lock_control := p_x_line_rec.lock_control + 1;
13667 
13668          -- Pack J: Promise Date is added to the update to reflect any change to promise date
13669          UPDATE OE_ORDER_LINES
13670          SET
13671           ship_from_org_id           = p_x_line_rec.ship_from_org_id
13672          ,schedule_ship_date         = p_x_line_rec.schedule_ship_date
13673          ,schedule_arrival_date      = p_x_line_rec.schedule_arrival_date
13674          ,delivery_lead_time         = p_x_line_rec.delivery_lead_time
13675          ,mfg_lead_time              = p_x_line_rec.mfg_lead_time
13676          ,shipping_method_code       = p_x_line_rec.shipping_method_code
13677          ,schedule_status_code       = p_x_line_rec.schedule_status_code
13678          ,visible_demand_flag        = p_x_line_rec.visible_demand_flag
13679          ,Original_Inventory_Item_Id = p_x_line_rec.Original_Inventory_Item_Id
13680          ,Original_item_identifier_Type
13681                                   = p_x_line_rec.Original_item_identifier_Type
13682          ,Original_ordered_item_id   = p_x_line_rec.Original_ordered_item_id
13683          ,Original_ordered_item      = p_x_line_rec.Original_ordered_item
13684          ,latest_acceptable_date     = p_x_line_rec.latest_acceptable_date
13685          ,override_atp_date_code     = p_x_line_rec.override_atp_date_code
13686          ,freight_carrier_code       = p_x_line_rec.freight_carrier_code
13687          ,firm_demand_flag           = p_x_line_rec.firm_demand_flag
13688          ,earliest_ship_date         = p_x_line_rec.earliest_ship_date
13689          ,promise_date               = p_x_line_rec.promise_date
13690          ,last_update_date           = p_x_line_rec.last_update_date
13691          ,last_updated_by            = p_x_line_rec.last_updated_by
13692          ,last_update_login          = p_x_line_rec.last_update_login
13693          ,lock_control               = p_x_line_rec.lock_control
13694          WHERE LINE_ID = p_x_line_rec.line_id;  --2806483 Added Fright_carrier_code
13695 
13696          IF l_debug_level  > 0 THEN
13697              oe_debug_pub.add(  'AFTER DOING DIRECT UPDATE' , 1 ) ;
13698          END IF;
13699 
13700       END IF; -- OE_GLOBALS.G_ATTR_UPDATED_BY_DEF.
13701    END IF; -- warehouse is changed.
13702 
13703    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13704       IF l_debug_level  > 0 THEN
13705           oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13706       END IF;
13707       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13708    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13709       IF l_debug_level  > 0 THEN
13710           oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13711       END IF;
13712       RAISE FND_API.G_EXC_ERROR;
13713    END IF;
13714 
13715    IF l_line_tbl.count = 0 THEN
13716 
13717       l_old_line_tbl(1) := p_old_line_rec;
13718       l_line_tbl(1)     := p_x_line_rec;
13719 
13720        -- We are doing this since we are calling the po from post write.
13721 
13722       l_line_tbl(1).operation := OE_GLOBALS.G_OPR_UPDATE;
13723 
13724 
13725    END IF;
13726 
13727    -- added for notification framework
13728    --check code release level first. Notification framework is at Pack H level
13729 
13730    IF OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110508' THEN
13731 
13732         -- calling notification framework to get index position
13733         OE_ORDER_UTIL.Update_Global_Picture(p_Upd_New_Rec_If_Exists =>True,--changed for bug 8737932
13734                     p_old_line_rec => p_old_line_rec,
13735                     p_line_rec =>p_x_line_rec,
13736                     p_line_id => p_x_line_rec.line_id,
13737                     x_index => l_index,
13738                     x_return_status => x_return_status);
13739                            IF l_debug_level  > 0 THEN
13740                                oe_debug_pub.add(  'UPDATE_GLOBAL RETURN STATUS FROM OE_SCHEDULE_UTIL.PROCESS_LINE IS: ' || X_RETURN_STATUS ) ;
13741                            END IF;
13742         IF l_debug_level  > 0 THEN
13743             oe_debug_pub.add(  'GLOBAL PICTURE INDEX IS: ' || L_INDEX , 1 ) ;
13744         END IF;
13745 
13746         IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13747            IF l_debug_level  > 0 THEN
13748                oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13749            END IF;
13750            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13751         ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13752            IF l_debug_level  > 0 THEN
13753                oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13754            END IF;
13755            RAISE FND_API.G_EXC_ERROR;
13756         END IF;
13757 
13758         IF l_index is not NULL THEN
13759           --update Global Picture directly
13760           OE_ORDER_UTIL.g_line_tbl(l_index).ship_from_org_id := p_x_line_rec.ship_from_org_id;
13761           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_ship_date := p_x_line_rec.schedule_ship_date;
13762           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_arrival_date := p_x_line_rec.schedule_arrival_date;
13763           OE_ORDER_UTIL.g_line_tbl(l_index).delivery_lead_time    := p_x_line_rec.delivery_lead_time;
13764           OE_ORDER_UTIL.g_line_tbl(l_index).mfg_lead_time         := p_x_line_rec.mfg_lead_time;
13765           OE_ORDER_UTIL.g_line_tbl(l_index).shipping_method_code   := p_x_line_rec.shipping_method_code;
13766           OE_ORDER_UTIL.g_line_tbl(l_index).schedule_status_code  := p_x_line_rec.schedule_status_code;
13767           OE_ORDER_UTIL.g_line_tbl(l_index).visible_demand_flag    := p_x_line_rec.visible_demand_flag;
13768           OE_ORDER_UTIL.g_line_tbl(l_index).latest_acceptable_date := p_x_line_rec.latest_acceptable_date;
13769           OE_ORDER_UTIL.g_line_tbl(l_index).last_update_date       := p_x_line_rec.last_update_date;
13770           OE_ORDER_UTIL.g_line_tbl(l_index).last_updated_by        := p_x_line_rec.last_updated_by;
13771           OE_ORDER_UTIL.g_line_tbl(l_index).last_update_login      := p_x_line_rec.last_update_login;
13772           OE_ORDER_UTIL.g_line_tbl(l_index).lock_control           := p_x_line_rec.lock_control;
13773           IF l_debug_level  > 0 THEN
13774               oe_debug_pub.add(  'GLOBAL SHIP_FROM_ORG_ID IS: ' || OE_ORDER_UTIL.G_LINE_TBL ( L_INDEX ) .SHIP_FROM_ORG_ID , 1 ) ;
13775           END IF;
13776         END IF; /*l_index is not null check*/
13777 
13778     -- Do not execute delayed request if it is a internal call.
13779     -- call (e.g. from oe_line_util.post_write_process)
13780 
13781     IF p_call_prn THEN
13782 
13783       IF p_caller = SCH_INTERNAL THEN
13784           l_process_requests := FALSE;
13785       ELSE
13786           l_process_requests := TRUE;
13787       END IF;
13788 
13789       OE_Order_PVT.Process_Requests_And_Notify
13790       ( p_process_requests        => l_process_requests
13791       , p_notify                  => FALSE
13792       , p_line_tbl                => l_line_tbl
13793       , p_old_line_tbl            => l_old_line_tbl
13794       , x_return_status           => x_return_status);
13795 
13796 
13797       IF l_debug_level  > 0 THEN
13798           oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST AND NOTIFY' , 1 ) ;
13799       END IF;
13800       IF l_debug_level  > 0 THEN
13801           oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
13802       END IF;
13803 
13804       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13805           IF l_debug_level  > 0 THEN
13806               oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13807           END IF;
13808           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13809       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13810           IF l_debug_level  > 0 THEN
13811               oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13812           END IF;
13813           RAISE FND_API.G_EXC_ERROR;
13814       END IF;
13815      END IF;
13816   ELSE /*pre pack H*/
13817 
13818     -- Do not execute delayed request if it is a internal call.
13819     -- call (e.g. from oe_line_util.post_write_process)
13820 
13821     IF p_call_prn THEN
13822 
13823       IF p_caller = SCH_INTERNAL THEN
13824           l_process_requests := FALSE;
13825       ELSE
13826           l_process_requests := TRUE;
13827       END IF;
13828 
13829       OE_Order_PVT.Process_Requests_And_Notify
13830       ( p_process_requests        => l_process_requests
13831       , p_notify                  => TRUE
13832       , p_line_tbl                => l_line_tbl
13833       , p_old_line_tbl            => l_old_line_tbl
13834       , x_return_status           => x_return_status);
13835 
13836 
13837       IF l_debug_level  > 0 THEN
13838           oe_debug_pub.add(  'SCH: AFTER CALLING PROCESS REQUEST AND NOTIFY' , 1 ) ;
13839       END IF;
13840       IF l_debug_level  > 0 THEN
13841           oe_debug_pub.add(  'X_RETURN_STATUS IS ' || X_RETURN_STATUS , 1 ) ;
13842       END IF;
13843 
13844       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13845           IF l_debug_level  > 0 THEN
13846               oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
13847           END IF;
13848           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13849       ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13850           IF l_debug_level  > 0 THEN
13851               oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
13852           END IF;
13853           RAISE FND_API.G_EXC_ERROR;
13854       END IF;
13855 
13856     END IF;
13857  END IF; /*code_release_level*/
13858     IF l_debug_level  > 0 THEN
13859         oe_debug_pub.add(  'EXITING PROCESS_LINE ' , 1 ) ;
13860     END IF;
13861 
13862 EXCEPTION
13863 
13864    WHEN FND_API.G_EXC_ERROR THEN
13865 
13866         x_return_status := FND_API.G_RET_STS_ERROR;
13867         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13868         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13869 
13870     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13871 
13872         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13873         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13874         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13875 
13876     WHEN OTHERS THEN
13877 
13878         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13879         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13880         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
13881 
13882         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
13883         THEN
13884             OE_MSG_PUB.Add_Exc_Msg
13885             (   G_PKG_NAME
13886             ,   'Process_Line'
13887             );
13888         END IF;
13889 END Process_Line;
13890 /*--------------------------------------------------------
13891 PROCEDURE ATP_Check
13892 
13893 --------------------------------------------------------*/
13894 Procedure ATP_Check
13895          ( p_old_line_rec       IN  OE_ORDER_PUB.line_rec_type,
13896            p_x_line_rec         IN  OUT NOCOPY OE_ORDER_PUB.line_rec_type,
13897            p_validate           IN  VARCHAR2 := FND_API.G_TRUE,
13898 x_atp_tbl OUT NOCOPY OE_ATP.atp_tbl_type,
13899 
13900 x_return_status OUT NOCOPY VARCHAR2
13901 
13902            )
13903 IS
13904 
13905   -- BUG 1955004
13906   l_scheduling_level_code VARCHAR2(30);
13907   -- END 1955004
13908 
13909 --
13910 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
13911 --
13912 BEGIN
13913 
13914      IF l_debug_level  > 0 THEN
13915          oe_debug_pub.add(  'ENTERING ATP_CHECK ' , 2 ) ;
13916      END IF;
13917   -- Since we are not calling need scheduling from here
13918 
13919    g_atp_tbl.Delete;
13920 
13921    IF p_validate = FND_API.G_TRUE THEN
13922 
13923       IF l_debug_level  > 0 THEN
13924           oe_debug_pub.add(  'BEFORE CALLING VALIDATE LINE' , 2 ) ;
13925       END IF;
13926       Validate_Line(p_line_rec      => p_x_line_rec,
13927                     p_old_line_rec  => p_old_line_rec,
13928                     p_sch_action    => p_x_line_rec.schedule_action_code,
13929                     x_return_status => x_return_status);
13930 
13931    END IF;
13932 
13933   -- BUG 1955004
13934   l_scheduling_level_code := Get_Scheduling_Level(p_x_line_rec.header_id,
13935                                                    p_x_line_rec.line_type_id);
13936 
13937   IF l_scheduling_level_code IS NULL THEN
13938 
13939      l_scheduling_level_code := SCH_LEVEL_THREE;
13940   END IF;
13941 
13942   IF l_debug_level  > 0 THEN
13943       oe_debug_pub.add(  'SCHEDULING_LEVEL_CODE IS:' || L_SCHEDULING_LEVEL_CODE , 2 ) ;
13944   END IF;
13945   -- 3763015
13946   IF l_scheduling_level_code <> SCH_LEVEL_FOUR AND
13947      l_scheduling_level_code <> SCH_LEVEL_FIVE AND
13948      NVL(fnd_profile.value('ONT_BYPASS_ATP'),'N') = 'N' AND
13949      (Nvl(p_x_line_rec.bypass_sch_flag, 'N') = 'N'
13950      OR p_x_line_rec.bypass_sch_flag = FND_API.G_MISS_CHAR ) THEN --14043008
13951     -- FOUR and FIVE CANNOT have ATP Performed
13952     -- validation may be off, so we we need to double-check here
13953    --END 1955004
13954 
13955    IF l_debug_level  > 0 THEN
13956        oe_debug_pub.add(  'BEFORE CALLING ACTION_ATP ' , 2 ) ;
13957    END IF;
13958    Action_ATP(p_x_line_rec    => p_x_line_rec,
13959               p_old_line_rec  => p_old_line_rec,
13960               x_return_status => x_return_status);
13961 
13962 
13963 
13964    IF l_debug_level  > 0 THEN
13965        oe_debug_pub.add(  'AFTER CALLING ACTION_ATP ' || X_RETURN_STATUS , 2 ) ;
13966    END IF;
13967    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
13968       IF l_debug_level  > 0 THEN
13969           oe_debug_pub.add(  'UNEXPECTED ERROR FROM ' , 1 ) ;
13970       END IF;
13971       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13972    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
13973       IF l_debug_level  > 0 THEN
13974           oe_debug_pub.add(  'RR: L2' , 1 ) ;
13975       END IF;
13976       RAISE FND_API.G_EXC_ERROR;
13977    END IF;
13978 
13979     x_atp_tbl := g_atp_tbl;
13980 
13981    END IF;  -- for new IF check added for 1955004
13982 
13983     IF l_debug_level  > 0 THEN
13984         oe_debug_pub.add(  'EXITING ATP_CHECK ' , 2 ) ;
13985     END IF;
13986 EXCEPTION
13987 
13988    WHEN FND_API.G_EXC_ERROR THEN
13989 
13990       x_return_status := FND_API.G_RET_STS_ERROR;
13991       RAISE FND_API.G_EXC_ERROR;
13992 
13993     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13994 
13995       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13996       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13997 
13998     WHEN OTHERS THEN
13999 
14000       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14001 
14002       IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14003       THEN
14004           OE_MSG_PUB.Add_Exc_Msg
14005           (   G_PKG_NAME
14006           ,   'ATP_Check'
14007           );
14008       END IF;
14009       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14010 
14011 END ATP_Check;
14012 
14013 /*--------------------------------------------------------
14014 PROCEDURE Multi_ATP_Check
14015 
14016 --------------------------------------------------------*/
14017 PROCEDURE Multi_ATP_Check
14018          ( p_old_line_tbl       IN  OE_ORDER_PUB.line_tbl_type,
14019            p_x_line_tbl         IN  OUT NOCOPY OE_ORDER_PUB.line_tbl_type,
14020 x_atp_tbl OUT NOCOPY OE_ATP.atp_tbl_type,
14021 
14022 x_return_status OUT NOCOPY VARCHAR2
14023 
14024            )
14025 IS
14026 l_msg_count               NUMBER;
14027 l_mrp_msg_data            VARCHAR2(2000);
14028 l_session_id              NUMBER := 0;
14029 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
14030 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
14031 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
14032 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
14033 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
14034 l_on_hand_qty             NUMBER;
14035 l_avail_to_reserve        NUMBER;
14036 l_on_hand_qty2             NUMBER; -- INVCONV
14037 l_avail_to_reserve2        NUMBER; -- invconv
14038 l_process_flag            VARCHAR2(1) := FND_API.G_FALSE;
14039 --ER#14763609
14040 l_fulfillment_base            oe_order_lines_all.fulfillment_base%type;
14041 l_secondary_uom_code          mtl_system_items_b.secondary_uom_code%type;
14042 l_primary_uom_code            mtl_system_items_b.primary_uom_code%type;
14043 l_on_hand_ord_qty             NUMBER ;
14044 l_avail_to_reserve_ord        NUMBER ;
14045 l_sub_secondary_uom_code      mtl_system_items_b.secondary_uom_code%type;
14046 l_sub_primary_uom_code        mtl_system_items_b.primary_uom_code%type;
14047 l_sub_on_hand_ord_qty         NUMBER ;
14048 l_sub_avail_to_reserve_ord    NUMBER ;
14049 --End ER#14763609
14050 
14051 --
14052 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14053 --
14054 BEGIN
14055 
14056    IF l_debug_level  > 0 THEN
14057        oe_debug_pub.add(  'ENTERING MULTI ATP_CHECK ' , 2 ) ;
14058    END IF;
14059 
14060    g_atp_tbl.Delete;
14061 
14062    IF l_debug_level  > 0 THEN
14063        oe_debug_pub.add(  'BEFORE CALLING LOAD_MRP_TBL ' , 2 ) ;
14064    END IF;
14065 
14066    Load_MRP_request_from_tbl
14067        ( p_line_tbl      => p_x_line_tbl
14068         ,p_old_line_tbl  => p_old_line_tbl
14069         ,p_sch_action    => OESCH_ACT_ATP_CHECK
14070         ,x_mrp_atp_rec   => l_mrp_atp_rec);
14071 
14072    IF l_debug_level  > 0 THEN
14073        oe_debug_pub.add(  'AFTER CALLING LOAD_MRP_TBL ' , 2 ) ;
14074    END IF;
14075 
14076    IF l_mrp_atp_rec.error_code.count > 0 THEN
14077 
14078 
14079      l_session_id := Get_Session_Id;
14080      G_ATP_CHECK_session_id := l_session_id;
14081 
14082      -- Call ATP
14083 
14084      IF l_debug_level  > 0 THEN
14085          oe_debug_pub.add(  'CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
14086      END IF;
14087 
14088      MRP_ATP_PUB.Call_ATP
14089         ( p_session_id             =>  l_session_id
14090         , p_atp_rec                =>  l_mrp_atp_rec
14091         , x_atp_rec                =>  l_out_mrp_atp_rec
14092         , x_atp_supply_demand      =>  l_atp_supply_demand
14093         , x_atp_period             =>  l_atp_period
14094         , x_atp_details            =>  l_atp_details
14095         , x_return_status          =>  x_return_status
14096         , x_msg_data               =>  l_mrp_msg_data
14097         , x_msg_count              =>  l_msg_count);
14098 
14099      IF l_debug_level  > 0 THEN
14100       oe_debug_pub.add(  ' AFTER CALLING MRP_ATP_PUB.CALL_ATP ' || X_RETURN_STATUS , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
14101      END IF;
14102 
14103      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14104         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14105 /*     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
14106             RAISE FND_API.G_EXC_ERROR; */
14107      END IF;
14108 
14109    END IF;  -- Moved the end of the if here becausw we want to call Load_Results
14110              -- with a count of 0 because of bug 1955004
14111 
14112      Load_Results_from_tbl(p_atp_rec        => l_out_mrp_atp_rec,
14113                            p_old_line_tbl   => p_old_line_tbl,  -- Added new parameter to support 1955004
14114                            p_x_line_tbl     => p_x_line_tbl,
14115                            x_return_status  => x_return_status);
14116 
14117      IF l_debug_level  > 0 THEN
14118          oe_debug_pub.add(  'AFTER CALLING ACTION_ATP ' || X_RETURN_STATUS , 2 ) ;
14119      END IF;
14120 
14121      IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14122             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14123      ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
14124             RAISE FND_API.G_EXC_ERROR;
14125      END IF;
14126 
14127    -- END IF; -- MRP count.  Moved this end if per 1955004 above
14128 
14129      -- We also need to pass back on-hand qty and available_to_reserve
14130      -- qties while performing ATP. Getting these values from inventory.
14131 
14132      FOR K IN 1..g_atp_tbl.count LOOP
14133         IF l_debug_level  > 0 THEN
14134             oe_debug_pub.add(  'CALLING QUERY_QTY_TREE' , 1 ) ;
14135         END IF;
14136         Query_Qty_Tree(p_org_id           => g_atp_tbl(K).ship_from_org_id,
14137                        p_item_id          => g_atp_tbl(K).inventory_item_id,
14138                        p_line_id          => g_atp_tbl(K).line_id,
14139                        p_sch_date     => nvl(g_atp_tbl(K).group_available_date,
14140                                        g_atp_tbl(K).ordered_qty_Available_Date),
14141                        x_on_hand_qty      => l_on_hand_qty,
14142                        x_avail_to_reserve => l_avail_to_reserve,
14143                        x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
14144                        x_avail_to_reserve2 => l_avail_to_reserve2, -- INVCONV
14145                        p_subinventory_code => g_atp_tbl(K).subinventory_code --11777419
14146                        );
14147 
14148 
14149         --  added by fabdi 03/May/2001
14150 
14151    --ER#14763609 Start
14152          IF l_debug_level  > 0 THEN
14153             oe_debug_pub.add(  '2. Fetching primary and secondary UOM' , 1 ) ;
14154          END IF;
14155 
14156 	 SELECT secondary_uom_code ,primary_uom_code
14157           INTO l_secondary_uom_code ,l_primary_uom_code
14158           FROM mtl_system_items_b
14159           WHERE inventory_item_id = g_atp_tbl(K).inventory_item_id
14160           AND organization_id   = g_atp_tbl(K).ship_from_org_id
14161           AND  ROWNUM=1;
14162 
14163          IF l_debug_level  > 0 THEN
14164             oe_debug_pub.add(  '2. l_primary_uom_code / l_secondary_uom_code ' || l_primary_uom_code || ' / ' || l_secondary_uom_code , 1 ) ;
14165          END IF;
14166 
14167           SELECT Nvl(fulfillment_base,'P')
14168           INTO l_fulfillment_base
14169           FROM oe_order_lines_all
14170           WHERE line_id   =  g_atp_tbl(K).line_id;
14171          IF l_debug_level  > 0 THEN
14172             oe_debug_pub.add(  '2. fulfillment_base: '|| l_fulfillment_base , 1 ) ;
14173          END IF;
14174     --ER#14763609 End
14175 
14176 	IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id
14177                                               => g_atp_tbl(K).ship_from_org_id)
14178         THEN
14179            l_process_flag := FND_API.G_FALSE;
14180         ELSE
14181            l_process_flag := FND_API.G_TRUE;
14182         END IF;
14183 
14184         IF l_process_flag = FND_API.G_TRUE
14185         THEN
14186           g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
14187           g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
14188         -- g_atp_tbl(K).QTY_ON_REQUEST_DATE  := l_avail_to_reserve;
14189         -- Above line commented for bug 11658607 as it was a wrong assignment  -- Available field in ATP
14190 
14191           IF l_debug_level  > 0 THEN
14192               oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
14193               oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
14194              oe_debug_pub.add(  'AVAILABLE ' || L_AVAIL_TO_RESERVE ) ;
14195          END IF;
14196 
14197 	--ER#14763609 Start
14198              IF l_fulfillment_base ='P' THEN
14199                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14200                                      (
14201                                      g_atp_tbl(K).inventory_item_id,
14202                                      l_primary_uom_code,
14203                                      g_atp_tbl(K).order_quantity_uom,
14204                                      l_on_hand_qty
14205                                      );
14206 
14207                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14208                                          (
14209                                          g_atp_tbl(K).inventory_item_id,
14210                                          l_primary_uom_code,
14211                                          g_atp_tbl(K).order_quantity_uom,
14212                                          l_avail_to_reserve
14213                                        );
14214                 ELSE
14215                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14216                                      (
14217                                      g_atp_tbl(K).inventory_item_id,
14218                                      l_secondary_uom_code,
14219                                      g_atp_tbl(K).order_quantity_uom,
14220                                      l_on_hand_qty2
14221                                      );
14222 
14223                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14224                                          (
14225                                          g_atp_tbl(K).inventory_item_id,
14226                                          l_secondary_uom_code,
14227                                          g_atp_tbl(K).order_quantity_uom,
14228                                          l_avail_to_reserve2
14229                                        );
14230                 END IF ;
14231 
14232 	     g_atp_tbl(K).on_hand_qty_orduom          := l_on_hand_ord_qty;
14233              g_atp_tbl(K).available_to_reserve_orduom := l_avail_to_reserve_ord;
14234              g_atp_tbl(K).primary_uom                 := l_primary_uom_code;
14235 
14236 	     IF l_debug_level  > 0 THEN
14237                  oe_debug_pub.add(  '5. l_on_hand_ord_qty: ' || l_on_hand_ord_qty ) ;
14238                  oe_debug_pub.add(  '5. l_avail_to_reserve_ord: ' || l_avail_to_reserve_ord ) ;
14239                  oe_debug_pub.add(  '5. l_primary_uom_code: ' || l_primary_uom_code ) ;
14240              END IF;
14241 
14242 --ER#14763609 End
14243 
14244 	ELSE -- NOT INV_GMI_RSV_BRANCH.Process_Branch
14245           IF g_atp_tbl(K).substitute_flag = 'N' THEN
14246              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
14247              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
14248              IF l_debug_level  > 0 THEN
14249                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
14250                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
14251              END IF;
14252 
14253 --ER#14763609 Start
14254              IF l_fulfillment_base ='P' THEN
14255                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14256                                      (
14257                                      g_atp_tbl(K).inventory_item_id,
14258                                      l_primary_uom_code,
14259                                      g_atp_tbl(K).order_quantity_uom,
14260                                      l_on_hand_qty
14261                                      );
14262 
14263                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14264                                          (
14265                                          g_atp_tbl(K).inventory_item_id,
14266                                          l_primary_uom_code,
14267                                          g_atp_tbl(K).order_quantity_uom,
14268                                          l_avail_to_reserve
14269                                        );
14270                 ELSE
14271                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14272                                      (
14273                                      g_atp_tbl(K).inventory_item_id,
14274                                      l_secondary_uom_code,
14275                                      g_atp_tbl(K).order_quantity_uom,
14276                                      l_on_hand_qty2
14277                                      );
14278 
14279                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14280                                          (
14281                                          g_atp_tbl(K).inventory_item_id,
14282                                          l_secondary_uom_code,
14283                                          g_atp_tbl(K).order_quantity_uom,
14284                                          l_avail_to_reserve2
14285                                        );
14286                 END IF ;
14287 
14288 	     g_atp_tbl(K).on_hand_qty_orduom          := l_on_hand_ord_qty;
14289              g_atp_tbl(K).available_to_reserve_orduom := l_avail_to_reserve_ord;
14290              g_atp_tbl(K).primary_uom                 := l_primary_uom_code;
14291 
14292 	     IF l_debug_level  > 0 THEN
14293                  oe_debug_pub.add(  '2. l_on_hand_ord_qty: ' || l_on_hand_ord_qty ) ;
14294                  oe_debug_pub.add(  '2. l_avail_to_reserve_ord: ' || l_avail_to_reserve_ord ) ;
14295                  oe_debug_pub.add(  '2. l_primary_uom_code: ' || l_primary_uom_code ) ;
14296              END IF;
14297 
14298 --ER#14763609 End
14299 
14300 
14301 	  ELSE  --g_atp_tbl(K).substitute_flag = 'N'
14302              g_atp_tbl(K).sub_on_hand_qty          := l_on_hand_qty;
14303              g_atp_tbl(K).sub_available_to_reserve := l_avail_to_reserve;
14304              IF l_debug_level  > 0 THEN
14305                  oe_debug_pub.add(  'SUB L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
14306                  oe_debug_pub.add(  'SUB L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
14307              END IF;
14308 --ER#14763609 Start
14309              IF l_fulfillment_base ='P' THEN
14310                 l_sub_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14311                                          (
14312                                          g_atp_tbl(K).inventory_item_id,
14313                                          l_primary_uom_code,
14314                                          g_atp_tbl(K).order_quantity_uom,
14315                                          l_on_hand_qty
14316                                          );
14317 
14318                l_sub_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14319                                          (
14320                                          g_atp_tbl(K).inventory_item_id,
14321                                          l_primary_uom_code,
14322                                          g_atp_tbl(K).order_quantity_uom,
14323                                          l_avail_to_reserve
14324                                        );
14325                 ELSE
14326                 l_sub_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14327                                      (
14328                                      g_atp_tbl(K).inventory_item_id,
14329                                      l_secondary_uom_code,
14330                                      g_atp_tbl(K).order_quantity_uom,
14331                                      l_on_hand_qty2
14332                                      );
14333 
14334                l_sub_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14335                                          (
14336                                          g_atp_tbl(K).inventory_item_id,
14337                                          l_secondary_uom_code,
14338                                          g_atp_tbl(K).order_quantity_uom,
14339                                          l_avail_to_reserve2
14340                                        );
14341                 END IF ;
14342 
14343 	     g_atp_tbl(K).sub_on_hand_qty_orduom          := l_sub_on_hand_ord_qty;
14344              g_atp_tbl(K).sub_avail_to_reserve_orduom     := l_sub_avail_to_reserve_ord;
14345              g_atp_tbl(K).sub_primary_uom                 := l_primary_uom_code;
14346 
14347 	     IF l_debug_level  > 0 THEN
14348                  oe_debug_pub.add(  '3. l_sub_on_hand_ord_qty: ' || l_sub_on_hand_ord_qty ) ;
14349                  oe_debug_pub.add(  '3. l_sub_avail_to_reserve_ord: ' || l_sub_avail_to_reserve_ord ) ;
14350                  oe_debug_pub.add(  '3. l_primary_uom_code: ' || l_primary_uom_code ) ;
14351              END IF;
14352 
14353 --ER#14763609 End
14354 
14355              Query_Qty_Tree
14356                   (p_org_id           => g_atp_tbl(K).ship_from_org_id,
14357                    p_item_id          => g_atp_tbl(K).request_item_id,
14358                    p_line_id          => g_atp_tbl(K).line_id,
14359                    p_sch_date         => g_atp_tbl(K).req_item_available_date,
14360                    x_on_hand_qty      => l_on_hand_qty,
14361                    x_avail_to_reserve => l_avail_to_reserve,
14362                    x_on_hand_qty2      => l_on_hand_qty2, -- INVCONV
14363                    x_avail_to_reserve2 => l_avail_to_reserve2 -- INVCONV
14364                        );
14365 
14366              g_atp_tbl(K).on_hand_qty          := l_on_hand_qty;
14367              g_atp_tbl(K).available_to_reserve := l_avail_to_reserve;
14368              IF l_debug_level  > 0 THEN
14369                  oe_debug_pub.add(  'L_ON_HAND_QTY' || L_ON_HAND_QTY ) ;
14370                  oe_debug_pub.add(  'L_AVAIL_TO_RESERVE' || L_AVAIL_TO_RESERVE ) ;
14371              END IF;
14372 --ER#14763609 Start
14373          IF l_debug_level  > 0 THEN
14374             oe_debug_pub.add(  '3. Fetching primary and secondary UOM' , 1 ) ;
14375          END IF;
14376 
14377 	 SELECT secondary_uom_code ,primary_uom_code
14378           INTO l_secondary_uom_code ,l_primary_uom_code
14379           FROM mtl_system_items_b
14380           WHERE inventory_item_id = g_atp_tbl(K).request_item_id
14381           AND organization_id   = g_atp_tbl(K).ship_from_org_id
14382           AND  ROWNUM=1;
14383          IF l_debug_level  > 0 THEN
14384             oe_debug_pub.add(  '3. l_primary_uom_code / l_secondary_uom_code ' || l_primary_uom_code || ' / ' || l_secondary_uom_code , 1 ) ;
14385          END IF;
14386 
14387              IF l_fulfillment_base ='P' THEN
14388                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14389                                      (
14390                                      g_atp_tbl(K).request_item_id,
14391                                      l_primary_uom_code,
14392                                      g_atp_tbl(K).order_quantity_uom,
14393                                      l_on_hand_qty
14394                                      );
14395 
14396                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14397                                          (
14398                                          g_atp_tbl(K).request_item_id,
14399                                          l_primary_uom_code,
14400                                          g_atp_tbl(K).order_quantity_uom,
14401                                          l_avail_to_reserve
14402                                        );
14403                 ELSE
14404                 l_on_hand_ord_qty := OE_Order_Misc_Util.Convert_Uom
14405                                      (
14406                                      g_atp_tbl(K).request_item_id,
14407                                      l_secondary_uom_code,
14408                                      g_atp_tbl(K).order_quantity_uom,
14409                                      l_on_hand_qty2
14410                                      );
14411 
14412                l_avail_to_reserve_ord:= OE_Order_Misc_Util.Convert_Uom
14413                                          (
14414                                          g_atp_tbl(K).request_item_id,
14415                                          l_secondary_uom_code,
14416                                          g_atp_tbl(K).order_quantity_uom,
14417                                          l_avail_to_reserve2
14418                                        );
14419                 END IF ;
14420 
14421 	     g_atp_tbl(K).on_hand_qty_orduom          := l_on_hand_ord_qty;
14422              g_atp_tbl(K).available_to_reserve_orduom := l_avail_to_reserve_ord;
14423              g_atp_tbl(K).primary_uom                 := l_primary_uom_code;
14424 
14425 	     IF l_debug_level  > 0 THEN
14426                  oe_debug_pub.add(  '4. l_on_hand_ord_qty: ' || l_on_hand_ord_qty ) ;
14427                  oe_debug_pub.add(  '4. l_avail_to_reserve_ord: ' || l_avail_to_reserve_ord ) ;
14428                  oe_debug_pub.add(  '4. l_primary_uom_code: ' || l_primary_uom_code ) ;
14429              END IF;
14430 
14431 --ER#14763609 End
14432 
14433 
14434 
14435 	  END IF; -- Substitution.
14436         END IF;
14437         -- end fabdi
14438 
14439      END LOOP;
14440 
14441 
14442     x_atp_tbl := g_atp_tbl;
14443 
14444     IF l_debug_level  > 0 THEN
14445         oe_debug_pub.add(  'EXITING MULTI ATP_CHECK ' , 2 ) ;
14446     END IF;
14447 EXCEPTION
14448 
14449    WHEN FND_API.G_EXC_ERROR THEN
14450 
14451         x_return_status := FND_API.G_RET_STS_ERROR;
14452         RAISE FND_API.G_EXC_ERROR;
14453 
14454     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14455 
14456         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14457         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14458 
14459     WHEN OTHERS THEN
14460 
14461         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14462 
14463         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14464         THEN
14465             OE_MSG_PUB.Add_Exc_Msg
14466             (   G_PKG_NAME
14467             ,   'MULTI_ATP_Check'
14468             );
14469         END IF;
14470 
14471         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14472 END Multi_ATP_Check;
14473 
14474 Procedure Delete_Row(p_line_id      IN NUMBER)
14475 IS
14476 l_line_rec               OE_ORDER_PUB.line_rec_type;
14477 l_return_status          VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
14478 l_sales_order_id         NUMBER;
14479 l_ato_exists             VARCHAR2(1);
14480 l_request_search_rslt    BOOLEAN;
14481 --
14482 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14483 --
14484 BEGIN
14485 
14486   IF l_debug_level  > 0 THEN
14487       oe_debug_pub.add(  'ENTERING OE_SCHEDULE_UTIL.DELETE_ROW' , 1 ) ;
14488   END IF;
14489   OE_Line_Util.Query_Row(p_line_id    => p_line_id,
14490                          x_line_rec   => l_line_rec);
14491 
14492   /* Fix for bug 2643593, reservations to be removed only for
14493      shippable line */
14494 
14495   IF nvl(l_line_rec.shippable_flag,'N') = 'Y' THEN
14496 
14497     l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id
14498                                               (l_line_rec.HEADER_ID);
14499 
14500 
14501      -- INVCONV - MERGED CALLS   FOR OE_LINE_UTIL.Get_Reserved_Quantity and OE_LINE_UTIL.Get_Reserved_Quantity2
14502 
14503      OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
14504                                               ,p_line_id   => l_line_rec.line_id
14505                                               ,p_org_id    => l_line_rec.ship_from_org_id
14506                                               ,x_reserved_quantity =>  l_line_rec.reserved_quantity
14507                                               ,x_reserved_quantity2 => l_line_rec.reserved_quantity2
14508                                                                                             );
14509 
14510     /*l_line_rec.reserved_quantity :=
14511               OE_LINE_UTIL.Get_Reserved_Quantity
14512                  (p_header_id   => l_sales_order_id,
14513                   p_line_id     => l_line_rec.line_id,
14514                   p_org_id      => l_line_rec.ship_from_org_id);
14515     l_line_rec.reserved_quantity2 :=           -- INVCONV
14516               OE_LINE_UTIL.Get_Reserved_Quantity2
14517                  (p_header_id   => l_sales_order_id,
14518                   p_line_id     => l_line_rec.line_id,
14519                   p_org_id      => l_line_rec.ship_from_org_id);   */
14520 
14521          IF l_debug_level  > 0 THEN
14522             oe_debug_pub.add(  'after Get_Reserved_Quantities, l_line_rec.reserved_quantity2 = ' ||  l_line_rec.reserved_quantity2, 1 ) ;
14523              END IF;
14524 
14525 
14526 
14527   END IF;
14528 
14529   IF l_line_rec.reserved_quantity is not null
14530   THEN
14531     -- Call INV to delete the old reservations
14532     -- shipping_interfaced_flag
14533 
14534        IF l_line_rec.reserved_quantity2 = 0 -- INVCONV
14535         THEN
14536           l_line_rec.reserved_quantity2 := NULL;
14537        END IF;
14538 
14539 
14540        Unreserve_Line
14541         (p_line_rec              => l_line_rec,
14542          p_quantity_to_unreserve => l_line_rec.reserved_quantity,
14543          p_quantity2_to_unreserve => l_line_rec.reserved_quantity2, -- INVCONV
14544          x_return_status         => l_return_status);
14545   END IF;
14546   -- 4026758
14547   IF l_line_rec.ship_set_id IS NOT NULL
14548      OR l_line_rec.arrival_set_id IS NOT NULL THEN
14549      -- Line is with a set.
14550 
14551      Log_Delete_Set_Request
14552        (p_header_id   => l_line_rec.header_id,
14553         p_line_id     => l_line_rec.line_id,
14554         p_set_id      => nvl(l_line_rec.ship_set_id,l_line_rec.arrival_set_id),
14555         x_return_status => l_return_status);
14556      IF l_debug_level  > 0 THEN
14557         oe_debug_pub.add(  'AFTER LOGGING DELETE SETS DELAYED REQUEST ' || L_RETURN_STATUS , 1 ) ;
14558      END IF;
14559 
14560   --bug#5631508
14561    IF L_RETURN_STATUS=FND_API.G_RET_STS_SUCCESS then
14562 
14563 	OE_AUDIT_HISTORY_PVT.DELETE_SET_HISTORY( p_line_id     => l_line_rec.line_id, x_return_status => l_return_status);
14564 
14565 	 IF l_debug_level  > 0 THEN
14566 		 oe_debug_pub.add(  'AFTER DELETE_SET_HISTORY ' || l_return_status , 1 ) ;
14567 	 END IF;
14568 
14569     END IF;
14570 
14571   END IF;
14572 
14573 
14574 
14575   IF l_line_rec.item_type_code <> OE_GLOBALS.G_ITEM_CONFIG THEN
14576 
14577 
14578    IF   OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
14579    AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10
14580    AND  l_line_rec.ato_line_id IS NOT NULL
14581    AND  l_line_rec.ato_line_id <> l_line_rec.line_id THEN
14582 
14583      BEGIN
14584 
14585      Select 'Y'
14586        Into l_ato_exists
14587      From oe_order_lines_all
14588      Where line_id = l_line_rec.ato_line_id;
14589      EXCEPTION
14590 
14591        WHEN OTHERS THEN
14592 
14593       l_ato_exists := 'N';
14594 
14595      END;
14596      IF l_ato_exists = 'Y' THEN
14597 
14598       l_request_search_rslt :=
14599           OE_Delayed_Requests_PVT.Check_For_Request
14600           (p_entity_code    => OE_GLOBALS.G_ENTITY_LINE,
14601            p_entity_id      => l_line_rec.ato_line_id,
14602            p_request_type   => OE_GLOBALS.G_SCHEDULE_ATO);
14603 
14604 
14605         IF l_request_search_rslt THEN
14606              Return;
14607         END IF;
14608 
14609     -- Log request.
14610         OE_Delayed_Requests_Pvt.Log_Request
14611        (p_entity_code            => OE_GLOBALS.G_ENTITY_LINE,
14612         p_entity_id              => l_line_rec.ato_line_id,
14613         p_requesting_entity_code => OE_GLOBALS.G_ENTITY_LINE,
14614         p_requesting_entity_id   => l_line_rec.line_id,
14615         p_request_type           => OE_GLOBALS.G_SCHEDULE_ATO,
14616         p_param1               => OE_Schedule_Util.OESCH_ACT_RESCHEDULE,
14617         p_param2                 => l_line_rec.top_model_line_id,
14618         p_param3                 => l_line_rec.ship_from_org_id,
14619         p_param4                 => l_line_rec.ship_to_org_id,
14620         p_param5                 => l_line_rec.shipping_method_code,
14621         p_param6                 => l_line_rec.demand_class_code,
14622         p_param7                 => l_line_rec.ship_from_org_id,
14623         p_param8                 => l_line_rec.demand_class_code,
14624         p_param11                => l_line_rec.override_atp_date_code,
14625         p_date_param1            => l_line_rec.request_date,
14626         p_date_param2            => l_line_rec.schedule_ship_date,
14627         p_date_param3            => l_line_rec.schedule_arrival_date,
14628         p_date_param4            => l_line_rec.request_date,
14629         p_date_param5            => l_line_rec.schedule_ship_date,
14630         p_date_param6            => l_line_rec.schedule_arrival_date,
14631         p_param12                => 'DELETE',
14632         p_param14                => l_line_rec.ship_set_id,
14633         p_param13                => l_line_rec.arrival_set_id,
14634         p_param15              => l_line_rec.ship_model_complete_flag,
14635         p_param25                => l_line_rec.header_id,
14636         x_return_status          => l_return_status);
14637 
14638     END IF; -- Exists
14639 
14640 
14641    ELSE -- Not an ato child
14642 
14643      -- Start 2691579
14644      l_line_rec.operation := OE_GLOBALS.G_OPR_DELETE;
14645      IF l_debug_level  > 0 THEN
14646         oe_debug_pub.add(  'Operation '||l_line_rec.operation , 1 ) ;
14647      END IF;
14648      -- End 2691579
14649 
14650      Action_Undemand(p_old_line_rec  => l_line_rec,
14651                      x_return_status => l_return_status);
14652 
14653    END IF; -- GOP CODE LEVEL
14654 
14655   END IF;
14656 
14657   IF l_debug_level  > 0 THEN
14658       oe_debug_pub.add(  'EXITING OE_SCHEDULE_UTIL.DELETE_ROW' , 1 ) ;
14659   END IF;
14660 EXCEPTION
14661 
14662     WHEN OTHERS THEN
14663 
14664         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14665         THEN
14666             OE_MSG_PUB.Add_Exc_Msg
14667             (   G_PKG_NAME
14668             ,   'Delete_row'
14669             );
14670         END IF;
14671       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14672 END Delete_Row;
14673 /*---------------------------------------------------------------------
14674 Procedure Name : Transfer_reservation
14675 Description    : This API calls Inventory's APIs to Transfer_reservation.
14676 --------------------------------------------------------------------- */
14677 
14678 Procedure Transfer_reservation
14679 ( p_rsv_rec              IN  inv_reservation_global.mtl_reservation_rec_type
14680  ,p_quantity_to_transfer IN  NUMBER
14681  ,p_quantity2_to_transfer IN  NUMBER DEFAULT 0
14682  ,p_line_to_transfer     IN  NUMBER
14683 ,x_return_status OUT NOCOPY VARCHAR2)
14684 
14685 IS
14686 l_rsv_rec             inv_reservation_global.mtl_reservation_rec_type;
14687 l_msg_count           NUMBER;
14688 l_msg_data            VARCHAR2(240);
14689 l_rsv_id              NUMBER;
14690 l_dummy_sn            inv_reservation_global.serial_number_tbl_type;
14691 l_buffer              VARCHAR2(2000);
14692 
14693 --
14694 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14695 --
14696 BEGIN
14697    IF l_debug_level  > 0 THEN
14698        oe_debug_pub.add(  'ENTERING TRANSFER_RESERVATION LINE :' || P_QUANTITY_TO_TRANSFER , 1 ) ;
14699        oe_debug_pub.add(  'TRANSFER_RESERVATION LINE qty2 :' || P_QUANTITY2_TO_TRANSFER , 1 ) ;
14700        oe_debug_pub.add(  'TOTAL RESERVATION ON THE LINE :' || P_RSV_REC.RESERVATION_QUANTITY , 1 ) ;
14701        oe_debug_pub.add(  'TOTAL RESERVATION qty2 ON THE LINE :' || P_RSV_REC.SECONDARY_RESERVATION_QUANTITY , 1 ) ;
14702        oe_debug_pub.add(  ' LINE :' || P_LINE_TO_TRANSFER , 1 ) ;
14703    END IF;
14704    x_return_status := FND_API.G_RET_STS_SUCCESS;
14705 
14706    l_rsv_rec := p_rsv_rec;
14707    l_rsv_rec.reservation_id := fnd_api.g_miss_num;
14708    l_rsv_rec.demand_source_line_id := p_line_to_transfer;
14709    --- Start 2346233 --
14710    --l_rsv_rec.reservation_quantity :=  p_quantity_to_transfer;
14711    --l_rsv_rec.primary_reservation_quantity := fnd_api.g_miss_num;
14712    l_rsv_rec.primary_reservation_quantity :=  p_quantity_to_transfer;
14713    l_rsv_rec.secondary_reservation_quantity :=  p_quantity2_to_transfer; -- INVCONV
14714 
14715    l_rsv_rec.reservation_quantity := fnd_api.g_miss_num;
14716   -- l_rsv_rec.secondary_reservation_quantity := fnd_api.g_miss_num; -- INVCONV -- commneted for bug16788917
14717    --- End 2346233 --
14718 
14719    IF l_debug_level  > 0 THEN
14720        oe_debug_pub.add(  'CALLING INVS TRANSFER RESERVATION' , 1 ) ;
14721    END IF;
14722 
14723    inv_reservation_pub.Transfer_reservation
14724    ( p_api_version_number        => 1.0
14725    , p_init_msg_lst              => FND_API.G_TRUE
14726    , x_return_status             => x_return_status
14727    , x_msg_count                 => l_msg_count
14728    , x_msg_data                  => l_msg_data
14729    , p_original_rsv_rec          => p_rsv_rec
14730    , p_to_rsv_rec                => l_rsv_rec
14731    , p_original_serial_number    => l_dummy_sn
14732    , p_to_serial_number          => l_dummy_sn
14733    , p_validation_flag           => FND_API.G_FALSE
14734    , x_to_reservation_id         => l_rsv_id);
14735 
14736                                              IF l_debug_level  > 0 THEN
14737                                                  oe_debug_pub.add(  '2. AFTER CALLING TRANSFER RESERVATION' || X_RETURN_STATUS , 1 ) ;
14738                                              END IF;
14739    IF l_debug_level  > 0 THEN
14740        oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14741    END IF;
14742 
14743    IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
14744       oe_msg_pub.transfer_msg_stack;
14745       l_msg_count:=OE_MSG_PUB.COUNT_MSG;
14746       for I in 1..l_msg_count loop
14747          l_msg_data := OE_MSG_PUB.Get(I,'F');
14748          IF l_debug_level  > 0 THEN
14749              oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14750          END IF;
14751       end loop;
14752       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
14753    ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
14754       oe_msg_pub.transfer_msg_stack;
14755       l_msg_count:=OE_MSG_PUB.COUNT_MSG;
14756       for I in 1..l_msg_count loop
14757           l_msg_data := OE_MSG_PUB.Get(I,'F');
14758           IF l_debug_level  > 0 THEN
14759               oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
14760           END IF;
14761       end loop;
14762       RAISE FND_API.G_EXC_ERROR;
14763 
14764    END IF;
14765 
14766                                           IF l_debug_level  > 0 THEN
14767                                               oe_debug_pub.add(  'EXITING TRANSFER RESERVATION' || X_RETURN_STATUS , 1 ) ;
14768                                           END IF;
14769 
14770 EXCEPTION
14771   WHEN FND_API.G_EXC_ERROR THEN
14772     x_return_status := FND_API.G_RET_STS_ERROR;
14773 
14774   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
14775     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14776 
14777      oe_msg_pub.transfer_msg_stack;
14778      l_msg_count:=OE_MSG_PUB.COUNT_MSG;
14779      for I in 1..l_msg_count loop
14780         l_msg_data := OE_MSG_PUB.Get(I,'F');
14781         oe_msg_pub.add_text(l_msg_data);
14782         IF l_debug_level  > 0 THEN
14783             oe_debug_pub.add(  'INV : ' || L_MSG_DATA , 2 ) ;
14784         END IF;
14785      end loop;
14786 
14787   WHEN OTHERS THEN
14788     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14789 
14790     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
14791     THEN
14792       OE_MSG_PUB.Add_Exc_Msg
14793       (   G_PKG_NAME
14794       ,   'Transfer_reservation'
14795        );
14796     END IF;
14797 
14798 END Transfer_reservation;
14799 
14800 PROCEDURE Set_Auto_Sch_Flag
14801 (p_value_from_user  IN VARCHAR2 := FND_API.G_MISS_CHAR)
14802 IS
14803 --
14804 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14805 --
14806 BEGIN
14807     IF l_debug_level  > 0 THEN
14808         oe_debug_pub.add(  'P_VALUE_FROM_USER ' || P_VALUE_FROM_USER , 1 ) ;
14809     END IF;
14810     OESCH_AUTO_SCH_FLAG := p_value_from_user;
14811 END Set_Auto_Sch_Flag;
14812 
14813 --Bug 5948059
14814 FUNCTION Get_Auto_Sch_Flag
14815 RETURN VARCHAR2
14816 IS
14817 --
14818 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14819 --
14820 BEGIN
14821     IF l_debug_level  > 0 THEN
14822         oe_debug_pub.add(  'In Get_Auto_Sch_Flag' ) ;
14823         oe_debug_pub.add(  'OESCH_AUTO_SCH_FLAG: '||OESCH_AUTO_SCH_FLAG);
14824     END IF;
14825     RETURN (OESCH_AUTO_SCH_FLAG);
14826 END;
14827 /* Function find line
14828 
14829   This is be used to find the line in the pl/sql table.
14830 */
14831 FUNCTION Find_line( p_x_line_tbl  IN OE_ORDER_PUB.Line_Tbl_Type,
14832                     p_line_id     IN  NUMBER)
14833 Return BOOLEAN
14834 IS
14835 --
14836 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14837 --
14838 BEGIN
14839 
14840   IF l_debug_level  > 0 THEN
14841       oe_debug_pub.add(  'ENTERING FIND_LINE: ' || P_LINE_ID , 1 ) ;
14842   END IF;
14843   IF p_x_line_tbl.count > 0 THEN --8706868
14844     FOR J IN 1..p_x_line_tbl.count LOOP
14845 
14846       IF p_line_id = p_x_line_tbl(J).line_id THEN
14847 
14848          IF l_debug_level  > 0 THEN
14849              oe_debug_pub.add(  ' LINE EXISTS IN THE TABLE' , 1 ) ;
14850          END IF;
14851 
14852          RETURN TRUE;
14853       END IF;
14854    END LOOP;
14855  END IF; -- 8706868
14856  RETURN FALSE;
14857 
14858 END Find_line;
14859 
14860 /*PROCEDURE PROCESS_SPLIT
14861   This procedure will be used to call mrp with appropriate records:
14862   If the ato model is part of SMC, then Whome smc model will be called
14863   If the ato model is part of set, then whole set information will
14864   be passed to MRP and so on */
14865 
14866 PROCEDURE PROCESS_SPLIT
14867 (p_x_line_tbl  IN  OE_ORDER_PUB.Line_Tbl_Type,
14868  x_return_status OUT NOCOPY VARCHAR2)
14869 
14870 IS
14871 l_line_tbl              OE_ORDER_PUB.line_tbl_type;
14872 l_local_line_tbl        OE_ORDER_PUB.line_tbl_type;
14873 l_old_line_tbl          OE_ORDER_PUB.line_tbl_type; -- 8706868
14874 l_mrp_line_tbl          OE_ORDER_PUB.line_tbl_type; -- 8706868
14875 l_non_mrp_line_tbl      OE_ORDER_PUB.line_tbl_type; -- 8706868
14876 l_old_mrp_line_tbl          OE_ORDER_PUB.line_tbl_type; -- 8706868
14877 l_order_date_type_code  VARCHAR2(20); -- 8706868
14878 l_rsv_update            BOOLEAN :=FALSE; --8706868
14879 K                       NUMBER;
14880 I                       NUMBER;
14881 L                       NUMBER := 0;
14882 M                       NUMBER := 0;
14883 l_ato_line_id           NUMBER;
14884 l_entity                VARCHAR2(30);
14885 
14886 -- MRP API variables
14887 l_session_id            NUMBER := 0;
14888 l_mrp_atp_rec           MRP_ATP_PUB.ATP_Rec_Typ;
14889 l_out_mtp_atp_rec       MRP_ATP_PUB.ATP_Rec_Typ;
14890 l_atp_supply_demand     MRP_ATP_PUB.ATP_Supply_Demand_Typ;
14891 l_atp_period            MRP_ATP_PUB.ATP_Period_Typ;
14892 l_atp_details           MRP_ATP_PUB.ATP_Details_Typ;
14893 mrp_msg_data            VARCHAR2(200);
14894 l_on_hand_qty           NUMBER;
14895 l_avail_to_reserve      NUMBER;
14896 l_out_atp_tbl           OE_ATP.atp_tbl_type;
14897 l_found                 BOOLEAN := FALSE;
14898 l_buffer                VARCHAR2(2000);
14899 l_msg_count             NUMBER;
14900 l_return_status         VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
14901 l_set_rec               OE_ORDER_CACHE.set_rec_type;
14902 l_sales_order_id        NUMBER; --8706868
14903 l_need_scheduling       BOOLEAN :=FALSE; --8706868
14904 
14905 --
14906 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
14907 --
14908 BEGIN
14909 
14910    IF l_debug_level  > 0 THEN
14911        oe_debug_pub.add(  'ENTERING PROCESS SPLIT'||p_x_line_tbl.count , 1 ) ;
14912    END IF;
14913 
14914    x_return_status := FND_API.G_RET_STS_SUCCESS;
14915    K := 0;
14916 
14917    --l_line_tbl := p_x_line_tbl;
14918 
14919    FOR I IN 1..p_x_line_tbl.count LOOP
14920 
14921     IF NOT find_line(p_x_line_tbl => l_line_tbl,
14922                      p_line_id    => p_x_line_tbl(I).line_id)
14923     THEN
14924      -- 8988079 : Restrict shipped lines
14925      IF p_x_line_tbl(I).schedule_status_code is not null
14926       AND p_x_line_tbl(I).shipped_quantity is null
14927       AND p_x_line_tbl(I).fulfilled_quantity IS NULL THEN -- 2680748 to restrict unscheduled lines
14928 
14929         l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(p_x_line_tbl(I).header_id),'SHIP'); -- 8706868
14930         IF p_x_line_tbl(I).arrival_set_id is not null THEN
14931 
14932            OE_Set_Util.Query_Set_Rows(p_set_id   => p_x_line_tbl(I).arrival_set_id,
14933                                  x_line_tbl => l_local_line_tbl);
14934 
14935            l_set_rec := OE_ORDER_CACHE.Load_Set
14936                          (p_x_line_tbl(I).arrival_set_id);
14937 
14938            FOR L IN 1..l_local_line_tbl.count LOOP
14939 
14940               K := K +1;
14941               l_line_tbl(K) := l_local_line_tbl(L);
14942               --8706868
14943                IF NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).request_date
14944                        ,l_line_tbl(K).request_date)  AND
14945                    p_x_line_tbl(I).split_by <>'SYSTEM' AND --10253393
14946 		   NVL(p_x_line_tbl(I).SPLIT_REQUEST_DATE,'N') = 'Y' THEN  -- 10278858
14947                   --l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_line_tbl(K).header_id),'SHIP');
14948                   IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y' THEN --12833832
14949                      IF l_order_date_type_code = 'SHIP' THEN
14950                        -- Its a scheduled line. Reschedule with new date
14951                         l_line_tbl(K).schedule_ship_date := p_x_line_tbl(I).request_date;
14952                      ELSE
14953                         l_line_tbl(K).schedule_arrival_date :=p_x_line_tbl(I).request_date;
14954                      END IF;
14955                   END IF;
14956                   l_line_tbl(K).request_date := p_x_line_tbl(I).request_date;
14957                END IF;
14958                IF p_x_line_tbl(I).ship_from_org_id IS NOT NULL AND
14959                   p_x_line_tbl(I).ship_from_org_id <> fnd_api.G_MISS_NUM AND
14960                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_from_org_id
14961                                       ,l_line_tbl(K).ship_from_org_id) AND
14962 		  NVL(p_x_line_tbl(I).SPLIT_SHIP_FROM,'N') = 'Y' THEN -- 10278858
14963                   -- Unreserve the line as warehouse being chabged and reservation is there.
14964  	          l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id(l_line_tbl(K).HEADER_ID);
14965 		       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
14966                                            ,p_line_id   => l_line_tbl(K).line_id
14967                                            ,p_org_id    => l_line_tbl(K).ship_from_org_id
14968                                            ,p_order_quantity_uom => l_line_tbl(K).order_quantity_uom
14969                                            ,p_inventory_item_id  => l_line_tbl(K).inventory_item_id --bug 16434168
14970                                            ,x_reserved_quantity =>  l_line_tbl(K).reserved_quantity
14971                                            ,x_reserved_quantity2 => l_line_tbl(K).reserved_quantity2
14972                                            );
14973                       IF NVL(l_line_tbl(K).reserved_quantity,0) >0  THEN
14974 		         OE_SCHEDULE_UTIL.Unreserve_Line
14975                                  (p_line_rec              => l_line_tbl(K),
14976                                   p_quantity_to_unreserve => l_line_tbl(K).reserved_quantity,
14977                                   p_quantity2_to_unreserve =>l_line_tbl(K).reserved_quantity2 , -- INVCONV
14978                                   x_return_status         => l_return_status);
14979                         l_line_tbl(K).reserved_quantity :=0;
14980 			l_line_tbl(K).reserved_quantity2 :=0;
14981 			oe_schedule_util.oe_split_rsv_tbl(MOD(l_line_tbl(K).line_id,G_BINARY_LIMIT)).line_id :=l_line_tbl(K).line_id;
14982                       END IF;
14983 
14984                   l_line_tbl(K).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
14985                END IF;
14986 	       IF p_x_line_tbl(I).ship_to_org_id IS NOT NULL AND
14987                   p_x_line_tbl(I).ship_to_org_id <> fnd_api.G_MISS_NUM AND
14988                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_to_org_id
14989                                       ,l_line_tbl(K).ship_to_org_id) AND
14990 	          NVL(p_x_line_tbl(I).SPLIT_SHIP_TO,'N') = 'Y' THEN -- 10278858
14991                   l_line_tbl(K).ship_to_org_id := p_x_line_tbl(I).ship_to_org_id;
14992                END IF;
14993 	       IF l_line_tbl(K).line_id = p_x_line_tbl(I).line_id THEN
14994   	          l_line_tbl(K).operation := p_x_line_tbl(I).operation;
14995                ELSE
14996                   l_line_tbl(K).operation := oe_globals.g_opr_update;
14997                END IF;
14998               --8706868
14999               l_line_tbl(K).schedule_action_code :=
15000                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15001               l_line_tbl(K).arrival_set := l_set_rec.set_name;
15002 
15003            END LOOP;
15004 
15005            l_local_line_tbl.delete;
15006 
15007         ELSIF p_x_line_tbl(I).ship_set_id is not null THEN
15008 
15009 
15010            OE_Set_Util.Query_Set_Rows(p_set_id   => p_x_line_tbl(I).ship_set_id,
15011                                  x_line_tbl => l_local_line_tbl);
15012 
15013            l_set_rec := OE_ORDER_CACHE.Load_Set
15014                          (p_x_line_tbl(I).ship_set_id);
15015            FOR L IN 1..l_local_line_tbl.count LOOP
15016 
15017               K := K +1;
15018               l_line_tbl(K) := l_local_line_tbl(L);
15019               --8706868
15020                IF NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).request_date
15021                        ,l_line_tbl(K).request_date) AND
15022 		       NVL(p_x_line_tbl(I).SPLIT_REQUEST_DATE,'N') = 'Y' THEN --10278858
15023                   --l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_line_tbl(K).header_id),'SHIP');
15024                   IF NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y' THEN --12833832
15025                      IF l_order_date_type_code = 'SHIP' THEN
15026                         -- Its a scheduled line. Reschedule with new date
15027                         l_line_tbl(K).schedule_ship_date := p_x_line_tbl(I).request_date;
15028                      ELSE
15029                         l_line_tbl(K).schedule_arrival_date :=p_x_line_tbl(I).request_date;
15030                      END IF;
15031                   END IF;
15032                   l_line_tbl(K).request_date := p_x_line_tbl(I).request_date;
15033                END IF;
15034                IF p_x_line_tbl(I).ship_from_org_id IS NOT NULL AND
15035                   p_x_line_tbl(I).ship_from_org_id <> fnd_api.G_MISS_NUM AND
15036                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_from_org_id
15037                                       ,l_line_tbl(K).ship_from_org_id) AND
15038 	          NVL(p_x_line_tbl(I).SPLIT_SHIP_FROM,'N') = 'Y' THEN -- 10278858
15039                   -- Unreserve the line as warehouse being chabged and reservation is there.
15040  	          l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id(l_line_tbl(K).HEADER_ID);
15041 		       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
15042                                            ,p_line_id   => l_line_tbl(K).line_id
15043                                            ,p_org_id    => l_line_tbl(K).ship_from_org_id
15044                                            ,p_order_quantity_uom => l_line_tbl(K).order_quantity_uom
15045                                            ,p_inventory_item_id  => l_line_tbl(K).inventory_item_id --bug 16434168
15046                                            ,x_reserved_quantity =>  l_line_tbl(K).reserved_quantity
15047                                            ,x_reserved_quantity2 => l_line_tbl(K).reserved_quantity2
15048                                            );
15049                       IF NVL(l_line_tbl(K).reserved_quantity,0) >0  THEN
15050 		         OE_SCHEDULE_UTIL.Unreserve_Line
15051                                  (p_line_rec              => l_line_tbl(K),
15052                                   p_quantity_to_unreserve => l_line_tbl(K).reserved_quantity,
15053                                   p_quantity2_to_unreserve =>l_line_tbl(K).reserved_quantity2 , -- INVCONV
15054                                   x_return_status         => l_return_status);
15055                         l_line_tbl(K).reserved_quantity :=0;
15056 			l_line_tbl(K).reserved_quantity2 :=0;
15057 			oe_schedule_util.oe_split_rsv_tbl(MOD(l_line_tbl(K).line_id,G_BINARY_LIMIT)).line_id :=l_line_tbl(K).line_id;
15058                       END IF;
15059                   l_line_tbl(K).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
15060                END IF;
15061 	       IF p_x_line_tbl(I).ship_to_org_id IS NOT NULL AND
15062                   p_x_line_tbl(I).ship_to_org_id <> fnd_api.G_MISS_NUM AND
15063                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_to_org_id
15064                                       ,l_line_tbl(K).ship_to_org_id) AND
15065  	          NVL(p_x_line_tbl(I).SPLIT_SHIP_TO,'N') = 'Y' THEN -- 10278858
15066                   l_line_tbl(K).ship_to_org_id := p_x_line_tbl(I).ship_to_org_id;
15067                END IF;
15068 	       IF l_line_tbl(K).line_id = p_x_line_tbl(I).line_id THEN
15069   	          l_line_tbl(K).operation := p_x_line_tbl(I).operation;
15070                ELSE
15071                   l_line_tbl(K).operation := oe_globals.g_opr_update;
15072                END IF;
15073               --8706868
15074               l_line_tbl(K).schedule_action_code :=
15075                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15076 
15077               l_line_tbl(K).ship_set := l_set_rec.set_name;
15078            END LOOP;
15079 
15080            l_local_line_tbl.delete;
15081 
15082         ELSIF p_x_line_tbl(I).ship_model_complete_flag ='Y'
15083              AND   nvl(p_x_line_tbl(I).model_remnant_flag,'N') = 'N' THEN
15084 
15085 
15086            OE_Config_Util.Query_Options
15087            (p_top_model_line_id => p_x_line_tbl(I).top_model_line_id,
15088             x_line_tbl          => l_local_line_tbl);
15089 
15090 
15091            FOR L IN 1..l_local_line_tbl.count LOOP
15092 
15093               K := K +1;
15094               l_line_tbl(K) := l_local_line_tbl(L);
15095                 /*
15096                IF NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).request_date
15097                        ,l_line_tbl(K).request_date) THEN
15098                   --l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_line_tbl(K).header_id),'SHIP');
15099                   IF l_order_date_type_code = 'SHIP' THEN
15100                      -- Its a scheduled line. Reschedule with new date
15101                      l_line_tbl(K).schedule_ship_date := p_x_line_tbl(I).request_date;
15102                   ELSE
15103                      l_line_tbl(K).schedule_arrival_date :=p_x_line_tbl(I).request_date;
15104                   END IF;
15105                   l_line_tbl(K).request_date := p_x_line_tbl(I).request_date;
15106                END IF;
15107                IF p_x_line_tbl(I).ship_from_org_id IS NOT NULL AND
15108                   p_x_line_tbl(I).ship_from_org_id <> fnd_api.G_MISS_NUM AND
15109                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_from_org_id
15110                                       ,l_line_tbl(K).ship_from_org_id)THEN
15111                   -- Unreserve the line as warehouse being chabged and reservation is there.
15112  	          l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id(l_line_tbl(K).HEADER_ID);
15113 		       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
15114                                            ,p_line_id   => l_line_tbl(K).line_id
15115                                            ,p_org_id    => l_line_tbl(K).ship_from_org_id
15116                                            ,p_order_quantity_uom => l_line_tbl(K).order_quantity_uom
15117                                            ,x_reserved_quantity =>  l_line_tbl(K).reserved_quantity
15118                                            ,x_reserved_quantity2 => l_line_tbl(K).reserved_quantity2
15119                                            );
15120                       IF l_line_tbl(K).reserved_quantity is not null THEN
15121 		         OE_SCHEDULE_UTIL.Unreserve_Line
15122                                  (p_line_rec              => l_line_tbl(K),
15123                                   p_quantity_to_unreserve => l_line_tbl(K).reserved_quantity,
15124                                   p_quantity2_to_unreserve =>l_line_tbl(K).reserved_quantity2 , -- INVCONV
15125                                   x_return_status         => l_return_status);
15126                         l_line_tbl(K).reserved_quantity :=0;
15127 			l_line_tbl(K).reserved_quantity2 :=0;
15128                       END IF;
15129                   l_line_tbl(K).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
15130                END IF;
15131 	       IF p_x_line_tbl(I).ship_to_org_id IS NOT NULL AND
15132                   p_x_line_tbl(I).ship_to_org_id <> fnd_api.G_MISS_NUM AND
15133                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_to_org_id
15134                                       ,l_line_tbl(K).ship_to_org_id)THEN
15135                   l_line_tbl(K).ship_to_org_id := p_x_line_tbl(I).ship_to_org_id;
15136                END IF;
15137 	       IF l_line_tbl(K).line_id = p_x_line_tbl(I).line_id THEN
15138   	          l_line_tbl(K).operation := p_x_line_tbl(I).operation;
15139                ELSE
15140                   l_line_tbl(K).operation := oe_globals.g_opr_update;
15141                END IF;
15142               */
15143               l_line_tbl(K).schedule_action_code :=
15144                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15145               l_line_tbl(K).ship_set := p_x_line_tbl(I).top_model_line_id;
15146               l_line_tbl(K).operation := p_x_line_tbl(I).operation; --Bug 14106410
15147 
15148            END LOOP;
15149 
15150            l_local_line_tbl.delete;
15151 
15152         ELSIF p_x_line_tbl(I).ato_line_id is not null
15153              AND   p_x_line_tbl(I).item_type_code <> 'STANDARD'
15154              AND   nvl(p_x_line_tbl(I).model_remnant_flag,'N') = 'N' THEN
15155 
15156             Begin
15157 
15158                Select ato_line_id
15159                Into   l_ato_line_id
15160                From   oe_order_lines_all
15161                Where  line_id = p_x_line_tbl(I).line_id;
15162             EXCEPTION
15163 
15164                WHEN OTHERS THEN
15165 
15166                  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15167 
15168             END;
15169 
15170             OE_Config_Util.Query_ATO_Options
15171             (p_ato_line_id => l_ato_line_id,
15172             x_line_tbl    => l_local_line_tbl);
15173 
15174 
15175             FOR L IN 1..l_local_line_tbl.count LOOP
15176 
15177                K := K +1;
15178                l_line_tbl(K) := l_local_line_tbl(L);
15179                /*
15180                IF NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).request_date
15181                        ,l_line_tbl(K).request_date) THEN
15182                   --l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_line_tbl(K).header_id),'SHIP');
15183                   IF l_order_date_type_code = 'SHIP' THEN
15184                      -- Its a scheduled line. Reschedule with new date
15185                      l_line_tbl(K).schedule_ship_date := p_x_line_tbl(I).request_date;
15186                   ELSE
15187                      l_line_tbl(K).schedule_arrival_date :=p_x_line_tbl(I).request_date;
15188                   END IF;
15189                   l_line_tbl(K).request_date := p_x_line_tbl(I).request_date;
15190                END IF;
15191                IF p_x_line_tbl(I).ship_from_org_id IS NOT NULL AND
15192                   p_x_line_tbl(I).ship_from_org_id <> fnd_api.G_MISS_NUM AND
15193                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_from_org_id
15194                                       ,l_line_tbl(K).ship_from_org_id)THEN
15195                   -- Unreserve the line as warehouse being chabged and reservation is there.
15196  	          l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id(l_line_tbl(K).HEADER_ID);
15197 		       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
15198                                            ,p_line_id   => l_line_tbl(K).line_id
15199                                            ,p_org_id    => l_line_tbl(K).ship_from_org_id
15200                                            ,p_order_quantity_uom => l_line_tbl(K).order_quantity_uom
15201                                            ,x_reserved_quantity =>  l_line_tbl(K).reserved_quantity
15202                                            ,x_reserved_quantity2 => l_line_tbl(K).reserved_quantity2
15203                                            );
15204                       IF l_line_tbl(K).reserved_quantity is not null THEN
15205 		         OE_SCHEDULE_UTIL.Unreserve_Line
15206                                  (p_line_rec              => l_line_tbl(K),
15207                                   p_quantity_to_unreserve => l_line_tbl(K).reserved_quantity,
15208                                   p_quantity2_to_unreserve =>l_line_tbl(K).reserved_quantity2 , -- INVCONV
15209                                   x_return_status         => l_return_status);
15210                         l_line_tbl(K).reserved_quantity :=0;
15211 			l_line_tbl(K).reserved_quantity2 :=0;
15212                       END IF;
15213                   l_line_tbl(K).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
15214                END IF;
15215 	       IF p_x_line_tbl(I).ship_to_org_id IS NOT NULL AND
15216                   p_x_line_tbl(I).ship_to_org_id <> fnd_api.G_MISS_NUM AND
15217                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_to_org_id
15218                                       ,l_line_tbl(K).ship_to_org_id)THEN
15219                   l_line_tbl(K).ship_to_org_id := p_x_line_tbl(I).ship_to_org_id;
15220                END IF;
15221 	       IF l_line_tbl(K).line_id = p_x_line_tbl(I).line_id THEN
15222   	          l_line_tbl(K).operation := p_x_line_tbl(I).operation;
15223                ELSE
15224                   l_line_tbl(K).operation := oe_globals.g_opr_update;
15225                END IF;
15226                */
15227                l_line_tbl(K).schedule_action_code :=
15228                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15229                l_line_tbl(K).ship_set := l_ato_line_id;
15230                l_line_tbl(K).operation := p_x_line_tbl(I).operation; --Bug 14106410
15231 
15232            END LOOP;
15233 
15234            l_local_line_tbl.delete;
15235 
15236 
15237         ELSE
15238 
15239            K := K +1;
15240            l_line_tbl(K) := p_x_line_tbl(I);
15241            l_line_tbl(K).schedule_action_code :=
15242                         OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15243 
15244 
15245         END IF;
15246       ELSE --10072873
15247 	 K := K +1;
15248          l_line_tbl(K) := p_x_line_tbl(I);
15249       END IF;  -- 2680748
15250 
15251     END IF; -- line is not part of the local table.
15252    END LOOP;
15253    IF l_line_tbl.count > 0 THEN
15254       null;
15255    ELSE
15256       l_line_tbl :=p_x_line_tbl;
15257    END IF;
15258    l_old_line_tbl  := l_line_tbl; --8706868
15259    FOR I IN 1..p_x_line_tbl.count LOOP
15260      IF p_x_line_tbl(I).item_type_code = 'MODEL' OR
15261         p_x_line_tbl(I).item_type_code = 'KIT' THEN --OR
15262       -- p_x_line_tbl(I).ship_set_id is not null OR
15263       -- p_x_line_tbl(I).arrival_set_id is not null THEN
15264 
15265          FOR J in 1..l_line_tbl.count LOOP
15266             IF (p_x_line_tbl(I).line_id = l_line_tbl(J).TOP_MODEL_LINE_ID) AND
15267 	    l_line_tbl(J).LINE_ID <> l_line_tbl(J).TOP_MODEL_LINE_ID AND
15268 	    (p_x_line_tbl(I).ship_set_id IS NULL OR p_x_line_tbl(I).arrival_set_id IS NULL) THEN
15269 	 --   OR ((p_x_line_tbl(I).ship_set_id = l_line_tbl(J).ship_set_id OR
15270 	 --    p_x_line_tbl(I).arrival_set_id = l_line_tbl(J).arrival_set_id) AND
15271 	 --    l_line_tbl(J).TOP_MODEL_LINE_ID IS NULL) THEN
15272 	     --8706868
15273              --10253393: Assign the request date if its not a system split
15274                IF NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).request_date
15275                        ,l_line_tbl(J).request_date) AND
15276 		       p_x_line_tbl(I).request_date IS NOT NULL AND --10072873
15277                        p_x_line_tbl(I).split_by <>'SYSTEM' AND  --10253393
15278 		       NVL(p_x_line_tbl(I).SPLIT_REQUEST_DATE,'N') = 'Y' THEN  -- 10278858
15279                   IF l_line_tbl(J).schedule_ship_date is NOT NULL AND
15280                       NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'Y' THEN --12833832
15281                      l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_line_tbl(J).header_id),'SHIP');
15282                      IF l_order_date_type_code = 'SHIP' THEN
15283                      -- Its a scheduled line. Reschedule with new date
15284                         l_line_tbl(J).schedule_ship_date := p_x_line_tbl(I).request_date;
15285                      ELSE
15286                         l_line_tbl(J).schedule_arrival_date :=p_x_line_tbl(I).request_date;
15287                      END IF;
15288                   END IF;
15289                   l_line_tbl(J).request_date := p_x_line_tbl(I).request_date;
15290 		    --bug 13954474
15291 		    l_line_tbl(J).SPLIT_REQUEST_DATE :=p_x_line_tbl(I).SPLIT_REQUEST_DATE;
15292  		    oe_debug_pub.add(  'Next for '||j||' '||l_line_tbl(J).SPLIT_REQUEST_DATE);
15293 		    --bug 13954474
15294                END IF;
15295                IF p_x_line_tbl(I).ship_from_org_id IS NOT NULL AND
15296                   p_x_line_tbl(I).ship_from_org_id <> fnd_api.G_MISS_NUM AND
15297                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_from_org_id
15298                                       ,l_line_tbl(J).ship_from_org_id) AND
15299 	          NVL(p_x_line_tbl(I).SPLIT_SHIP_FROM,'N') = 'Y' THEN -- 10278858
15300                    -- Unreserve the line as warehouse being chabged and reservation is there.
15301 		    IF l_line_tbl(J).schedule_ship_date is NOT NULL then  --9662817
15302 		  --  AND l_line_tbl(J).operation = oe_globals.g_opr_update  THEN
15303 		       l_sales_order_id := OE_SCHEDULE_UTIL.Get_mtl_sales_order_id(l_line_tbl(J).HEADER_ID);
15304 		       BEGIN
15305 		       OE_LINE_UTIL.Get_Reserved_Quantities(p_header_id => l_sales_order_id
15306                                            ,p_line_id   => l_line_tbl(J).line_id
15307                                            ,p_org_id    => l_line_tbl(J).ship_from_org_id
15308                                            ,p_order_quantity_uom => l_line_tbl(J).order_quantity_uom
15309                                            ,p_inventory_item_id  => l_line_tbl(J).inventory_item_id --bug 16434168
15310                                            ,x_reserved_quantity =>  l_line_tbl(J).reserved_quantity
15311                                            ,x_reserved_quantity2 => l_line_tbl(J).reserved_quantity2
15312                                            );
15313                       EXCEPTION
15314  		        WHEN OTHERS THEN
15315                          l_line_tbl(J).reserved_quantity :=0;
15316 			 l_line_tbl(J).reserved_quantity2 :=0;
15317                       END;
15318                       IF NVL(l_line_tbl(J).reserved_quantity,0) >0  THEN
15319 		         OE_SCHEDULE_UTIL.Unreserve_Line
15320                                  (p_line_rec              => l_line_tbl(J),
15321                                   p_quantity_to_unreserve => l_line_tbl(J).reserved_quantity,
15322                                   p_quantity2_to_unreserve =>l_line_tbl(J).reserved_quantity2 , -- INVCONV
15323                                   x_return_status         => l_return_status);
15324                         l_line_tbl(J).reserved_quantity :=0;
15325 			l_line_tbl(J).reserved_quantity2 :=0;
15326 			oe_schedule_util.oe_split_rsv_tbl(MOD(l_line_tbl(J).line_id,G_BINARY_LIMIT)).line_id :=l_line_tbl(J).line_id;
15327                       END IF;
15328 
15329 		  END IF;
15330 		  l_line_tbl(J).ship_from_org_id := p_x_line_tbl(I).ship_from_org_id;
15331 		  l_line_tbl(J).subinventory:= null;
15332 		  --bug 13954474
15333 		  l_line_tbl(J).SPLIT_SHIP_FROM :=p_x_line_tbl(I).SPLIT_SHIP_FROM;
15334                 --bug 13954474
15335                END IF;
15336 	       IF p_x_line_tbl(I).ship_to_org_id IS NOT NULL AND
15337                   p_x_line_tbl(I).ship_to_org_id <> fnd_api.G_MISS_NUM AND
15338                   NOT OE_GLOBALS.EQUAL(p_x_line_tbl(I).ship_to_org_id
15339                                       ,l_line_tbl(J).ship_to_org_id) AND
15340 		  NVL(p_x_line_tbl(I).SPLIT_SHIP_TO,'N') = 'Y' THEN -- 10278858
15341 				      oe_debug_pub.add(  'Next for 4');
15342                   l_line_tbl(J).ship_to_org_id := p_x_line_tbl(I).ship_to_org_id;
15343 		    --bug 13954474
15344 		    l_line_tbl(J).SPLIT_SHIP_TO :=p_x_line_tbl(I).SPLIT_SHIP_TO;
15345 		    --bug 13954474
15346                END IF;
15347 	       IF l_line_tbl(J).line_id = p_x_line_tbl(I).line_id THEN
15348   	          l_line_tbl(J).operation := p_x_line_tbl(I).operation;
15349                ELSE
15350                   l_line_tbl(J).operation := oe_globals.g_opr_update;
15351 		  oe_debug_pub.add(  'Next for 10');
15352                END IF;
15353 	       IF l_line_tbl(J).schedule_ship_date is NOT NULL THEN
15354 	         l_line_tbl(J).schedule_action_code :=
15355                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15356                  l_need_scheduling := TRUE;
15357 		 oe_debug_pub.add(  'Next for 14');
15358                END IF;
15359 	       --8706868
15360 		--bug 13954474
15361 		ELSIF p_x_line_tbl(I).line_id = l_line_tbl(J).LINE_ID AND
15362 	              l_line_tbl(J).LINE_ID <> l_line_tbl(J).TOP_MODEL_LINE_ID AND
15363 	            	p_x_line_tbl(I).ship_set_id IS NULL OR p_x_line_tbl(I).arrival_set_id IS NULL THEN
15364                    l_line_tbl(J).SPLIT_REQUEST_DATE :=p_x_line_tbl(I).SPLIT_REQUEST_DATE;
15365                    l_line_tbl(J).SPLIT_SHIP_FROM :=p_x_line_tbl(I).SPLIT_SHIP_FROM;
15366 		     l_line_tbl(J).SPLIT_SHIP_TO :=p_x_line_tbl(I).SPLIT_SHIP_TO;
15367 		--bug 13954474
15368             END IF;
15369 	 END LOOP;
15370     END IF; --Model check
15371    END LOOP;
15372 
15373 --   G_OVERRIDE_FLAG := 'N'; -- 8706868 -- 'Y';
15374    l_old_line_tbl  := l_line_tbl; --8706868
15375    IF l_debug_level  > 0 THEN
15376      oe_debug_pub.add(  'LINE COUNT '||l_line_tbl.count,1);
15377    END IF;
15378    IF l_line_tbl.count > 0  THEN
15379       FOR I in 1..l_line_tbl.count LOOP
15380          --10038568 restrict return lines.
15381 	 --10208311 do not check shipped Qty and Fulfilled qty if its a system split
15382          IF ((l_line_tbl(I).shipped_quantity is null AND -- 9465045
15383             l_line_tbl(I).fulfilled_quantity IS NULL) OR --10072873
15384 	        l_line_tbl(I).split_by = 'SYSTEM') AND
15385             l_line_tbl(I).source_type_code = OE_GLOBALS.G_SOURCE_INTERNAL AND
15386             l_line_tbl(I).item_type_code <> OE_GLOBALS.G_ITEM_SERVICE AND
15387 			NVL(l_line_tbl(I).subscription_enable_flag,'N') <> 'Y' AND --sol_ord_er #16014165
15388             l_line_tbl(I).line_category_code <>  'RETURN' AND
15389             l_line_tbl(I).schedule_ship_date IS NOT NULL AND
15390             (Nvl(l_line_tbl(I).bypass_sch_flag, 'N') = 'N' --DOO Integraton
15391              OR l_line_tbl(I).bypass_sch_flag =FND_API.G_MISS_CHAR) THEN -- 13392107
15392 	    L := L+1;
15393 	    l_mrp_line_tbl(L):=l_line_tbl(I);
15394             --11694571
15395             -- If its a user split and there is change to split attributes then
15396             -- reschedule in override mode.
15397             IF l_line_tbl(I).split_by = 'USER' AND
15398               (NVL(l_line_tbl(I).SPLIT_REQUEST_DATE,'N') = 'N' OR
15399                 (NVL(l_line_tbl(I).SPLIT_REQUEST_DATE,'N') = 'Y' AND
15400                  NVL(OE_SYS_PARAMETERS.value('RESCHEDULE_REQUEST_DATE_FLAG'),'Y') = 'N')) AND --12833832
15401               NVL(l_line_tbl(I).SPLIT_SHIP_FROM,'N') = 'N' AND
15402               NVL(l_line_tbl(I).SPLIT_SHIP_TO,'N') = 'N' THEN
15403 
15404               l_mrp_line_tbl(L).override_atp_date_code := 'Y';
15405             END IF;
15406 
15407 	    l_old_mrp_line_tbl(L):=l_line_tbl(I); --10072873
15408 	    l_mrp_line_tbl(L).schedule_action_code :=
15409                            OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE;
15410 	    l_need_scheduling := TRUE;
15411 	    IF l_debug_level  > 0 THEN
15412               oe_debug_pub.add(  'MRP Line Id '||l_line_tbl(I).line_id,1);
15413 	      oe_debug_pub.add(  'Req date '||l_line_tbl(I).request_date,1);
15414 	      oe_debug_pub.add(  'Ship From '||l_line_tbl(I).ship_from_org_id,1);
15415             END IF;
15416 	 ELSE
15417             M:= M+1;
15418 	    l_non_mrp_line_tbl(M):=l_line_tbl(I);
15419             IF l_debug_level  > 0 THEN
15420               oe_debug_pub.add(  'NON MRP Line Id '||l_line_tbl(I).line_id,1);
15421 	      oe_debug_pub.add(  'Req date '||l_line_tbl(I).request_date,1);
15422 	      oe_debug_pub.add(  'Ship From '||l_line_tbl(I).ship_from_org_id,1);
15423             END IF;
15424 	 END IF;
15425       END LOOP;
15426       L :=0;
15427       /*
15428       FOR I in 1..l_old_line_tbl.count LOOP
15429           IF l_old_line_tbl(I).schedule_ship_date IS NOT NULL THEN
15430 	     L:=L+1;
15431              l_old_mrp_line_tbl(L):=l_old_line_tbl(I);
15432 	  END IF;
15433       END LOOP;
15434       */
15435       IF l_need_scheduling = TRUE THEN
15436         -- 8706868
15437         --Validate the line info
15438 	--10208311 no need to call validate_group if its a system split
15439          IF l_line_tbl(1).split_by = 'SYSTEM' THEN
15440             l_return_status := FND_API.G_RET_STS_SUCCESS;
15441          ELSE
15442             OE_GROUP_SCH_UTIL.Validate_Group
15443              (p_x_line_tbl      => l_mrp_line_tbl,
15444               p_sch_action      => OE_SCHEDULE_UTIL.OESCH_ACT_RESCHEDULE,
15445               p_validate_action => 'COMPLETE',
15446               x_return_status   => l_return_status);
15447 
15448 
15449             IF l_debug_level  > 0 THEN
15450               oe_debug_pub.add(  ' AFTER VALIDATE_GROUP : '||l_return_status,1);
15451             END IF;
15452         END IF;
15453         IF l_return_status = FND_API.G_RET_STS_SUCCESS
15454                           AND l_mrp_line_tbl.count > 0 THEN --12757660
15455 
15456            IF l_debug_level  > 0 THEN
15457              oe_debug_pub.add(  'SPLIT BEFORE CALLING LOAD_MRP_REQUEST' , 2 ) ;
15458            END IF;
15459 
15460            Load_MRP_request_from_tbl
15461            ( p_line_tbl          => l_mrp_line_tbl --l_line_tbl
15462             ,p_old_line_tbl      => l_old_mrp_line_tbl --l_old_line_tbl --8706868
15463             ,p_sch_action        => OESCH_ACT_RESCHEDULE
15464             ,x_mrp_atp_rec       => l_mrp_atp_rec);
15465 
15466            l_session_id := Get_Session_Id;
15467 
15468            -- Call ATP
15469            IF l_debug_level  > 0 THEN
15470               oe_debug_pub.add(  'COUNT IS ' || L_MRP_ATP_REC.ERROR_CODE.COUNT , 1 ) ;
15471            END IF;
15472 
15473            -- We are adding this so that we will not call MRP when
15474            -- table count is 0.
15475 
15476            IF l_mrp_atp_rec.error_code.count > 0 THEN
15477 
15478               IF l_debug_level  > 0 THEN
15479                  oe_debug_pub.add(  'SPLIT CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 0.5 ) ;  -- debug level changed to 0.5 for bug 13435459
15480               END IF;
15481 
15482               MRP_ATP_PUB.Call_ATP
15483               (  p_session_id             =>  l_session_id
15484                , p_atp_rec                =>  l_mrp_atp_rec
15485                , x_atp_rec                =>  l_out_mtp_atp_rec
15486                , x_atp_supply_demand      =>  l_atp_supply_demand
15487                , x_atp_period             =>  l_atp_period
15488                , x_atp_details            =>  l_atp_details
15489                , x_return_status          =>  l_return_status
15490                , x_msg_data               =>  mrp_msg_data
15491                , x_msg_count              =>  l_msg_count);
15492 
15493 
15494              IF l_debug_level  > 0 THEN
15495                oe_debug_pub.add(  'SPLIT. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || L_RETURN_STATUS ,0.5 ) ; -- debug level changed to 0.5 for bug 13435459
15496              END IF;
15497 
15498              IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15499                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15500              END IF;
15501              --10338643
15502              IF NVL(l_mrp_line_tbl(1).split_by,'USER') <> 'SYSTEM' THEN
15503 
15504                 Load_Results_from_tbl
15505                 (p_atp_rec        => l_out_mtp_atp_rec,
15506                  p_old_line_tbl   => l_old_mrp_line_tbl, --l_old_line_tbl,-- l_line_tbl, -- 8706868
15507                  p_x_line_tbl     => l_mrp_line_tbl, --l_line_tbl,
15508                  x_return_status  => l_return_status);
15509                 --11694571
15510                 FOR I in 1..l_mrp_line_tbl.count LOOP
15511                    l_mrp_line_tbl(I).override_atp_date_code := l_old_mrp_line_tbl(I).override_atp_date_code;
15512                    l_mrp_line_tbl(I).operation := oe_globals.g_opr_update; --14350185:MRP might return different warehouse or date
15513                 END LOOP;
15514 
15515                 IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15516                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15517                 ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
15518                   RAISE FND_API.G_EXC_ERROR;
15519                 END IF;
15520              --8706868
15521              END IF;
15522 	     oe_config_schedule_pvt.Save_Sch_Attributes
15523                   ( p_x_line_tbl     => l_mrp_line_tbl
15524                    ,p_old_line_tbl   => l_old_mrp_line_tbl
15525                    ,p_sch_action     => OESCH_ACT_RESCHEDULE
15526                    ,p_caller         => OE_SCHEDULE_UTIL.SCH_EXTERNAL
15527                    ,x_return_status  => l_return_status);
15528 
15529              IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15530                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15531              ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
15532                RAISE FND_API.G_EXC_ERROR;
15533              END IF;
15534            END IF; -- MRP count check.
15535          END IF;
15536        END IF; -- need scheduling
15537        IF l_non_mrp_line_tbl.count >1 THEN
15538          l_local_line_tbl.delete;
15539 	 l_local_line_tbl := l_non_mrp_line_tbl;
15540 	 oe_config_schedule_pvt.Save_Sch_Attributes
15541                   ( p_x_line_tbl     => l_non_mrp_line_tbl --l_line_tbl
15542                    ,p_old_line_tbl   => l_local_line_tbl --l_line_tbl -- l_old_line_tbl
15543                    ,p_sch_action     => OESCH_ACT_RESCHEDULE
15544                    ,p_caller         => OE_SCHEDULE_UTIL.SCH_EXTERNAL
15545                    ,x_return_status  => l_return_status);
15546 
15547             IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15548                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15549             ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
15550                RAISE FND_API.G_EXC_ERROR;
15551             END IF;
15552        END IF;
15553    END IF; -- line count.
15554 
15555    G_OVERRIDE_FLAG := 'N';
15556 
15557    x_return_status := l_return_status;
15558 
15559   IF l_debug_level  > 0 THEN
15560       oe_debug_pub.add(  'EXITING PROCESS SPLIT' , 1 ) ;
15561   END IF;
15562 EXCEPTION
15563   -- 8706868
15564   WHEN FND_API.G_EXC_ERROR THEN
15565     x_return_status := FND_API.G_RET_STS_ERROR;
15566 
15567   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
15568     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
15569   WHEN OTHERS THEN
15570 
15571         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
15572         THEN
15573             OE_MSG_PUB.Add_Exc_Msg
15574             (   G_PKG_NAME
15575             ,   'PROCESS_SPLIT'
15576             );
15577         END IF;
15578 
15579 END PROCESS_SPLIT;
15580 
15581 /*--------------------------------------------------------------------
15582 Procedure Name : Split_Scheduling
15583 Description    : The split API calls this procedure with a table of record.
15584                  There is an update line (the line which is getting split)
15585                  and multiple insert lines (new lines created due to the split).
15586                  We need to do the following:
15587 
15588                  For scheduling
15589                  -------------
15590                  On the updated line: Reschedule the line.
15591                  On the inserted lines: Schedule the lines.
15592 
15593                  For reservation
15594                  ---------------
15595                  If the split is due to shipping, we need to update the
15596                  reservations (whichever exist) to the new line which
15597                  got created.
15598 
15599                  If the split is due to the user splitting, there could be
15600                  multiple records created due to the split. We should update
15601                  the old reservation to reflect the change in qty for the
15602                  original line, and create new reservations for the new lines
15603                  which got created.
15604 
15605 
15606 ---------------------------------------------------------------------- */
15607 
15608 
15609 Procedure SPLIT_SCHEDULING
15610 ( p_x_line_tbl         IN OUT NOCOPY OE_ORDER_PUB.line_tbl_type
15611 , x_return_status OUT NOCOPY VARCHAR2)
15612 
15613 IS
15614 l_rsv_qty               NUMBER := 0;
15615 l_header_id             NUMBER;
15616 l_line_id               NUMBER;
15617 l_sales_order_id        NUMBER;
15618 l_qty_to_retain         NUMBER;
15619 l_x_error_code          NUMBER;
15620 l_lock_records          VARCHAR2(1);
15621 l_sort_by_req_date      NUMBER;
15622 continue_loop           BOOLEAN;
15623 K                       NUMBER;
15624 J                       NUMBER;
15625 l_reserved_quantity     NUMBER;
15626 l_msg_count             NUMBER;
15627 l_query_rsv_rec         inv_reservation_global.mtl_reservation_rec_type;
15628 l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
15629 l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
15630 l_msg_data              VARCHAR2(240);
15631 l_count                 NUMBER;
15632 l_available_qty         NUMBER := 0;
15633 --- 2346233 --
15634 l_ordered_quantity      NUMBER;
15635 l_rsv_qty_primary       NUMBER :=0;
15636 
15637 -- INVCONV
15638 l_reserved_quantity2     NUMBER;
15639 l_ordered_quantity2      NUMBER;
15640 l_rsv_qty_secondary      NUMBER :=0;
15641 l_qty2_to_retain         NUMBER;
15642 l_rsv_qty2               NUMBER := 0;
15643 l_available_qty2         NUMBER := 0;
15644 l_do_reservation         BOOLEAN :=FALSE; -- 8706868
15645 l_line_found             BOOLEAN := FALSE; -- 8706868
15646 --
15647 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
15648 --
15649 BEGIN
15650 
15651   IF l_debug_level  > 0 THEN
15652       oe_debug_pub.add(  '31. ENTERING SPLIT_SCHEDULING' , 1 ) ;
15653   END IF;
15654 
15655   x_return_status := FND_API.G_RET_STS_SUCCESS;
15656   IF l_debug_level  > 0 THEN
15657       oe_debug_pub.add(  'PICTURE SENT ' , 1 ) ;
15658   END IF;
15659 
15660   FOR I IN 1..p_x_line_tbl.count LOOP
15661       IF l_debug_level  > 0 THEN
15662           oe_debug_pub.add(  'LINE ID ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
15663           oe_debug_pub.add(  'SPLIT ID ' || P_X_LINE_TBL ( I ) .SPLIT_FROM_LINE_ID , 1 ) ;
15664           oe_debug_pub.add(  'SPLIT ACTION ' || P_X_LINE_TBL ( I ) .SPLIT_ACTION_CODE , 1 ) ;
15665           oe_debug_pub.add(  'OPERATIONS ' || P_X_LINE_TBL ( I ) .OPERATION , 1 ) ;
15666       END IF;
15667   END LOOP;
15668   IF p_x_line_tbl(1).split_by = 'SYSTEM' THEN
15669      G_OVERRIDE_FLAG     := 'Y';
15670   ELSE
15671      G_OVERRIDE_FLAG     := 'N'; --'Y'; --8706868
15672   END IF;
15673   IF l_debug_level  > 0 THEN
15674       oe_debug_pub.add(  'COUNT IS :' || P_X_LINE_TBL.COUNT , 1 ) ;
15675   END IF;
15676 
15677   process_split(p_x_line_tbl => p_x_line_tbl,
15678                 x_return_status => x_return_status);
15679 
15680   IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN --8706868
15681   -- get the latest picture.9662817
15682  FOR I in 1..p_x_line_tbl.count LOOP
15683        IF p_x_line_tbl(I).item_type_code <> 'STANDARD' THEN --AND
15684           --p_x_line_tbl(I).schedule_status_code is not null THEN
15685           IF l_debug_level  > 0 THEN
15686               oe_debug_pub.add(  'LINE: ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
15687           END IF;
15688           SELECT schedule_ship_date,schedule_arrival_date,ship_from_org_id
15689           INTO p_x_line_tbl(I).schedule_ship_date,p_x_line_tbl(I).schedule_arrival_date,
15690                p_x_line_tbl(I).ship_from_org_id
15691           FROM OE_ORDER_LINES_ALL
15692           WHERE line_id=p_x_line_tbl(I).line_id;
15693        END IF;
15694   END LOOP;
15695   FOR I in 1..p_x_line_tbl.count LOOP
15696   /* --9615081
15697        IF p_x_line_tbl(I).item_type_code <> 'STANDARD' THEN --AND
15698           --p_x_line_tbl(I).schedule_status_code is not null THEN
15699           IF l_debug_level  > 0 THEN
15700               oe_debug_pub.add(  'LINE: ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
15701           END IF;
15702           SELECT schedule_ship_date,schedule_arrival_date,ship_from_org_id
15703           INTO p_x_line_tbl(I).schedule_ship_date,p_x_line_tbl(I).schedule_arrival_date,p_x_line_tbl(I).ship_from_org_id
15704           FROM OE_ORDER_LINES_ALL
15705           WHERE line_id=p_x_line_tbl(I).line_id;
15706        END IF;
15707        */
15708        IF p_x_line_tbl(I).operation = OE_GLOBALS.G_OPR_UPDATE AND
15709           p_x_line_tbl(I).schedule_status_code is not null AND
15710           p_x_line_tbl(I).split_action_code = 'SPLIT' THEN
15711 
15712           IF l_debug_level  > 0 THEN
15713               oe_debug_pub.add(  'SPLITTING SCHEDULING' , 1 ) ;
15714               oe_debug_pub.add(  'SPLITTING SCHEDULING FOR LINE: ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
15715           END IF;
15716 
15717           -- Only play with reservation if the line is not interfaced with
15718           -- shipping. Otherwise shipping will take care of transferring
15719           -- reservations.
15720 
15721           IF  (p_x_line_tbl(I).shipped_quantity is null)
15722           AND (nvl(p_x_line_tbl(I).shipping_interfaced_flag, 'N') = 'N')
15723 	  AND (nvl(p_x_line_tbl(I).shippable_flag,'N') = 'Y')
15724           THEN
15725 
15726              -- We have updated the demand picture in MRP with the split.
15727              -- Now let's update the reservation picture.
15728 
15729              --      l_query_rsv_rec                := null;
15730              l_query_rsv_rec.reservation_id := fnd_api.g_miss_num;
15731              l_sales_order_id
15732                        := Get_mtl_sales_order_id(p_x_line_tbl(I).header_id);
15733              l_query_rsv_rec.demand_source_header_id  := l_sales_order_id;
15734              l_query_rsv_rec.demand_source_line_id    := p_x_line_tbl(I).line_id;
15735 
15736              -- 02-jun-2000 mpetrosi added org_id to query_reservation start
15737              l_query_rsv_rec.organization_id := p_x_line_tbl(I).ship_from_org_id;
15738              -- 02-jun-2000 mpetrosi added org_id to query_reservation end
15739 
15740 
15741              IF l_debug_level  > 0 THEN
15742                 oe_debug_pub.add(  'CALLING INVS QUERY_RESERVATION ' , 1 ) ;
15743              END IF;
15744 
15745              inv_reservation_pub.query_reservation
15746               ( p_api_version_number       => 1.0
15747               , p_init_msg_lst              => fnd_api.g_true
15748               , x_return_status             => x_return_status
15749               , x_msg_count                 => l_msg_count
15750               , x_msg_data                  => l_msg_data
15751               , p_query_input               => l_query_rsv_rec
15752               , x_mtl_reservation_tbl       => l_rsv_tbl
15753               , x_mtl_reservation_tbl_count => l_count
15754               , x_error_code                => l_x_error_code
15755               , p_lock_records              => l_lock_records
15756               , p_sort_by_req_date          => l_sort_by_req_date
15757               );
15758 
15759              IF l_debug_level  > 0 THEN
15760                 oe_debug_pub.add(  'AFTER CALLING INVS QUERY_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
15761              END IF;
15762 
15763              IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
15764                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
15765              ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
15766                 RAISE FND_API.G_EXC_ERROR;
15767              END IF;
15768              IF l_debug_level  > 0 THEN
15769               oe_debug_pub.add(  'RESERVATION RECORD COUNT IS: ' || L_RSV_TBL.COUNT , 1 ) ;
15770              END IF;
15771 
15772              -- Let's get the total reserved_quantity
15773              l_reserved_quantity := 0;
15774              -- INVCONV
15775              l_reserved_quantity2 := 0;
15776 
15777              FOR K IN 1..l_rsv_tbl.count LOOP
15778               -- Start 2346233 --
15779 /*               l_reserved_quantity := l_reserved_quantity +
15780                                        l_rsv_tbl(K).reservation_quantity;
15781 */
15782 
15783                 l_reserved_quantity := l_reserved_quantity + l_rsv_tbl(K).primary_reservation_quantity;
15784                 l_reserved_quantity2 := l_reserved_quantity2 + l_rsv_tbl(K).secondary_reservation_quantity; -- INVCONV
15785                 -- End 2346233 --
15786              END LOOP;
15787 
15788              IF l_debug_level  > 0 THEN
15789                  oe_debug_pub.add(  'RESERVED QUANTITY : ' || L_RESERVED_QUANTITY , 1 ) ;
15790                  oe_debug_pub.add(  'RESERVED QUANTITY2 : ' || L_RESERVED_QUANTITY2 , 1 ) ;
15791              END IF;
15792              -- We should process reservation logic only when reservation qty exists
15793              -- and parent line order qty is less than total reserved qty. If parent
15794              -- ordered qty is greter or equal to total reserved qty, we do not need
15795              -- transfer any reservations.
15796 
15797              -- Start 2346233 --
15798              IF  l_reserved_quantity > 0 THEN
15799                 IF NOT OE_GLOBALS.Equal(p_x_line_tbl(I).order_quantity_uom,
15800                                        l_rsv_tbl(1).primary_uom_code) THEN
15801                    l_ordered_quantity :=
15802                         INV_CONVERT.INV_UM_CONVERT( item_id       => p_x_line_tbl(I).inventory_item_id,
15803                                                     precision     => 5,
15804                                                     from_quantity =>p_x_line_tbl(I).ordered_quantity,
15805                                                     from_unit     =>p_x_line_tbl(I).order_quantity_uom,
15806                                                     to_unit       =>l_rsv_tbl(1).primary_uom_code,
15807                                                     from_name     =>NULL,
15808                                                     to_name       =>NULL
15809                                                    );
15810 
15811                 ELSE
15812                    l_ordered_quantity := p_x_line_tbl(I).ordered_quantity;
15813                 END IF;
15814                 l_ordered_quantity2 := p_x_line_tbl(I).ordered_quantity2; -- INVCONV
15815              END IF;
15816 
15817 /*             IF   l_reserved_quantity > 0
15818             AND  p_x_line_tbl(I).ordered_quantity < l_reserved_quantity
15819             THEN */  --'IF' modified for bug16788917
15820                ---- End 2346233 ---
15821              IF  (   (l_reserved_quantity > 0  AND  l_ordered_quantity < l_reserved_quantity
15822                         AND  Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15823                    OR (l_reserved_quantity2 > 0  AND  l_ordered_quantity2 < l_reserved_quantity2
15824                         AND  Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15825                  )-- if condition modified for bug16788917
15826              THEN
15827 
15828                -- There can be 2 kinds of splits. One where the user split,
15829                -- in which case, the reservations have to split if line is not
15830                -- shipping interfaced. WSH interfaced line will be taken careby WSH.
15831                -- System split happens when shipping occurs partially.
15832                -- In that case, the remaining reservations are trasferred to the
15833                -- new line. This will be taken care by shipping code.
15834 
15835                -- l_qty_to_retain is to retain in the reservation record.
15836                -- l_available_qty is reamining qty that can be transferred.
15837 
15838                IF l_debug_level  > 0 THEN
15839                   oe_debug_pub.add(  'USER INITIATED SPLIT' , 1 ) ;
15840                END IF;
15841 
15842                --- Start 2346233 --
15843                -- l_qty_to_retain := p_x_line_tbl(I).ordered_quantity;
15844                l_qty_to_retain := l_ordered_quantity;
15845                l_qty2_to_retain := l_ordered_quantity2; -- INVCONV
15846                --- End 2346233 --
15847 
15848 /*               need OPM branching  -- Don't branch now  INVCONV
15849                IF NOT INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_x_line_tbl(I).ship_from_org_id) THEN */
15850                J:= 1;
15851                FOR K IN 1..l_rsv_tbl.count LOOP
15852 --                  IF l_qty_to_retain > 0 THEN -- If cond modified for bug16788917
15853                      IF (   (l_qty_to_retain >0   AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15854 		        OR  (l_qty2_to_retain > 0 AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15855 			 )
15856                      THEN
15857                      IF l_debug_level  > 0 THEN
15858                         oe_debug_pub.add(  'L_QTY_TO_RETAIN ' || L_QTY_TO_RETAIN , 1 ) ;
15859                         oe_debug_pub.add(  'L_QTY2_TO_RETAIN ' || l_qty2_to_retain , 1 ) ;
15860                      END IF;
15861                          ---- Start 2346233 ---
15862 --                     IF l_rsv_tbl(K).primary_reservation_quantity <= l_qty_to_retain --bug16788917
15863                      IF (   (l_rsv_tbl(K).primary_reservation_quantity <= l_qty_to_retain
15864 		                               AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15865 		        OR  (l_rsv_tbl(K).secondary_reservation_quantity <= l_qty2_to_retain
15866 		                               AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15867 			 )-- If cond modified for bug16788917
15868                      THEN
15869                         l_qty_to_retain := l_qty_to_retain -
15870                                           l_rsv_tbl(K).primary_reservation_quantity;
15871                         l_qty2_to_retain := nvl(l_qty2_to_retain, 0) -
15872                                           nvl(l_rsv_tbl(K).secondary_reservation_quantity, 0); -- INVCONV
15873 /*
15874                       IF l_rsv_tbl(K).reservation_quantity <= l_qty_to_retain
15875                       THEN
15876                          l_qty_to_retain := l_qty_to_retain -
15877                                           l_rsv_tbl(K).reservation_quantity;
15878 */
15879                       ---- End 2346233 ---
15880 
15881                        IF l_debug_level  > 0 THEN
15882                           oe_debug_pub.add(  'BEFORE DELETE ' || K || '/' || L_RSV_TBL ( K ) .RESERVATION_QUANTITY , 1 ) ;
15883                           oe_debug_pub.add(  'BEFORE DELETE Primary ' || K || '/' || L_RSV_TBL ( K ) .PRIMARY_RESERVATION_QUANTITY , 1 ) ;
15884                        END IF;
15885                        l_rsv_tbl.delete(K);
15886                      END IF;
15887 
15888                   END IF; -- l_qty_to_retain.
15889 
15890                END LOOP;
15891 
15892                K := l_rsv_tbl.first;
15893                continue_loop := TRUE;
15894                l_rsv_rec := l_rsv_tbl(k);
15895                WHILE K IS NOT NULL LOOP
15896                   IF l_debug_level  > 0 THEN
15897                      oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP K ' || K , 1 ) ;
15898                   END IF;
15899    --               IF l_qty_to_retain = 0 THEN-- If cond modified for bug16788917
15900                      IF (   (l_qty_to_retain =0 AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15901 		        OR  (l_qty2_to_retain =0 AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15902 			 ) THEN
15903                      l_rsv_rec := l_rsv_tbl(k);
15904                   END IF;
15905 --start bug16788917
15906 --pro-rate pending qty to retain, based on FB and reserved qty combination
15907                IF Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P' THEN
15908                   l_qty2_to_retain := l_qty_to_retain * l_rsv_rec.secondary_reservation_quantity /l_rsv_rec.primary_reservation_quantity ;
15909                ELSE --FB ='S'
15910                   l_qty_to_retain := l_qty2_to_retain * l_rsv_rec.primary_reservation_quantity /l_rsv_rec.secondary_reservation_quantity ;
15911                END IF ;
15912 --end bug16788917
15913 
15914                   WHILE J <= p_x_line_tbl.count AND continue_loop
15915                   LOOP
15916                      IF l_debug_level  > 0 THEN
15917                          oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP J ' || J , 1 ) ;
15918                      END IF;
15919                      IF p_x_line_tbl(J).operation =
15920                                         OE_GLOBALS.G_OPR_CREATE AND
15921                         p_x_line_tbl(J).split_from_line_id =
15922                                          p_x_line_tbl(I).line_id AND
15923                          p_x_line_tbl(J).ship_from_org_id =
15924                                          p_x_line_tbl(I).ship_from_org_id    --8706868
15925                      THEN
15926                          continue_loop := FALSE;
15927                          l_rsv_qty := p_x_line_tbl(J).ordered_quantity;
15928                          l_rsv_qty2 := p_x_line_tbl(J).ordered_quantity2; -- INVCONV
15929                          ---- Start 2346233 ---
15930                          IF NOT OE_GLOBALS.Equal(p_x_line_tbl(J).order_quantity_uom,
15931                                                  l_rsv_rec.primary_uom_code) THEN
15932                             l_rsv_qty_primary :=
15933                               INV_CONVERT.INV_UM_CONVERT( item_id       => p_x_line_tbl(J).inventory_item_id,
15934                                                           precision     => 5,
15935                                                           from_quantity =>p_x_line_tbl(J).ordered_quantity,
15936                                                           from_unit     =>p_x_line_tbl(J).order_quantity_uom,
15937                                                           to_unit       =>l_rsv_rec.primary_uom_code,
15938                                                           from_name     =>NULL,
15939                                                           to_name       =>NULL
15940                                                         );
15941 
15942                          ELSE
15943                             l_rsv_qty_Primary := p_x_line_tbl(J).ordered_quantity;
15944                          END IF;
15945                          l_rsv_qty_secondary := p_x_line_tbl(J).ordered_quantity2; -- INVCONV
15946                          ---- End 2346233 ---
15947 
15948                          IF l_debug_level  > 0 THEN
15949                             oe_debug_pub.add(  'FOUND NEW RECORD ' || P_X_LINE_TBL ( J ) .LINE_ID || '/' ||L_RSV_QTY , 1 ) ;
15950                          END IF;
15951                          l_line_id :=  p_x_line_tbl(J).line_id;
15952                          J := J +1;
15953                      ELSE
15954                          J := J + 1;
15955                      END IF;
15956                  END LOOP;
15957 
15958   --                 IF l_rsv_qty > 0  THEN--modified for bug16788917
15959                      IF  ( l_rsv_qty_primary > 0 AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15960                        OR ( l_rsv_qty_secondary > 0 AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15961 		     THEN
15962 --start bug16788917
15963 --pro-rate qty to transfer, based on FB and reserved qty combination
15964                IF Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P' THEN
15965                   l_rsv_qty_secondary := l_rsv_qty_Primary * l_rsv_rec.secondary_reservation_quantity /l_rsv_rec.primary_reservation_quantity ;
15966                ELSE --FB ='S'
15967                   l_rsv_qty_Primary := l_rsv_qty_secondary * l_rsv_rec.primary_reservation_quantity /l_rsv_rec.secondary_reservation_quantity ;
15968                END IF ;
15969 --end bug16788917
15970 		     --- Start 2346233 --
15971 /*                      l_available_qty := l_rsv_rec.reservation_quantity -
15972                                                     l_qty_to_retain;  */
15973                      l_available_qty := l_rsv_rec.primary_reservation_quantity -
15974                                         l_qty_to_retain;
15975                      l_available_qty2 := nvl(l_rsv_rec.secondary_reservation_quantity, 0) -
15976                                         nvl(l_qty2_to_retain, 0) ;  -- INVCONV
15977                      IF l_available_qty2 = 0
15978                       THEN
15979                       l_available_qty2 := NULL;
15980                      END IF;
15981 
15982 
15983 
15984 
15985 
15986                      IF l_debug_level  > 0 THEN
15987                         oe_debug_pub.add(  'L_RSV_QTY_PRIMARY' || l_rsv_qty_primary , 1 ) ;
15988                         oe_debug_pub.add(  'L_RSV_QTY_SECONDARY' || l_rsv_qty_secondary , 1 ) ;--bug16788917
15989                      END IF;
15990 
15991                      -- IF  l_rsv_qty <= l_available_qty  THEN
15992 --                     IF  l_rsv_qty_primary <= l_available_qty  THEN -- bug16788917
15993                      --- End 2346233 --
15994                      IF  ( l_rsv_qty_primary <= l_available_qty AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
15995                        OR ( l_rsv_qty_secondary <= NVL(l_available_qty2,0) AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
15996 		     THEN
15997 
15998 
15999                        IF l_debug_level  > 0 THEN
16000                            oe_debug_pub.add(  'AVAILABLE MORE THAN NEEDED ' || L_AVAILABLE_QTY , 1 ) ;
16001                            oe_debug_pub.add(  'AVAILABLE QTY 2: ' || l_available_qty2 , 1 ) ;--bug16788917
16002                        END IF;
16003                        -- Transfer full l_rsv_qty.
16004                        Transfer_Reservation
16005                        (p_rsv_rec              => l_rsv_rec,
16006 --                        p_quantity_to_transfer => l_rsv_qty,
16007 --                        p_quantity2_to_transfer => l_rsv_qty2, -- INVCONV
16008                         p_quantity_to_transfer => l_rsv_qty_primary,--bug16788917 issue 1
16009                         p_quantity2_to_transfer => l_rsv_qty_secondary,--bug16788917
16010                         p_line_to_transfer     => l_line_id,
16011                         x_return_status        => x_return_status);
16012                         --- Start 2346233 --
16013 /*                         l_rsv_rec.reservation_quantity := l_rsv_rec.reservation_quantity -
16014                                                          l_rsv_qty;
16015                         -- l_qty_to_retain := l_qty_to_retain + l_rsv_qty;
16016                         l_available_qty := l_rsv_rec.reservation_quantity -
16017                                                         l_qty_to_retain; */
16018                         l_rsv_rec.primary_reservation_quantity :=
16019                                 l_rsv_rec.primary_reservation_quantity - l_rsv_qty_primary;
16020                         l_available_qty := l_rsv_rec.primary_reservation_quantity - l_qty_to_retain;
16021                         l_rsv_qty_primary := 0;
16022                                                 -- INVCONV
16023                         l_rsv_rec.secondary_reservation_quantity :=
16024                                 nvl(l_rsv_rec.secondary_reservation_quantity,0) - nvl(l_rsv_qty_secondary,0 );
16025 
16026                         IF l_rsv_rec.secondary_reservation_quantity = 0 -- INVCONV
16027                           THEN
16028                            l_rsv_rec.secondary_reservation_quantity := NULL;
16029                         END IF;
16030 
16031                         l_available_qty2 := l_rsv_rec.secondary_reservation_quantity - l_qty2_to_retain;
16032 
16033                         IF l_available_qty2 = 0 -- INVCONV
16034                           THEN
16035                            l_available_qty2 := NULL;
16036                         END IF;
16037 
16038 
16039                         l_rsv_qty_secondary := 0;
16040 
16041 
16042                         --- End 2346233 --
16043 
16044                        l_rsv_qty := 0;
16045 		       l_line_found := TRUE; -- 8706868
16046                        l_rsv_qty2 := 0; -- INVCONV
16047                        continue_loop := TRUE;
16048                        IF l_debug_level  > 0 THEN
16049                            oe_debug_pub.add(  'AVAILABLE QTY ' || L_AVAILABLE_QTY , 1 ) ;
16050                            oe_debug_pub.add(  'AVAILABLE QTY2 '|| L_AVAILABLE_QTY2 , 1 ) ;--bug16788917
16051                       END IF;
16052 --bug16788917 /issue 3
16053                        l_rsv_tbl(k).primary_reservation_quantity  := l_rsv_rec.primary_reservation_quantity;
16054                        l_rsv_tbl(k).secondary_reservation_quantity := l_rsv_rec.secondary_reservation_quantity;
16055 --end bug16788917
16056                        -- Start 2346233 --
16057                        --IF l_rsv_rec.reservation_quantity = l_qty_to_retain THEN
16058                        -- Bug 5014710
16059  --                      IF round(l_rsv_rec.primary_reservation_quantity,5) = round(l_qty_to_retain,5) THEN
16060                        -- End 2346233 ----If cond moddified for bug16788917
16061                        IF (round(l_rsv_rec.primary_reservation_quantity,5) = round(l_qty_to_retain,5)
16062                                    AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
16063                          OR (round(l_rsv_rec.secondary_reservation_quantity,5) = round(l_qty2_to_retain,5)
16064                                    AND Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')--bug16788917
16065 			THEN
16066                           K := l_rsv_tbl.next(K);
16067                           l_available_qty := 0;
16068                           l_qty_to_retain := 0;
16069                           l_available_qty2 := 0; -- INVCONV
16070                           l_qty2_to_retain := 0; -- INVCONV
16071 
16072                        END IF;
16073                      ELSE
16074                            -- Transfer remaining
16075                        Transfer_Reservation
16076                        (p_rsv_rec               =>l_rsv_rec,
16077                         p_quantity_to_transfer  =>l_available_qty,
16078                         p_quantity2_to_transfer =>l_available_qty2, -- INVCONV
16079                         p_line_to_transfer     =>l_line_id,
16080                         x_return_status        =>x_return_status);
16081 
16082              --         l_rsv_qty := l_rsv_qty - l_available_qty;-- bug16788917
16083                         l_rsv_qty_primary := l_rsv_qty_primary - l_available_qty;--bug16788917
16084                         l_available_qty := 0;
16085                         l_qty_to_retain := 0;
16086 		        l_line_found := TRUE; -- 12317262
16087 
16088                         -- INVCONV
16089                         l_rsv_qty2 := nvl(l_rsv_qty2, 0) - nvl(l_available_qty2, 0); -- INVCONV
16090 			l_rsv_qty_secondary :=  nvl(l_rsv_qty_secondary, 0) - nvl(l_available_qty2, 0); --bug16788917
16091                         l_available_qty2 := 0;
16092                         l_qty2_to_retain := 0;
16093 
16094                         K := l_rsv_tbl.next(K);
16095                       END IF;
16096                     ELSE
16097 		       K := l_rsv_tbl.next(K); -- 8706868
16098 
16099                     END IF; -- l_rsv_qty > 0
16100                   -- END LOOP;
16101                  END LOOP; -- K loop.
16102 		 --8706868
16103                  IF l_rsv_qty = 0  AND not l_line_found THEN
16104                     l_available_qty := l_rsv_rec.primary_reservation_quantity - l_qty_to_retain;
16105                     l_available_qty2 := nvl(l_rsv_rec.secondary_reservation_quantity, 0) -
16106                                         nvl(l_qty2_to_retain, 0) ;  -- INVCONV
16107                     IF l_available_qty2 = 0  THEN
16108                        l_available_qty2 := NULL;
16109                     END IF;
16110                 END IF;
16111 
16112 		 --8706868 : Unreserve remaining extra qty.
16113  --                IF L_AVAILABLE_QTY > 0 THEN--bug16788917
16114 		 IF  (L_AVAILABLE_QTY > 0 AND  Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='P')
16115 		     OR  (L_AVAILABLE_QTY2 > 0 AND  Nvl(p_x_line_tbl(I).fulfillment_base ,'P') ='S')
16116 		  THEN
16117 		    Do_Unreserve
16118                      ( p_line_rec               => p_x_line_tbl(I)
16119                      , p_quantity_to_unreserve  => L_AVAILABLE_QTY--this needs to be in ordered uom not prim/NEEDS FIX
16120                      , p_quantity2_to_unreserve  => l_available_qty2
16121                      , p_old_ship_from_org_id    => p_x_line_tbl(I).ship_from_org_id
16122                     , x_return_status          => x_return_status);
16123 
16124 		 END IF;
16125 /*                 ELSE  if OPM -- INVCONV  - don;t need this branch cos no OPM now .
16126                  J:= 1;
16127                  -- Bug 3330925 (getting rid of continue_loop)
16128                  -- continue_loop := TRUE;
16129                  WHILE J <= p_x_line_tbl.count -- AND continue_loop
16130                  LOOP
16131                    IF l_debug_level  > 0 THEN
16132                        oe_debug_pub.add(  'IN THE BEGINING OF THE LOOP J TO FIND THE SPLITTED LINE ' || J , 1 ) ;
16133                    END IF;
16134 
16135 
16136                    IF p_x_line_tbl(J).operation =
16137                                       OE_GLOBALS.G_OPR_CREATE AND
16138                       p_x_line_tbl(J).split_from_line_id =
16139                                        p_x_line_tbl(I).line_id
16140                    THEN
16141                        -- continue_loop := FALSE;
16142                        l_rsv_qty := p_x_line_tbl(J).ordered_quantity;
16143                                          IF l_debug_level  > 0 THEN
16144                                              oe_debug_pub.add(  'FOUND NEW RECORD ' || P_X_LINE_TBL ( J ) .LINE_ID || '/' ||L_RSV_QTY , 1 ) ;
16145                                          END IF;
16146                        l_line_id :=  p_x_line_tbl(J).line_id;
16147 
16148                        -- Begin Bug 3330925
16149                        IF l_debug_level  > 0 THEN
16150                         oe_debug_pub.add(  'CALLING GMI SPLIT_TRANS_FROM_OM ' ) ;
16151                        END IF;
16152                        GMI_RESERVATION_UTIL.split_trans_from_OM
16153                          ( p_old_source_line_id     => p_x_line_tbl(I).line_id,
16154                            p_new_source_line_id     => l_line_id,
16155                            p_qty_to_split           => p_x_line_tbl(I).ordered_quantity,
16156                            p_qty2_to_split          => p_x_line_tbl(I).ordered_quantity2,
16157                            x_return_status          => x_return_status,
16158                            x_msg_count              => l_msg_count,
16159                            x_msg_data               => l_msg_data
16160                          );
16161                        -- End Bug 3330925
16162 
16163                        J := J +1;
16164                    ELSE
16165                        J := J + 1;
16166                    END IF;
16167                  END LOOP; -- J loop
16168 
16169                  -- Bug3330925 (moved this within the Loop above)
16170                   IF l_debug_level  > 0 THEN
16171                      oe_debug_pub.add(  'CALLING GMI SPLIT_TRANS_FROM_OM ' ) ;
16172                  END IF;
16173                  GMI_RESERVATION_UTIL.split_trans_from_OM
16174                      ( p_old_source_line_id     => p_x_line_tbl(I).line_id,
16175                        p_new_source_line_id     => l_line_id,
16176                        p_qty_to_split           => p_x_line_tbl(I).ordered_quantity,
16177                        p_qty2_to_split          => p_x_line_tbl(I).ordered_quantity2,
16178                        x_return_status          => x_return_status,
16179                        x_msg_count              => l_msg_count,
16180                        x_msg_data               => l_msg_data
16181                       );
16182                END IF; --  OPM branching */   -- INVCONV - not needed  now
16183             --8706868
16184             ELSIF l_reserved_quantity = 0 AND
16185 	         Within_Rsv_Time_Fence(p_x_line_tbl(I).schedule_ship_date,
16186                                  p_x_line_tbl(I).org_id)  AND
16187     	        (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
16188                  sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
16189                  sch_cached_sch_level_code is null)
16190               AND (NOT G_GOP_AUTO_SPLIT  OR G_GOP_AUTO_SPLIT_LINE_ID <> p_x_line_tbl(I).LINE_ID ) --ER 2736896. Do not reserve here.
16191 			  THEN
16192                Reserve_Line
16193                 ( p_line_rec             => p_x_line_tbl(I)
16194                 , p_quantity_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity, 0)
16195                 , p_quantity2_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity2, 0) -- INVCONV
16196                 , x_return_Status        => x_return_status);
16197             END IF;  -- l_reserved_quantity > 0
16198 	    -- 8706868
16199          ELSIF nvl(p_x_line_tbl(I).shipping_interfaced_flag, 'N') = 'Y' AND
16200  	         OE_SCHEDULE_UTIL.oe_split_rsv_tbl.count > 0 THEN
16201 
16202 	    IF  OE_SCHEDULE_UTIL.oe_split_rsv_tbl.EXISTS
16203                          (MOD(p_x_line_tbl(I).line_id,G_BINARY_LIMIT)) AND
16204                  (p_x_line_tbl(I).shipped_quantity is null) AND
16205                  Within_Rsv_Time_Fence(p_x_line_tbl(I).schedule_ship_date,
16206                                  p_x_line_tbl(I).org_id)  AND
16207     	        (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
16208                  sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
16209                  sch_cached_sch_level_code is null) THEN
16210                Reserve_Line
16211                 ( p_line_rec             => p_x_line_tbl(I)
16212                 , p_quantity_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity, 0)
16213                 , p_quantity2_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity2, 0) -- INVCONV
16214                 , x_return_Status        => x_return_status);
16215            END IF;
16216        END IF; --  shipped_quantity is null
16217     --8706868
16218     ELSIF p_x_line_tbl(I).operation = OE_GLOBALS.G_OPR_CREATE AND
16219           p_x_line_tbl(I).schedule_status_code is not null AND
16220 	  nvl(p_x_line_tbl(I).shippable_flag,'N') = 'Y' AND
16221 	  (p_x_line_tbl(I).shipped_quantity is null) AND
16222            Within_Rsv_Time_Fence(p_x_line_tbl(I).schedule_ship_date,
16223                                 p_x_line_tbl(I).org_id)  AND
16224 	   (sch_cached_sch_level_code = SCH_LEVEL_THREE  OR
16225             sch_cached_sch_level_code = SCH_LEVEL_FOUR OR
16226              sch_cached_sch_level_code is null) THEN
16227 
16228 	     l_do_reservation := FALSE;
16229 	     IF (nvl(p_x_line_tbl(I).shipping_interfaced_flag, 'N') = 'N') THEN
16230                 l_do_reservation := TRUE;
16231              ELSIF (nvl(p_x_line_tbl(I).shipping_interfaced_flag, 'N') = 'Y') THEN
16232                 -- Do reservation if parent warehouse is different from child warehouse or
16233 		-- parent line is in table oe_schedule_util.oe_split_rsv_tbl
16234 		IF OE_SCHEDULE_UTIL.oe_split_rsv_tbl.count > 0 THEN
16235 		   IF OE_SCHEDULE_UTIL.oe_split_rsv_tbl.EXISTS
16236                          (MOD(p_x_line_tbl(I).split_from_line_id,G_BINARY_LIMIT)) THEN
16237                       l_do_reservation := TRUE;
16238                    END IF;
16239                 ELSE
16240 		   FOR K in 1..p_x_line_tbl.count LOOP
16241 		      IF p_x_line_tbl(I).split_from_line_id =p_x_line_tbl(K).line_id AND
16242 		         p_x_line_tbl(I).ship_from_org_id <> p_x_line_tbl(K).ship_from_org_id THEN
16243                         l_do_reservation := TRUE;
16244 			EXIT;
16245                       END IF;
16246 		   END LOOP;
16247 		END IF;
16248 	     END IF;
16249 	     --10208311 Check for reservation only if its a USER split
16250 	     IF l_do_reservation AND
16251                 NVL(p_x_line_tbl(I).split_by,'USER') <> 'SYSTEM' THEN
16252                 -- First check it it already has the reservation
16253                 l_query_rsv_rec.reservation_id := fnd_api.g_miss_num;
16254                 l_sales_order_id
16255                        := Get_mtl_sales_order_id(p_x_line_tbl(I).header_id);
16256                 l_query_rsv_rec.demand_source_header_id  := l_sales_order_id;
16257                 l_query_rsv_rec.demand_source_line_id    := p_x_line_tbl(I).line_id;
16258                 l_query_rsv_rec.organization_id := p_x_line_tbl(I).ship_from_org_id;
16259 
16260                 IF l_debug_level  > 0 THEN
16261                    oe_debug_pub.add(  'CALLING INVS QUERY_RESERVATION ' , 1 ) ;
16262                 END IF;
16263 
16264                 inv_reservation_pub.query_reservation
16265                   ( p_api_version_number       => 1.0
16266                   , p_init_msg_lst              => fnd_api.g_true
16267                   , x_return_status             => x_return_status
16268                   , x_msg_count                 => l_msg_count
16269                   , x_msg_data                  => l_msg_data
16270                   , p_query_input               => l_query_rsv_rec
16271                   , x_mtl_reservation_tbl       => l_rsv_tbl
16272                   , x_mtl_reservation_tbl_count => l_count
16273                   , x_error_code                => l_x_error_code
16274                   , p_lock_records              => l_lock_records
16275                   , p_sort_by_req_date          => l_sort_by_req_date
16276                  );
16277                 IF l_debug_level  > 0 THEN
16278                    oe_debug_pub.add(  'AFTER CALLING INVS QUERY_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
16279                 END IF;
16280 
16281                 IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
16282                    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16283                 ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
16284                   RAISE FND_API.G_EXC_ERROR;
16285                 END IF;
16286                 IF l_debug_level  > 0 THEN
16287                   oe_debug_pub.add(  'RESERVATION RECORD COUNT IS: ' || L_RSV_TBL.COUNT , 1 ) ;
16288                 END IF;
16289                 IF l_rsv_tbl.count = 0 THEN
16290                    Reserve_Line
16291                    ( p_line_rec             => p_x_line_tbl(I)
16292                    , p_quantity_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity, 0)
16293                    , p_quantity2_to_reserve  => nvl(p_x_line_tbl(I).ordered_quantity2, 0) -- INVCONV
16294                    , x_return_Status        => x_return_status);
16295                END IF;
16296           END IF;
16297       END IF; -- If operation on the line was UPDATE
16298 
16299     END LOOP; -- Main Loop.
16300   END IF; -- 8706868
16301   G_OVERRIDE_FLAG     := 'N';
16302   IF l_debug_level  > 0 THEN
16303       oe_debug_pub.add(  'SCHEDULING RESULTS OF THE LINES: ' , 1 ) ;
16304   END IF;
16305 
16306 
16307   FOR I IN 1..p_x_line_tbl.count LOOP
16308       IF l_debug_level  > 0 THEN
16309          oe_debug_pub.add(  'LINE ID : ' || P_X_LINE_TBL ( I ) .LINE_ID , 1 ) ;
16310          oe_debug_pub.add(  'SCHEDULE STATUS : ' || P_X_LINE_TBL ( I ) .SCHEDULE_STATUS_CODE , 1 ) ;
16311       END IF;
16312   END LOOP;
16313   IF l_debug_level  > 0 THEN
16314       oe_debug_pub.add(  'EXITING SPLIT_SCHEDULING WITH ' || X_RETURN_STATUS , 1 ) ;
16315   END IF;
16316 
16317 EXCEPTION
16318     WHEN OTHERS THEN
16319         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16320 
16321         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
16322         THEN
16323             OE_MSG_PUB.Add_Exc_Msg
16324             (   G_PKG_NAME
16325             ,   'Split_Scheduling'
16326             );
16327         END IF;
16328         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16329 END SPLIT_SCHEDULING;
16330 
16331 /*------------------------------------------------------------
16332 Procedure Schedule_Split_Lines
16333 p_param1    => p_x_line_tbl(I).schedule_status_code,
16334 p_param2    => p_x_line_tbl(I).arrival_set_id,
16335 p_param3    => p_x_line_tbl(I).ship_set_id,
16336 p_param4    => p_x_line_tbl(I).ship_model_complete_flag,
16337 p_param5    => p_x_line_tbl(I).model_remnant_flag,
16338 p_param6    => p_x_line_tbl(I).top_model_line_id,
16339 p_param7    => p_x_line_tbl(I).ato_line_id,
16340 p_param8    => p_x_line_tbl(I).item_type_code,
16341 p_param9    => p_x_line_tbl(I).source_type_code,
16342 
16343 This procedure will be called from OEXVREQB.pls.This procedure will process
16344 the scheduling for the system split records.
16345 
16346 OM will log delayed request named split_schedule from post_line_process
16347 of oe_line_util to fix the bug 2913742.
16348 
16349 From now on split scheduling will not be used for system splits. This API will
16350 take care of scheduling the system split records.
16351 
16352 
16353 -------------------------------------------------------------*/
16354 Procedure Schedule_Split_Lines
16355 ( p_sch_set_tbl     IN  OE_ORDER_PUB.request_tbl_type
16356 , x_return_status   OUT NOCOPY VARCHAR2)
16357 IS
16358 l_line_tbl           OE_ORDER_PUB.line_Tbl_Type;
16359 l_line_rec           OE_ORDER_PUB.Line_Rec_Type;
16360 BEGIN
16361 
16362   oe_debug_pub.add('Entering Schedule_Split_Lines',1);
16363 
16364   x_return_status := FND_API.G_RET_STS_SUCCESS;
16365 
16366   FOR I in 1..p_sch_set_tbl.count LOOP
16367 
16368    IF p_sch_set_tbl(I).param2 IS NOT NULL
16369    OR p_sch_set_tbl(I).param3 IS NOT NULL
16370    OR (p_sch_set_tbl(I).param4 = 'Y'
16371    AND nvl(p_sch_set_tbl(I).param5,'N') = 'N')
16372    OR (p_sch_set_tbl(I).param7 IS NOT NULL
16373    AND p_sch_set_tbl(I).param8 <> 'STANDARD'
16374    AND nvl(p_sch_set_tbl(I).param5,'N') = 'N'
16375    AND p_sch_set_tbl(I).param9 = OE_GLOBALS.G_SOURCE_INTERNAL  --added for bug 12757660
16376                            ) THEN -- 4421848: added remnant flag check
16377 
16378    oe_debug_pub.add('Belongs to Set',2);
16379    l_line_tbl(I).line_id                  := p_sch_set_tbl(I).entity_id;
16380    l_line_tbl(I).schedule_status_code     := p_sch_set_tbl(I).param1;
16381    l_line_tbl(I).arrival_set_id           := p_sch_set_tbl(I).param2;
16382    l_line_tbl(I).ship_set_id              := p_sch_set_tbl(I).param3;
16383    l_line_tbl(I).ship_model_complete_flag := p_sch_set_tbl(I).param4;
16384    l_line_tbl(I).model_remnant_flag       := p_sch_set_tbl(I).param5;
16385    l_line_tbl(I).top_model_line_id        := p_sch_set_tbl(I).param6;
16386    l_line_tbl(I).ato_line_id              := p_sch_set_tbl(I).param7;
16387    l_line_tbl(I).item_type_code           := p_sch_set_tbl(I).param8;
16388 
16389    ELSE
16390     -- Standard or independent line.
16391    oe_debug_pub.add('Independent or remnant line',2);
16392 
16393     OE_Line_Util.Query_Row(p_line_id  => p_sch_set_tbl(I).entity_id,
16394                            x_line_rec => l_line_rec);
16395 
16396     l_line_tbl(I) := l_line_rec;
16397 
16398 
16399    END IF;
16400   END LOOP;
16401 
16402   G_OVERRIDE_FLAG     := 'Y';
16403 
16404   oe_debug_pub.add(  'COUNT IS :' || l_line_tbl.COUNT , 1 ) ;
16405 
16406   process_split(p_x_line_tbl => l_line_tbl,
16407                 x_return_status => x_return_status);
16408 
16409   G_OVERRIDE_FLAG     := 'N';
16410 EXCEPTION
16411     WHEN OTHERS THEN
16412         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16413 
16414         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
16415         THEN
16416             OE_MSG_PUB.Add_Exc_Msg
16417             (   G_PKG_NAME
16418             ,   'Schedule_Split_Lines'
16419             );
16420         END IF;
16421         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16422 End Schedule_Split_Lines;
16423 
16424 /*--------------------------------------------------------------------------
16425 Procedure Name : Update_Results_from_backlog_wb
16426 Description    : This procedure is called from the backlog's scheduler's
16427                  workbenck and the Supply Chain ATP form, after the user
16428                  has performed some scheduling in their form. They call
16429                  this API to update the results of scheduling on the order
16430                  lines table.
16431                  For the purpose of this call, we have created a new table type
16432                  mrp_line_tbl_type, which is table of mrp_line_rec_type.
16433                  This record is created with only those fields whose values
16434                  we can get back from MRP's form. We take the field values
16435                  from this record and update the lines information in
16436                  oe_order_lines table.
16437 -------------------------------------------------------------------------- */
16438 Procedure Update_Results_from_backlog_wb
16439 ( p_mrp_line_tbl  IN  mrp_line_tbl_type
16440 , x_msg_count OUT NOCOPY NUMBER
16441 
16442 , x_msg_data OUT NOCOPY VARCHAR2
16443 
16444 , x_return_status OUT NOCOPY VARCHAR2)
16445 
16446 IS
16447 /*
16448 l_line_rec                  OE_ORDER_PUB.line_rec_type;
16449 I                           NUMBER;
16450 l_return_status             VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
16451 l_msg_count                 NUMBER := 0;
16452 l_msg_data                  VARCHAR2(2000) := null;
16453 l_control_rec               OE_GLOBALS.control_rec_type;
16454 l_line_tbl                  OE_ORDER_PUB.line_tbl_type;
16455 l_old_line_tbl              OE_ORDER_PUB.line_tbl_type;
16456 l_file_val                  VARCHAR2(80);
16457 */
16458 --
16459 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
16460 --
16461 BEGIN
16462    -- (4735196) This api is obsoleted.
16463    --Use OE_SCHEDULE_GRP.Update_Scheduling_Results instead.
16464     IF l_debug_level  > 0 THEN
16465         oe_debug_pub.add(  'This API is not supported ' , 1 ) ;
16466     END IF;
16467    /*
16468      fnd_profile.put('OE_DEBUG_LOG_DIRECTORY','/sqlcom/outbound/dom1151');
16469 
16470     IF l_debug_level  > 0 THEN
16471         oe_debug_pub.add(  'ENTERING UPDATE_RESULTS_FROM_BACKLOG_WB' , 1 ) ;
16472     END IF;
16473     IF l_debug_level  > 0 THEN
16474         oe_debug_pub.add(  'COUNT IS: ' || P_MRP_LINE_TBL.COUNT , 1 ) ;
16475     END IF;
16476     --  Set control flags.
16477 
16478     l_control_rec.controlled_operation := TRUE;
16479     l_control_rec.change_attributes    := TRUE;
16480 
16481     l_control_rec.default_attributes   := TRUE;
16482     l_control_rec.check_security       := TRUE;
16483 
16484     l_control_rec.write_to_DB          := TRUE;
16485     l_control_rec.validate_entity      := TRUE;
16486 
16487     l_control_rec.process              := TRUE;
16488 
16489     --  Instruct API to retain its caches
16490 
16491     l_control_rec.clear_api_cache      := FALSE;
16492     l_control_rec.clear_api_requests   := FALSE;
16493 
16494     FOR I in 1..p_mrp_line_tbl.count LOOP
16495 
16496   IF l_debug_level  > 0 THEN
16497       oe_debug_pub.add(  'RR: LINE ID ' || P_MRP_LINE_TBL ( I ) .LINE_ID , 1 ) ;
16498   END IF;
16499   IF l_debug_level  > 0 THEN
16500       oe_debug_pub.add(  'RR: SCHEDULE_SHIP_DATE ' || P_MRP_LINE_TBL ( I ) .SCHEDULE_SHIP_DATE , 1 ) ;
16501   END IF;
16502 
16503         l_line_rec        := OE_Order_Pub.G_MISS_LINE_REC;
16504         l_line_rec.line_id               := p_mrp_line_tbl(I).line_id;
16505         l_line_rec.schedule_ship_date    :=
16506                                   p_mrp_line_tbl(I).schedule_ship_date;
16507         l_line_rec.schedule_arrival_date :=
16508                                   p_mrp_line_tbl(I).schedule_arrival_date;
16509         l_line_rec.ship_from_org_id      :=
16510                                   p_mrp_line_tbl(I).ship_from_org_id;
16511         l_line_rec.shipping_method_code  :=
16512                                   p_mrp_line_tbl(I).ship_method_code;
16513         l_line_rec.schedule_status_code    :=
16514                                   OESCH_STATUS_SCHEDULED;
16515   -- Start Audit Trail
16516   l_line_rec.change_reason := 'SYSTEM';
16517   --  l_line_rec.change_comments := 'Scheduling Action';
16518   -- End Audit Trail
16519         l_line_rec.visible_demand_flag   := 'Y';
16520         l_line_rec.operation             := OE_GLOBALS.G_OPR_UPDATE;
16521         l_line_tbl(I)                    := l_line_rec;
16522     END LOOP;
16523 
16524 
16525     IF l_debug_level  > 0 THEN
16526         oe_debug_pub.add(  'LINE COUNT IS: ' || L_LINE_TBL.COUNT , 1 ) ;
16527     END IF;
16528     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
16529     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'N';
16530 
16531     IF l_debug_level  > 0 THEN
16532         oe_debug_pub.add(  'BKL: CALLING PROCESS ORDER' , 1 ) ;
16533     END IF;
16534 
16535 
16536     --  Call OE_Order_PVT.Process_order
16537 
16538     OE_Order_PVT.Lines
16539     (p_validation_level    => FND_API.G_VALID_LEVEL_NONE,
16540      p_control_rec         => l_control_rec,
16541      p_x_line_tbl          => l_line_tbl,
16542      p_x_old_line_tbl      => l_old_line_tbl,
16543      x_return_status       => l_return_status);
16544 
16545     IF l_debug_level  > 0 THEN
16546         oe_debug_pub.add(  'BKL: AFTER CALLING PROCESS ORDER' , 1 ) ;
16547     END IF;
16548     IF l_debug_level  > 0 THEN
16549         oe_debug_pub.add(  'L_RETURN_STATUS IS ' || L_RETURN_STATUS , 1 ) ;
16550     END IF;
16551 
16552     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
16553         IF l_debug_level  > 0 THEN
16554             oe_debug_pub.add(  'RR: UNEXP ERRORED OUT' , 1 ) ;
16555         END IF;
16556         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16557     ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
16558         IF l_debug_level  > 0 THEN
16559             oe_debug_pub.add(  'RR: ERRORED OUT' , 1 ) ;
16560         END IF;
16561         RAISE FND_API.G_EXC_ERROR;
16562     END IF;
16563 
16564 -- code fix for 3502139
16565 -- call Process_Requests_And_Notify to execute the delayed requests
16566     OE_ORDER_PVT.Process_Requests_And_Notify
16567     (
16568     x_return_status => l_return_status
16569     );
16570 -- code fix for 3502139
16571 
16572 -- Added the following code to fix the bug 3105070
16573 
16574     FOR I in 1..l_line_tbl.count LOOP
16575       IF l_line_tbl(I).source_document_type_id = 10        AND
16576          NOT OE_GLOBALS.EQUAL(l_line_tbl(I).schedule_arrival_date,
16577                           l_old_line_tbl(I).schedule_arrival_date)
16578 
16579       THEN
16580         oe_debug_pub.add(  'PASSING SCHEDULE_ARRIVAL_DATE TO PO ' , 3 ) ;
16581          Update_PO(l_line_tbl(I).schedule_arrival_date,
16582                    l_line_tbl(I).source_document_id,
16583                    l_line_tbl(I).source_document_line_id);
16584         oe_debug_pub.add(  'AFTER PO CALL BACK' , 3 ) ;
16585       END IF;
16586    END LOOP ;
16587 
16588  -- End of the code added to fix the bug 3105070
16589 
16590     OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16591     OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16592 
16593     x_return_status := l_return_status;
16594     x_msg_count     := l_msg_count;
16595     x_msg_data      := l_msg_data;
16596 
16597     IF l_debug_level  > 0 THEN
16598         oe_debug_pub.add(  'EXITING UPDATE_RESULTS_FROM_BACKLOG_WB' , 1 ) ;
16599     END IF;
16600     OE_DEBUG_PUB.Debug_Off;
16601  */
16602    NULL;
16603    x_msg_data := 'This API is not supported';
16604    RAISE FND_API.G_EXC_ERROR;
16605 EXCEPTION
16606 
16607    WHEN FND_API.G_EXC_ERROR THEN
16608 
16609         x_return_status := FND_API.G_RET_STS_ERROR;
16610         /*
16611         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16612         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16613         */
16614     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
16615 
16616         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16617         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16618         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16619 
16620     WHEN OTHERS THEN
16621 
16622         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
16623 
16624         OE_SCHEDULE_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16625         OE_ORDER_SCH_UTIL.OESCH_PERFORM_SCHEDULING := 'Y';
16626         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
16627         THEN
16628             OE_MSG_PUB.Add_Exc_Msg
16629             (   G_PKG_NAME
16630             ,   'Schedule_line'
16631             );
16632         END IF;
16633 END Update_Results_from_backlog_wb;
16634 /*-----------------------------------------------------+
16635  | Name        :   Post_Forms_Commit                   |
16636  | Parameters  :                                       |
16637  |                                                     |
16638  | Description :   This Procedure is called from       |
16639  |                 OEOXOEFRM.pld POST_FORMS_COMMIT     |
16640  |                 This Procedure was added for        |
16641  |                 Bug: 2097933.                       |
16642  |                 With this procedure we check if     |
16643  |                 there is sufficient Qty for         |
16644  |                 Reservation just before we are      |
16645  |                 Committing the line.                |
16646  |         If there is no sufficient Qty for   |
16647  |         reservation then the Inventory      |
16648  |         populates a pl-sql table. Before    |
16649  |                 commit we check if the pl-sql table |
16650  |         is NOT Null or not.             |
16651  +-----------------------------------------------------*/
16652 
16653 Procedure Post_Forms_Commit
16654 (x_return_status OUT NOCOPY VARCHAR2
16655 
16656 ,x_msg_count OUT NOCOPY NUMBER
16657 
16658 ,x_msg_data OUT NOCOPY VARCHAR2) IS
16659 
16660 l_return_status VARCHAR2(100);
16661 l_msg_count     NUMBER;
16662 l_msg_data      VARCHAR2(500);
16663 l_failed_rsv_temp_tbl INV_RESERVATION_GLOBAL.mtl_failed_rsv_tbl_type;
16664 --
16665 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
16666 --
16667 Begin
16668 
16669   IF l_debug_level  > 0 THEN
16670       oe_debug_pub.add(  '*** INSIDE THE POST_FORMS_COMMIT ***' , 1 ) ;
16671   END IF;
16672 
16673   x_return_status := FND_API.G_RET_STS_SUCCESS;
16674 
16675   -- Check for Performed Reservation Start
16676   IF OESCH_PERFORMED_RESERVATION = 'Y' THEN
16677 
16678     IF l_debug_level  > 0 THEN
16679         oe_debug_pub.add(  ' BEFORE CALLING THE INV FOR DO_CHECK_FOR_COMMIT' , 1 ) ;
16680     END IF;
16681     INV_RESERVATION_PVT.Do_Check_For_Commit
16682         (p_api_version_number  => 1.0
16683         ,p_init_msg_lst        => FND_API.G_FALSE
16684         ,x_return_status       => l_return_status
16685         ,x_msg_count           => l_msg_count
16686         ,x_msg_data            => l_msg_data
16687         ,x_failed_rsv_temp_tbl => l_failed_rsv_temp_tbl);
16688 
16689                                            IF l_debug_level  > 0 THEN
16690                                                oe_debug_pub.add(  'AFTER CALLING THE INV FOR DO_CHECK_FOR_COMMIT : ' || L_RETURN_STATUS , 1 ) ;
16691                                            END IF;
16692 
16693   -- We need to find out if the Reservation has failed
16694     IF l_failed_rsv_temp_tbl.count > 0 THEN
16695       IF l_debug_level  > 0 THEN
16696           oe_debug_pub.add(  ' THE RESERVATION PROCESS HAS FAILED ' , 1 ) ;
16697       END IF;
16698       FND_MESSAGE.SET_NAME('ONT','OE_SCH_RSV_FAILURE');
16699       OE_MSG_PUB.Add;
16700     END IF;
16701 
16702     -- Error Handling Start
16703     IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
16704       IF l_debug_level  > 0 THEN
16705           oe_debug_pub.add(  'INSIDE UNEXPECTED ERROR' , 1 ) ;
16706       END IF;
16707       OE_MSG_PUB.Transfer_Msg_Stack;
16708       l_msg_count   := OE_MSG_PUB.COUNT_MSG;
16709 
16710       FOR I IN 1..l_msg_count LOOP
16711         l_msg_data :=  OE_MSG_PUB.Get(I,'F');
16712 
16713         IF l_debug_level  > 0 THEN
16714             oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
16715         END IF;
16716       END LOOP;
16717       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16718 
16719     ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
16720       IF l_debug_level  > 0 THEN
16721           oe_debug_pub.add(  ' INSIDE EXPECTED ERROR' , 1 ) ;
16722       END IF;
16723       OE_MSG_PUB.Transfer_Msg_Stack;
16724       l_msg_count   := OE_MSG_PUB.COUNT_MSG;
16725 
16726       FOR I IN 1..l_msg_count LOOP
16727         l_msg_data :=  OE_MSG_PUB.Get(I,'F');
16728         IF l_debug_level  > 0 THEN
16729             oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
16730         END IF;
16731       END LOOP;
16732       RAISE FND_API.G_EXC_ERROR;
16733 
16734     END IF;
16735   --Error Handling End
16736 
16737     OESCH_PERFORMED_RESERVATION := 'N';
16738 
16739   -- Check for Performed Reservation End
16740   END IF;
16741 
16742     --  Get message count and data
16743 
16744     oe_msg_pub.count_and_get
16745     (   p_count                       => x_msg_count
16746     ,   p_data                        => x_msg_data
16747     );
16748 
16749 
16750   IF l_debug_level  > 0 THEN
16751       oe_debug_pub.add(  '*** BEFORE EXITING POST_FORMS_COMMIT ***' , 1 ) ;
16752   END IF;
16753 
16754 EXCEPTION
16755   WHEN FND_API.G_EXC_ERROR THEN
16756  --   OESCH_PERFORMED_RESERVATION := 'N';
16757 
16758     x_return_status := FND_API.G_RET_STS_ERROR;
16759 
16760     --  Get message count and data
16761 
16762     oe_msg_pub.count_and_get
16763      (   p_count                       => x_msg_count
16764      ,   p_data                        => x_msg_data);
16765 
16766   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
16767 --    OESCH_PERFORMED_RESERVATION := 'N';
16768     IF OE_MSG_PUB.Check_Msg_Level
16769         (OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
16770     THEN
16771       OE_MSG_PUB.Add_Exc_Msg
16772         (G_PKG_NAME , 'Post_Forms_Commit');
16773     END IF;
16774 
16775     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
16776 
16777     --  Get message count and data
16778 
16779     oe_msg_pub.count_and_get
16780     (   p_count                       => x_msg_count
16781     ,   p_data                        => x_msg_data);
16782 
16783 
16784 END Post_Forms_Commit;
16785 
16786 -- added by fabdi 03/May/2001 - For process ATP
16787 /*--------------------------------------------------------------------------
16788 Procedure Name : get_process_query_quantities
16789 Description : This precedure works out nocopy the on_hand_qty and avail_to_reserve
16790 
16791 quanties to display in the same ATP window for process inventory only. The procedure
16792 takes into account grade controlled items and displays inventory result for a particular
16793 grade as well a total sum if grade is null.
16794 This procedure is called from Query_Qty_Tree only
16795 --------------------------------------------------------------------------
16796 -- INVCONV  - not used now     cos of OPM inventory convergence
16797 PROCEDURE get_process_query_quantities
16798   (   p_org_id       IN  NUMBER
16799    ,  p_item_id      IN  NUMBER
16800    ,  p_line_id      IN  NUMBER
16801 , x_on_hand_qty OUT NOCOPY NUMBER
16802 
16803 , x_avail_to_reserve OUT NOCOPY NUMBER
16804 
16805   ) IS
16806 
16807  l_commitedsales_qty     NUMBER;
16808  l_commitedprod_qty     NUMBER;
16809  l_onhand_order_qty     NUMBER;
16810  l_grade_ctl    NUMBER;
16811  l_grade    VARCHAR2(4);
16812 
16813  -- main cursor (with total sum)
16814  CURSOR c_onhand_qty(p_organisation_id number, p_itemid number) IS
16815  SELECT sum(s.onhand_order_qty), sum(s.COMMITTEDSALES_QTY), sum(s.COMMITTEDPROD_QTY)
16816  FROM ic_summ_inv s
16817  WHERE s.item_id = p_itemid AND
16818  s.whse_code = (Select wh.whse_code
16819                 from ic_whse_mst wh
16820                 where wh.MTL_ORGANIZATION_ID = p_organisation_id);
16821 
16822  -- cursor (with single grade sum)
16823  CURSOR c_onhand_qty2(p_organisation_id number, p_itemid number) IS
16824  SELECT s.onhand_order_qty, s.COMMITTEDSALES_QTY, s.COMMITTEDPROD_QTY
16825  FROM ic_summ_inv s
16826  WHERE s.item_id = p_itemid AND
16827  s.whse_code = (Select wh.whse_code
16828                 from ic_whse_mst wh
16829                 where wh.MTL_ORGANIZATION_ID = p_organisation_id) AND
16830  s.qc_grade =  (SELECT preferred_grade
16831                FROM oe_order_lines
16832                WHERE line_id = p_line_id);
16833 
16834 
16835  -- Grade ctl cursor
16836  CURSOR c_grade_ctl(p_item_id number) IS
16837  SELECT grade_ctl
16838  FROM ic_item_mst
16839  WHERE item_id = p_item_id;
16840 
16841  Cursor c_get_grade (p_line_id number) IS
16842  SELECT preferred_grade
16843  FROM oe_order_lines
16844  WHERE line_id = p_line_id;
16845 
16846   --
16847   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
16848   --
16849 BEGIN
16850 
16851     IF l_debug_level  > 0 THEN
16852         oe_debug_pub.add(  'INSIDE GET_PROCESS_QUERY_QUANTITIES ' ) ;
16853     END IF;
16854     IF l_debug_level  > 0 THEN
16855         oe_debug_pub.add(  'P_LINE_ID - IN GET_PROCESS_QUERY_QUANTITIES IS: '|| P_LINE_ID ) ;
16856     END IF;
16857 
16858     OPEN c_grade_ctl(p_item_id);
16859     OPEN c_get_grade(p_line_id);
16860     FETCH c_grade_ctl into l_grade_ctl;
16861     FETCH c_get_grade into l_grade;
16862     IF l_debug_level  > 0 THEN
16863         oe_debug_pub.add(  'GRADE OF ITEM IS '|| L_GRADE ) ;
16864     END IF;
16865 
16866      IF (l_grade_ctl > 0 AND l_grade is NOT NULL) THEN
16867         -- Grade ctl item
16868         OPEN c_onhand_qty2(p_org_id, p_item_id);
16869         FETCH c_onhand_qty2 into l_onhand_order_qty, l_commitedsales_qty, l_commitedprod_qty;
16870         if c_onhand_qty2%NOTFOUND then
16871                 CLOSE c_onhand_qty2;
16872                 IF l_debug_level  > 0 THEN
16873                     oe_debug_pub.add(  ' ( 2 ) NO DATA FOUND FOR ITEM '|| P_ITEM_ID || ' ORG_ID '|| P_ORG_ID ) ;
16874                 END IF;
16875                 x_on_hand_qty := 0;
16876                 x_avail_to_reserve := 0;
16877                 IF l_debug_level  > 0 THEN
16878                     oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
16879                 END IF;
16880                 IF l_debug_level  > 0 THEN
16881                     oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
16882                 END IF;
16883                 return;
16884         end if;
16885         CLOSE c_onhand_qty2;
16886 
16887      ELSE
16888         -- non grade ctl item
16889         OPEN c_onhand_qty(p_org_id, p_item_id);
16890         FETCH c_onhand_qty into l_onhand_order_qty, l_commitedsales_qty, l_commitedprod_qty;
16891         if c_onhand_qty%NOTFOUND then
16892                 CLOSE c_onhand_qty;
16893                 IF l_debug_level  > 0 THEN
16894                     oe_debug_pub.add(  ' ( 1 ) NO DATA FOUND FOR ITEM '|| P_ITEM_ID || ' ORG_ID '|| P_ORG_ID ) ;
16895                 END IF;
16896                 x_on_hand_qty := 0;
16897                 x_avail_to_reserve := 0;
16898                 IF l_debug_level  > 0 THEN
16899                     oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
16900                 END IF;
16901                 IF l_debug_level  > 0 THEN
16902                     oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
16903                 END IF;
16904                 return;
16905         end if;
16906         CLOSE c_onhand_qty;
16907      END IF;
16908    CLOSE c_grade_ctl;
16909    CLOSE c_get_grade;
16910 
16911    -- Quantity Calculations
16912    x_on_hand_qty := l_onhand_order_qty;
16913    x_avail_to_reserve :=  l_onhand_order_qty - (l_commitedsales_qty + l_commitedprod_qty);
16914 
16915    IF l_debug_level  > 0 THEN
16916        oe_debug_pub.add(  'POCESS X_ON_HAND_QTY IS: '|| X_ON_HAND_QTY ) ;
16917    END IF;
16918    IF l_debug_level  > 0 THEN
16919        oe_debug_pub.add(  'PROCESS X_AVAIL_TO_RESERVE IS: '|| X_AVAIL_TO_RESERVE ) ;
16920    END IF;
16921 END get_process_query_quantities; */
16922 -- end fabdi
16923 -- Start  2595661
16924 /*
16925    FUNCTION Name - GET_PICK_STATUS
16926    Description - To get the pick status of a particular line
16927                 This will return true if a single or more line with released_status in ('S','Y','C')
16928 */
16929 FUNCTION Get_Pick_Status (p_line_id IN NUMBER) RETURN BOOLEAN
16930 IS
16931   l_Pick_Status   VARCHAR2(1);
16932 --
16933 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
16934 --
16935 BEGIN
16936    SELECT  '1'
16937    INTO  l_Pick_status
16938    FROM  WSH_DELIVERY_DETAILS
16939    WHERE  SOURCE_CODE = 'OE'
16940    AND  SOURCE_LINE_ID = p_line_id
16941    AND  RELEASED_STATUS IN ('S','Y','C');
16942 
16943    RETURN (TRUE);
16944 
16945 EXCEPTION
16946    WHEN NO_DATA_FOUND THEN
16947        RETURN (FALSE);
16948    WHEN TOO_MANY_ROWS THEN
16949        RETURN (TRUE);
16950    WHEN OTHERS THEN
16951       IF l_debug_level  > 0 THEN
16952           oe_debug_pub.ADD('Error message in Get_Pick_Status : '||substr(sqlerrm,1,100),1);
16953 
16954       END IF;
16955       RETURN (FALSE);
16956 END Get_Pick_Status;
16957 
16958 PROCEDURE Do_Unreserve (p_line_rec             IN  OE_ORDER_PUB.Line_Rec_Type
16959                        ,p_quantity_to_unreserve IN  NUMBER
16960                        ,p_quantity2_to_unreserve IN  NUMBER -- INVCONV
16961                        ,p_old_ship_from_org_id   IN  NUMBER DEFAULT NULL -- 5024936
16962                        ,x_return_status         OUT NOCOPY VARCHAR2)
16963 IS
16964   l_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
16965   l_rsv_new_rec           inv_reservation_global.mtl_reservation_rec_type;
16966   l_msg_count             NUMBER;
16967   l_msg_data              VARCHAR2(240);
16968   l_rsv_id                NUMBER;
16969   l_rsv_tbl               inv_reservation_global.mtl_reservation_tbl_type;
16970   l_count                 NUMBER;
16971   l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
16972   l_qty_to_unreserve      NUMBER;
16973   l_qty2_to_unreserve     NUMBER := p_quantity2_to_unreserve;  --ER#14763609
16974   l_source_code           VARCHAR2(40) := FND_PROFILE.VALUE('ONT_SOURCE_CODE');
16975   l_sales_order_id        NUMBER;
16976   l_x_error_code          NUMBER;
16977   l_lock_records          VARCHAR2(1);
16978   l_sort_by_req_date      NUMBER ;
16979   l_buffer                VARCHAR2(2000);
16980 
16981 --
16982 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
16983 --
16984 BEGIN
16985 
16986   IF l_debug_level  > 0 THEN
16987       oe_debug_pub.add(  'ENTERING DO_UNRESERVE' , 3 ) ;
16988       oe_debug_pub.add(  'QUANTITY TO UNRESERVE :' || P_QUANTITY_TO_UNRESERVE , 3 ) ;
16989       oe_debug_pub.add(  'QUANTITY2 TO UNRESERVE :' || P_QUANTITY2_TO_UNRESERVE , 3 ) ;--ER#14763609
16990       oe_debug_pub.add(  'fulfillment_Base :' || p_line_rec.fulfillment_base , 3 ) ;--ER#14763609
16991   END IF;
16992 
16993   -- If the quantity to unreserve is passed and null or missing, we do not
16994   -- need to go throug this procedure.
16995 
16996   -- 2746991
16997   --IF p_quantity_to_unreserve is null OR
16998  IF ( Nvl(p_line_rec.fulfillment_base, 'P') ='P') THEN     --ER#14763609/bug16434168(issue 4)
16999 
17000   IF NVL(p_quantity_to_unreserve,0) = 0 OR
17001      p_quantity_to_unreserve = FND_API.G_MISS_NUM THEN
17002      goto end_of_loop;
17003   END IF;
17004 
17005  ELSE --bug 16434168/ER#14763609
17006 
17007   IF  (NVL(p_quantity2_to_unreserve,0) = 0) OR
17008        p_quantity2_to_unreserve = FND_API.G_MISS_NUM THEN
17009      goto end_of_loop;
17010   END IF;
17011 
17012  END IF;
17013 
17014   IF p_line_rec.source_document_type_id = 10 THEN
17015 
17016      -- This is an internal order line. We need to give
17017      -- a different demand source type for these lines.
17018 
17019      l_rsv_rec.demand_source_type_id        :=
17020           INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_INTERNAL_ORD;
17021           -- intenal order
17022   ELSE
17023      l_rsv_rec.demand_source_type_id        :=
17024           INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_OE;
17025           -- order entry
17026   END IF;
17027 
17028 
17029   -- Get demand_source_header_id from mtl_sales_orders
17030 
17031   l_sales_order_id := Get_mtl_sales_order_id(p_line_rec.HEADER_ID);
17032 
17033   IF l_debug_level  > 0 THEN
17034       oe_debug_pub.add(  'L_SALES_ORDER_ID' || L_SALES_ORDER_ID , 3 ) ;
17035   END IF;
17036 
17037   l_rsv_rec.demand_source_header_id  := l_sales_order_id;
17038   l_rsv_rec.demand_source_line_id    := p_line_rec.line_id;
17039   -- 02-jun-2000 mpetrosi added org_id to query_reservation start
17040   --5024936
17041   IF p_old_ship_from_org_id IS NOT NULL THEN
17042      l_rsv_rec.organization_id := p_old_ship_from_org_id;
17043   ELSE
17044      l_rsv_rec.organization_id := p_line_rec.ship_from_org_id;
17045   END IF;
17046   IF l_debug_level  > 0 THEN
17047       oe_debug_pub.add(  'SHIP FROM ORG ' || l_rsv_rec.organization_id , 3 ) ;
17048   END IF;
17049 
17050   inv_reservation_pub.query_reservation
17051   ( p_api_version_number        => 1.0
17052   , p_init_msg_lst              => fnd_api.g_true
17053   , p_query_input               => l_rsv_rec
17054   , p_cancel_order_mode         => INV_RESERVATION_GLOBAL.G_CANCEL_ORDER_YES
17055   , p_lock_records              => l_lock_records
17056   , p_sort_by_req_date          => l_sort_by_req_date
17057   , x_mtl_reservation_tbl       => l_rsv_tbl
17058   , x_mtl_reservation_tbl_count => l_count
17059   , x_error_code                => l_x_error_code
17060   , x_return_status             => x_return_status
17061   , x_msg_count                 => l_msg_count
17062   , x_msg_data                  => l_msg_data);
17063 
17064   IF l_debug_level  > 0 THEN
17065       oe_debug_pub.add(  '3. AFTER QUERY RESERVATION'||X_RETURN_STATUS , 1 ) ;
17066   END IF;
17067   IF l_debug_level  > 0 THEN
17068       oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
17069   END IF;
17070   IF l_rsv_tbl.count > 0 THEN --Bug 8644811 : Proceed only if there is any record in the table
17071      -- Start 2346233
17072      -- l_qty_to_unreserve      := p_quantity_to_unreserve;
17073      IF NOT OE_GLOBALS.Equal(p_line_rec.order_quantity_uom,l_rsv_tbl(1).primary_uom_code ) THEN
17074         l_qty_to_unreserve :=  INV_CONVERT.INV_UM_CONVERT( item_id       => p_line_rec.inventory_item_id,
17075                                                         precision     => 5,
17076                                                         from_quantity =>p_quantity_to_unreserve,
17077                                                         from_unit     =>p_line_rec.order_quantity_uom,
17078                                                         to_unit       =>l_rsv_tbl(1).primary_uom_code,
17079                                                         from_name     =>NULL,
17080                                                         to_name       =>NULL
17081                                                       );
17082      ELSE
17083         -- Bug 6335352
17084         /* Added below condition specifically for SET processing and when UOM is changed to Primary UOM
17085         Sets are processed using Delayed Requests. By the time delayed request executes, the ordered quantity in line
17086         gets updated, but reservation is still for the original UOM (non-primary). So unreservation in this case
17087         will be done using primary reservation quantity
17088         */
17089         IF l_rsv_tbl(1).primary_reservation_quantity > p_quantity_to_unreserve
17090           AND NOT oe_globals.equal(p_line_rec.order_quantity_uom,l_rsv_tbl(1).reservation_uom_code)
17091         THEN
17092            l_qty_to_unreserve      := l_rsv_tbl(1).primary_reservation_quantity ;
17093         ELSE
17094            l_qty_to_unreserve      := p_quantity_to_unreserve;
17095         END IF;
17096      END IF;
17097      IF l_debug_level  > 0 THEN
17098        oe_debug_pub.add(  'QUANTITY TO UNRESERVE :' || l_QTY_TO_UNRESERVE , 3 ) ;
17099      END IF;
17100      -- End 2346233
17101 
17102 
17103      FOR I IN 1..l_rsv_tbl.COUNT LOOP
17104 
17105         l_rsv_rec := l_rsv_tbl(I);
17106         IF l_debug_level  > 0 THEN
17107            oe_debug_pub.add(  'RESERVED QTY : ' || L_RSV_REC.RESERVATION_QUANTITY , 1 ) ;
17108            oe_debug_pub.add(  'RESERVED QTY2 : ' || L_RSV_REC.SECONDARY_RESERVATION_QUANTITY , 1 ) ;--ER#14763609
17109            oe_debug_pub.add(  'QTY TO UNRESERVE: ' || L_QTY_TO_UNRESERVE , 1 ) ;
17110            oe_debug_pub.add(  'QTY2 TO UNRESERVE: ' || l_qty2_to_unreserve , 1 ) ; --ER#14763609
17111         END IF;
17112 
17113         --Start 2346233
17114        /*
17115        IF (l_rsv_rec.reservation_quantity <= l_qty_to_unreserve)
17116        THEN
17117        */
17118 --       IF (l_rsv_rec.primary_reservation_quantity <= l_qty_to_unreserve) THEN -- End 2346233
17119 -- If condition modified for ER#14763609
17120          IF ((l_rsv_rec.primary_reservation_quantity <= l_qty_to_unreserve) AND ( Nvl(p_line_rec.fulfillment_base, 'P') ='P'))
17121 	     OR ((l_rsv_rec.secondary_reservation_quantity <= l_qty2_to_unreserve) AND ( Nvl(p_line_rec.fulfillment_base, 'P') ='S'))
17122 	       THEN
17123 
17124           IF l_debug_level  > 0 THEN
17125              oe_debug_pub.add(  'CALLING INVS DELETE_RESERVATION' , 3 ) ;
17126           END IF;
17127           inv_reservation_pub.delete_reservation
17128           ( p_api_version_number      => 1.0
17129           , p_init_msg_lst            => fnd_api.g_true
17130           , x_return_status           => x_return_status
17131           , x_msg_count               => l_msg_count
17132           , x_msg_data                => l_msg_data
17133           , p_rsv_rec                 => l_rsv_rec
17134           , p_serial_number           => l_dummy_sn
17135           );
17136 
17137           IF l_debug_level  > 0 THEN
17138              oe_debug_pub.add(  'AFTER CALLING INVS DELETE_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
17139           END IF;
17140 
17141           IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
17142              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
17143           ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
17144             oe_msg_pub.transfer_msg_stack;
17145             l_msg_count:=OE_MSG_PUB.COUNT_MSG;
17146             FOR I in 1..l_msg_count LOOP
17147                l_msg_data := OE_MSG_PUB.Get(I,'F');
17148                IF l_debug_level  > 0 THEN
17149                    oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
17150                END IF;
17151            END LOOP;
17152 
17153            RAISE FND_API.G_EXC_ERROR;
17154           END IF;
17155 
17156           --Start 2346233
17157           /*
17158           l_qty_to_unreserve := l_qty_to_unreserve -
17159                             l_rsv_rec.reservation_quantity;
17160           */
17161           l_qty_to_unreserve := l_qty_to_unreserve -
17162                             l_rsv_rec.primary_reservation_quantity;
17163           -- End 2346233
17164 
17165 	   --ER#14763609
17166 	  l_qty2_to_unreserve := l_qty2_to_unreserve -
17167                                  l_rsv_rec.secondary_reservation_quantity;
17168 
17169 
17170           IF ( Nvl(p_line_rec.fulfillment_base, 'P') ='P') THEN     --ER#14763609
17171 	       IF (l_qty_to_unreserve <= 0) THEN
17172                     goto end_of_loop;
17173                END IF;
17174           ELSE
17175 	       IF (l_qty2_to_unreserve <= 0) THEN
17176                     goto end_of_loop;
17177                END IF;
17178           END IF ;
17179 
17180 
17181        ELSE -- res rec qty > l_qty_to_unreserve
17182           l_rsv_new_rec                              := l_rsv_rec;
17183           -- Start 2346233
17184           /*
17185           l_rsv_new_rec.reservation_quantity         :=
17186 
17187           l_rsv_rec.reservation_quantity - l_qty_to_unreserve ;
17188           l_rsv_new_rec.primary_reservation_quantity := fnd_api.g_miss_num;
17189           */
17190           l_rsv_new_rec.secondary_reservation_quantity := l_rsv_rec.secondary_reservation_quantity - l_qty2_to_unreserve ; ----ER#14763609
17191 
17192       IF (Nvl(p_line_rec.fulfillment_base, 'P') ='P') THEN    --ER#14763609
17193 	  l_rsv_new_rec.primary_reservation_quantity :=
17194             l_rsv_rec.primary_reservation_quantity - l_qty_to_unreserve ;
17195           l_rsv_new_rec.reservation_quantity := fnd_api.g_miss_num;
17196           -- End 2346233
17197 	   ELSE
17198  --         l_rsv_new_rec.reservation_quantity := l_rsv_rec.reservation_quantity - p_quantity_to_unreserve ;
17199 	  /* Instead of converting reservation_quantity from secondary_reservation_quantity/secondary_uom_code
17200 	  to reservation_uom_code, lets send reservation_quantity/reservation_uom_code as secondary_reservation_quantity/secondary_uom_code
17201 	  This will avoid rounding off issue. for example <<Assuming ORD UOM to be EA>>..
17202 	  suppose  secondary_reservation_quantity/secondary_uom_code is 10/EA
17203           and reservation_uom_code is DZ.
17204 	  So instead of converting 10 Ez to 0.83333 DZ ,  we will send Reservation Qty as 10 Ea.
17205 	  Becasue if we send 0.83333 DZ, then reserved qty displayed in sales order form will be 9.99996 Ea.(refer oe_line_util.get_reserved_qtys)
17206 	  So by sending Reservation Qty as 10 Ea., reserved qty seen in sales order will be 10 Ea.
17207 	  */
17208 --Ignore above note., check issue 3 on bug 16434168
17209 --start changes for bug 16434168
17210 /*	  l_rsv_new_rec.reservation_quantity         := l_rsv_new_rec.secondary_reservation_quantity ;
17211 	  l_rsv_new_rec.reservation_uom_code         := l_rsv_new_rec.secondary_uom_code;*/
17212 
17213      IF NOT OE_GLOBALS.Equal(l_rsv_rec.reservation_uom_code ,l_rsv_new_rec.secondary_uom_code ) THEN
17214 	  l_rsv_new_rec.reservation_quantity         := INV_CONVERT.INV_UM_CONVERT( item_id       => p_line_rec.inventory_item_id,
17215                                                                                     precision     => 5,
17216                                                                                     from_quantity =>l_rsv_new_rec.secondary_reservation_quantity,
17217                                                                                     to_unit       =>l_rsv_rec.reservation_uom_code,
17218                                                                                     from_unit     =>l_rsv_new_rec.secondary_uom_code,
17219                                                                                     from_name     =>NULL,
17220                                                                                     to_name       =>NULL
17221                                                                                    ) ;
17222      ELSE
17223        l_rsv_new_rec.reservation_quantity   := l_rsv_new_rec.secondary_reservation_quantity;
17224      END IF;
17225 --end changes for bug 16434168
17226           l_rsv_new_rec.primary_reservation_quantity := fnd_api.g_miss_num;
17227        END IF ;   --ER#14763609
17228 
17229           IF l_debug_level  > 0 THEN
17230              oe_debug_pub.add(  'OLD QTY : ' || L_RSV_REC.RESERVATION_QUANTITY , 3 ) ;
17231              oe_debug_pub.add(  'NEW QTY : ' || L_RSV_NEW_REC.RESERVATION_QUANTITY , 3 ) ;
17232           END IF;
17233 
17234           -- INVCONV
17235           /* OPM 14/SEP/00 send process attributes into the reservation
17236           =============================================================*
17237           IF INV_GMI_RSV_BRANCH.Process_Branch(p_organization_id => p_line_rec.ship_from_org_id)    -- OPM 2645605
17238           then */
17239 
17240   --       l_rsv_new_rec.attribute1     := p_line_rec.preferred_grade;
17241     /*    --commented for --ER#14763609, found regression in this code when multille reservations exxists
17242 
17243           -- 13928724
17244           IF p_line_rec.ordered_quantity2 < p_line_rec.reserved_quantity2 THEN
17245             l_rsv_new_rec.secondary_reservation_quantity   := p_line_rec.ordered_quantity2; -- INVCONV
17246           ELSE
17247             l_rsv_new_rec.secondary_reservation_quantity   := p_line_rec.reserved_quantity2;
17248           END IF;
17249 
17250 */ -- END ER#14763609,
17251           l_rsv_new_rec.secondary_uom_code               := p_line_rec.ordered_quantity_uom2;
17252  --       l_rsv_new_rec.attribute2     := p_line_rec.ordered_quantity2;
17253   --      l_rsv_new_rec.attribute3     := p_line_rec.ordered_quantity_uom2;
17254 
17255           --  END IF; INVCONV
17256 
17257           /* OPM 14/SEP/00 END
17258           ====================*/
17259 
17260 
17261           IF l_debug_level  > 0 THEN
17262             oe_debug_pub.add(  'CALLING INVS UPDATE_RESERVATION: ' , 3 ) ;
17263           END IF;
17264           inv_reservation_pub.update_reservation
17265           ( p_api_version_number        => 1.0
17266           , p_init_msg_lst              => fnd_api.g_true
17267           , p_original_rsv_rec          => l_rsv_rec
17268           , p_to_rsv_rec                => l_rsv_new_rec
17269           , p_original_serial_number    => l_dummy_sn -- no serial contorl
17270           , p_to_serial_number          => l_dummy_sn -- no serial control
17271           , p_validation_flag           => fnd_api.g_true
17272           , x_return_status             => x_return_status
17273           , x_msg_count                 => l_msg_count
17274           , x_msg_data                  => l_msg_data);
17275 
17276           IF l_debug_level  > 0 THEN
17277              oe_debug_pub.add(  'AFTER INVS UPDATE_RESERVATION: ' || X_RETURN_STATUS , 1 ) ;
17278           END IF;
17279 
17280           IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
17281              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
17282           ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
17283              oe_msg_pub.transfer_msg_stack;
17284              l_msg_count:=OE_MSG_PUB.COUNT_MSG;
17285              for I in 1..l_msg_count loop
17286                 l_msg_data := OE_MSG_PUB.Get(I,'F');
17287                 IF l_debug_level  > 0 THEN
17288                    oe_debug_pub.add(  L_MSG_DATA , 1 ) ;
17289                 END IF;
17290              end loop;
17291              RAISE FND_API.G_EXC_ERROR;
17292           END IF;
17293 
17294           l_qty_to_unreserve := 0;
17295           goto end_of_loop;
17296 
17297         END IF;
17298      END LOOP;
17299   ELSE
17300      IF l_debug_level  > 0 THEN
17301       oe_debug_pub.add(  'Nothing to unreserve' , 3 ) ;
17302      END IF;
17303   END IF; -- 8644811
17304   <<end_of_loop>>
17305   IF l_debug_level  > 0 THEN
17306       oe_debug_pub.add(  'EXITING DO_UNRESERVE' , 3 ) ;
17307   END IF;
17308 
17309 EXCEPTION
17310   WHEN FND_API.G_EXC_ERROR THEN
17311     x_return_status := FND_API.G_RET_STS_ERROR;
17312 
17313   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
17314     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17315 
17316   WHEN OTHERS THEN
17317     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
17318 
17319     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
17320     THEN
17321       OE_MSG_PUB.Add_Exc_Msg
17322       (   G_PKG_NAME
17323       ,   'Do_Unreserve'
17324        );
17325     END IF;
17326 
17327 END Do_Unreserve;
17328 -- End  2595661
17329 -- Pack J
17330 /*--------------------------------------------------------------------------
17331 Procedure Name : Promise_Date_for_Sch_Action
17332 Description    : This API checks Promise date setup flag and based of Order
17333                  date type code and scheduling action sets the promise date
17334                  with schedule ship date/schedule arrival date.
17335 -------------------------------------------------------------------------- */
17336 PROCEDURE Promise_Date_for_Sch_Action
17337 (p_x_line_rec IN OUT NOCOPY OE_ORDER_PUB.Line_Rec_Type,
17338  p_sch_action IN VARCHAR2,
17339  P_header_id  IN NUMBER DEFAULT NULL)
17340 IS
17341   CURSOR order_header_id IS
17342   SELECT header_id
17343   FROM oe_order_lines_all
17344   WHERE line_id = p_x_line_rec.line_id;
17345 
17346   l_promise_date_flag  VARCHAR2(2);
17347   l_order_date_type_code VARCHAR2(15);
17348   l_header_id  NUMBER;
17349 --
17350 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
17351 --
17352 
17353 BEGIN
17354    IF l_debug_level  > 0 THEN
17355       oe_debug_pub.add(  'ENTERING PROMISE_DATE_FOR_SCH_ACTION ' , 3 ) ;
17356    END IF;
17357    IF p_header_id IS NULL THEN
17358       -- Heade id not provided.
17359       OPEN order_header_id;
17360       FETCH order_header_id INTO l_header_id;
17361       CLOSE order_header_id;
17362    ELSE
17363       l_header_id := p_header_id;
17364    END IF;
17365    l_order_date_type_code := NVL(oe_schedule_util.Get_Date_Type(l_header_id),'SHIP');
17366    l_promise_date_flag := Oe_Sys_Parameters.Value('PROMISE_DATE_FLAG');
17367    IF l_debug_level  > 0 THEN
17368       oe_debug_pub.add(  'PROMISE DATE FLAG: '||l_promise_date_flag , 3 ) ;
17369       oe_debug_pub.add(  'STATUS CODE : '||p_x_line_rec.schedule_status_code , 3 ) ;
17370    END IF;
17371    IF (l_promise_date_flag ='FS'
17372       AND ((p_sch_action = OESCH_ACT_DEMAND
17373        OR p_sch_action = OESCH_ACT_SCHEDULE)
17374       OR (p_sch_action = OESCH_ACT_RESCHEDULE
17375        AND p_x_line_rec.schedule_status_code IS NULL  --4496187
17376        and Nvl(p_x_line_rec.cancelled_flag,'N')='N' ))) --12980916
17377      OR (l_promise_date_flag ='S'
17378      AND (p_sch_action = OESCH_ACT_DEMAND
17379       OR p_sch_action = OESCH_ACT_SCHEDULE
17380       OR p_sch_action = OESCH_ACT_REDEMAND
17381       OR p_sch_action = OESCH_ACT_RESCHEDULE))THEN
17382 
17383        IF l_order_date_type_code = 'SHIP' THEN
17384           p_x_line_rec.promise_date := p_x_line_rec.schedule_ship_date;
17385        ELSE
17386           p_x_line_rec.promise_date := p_x_line_rec.schedule_arrival_date;
17387        END IF;
17388    ELSIF l_promise_date_flag ='S'
17389      AND (p_sch_action = OESCH_ACT_UNSCHEDULE
17390       OR p_sch_action = OESCH_ACT_UNDEMAND) THEN --3345776
17391       -- Promise date setup is schedule ship date/ arrival date
17392       -- Clearing the promise date for unscheduling
17393       p_x_line_rec.promise_date := Null;
17394    END IF;
17395    IF l_debug_level  > 0 THEN
17396       oe_debug_pub.add(  'EXITING PROMISE_DATE_FOR_SCH_ACTION ' , 3 ) ;
17397    END IF;
17398 END Promise_Date_for_Sch_Action;
17399 
17400 PROCEDURE Global_atp(p_line_id IN NUMBER)
17401 IS
17402      l_header_id               NUMBER;
17403      l_order_number            NUMBER;
17404      l_line_id                 NUMBER;
17405      l_inventory_item_id       NUMBER;
17406      l_sold_to_org_id          NUMBER;
17407      l_ship_to_org_id          NUMBER;
17408      l_ship_from_org_id        NUMBER;
17409      l_quantity_ordered        NUMBER;
17410      l_uom_code                VARCHAR2(3);
17411      l_requested_ship_date     DATE  := null;
17412      l_requested_arrival_date  DATE  := null;
17413      l_delivery_lead_time      NUMBER;
17414      l_latest_acceptable_date  DATE  := null;
17415      l_freight_carrier         VARCHAR2(30) := null;
17416      l_ship_method             VARCHAR2(30) := null;
17417      l_demand_class            VARCHAR2(30) := null;
17418      l_ship_set_name           VARCHAR2(30) := null;
17419      l_arrival_set_name        VARCHAR2(30) := null;
17420      l_order_date_type_code    VARCHAR2(30) := null;
17421      l_session_id              NUMBER;
17422      l_scenario_id             NUMBER := -1;
17423      l_order_header_id         NUMBER;
17424      l_order_line_id           NUMBER;
17425      l_valid                   BOOLEAN := TRUE;
17426      l_instance_id             NUMBER;
17427      result                    BOOLEAN;
17428      l_project_id              NUMBER;
17429      l_task_id                 NUMBER;
17430      l_project_number          NUMBER;
17431      l_task_number             NUMBER;
17432      l_ship_method_text        VARCHAR2(80);
17433      l_line_number             NUMBER;
17434      l_shipment_number         NUMBER;
17435      l_option_number           NUMBER;
17436      l_promise_date            DATE;
17437      l_request_date            DATE;
17438      l_customer_name           VARCHAR2(50) := null;
17439      l_customer_location       VARCHAR2(40) := null;
17440      l_ship_set_id             NUMBER := null;
17441      l_ship_set_id_1           NUMBER := null;
17442      l_arrival_set_id          NUMBER := null;
17443      l_ato_line_id             NUMBER := null;
17444      l_top_model_line_id       NUMBER := null;
17445      l_smc_flag                VARCHAR2(1) := null;
17446      l_ordered_item            VARCHAR2(2000);
17447      l_return_status           VARCHAR2(1);
17448      l_insert_code             NUMBER;
17449      l_insert_flag             VARCHAR2(240);
17450      p_arrival_set_id          NUMBER;
17451      p_ship_set_id             NUMBER;
17452      p_top_model_line_id       NUMBER;
17453      p_ato_line_id             NUMBER;
17454      l_st_ato_line_id          NUMBER;
17455      l_atp_lead_time           NUMBER := 0;
17456      l_st_atp_lead_time        NUMBER := 0;
17457      l_item_type_code          VARCHAR2(30);
17458      l_ato_model_line_id       NUMBER;
17459      l_conc_line_number        VARCHAR2(30);
17460      l_config_line_id          NUMBER;
17461      l_component_code          VARCHAR2(1000);
17462      l_component_sequence_id   NUMBER;
17463      l_link_to_line_id         NUMBER;
17464 
17465      CURSOR set_lines(p_header_id         IN NUMBER,
17466                       p_line_id           IN NUMBER,
17467                       p_arrival_set_id    IN NUMBER,
17468                       p_ship_set_id       IN NUMBER,
17469                       p_top_model_line_id IN NUMBER,
17470                       p_ato_line_id       IN NUMBER)
17471      IS
17472      SELECT line_id,
17473             header_id,
17474             inventory_item_id,
17475             ordered_item,
17476             sold_to_org_id,
17477             ship_to_org_id,
17478             ship_from_org_id,
17479             demand_class_code,
17480             ordered_quantity,
17481             order_quantity_uom,
17482             latest_acceptable_date,
17483             line_number,
17484             shipment_number,
17485             option_number,
17486             delivery_lead_time,
17487             request_date,
17488             promise_date,
17489             project_id,
17490             task_id,
17491             shipping_method_code,
17492             ship_set_id,
17493             arrival_set_id,
17494             link_to_line_id,
17495             ato_line_id,
17496             item_type_code,
17497             top_model_line_id,
17498             component_sequence_id,
17499             component_code
17500      FROM OE_ORDER_LINES_ALL
17501      where header_id = p_header_id AND
17502           (arrival_set_id = p_arrival_set_id OR
17503            ship_set_id = p_ship_set_id OR
17504            top_model_line_id = p_top_model_line_id OR
17505            ato_line_id = p_ato_line_id) AND
17506            line_id <> p_line_id AND
17507            item_type_code <> 'CONFIG';
17508 
17509 BEGIN
17510 
17511            BEGIN
17512               SELECT header_id,
17513                      inventory_item_id,
17514                      ordered_item,
17515                      sold_to_org_id,
17516                      ship_to_org_id,
17517                      ship_from_org_id,
17518                      demand_class_code,
17519                      ordered_quantity,
17520                      order_quantity_uom,
17521                      latest_acceptable_date,
17522                      line_number,
17523                      shipment_number,
17524                      option_number,
17525                      ship_model_complete_flag,
17526                      top_model_line_id,
17527                      ato_line_id,
17528                      delivery_lead_time,
17529                      request_date,
17530                      promise_date,
17531                      project_id,
17532                      task_id,
17533                      shipping_method_code,
17534                      ship_set_id,
17535                      arrival_set_id,
17536                      item_type_code,
17537                      link_to_line_id,
17538                      component_code,
17539                      component_sequence_id
17540               INTO l_header_id,
17541                    l_inventory_item_id,
17542                    l_ordered_item,
17543                    l_sold_to_org_id,
17544                    l_ship_to_org_id,
17545                    l_ship_from_org_id,
17546                    l_demand_class,
17547                    l_quantity_ordered,
17548                    l_uom_code,
17549                    l_latest_acceptable_date,
17550                    l_line_number,
17551                    l_shipment_number,
17552                    l_option_number,
17553                    l_smc_flag,
17554                    l_top_model_line_id,
17555                    l_ato_line_id,
17556                    l_delivery_lead_time,
17557                    l_request_date,
17558                    l_promise_date,
17559                    l_project_id,
17560                    l_task_id,
17561                    l_ship_method,
17562                    l_ship_set_id,
17563                    l_arrival_set_id,
17564                    l_item_type_code,
17565                    l_link_to_line_id,
17566                    l_component_code,
17567                    l_component_sequence_id
17568               FROM oe_order_lines_all
17569               WHERE line_id = p_line_id;
17570 
17571               l_line_id := p_line_id;
17572 
17573               SELECT ORDER_NUMBER ,ORDER_DATE_TYPE_CODE
17574               INTO   l_order_number,l_order_date_type_code
17575               FROM   oe_order_headers
17576               WHERE  header_id=l_header_id;
17577 
17578               IF l_order_date_type_code = 'ARRIVAL' THEN
17579                  l_requested_arrival_date := l_request_date;
17580                  IF l_requested_arrival_date is null THEN
17581                     l_requested_arrival_date := SYSDATE;
17582                  END IF;
17583                  l_requested_ship_date := null;
17584               ELSE
17585                  l_requested_ship_date := l_request_date;
17586                  IF l_requested_ship_date is null THEN
17587                     l_requested_ship_date := SYSDATE;
17588                  END IF;
17589                  l_requested_arrival_date := null;
17590               END IF;
17591 
17592 
17593            EXCEPTION
17594                WHEN OTHERS THEN
17595                     l_valid := FALSE;
17596            END;
17597 
17598 
17599         IF l_valid THEN
17600 
17601             -- Get the lead time for ATO options
17602             IF l_ato_line_id is not null AND
17603                l_line_id <> l_ato_line_id
17604             THEN
17605 
17606               -- This lines is a ato option or class. Set the atp_lead_time
17607               -- for it.
17608 
17609               IF l_ato_line_id = l_st_ato_line_id THEN
17610                   l_atp_lead_time       := l_st_atp_lead_time;
17611               ELSE
17612                   l_st_atp_lead_time :=
17613                       OE_SCHEDULE_UTIL.Get_Lead_Time
17614                          (p_ato_line_id      => l_ato_line_id,
17615                           p_ship_from_org_id => l_ship_from_org_id);
17616                   l_atp_lead_time  := l_st_atp_lead_time;
17617 
17618                   l_st_ato_line_id := l_ato_line_id;
17619               END IF;
17620             ELSE
17621                l_atp_lead_time          :=0;
17622             END IF;
17623 
17624             -- Get the display values which need to be passed to MRP
17625 
17626             l_project_number    := l_project_id;
17627             l_task_number       := l_task_id;
17628             l_ship_method_text  := l_ship_method;
17629 
17630             IF l_sold_to_org_id is not null
17631             THEN
17632                BEGIN
17633                   SELECT NAME
17634                   INTO l_customer_name
17635                   FROM OE_SOLD_TO_ORGS_V
17636                   WHERE organization_id = l_sold_to_org_id;
17637                EXCEPTION
17638                   WHEN NO_DATA_FOUND THEN
17639                      l_customer_name     := null;
17640                END;
17641             END IF;
17642 
17643             IF l_ship_to_org_id is not null
17644             THEN
17645                BEGIN
17646                   SELECT NAME
17647                   INTO l_customer_location
17648                   FROM OE_SHIP_TO_ORGS_V
17649                   WHERE organization_id = l_ship_to_org_id;
17650                EXCEPTION
17651                   WHEN NO_DATA_FOUND THEN
17652                      l_customer_location := null;
17653                END;
17654             END IF;
17655 
17656             IF l_ship_set_id is not null
17657             THEN
17658                l_ship_set_id_1 := l_ship_set_id;
17659                BEGIN
17660                   SELECT SET_NAME
17661                   INTO l_ship_set_name
17662                   FROM OE_SETS
17663                   WHERE set_id = l_ship_set_id;
17664                EXCEPTION
17665                   WHEN NO_DATA_FOUND THEN
17666                      l_ship_set_name := null;
17667                END;
17668             ELSE
17669                IF nvl(l_smc_flag,'N') = 'Y' THEN
17670                   l_ship_set_id_1 := l_top_model_line_id;
17671                ELSIF l_ato_line_id is not null THEN
17672                  IF  NOT(OE_CODE_CONTROL.CODE_RELEASE_LEVEL >= '110510'
17673                          AND  MSC_ATP_GLOBAL.GET_APS_VERSION = 10) THEN
17674                      l_ship_set_id_1 := l_ato_line_id;
17675                  END IF;
17676                END IF;
17677             END IF;
17678 
17679             IF l_arrival_set_id is not null
17680             THEN
17681                BEGIN
17682                   SELECT SET_NAME
17683                   INTO l_arrival_set_name
17684                   FROM OE_SETS
17685                   WHERE set_id = l_arrival_set_id;
17686                EXCEPTION
17687                   WHEN NO_DATA_FOUND THEN
17688                      l_arrival_set_name := null;
17689                END;
17690             END IF;
17691 -- Pack-J changes
17692             IF l_ato_line_id is not null AND
17693             NOT (l_ato_line_id = l_line_id AND
17694                  l_item_type_code in ('STANDARD','OPTION','INCLUDED')) --9775352
17695             THEN
17696 
17697                  l_ato_model_line_id := l_ato_line_id;
17698 
17699                  BEGIN
17700 
17701                    Select line_id
17702                    Into   l_config_line_id
17703                    From   oe_order_lines_all
17704                    Where  ato_line_id = l_line_id
17705                    And    item_type_code = 'CONFIG';
17706 
17707                  EXCEPTION
17708                    WHEN OTHERS THEN
17709                     l_config_line_id := Null;
17710                  END;
17711             ELSE
17712                l_link_to_line_id   := Null;
17713                l_ato_model_line_id := Null;
17714                l_config_line_id    := Null;
17715             END IF;
17716 
17717             l_conc_line_number :=
17718                    OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(l_line_id);
17719 
17720             l_session_id := Get_Session_Id;
17721 
17722          SELECT instance_id
17723             INTO l_instance_id
17724             FROM mrp_ap_apps_instances;
17725 
17726             l_insert_flag :=  fnd_profile.value('MRP_ATP_CALC_SD');
17727 
17728             IF nvl(l_insert_flag,'N') = 'Y' THEN
17729                l_insert_code   := 1;
17730             ELSE
17731                l_insert_code   := 2;
17732             END IF;
17733 
17734             -- Insert into mrp_atp_schedule_temp table
17735 
17736             INSERT INTO MRP_ATP_SCHEDULE_TEMP
17737             (INVENTORY_ITEM_ID,
17738           SR_INSTANCE_ID,
17739              SOURCE_ORGANIZATION_ID,
17740              CUSTOMER_ID,
17741              CUSTOMER_SITE_ID,
17742              DESTINATION_TIME_ZONE,
17743              QUANTITY_ORDERED,
17744              UOM_CODE,
17745              REQUESTED_SHIP_DATE,
17746              REQUESTED_ARRIVAL_DATE,
17747              LATEST_ACCEPTABLE_DATE,
17748              DELIVERY_LEAD_TIME,
17749              FREIGHT_CARRIER,
17750              INSERT_FLAG,
17751              SHIP_METHOD,
17752              DEMAND_CLASS,
17753              SHIP_SET_NAME,
17754              SHIP_SET_ID,
17755              ARRIVAL_SET_NAME,
17756              ARRIVAL_SET_ID,
17757              OVERRIDE_FLAG,
17758              SESSION_ID,
17759              ORDER_HEADER_ID,
17760              ORDER_LINE_ID,
17761              INVENTORY_ITEM_NAME,
17762              SOURCE_ORGANIZATION_CODE,
17763              ORDER_LINE_NUMBER,
17764              SHIPMENT_NUMBER,
17765              OPTION_NUMBER,
17766              PROMISE_DATE,
17767              CUSTOMER_NAME,
17768              CUSTOMER_LOCATION,
17769              OLD_LINE_SCHEDULE_DATE,
17770              OLD_SOURCE_ORGANIZATION_CODE,
17771              CALLING_MODULE,
17772              ACTION,
17773              STATUS_FLAG,
17774              SCENARIO_ID,
17775              ATP_LEAD_TIME,
17776              ORDER_NUMBER,
17777              OLD_SOURCE_ORGANIZATION_ID,
17778              OLD_DEMAND_CLASS,
17779              PROJECT_ID,
17780              TASK_ID,
17781              PROJECT_NUMBER,
17782              TASK_NUMBER,
17783              SHIP_METHOD_TEXT,
17784              TOP_MODEL_LINE_ID,
17785              ATO_MODEL_LINE_ID,
17786              PARENT_LINE_ID,
17787              VALIDATION_ORG,
17788              COMPONENT_SEQUENCE_ID,
17789              COMPONENT_CODE,
17790              INCLUDED_ITEM_FLAG,
17791              LINE_NUMBER,
17792              CONFIG_ITEM_LINE_ID
17793              )
17794             VALUES
17795             (l_inventory_item_id,
17796              l_instance_id,
17797              l_ship_from_org_id,  --null -- Bug 2913742
17798              l_sold_to_org_id, -- CUSTOMER_ID
17799              l_ship_to_org_id, -- CUSTOMER_SITE_ID
17800              null,  -- DESTINATION_TIME_ZONE
17801              l_quantity_ordered,
17802              l_uom_code,
17803              l_requested_ship_date,
17804              l_requested_arrival_date,
17805              l_latest_acceptable_date,
17806              l_delivery_lead_time,
17807              l_freight_carrier,
17808              l_insert_code,
17809              l_ship_method,
17810              l_demand_class,
17811              l_ship_set_name,
17812              l_ship_set_id_1,
17813              l_arrival_set_name,
17814              l_arrival_set_id,
17815              null, -- OVERRIDE_FLAG
17816              l_session_id,
17817              l_header_id,
17818              l_line_id,
17819              l_ordered_item, -- l_INVENTORY_ITEM_NAME,
17820              null, -- l_SOURCE_ORGANIZATION_CODE,
17821              l_line_number,
17822              l_shipment_number,
17823              l_option_number,
17824              l_promise_date,
17825              l_customer_name,
17826              l_customer_location,
17827              null, -- l_OLD_LINE_SCHEDULE_DATE,
17828              null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
17829              null, -- l_CALLING_MODULE,
17830              100,
17831              4, -- l_STATUS_FLAG,
17832              l_scenario_id,
17833              l_atp_lead_time,
17834              l_order_number,
17835              l_ship_from_org_id,
17836              l_demand_class,
17837              l_project_id,
17838              l_task_id,
17839              l_project_number,
17840              l_task_number,
17841              l_ship_method_text,
17842              l_top_model_line_id,
17843              l_ato_model_line_id,
17844              l_link_to_line_id,
17845              OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID'),
17846              l_component_sequence_id,
17847              l_component_code,
17848              1 , --l_included_item_flag
17849              l_conc_line_number,
17850              l_config_line_id
17851              );
17852 
17853              IF (l_ship_set_id is not null OR
17854                 l_arrival_set_id is not null OR
17855                 nvl(l_smc_flag,'N') = 'Y' OR
17856                 l_ato_line_id is not null) THEN
17857 
17858                 --p_line_id := l_line_id;
17859                 -- IF the line being passed to MRP is in a Ship Set,
17860                 -- or arrival set, we should get the remaining lines
17861                 -- of the set and pass them to MRP too.
17862 
17863                 IF l_arrival_set_id is not null THEN
17864                    p_arrival_set_id    := l_arrival_set_id;
17865                    p_ship_set_id       := null;
17866                    p_top_model_line_id := null;
17867                    p_ato_line_id       := null;
17868 
17869                    OPEN set_lines(p_header_id         => l_header_id,
17870                                   p_line_id           => p_line_id,
17871                                   p_arrival_set_id    => p_arrival_set_id,
17872                                   p_ship_set_id       => p_ship_set_id,
17873                                   p_top_model_line_id => p_top_model_line_id,
17874                                   p_ato_line_id       => p_ato_line_id);
17875 
17876                 ELSIF l_ship_set_id is not null THEN
17877 
17878                    p_arrival_set_id    := null;
17879                    p_ship_set_id       := l_ship_set_id;
17880                    p_top_model_line_id := null;
17881                    p_ato_line_id       := null;
17882 
17883                    OPEN set_lines(p_header_id         => l_header_id,
17884                                   p_line_id           => p_line_id,
17885                                   p_arrival_set_id    => p_arrival_set_id,
17886                                   p_ship_set_id       => p_ship_set_id,
17887                                   p_top_model_line_id => p_top_model_line_id,
17888                                   p_ato_line_id       => p_ato_line_id);
17889 
17890                 ELSIF l_smc_flag is not null THEN
17891                    p_arrival_set_id    := null;
17892                    p_ship_set_id       := null;
17893                    p_top_model_line_id := l_top_model_line_id;
17894                    p_ato_line_id       := null;
17895 
17896                    OPEN set_lines(p_header_id         => l_header_id,
17897                                   p_line_id           => p_line_id,
17898                                   p_arrival_set_id    => p_arrival_set_id,
17899                                   p_ship_set_id       => p_ship_set_id,
17900                                   p_top_model_line_id => p_top_model_line_id,
17901                                   p_ato_line_id       => p_ato_line_id);
17902 
17903                 ELSIF l_ato_line_id is not null THEN
17904                    p_arrival_set_id    := null;
17905                    p_ship_set_id       := null;
17906                    p_top_model_line_id := null;
17907                    p_ato_line_id       := l_ato_line_id;
17908 
17909                    OPEN set_lines(p_header_id         => l_header_id,
17910                                   p_line_id           => p_line_id,
17911                                   p_arrival_set_id    => p_arrival_set_id,
17912                                   p_ship_set_id       => p_ship_set_id,
17913                                   p_top_model_line_id => p_top_model_line_id,
17914                                   p_ato_line_id       => p_ato_line_id);
17915                 END IF;
17916                 LOOP
17917                    FETCH set_lines
17918                    INTO l_line_id,
17919                         l_header_id,
17920                         l_inventory_item_id,
17921                         l_ordered_item,
17922                         l_sold_to_org_id,
17923                         l_ship_to_org_id,
17924                         l_ship_from_org_id,
17925                         l_demand_class,
17926                         l_quantity_ordered,
17927                         l_uom_code,
17928                         l_latest_acceptable_date,
17929                         l_line_number,
17930                         l_shipment_number,
17931                         l_option_number,
17932                         l_delivery_lead_time,
17933                         l_request_date,
17934                         l_promise_date,
17935                         l_project_id,
17936                         l_task_id,
17937                         l_ship_method,
17938                         l_ship_set_id,
17939                         l_arrival_set_id,
17940                         l_link_to_line_id,
17941                         l_ato_line_id, -- 3730998
17942                         l_item_type_code,
17943                         l_top_model_line_id,
17944                         l_component_sequence_id,
17945                         l_component_code;
17946                    EXIT WHEN set_lines%NOTFOUND;
17947 
17948 
17949                    l_project_number    := l_project_id;
17950                    l_task_number       := l_task_id;
17951                    l_ship_method_text  := l_ship_method;
17952 
17953                    IF l_sold_to_org_id is not null
17954                    THEN
17955                       BEGIN
17956                          SELECT NAME
17957                          INTO l_customer_name
17958                          FROM OE_SOLD_TO_ORGS_V
17959                          WHERE organization_id = l_sold_to_org_id;
17960                       EXCEPTION
17961                          WHEN NO_DATA_FOUND THEN
17962                             l_customer_name     := null;
17963                       END;
17964                    END IF;
17965 
17966                    IF l_ship_to_org_id is not null
17967                    THEN
17968                       BEGIN
17969                          SELECT NAME
17970                          INTO l_customer_location
17971                          FROM OE_SHIP_TO_ORGS_V
17972                          WHERE organization_id = l_ship_to_org_id;
17973                       EXCEPTION
17974                          WHEN NO_DATA_FOUND THEN
17975                             l_customer_location := null;
17976                       END;
17977                    END IF;
17978 
17979                    IF l_ship_set_id is not null
17980                    THEN
17981                       BEGIN
17982                          SELECT SET_NAME
17983                          INTO l_ship_set_name
17984                          FROM OE_SETS
17985                          WHERE set_id = l_ship_set_id;
17986                       EXCEPTION
17987                          WHEN NO_DATA_FOUND THEN
17988                             l_ship_set_name := null;
17989                       END;
17990                    END IF;
17991 
17992                    IF l_arrival_set_id is not null
17993                    THEN
17994                       BEGIN
17995                          SELECT SET_NAME
17996                          INTO l_arrival_set_name
17997                          FROM OE_SETS
17998                          WHERE set_id = l_arrival_set_id;
17999                       EXCEPTION
18000                          WHEN NO_DATA_FOUND THEN
18001                             l_arrival_set_name := null;
18002                       END;
18003                    END IF;
18004 
18005                    IF l_order_date_type_code = 'ARRIVAL' THEN
18006                       l_requested_arrival_date := l_request_date;
18007                       IF l_requested_arrival_date is null THEN
18008                          l_requested_arrival_date := SYSDATE;
18009                       END IF;
18010                       l_requested_ship_date := null;
18011                    ELSE
18012                       l_requested_ship_date := l_request_date;
18013                       IF l_requested_ship_date is null THEN
18014                          l_requested_ship_date := SYSDATE;
18015                       END IF;
18016                       l_requested_arrival_date := null;
18017                    END IF;
18018 
18019                    -- Pack-J changes
18020                    IF l_ato_line_id is not null AND
18021                    NOT (l_ato_line_id = l_line_id AND
18022                         l_item_type_code in ('STANDARD','OPTION','INCLUDED'))
18023 			--9775352
18024                    THEN
18025 
18026                         l_ato_model_line_id := l_ato_line_id;
18027 
18028                         BEGIN
18029 
18030                           Select line_id
18031                           Into   l_config_line_id
18032                           From   oe_order_lines_all
18033                           Where  ato_line_id = l_line_id
18034                           And    item_type_code = 'CONFIG';
18035 
18036                         EXCEPTION
18037                           WHEN OTHERS THEN
18038                            l_config_line_id := Null;
18039                         END;
18040                    ELSE
18041                       l_link_to_line_id   := Null;
18042                       l_ato_model_line_id := Null;
18043                       l_config_line_id    := Null;
18044                    END IF;
18045 
18046                    l_conc_line_number :=
18047                           OE_ORDER_MISC_PUB.GET_CONCAT_LINE_NUMBER(l_line_id);
18048 
18049 
18050                    -- Insert into mrp_atp_schedule_temp table
18051 
18052                    INSERT INTO MRP_ATP_SCHEDULE_TEMP
18053                    (INVENTORY_ITEM_ID,
18054                  SR_INSTANCE_ID,
18055                     SOURCE_ORGANIZATION_ID,
18056                     CUSTOMER_ID,
18057                     CUSTOMER_SITE_ID,
18058                     DESTINATION_TIME_ZONE,
18059                     QUANTITY_ORDERED,
18060                     UOM_CODE,
18061                     REQUESTED_SHIP_DATE,
18062                     REQUESTED_ARRIVAL_DATE,
18063                     LATEST_ACCEPTABLE_DATE,
18064                     DELIVERY_LEAD_TIME,
18065                     FREIGHT_CARRIER,
18066                     INSERT_FLAG,
18067                     SHIP_METHOD,
18068                     DEMAND_CLASS,
18069                     SHIP_SET_NAME,
18070                     SHIP_SET_ID,
18071                     ARRIVAL_SET_NAME,
18072                     ARRIVAL_SET_ID,
18073                     OVERRIDE_FLAG,
18074                     SESSION_ID,
18075                     ORDER_HEADER_ID,
18076                     ORDER_LINE_ID,
18077                     INVENTORY_ITEM_NAME,
18078                     SOURCE_ORGANIZATION_CODE,
18079                     ORDER_LINE_NUMBER,
18080                     SHIPMENT_NUMBER,
18081                     OPTION_NUMBER,
18082                     PROMISE_DATE,
18083                     CUSTOMER_NAME,
18084                     CUSTOMER_LOCATION,
18085                     OLD_LINE_SCHEDULE_DATE,
18086                     OLD_SOURCE_ORGANIZATION_CODE,
18087                     CALLING_MODULE,
18088                     ACTION,
18089                     STATUS_FLAG,
18090                     SCENARIO_ID,
18091                     ATP_LEAD_TIME,
18092                     ORDER_NUMBER,
18093                     OLD_SOURCE_ORGANIZATION_ID,
18094                     OLD_DEMAND_CLASS,
18095                     PROJECT_ID,
18096                     TASK_ID,
18097                     PROJECT_NUMBER,
18098                     TASK_NUMBER,
18099                     SHIP_METHOD_TEXT,
18100                     TOP_MODEL_LINE_ID,
18101                     ATO_MODEL_LINE_ID,
18102                     PARENT_LINE_ID,
18103                     VALIDATION_ORG,
18104                     COMPONENT_SEQUENCE_ID,
18105                     COMPONENT_CODE,
18106                     INCLUDED_ITEM_FLAG,
18107                     LINE_NUMBER,
18108                     CONFIG_ITEM_LINE_ID
18109                     )
18110                    VALUES
18111                    (l_inventory_item_id,
18112                     l_instance_id,
18113                     l_ship_from_org_id,  --null -- Bug 2913742
18114                     l_sold_to_org_id, -- CUSTOMER_ID
18115                     l_ship_to_org_id, -- CUSTOMER_SITE_ID
18116                     null,  -- DESTINATION_TIME_ZONE
18117                     l_quantity_ordered,
18118                     l_uom_code,
18119                     l_requested_ship_date,
18120                     l_requested_arrival_date,
18121                     l_latest_acceptable_date,
18122                     l_delivery_lead_time,
18123                     l_freight_carrier,
18124                     l_insert_code,
18125                     l_ship_method,
18126                     l_demand_class,
18127                     l_ship_set_name,
18128                     l_ship_set_id_1,
18129                     l_arrival_set_name,
18130                     l_arrival_set_id,
18131                     null, -- OVERRIDE_FLAG
18132                     l_session_id,
18133                     l_header_id,
18134                     l_line_id,
18135                     l_ordered_item, -- l_INVENTORY_ITEM_NAME,
18136                     null, -- l_SOURCE_ORGANIZATION_CODE,
18137                     l_line_number,
18138                     l_shipment_number,
18139                     l_option_number,
18140                     l_promise_date,
18141                     l_customer_name,
18142                     l_customer_location,
18143                     null, -- l_OLD_LINE_SCHEDULE_DATE,
18144                     null, -- l_OLD_SOURCE_ORGANIZATION_CODE,
18145                     null, -- l_CALLING_MODULE,
18146                     100,
18147                     4, -- l_STATUS_FLAG,
18148                     l_scenario_id,
18149                     l_atp_lead_time,
18150                     l_order_number,
18151                     l_ship_from_org_id,
18152                     l_demand_class,
18153                     l_project_id,
18154                     l_task_id,
18155                     l_project_number,
18156                     l_task_number,
18157                     l_ship_method_text,
18158                     l_top_model_line_id,
18159                     l_ato_model_line_id,
18160                     l_link_to_line_id,
18161                     OE_Sys_Parameters.VALUE('MASTER_ORGANIZATION_ID'),
18162                     l_component_sequence_id,
18163                     l_component_code,
18164                     1 , --l_included_item_flag
18165                     l_conc_line_number,
18166                     l_config_line_id
18167                     );
18168 
18169 
18170                 END LOOP;
18171              END IF;
18172 
18173         END IF; /* l_valid */
18174 
18175 EXCEPTION
18176   WHEN OTHERS THEN
18177 
18178     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
18179     THEN
18180       OE_MSG_PUB.Add_Exc_Msg
18181       (   G_PKG_NAME
18182       ,   'Global_ATP'
18183        );
18184     END IF;
18185 
18186 END Global_ATP;
18187 
18188 
18189 Procedure Cascade_Ship_Set_Attr
18190 ( p_request_rec     IN  OE_Order_Pub.Request_Rec_Type
18191 , x_return_status   OUT NOCOPY /* file.sql.39 change */ VARCHAR2)
18192 IS
18193 
18194 l_line_tbl            OE_ORDER_PUB.line_tbl_type;
18195 l_old_line_tbl        OE_ORDER_PUB.line_tbl_type;
18196 l_control_rec         OE_GLOBALS.control_rec_type;
18197 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
18198 --
18199 l_msg_count                   NUMBER;
18200 l_msg_data                    VARCHAR2(2000);
18201 BEGIN
18202    IF l_debug_level  > 0 THEN
18203       oe_debug_pub.add(  'ENTERING Cascade Set Attr ' , 1 ) ;
18204       oe_debug_pub.add(  'Header_id  ' || p_request_rec.param1 , 2 ) ;
18205       oe_debug_pub.add(  'Ship_Set_id  ' || p_request_rec.param2 , 2 ) ;
18206       oe_debug_pub.add(  'Shipping Method  ' || p_request_rec.param3 , 2 ) ;
18207    END IF;
18208 
18209    x_return_status := FND_API.G_RET_STS_SUCCESS;
18210 
18211      Oe_Config_Schedule_Pvt.Query_Set_Lines
18212         (p_header_id     => p_request_rec.param1,
18213          p_ship_set_id   => p_request_rec.param2,
18214          p_sch_action    => 'UI_ACTION',
18215          x_line_tbl      => l_line_tbl,
18216          x_return_status  => x_return_status);
18217 
18218      l_old_line_tbl := l_line_tbl;
18219 
18220      FOR I IN 1..l_line_tbl.count LOOP
18221 
18222        l_line_tbl(I).shipping_method_code := p_request_rec.param3;
18223        l_line_tbl(I).operation := OE_GLOBALS.G_OPR_UPDATE;
18224 
18225      END LOOP;
18226 
18227     IF l_debug_level  > 0 THEN
18228        oe_debug_pub.add(  'Before calling Process Order from cascade ' , 2 ) ;
18229     END IF;
18230     Call_Process_Order(p_x_old_line_tbl  => l_old_line_tbl,
18231                        p_x_line_tbl      => l_line_tbl,
18232                        p_control_rec     => l_control_rec,
18233                        p_caller          => 'SCH_INTERNAL',
18234                        x_return_status   => x_return_status);
18235 
18236     IF l_debug_level  > 0 THEN
18237       oe_debug_pub.add(  'After calling Po : '  || x_return_status, 2 ) ;
18238     END IF;
18239 
18240     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18241        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18242     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
18243        RAISE FND_API.G_EXC_ERROR;
18244     END IF;
18245 
18246     OE_Order_PVT.Process_Requests_And_Notify
18247     ( p_process_requests        => TRUE
18248     , p_notify                  => FALSE
18249     , p_line_tbl                => l_line_tbl
18250     , p_old_line_tbl            => l_old_line_tbl
18251     , x_return_status           => x_return_status);
18252 
18253     IF l_debug_level  > 0 THEN
18254       oe_debug_pub.add(  'After calling PRN: '  || x_return_status, 2 ) ;
18255     END IF;
18256 
18257     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18258       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18259     ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
18260       RAISE FND_API.G_EXC_ERROR;
18261     END IF;
18262 
18263     OE_Set_Util.Update_Set
18264         (p_Set_Id                   => p_request_rec.param2,
18265          p_Shipping_Method_Code     => p_request_rec.param3,
18266          X_Return_Status            => x_return_status,
18267          x_msg_count                => l_msg_count,
18268          x_msg_data                 => l_msg_data
18269         );
18270 
18271    IF l_debug_level  > 0 THEN
18272       oe_debug_pub.add(  'Exiting Cascade Set Attr ' , 1 ) ;
18273    END IF;
18274 
18275 EXCEPTION
18276   WHEN OTHERS THEN
18277 
18278     IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
18279     THEN
18280       OE_MSG_PUB.Add_Exc_Msg
18281       (   G_PKG_NAME
18282       ,   'Cascade_Ship_set_attr'
18283        );
18284     END IF;
18285 
18286 END Cascade_Ship_set_attr;
18287 
18288 /*---------------------------------------------------------------------
18289 Procedure Name : MRP_ROLLBACK (OVERRLOADED)
18290 Description    : Call MRP API UNSCHEDULE the order line
18291                  which has been demanded in GOP with substitute item
18292                  but in OM still has original item
18293                  Added this procedure for the ER 2736896
18294                  ONLY called from procedure check_split_needed()
18295 --------------------------------------------------------------------- */
18296 
18297 Procedure MRP_ROLLBACK
18298 ( p_line_id IN NUMBER
18299  ,p_item_id in NUMBER
18300  ,p_schedule_action_code IN VARCHAR2
18301  ,x_return_status OUT NOCOPY VARCHAR2)
18302 
18303 IS
18304 l_msg_count               NUMBER;
18305 l_session_id              NUMBER := 0;
18306 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
18307 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
18308 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
18309 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
18310 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
18311 l_mrp_msg_data            VARCHAR2(200);
18312 l_old_line_rec            OE_Order_Pub.Line_Rec_Type;
18313 l_new_line_rec            OE_Order_Pub.Line_Rec_Type;
18314 
18315 --
18316 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
18317 --
18318 BEGIN
18319 
18320    IF l_debug_level  > 0 THEN
18321        oe_debug_pub.add(  'ENTERING CALL MRP ROLLBACK' , 1 ) ;
18322    END IF;
18323 
18324       OE_Line_Util.Query_Row(p_line_id  => p_line_id,
18325                              x_line_rec => l_old_line_rec);
18326       l_old_line_rec.schedule_action_code := p_schedule_action_code ;
18327       -- l_old_line_rec.schedule_action_code := OE_SCHEDULE_UTIL.OESCH_ACT_UNSCHEDULE;
18328 	  l_old_line_rec.inventory_item_id := p_item_id;
18329    l_new_line_rec := l_old_line_rec;
18330    Load_MRP_request_from_rec
18331        ( p_line_rec              => l_new_line_rec
18332        , p_old_line_rec          => l_old_line_rec
18333        , x_mrp_atp_rec             => l_mrp_atp_rec);
18334 
18335 
18336    IF l_mrp_atp_rec.error_code.count > 0 THEN
18337       l_session_id := Get_Session_Id;
18338 
18339       -- Call ATP
18340 
18341        IF l_debug_level  > 0 THEN
18342            oe_debug_pub.add(  '1. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
18343        END IF;
18344 
18345        MRP_ATP_PUB.Call_ATP
18346               (  p_session_id             =>  l_session_id
18347                , p_atp_rec                =>  l_mrp_atp_rec
18348                , x_atp_rec                =>  l_out_mrp_atp_rec
18349                , x_atp_supply_demand      =>  l_atp_supply_demand
18350                , x_atp_period             =>  l_atp_period
18351                , x_atp_details            =>  l_atp_details
18352                , x_return_status          =>  x_return_status
18353                , x_msg_data               =>  l_mrp_msg_data
18354                , x_msg_count              =>  l_msg_count);
18355 
18356        IF l_debug_level  > 0 THEN
18357           oe_debug_pub.add(  '3. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
18358        END IF;
18359 
18360        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18361           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18362        ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
18363              Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
18364                                 p_line_id => p_line_id);
18365              RAISE FND_API.G_EXC_ERROR;
18366        END IF;
18367 
18368        Load_Results_from_rec(p_atp_rec       => l_out_mrp_atp_rec,
18369                              p_x_line_rec    => l_new_line_rec,
18370                              x_return_status => x_return_status);
18371 
18372        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18373           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18374        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
18375           RAISE FND_API.G_EXC_ERROR;
18376        END IF;
18377 
18378    END IF; -- Mrp count.
18379    IF l_debug_level  > 0 THEN
18380        oe_debug_pub.add(  'EXITING CALL MRP ROLLBACK ' , 1 ) ;
18381    END IF;
18382 
18383 
18384 EXCEPTION
18385    WHEN FND_API.G_EXC_ERROR THEN
18386 
18387         x_return_status := FND_API.G_RET_STS_ERROR;
18388 
18389    WHEN OTHERS THEN
18390 
18391         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18392 
18393 /*
18394         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
18395         THEN
18396             OE_MSG_PUB.Add_Exc_Msg
18397             (   G_PKG_NAME,
18398               'MRP_ROLLBACK');
18399         END IF;
18400         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18401 */
18402 END MRP_ROLLBACK;
18403 
18404 /* Added the following 2 procedures to fix the bug 6378240  */
18405 
18406 /*---------------------------------------------------------------------
18407 Procedure Name : MRP_ROLLBACK
18408 Description    : Call MRP API to rollback the changes as the changes
18409                  on the line are being rollbacked.
18410                  Added this procedure to fix the bugs 6015417 , 6053872
18411 --------------------------------------------------------------------- */
18412 
18413 Procedure MRP_ROLLBACK
18414 ( p_line_id IN NUMBER
18415  ,p_schedule_action_code IN VARCHAR2
18416  ,x_return_status OUT NOCOPY VARCHAR2)
18417 
18418 IS
18419 l_msg_count               NUMBER;
18420 l_session_id              NUMBER := 0;
18421 l_mrp_atp_rec             MRP_ATP_PUB.ATP_Rec_Typ;
18422 l_out_mrp_atp_rec         MRP_ATP_PUB.ATP_Rec_Typ;
18423 l_atp_supply_demand       MRP_ATP_PUB.ATP_Supply_Demand_Typ;
18424 l_atp_period              MRP_ATP_PUB.ATP_Period_Typ;
18425 l_atp_details             MRP_ATP_PUB.ATP_Details_Typ;
18426 l_mrp_msg_data            VARCHAR2(200);
18427 l_old_line_rec            OE_Order_Pub.Line_Rec_Type;
18428 l_new_line_rec            OE_Order_Pub.Line_Rec_Type;
18429 
18430 --
18431 l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
18432 --
18433 BEGIN
18434 
18435    IF l_debug_level  > 0 THEN
18436        oe_debug_pub.add(  'ENTERING CALL MRP ROLLBACK' , 1 ) ;
18437    END IF;
18438 
18439       OE_Line_Util.Query_Row(p_line_id  => p_line_id,
18440                              x_line_rec => l_old_line_rec);
18441       l_old_line_rec.schedule_action_code := p_schedule_action_code ;
18442       -- l_old_line_rec.schedule_action_code := OE_SCHEDULE_UTIL.OESCH_ACT_UNSCHEDULE;
18443    l_new_line_rec := l_old_line_rec;
18444    Load_MRP_request_from_rec
18445        ( p_line_rec              => l_new_line_rec
18446        , p_old_line_rec          => l_old_line_rec
18447        , x_mrp_atp_rec             => l_mrp_atp_rec);
18448 
18449 
18450    IF l_mrp_atp_rec.error_code.count > 0 THEN
18451       l_session_id := Get_Session_Id;
18452 
18453       -- Call ATP
18454 
18455        IF l_debug_level  > 0 THEN
18456            oe_debug_pub.add(  '1. CALLING MRP API WITH SESSION ID '||L_SESSION_ID , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
18457        END IF;
18458 
18459        MRP_ATP_PUB.Call_ATP
18460               (  p_session_id             =>  l_session_id
18461                , p_atp_rec                =>  l_mrp_atp_rec
18462                , x_atp_rec                =>  l_out_mrp_atp_rec
18463                , x_atp_supply_demand      =>  l_atp_supply_demand
18464                , x_atp_period             =>  l_atp_period
18465                , x_atp_details            =>  l_atp_details
18466                , x_return_status          =>  x_return_status
18467                , x_msg_data               =>  l_mrp_msg_data
18468                , x_msg_count              =>  l_msg_count);
18469 
18470        IF l_debug_level  > 0 THEN
18471           oe_debug_pub.add(  '3. AFTER CALLING MRP_ATP_PUB.CALL_ATP' || X_RETURN_STATUS , 0.5 ) ; -- debug level changed to 0.5 for bug 13435459
18472        END IF;
18473 
18474        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18475           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18476        ELSIF  x_return_status = FND_API.G_RET_STS_ERROR THEN
18477              Display_sch_errors(p_atp_rec => l_out_mrp_atp_rec,
18478                                 p_line_id => p_line_id);
18479              RAISE FND_API.G_EXC_ERROR;
18480        END IF;
18481 
18482        Load_Results_from_rec(p_atp_rec       => l_out_mrp_atp_rec,
18483                              p_x_line_rec    => l_new_line_rec,
18484                              x_return_status => x_return_status);
18485 
18486        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18487           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18488        ELSIF x_return_status = FND_API.G_RET_STS_ERROR THEN
18489           RAISE FND_API.G_EXC_ERROR;
18490        END IF;
18491 
18492    END IF; -- Mrp count.
18493    IF l_debug_level  > 0 THEN
18494        oe_debug_pub.add(  'EXITING CALL MRP ROLLBACK ' , 1 ) ;
18495    END IF;
18496 
18497 
18498 EXCEPTION
18499    WHEN FND_API.G_EXC_ERROR THEN
18500 
18501         x_return_status := FND_API.G_RET_STS_ERROR;
18502 
18503    WHEN OTHERS THEN
18504 
18505         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18506 
18507 /*
18508         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
18509         THEN
18510             OE_MSG_PUB.Add_Exc_Msg
18511             (   G_PKG_NAME,
18512               'MRP_ROLLBACK');
18513         END IF;
18514         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18515 */
18516 END MRP_ROLLBACK;
18517 
18518 Procedure CALL_MRP_ROLLBACK
18519 ( x_return_status OUT NOCOPY VARCHAR2)
18520 
18521 IS
18522 
18523 l_status VARCHAR2(1);
18524 CURSOR C1 is
18525 select line_id , schedule_action_code
18526 from oe_schedule_lines_temp;
18527 
18528 BEGIN
18529 
18530  oe_debug_pub.add(  '6015417,6053872 : in call_mrp_rollback  ');
18531  for rec in C1 loop
18532        oe_debug_pub.add(  '6015417,6053872 : line_id  '||rec.line_id ||'  ' ||rec.schedule_action_code);
18533        if rec.schedule_action_code = 'SCHEDULE' then
18534            MRP_ROLLBACK
18535             ( p_line_id  =>  rec.line_id
18536              ,p_schedule_action_code  =>  OESCH_ACT_UNSCHEDULE
18537              ,x_return_status    => l_status);
18538            --8731703
18539              IF OE_SCH_CONC_REQUESTS.g_conc_program = 'Y' THEN
18540                 OE_SCH_CONC_REQUESTS.OE_line_status_Tbl(rec.line_id) := 'N';
18541              END IF;
18542         end if;
18543  end loop;
18544 
18545 EXCEPTION
18546    WHEN FND_API.G_EXC_ERROR THEN
18547 
18548         x_return_status := FND_API.G_RET_STS_ERROR;
18549 
18550    WHEN OTHERS THEN
18551 
18552         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18553 
18554 /*
18555         IF OE_MSG_PUB.Check_Msg_Level(OE_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
18556         THEN
18557             OE_MSG_PUB.Add_Exc_Msg
18558             (   G_PKG_NAME,
18559               'Call_MRP_ROLLBACK');
18560         END IF;
18561 */
18562 END CALL_MRP_ROLLBACK;
18563 
18564 /* Added the following to fix the bug 6663462 */
18565 
18566 
18567 Procedure DELAYED_SCHEDULE_LINES
18568 ( x_return_status OUT NOCOPY VARCHAR2)
18569 
18570 IS
18571 
18572 l_status VARCHAR2(1);
18573 j        NUMBER;
18574 i        NUMBER := 0 ;
18575 l_atp_tbl OE_ATP.Atp_Tbl_Type;
18576 l_return_status   VARCHAR2(1);
18577 l_msg_count       NUMBER;
18578 l_msg_data        VARCHAR2(2000);
18579 l_line_tbl                OE_ORDER_PUB.Line_tbl_type;
18580 l_old_line_tbl            OE_ORDER_PUB.Line_tbl_type;
18581 l_line_rec OE_Order_PUB.Line_Rec_Type; --Bug 8652339
18582 BEGIN
18583   oe_debug_pub.add(  '6663462 : in schedule_delayed_lines ');
18584 
18585   --Bug 8652339 : Initialize l_line_tbl from OE_ORDER_LINES_ALL table
18586   --using OE_LINE_UTIL.QUERY_ROW instead of following.
18587   --l_line_tbl := OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl;
18588   for j in 1..OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl.count loop
18589 
18590     BEGIN
18591 
18592        oe_line_util.query_row(p_line_id => OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl(j).line_id
18593                               ,x_line_rec => l_line_rec
18594                           );
18595        i := i + 1 ;
18596        l_line_tbl(i) := l_line_rec;
18597 
18598     EXCEPTION
18599 
18600 /* added the exception handler to fix the bug 11814008  */
18601 
18602       WHEN NO_DATA_FOUND THEN
18603         NULL;
18604 
18605     END;
18606   end loop;
18607 
18608   l_old_line_tbl := l_line_tbl;
18609   for j in 1..l_line_tbl.count LOOP
18610        oe_debug_pub.add(  ' 6663462  : line_id   '|| l_line_tbl(j).line_id );
18611        l_line_tbl(j).operation := OE_GLOBALS.G_OPR_UPDATE; --6715950
18612   end loop;
18613   oe_debug_pub.add(  ' 6663462  : calling process group ' );
18614   IF l_line_tbl.count > 0 THEN
18615 
18616      Oe_Config_Schedule_Pvt.Process_Group
18617        (p_x_line_tbl     => l_line_tbl
18618        ,p_old_line_tbl   => l_old_line_tbl
18619        ,p_caller         => 'UI_ACTION'
18620        ,p_sch_action     => 'SCHEDULE'
18621        ,p_partial        => TRUE
18622        ,x_return_status  => x_return_status);
18623 
18624      OE_SCHEDULE_UTIL.OE_Delayed_Schedule_line_tbl.delete;
18625 
18626   END IF;
18627 
18628 EXCEPTION
18629    WHEN FND_API.G_EXC_ERROR THEN
18630 
18631         x_return_status := FND_API.G_RET_STS_ERROR;
18632 
18633    WHEN OTHERS THEN
18634         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18635 
18636 END DELAYED_SCHEDULE_LINES ;
18637 
18638 /*----------------------------------------------------------------------------------
18639 * PROCEDURE IS_ITEM_SUBSTITUTED
18640 * Added for ER 6110708. This API will be used for Item Substituted Validation template.
18641 * ------------------------------------------------------------------------------------*/
18642 PROCEDURE IS_ITEM_SUBSTITUTED
18643 (
18644   p_application_id    IN  NUMBER
18645 , p_entity_short_name   IN  VARCHAR2
18646 , p_validation_entity_short_name  IN  VARCHAR2
18647 , p_validation_tmplt_short_name IN  VARCHAR2
18648 , p_record_set_short_name     IN  VARCHAR2
18649 , p_scope             IN  VARCHAR2
18650 , x_result_out OUT NOCOPY NUMBER
18651 
18652 )
18653 IS
18654 BEGIN
18655 
18656   IF OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED = 'Y' THEN
18657     x_result_out := 1;
18658   ELSE
18659     x_result_out := 0;
18660   END IF;
18661 
18662 END IS_ITEM_SUBSTITUTED;
18663 
18664 /*-----------------------------------------------------------------------------------
18665  * PROCEDURE IS_LINE_PICKED
18666  * Added for ER 6110708. This API will be used for Not Picked Validation template.
18667  * ----------------------------------------------------------------------------------*/
18668 PROCEDURE IS_LINE_PICKED
18669 (
18670   p_application_id    IN  NUMBER
18671 , p_entity_short_name   IN  VARCHAR2
18672 , p_validation_entity_short_name  IN  VARCHAR2
18673 , p_validation_tmplt_short_name IN  VARCHAR2
18674 , p_record_set_short_name     IN  VARCHAR2
18675 , p_scope             IN  VARCHAR2
18676 , x_result_out OUT NOCOPY NUMBER
18677 
18678 )
18679 IS
18680 
18681   CURSOR C_IS_LINE_PICKED
18682   IS
18683   SELECT PICK_STATUS
18684   FROM   WSH_DELIVERY_LINE_STATUS_V
18685   WHERE  SOURCE_CODE = 'OE'
18686   AND    SOURCE_LINE_ID = OE_LINE_SECURITY.g_record.line_id
18687   AND    PICK_STATUS NOT IN ('N', 'R', 'X', 'B'); --Added 'B', (Backordered Status) for bug 8521082
18688 
18689   l_pick_status VARCHAR2(1);
18690 
18691 BEGIN
18692 
18693   OPEN C_IS_LINE_PICKED;
18694   FETCH C_IS_LINE_PICKED into l_pick_status;
18695 
18696   /* If there is atleast one delivery for the current line which is not having the pick status of N,R and X,
18697      that means the delivery has been picked atleast once by Pick Release program. The delivery has gone past the Picking at least once.
18698   */
18699   IF C_IS_LINE_PICKED%FOUND THEN
18700     x_result_out := 1;
18701   ELSE
18702     x_result_out := 0;
18703   END IF;
18704 
18705   CLOSE C_IS_LINE_PICKED;
18706 
18707 END IS_LINE_PICKED;
18708 
18709 /*----------------------------------------------------------------------------------
18710  * PROCEDURE VALIDATE_ITEM_SUBSTITUTION
18711  * Added for ER 6110708. This API will validate the new substitute item before
18712  * calling process order api.
18713  * ---------------------------------------------------------------------------------*/
18714 PROCEDURE VALIDATE_ITEM_SUBSTITUTION
18715 (
18716 p_new_inventory_item_id   IN NUMBER,
18717 p_old_inventory_item_id   IN NUMBER,
18718 p_new_ship_from_org_id    IN NUMBER,
18719 p_old_ship_from_org_id    IN NUMBER,
18720 p_old_shippable_flag      IN VARCHAR2
18721 )
18722 IS
18723   l_shippable_flag  varchar2(1);
18724 BEGIN
18725    BEGIN
18726       SELECT shippable_item_flag
18727       INTO   l_shippable_flag
18728       FROM   MTL_SYSTEM_ITEMS
18729       WHERE  INVENTORY_ITEM_ID = p_new_inventory_item_id
18730       AND    ORGANIZATION_ID = p_new_ship_from_org_id;
18731 
18732       IF l_shippable_flag <> p_old_shippable_flag THEN
18733            oe_debug_pub.add(  'Item substitution cannot happen between shippable and non-shippable items' , 5 ) ;
18734            Fnd_Message.set_name('ONT','OE_SCH_LOOP_SHP_NONSHP');
18735            Oe_Msg_Pub.Add;
18736            OE_SCHEDULE_UTIL.OESCH_ITEM_IS_SUBSTITUTED := 'N';
18737            RAISE FND_API.G_EXC_ERROR;
18738       END IF;
18739    END;
18740 END VALIDATE_ITEM_SUBSTITUTION;
18741 
18742 /*----------------------------------------------------------------------------------
18743  * PROCEDURE res_against_req_po
18744  * Added for ER 9224462. This API will validate if there are any reservations of a
18745  * line against a REQ or PO. If it has, p_result will return 1, else return 0.
18746  * This API has standard signature as required by Processing Constraints framework,
18747  * for API based Validation template. Based on this API a Validation template will
18748  * be created.
18749  * ---------------------------------------------------------------------------------*/
18750 PROCEDURE res_against_req_po
18751 ( p_application_id               IN NUMBER,
18752   p_entity_short_name            in VARCHAR2,
18753   p_validation_entity_short_name in VARCHAR2,
18754   p_validation_tmplt_short_name  in VARCHAR2,
18755   p_record_set_tmplt_short_name  in VARCHAR2,
18756   p_scope                        in VARCHAR2,
18757   p_result                       OUT NOCOPY NUMBER )
18758 IS
18759 l_return_status        VARCHAR2(1);
18760 l_msg_count            NUMBER;
18761 l_msg_data             VARCHAR2(2000);
18762 l_reservation_rec      inv_reservation_global.mtl_reservation_rec_type;
18763 l_rsv_tbl              inv_reservation_global.mtl_reservation_tbl_type;
18764 l_count                NUMBER;
18765 l_x_error_code         NUMBER;
18766 l_lock_records         VARCHAR2(1);
18767 l_sort_by_req_date     NUMBER;
18768 
18769 l_ordered_quantity NUMBER := -1;
18770 l_reserved_quantity    NUMBER := 0;
18771 
18772 BEGIN
18773 
18774 If p_validation_entity_short_name = 'LINE' Then
18775 
18776   oe_debug_pub.add('Getting Reservation Details:');
18777   oe_debug_pub.add('Header :'||OE_LINE_SECURITY.g_record.header_id);
18778   oe_debug_pub.add('Line :'||OE_LINE_SECURITY.g_record.line_id);
18779 
18780   l_reservation_rec.reservation_id := fnd_api.g_miss_num;
18781   l_reservation_rec.demand_source_header_id :=
18782              oe_schedule_util.Get_mtl_sales_order_id(OE_LINE_SECURITY.g_record.header_id);
18783   l_reservation_rec.demand_source_line_id := OE_LINE_SECURITY.g_record.line_id;
18784   l_reservation_rec.organization_id  := OE_LINE_SECURITY.g_record.ship_from_org_id;
18785 
18786   inv_reservation_pub.query_reservation
18787            ( p_api_version_number        => 1.0
18788            , p_init_msg_lst              => fnd_api.g_true
18789            , x_return_status             => l_return_status
18790            , x_msg_count                 => l_msg_count
18791            , x_msg_data                  => l_msg_data
18792            , p_query_input               => l_reservation_rec
18793            , x_mtl_reservation_tbl       => l_rsv_tbl
18794            , x_mtl_reservation_tbl_count => l_count
18795            , x_error_code                => l_x_error_code
18796            , p_lock_records              => l_lock_records
18797            , p_sort_by_req_date          => l_sort_by_req_date
18798            );
18799 
18800 
18801   oe_debug_pub.add('After Querying Reservations');
18802 
18803   IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
18804     oe_debug_pub.add('Error Code :'||l_x_error_code);
18805     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
18806   ELSIF l_return_status = FND_API.G_RET_STS_ERROR THEN
18807     oe_debug_pub.add('Error Code :'||l_x_error_code);
18808     RAISE FND_API.G_EXC_ERROR;
18809   END IF;
18810 
18811   oe_debug_pub.add('Total Number of Reservation Records:'||l_rsv_tbl.count);
18812 
18813   FOR K IN 1..l_rsv_tbl.count LOOP
18814     oe_debug_pub.add('Supply_source '||k||' :'||l_rsv_tbl(K).supply_source_type_id);
18815     oe_debug_pub.add('Demand_source '||k||' :'||l_rsv_tbl(K).demand_source_type_id);
18816     IF l_rsv_tbl(K).supply_source_type_id IN (1,17) THEN
18817        	p_result := 1; --Return 1;
18818     	oe_debug_pub.add('There is some Qty Reserved against Req/Purchase Order. Return Value from Validation Pkg :'||p_result);
18819     	return;
18820     END IF;
18821   END LOOP;
18822 
18823   p_result := 0; --Return 0;
18824   oe_debug_pub.add('There are no reservations against Req/Purchase Order. Return Value from Validation Pkg :'||p_result);
18825 
18826 END IF;
18827 
18828 EXCEPTION
18829   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
18830     oe_debug_pub.add('Unexpected Error'||l_msg_data);
18831   WHEN FND_API.G_EXC_ERROR THEN
18832     oe_debug_pub.add('Expected Error'||l_msg_data);
18833 END res_against_req_po;
18834 
18835 /*4241385*/
18836 /*----------------------------------------------------------------------------------
18837  * PROCEDURE GET_SET_DETAILS
18838  * Added for ER 4241385. This API will take set_id as the input parameter and return
18839  whether the set exists or not (new set, or exisitng set) and if the set exisits,
18840  whether it is scheduled or not.
18841  * ---------------------------------------------------------------------------------*/
18842 PROCEDURE get_set_details
18843   (
18844     p_set_id IN NUMBER ,
18845     x_set_exists OUT NOCOPY BOOLEAN ,
18846     x_set_scheduled OUT NOCOPY BOOLEAN )
18847                                 IS
18848   l_debug_level CONSTANT NUMBER := oe_debug_pub.g_debug_level;
18849   l_ship_date DATE ;
18850   l_arrival_date DATE ;
18851 BEGIN
18852     IF l_debug_level >0 THEN
18853       oe_debug_pub.ADD('in get_Set_details-set_id is:'||p_set_id);
18854     END IF ;
18855 	SELECT schedule_ship_date,
18856                schedule_arrival_date
18857         INTO   l_ship_date,
18858                l_arrival_date
18859         FROM oe_sets
18860         WHERE set_id=p_set_id;
18861 
18862         x_set_exists     := TRUE ;
18863            IF l_ship_date   IS NULL
18864 	   AND l_arrival_date IS NULL THEN
18865                  x_set_scheduled:= FALSE ;
18866            ELSE
18867                  x_set_scheduled:= TRUE ;
18868            END IF ;
18869   --check the debug level commands.
18870               IF l_debug_level >0 THEN
18871                  IF x_set_exists THEN
18872                    oe_debug_pub.ADD('set exists');
18873                     IF x_set_scheduled THEN
18874                        oe_debug_pub.ADD('set is scheduled');
18875                     ELSE
18876                         oe_debug_pub.ADD('set is not scheduled.');
18877                      END IF ;
18878                  END IF ;
18879               END IF ;
18880 EXCEPTION
18881 WHEN NO_DATA_FOUND THEN
18882   x_set_exists    := FALSE;
18883   IF l_debug_level >0 THEN
18884     oe_debug_pub.ADD('in no data found exception. set does not exists');
18885   END IF ;
18886 WHEN OTHERS THEN
18887   IF l_debug_level >0 THEN
18888     oe_debug_pub.ADD('error in get_set_details:'||SQLERRM,5);
18889   END IF ;
18890 END get_set_details ;
18891 /*4241385*/
18892 END OE_SCHEDULE_UTIL;