DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_DIRECT_SHIP_PVT

Source


1 PACKAGE BODY WMS_DIRECT_SHIP_PVT AS
2 /* $Header: WMSDSPVB.pls 120.31.12020000.28 2013/05/17 22:51:04 sahmahes ship $ */
3 
4         -- standard global constants
5         G_PKG_NAME CONSTANT VARCHAR2(30)                := 'WMS_DIRECT_SHIP_PVT';
6         p_message_type  CONSTANT VARCHAR2(1)            := 'E';
7 
8 --Inline branching
9 G_WMS_CURRENT_RELEASE_LEVEL NUMBER := wms_control.g_current_release_level;
10 G_J_RELEASE_LEVEL       NUMBER := inv_release.g_j_release_level;
11 
12 FUNCTION GET_CATCH_WEIGHT
13                         (P_ORG_ID IN NUMBER
14                         ,P_LPN_ID IN NUMBER
15                         ,P_INVENTORY_ITEM_ID IN NUMBER
16                         ,P_REVISION IN VARCHAR2
17                         ,P_LOT_NUMBER IN VARCHAR2
18                         ,P_PICKED_QUANTITY_IN_PRI_UOM IN NUMBER
19                         ) RETURN NUMBER;
20 
21 /* types defined for Patchset I */
22 
23 -- This record stores the data for one row in lpn_contents.
24 TYPE lpn_content_rec IS RECORD (lpn_id                NUMBER,
25                                 subinventory_code     VARCHAR2(30),
26                                 locator_id            NUMBER,
27                                 inventory_item_id     NUMBER,
28                                 revision_control      BOOLEAN,
29                                 revision              VARCHAR2(3),
30                                 lot_control           BOOLEAN,
31 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
32                                 lot_number            VARCHAR2(80),
33                                 serial_control_code   NUMBER,
34                                 serial_control        BOOLEAN,
35                                 end_item_unit_number  VARCHAR2(30),
36                                 quantity              NUMBER,
37                                 secondary_quantity    NUMBER
38                                 );
39 
40 -- This table containes records of type lpn_content_rec. The table is
41 -- constructed from the output of cursor lpn_content_cur
42 TYPE lpn_contents_tab IS TABLE OF lpn_content_rec INDEX BY BINARY_INTEGER;
43 
44 -- This record form the lpn_contents_lookup_tab.
45 -- Here start_index and end_index specify record locations in lpn_contents_tab.
46 TYPE lpn_contents_lookup_rec IS RECORD (start_index NUMBER, end_index NUMBER);
47 
48 -- This table stores the lookup index values corresponding to a item in
49 -- lpn_contents_tab
50 TYPE lpn_contents_lookup_tab IS TABLE OF lpn_contents_lookup_rec INDEX BY BINARY_INTEGER;
51 
52 -- This table will stores all the checked deliveries for the current LPN
53 TYPE checked_delivery_tab IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
54 
55 -- for delivery grouping validations
56 TYPE del_grp_rls_flags IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
57 TYPE del_grp_rls_fld IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;
58 -- table of checked deliveries
59 TYPE checked_deliveries IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
60 
61 TYPE oe_header_src_type IS TABLE OF NUMBER INDEX BY LONG;
62 
63 TYPE delivery_detail_rec IS RECORD(
64     organization_id            NUMBER
65   , dock_door_id               NUMBER
66   , lpn_id                     NUMBER
67   , order_header_id            NUMBER
68   , order_line_id              NUMBER
69   , line_item_id               NUMBER
70   , transaction_temp_id        NUMBER
71   , delivery_detail_id         NUMBER
72   , requested_quantity         NUMBER
73   , primary_uom_code           VARCHAR2(3)
74   , requested_quantity2        NUMBER
75   , requested_quantity_uom2    VARCHAR2(3)
76   , lot_control_code           NUMBER
77   , serial_number_control_code NUMBER
78   , inventory_item_id          NUMBER
79   , ont_pricing_qty_source     VARCHAR2(30)
80   ); -- Added bug4128854
81 
82 TYPE delivery_detail_tab IS TABLE OF delivery_detail_rec
83     INDEX BY BINARY_INTEGER; --Added bug 4128854
84 /* global variables added for Patchset I */
85 
86 g_lpn_contents_tab lpn_contents_tab;
87 g_lpn_contents_lookup_tab lpn_contents_lookup_tab;
88 g_total_lpn_quantity NUMBER;
89 g_total_lpn_sec_qty NUMBER;
90 g_checked_delivery_tab checked_delivery_tab;
91 g_project_id NUMBER;
92 g_task_id NUMBER;
93 g_cross_project_allowed VARCHAR2(1);
94 g_cross_unit_allowed VARCHAR2(1);
95 g_subinventory_code VARCHAR2(30);
96 g_locator_id NUMBER;
97 g_group_id NUMBER;
98 -- who columns
99 g_last_update_date DATE;
100 g_last_updated_by NUMBER;
101 g_last_update_login NUMBER;
102 g_created_by NUMBER;
103 g_creation_date DATE;
104 -- used for validating delivery grouping rules
105 g_del_grp_rls_flags del_grp_rls_flags;
106 g_del_grp_rls_fld_value del_grp_rls_fld;
107 g_del_grp_rls_fld_temp del_grp_rls_fld;
108 -- checked deliveries for lpn
109 g_checked_deliveries checked_deliveries;
110 g_oe_header_type oe_header_src_type;
111 
112 g_fulfillment_base     VARCHAR2(1) := 'P';
113 g_matl_stat_checked    BOOLEAN     := FALSE;
114 G_RET_STS_SUCCESS      VARCHAR2(1) := FND_API.g_ret_sts_success;
115 G_RET_STS_ERROR        VARCHAR2(1) := FND_API.g_ret_sts_error;
116 G_RET_STS_UNEXP_ERROR  VARCHAR2(1) := FND_API.G_RET_STS_UNEXP_ERROR;
117 G_FALSE                VARCHAR2(1) := FND_API.G_FALSE;
118 G_TRUE                 VARCHAR2(1) := FND_API.G_TRUE;
119 G_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
120 
121 PROCEDURE DEBUG(p_message       IN VARCHAR2,
122                 p_module   IN VARCHAR2 ,
123                 p_level         IN VARCHAR2 DEFAULT 9) IS
124     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
125 BEGIN
126 
127 IF (l_debug = 1) THEN
128    INV_TRX_UTIL_PUB.TRACE( P_MESG =>P_MESSAGE
129                        ,P_MOD => p_module
130                        ,p_level => p_level
131                        );
132 END IF;
133  --dbms_output.put_line(p_message);
134 END; -- DEBUG
135 
136 PROCEDURE process_mobile_msg IS
137    l_msg_data VARCHAR2(2000);
138    l_msg_count NUMBER;
139    l_dummy_number NUMBER;
140 
141    l_app_short_name VARCHAR2(20);
142    l_msg_name       VARCHAR2(50);
143 BEGIN
144    debug('Entered procedure','process_mobile_msg');
145 
146    l_msg_count := fnd_msg_pub.count_msg;
147    debug('Msg Count: ' || l_msg_count,'process_mobile_msg');
148 
149    FOR i IN 1..l_msg_count LOOP
150       fnd_msg_pub.get(p_msg_index => i,
151               p_data => l_msg_data,
152               p_msg_index_out => l_dummy_number);
153 
154       debug('i: ' || i || ' index_out: ' || l_dummy_number || ' encoded_data: ' || l_msg_data,'process_mobile_msg');
155 
156       fnd_message.parse_encoded(ENCODED_MESSAGE => l_msg_data,
157                 APP_SHORT_NAME  => l_app_short_name,
158                 MESSAGE_NAME    => l_msg_name);
159 
160       debug('App_short_name: ' || l_app_short_name || ' Msg_name: ' || l_msg_name,'process_mobile_msg');
161 
162       IF ((l_msg_name <> 'WSH_DET_INV_INT_SUBMITTED')) THEN
163      fnd_msg_pub.delete_msg(p_msg_index=>i);
164      debug('Deleted message at position: ' || i,'process_mobile_msg');
165       END IF;
166    END LOOP;
167 EXCEPTION
168    WHEN OTHERS THEN
169       debug('Exception raised!','process_mobile_msg');
170       NULL;
171 END process_mobile_msg;
172 
173 FUNCTION get_fulfillment_base RETURN VARCHAR2 IS
174 BEGIN
175    RETURN g_fulfillment_base;
176 END get_fulfillment_base;
177 
178 FUNCTION get_demand_src_type(p_order_header_id  NUMBER)
179    RETURN NUMBER IS
180    l_debug             NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
181    l_order_source_id   NUMBER;
182 BEGIN
183    IF NOT g_oe_header_type.EXISTS(p_order_header_id) THEN
184       SELECT order_source_id
185         INTO l_order_source_id
186         FROM oe_order_headers_all
187        WHERE header_id = p_order_header_id;
188 
189       IF l_order_source_id = 10 THEN
190          g_oe_header_type(p_order_header_id) := 8;
191       ELSE
192          g_oe_header_type(p_order_header_id) := 2;
193       END IF;
194    END IF;
195    RETURN g_oe_header_type(p_order_header_id);
196 EXCEPTION
197     WHEN OTHERS THEN
198       IF (l_debug = 1) THEN
199          DEBUG('Exception getting order src from OE order headers: ' || SQLERRM, 'get_demand_src_type');
200       END IF;
201 
202       RAISE fnd_api.g_exc_error;
203 END get_demand_src_type;
204 
205 PROCEDURE GET_TRIPSTOP_INFO(x_tripstop_info OUT NOCOPY t_genref
206                               ,p_trip_id IN NUMBER
207                                 ,p_org_id IN NUMBER)  IS
208     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
209 BEGIN
210 /*Bug 2900813: Added code to fetch even Ship Method code and Enforce Ship Method Flag*/
211 OPEN x_tripstop_info FOR
212   SELECT         wt.trip_id
213                 ,wt.name
214                 ,wt.vehicle_item_id
215                 ,msi.concatenated_segments
216                 ,wt.vehicle_num_prefix
217                 ,wt.vehicle_number
218                 ,wts.departure_seal_code
219                 ,WMS_DIRECT_SHIP_PVT.GET_ENFORCE_SHIP
220                 ,WMS_DIRECT_SHIP_PVT.GET_SHIPMETHOD_MEANING(wt.ship_method_code)
221                 ,wt.ship_method_code
222     FROM  wsh_trips_ob_grp_v wt
223         ,wsh_trip_stops_ob_grp_v wts
224         ,mtl_system_items_kfv msi
225   WHERE wt.trip_id = p_trip_id
226   AND   wt.trip_id = wts.trip_id
227   AND   (wt.vehicle_item_id = msi.inventory_item_id(+)
228           AND      msi.organization_id(+) = p_org_id)
229   AND   ROWNUM < 2;
230 END GET_TRIPSTOP_INFO;
231 
232 
233 PROCEDURE GET_DELIVERY_INFO(x_delivery_info OUT NOCOPY t_genref,
234                            p_delivery_id IN NUMBER,
235          p_org_id IN NUMBER)  IS
236 
237     l_debug   NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
238     l_trip_id NUMBER;
239     l_trip_name wsh_trips.name%TYPE;
240 
241 BEGIN
242 open x_delivery_info for
243     SELECT wnd.delivery_id
244           ,wnd.name
245           ,WMS_DIRECT_SHIP_PVT.GET_DELIVERY_LPN(wnd.delivery_id)
246           ,nvl(wnd.net_weight, 0)
247           ,nvl(wnd.gross_weight, 0)
248           ,wnd.weight_uom_code
249           ,wnd.waybill
250           ,WMS_DIRECT_SHIP_PVT.GET_SHIPMETHOD_MEANING(wnd.ship_method_code)
251           ,wnd.ship_method_code
252           ,WMS_DIRECT_SHIP_PVT.GET_FOBLOC_CODE_MEANING(wnd.fob_code)
253           ,wnd.fob_location_id
254           ,WMS_DIRECT_SHIP_PVT.GET_FOB_LOCATION(wnd.fob_location_id)
255           ,wnd.freight_terms_code
256           ,WMS_DIRECT_SHIP_PVT.GET_FREIGHT_TERM(wnd.freight_terms_code)
257           ,WMS_DIRECT_SHIP_PVT.GET_FOB_LOCATION(wnd.INTMED_SHIP_TO_LOCATION_ID)
258           ,WMS_DIRECT_SHIP_PVT.GET_BOL(wnd.delivery_id)
259           ,nvl(wnd.status_code,'OP')
260           ,WMS_DIRECT_SHIP_PVT.GET_ENFORCE_SHIP
261           ,wts1.trip_id --2767767
262 	  ,wnd.fob_code  --Bug#9668537,9668537 and 9399092
263 FROM wsh_new_deliveries_ob_grp_v wnd,
264       -- 2767767
265 (SELECT wdl.delivery_id,wts.trip_id
266         FROM wsh_delivery_legs_ob_grp_v wdl,
267              wsh_trip_stops_ob_grp_v wts
268         WHERE wdl.delivery_id=p_delivery_id
269         AND   wdl.pick_up_stop_id=wts.stop_id
270         AND ROWNUM=1) wts1 -- end 2767767
271 WHERE wnd.delivery_id = p_delivery_id
272 and   wnd.delivery_id=wts1.delivery_id(+); --2767767
273 
274 /* bug # 2994098 */
275 -- If a delivery is ship confirmed from STF without closing the trip
276 -- then trip id should be updated in wstt because user may try to close the trip from mobile.
277 BEGIN
278     SELECT wts.trip_id,wt.name
279     INTO l_trip_id,l_trip_name
280         FROM wsh_delivery_legs_ob_grp_v wdl,
281              wsh_trip_stops_ob_grp_v wts,
282          wsh_trips_ob_grp_v wt
283         WHERE wdl.delivery_id=p_delivery_id
284         AND wdl.pick_up_stop_id=wts.stop_id
285          AND wt.trip_id=wts.trip_id
286         AND ROWNUM=1;
287     IF l_trip_id IS NOT NULL THEN
288         UPDATE wms_shipping_transaction_temp
289         SET trip_id=l_trip_id,trip_name=l_trip_name
290         WHERE delivery_id=p_delivery_id and trip_id is null;
291     END IF;
292 
293  EXCEPTION
294     WHEN OTHERS THEN
295     debug('trip_id update on wstt failed','get_delivery_info');
296         NULL;
297  END;
298 END GET_DELIVERY_INFO;
299 
300 /* bug 11828449 */
301 FUNCTION check_direct_ship_for_lpn (p_lpn_id IN NUMBER) RETURN BOOLEAN IS
302   l_debug         NUMBER            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
303   l_direct_ship      VARCHAR(1)       :=  'N';
304 
305 BEGIN
306    IF (l_debug = 1) THEN
307       DEBUG('lpn id:'||p_lpn_id, 'check_direct_ship_for_lpn');
308    END IF;
309    IF (p_lpn_id IS NOT NULL) THEN
310     l_direct_ship   :=  'N';
311     BEGIN
312 	  SELECT 'Y' INTO l_direct_ship
313       FROM  wms_shipping_transaction_temp wstt
314       WHERE wstt.outermost_lpn_id = (SELECT wlpn.outermost_lpn_id FROM wms_license_plate_numbers wlpn WHERE wlpn.lpn_id = p_lpn_id)
315       and wstt.direct_ship_flag = 'Y'
316       and rownum <2;
317     EXCEPTION
318        WHEN no_data_found THEN
319 	    IF (l_debug = 1) THEN
320 	       debug('This lpn does not belong to Direct Ship');
321 	    END IF;
322 	    l_direct_ship   :=  'N';
323        WHEN OTHERS THEN
324 	  IF (l_debug = 1) THEN
325 	     DEBUG('Exception occured','check_direct_ship_for_lpn');
326 	  END IF;
327 
328     RAISE fnd_api.g_exc_error;
329       END;
330    END IF;
331 
332    IF (l_direct_ship ='Y') THEN
333       RETURN TRUE;
334     ELSE
335       RETURN FALSE;
336    END IF;
337      END; --check_direct_ship_for_lpn
338 
339 /* bug 11828449 */
340 
341 -- This Function concatenates all LPN NAME that are part of Delivery
342 FUNCTION GET_DELIVERY_LPN(p_delivery_id IN NUMBER ) RETURN VARCHAR2 IS
343   CURSOR delivery_lpn IS
344   SELECT distinct license_plate_number
345   FROM  wms_shipping_transaction_temp wstt
346        ,wms_license_plate_numbers wlpn
347   WHERE wstt.delivery_id = p_delivery_id
348   AND   wstt.outermost_lpn_id = wlpn.lpn_id
349   AND   wstt.direct_ship_flag = 'Y';
350 l_lpn_string  VARCHAR2(30);
351 l_lpn_concat  VARCHAR2(2000);
352     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
353 BEGIN
354   OPEN delivery_lpn;
355   LOOP
356     FETCH delivery_lpn into l_lpn_string;
357         EXIT WHEN delivery_lpn%NOTFOUND;
358    IF delivery_lpn%ROWCOUNT = 1 THEN
359      l_lpn_concat := l_lpn_string;
360    ELSE
361         l_lpn_concat := l_lpn_concat || '|' || l_lpn_string;
362    END IF;
363   END LOOP;
364   CLOSE delivery_lpn;
365   RETURN l_lpn_concat;
366 END;  -- GET_DELIVERY_LPN
367 
368 FUNCTION GET_SHIPMETHOD_MEANING(p_ship_method_code  IN  VARCHAR2)
369 RETURN  VARCHAR2  IS
370 l_ship_method_meaning VARCHAR2(80);
371     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
372 BEGIN
373   IF p_ship_method_code IS NULL THEN
374     RETURN '';
375   ELSE
376      SELECT meaning
377      INTO l_ship_method_meaning
378      FROM fnd_lookup_values_vl
379      WHERE lookup_type = 'SHIP_METHOD'
380      AND   view_application_id = 3
381      AND   lookup_code = p_ship_method_code;
382   END IF;
383      RETURN l_ship_method_meaning;
384   EXCEPTION
385      WHEN OTHERS THEN
386         RETURN '';
387 END GET_SHIPMETHOD_MEANING;
388 
389 FUNCTION GET_FOBLOC_CODE_MEANING(p_fob_code  IN  VARCHAR2)
390 RETURN VARCHAR2 IS
391  l_fob_loc_meaning  VARCHAR2(80);
392     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
393 BEGIN
394   IF p_fob_code IS NULL THEN
395      RETURN '';
396   ELSE
397      SELECT meaning
398          INTO l_fob_loc_meaning
399      FROM  ar_lookups
400      WHERE lookup_type = 'FOB'
401      AND SYSDATE BETWEEN nvl(start_date_active,sysdate) AND nvl(end_date_active,sysdate)
402      AND  enabled_flag = 'Y'
403      AND  lookup_code = p_fob_code;
404   END IF;
405     RETURN l_fob_loc_meaning;
406 EXCEPTION
407      WHEN OTHERS THEN
408         RETURN '';
409 END GET_FOBLOC_CODE_MEANING;
410 
411 FUNCTION GET_FOB_LOCATION(p_fob_location_id IN NUMBER)
412 RETURN VARCHAR2  IS
413  l_fob_location VARCHAR2(80);
414     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
415 BEGIN
416   IF p_fob_location_id IS NULL THEN
417     RETURN '';
418   ELSE
419     SELECT description
420         INTO l_fob_location
421         FROM wsh_hr_locations_v
422    WHERE location_id = p_fob_location_id;
423   END IF;
424     RETURN l_fob_location;
425 EXCEPTION
426     WHEN OTHERS THEN
427          RETURN '';
428 END GET_FOB_LOCATION;
429 
430 FUNCTION GET_FREIGHT_TERM(p_freight_term_code VARCHAR2)
431 RETURN VARCHAR2 IS
432   l_freight_term  VARCHAR2(80);
433     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
434 BEGIN
435   IF p_freight_term_code IS NULL THEN
436      RETURN '';
437   ELSE
438     SELECT freight_terms
439     INTO l_freight_term
440     FROM oe_frght_terms_active_v
441     WHERE freight_terms_code = p_freight_term_code;
442   END IF;
443     RETURN      l_freight_term;
444 EXCEPTION
445     WHEN OTHERS THEN
446          RETURN '';
447 END GET_FREIGHT_TERM;
448 
449 FUNCTION GET_BOL(p_delivery_id  NUMBER)
450 RETURN NUMBER IS
451   l_BOL         NUMBER;
452     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
453 BEGIN
454   IF p_delivery_id IS NULL THEN
455      RETURN '';
456   ELSE
457     SELECT wdi.SEQUENCE_NUMBER
458     INTO l_BOL
459     FROM wsh_document_instances wdi
460             ,wsh_delivery_legs_ob_grp_v      wdl
461         WHERE wdl.delivery_id = p_delivery_id
462         AND   wdi.entity_id   = wdl.delivery_leg_id
463         AND   wdi.entity_name = 'WSH_DELIVERY_LEGS'
464         AND   rownum < 2;
465         -- Right now datamodel sugget that  WDI - WDL >- WND. So for 1 delivery multiple delivery leg
466         -- multiple BOL. Desktop Application does not support this still I would like to show one BOL
467   END IF;
468     RETURN      l_BOL;
469 EXCEPTION
470     WHEN OTHERS THEN
471          RETURN '';
472 END GET_BOL;
473 
474 FUNCTION get_enforce_ship RETURN VARCHAR2 IS
475    l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
476    x_enforce_ship VARCHAR2(1);
477    x_return_status VARCHAR2(1);
478    x_param_info WSH_SHIPPING_PARAMS_GRP.Global_Params_Rec;
479 BEGIN
480    IF l_debug = 1 THEN
481       debug('Calling Shipping API get_global_parameters','get_enforce_ship');
482    END IF;
483 
484    WSH_SHIPPING_PARAMS_GRP.get_global_parameters
485      (x_global_param_info=>x_param_info,
486       x_return_status => x_return_status);
487 
488    x_enforce_ship := x_param_info.ENFORCE_SHIP_METHOD;
489 
490    IF (l_debug = 1) THEN
491       debug('Shipping API returned status: ' || x_return_status,'get_enforce_ship');
492       debug('Enforce ship Y/N : ' || x_enforce_ship, 'get_enforce_ship');
493    END IF;
494 
495    IF x_enforce_ship IS NULL THEN
496       x_enforce_ship := 'N';
497    END IF;
498 
499    RETURN x_enforce_ship;
500 EXCEPTION
501    WHEN OTHERS THEN
502       IF (l_debug = 1) THEN
503      debug('Others exception raised','get_enforce_ship');
504       END IF;
505       RETURN 'N';
506 END get_enforce_ship;
507 
508 PROCEDURE CHECK_DELIVERY(x_return_status        OUT NOCOPY VARCHAR2
509                         ,x_msg_count            OUT NOCOPY NUMBER
510                         ,x_msg_data             OUT NOCOPY VARCHAR2
511                         ,x_error_code           OUT NOCOPY NUMBER
512                         ,p_delivery_id          IN  NUMBER
513                         ,p_org_id               IN  NUMBER
514                         ,p_dock_door_id         IN  NUMBER
515                  )
516 IS
517 temp_val         NUMBER;
518 l_ship_set       VARCHAR2(2000) := NULL;
519 l_error_msg      VARCHAR2(20000) := NULL;
520 l_return_status  VARCHAR2(1);
521 l_msg_data       VARCHAR2(3000);
522 l_msg_count      NUMBER;
523 x_error_msg      VARCHAR2(20000);
524 x_delivery_name  VARCHAR2(30);
525 x_outermost_lpn  VARCHAR2(10);
526 l_loaded_dock_door  VARCHAR2(2000);
527 unspec_ship_set_exists  EXCEPTION;
528 incomplete_delivery     EXCEPTION;
529 no_ship_method_code     EXCEPTION;
530 check_ord_line_split    EXCEPTION;
531 delivery_lines_mix      EXCEPTION;
532 l_del_name            VARCHAR2(30);
533 l_enforce_ship_method  VARCHAR2(1);
534 l_ship_method_code     VARCHAR2(30);
535 l_missing_count        NUMBER;
536 CURSOR lpn_in_other_dock(p_delivery_id  NUMBER) IS
537   SELECT milk.concatenated_segments
538        , wstt.outermost_lpn
539   FROM mtl_item_locations_kfv milk
540       ,wms_shipping_transaction_temp wstt
541   WHERE wstt.delivery_id           = p_delivery_id
542   AND   wstt.organization_id       = p_org_id
543   AND   wstt.dock_appoint_flag     = 'N'
544   AND   wstt.direct_ship_flag      = 'Y'
545   AND   wstt.dock_door_id          <> p_dock_door_id
546   AND   milk.organization_id       = p_org_id
547   AND   milk.inventory_location_id =wstt.dock_door_id;
548 
549     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
550 BEGIN
551    IF (l_debug = 1) THEN
552       debug('In check deliveries','Check_Delivery');
553       debug('p_delivery_id  : ' || p_delivery_id ,'Check_Delivery');
554       debug('p_org_id       : ' || p_org_id      ,'Check_Delivery');
555       debug('p_dock_door_id : ' || p_dock_door_id,'Check_Delivery');
556    END IF;
557    x_return_Status := fnd_api.g_ret_sts_success;
558    x_error_code := 0;  -- everything is fine
559    -- This procedure does all validation for a Delivery before it can be CONFIRMED
560    -- x_error_code = 1  Missing Item Calling program should call Missing Item cursor
561    -- x_error_code = 2  Material Status does not allow Sales Order Issue transaction
562    -- x_error_code = 3  Shipset Validation Failed. x_ship_set will be populated with
563    -- Shipset Name, show it on mobile page
564    -- x_error_code = 4  For the delivery there are some LPNs loaded on different Dock
565    -- x_error_code = 5  Delivery could not be locked. Error
566    -- x_error_code = 6  No Ship_method_code is provided when it is required. Error
567    -- x_error_code = 7  Lines were split in order management . Error
568    --x_error_code  =8   All delivery Delivery Lines are not from the direct_ship source
569    -- Locked the record first, so that others will not able to ship the same delivery
570    IF p_delivery_id IS NOT NULL THEN
571       BEGIN
572          SELECT name
573          INTO l_del_name
574          FROM wsh_new_deliveries_ob_grp_v
575          WHERE delivery_id =p_delivery_id;
576       EXCEPTION
577          WHEN no_data_found THEN
578             NULL;
579       END;
580    END IF;
581    BEGIN
582       SELECT 1
583       INTO temp_val
584       FROM wsh_new_deliveries_ob_grp_v
585       WHERE delivery_id = p_delivery_id
586       FOR UPDATE NOWAIT;
587       IF (l_debug = 1) THEN
588          debug('Lock delivery successful','Check_Delivery');
589       END IF;
590    EXCEPTION WHEN others THEN
591           x_return_Status := fnd_api.g_ret_sts_error;
592           fnd_message.set_name('WMS','WMS_DEL_LOCK_FAILURE');
593           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
594           /*Failed to aquire locks on wsh_new_deliveries for <DELIVERY_NAME>*/
595           fnd_msg_pub.add;
596           fnd_msg_pub.count_and_get( p_count => x_msg_count,
597                                      p_data  => x_msg_data
598                                     );
599           /*Failed to lock the delivery */
600           x_error_code := 5;
601           IF (l_debug = 1) THEN
602              debug('Lock delivery unsuccessful','Check_Delivery');
603           END IF;
604           RETURN;
605    END;
606 
607    -- First check if the entire delivery is ready to be ship confirmed
608    -- If No means there are some Missing Item  exists. Show Missing Item page
609 /*   INV_SHIPPING_TRANSACTION_PUB.CHECK_ENTIRE_EZ_DELIVERY(
610                                  p_delivery_id     => p_delivery_id,
611                                  x_return_Status   => l_return_status,
612                                  x_error_msg       => l_error_msg);*/
613      wms_direct_ship_pvt.CHECK_MISSING_ITEM_CUR(
614                                  p_delivery_id  => p_delivery_id
615                                  ,p_dock_door_id => p_dock_door_id
616                                  ,p_organization_id => p_org_id
617                                  ,x_return_Status  => l_return_status
618                                  ,x_missing_count  => l_missing_count
619                               );
620 
621         IF l_return_Status =  'S' AND l_missing_count > 0 then
622      IF (l_debug = 1) THEN
623         debug('There are Missing Items','CHECK_DELIVERY');
624      END IF;
625      fnd_msg_pub.count_and_get( p_count => x_msg_count,
626                                 p_data  => x_msg_data
627                                 );
628      x_return_Status := fnd_api.g_ret_sts_error;
629      x_error_code := 1; -- not entire delivery is ready
630      rollback;
631      return;
632    ELSIF l_return_status <> 'S' THEN
633      x_return_Status := fnd_api.g_ret_sts_unexp_error;
634      rollback;
635      return;
636    END IF;
637 
638    IF (l_debug = 1) THEN
639       debug('Call to Iwms_direct_ship_pvt.CHECK_MISSING_ITEM_CUR successful ','Check_Delivery');
640    END IF;
641         -- Check Delivery Status (Material Status
642    INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS
643              (p_delivery_id    => p_delivery_id
644              ,x_return_Status => l_return_status
645              ,x_error_msg     => x_error_msg);
646       IF l_return_status = fnd_api.g_ret_sts_error THEN
647          fnd_message.set_name('WMS','WMS_NO_ISSUE_TRX');
648           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
649          /*Material Status prevents the sales order issue transaction for one
650            or more items in the delivery <DELIVERY_NAME>*/
651          FND_MSG_PUB.ADD;
652          IF (l_debug = 1) THEN
653             debug('Call to INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS ' ||
654                   'returned with Status E '||P_DELIVERY_ID,'Check_Delivery');
655          END IF;
656          x_error_code := 2;      -- status doesn't allow ship confirm
657         RAISE incomplete_delivery;
658       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
659           fnd_message.set_name('WMS','WMS_NO_ISSUE_TRX');
660           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
661          /*Material Status prevents the sales order issue transaction for one or
662            more items in the delivery <DELIVERY_NAME>*/
663           FND_MSG_PUB.ADD;
664           x_error_code := 2;
665          RAISE fnd_api.g_exc_unexpected_error;
666       END IF;
667       IF (l_debug = 1) THEN
668          debug('INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS was successful','Check_Delivery');
669       END IF;
670         -- Check for Ship Set
671         -- x_return_status (l_return_status = 'C' then Shipset was Null,
672         -- 'E' means Shipset Validation failed. 'U' means Unexpected Error
673         WMS_SHIPPING_TRANSACTION_PUB.SHIP_SET_CHECK( p_trip_id => null,
674                             p_dock_door_id => p_dock_door_id,
675                             p_organization_id => p_org_id,
676                             x_ship_set      => l_ship_set,
677                             x_return_Status => l_return_status,
678                             x_error_msg     => l_error_msg,
679                             p_direct_ship_flag => 'Y' );
680      IF l_return_status = fnd_api.g_ret_sts_error THEN
681           IF (l_debug = 1) THEN
682              debug('WMS_SHIPPING_TRANSACTION_PUB.ship_set_check returned with status' ||
683                    ' E- shipset  '||l_ship_set,'Check_Delivery');
684           END IF;
685           FND_MESSAGE.SET_NAME('INV', 'WMS_WSH_SHIPSET_FORCED');
686           FND_MESSAGE.SET_TOKEN('SHIP_SET_NAME', l_ship_set);
687           FND_MSG_PUB.ADD;
688           RAISE unspec_ship_set_exists;
689       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
690           FND_MESSAGE.SET_NAME('INV', 'WMS_SHIPSET_FAILED');
691           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
692           /*Ship set validation for the delivery <DELIVERY_NAME> has failed*/
693           FND_MSG_PUB.ADD;
694           IF (l_debug = 1) THEN
695              debug('WMS_SHIPPING_TRANSACTION_PUB.ship_set_check returned with status U','Check_Delivery');
696           END IF;
697            RAISE fnd_api.g_exc_unexpected_error;
698       END IF;
699       IF (l_debug = 1) THEN
700          debug('Call to WMS_SHIPPING_TRANSACTION_PUB.SHIP_SET_CHECK was successful ','Check_Delivery');
701       END IF;
702         -- Check if delivery has some delivery_details (LPNs) loaded on different dock door
703      OPEN lpn_in_other_dock(p_delivery_id);
704      FETCH lpn_in_other_dock INTO l_loaded_dock_door,x_outermost_lpn;
705      IF lpn_in_other_dock%FOUND THEN
706          IF (l_debug = 1) THEN
707             debug('Cursor lpn_in_other_dock returned a row ','Check_Delivery');
708          END IF;
709          BEGIN
710               SELECT name
711                  INTO x_delivery_name
712                  FROM wsh_new_deliveries_ob_grp_v
713                  WHERE delivery_id = p_delivery_id;
714               FND_MESSAGE.SET_NAME('WMS','WMS_LPN_OTHER_DOCK');
715               FND_MESSAGE.SET_TOKEN('LPN_NAME',x_outermost_lpn);
716               FND_MESSAGE.SET_TOKEN('DOCK',l_loaded_dock_door);
717               /* LPN <LPN_NAME> in this delivery <DELIVERY_NAME> is loaded at another dock door <DOCK>*/
718               FND_MSG_PUB.ADD;
719                fnd_msg_pub.count_and_get( p_count => x_msg_count,
720                                           p_data  => x_msg_data
721                                         );
722               l_return_status := fnd_api.g_ret_sts_error;
723               x_error_code    :=  4;
724               IF (l_debug = 1) THEN
725                  debug('lpn is in other dock door lpn,dock_door and del_id are '
726                         || x_outermost_lpn ||' - ' ||l_loaded_dock_door || ' - '
727                         ||p_delivery_id, 'Check_Delivery');
728               END IF;
729          EXCEPTION
730             WHEN no_data_found THEN
731                IF (l_debug = 1) THEN
732                   debug('In No data found -Check_Deliveries','Check_Delivery');
733                END IF;
734                null;
735          END;
736      END IF;
737      CLOSE lpn_in_other_dock;
738 
739      wms_direct_ship_pvt.chk_del_for_direct_ship(x_return_status =>l_return_status
740                                                  ,x_msg_count    => l_msg_count
741                                                  ,x_msg_data     => l_msg_data
742                                                  ,p_delivery_id  => p_delivery_id
743                                                     );
744       IF l_return_status = fnd_api.g_ret_sts_error THEN
745          IF (l_debug = 1) THEN
746             debug('Call to chk_del_for_direct_ship returned with Status E '||P_DELIVERY_ID,'Check_Delivery');
747          END IF;
748          RAISE delivery_lines_mix;
749       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
750           IF (l_debug = 1) THEN
751              debug('Call to chk_del_for_direct_ship returned with Status U '||P_DELIVERY_ID,'Check_Delivery');
752           END IF;
753           RAISE fnd_api.g_exc_unexpected_error;
754       END IF;
755    EXCEPTION
756         WHEN unspec_ship_set_exists THEN
757             x_return_status := fnd_api.g_ret_sts_error;
758             x_error_code := 3;
759             IF (l_debug = 1) THEN
760                debug('In exception unspec_ship_set_exists -Check_Deliveries - errorcode '
761                       ||x_error_code,'Check_Delivery');
762             END IF;
763             --  Get message count and data
764             fnd_msg_pub.count_and_get
765               (  p_count => x_msg_count
766                , p_data  => x_msg_data);
767 
768         WHEN incomplete_delivery THEN
769            x_return_status := fnd_api.g_ret_sts_error;
770            fnd_msg_pub.count_and_get( p_count => x_msg_count,
771                                     p_data  => x_msg_data
772                                   );
773             x_error_code := 2;
774             IF (l_debug = 1) THEN
775                debug('In exception incomplete_delivery -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
776             END IF;
777 
778         WHEN no_ship_method_code THEN
779            x_return_status := fnd_api.g_ret_sts_error;
780            FND_MESSAGE.SET_NAME('WMS','WMS_SHIP_METHOD_CODE');
781            FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
782            /* No Ship method code provided for the delivery DELIVERY_NAME .This is required */
783            FND_MSG_PUB.ADD;
784            fnd_msg_pub.count_and_get( p_count => x_msg_count,
785                                     p_data  => x_msg_data
786                                   );
787             IF (l_debug = 1) THEN
788                debug('In exception no_ship_method_code -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
789             END IF;
790             x_error_code := 6;
791         WHEN check_ord_line_split THEN
792            x_return_status := fnd_api.g_ret_sts_error;
793            x_error_code := 7;
794            fnd_msg_pub.count_and_get
795               (  p_count => x_msg_count
796                , p_data  => x_msg_data
797                );
798         WHEN delivery_lines_mix THEN
799            x_return_status := fnd_api.g_ret_sts_error;
800            x_error_code := 8;
801             FND_MESSAGE.SET_NAME('WMS','WMS_DEL_LINES_MIX');
802            FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
803            /* The Delivery also has lines that were not staged through Direct Ship*/
804            FND_MSG_PUB.ADD;
805            fnd_msg_pub.count_and_get
806               (  p_count => x_msg_count
807                , p_data  => x_msg_data
808                );
809         WHEN fnd_api.g_exc_unexpected_error THEN
810          x_return_status := fnd_api.g_ret_sts_unexp_error ;
811           --  Get message count and data
812             fnd_msg_pub.count_and_get
813               (  p_count => x_msg_count
814                , p_data  => x_msg_data
815                );
816             IF (l_debug = 1) THEN
817                debug('In unexpected  -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
818             END IF;
819 
820         WHEN OTHERS THEN
821             x_return_status := fnd_api.g_ret_sts_unexp_error;
822             fnd_msg_pub.count_and_get
823               (  p_count => x_msg_count
824                , p_data  => x_msg_data
825                );
826                x_error_code := 5;
827             IF (l_debug = 1) THEN
828                debug('In exception when others -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
829             END IF;
830 
831 END CHECK_DELIVERY;
832 
833 PROCEDURE UPDATE_DELIVERY(
834               x_return_status        OUT NOCOPY VARCHAR2
835              ,x_msg_count            OUT NOCOPY NUMBER
836              ,x_msg_data             OUT NOCOPY VARCHAR2
837              ,p_delivery_id             IN  NUMBER
838              ,p_net_weight                 IN  NUMBER
839              ,p_gross_weight            IN  NUMBER
840              ,p_wt_uom_code             IN  VARCHAR2
841              ,p_waybill                    IN  VARCHAR2
842              ,p_ship_method_code      IN  VARCHAR2
843              ,p_fob_code                   IN  VARCHAR2
844              ,p_fob_location_id      IN  NUMBER
845              ,p_freight_term_code     IN         VARCHAR2
846              ,p_freight_term_name     IN  VARCHAR2
847              ,p_intmed_shipto_loc_id  IN  NUMBER
848                          )IS
849 
850 l_init_msg_list                  VARCHAR2(1) :=FND_API.G_TRUE;
851 l_return_status                  VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
852 l_msg_count                      NUMBER;
853 l_msg_data                       VARCHAR2(20000);
854 l_fob_location_id      NUMBER := P_fob_location_id;
855 l_intmed_shipto_loc_id NUMBER :=p_intmed_shipto_loc_id;
856 
857 
858 l_freight_cost_rec                WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
859 l_delivery_info                   WSH_DELIVERIES_PUB.Delivery_Pub_Rec_Type;
860 l_row_id                          ROWID;
861 l_delivery_id                     NUMBER;
862 l_freight_cost_id                 NUMBER;
863 l_delivery_name                   VARCHAR2(30);
864 l_name                            VARCHAR2(30);
865 l_del_name                        VARCHAR2(30); /* fOR MESSAGE TOKEN */
866 l_status_code                     VARCHAR2(3);
867 l_trip_id                         NUMBER; -- 2767767
868 
869 CURSOR delivery_freight IS
870 SELECT  ROWID
871         ,FREIGHT_COST_TYPE_ID
872         ,CURRENCY_CODE
873                   ,FREIGHT_AMOUNT
874                   ,CONVERSION_TYPE
875         FROM  WMS_FREIGHT_COST_TEMP
876         WHERE delivery_id = p_delivery_id
877           AND FREIGHT_COST_ID IS NULL
878         FOR UPDATE OF FREIGHT_COST_ID;
879     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
880 BEGIN
881     x_return_status := fnd_api.g_ret_sts_success;
882 
883  IF p_delivery_id IS NOT NULL THEN
884      SELECT status_code
885      INTO l_status_code
886      FROM wsh_new_deliveries_ob_grp_v
887      WHERE delivery_id = p_delivery_id;
888 
889      IF l_status_code IN ('CL','CO','IT') THEN
890         RETURN ;
891      END IF;
892   ELSE
893      RETURN;
894   END IF;
895 
896     IF (l_debug = 1) THEN
897        debug('In Update delivery procedure ','update_Delivery');
898        DEBUG('p_delivery_id          : '||p_delivery_id,'update_Delivery');
899        DEBUG('p_ship_method_code     : '||p_ship_method_code,'update_Delivery');
900        DEBUG('p_FREIGHT_TERM_CODE    : '||p_FREIGHT_TERM_CODE,'update_Delivery');
901        DEBUG('p_fob_code             : '||p_fob_code,'update_Delivery');
902        DEBUG('p_fob_location_id      : '||p_fob_location_id,'update_Delivery');
903        DEBUG('p_waybill              : '||p_waybill,'update_Delivery');
904        DEBUG('p_net_weight           : '||p_net_weight,'update_Delivery');
905        DEBUG('p_gross_weight         : '||p_gross_weight,'update_Delivery');
906        DEBUG('p_wt_uom_code          : '||p_wt_uom_code,'update_Delivery');
907        DEBUG('p_intmed_shipto_loc_id : '||p_intmed_shipto_loc_id,'update_Delivery');
908     END IF;
909 
910     IF p_delivery_id IS NOT NULL  THEN
911        BEGIN
912           SELECT name
913              INTO l_del_name
914              FROM wsh_new_deliveries_ob_grp_v
915              WHERE delivery_id = p_delivery_id;
916        EXCEPTION
917           WHEN NO_DATA_FOUND THEN
918              NULL;
919        END;
920     END IF;
921 
922 OPEN delivery_freight;
923 LOOP
924   FETCH delivery_freight into l_row_id
925                              ,l_freight_cost_rec.FREIGHT_COST_TYPE_ID
926                              ,l_freight_cost_rec.CURRENCY_CODE
927                              ,l_freight_cost_rec.UNIT_AMOUNT
928                              ,l_freight_cost_rec.CONVERSION_TYPE_CODE;
929 
930        l_freight_cost_rec.delivery_ID     :=p_delivery_id;
931        l_freight_cost_rec.action_code     := 'CREATE';
932   EXIT WHEN delivery_freight%NOTFOUND;
933 
934   WSH_FREIGHT_COSTS_PUB.Create_Update_Freight_Costs (
935                          p_api_version_number     =>1.0
936                        , p_init_msg_list          =>l_init_msg_list
937                        , p_commit                 => FND_API.G_FALSE
938                        , x_return_status          => l_return_status
939                        , x_msg_count              => l_msg_count
940                        , x_msg_data               => l_msg_data
941                        , p_pub_freight_costs      => l_freight_cost_rec
942                        , p_action_code            => 'CREATE'
943                        , x_freight_cost_id        => l_freight_cost_id
944                        );
945 
946   IF( l_return_status IN(fnd_api.g_ret_sts_error)) THEN
947      FND_MESSAGE.SET_NAME('WMS','WMS_CREATE_FREIGHT_FAIL');
948      FND_MESSAGE.SET_TOKEN('OBJ','Delivery');
949      FND_MESSAGE.SET_TOKEN('VAL',l_del_name);
950      /* Creation of freight cost for OBJ VAL has failed  */
951      fnd_msg_pub.add;
952      IF (l_debug = 1) THEN
953         debug('Update of Freight Cost for Del is failed ','UPDATE_DELIVERY');
954      END IF;
955      raise FND_API.G_EXC_ERROR;
956   ELSIF (l_return_status = 'U' ) THEN
957      FND_MESSAGE.SET_TOKEN('OBJ','Delivery');
958      FND_MESSAGE.SET_TOKEN('VAL',l_del_name);
959      /* Creation of freight cost for <OBJ> <VAL> has failed  */
960       fnd_msg_pub.add;
961        IF (l_debug = 1) THEN
962           debug('Update of Freight Cost for Del is succ ','UPDATE_DELIVERY');
963        END IF;
964        raise fnd_api.g_exc_unexpected_error;
965   END IF;
966 
967   UPDATE WMS_FREIGHT_COST_TEMP
968   SET freight_cost_id = l_freight_cost_id,
969       last_update_date=  SYSDATE,
970       last_updated_by =  FND_GLOBAL.USER_ID
971   WHERE rowid = l_row_id;
972 
973 END LOOP;
974 CLOSE delivery_freight;
975    IF l_intmed_shipto_loc_id =0 THEN
976       l_intmed_shipto_loc_id := NULL;
977    END IF;
978    IF l_fob_location_id =0  THEN
979        l_fob_location_id:=NULL;
980    END IF;
981    IF (p_net_weight             IS NOT NULL OR
982        p_gross_weight           IS NOT NULL OR
983        p_wt_uom_code            IS NOT NULL OR
984        p_waybill                IS NOT NULL OR
985        p_ship_method_code       IS NOT NULL OR
986        p_fob_code               IS NOT NULL OR
987        p_fob_location_id        IS NOT NULL OR
988        p_freight_term_code      IS NOT NULL OR
989        p_freight_term_name      IS NOT NULL OR
990        p_intmed_shipto_loc_id   IS NOT NULL
991           )  THEN
992 
993        -- 2767767
994        -- if delivery is assigned to trip update of ship method is not allowed
995        BEGIN
996               SELECT distinct wts.trip_id
997               INTO l_trip_id
998               FROM wsh_delivery_legs_ob_grp_v wdl,
999                    wsh_trip_stops_ob_grp_v wts
1000               WHERE wdl.delivery_id=p_delivery_id
1001               AND   wdl.pick_up_stop_id=wts.stop_id
1002               AND rownum=1;
1003             IF (l_debug = 1) THEN
1004                 debug('Delivery '||p_delivery_id||' is assigned to trip '||l_trip_id,'Update Delivery');
1005              END IF;
1006           EXCEPTION
1007               WHEN NO_DATA_FOUND THEN
1008               l_trip_id:=NULL;
1009           END;
1010 
1011 
1012           l_delivery_info.DELIVERY_ID                            := p_delivery_id;
1013           -- 2767767
1014           IF l_trip_id IS  NULL THEN
1015              l_delivery_info.SHIP_METHOD_CODE                       := p_ship_method_code;
1016               IF (l_debug = 1) THEN
1017                 debug('Delivery ' ||p_delivery_id||' is assigned to trip '
1018                       ||l_trip_id ||' and hence not updating ship method','Update Delivery');
1019               END IF;
1020           END IF;
1021           l_delivery_info.FREIGHT_TERMS_CODE        := p_freight_term_code;
1022           l_delivery_info.FOB_CODE                  := p_fob_code;
1023           l_delivery_info.FOB_LOCATION_ID           := l_fob_location_id;
1024           l_delivery_info.WAYBILL                   := p_waybill;
1025           l_delivery_info.NET_WEIGHT                := p_net_weight;
1026           l_delivery_info.GROSS_WEIGHT              := p_gross_weight;
1027           l_delivery_info.WEIGHT_UOM_CODE           := p_wt_uom_code;
1028           l_delivery_info.INTMED_SHIP_TO_LOCATION_ID:=l_intmed_shipto_loc_id;
1029 
1030            WSH_DELIVERIES_PUB.Create_Update_Delivery(
1031                        p_api_version_number                => 1.0
1032                       ,p_init_msg_list                     => l_init_msg_list
1033                       ,x_return_status                     => l_return_status
1034                       ,x_msg_count                         => l_msg_count
1035                       ,x_msg_data                          => l_msg_data
1036                       ,p_action_code                       => 'UPDATE'
1037                       ,p_delivery_info                     => l_delivery_info
1038                       ,p_delivery_name                     => l_delivery_name
1039                       ,x_delivery_id                       => l_delivery_id
1040                       ,x_name                              => l_name);
1041 
1042              IF( l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
1043                 IF (l_debug = 1) THEN
1044                    debug('Create _Update_Delivery has errored ','UPDATE_DELIVERY');
1045                 END IF;
1046                 FND_MESSAGE.SET_NAME('WMS','WMS_UPDATE_DELIVERY_FAILED');
1047                 FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1048                /* Updation of delivery <DELIVERY_NAME> has failed */
1049                 fnd_msg_pub.add;
1050                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1051              ELSIF (l_return_status IN (fnd_api.g_ret_sts_error)) THEN
1052                 FND_MESSAGE.SET_NAME('WMS','UPDATE_DELIVERY_FAILED');
1053                 FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1054                /* Updation of delivery <DELIVERY_NAME> has failed */
1055                 fnd_msg_pub.add;
1056                 raise FND_API.G_EXC_ERROR;
1057              END IF;
1058    END IF;
1059    IF (l_debug = 1) THEN
1060       debug('Update delivery completed successfully');
1061    END IF;
1062 EXCEPTION
1063    WHEN fnd_api.g_exc_error THEN
1064       x_return_status := fnd_api.g_ret_sts_error;
1065        fnd_msg_pub.count_and_get( p_count => x_msg_count,
1066                                   p_data  => x_msg_data
1067                                   );
1068       ROLLBACK;
1069 
1070       IF delivery_freight%ISOPEN THEN
1071              CLOSE delivery_freight;
1072       END IF;
1073 
1074    WHEN fnd_api.g_exc_unexpected_error THEN
1075       x_return_status := fnd_api.g_ret_sts_unexp_error;
1076        fnd_msg_pub.count_and_get( p_count => x_msg_count,
1077                                   p_data  => x_msg_data
1078                                   );
1079       ROLLBACK;
1080 
1081       IF delivery_freight%ISOPEN THEN
1082              CLOSE delivery_freight;
1083       END IF;
1084       IF (l_debug = 1) THEN
1085          debug('Update of Delivery has failed :Unexpected Error','Update Delivery');
1086       END IF;
1087 
1088    WHEN OTHERS THEN
1089       ROLLBACK;
1090       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1091         fnd_msg_pub.count_and_get( p_count => x_msg_count,
1092                                    p_data  => x_msg_data
1093                                 );
1094       IF delivery_freight%ISOPEN THEN
1095              CLOSE delivery_freight;
1096       END IF;
1097       IF (l_debug = 1) THEN
1098          debug('Update of Delivery has failed : Unexpected Error '||SQLERRM,'Update_Delivery');
1099       END IF;
1100 
1101           IF SQLCODE IS NOT NULL THEN
1102             IF (l_debug = 1) THEN
1103                debug(SQLCODE,'Update_Delivery');
1104             END IF;
1105           END IF;
1106 
1107 END; -- UPDATE_DELIVERY
1108 
1109 PROCEDURE MISSING_ITEM_CUR( x_missing_item_cur  OUT NOCOPY t_genref
1110                             ,p_delivery_id         IN  NUMBER
1111                             ,p_dock_door_id        IN  NUMBER
1112                            ,p_organization_id   IN  NUMBER
1113                                    ) IS
1114 l_count NUMBER;
1115     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1116 BEGIN
1117    IF (l_debug = 1) THEN
1118       Debug( ' in missing_item_check', 'MISSING_ITEM_CUR');
1119    END IF;
1120    l_count := 0;
1121 
1122   IF ( p_delivery_id <> 0 ) then
1123         IF (l_debug = 1) THEN
1124            Debug( 'p_dock_door_id : ' || p_dock_door_id, 'MISSING_ITEM_CUR');
1125            Debug( 'p_organization_id : ' || p_organization_id, 'MISSING_ITEM_CUR');
1126         END IF;
1127     BEGIN
1128        OPEN x_missing_item_cur FOR
1129        select wnd.name,
1130               wdd.delivery_detail_id,
1131               wdd.inventory_item_id,
1132               wdd.requested_quantity,
1133               msik.concatenated_segments,
1134               msik.description
1135        from wsh_delivery_details_ob_grp_v wdd
1136            ,wsh_delivery_assignments_v wda
1137            ,wsh_new_deliveries_ob_grp_v wnd
1138            ,mtl_system_items_kfv msik
1139        where wnd.delivery_id = p_delivery_id
1140        AND   wnd.delivery_id = wda.delivery_id
1141        AND   wda.delivery_detail_id = wdd.delivery_detail_id
1142        AND   wdd.lpn_id is null
1143        AND   wdd.inventory_item_id = msik.inventory_item_id
1144        AND   wdd.organization_id = msik.organization_id
1145        AND   ((wda.parent_delivery_detail_id is null
1146                AND msik.mtl_transactions_enabled_flag <> 'N')
1147                OR wdd.released_status is null
1148                OR wdd.released_status NOT IN ('X', 'Y'));
1149 
1150         IF (l_debug = 1) THEN
1151            debug('Right Cur: Delivery Id : '||p_delivery_id||' Org Id '
1152                   ||p_organization_id||' Dock Door '||p_dock_door_id,'Missing_Item_Cur');
1153         END IF;
1154         RETURN;
1155     EXCEPTION
1156        WHEN NO_DATA_FOUND THEN
1157         IF (l_debug = 1) THEN
1158            debug('Dummy Cur: Delivery Id : '||p_delivery_id||' Org Id '
1159                   ||p_organization_id||' Dock Door '||p_dock_door_id,'Missing_Item_Cur');
1160         END IF;
1161         open x_missing_item_cur FOR select 1 from dual;
1162         return;
1163     END;
1164   END IF;
1165 END MISSING_ITEM_CUR;
1166 
1167 PROCEDURE SHIP_CONFIRM(
1168               x_return_status          OUT  NOCOPY VARCHAR2
1169               ,x_msg_count             OUT  NOCOPY NUMBER
1170               ,x_msg_data                   OUT  NOCOPY VARCHAR2
1171               ,x_missing_item_cur      OUT  NOCOPY t_genref
1172               ,x_error_code              OUT  NOCOPY NUMBER
1173               ,p_delivery_id             IN  NUMBER
1174               ,p_net_weight              IN  NUMBER     DEFAULT NULL
1175               ,p_gross_weight            IN  NUMBER     DEFAULT NULL
1176               ,p_wt_uom_code             IN  VARCHAR2   DEFAULT NULL
1177               ,p_waybill                    IN  VARCHAR2        DEFAULT NULL
1178               ,p_ship_method_code      IN  VARCHAR2     DEFAULT NULL
1179               ,p_fob_code                   IN  VARCHAR2        DEFAULT NULL
1180               ,p_fob_location_id       IN  NUMBER       DEFAULT NULL
1181               ,p_freight_term_code     IN  VARCHAR2     DEFAULT NULL
1182               ,p_freight_term_name     IN  VARCHAR2     DEFAULT NULL
1183               ,p_intmed_shipto_loc_id  IN  NUMBER       DEFAULT NULL
1184               ,p_org_id                     IN  NUMBER  DEFAULT NULL
1185               ,p_dock_door_id            IN  NUMBER     DEFAULT NULL
1186               ) IS
1187 l_init_msg_list                 VARCHAR2(1) :=FND_API.G_TRUE;
1188 l_return_status                 VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
1189 l_msg_count                     NUMBER;
1190 l_msg_data                      VARCHAR2(20000);
1191 
1192 l_ship_set                      VARCHAR2(2000) := NULL;
1193 l_error_msg                     VARCHAR2(20000) := NULL;
1194 l_trip_id                       NUMBER;
1195 l_trip_name                     VARCHAR2(30);
1196 unspec_ship_set_exists          EXCEPTION;
1197 incomplete_delivery             EXCEPTION;
1198 l_error_code                    NUMBER;
1199 x_ret_code                      number;
1200 l_status_code                   varchar2(3);
1201 l_del_name                      VARCHAR2(30);
1202 l_msg_table                     WSH_INTEGRATION.MSG_TABLE ;
1203 l_count                         NUMBER := 0;
1204 
1205 -- varajago: Added the following 3, for 5204686
1206 l_enforce_shipmethod            VARCHAR2(1) ;
1207 l_trip_shipmethod_code          VARCHAR2(30);
1208 l_trip_shipmethod_meaning       VARCHAR2(80);
1209 
1210 
1211 l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1212 
1213 l_ignore_for_planning     wsh_delivery_details.ignore_for_planning%type;
1214 l_tms_interface_flag      wsh_new_deliveries.TMS_INTERFACE_FLAG%type;
1215 l_otm_trip_id             wsh_trips.trip_id%type;
1216 l_otm_carrier_id          wsh_trips.carrier_id%type;
1217 l_otm_ship_method_code    wsh_trips.ship_method_code%type;
1218 l_otm_mode                wsh_trips.mode_of_transport%type;
1219 l_otm_plan_name           wsh_trips.tp_plan_name%type;
1220 
1221 l_severity          WSH_EXCEPTIONS.SEVERITY%TYPE;
1222 l_exception_name    WSH_EXCEPTIONS.EXCEPTION_NAME%TYPE;
1223 l_exception_text    WSH_EXCEPTIONS.MESSAGE%TYPE;
1224 
1225 l_otm_ifp_del_ids wsh_util_core.id_tab_type;
1226 
1227 l_num_warn         NUMBER :=0;
1228 l_num_error       NUMBER :=0;
1229 
1230   l_allow_ship_set_break   VARCHAR2(1) := 'N';    -- code added for bug#8596010
1231 
1232 CURSOR c_Get_Trip(v_del_id NUMBER) IS
1233    select wt.trip_id , wt.carrier_id, wt.ship_method_code, wt.mode_of_transport,
1234      wt.tp_plan_name -- glog proj
1235      from wsh_delivery_legs wdl, wsh_trip_stops wts, wsh_trips wt
1236      where wdl.pick_up_stop_id=wts.stop_id
1237      and wdl.delivery_id=v_del_id
1238      and wts.trip_id=wt.trip_id;
1239 
1240   CURSOR c_get_delivery_info IS
1241      SELECT status_code,NAME,ignore_for_planning, tms_interface_flag
1242        FROM wsh_new_deliveries_ob_grp_v
1243        WHERE delivery_id = p_delivery_id;
1244 
1245 BEGIN
1246   x_return_status := FND_API.G_RET_STS_SUCCESS;
1247   x_ret_code := 0;
1248   IF (l_debug = 1) THEN
1249      debug('p_delivery_id          : '||p_delivery_id,'ship_confirm');
1250      debug('p_net_weight           : '||p_net_weight,'ship_confirm');
1251      debug('p_gross_weight         : '||p_gross_weight,'ship_confirm');
1252      debug('p_wt_uom_code          : '||p_wt_uom_code,'ship_confirm');
1253      debug('p_waybill              : '||p_waybill,'ship_confirm');
1254      debug('p_ship_method_code     : '||p_ship_method_code,'ship_confirm');
1255      debug('p_fob_code             : '||p_fob_code,'ship_confirm');
1256      debug('p_fob_location_id      : '||p_fob_location_id,'ship_confirm');
1257      debug('p_freight_term_code    : '||p_freight_term_code,'ship_confirm');
1258      debug('p_freight_term_name    : '||p_freight_term_name,'ship_confirm');
1259      debug('p_intmed_shipto_loc_id : '||p_intmed_shipto_loc_id,'ship_confirm');
1260      debug('p_org_id               : '||p_org_id,'ship_confirm');
1261      debug('p_dock_door_id         : '||p_dock_door_id,'ship_confirm');
1262   END IF;
1263 
1264   IF p_delivery_id IS NOT NULL THEN
1265 
1266      OPEN c_get_delivery_info;
1267      FETCH c_get_delivery_info INTO l_status_code,l_del_name, l_ignore_for_planning, l_tms_interface_flag;
1268 
1269      IF (c_get_delivery_info%NOTFOUND) THEN
1270 	NULL;
1271      END IF;
1272 
1273      CLOSE c_get_delivery_info;
1274 
1275 
1276      IF l_status_code IN ('CL','CO','IT') THEN
1277 	RETURN ;
1278      END IF;
1279    ELSE
1280      RETURN;
1281   END IF;
1282 
1283   IF (    p_net_weight          IS NOT NULL OR
1284 	  p_gross_weight        IS NOT NULL OR
1285 	  p_wt_uom_code         IS NOT NULL OR
1286 	  p_waybill             IS NOT NULL OR
1287 	  p_ship_method_code    IS NOT NULL OR
1288 	  p_fob_code    IS NOT NULL OR
1289 	  p_fob_location_id     IS NOT NULL OR
1290 	  p_freight_term_code   IS NOT NULL OR
1291 	  p_freight_term_name   IS NOT NULL OR
1292 	  p_intmed_shipto_loc_id IS NOT NULL
1293 	  )  THEN
1294      WMS_DIRECT_SHIP_PVT.UPDATE_DELIVERY(
1295               x_return_status          => l_return_status
1296               ,x_msg_count             => l_msg_count
1297               ,x_msg_data              => l_msg_data
1298               ,p_delivery_id             => p_delivery_id
1299               ,p_net_weight              => p_net_weight
1300               ,p_gross_weight            => p_gross_weight
1301               ,p_wt_uom_code             => p_wt_uom_code
1302               ,p_waybill                         => p_waybill
1303               ,p_ship_method_code      => p_ship_method_code
1304               ,p_fob_code                   => p_fob_code
1305               ,p_fob_location_id              => p_fob_location_id
1306               ,p_freight_term_code     => p_freight_term_code
1307               ,p_freight_term_name     => p_freight_term_name
1308               ,p_intmed_shipto_loc_id  => p_intmed_shipto_loc_id
1309               );
1310      IF l_return_status = fnd_api.g_ret_sts_error THEN
1311 	IF (l_debug = 1) THEN
1312 	   DEBUG('Update Delivery API failed with status E ','SHIP_CONFIRM');
1313 	END IF;
1314 	RAISE fnd_api.g_exc_error;
1315       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1316 	IF (l_debug = 1) THEN
1317 	   DEBUG('Update Delivery API failed with status U','SHIP_CONFIRM');
1318 	 END IF;
1319 	 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1320      END IF;
1321   END IF;
1322 
1323   -- Check for material status, shipset and missing item
1324   IF (l_debug = 1) THEN
1325      debug('WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM. Check the delivery for the correctness ' || ' before Confirming it','Ship_confirm');
1326   END IF;
1327   WMS_DIRECT_SHIP_PVT.CHECK_DELIVERY(
1328 				     x_return_status        => l_return_status
1329 				     ,x_msg_count            => l_msg_count
1330 				     ,x_msg_data        => l_msg_data
1331 				     ,x_error_code           => l_error_code
1332 				     ,p_delivery_id          => p_delivery_id
1333 				     ,p_org_id          => p_org_id
1334 				     ,p_dock_door_id         => p_dock_door_id
1335 				     );
1336 
1337   IF l_error_code = 1 THEN  -- Case of Missing Item, Call Missing Item Cursor
1338      IF (l_debug = 1) THEN
1339 	debug(' Ship_Confirm. Missing Item Error','Ship_Confirm');
1340      END IF;
1341      WMS_DIRECT_SHIP_PVT.MISSING_ITEM_CUR(
1342 					  x_missing_item_cur      => x_missing_item_cur
1343 					  ,p_delivery_id            => p_delivery_id
1344 					  ,p_organization_id        => p_org_id
1345 					  ,p_dock_door_id   => p_dock_door_id);
1346      x_error_code := l_error_code;
1347      x_return_status := FND_API.G_RET_STS_ERROR;
1348      fnd_msg_pub.count_and_get( p_count => x_msg_count,
1349 				p_data  => x_msg_data);
1350      RETURN;
1351    ELSIF (l_error_code in (2,3,4,5,6,7,8) ) THEN
1352      IF (l_debug = 1) THEN
1353         debug(' Ship_Confirm Error. ShipSet, LPN Loaded on different Dock,ship_method_code ','Ship_Confirm');
1354      END IF;
1355      x_error_code := l_error_code;
1356      x_return_status := l_return_status;
1357      fnd_msg_pub.count_and_get( p_count => x_msg_count,
1358                                 p_data  => x_msg_data);
1359      RETURN;
1360   END IF ;
1361 
1362 
1363   /* Note: We do not need this update to ignore for planning here again becs things are taken care of during
1364   the stage_lpn() API. At this time, we will always have the delivery - either existing or created in stage_LPN()
1365     there is no check on exception severity in stage_lpn().
1366 
1367   -- Glog Changes
1368   IF WSH_UTIL_CORE.GC3_IS_INSTALLED = 'Y' AND nvl(l_ignore_for_planning, 'N') = 'N' THEN
1369 
1370      l_otm_trip_id  := 0;
1371      l_otm_carrier_id   := 0;
1372      l_otm_ship_method_code := NULL;
1373      l_otm_mode     := NULL;
1374      l_otm_plan_name    := NULL;
1375 
1376      OPEN c_GET_TRIP(p_delivery_id);
1377      FETCH c_GET_TRIP INTO l_otm_trip_id, l_otm_carrier_id, l_otm_ship_method_code, l_otm_mode, l_otm_plan_name;
1378 
1379      IF (c_GET_TRIP%NOTFOUND) THEN
1380 	l_otm_ship_method_code := p_ship_method_code;
1381      END IF;
1382 
1383      CLOSE c_GET_TRIP;
1384 
1385      IF l_debug = 1 THEN
1386 	debug(' otm_plan_name : ' || l_otm_plan_name, 'Ship Confirm');
1387 	debug(' otm_trip_id : '|| l_otm_trip_id, 'Ship Confirm');
1388 	debug(' tms_interface_flag : ' ||l_tms_interface_flag, 'Ship Confirm');
1389      END IF;
1390 
1391      --
1392      -- The 'null' tp_plan_name indicates that the delivery is not
1393      -- associated to an OTM plan. Hence ignore the delivery for planning.
1394      -- (CR->NS and AW->DR for 'Not assigned' to an OTM trip Delivery).
1395      -- This update for the ignore_for_plan to 'Y' is needed to autocreate the trip
1396      -- for this delivery during ship conform.
1397      --
1398 
1399      WSH_INTERFACE_EXT_GRP.OTM_PRE_SHIP_CONFIRM(
1400 					    p_delivery_id  =>  p_delivery_id,
1401 					    p_tms_interface_flag  => l_tms_interface_flag,
1402 					    p_trip_id   => l_otm_trip_id,
1403 					    x_return_status =>  l_return_status);
1404   END IF;
1405 */
1406 
1407   -- Create the Trip for the delivery is it does not exists already
1408   IF (l_debug = 1) THEN
1409      debug(' Ship_Confirm. Check Delivery was success, Create Trip if needed for delivery','Ship_Confirm');
1410   END IF;
1411 
1412   IF (l_debug = 1) THEN
1413      debug(' Ship_Confirm. Calling CREATE_TRIP','Ship_Confirm');
1414      debug('p_org_id  '||p_org_id);
1415      debug('p_dock_door_id '||p_dock_door_id);
1416   END IF;
1417    WMS_DIRECT_SHIP_PVT.CREATE_TRIP(
1418              x_return_status    => l_return_status
1419              ,p_organization_id  => p_org_id
1420              ,p_dock_door_id     => p_dock_door_id
1421        ,p_delivery_id      =>p_delivery_id /* bug 2741857 */
1422              ,p_direct_ship_flag => 'Y'
1423              );
1424      IF l_return_status = fnd_api.g_ret_sts_error THEN
1425         IF (l_debug = 1) THEN
1426            debug(' Ship_Confirm. Create Trip Failed','Ship_Confirm');
1427         END IF;
1428         fnd_message.set_name('WMS','WMS_CREATE_TRIP_FAIL');
1429         FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1430         /* Failed to create trip for the delivery DELIVERY_NAME */
1431         FND_MSG_PUB.ADD;
1432         RAISE fnd_api.g_exc_error;
1433      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1434         fnd_message.set_name('WMS','WMS_CREATE_TRIP_FAIL');
1435         FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1436         FND_MSG_PUB.ADD;
1437         IF (l_debug = 1) THEN
1438            debug(' Ship_Confirm. Create Trip Failed Unexpected Error','Ship_Confirm');
1439         END IF;
1440         RAISE fnd_api.g_exc_unexpected_error;
1441      END IF;
1442       IF (l_debug = 1) THEN
1443          debug('before calling Ship Confirm','Ship Confirm');
1444       END IF;
1445 
1446       INV_SHIPPING_TRANSACTION_PUB.get_shipmethod_details
1447                     ( p_org_id                  => p_org_id
1448                      ,p_delivery_id             => p_delivery_id
1449                      ,p_enforce_shipmethod      => l_enforce_shipmethod
1450                      ,p_trip_id                 => l_trip_id
1451                      ,x_trip_shipmethod_code    => l_trip_shipmethod_code
1452                      ,x_trip_shipmethod_meaning => l_trip_shipmethod_meaning
1453                      ,x_return_status           => l_return_status
1454                      ,x_msg_data                => l_msg_data
1455                      ,x_msg_count               => l_msg_count);
1456 
1457       IF (l_debug = 1) THEN
1458           debug('get_shipmethod_details.' ,'Ship_Confirm');
1459           debug('l_return_status: '|| l_return_status,'Ship_Confirm');
1460           debug('l_msg_data: '     || l_msg_data,     'Ship_Confirm');
1461           debug('l_msg_count: '    || l_msg_count,    'Ship_Confirm');
1462           debug('l_enforce_shipmethod: '      || l_enforce_shipmethod,    'Ship_Confirm');
1463           debug('l_trip_id: '      || l_trip_id,    'Ship_Confirm');
1464           debug('l_trip_shipmethod_code: '    || l_trip_shipmethod_code,   'Ship_Confirm');
1465           debug('l_trip_shipmethod_meaning: ' || l_trip_shipmethod_meaning,'Ship_Confirm');
1466       END IF;
1467       IF l_return_status = fnd_api.g_ret_sts_error THEN
1468             RAISE fnd_api.g_exc_error;
1469       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1470             RAISE fnd_api.g_exc_unexpected_error;
1471       END IF;
1472 
1473       -- If everything is fine then confirm the delivery
1474 
1475       --Set global to say shipping via Direct Ship instead of desktop
1476       --Added so that WMS_SHIPPING_PUB.DEL_WSTT_RECS_BY_DELIVERY_ID will
1477       --not fail in Shipping's API.
1478       wms_globals.g_ship_confirm_method := 'DIRECT';
1479 
1480        -- code added for bug#8596010
1481             wms_shipping_transaction_pub.g_allow_ship_set_break := l_allow_ship_set_break;
1482 
1483        IF l_debug = 1 THEN
1484 
1485        debug('l_allow_ship_set_break: ' || l_allow_ship_set_break,'Ship Confirm');
1486 
1487        END IF;
1488        -- End of code added for bug#8596010
1489 
1490       WSH_DELIVERIES_PUB.Delivery_Action ( p_api_version_number     => 1.0
1491                    ,p_init_msg_list          => l_init_msg_list
1492                    ,x_return_status          => l_return_status
1493                     ,x_msg_count              => l_msg_count
1494                     ,x_msg_data               => l_msg_data
1495                     ,p_action_code            => 'CONFIRM'
1496                     ,p_delivery_id            => p_delivery_id
1497                     ,p_sc_action_flag         => 'S'
1498                     ,p_sc_intransit_flag      => 'N'
1499                     ,p_sc_close_trip_flag     => 'N'
1500                     ,p_sc_create_bol_flag     => 'Y'
1501                     ,p_sc_stage_del_flag      => 'Y'
1502                     ,p_sc_defer_interface_flag => 'Y'
1503 		    ,p_sc_trip_ship_method       => l_trip_shipmethod_code -- added for 5204688
1504                     ,p_wv_override_flag       => 'N'
1505                     ,x_trip_id                =>  l_trip_id
1506             ,x_trip_name              =>  l_trip_name);
1507       wms_globals.g_ship_confirm_method := NULL;
1508 
1509      IF (l_debug = 1) THEN
1510         debug('after calling Ship confirm','Ship confirm');
1511      END IF;
1512      IF (l_return_status=fnd_api.g_ret_sts_success) THEN
1513      l_count:=0;
1514 
1515      FOR i in 1..WSH_INTEGRATION.G_MSG_TABLE.COUNT LOOP
1516             IF (WSH_INTEGRATION.G_MSG_TABLE(i).MESSAGE_TYPE = 'W' ) THEN
1517                l_count := l_count + 1;
1518                l_msg_table(l_count) := WSH_INTEGRATION.G_MSG_TABLE(i);
1519             END IF;
1520     END LOOP;
1521     IF (l_debug = 1) THEN
1522        debug('before calling process shipping warning msgs','ship_confirm');
1523     END IF;
1524     WMS_SHIPPING_MESSAGES.PROCESS_SHIPPING_WARNING_MSGS(x_return_status  => l_return_status ,
1525                                                             x_msg_count      => l_msg_count ,
1526                                                             x_msg_data       => l_msg_data,
1527                                                             p_commit         => FND_API.g_false,
1528                                                             x_shipping_msg_tab  => l_msg_table);
1529 
1530     FOR i in 1..l_msg_table.count LOOP
1531               IF ( l_msg_table(i).MESSAGE_TYPE = 'E' ) THEN
1532                  --l_error_exists := TRUE;
1533                 l_return_status:=fnd_api.g_ret_sts_error;
1534                  FOR j in 1..WSH_INTEGRATION.G_MSG_TABLE.COUNT LOOP
1535                     IF (l_msg_table(i).message_name = WSH_INTEGRATION.G_MSG_TABLE(j).MESSAGE_NAME) THEN
1536                        WSH_INTEGRATION.G_MSG_TABLE(j).MESSAGE_TYPE := 'E';
1537                     END IF;
1538                  END LOOP;
1539               END IF;
1540            END LOOP;
1541            l_msg_table.delete;
1542      END IF;
1543     IF( l_return_status IN (fnd_api.g_ret_sts_error)) THEN
1544        FND_MESSAGE.SET_NAME('WMS','WMS_CONFIRM_DEL_FAIL');
1545        FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1546        -- Ship Confirm of delivey <DELIVERY_NAME> failed
1547        FND_MSG_PUB.ADD;
1548        IF (l_debug = 1) THEN
1549           debug(' Ship_Confirm. Confirm Delivery is failing from WSH_DELIVERY_PUB','Ship_Confirm');
1550        END IF;
1551        raise FND_API.G_EXC_ERROR;
1552 
1553     ELSIF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
1554        FND_MESSAGE.SET_NAME('WMS','WMS_CONFIRM_DEL_FAIL');
1555        FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1556        /* cONFIRM DELIVERY FAILED*/
1557        FND_MSG_PUB.ADD;
1558        IF (l_debug = 1) THEN
1559           debug(' Ship_Confirm. Confirm Delivery is failing from WSH_DELIVERY_PUB, Unexpected error','Ship_Confirm');
1560        END IF;
1561        raise FND_API.G_EXC_UNEXPECTED_ERROR;
1562     END IF;
1563 
1564     --Ship confirm successful.  Display only Interface Trip stop message from shipping
1565     process_mobile_msg;
1566     x_msg_count := fnd_msg_pub.count_msg;
1567 EXCEPTION
1568    WHEN fnd_api.g_exc_error THEN
1569       x_return_status := fnd_api.g_ret_sts_error;
1570       IF (l_debug = 1) THEN
1571          debug('In Exception (expected error) - E ','Ship_Confirm');
1572       END IF;
1573          fnd_msg_pub.count_and_get
1574             ( p_count => x_msg_count,
1575               p_data  => x_msg_data
1576               );
1577    WHEN fnd_api.g_exc_unexpected_error THEN
1578       x_return_status := fnd_api.g_ret_sts_unexp_error;
1579       IF (l_debug = 1) THEN
1580          debug('In Exception (unexpected error) - U ','Ship_Confirm');
1581       END IF;
1582          fnd_msg_pub.count_and_get
1583             ( p_count => x_msg_count,
1584               p_data  => x_msg_data
1585               );
1586    WHEN OTHERS THEN
1587       x_return_status := fnd_api.g_ret_sts_unexp_error;
1588       IF (l_debug = 1) THEN
1589          debug('In Exception (others)' ||SQLERRM,'Ship_Confirm');
1590       END IF;
1591          fnd_msg_pub.count_and_get
1592             ( p_count => x_msg_count,
1593               p_data  => x_msg_data
1594               );
1595 END;  -- SHIP_CONFIRM
1596 
1597 -- Called when User presses Trip Info Button or CLOSE TRUCK Button
1598 
1599 PROCEDURE CONFIRM_ALL_DELIVERIES(
1600               x_return_status        OUT NOCOPY VARCHAR2
1601              ,x_msg_count            OUT NOCOPY NUMBER
1602              ,x_msg_data             OUT NOCOPY VARCHAR2
1603              ,x_missing_item_cur     OUT NOCOPY t_genref
1604              ,x_error_code           OUT  NOCOPY NUMBER
1605              ,p_delivery_id          IN  NUMBER
1606              ,p_net_weight           IN  NUMBER
1607              ,p_gross_weight         IN  NUMBER
1608              ,p_wt_uom_code          IN  VARCHAR2
1609              ,p_waybill              IN  VARCHAR2
1610              ,p_ship_method_code     IN  VARCHAR2
1611              ,p_fob_code             IN  VARCHAR2
1612              ,p_fob_location_id      IN  NUMBER
1613              ,p_freight_term_code    IN  VARCHAR2
1614              ,p_freight_term_name    IN  VARCHAR2
1615              ,p_intmed_shipto_loc_id IN  NUMBER
1616              ,p_org_id               IN  NUMBER
1617              ,p_dock_door_id         IN  NUMBER)IS
1618 l_init_msg_list                                          VARCHAR2(1) :=FND_API.G_TRUE;
1619 l_return_status                                          VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
1620 l_msg_count                                                      NUMBER;
1621 l_msg_data                                                       VARCHAR2(20000);
1622 l_rowid                                                          ROWID;
1623 l_delivery_id                                            NUMBER;
1624 l_delivery_name                                          VARCHAR2(30);
1625 l_name                                                   VARCHAR2(30);
1626 l_ship_set                                               NUMBER;
1627 l_t_genref                                              t_genref;
1628 l_error_code                                             NUMBER;
1629 l_trip_id                                                NUMBER;
1630 l_trip_name                                              VARCHAR2(30);
1631 CURSOR loaded_deliveries IS
1632   SELECT DISTINCT WSTT.delivery_id
1633   FROM WMS_SHIPPING_TRANSACTION_TEMP wstt
1634       ,WSH_NEW_DELIVERIES_OB_GRP_V WND
1635   WHERE wstt.organization_id = p_org_id
1636   AND   wstt.dock_door_id = p_dock_door_id
1637   AND   wstt.dock_appoint_flag = 'N'
1638   AND   nvl(wstt.direct_ship_flag,'N') = 'Y'
1639   AND   wstt.delivery_id = wnd.delivery_id
1640   AND   wnd.status_code = 'OP';
1641 
1642 --
1643 CURSOR ALL_DELIVERIES IS
1644     SELECT DELIVERY_ID FROM WMS_SHIPPING_TRANSACTION_TEMP
1645     WHERE DELIVERY_ID IS NOT NULL
1646     AND TRIP_ID IS NULL
1647     AND ORGANIZATION_ID=P_ORG_ID
1648     AND DOCK_DOOR_ID=P_DOCK_DOOR_ID
1649     AND NVL(DIRECT_SHIP_FLAG,'N')='Y';
1650 
1651     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1652 BEGIN
1653 
1654 -- Call this API from Trip Info page of the Delivery Info. If thats the case
1655 -- then Delivery might have been changed. First update it then Shipconfirm all
1656 -- delivery loaded on the dock.
1657 IF (l_debug = 1) THEN
1658    debug('Confirm All Deliveries: Begin delivery_id'||to_char(p_delivery_id),'CONFIRM_ALL_DELIVERIES');
1659 END IF;
1660 x_return_status:= FND_API.G_RET_STS_SUCCESS;
1661        IF (l_debug = 1) THEN
1662           debug('The value OF p_delivery_id        is '||p_delivery_id,'Confirm_All_Delivery');
1663           debug('The value OF p_net_weight is '||p_net_weight,'Confirm_All_Delivery');
1664           debug('The value OF p_gross_weight       is '||p_gross_weight,'Confirm_All_Delivery');
1665           debug('The value OF p_wt_uom_code        is '||p_wt_uom_code,'Confirm_All_Delivery');
1666           debug('The value OF p_waybill    is '||p_waybill,'Confirm_All_Delivery');
1667           debug('The value OF p_ship_method_code   is '||p_ship_method_code,'Confirm_All_Delivery');
1668           debug('The value OF p_fob_code   is '||p_fob_code,'Confirm_All_Delivery');
1669           debug('The value OF p_fob_location_id    is '||p_fob_location_id,'Confirm_All_Delivery');
1670           debug('The value OF p_freight_term_code  is '||p_freight_term_code,'Confirm_All_Delivery');
1671           debug('The value OF p_freight_term_name  is '||p_freight_term_name,'Confirm_All_Delivery');
1672           debug('The value OF p_intmed_shipto_loc_id       is '||p_intmed_shipto_loc_id,'Confirm_All_Delivery');
1673           debug('The value OF p_org_id     is '||p_org_id,'Confirm_All_Delivery');
1674           debug('The value OF p_dock_door_id       is '||p_dock_door_id,'Confirm_All_Delivery');
1675        END IF;
1676 
1677  IF (p_net_weight              IS NOT NULL OR
1678           p_gross_weight       IS NOT NULL OR
1679           p_wt_uom_code        IS NOT NULL OR
1680           p_waybill            IS NOT NULL OR
1681           p_ship_method_code   IS NOT NULL OR
1682           p_fob_code           IS NOT NULL OR
1683           p_fob_location_id    IS NOT NULL OR
1684           p_freight_term_code  IS NOT NULL OR
1685           p_freight_term_name      IS NOT NULL OR
1686           p_intmed_shipto_loc_id   IS NOT NULL
1687           )  THEN
1688     WMS_DIRECT_SHIP_PVT.UPDATE_DELIVERY(
1689               x_return_status        => l_return_status
1690               ,x_msg_count           => l_msg_count
1691               ,x_msg_data            => l_msg_data
1692               ,p_delivery_id                     => p_delivery_id
1693               ,p_net_weight                      => p_net_weight
1694               ,p_gross_weight               => p_gross_weight
1695               ,p_wt_uom_code                     => p_wt_uom_code
1696               ,p_waybill                                 => p_waybill
1697               ,p_ship_method_code        => p_ship_method_code
1698               ,p_fob_code                      => p_fob_code
1699               ,p_fob_location_id                 => p_fob_location_id
1700               ,p_freight_term_code   => p_freight_term_code
1701               ,p_freight_term_name   => p_freight_term_name
1702               ,p_intmed_shipto_loc_id => p_intmed_shipto_loc_id
1703               );
1704     IF (l_debug = 1) THEN
1705        debug('Return Status from Update_Delivery: '||l_return_status, ' Confirm_All_Deliveries');
1706     END IF;
1707            IF l_return_status = fnd_api.g_ret_sts_error THEN
1708               IF (l_debug = 1) THEN
1709                  debug('Update Delivery API failed with status E ','Confirm_All_deliveries');
1710               END IF;
1711               RAISE fnd_api.g_exc_error;
1712            ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1713               IF (l_debug = 1) THEN
1714                  debug('Update Delivery API failed with status U','Confirm_All_Deliveries');
1715               END IF;
1716               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1717            END IF;
1718  END IF;
1719  /* bug # 2994098 */
1720     -- If a delivery is ship confirmed from STF without closing the trip
1721     -- then trip id should be updated in wstt because user may try to close the trip from mobile.
1722     BEGIN
1723     FOR l_deliveries IN all_deliveries
1724     LOOP
1725        BEGIN
1726          SELECT wts.trip_id,wt.name
1727          INTO l_trip_id,l_trip_name
1728          FROM wsh_delivery_legs_ob_grp_v wdl,
1729               wsh_trip_stops_ob_grp_v wts,
1730               wsh_trips_ob_grp_v wt
1731         WHERE wdl.delivery_id=l_deliveries.delivery_id
1732         AND   wdl.pick_up_stop_id=wts.stop_id
1733         AND   wt.trip_id=wts.trip_id
1734         AND  ROWNUM=1;
1735         IF l_trip_id IS NOT NULL THEN
1736             UPDATE wms_shipping_transaction_temp
1737             SET trip_id=l_trip_id,trip_name=l_trip_name
1738             WHERE delivery_id=l_deliveries.delivery_id AND trip_id IS NULL;
1739         END IF;
1740        EXCEPTION
1741         WHEN NO_DATA_FOUND THEN
1742         debug('trip_id update on wstt failed','CONFIRM_ALL_DELIVERIES');
1743          END;
1744     END LOOP;
1745     EXCEPTION
1746         WHEN OTHERS THEN
1747          debug('trip_id update on wstt failed','CONFIRM_ALL_DELIVERIES');
1748     END;
1749 
1750 
1751   IF (p_delivery_id IS NOT NULL) THEN
1752      IF (l_debug = 1) THEN
1753         debug('P_Delivery_Id is not null ','Confirm_All_Deliveries');
1754      END IF;
1755      WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM(
1756                   x_return_status   =>l_return_status
1757                   ,x_msg_count       =>l_msg_count
1758                   ,x_msg_data        =>l_msg_data
1759                   ,x_missing_item_cur=>l_t_genref
1760                   ,x_error_code      =>l_error_code
1761                   ,p_delivery_id     =>p_delivery_id
1762                   ,p_org_id          =>p_org_id
1763                   ,p_dock_door_id    =>p_dock_door_id
1764                   );
1765       x_missing_item_cur := l_t_genref;
1766       x_error_code       := l_error_code;
1767 
1768      IF (l_debug = 1) THEN
1769         debug('Return Status - Error_code from Ship_Confirm 1: '||l_return_status
1770               ||' - '||l_error_code, ' Confirm_All_Deliveries');
1771      END IF;
1772      IF l_return_status = fnd_api.g_ret_sts_error THEN
1773                 IF (l_debug = 1) THEN
1774                    debug('Ship Confirm API failed with status E ','Confirm_All_deliveries');
1775                 END IF;
1776                 RAISE fnd_api.g_exc_error;
1777      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1778                 IF (l_debug = 1) THEN
1779                    debug('Ship Confirm API failed with status U','Confirm_All_Deliveries');
1780                 END IF;
1781                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1782      END IF;
1783   END IF;
1784   IF ((p_org_id IS NOT NULL) AND (p_dock_door_id IS NOT NULL)) THEN
1785      IF (l_debug = 1) THEN
1786         debug('Confirm All Deliveries: Org id and Dock Door is not null loop ' ||
1787               'loaded deliveries','Confirm_All_Deliveries');
1788      END IF;
1789    OPEN loaded_deliveries;
1790    LOOP
1791      FETCH loaded_deliveries into l_delivery_id;
1792      EXIT WHEN loaded_deliveries%NOTFOUND;
1793      IF (l_debug = 1) THEN
1794         debug('Confirm All Deliveries: Delivery Id '||to_char(l_delivery_id),'Confirm_All_Deliveries');
1795      END IF;
1796      WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM(
1797            x_return_status   =>l_return_status
1798            ,x_msg_count       =>l_msg_count
1799            ,x_msg_data       =>l_msg_data
1800            ,x_missing_item_cur=>l_t_genref
1801            ,x_error_code      =>l_error_code
1802            ,p_delivery_id     =>l_delivery_id
1803            ,p_org_id         =>p_org_id
1804            ,p_dock_door_id    =>p_dock_door_id
1805            );
1806       x_missing_item_cur := l_t_genref;
1807       x_error_code       := l_error_code;
1808       IF (l_debug = 1) THEN
1809          debug('Return Status - Error_code from Ship_Confirm 2: '||l_return_status
1810                ||' - '||l_error_code, ' Confirm_All_Deliveries');
1811       END IF;
1812      IF l_return_status = fnd_api.g_ret_sts_error THEN
1813         IF (l_debug = 1) THEN
1814            debug('Ship Confirm API failed with status E ','Confirm_All_deliveries');
1815         END IF;
1816         RAISE fnd_api.g_exc_error;
1817      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1818         IF (l_debug = 1) THEN
1819            debug('Ship Confirm API failed with status U','Confirm_All_Deliveries');
1820         END IF;
1821         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1822      END IF;
1823    END LOOP;
1824    close loaded_deliveries;
1825 END IF;
1826 
1827 EXCEPTION
1828    WHEN fnd_api.g_exc_error THEN
1829       x_return_status := fnd_api.g_ret_sts_error;
1830       IF loaded_deliveries%isopen THEN
1831          CLOSE loaded_deliveries;
1832       END IF ;
1833       IF (l_debug = 1) THEN
1834          debug('In Exception (expected error) - E ','Confirm_All_Deliveries');
1835       END IF;
1836          fnd_msg_pub.count_and_get
1837             ( p_count => x_msg_count,
1838               p_data  => x_msg_data
1839               );
1840    WHEN fnd_api.g_exc_unexpected_error THEN
1841       x_return_status := fnd_api.g_ret_sts_unexp_error;
1842       IF loaded_deliveries%isopen THEN
1843          CLOSE loaded_deliveries;
1844       END IF ;
1845       IF (l_debug = 1) THEN
1846          debug('In Exception (unexpected error) - U ','Confirm_All_Deliveries');
1847       END IF;
1848          fnd_msg_pub.count_and_get
1849             ( p_count => x_msg_count,
1850               p_data  => x_msg_data
1851               );
1852    WHEN OTHERS THEN
1853       x_return_status := fnd_api.g_ret_sts_unexp_error;
1854       IF loaded_deliveries%isopen THEN
1855          CLOSE loaded_deliveries;
1856       END IF ;
1857       IF (l_debug = 1) THEN
1858          debug('In Exception (others)' ||SQLERRM,'Confirm_All_Deliveries');
1859       END IF;
1860          fnd_msg_pub.count_and_get
1861             ( p_count => x_msg_count,
1862               p_data  => x_msg_data
1863               );
1864 
1865 END; -- CONFIRM_DELIVERY
1866 
1867 PROCEDURE CREATE_TRIP(x_return_status    OUT NOCOPY VARCHAR2,
1868                       p_organization_id  IN  NUMBER,
1869                       p_dock_door_id     IN  NUMBER,
1870                       p_delivery_id      IN NUMBER,
1871                       p_direct_ship_flag IN  VARCHAR2 DEFAULT 'N'
1872                       ) IS
1873 l_del_tab            WSH_UTIL_CORE.ID_TAB_TYPE;
1874 l_auto_trip_del      WSH_UTIL_CORE.ID_TAB_TYPE;
1875 l_auto_trip_index    NUMBER:=1;
1876 --l_delivery_id        NUMBER;
1877 l_return_Status      VARCHAR2(1);
1878 l_del_index          NUMBER;
1879 l_del_count          NUMBER;
1880 l_trip_id                NUMBER;
1881 l_chk_trip_id        NUMBER;
1882 l_trip_name          VARCHAR2(30);
1883 l_msg_count          NUMBER;
1884 l_msg_data           VARCHAR2(20000);
1885 l_out_trip_id        VARCHAR2(30);
1886 l_out_trip_name      VARCHAR2(30);
1887 
1888     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1889 BEGIN
1890    x_return_status:= fnd_api.g_ret_sts_success;
1891    wms_globals.g_ship_confirm_method := 'DIRECT';
1892    IF (l_debug = 1) THEN
1893       debug('Begin Create Trip ','CREATE_TRIP');
1894       debug('p_organization_id  :  ' || p_organization_id,'CREATE_TRIP');
1895       debug('p_dock_door_id     :  ' || p_dock_door_id,'CREATE_TRIP');
1896       debug('p_delivery_id      :  ' || p_delivery_id,'CREATE_TRIP');
1897       debug('p_direct_ship_flag :  ' || p_direct_ship_flag,'CREATE_TRIP');
1898    END IF;
1899    l_del_index := 1;
1900       -- Add the following code in case user created Trip manually from desktop
1901       BEGIN
1902          SELECT wts.trip_id
1903          INTO l_trip_id
1904          FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
1905          WHERE wdl.delivery_id = p_delivery_id --l_delivery_id /* bug 2741857 */
1906            and wdl.pick_up_stop_id = wts.stop_id;
1907 
1908          IF l_trip_id IS NOT NULL THEN
1909            UPDATE wms_shipping_transaction_temp
1910             SET    trip_id = l_trip_id,
1911                    last_update_date =  SYSDATE,
1912                    last_updated_by  =  FND_GLOBAL.USER_ID
1913             WHERE  delivery_id = p_delivery_id;--l_delivery_id; /* bug 2741857 */
1914          END IF;
1915       EXCEPTION
1916          WHEN NO_DATA_FOUND THEN
1917          IF (l_debug = 1) THEN
1918             debug('Exception part: '||p_delivery_id,'CREATE_TRIP');
1919          END IF;
1920             l_trip_id := null;
1921             l_del_tab(l_del_index) := p_delivery_id; --l_delivery_id; /* bug 2741857 */
1922             l_del_index := l_del_index + 1;
1923       END;
1924 IF (l_debug = 1) THEN
1925    debug('End Loop thru all delivery loaded on the dock '||l_del_tab.count,'CREATE_TRIP');
1926 END IF;
1927    -- Commit the update of delivery with proper trip ids
1928   BEGIN
1929      SELECT wt.trip_id
1930      INTO l_chk_trip_id
1931      FROM WMS_SHIPPING_TRANSACTION_TEMP wstt
1932          ,WSH_TRIPS_OB_GRP_V wt
1933      WHERE
1934           wt.trip_id = wstt.trip_id
1935       AND  organization_id    = p_organization_id
1936       AND status_code         = 'OP'
1937       AND dock_door_id       = p_dock_door_id
1938       AND dock_appoint_flag  = 'N'
1939        AND nvl(direct_ship_flag,'N') = 'Y'
1940            AND nvl(planned_flag,'N') = 'N'  -- bug 7211509, 7216163. If there are prev unplanned trips available, we shoud utilize it
1941            AND ROWNUM =1;
1942   EXCEPTION
1943    WHEN NO_DATA_FOUND THEN
1944       l_chk_trip_id := NULL;
1945   END;
1946 
1947   IF l_chk_trip_id IS NOT NULL AND (l_del_tab.COUNT > 0) THEN
1948       FOR i IN 1..l_del_tab.count LOOP
1949           IF (l_debug = 1) THEN
1950              debug('The value of l_chk_trip_id is '||l_chk_trip_id,'create_trip');
1951           END IF;
1952          WSH_DELIVERIES_PUB.Delivery_Action
1953            ( p_api_version_number     => 1.0,
1954              p_init_msg_list          => FND_API.G_TRUE,
1955              x_return_status          => l_return_status,
1956              x_msg_count              => l_msg_count,
1957              x_msg_data               => l_msg_data,
1958              p_action_code            => 'ASSIGN-TRIP',
1959              p_delivery_id            => l_del_tab(i),
1960              p_asg_trip_id            => l_chk_trip_id ,
1961              x_trip_id                => l_out_trip_id,
1962              X_TRIP_NAME              => l_out_trip_name );
1963          IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
1964             IF (l_debug = 1) THEN
1965                debug('UPDATE WSTT with the trip_id assigned','CREATE_TRIP');
1966             END IF;
1967                UPDATE WMS_SHIPPING_TRANSACTION_TEMP
1968                SET trip_id   = l_chk_trip_id
1969                WHERE delivery_id = l_del_tab(i);
1970          ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
1971                l_auto_trip_del(l_auto_trip_index) := l_del_tab(i);
1972                l_auto_trip_index := l_auto_trip_index +1;
1973          ELSIF  l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1974             IF (l_debug = 1) THEN
1975                debug('Return unexpected error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP',
1976                      'WMS_DIRECT_SHIP_PVT.Create_Trip');
1977             END IF;
1978             RAISE fnd_api.g_exc_unexpected_error;
1979          END IF;
1980       END LOOP;
1981   ELSIF l_del_tab.COUNT >0 THEN
1982       IF (l_debug = 1) THEN
1983          debug('No open trip exists.Hence auto creating trip ','create_trip');
1984       END IF;
1985       WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP(p_del_rows      => l_del_tab,
1986                                         x_trip_id       => l_trip_id,
1987                                         x_trip_name     => l_trip_name,
1988                                         x_return_status => l_return_status);
1989       IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
1990          IF (l_debug = 1) THEN
1991             debug('UPDATE WSTT with the trip created','CREATE_TRIP');
1992          END IF;
1993          FOR k in 1..l_del_tab.COUNT LOOP
1994             UPDATE WMS_SHIPPING_TRANSACTION_TEMP
1995             SET trip_id   = l_trip_id
1996             WHERE delivery_id = l_del_tab(k);
1997          END LOOP;
1998       ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
1999          IF (l_debug = 1) THEN
2000             debug('Return error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP','WMS_DIRECT_SHIP_PVT.Create_Trip');
2001          END IF;
2002          RAISE fnd_api.g_exc_error;
2003       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2004          IF (l_debug = 1) THEN
2005             debug('Return unexpected error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP',
2006                      'WMS_DIRECT_SHIP_PVT.Create_Trip');
2007          END IF;
2008          RAISE fnd_api.g_exc_unexpected_error;
2009       END IF;
2010   END IF;
2011 
2012   IF l_auto_trip_del.count >0  THEN
2013       IF (l_debug = 1) THEN
2014          debug('Assignment of delivery failed.Hence auto creating trip for the failed deliveries  ',
2015                      'create_trip');
2016       END IF;
2017      WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP(p_del_rows      => l_auto_trip_del,
2018                                         x_trip_id       => l_trip_id,
2019                                         x_trip_name     => l_trip_name,
2020                                         x_return_status => l_return_status);
2021      IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
2022         IF (l_debug = 1) THEN
2023            debug('UPDATE WSTT with the trip for failed assignments','CREATE_TRIP');
2024         END IF;
2025 
2026         FOR j in 1..l_auto_trip_del.count LOOP
2027            UPDATE WMS_SHIPPING_TRANSACTION_TEMP
2028            SET trip_id   = l_trip_id
2029            WHERE delivery_id = l_auto_trip_del(j);
2030         END LOOP;
2031 
2032       ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
2033          IF (l_debug = 1) THEN
2034             debug('The API WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP returned status E ','WMS_DIRECT_SHIP_PVT.Create_Trip');
2035          END IF;
2036          RAISE fnd_api.g_exc_error;
2037       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2038          IF (l_debug = 1) THEN
2039             debug('The API WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP returned status U ','WMS_DIRECT_SHIP_PVT.Create_Trip');
2040          END IF;
2041          RAISE fnd_api.g_exc_unexpected_error;
2042       END IF;
2043   END IF;
2044 
2045   wms_globals.g_ship_confirm_method := null;
2046 EXCEPTION
2047    WHEN fnd_api.g_exc_error THEN
2048       x_return_status := fnd_api.g_ret_sts_error;
2049       wms_globals.g_ship_confirm_method := NULL;
2050       IF (l_debug = 1) THEN
2051          debug('In Exception (expected error) - E ','CREATE_TRIP');
2052       END IF;
2053          fnd_msg_pub.count_and_get
2054             ( p_count => l_msg_count,
2055               p_data  => l_msg_data
2056               );
2057    WHEN fnd_api.g_exc_unexpected_error THEN
2058       x_return_status := fnd_api.g_ret_sts_unexp_error;
2059       wms_globals.g_ship_confirm_method := NULL;
2060       IF (l_debug = 1) THEN
2061          debug('In Exception (unexpected error) - U ','Create_Trip');
2062       END IF;
2063          fnd_msg_pub.count_and_get
2064             ( p_count => l_msg_count,
2065               p_data  => l_msg_data
2066               );
2067    WHEN others THEN
2068       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
2069       wms_globals.g_ship_confirm_method := NULL;
2070       IF (l_debug = 1) THEN
2071      debug('Exception When Others'||SQLERRM,'WMS_DIRECT_SHIP.Create_Trips');
2072       END IF;
2073 END CREATE_TRIP; -- END CREATE_TRIP
2074 
2075 PROCEDURE UPDATE_TRIPSTOP(
2076               x_return_status        OUT NOCOPY VARCHAR2
2077               ,x_msg_count            OUT NOCOPY NUMBER
2078               ,x_msg_data             OUT NOCOPY VARCHAR2
2079               ,p_trip_id              IN  NUMBER
2080               ,p_vehicle_item_id      IN  NUMBER
2081               ,p_vehicle_num_prefix   IN  VARCHAR2
2082               ,p_vehicle_num          IN  VARCHAR2
2083               ,p_seal_code            IN  VARCHAR2
2084               ,p_org_id               IN  NUMBER   DEFAULT NULL
2085               ,p_dock_door_id         IN  NUMBER   DEFAULT NULL
2086               ,p_ship_method_code     IN VARCHAR2 DEFAULT NULL)
2087  IS
2088 
2089 l_init_msg_list                VARCHAR2(1) :=FND_API.G_TRUE;
2090 l_return_status                VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
2091 l_msg_count                    NUMBER;
2092 l_msg_data                     VARCHAR2(20000);
2093 
2094 l_freight_cost_rec             WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
2095 l_stop_info                    WSH_TRIP_STOPS_PUB.Trip_Stop_Pub_Rec_Type;
2096 p_trip_info                    WSH_TRIPS_PUB.Trip_Pub_Rec_Type;
2097 l_freight_cost_id              NUMBER;
2098 l_stop_id                      NUMBER;
2099 l_trip_id                      NUMBER;
2100 l_trip_name                    VARCHAR2(30);
2101 counter                        NUMBER;
2102 l_row_id                       rowid;
2103 l_name                         VARCHAR2(30);
2104 l_vehicle_item_id              NUMBER;
2105 l_org_id                       NUMBER;
2106 l_dock_door_id                 NUMBER;
2107 
2108 CURSOR trip_freight IS
2109    SELECT  ROWID
2110           ,FREIGHT_COST_TYPE_ID
2111           ,CURRENCY_CODE
2112           ,FREIGHT_AMOUNT
2113           ,CONVERSION_TYPE
2114    FROM  WMS_FREIGHT_COST_TEMP
2115    WHERE TRIP_ID = p_trip_id
2116    AND   FREIGHT_COST_ID IS NULL
2117    FOR UPDATE OF FREIGHT_COST_ID NOWAIT;
2118 
2119  CURSOR stops IS
2120  SELECT 'x'
2121  FROM   wsh_trip_stops_ob_grp_v
2122  WHERE  trip_id = p_trip_id
2123  FOR UPDATE  NOWAIT;
2124 
2125  CURSOR trips IS
2126  SELECT 'x'
2127  FROM   wsh_trips_ob_grp_v
2128  WHERE trip_id = p_trip_id
2129  FOR UPDATE NOWAIT;
2130 
2131     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2132 BEGIN
2133    IF (l_debug = 1) THEN
2134       debug('Begin UPDATE_TRIPSTOP ','UPDATE_TRIPSTOP');
2135       debug('p_trip_id            : ' || p_trip_id              , 'UPDATE_TRIPSTOP');
2136       debug('p_vehicle_item_id    : ' || p_vehicle_item_id      , 'UPDATE_TRIPSTOP');
2137       debug('p_vehicle_num_prefix : ' || p_vehicle_num_prefix   , 'UPDATE_TRIPSTOP');
2138       debug('p_vehicle_num        : ' || p_vehicle_num          , 'UPDATE_TRIPSTOP');
2139       debug('p_seal_code          : ' || p_seal_code            , 'UPDATE_TRIPSTOP');
2140       debug('p_org_id             : ' || p_org_id               , 'UPDATE_TRIPSTOP');
2141       debug('p_dock_door_id       : ' || p_dock_door_id         , 'UPDATE_TRIPSTOP');
2142       debug('p_ship_method_code   : ' || p_ship_method_code     , 'UPDATE_TRIPSTOP');
2143    END IF;
2144    IF p_trip_id <>0 THEN
2145       l_trip_id := p_trip_id;
2146    END IF;
2147    IF p_vehicle_item_id <>0 THEN
2148       l_vehicle_item_id := p_vehicle_item_id;
2149    END IF;
2150    IF p_org_id <> 0 THEN
2151       l_org_id := p_org_id;
2152    END IF;
2153    IF p_dock_door_id <> 0 THEN
2154       l_dock_door_id := p_dock_door_id;
2155    END IF;
2156 
2157    l_return_status := fnd_api.g_ret_sts_success;
2158    IF (l_debug = 1) THEN
2159       debug('In the procedure update trip_stop','trip_Stop');
2160    END IF;
2161 
2162    IF (l_debug = 1) THEN
2163      debug('Update Trip Ship method:'||p_ship_method_code,'Update_tripstop');
2164   END IF;
2165 
2166 -- Update_TripStop will be called from the save button of the Trip-Stop info page.
2167 -- It should update the data entered in the page and also update the freight cost entered
2168 -- if any.
2169 -- When updating the frieght cost info ensure that freight is not entered before.
2170 
2171 -- Check if there is any frieght cost info entered in the page, update it for trip else --create a new
2172 --IF ( p_trip_id IS NULL ) THEN
2173    IF (l_trip_id IS NULL) THEN
2174 
2175   IF (l_debug = 1) THEN
2176      debug('Update Trip No Trip Id is passed','Update_tripstop');
2177   END IF;
2178   RETURN;
2179 ELSE
2180    BEGIN
2181       SELECT name
2182          INTO l_name
2183          FROM wsh_trips_ob_grp_v
2184        WHERE trip_id=l_trip_id;
2185    EXCEPTION
2186       WHEN no_data_found THEN
2187          RETURN;
2188    END;
2189 END IF;
2190 OPEN trip_freight;
2191 LOOP
2192   FETCH trip_freight
2193    into l_row_id
2194         ,l_freight_cost_rec.FREIGHT_COST_TYPE_ID
2195         ,l_freight_cost_rec.CURRENCY_CODE
2196         ,l_freight_cost_rec.UNIT_AMOUNT
2197         ,l_freight_cost_rec.CONVERSION_TYPE_CODE;
2198 
2199        --l_freight_cost_rec.TRIP_ID     :=p_trip_id;
2200     l_freight_cost_rec.TRIP_ID:=l_trip_id;
2201     l_freight_cost_rec.action_code := 'CREATE';
2202   EXIT WHEN trip_freight%NOTFOUND;
2203 
2204   --patchst J.  Shipping API cleanup
2205   WSH_FREIGHT_COSTS_PUB.Create_Update_Freight_Costs
2206     (p_api_version_number     =>1.0
2207      , p_init_msg_list          =>l_init_msg_list
2208      , p_commit                 => FND_API.G_FALSE
2209      , x_return_status          => l_return_status
2210      , x_msg_count                      => l_msg_count
2211      , x_msg_data               => l_msg_DATA
2212      , p_pub_freight_costs      => l_freight_cost_rec
2213      , p_action_code            => 'CREATE'
2214      , x_freight_cost_id        => l_freight_cost_id
2215      );
2216   IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2217      FND_MESSAGE.SET_NAME('WMS','WMS_CREATE_FREIGHT_FAIL');
2218      FND_MESSAGE.SET_TOKEN('OBJ','Trip');
2219      FND_MESSAGE.SET_TOKEN('VAL',l_name);
2220      /* Creation of freight cost for <OBJ> <VAL> has failed  */
2221 
2222      FND_MSG_PUB.ADD;
2223      IF (l_debug = 1) THEN
2224     debug('Create_Update Freight Cost API failed with status E ','Update Trip Stop');
2225      END IF;
2226      RAISE fnd_api.g_exc_error;
2227    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2228      FND_MESSAGE.SET_NAME('WMS','CREATE_FREIGHT_FAIL');
2229      FND_MSG_PUB.ADD;
2230      IF (l_debug = 1) THEN
2231     debug('Update Freight Cost failed with status U','Update TripStop');
2232      END IF;
2233      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2234   END IF;
2235   --\Shipping API cleanup
2236 
2237   UPDATE WMS_FREIGHT_COST_TEMP
2238   SET freight_cost_id   = l_freight_cost_id
2239       ,last_update_date  =  SYSDATE
2240       ,last_updated_by   =  FND_GLOBAL.USER_ID
2241   WHERE rowid = l_row_id;
2242 
2243 END LOOP;
2244 CLOSE trip_freight;
2245 
2246 -- Freight has been updated/ created. Now Update the Trip Stop info which is being entered by user.
2247 -- Seal Code is store in Trip- Stop so we need to updated it. Rest of the info are stored in Trips
2248 
2249 
2250          SELECT stop_id
2251          INTO   l_stop_id
2252          FROM   WSH_TRIP_STOPS_OB_GRP_V wts, WSH_DELIVERY_LEGS_OB_GRP_V wdl
2253         -- WHERE  wts.trip_id = p_trip_id
2254             WHERE wts.trip_id=l_trip_id
2255          AND    wts.stop_id = wdl.pick_up_stop_id
2256        AND ROWNUM <2;
2257 
2258 
2259 
2260    IF (p_seal_code IS NOT NULL ) THEN
2261       --patchset J.  Shipping API cleanup
2262       IF (l_debug = 1) THEN
2263          debug('Call WSH_TRIP_STOPS_PUB.Create_Update_Stop to Update the Seal Code','Update Trip Stop');
2264       END IF;
2265       l_stop_info.stop_id              := l_stop_id;
2266       l_stop_info.trip_id              :=l_trip_id;
2267       l_stop_info.DEPARTURE_SEAL_CODE  := p_seal_code;
2268       WSH_TRIP_STOPS_PUB.Create_Update_Stop(
2269                          p_api_version_number           => 1.0
2270                         ,p_init_msg_list            => l_init_msg_list
2271                         ,x_return_status            => l_return_status
2272                         ,x_msg_count                => l_msg_count
2273                         ,x_msg_data                 => l_msg_data
2274                         ,p_action_code              => 'UPDATE'
2275                         ,p_stop_info                => l_stop_info
2276                         ,p_trip_id                  => l_trip_id
2277                         ,x_stop_id                  => l_stop_id);
2278 
2279          IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2280             FND_MESSAGE.SET_NAME('WMS','WMS_UPD_STOP_FAIL');
2281             FND_MESSAGE.SET_TOKEN('TRIP_NAME',l_name);
2282           -- Failed to Create/update Trip Stop for the trip <TRIP_NAME>
2283             fnd_msg_pub.ADD;
2284             IF (l_debug = 1) THEN
2285                debug('Create_Update_Stop API failed with status E ','Update Trip Stop');
2286             END IF;
2287             RAISE fnd_api.g_exc_error;
2288          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2289             FND_MESSAGE.SET_NAME('WMS','WMS_UPD_STOP_FAIL');
2290             FND_MESSAGE.SET_TOKEN('TRIP_NAME',l_name);
2291           -- Failed to Create/update Trip Stop for the trip <TRIP_NAME>
2292             fnd_msg_pub.ADD;
2293             IF (l_debug = 1) THEN
2294                debug('Create_Update_Stop failed with status U','Update TripStop');
2295             END IF;
2296             raise FND_API.G_EXC_UNEXPECTED_ERROR;
2297          END IF;
2298      --\Shipping API cleanup
2299   END IF; -- Update Stop
2300 
2301  -- After Updating the Stop with Seal code, Update the Trip with Vehicle_item_id etc.
2302 
2303    IF (l_debug = 1) THEN
2304        debug('Update Trip also ','Update_Trip_Stop');
2305     END IF;
2306   p_trip_info.trip_id                                    :=l_trip_id;
2307  p_trip_info.VEHICLE_ITEM_ID         :=l_vehicle_item_id;
2308  p_trip_info.vehicle_organization_id  :=l_org_id;
2309  p_trip_info.VEHICLE_NUMBER                      := p_vehicle_num;
2310  p_trip_info.VEHICLE_NUM_PREFIX                  := p_vehicle_num_prefix;
2311  p_trip_info.last_update_date           := SYSDATE;
2312  p_trip_info.last_updated_by            :=FND_GLOBAL.USER_ID;
2313  p_trip_info.last_update_login          := FND_GLOBAL.USER_ID;
2314  p_trip_info.ship_method_code           :=p_ship_method_code;    --Bug 2980013:Updating the Ship Method Code*/
2315 
2316  IF (l_debug = 1) THEN
2317     debug('Call to Create_update_Trip','Update_Trip_Stop');
2318  END IF;
2319 
2320           WSH_TRIPS_PUB.Create_Update_Trip  (
2321                         p_api_version_number  => 1.0
2322                        ,p_init_msg_list       => l_init_msg_list
2323                        ,x_return_status       => l_return_status
2324                        ,x_msg_count           => l_msg_count
2325                        ,x_msg_data            => l_msg_data
2326                        ,p_action_code         => 'UPDATE'
2327                        ,p_trip_info           => p_trip_info
2328                        ,x_trip_id             => l_trip_id
2329                        ,x_trip_name           => l_trip_name);
2330          IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2331             IF (l_debug = 1) THEN
2332                debug('Create_Update_Trip API failed with status E ','Update Trip Stop');
2333             END IF;
2334             RAISE fnd_api.g_exc_error;
2335          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2336             IF (l_debug = 1) THEN
2337                debug('Create_Update_Trip failed with status U','Update TripStop');
2338             END IF;
2339             raise FND_API.G_EXC_UNEXPECTED_ERROR;
2340          END IF;
2341 
2342     -- Update Trip
2343 
2344 COMMIT;
2345 
2346 EXCEPTION
2347    WHEN fnd_api.g_exc_error THEN
2348       x_return_status := fnd_api.g_ret_sts_error;
2349       IF (l_debug = 1) THEN
2350          debug('In exception (E) ','Update_Trip_Stop');
2351       END IF;
2352       ROLLBACK;
2353       IF trip_freight%ISOPEN THEN
2354              CLOSE trip_freight;
2355       END IF;
2356       fnd_msg_pub.count_and_get
2357             ( p_count => x_msg_count,
2358               p_data  => x_msg_data
2359             );
2360 
2361    WHEN fnd_api.g_exc_unexpected_error THEN
2362       x_return_status := fnd_api.g_ret_sts_unexp_error;
2363       IF (l_debug = 1) THEN
2364          debug('Update of Trip Stop has failed :Unexpected Error','Update_Trip_Stop');
2365       END IF;
2366       ROLLBACK;
2367       IF trip_freight%ISOPEN THEN
2368              CLOSE trip_freight;
2369       END IF;
2370       fnd_msg_pub.count_and_get
2371             ( p_count => x_msg_count,
2372               p_data  => x_msg_data
2373             );
2374 
2375    WHEN OTHERS THEN
2376       ROLLBACK;
2377       x_return_status := fnd_api.g_ret_sts_unexp_error ;
2378       IF (l_debug = 1) THEN
2379          debug('Update of Trip has failed : Unexpected Error '||SQLERRM,'Update_trip_Stop');
2380       END IF;
2381       IF trip_freight%ISOPEN THEN
2382              CLOSE trip_freight;
2383       END IF;
2384           IF SQLCODE IS NOT NULL THEN
2385             IF (l_debug = 1) THEN
2386                debug(SQLCODE,'Update_Trip_Stop');
2387             END IF;
2388           END IF;
2389 
2390 END;
2391 
2392 PROCEDURE print_shipping_document(
2393   x_return_status      OUT NOCOPY    VARCHAR2
2394 , x_msg_count          OUT NOCOPY    NUMBER
2395 , x_msg_data           OUT NOCOPY    VARCHAR2
2396 , p_trip_id            IN            NUMBER
2397 , p_vehicle_item_id    IN            NUMBER
2398 , p_vehicle_num_prefix IN            VARCHAR2
2399 , p_vehicle_num        IN            VARCHAR2
2400 , p_seal_code          IN            VARCHAR2
2401 , p_document_set_id    IN            NUMBER
2402 , p_org_id             IN            NUMBER DEFAULT NULL
2403 , p_dock_door_id       IN            NUMBER DEFAULT NULL
2404 , p_ship_method_code   IN            VARCHAR2 DEFAULT NULL
2405 ) IS
2406   CURSOR stop_cur IS
2407     SELECT wdl.pick_up_stop_id pick_up_stop_id
2408       FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
2409      WHERE wts.trip_id = p_trip_id
2410        AND wdl.pick_up_stop_id = wts.stop_id;
2411 
2412   CURSOR del_cur IS
2413     SELECT delivery_id
2414       FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
2415      WHERE wts.trip_id = p_trip_id
2416        AND wdl.pick_up_stop_id = wts.stop_id;
2417 
2418   l_del_cur             del_cur%ROWTYPE;
2419   l_stop_cur            stop_cur%ROWTYPE;
2420   l_init_msg_list       VARCHAR2(1)                             := fnd_api.g_true;
2421   l_return_status       VARCHAR2(1)                             := fnd_api.g_ret_sts_success;
2422   l_msg_count           NUMBER;
2423   l_msg_data            VARCHAR2(20000);
2424   l_report_set_id       NUMBER;
2425   l_trip_id             wsh_util_core.id_tab_type;
2426   l_stop_id             wsh_util_core.id_tab_type;
2427   l_delivery_id         wsh_util_core.id_tab_type;
2428   l_document_param_info wsh_document_sets.document_set_tab_type;
2429   l_count1              NUMBER;
2430   l_count2              NUMBER;
2431   l_dummy               VARCHAR2(20000);
2432   l_num                 NUMBER;
2433   l_debug               NUMBER                                  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2434 
2435 BEGIN
2436   x_return_status  := fnd_api.g_ret_sts_success;
2437 
2438   IF (l_debug = 1) THEN
2439       debug('Begin print_shipping_document ','print_shipping_document');
2440       debug('p_trip_id            : ' || p_trip_id              , 'print_shipping_document');
2441       debug('p_vehicle_item_id    : ' || p_vehicle_item_id      , 'print_shipping_document');
2442       debug('p_vehicle_num_prefix : ' || p_vehicle_num_prefix   , 'print_shipping_document');
2443       debug('p_vehicle_num        : ' || p_vehicle_num          , 'print_shipping_document');
2444       debug('p_seal_code          : ' || p_seal_code            , 'print_shipping_document');
2445       debug('p_document_set_id    : ' || p_document_set_id      , 'print_shipping_document');
2446       debug('p_org_id             : ' || p_org_id               , 'print_shipping_document');
2447       debug('p_dock_door_id       : ' || p_dock_door_id         , 'print_shipping_document');
2448       debug('p_ship_method_code   : ' || p_ship_method_code     , 'print_shipping_document');
2449   END IF;
2450 
2451   wms_direct_ship_pvt.update_tripstop(
2452     x_return_status              => l_return_status
2453   , x_msg_count                  => l_msg_count
2454   , x_msg_data                   => l_msg_data
2455   , p_trip_id                    => p_trip_id
2456   , p_vehicle_item_id            => p_vehicle_item_id
2457   , p_vehicle_num_prefix         => p_vehicle_num_prefix
2458   , p_vehicle_num                => p_vehicle_num
2459   , p_seal_code                  => p_seal_code
2460   , p_org_id                     => p_org_id
2461   , p_dock_door_id               => p_dock_door_id
2462   , p_ship_method_code           => p_ship_method_code
2463   );
2464 
2465   IF l_return_status = fnd_api.g_ret_sts_error THEN
2466     IF (l_debug = 1) THEN
2467       DEBUG('Update tripstop API failed with status E ', 'Print_shipping_dcouments');
2468     END IF;
2469 
2470     RAISE fnd_api.g_exc_error;
2471   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2472     IF (l_debug = 1) THEN
2473       DEBUG('Update tripstop API failed with status U ', 'Print_shipping_dcouments');
2474     END IF;
2475 
2476     RAISE fnd_api.g_exc_unexpected_error;
2477   END IF;
2478 
2479   IF p_document_set_id = 0 THEN
2480     l_report_set_id  := NULL;
2481   ELSE
2482     l_report_set_id  := p_document_set_id;
2483   END IF;
2484 
2485   IF (l_report_set_id IS NULL) THEN
2486      BEGIN
2487       SELECT delivery_report_set_id
2488         INTO l_report_set_id
2489         FROM wsh_shipping_parameters
2490        WHERE organization_id = p_org_id
2491          AND ROWNUM < 2;
2492     EXCEPTION
2493       WHEN NO_DATA_FOUND THEN
2494         l_report_set_id  := NULL;
2495     -- Return an error message that says no default Document is defined for the org
2496     --- So Document printing will not happen
2497     END;
2498   END IF;
2499 
2500   IF (l_report_set_id IS NOT NULL) THEN
2501     OPEN del_cur;
2502 
2503     LOOP
2504       FETCH del_cur INTO l_del_cur;
2505       EXIT WHEN del_cur%NOTFOUND;
2506       l_delivery_id(del_cur%ROWCOUNT)    := l_del_cur.delivery_id;
2507     END LOOP;
2508 
2509     CLOSE del_cur;
2510 
2511     IF (l_debug = 1) THEN
2512       DEBUG('Call WSH_DOCUMENT_SETS.Print_Document_Sets  Document Set Id ' ||
2513              l_report_set_id, 'Print_Shipping_Documents');
2514     END IF;
2515 
2516     l_count1      := 0;
2517     l_count2      := 0;
2518     l_count1      := fnd_msg_pub.count_msg();
2519 
2520     IF (l_debug = 1) THEN
2521       DEBUG('Call WSH_DOCUMENT_SETS.Print_document_Sets for all the deliveries');
2522       DEBUG('Msg table count' || l_count1, 'Print_Shipping_document');
2523     END IF;
2524 
2525     /* bug 13576503 */
2526     IF (p_trip_id IS NOT NULL) THEN
2527        l_document_param_info(1).p_trip_id :=p_trip_id;
2528     END IF;
2529     /* bug 13576503 */
2530 
2531     wsh_document_sets.print_document_sets(
2532       p_report_set_id              => l_report_set_id
2533     , p_organization_id            => p_org_id
2534     , p_trip_ids                   => l_trip_id
2535     , p_stop_ids                   => l_stop_id
2536     , p_delivery_ids               => l_delivery_id
2537     , p_document_param_info        => l_document_param_info
2538     , x_return_status              => l_return_status
2539     );
2540     l_count2      := fnd_msg_pub.count_msg();
2541 
2542     IF (l_debug = 1) THEN
2543       DEBUG('Msg table count' || l_count2, 'Print_Shipping_document');
2544     END IF;
2545 
2546     IF l_count2 > l_count1 THEN
2547       l_count1  := l_count1 + 2;
2548 
2549       FOR i IN REVERSE l_count1 .. l_count2 LOOP
2550         l_dummy  := fnd_msg_pub.get(i, fnd_api.g_true);
2551 
2552         IF (l_debug = 1) THEN
2553           DEBUG('Messages are' || l_dummy, 'Print_Shipping_document');
2554         END IF;
2555 
2556         fnd_msg_pub.delete_msg(i);
2557       END LOOP;
2558     END IF;
2559 
2560     IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2561       fnd_message.set_name('WMS', 'WMS_PRINT_DOC_SET_FAIL');
2562       /* Failed to print the documents set */
2563       fnd_msg_pub.ADD;
2564 
2565       IF (l_debug = 1) THEN
2566         DEBUG('Print Document_sets API failed with status E ', 'Print_shipping_dcouments');
2567       END IF;
2568     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2569       fnd_message.set_name('WMS', 'WMS_PRINT_DOC_SET_FAIL');
2570       /* Failed to print the documents set */
2571       fnd_msg_pub.ADD;
2572 
2573       IF (l_debug = 1) THEN
2574         DEBUG('Print Document_sets API failed with status U ', 'Print_shipping_dcouments');
2575       END IF;
2576     END IF;
2577 
2578   END IF; -- Print report set
2579 EXCEPTION
2580   WHEN fnd_api.g_exc_error THEN
2581     x_return_status  := fnd_api.g_ret_sts_error;
2582 
2583     IF (l_debug = 1) THEN
2584       DEBUG('Print Document has failed :Error', 'Print_shipping_dcouments');
2585     END IF;
2586 
2587     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2588     ROLLBACK;
2589   -- Think of some messages here.
2590   WHEN fnd_api.g_exc_unexpected_error THEN
2591     IF (l_debug = 1) THEN
2592       DEBUG('Print Document has failed with status U', 'Print_shipping_dcouments');
2593     END IF;
2594 
2595     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2596     ROLLBACK;
2597     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2598 
2599     -- Think of some messages here.
2600     IF (l_debug = 1) THEN
2601       DEBUG('Print Document has failed :Unexpected Error', 'Print_shipping_dcouments');
2602     END IF;
2603   WHEN OTHERS THEN
2604     ROLLBACK;
2605     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2606 
2607     IF (l_debug = 1) THEN
2608       DEBUG('Print Document has failed : Unexpected Error', 'Print_shipping_dcouments');
2609     END IF;
2610 
2611     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2612 
2613     IF SQLCODE IS NOT NULL THEN
2614       IF (l_debug = 1) THEN
2615         DEBUG(SQLCODE, 'Print_shipping_dcouments');
2616       END IF;
2617     END IF;
2618 END;
2619 
2620   -- PRINT_SHIPPING_DOCUMENT
2621 
2622 PROCEDURE close_trip(
2623   x_return_status      OUT NOCOPY    VARCHAR2
2624 , x_msg_count          OUT NOCOPY    NUMBER
2625 , x_msg_data           OUT NOCOPY    VARCHAR2
2626 , p_trip_id            IN            NUMBER
2627 , p_vehicle_item_id    IN            NUMBER
2628 , p_vehicle_num_prefix IN            VARCHAR2
2629 , p_vehicle_num        IN            VARCHAR2
2630 , p_seal_code          IN            VARCHAR2
2631 , p_document_set_id    IN            NUMBER
2632 , p_org_id             IN            NUMBER DEFAULT NULL
2633 , p_dock_door_id       IN            NUMBER DEFAULT NULL
2634 , p_ship_method_code   IN            VARCHAR2 DEFAULT NULL
2635 ) IS
2636   l_init_msg_list        VARCHAR2(1)               := fnd_api.g_true;
2637   l_return_status        VARCHAR2(1)               := fnd_api.g_ret_sts_success;
2638   l_msg_count            NUMBER;
2639   l_msg_data             VARCHAR2(20000);
2640   l_stop_id              NUMBER;
2641   l_trip_name            VARCHAR2(30);
2642   l_trip_id              NUMBER;
2643   l_outermost_lpn        VARCHAR2(30);
2644   l_loaded_dock_door     VARCHAR2(2000);
2645   l_name                 VARCHAR2(30);
2646   l_delivery_details_tab wsh_util_core.id_tab_type;
2647   l_delivery_id          NUMBER;
2648   l_del_index            NUMBER;
2649   l_enforce_ship_method  VARCHAR2(1);
2650   no_ship_method_code    EXCEPTION;
2651 
2652   CURSOR lpn_in_other_dock(p_trip_id NUMBER) IS
2653     SELECT DISTINCT milk.concatenated_segments
2654                   , wstt.outermost_lpn
2655                FROM mtl_item_locations_kfv milk, wms_shipping_transaction_temp wstt
2656               WHERE wstt.trip_id = p_trip_id
2657                 AND wstt.organization_id = p_org_id
2658                 AND wstt.dock_appoint_flag = 'N'
2659                 AND wstt.direct_ship_flag = 'Y'
2660                 AND wstt.dock_door_id <> p_dock_door_id
2661                 AND milk.organization_id = p_org_id
2662                 AND milk.inventory_location_id = wstt.dock_door_id;
2663 
2664   CURSOR closed_trips IS
2665     SELECT DISTINCT wstt.trip_id
2666                FROM wms_shipping_transaction_temp wstt, wsh_trips_ob_grp_v wt
2667               WHERE wstt.organization_id = p_org_id
2668                 AND wstt.dock_door_id = p_dock_door_id
2669                 AND wt.trip_id = wstt.trip_id
2670                 AND wt.status_code IN('CL', 'IT');
2671 
2672   CURSOR delivery_details IS
2673     SELECT DISTINCT wstt.delivery_id
2674                FROM wms_shipping_transaction_temp wstt
2675               WHERE wstt.organization_id = p_org_id
2676                 AND wstt.dock_door_id = p_dock_door_id
2677                 AND wstt.trip_id = p_trip_id;
2678 
2679   l_debug                NUMBER                    := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2680 BEGIN
2681   IF (l_debug = 1) THEN
2682     DEBUG('p_trip_id            : ' || p_trip_id, 'close_trip');
2683     DEBUG('p_vehicle_item_id    : ' || p_vehicle_item_id, 'close_trip');
2684     DEBUG('p_vehicle_num_prefix : ' || p_vehicle_num_prefix, 'close_trip');
2685     DEBUG('p_vehicle_num        : ' || p_vehicle_num, 'close_trip');
2686     DEBUG('p_seal_code          : ' || p_seal_code, 'close_trip');
2687     DEBUG('p_document_set_id    : ' || p_document_set_id, 'close_trip');
2688     DEBUG('p_org_id             : ' || p_org_id, 'close_trip');
2689     DEBUG('p_dock_door_id       : ' || p_dock_door_id, 'close_trip');
2690     DEBUG('p_ship_method_code   : ' || p_ship_method_code, 'close_trip');
2691   END IF;
2692 
2693   x_return_status  := fnd_api.g_ret_sts_success;
2694   fnd_msg_pub.initialize;
2695 
2696   IF (l_debug = 1) THEN
2697     DEBUG('Begin Close Trip ', 'CLOSE_TRIP');
2698   END IF;
2699 
2700   -- Call Document Printing API which takes care of Updating the delivery and Printing the Doc.
2701   IF (l_debug = 1) THEN
2702     DEBUG('Call Print Document ', 'CLOSE_TRIP');
2703   END IF;
2704 
2705   IF p_trip_id IS NULL
2706      OR p_trip_id = 0 THEN
2707     IF (l_debug = 1) THEN
2708       DEBUG('The value for trip_id passed is zero. Exiting out ', 'close_trip');
2709     END IF;
2710 
2711     RAISE fnd_api.g_exc_error;
2712   ELSE
2713     BEGIN
2714       SELECT NAME
2715         INTO l_name
2716         FROM wsh_trips_ob_grp_v
2717        WHERE trip_id = p_trip_id;
2718     EXCEPTION
2719       WHEN NO_DATA_FOUND THEN
2720         NULL;
2721     END;
2722   END IF;
2723 
2724   IF (l_debug=1) then
2725      DEBUG('Checking Ship Method Validity.', 'Close_Trip');
2726   END IF;
2727   /* Checking if the Ship Method has been entered if the enforce Ship method flag is set to Y*/
2728 
2729   INV_SHIPPING_TRANSACTION_PUB.get_enforce_ship
2730     (p_org_id         => p_org_id
2731      ,x_enforce_ship  => l_enforce_ship_method
2732      ,x_return_status => l_return_status
2733      ,x_msg_data      => l_msg_data
2734      ,x_msg_count     => l_msg_count);
2735 
2736   IF (l_debug=1) THEN
2737      debug('get_enforce_ship returned status: ' || l_return_status,'Close_Trip');
2738      debug('Enforce_ship_method flag: ' || l_enforce_ship_method,'Close_trip');
2739   END IF;
2740 
2741   IF l_enforce_ship_method = 'Y'
2742      AND p_ship_method_code IS NULL THEN
2743     RAISE no_ship_method_code;
2744   END IF;
2745 
2746   wms_direct_ship_pvt.print_shipping_document(
2747     x_return_status              => l_return_status
2748   , x_msg_count                  => l_msg_count
2749   , x_msg_data                   => l_msg_data
2750   , p_trip_id                    => p_trip_id
2751   , p_vehicle_item_id            => p_vehicle_item_id
2752   , p_vehicle_num_prefix         => p_vehicle_num_prefix
2753   , p_vehicle_num                => p_vehicle_num
2754   , p_seal_code                  => p_seal_code
2755   , p_document_set_id            => p_document_set_id
2756   , p_org_id                     => p_org_id
2757   , p_dock_door_id               => p_dock_door_id
2758   , p_ship_method_code           => p_ship_method_code
2759   );
2760 
2761   IF l_return_status = fnd_api.g_ret_sts_error THEN
2762     IF (l_debug = 1) THEN
2763       DEBUG('Print shipping documents API failed with status E ', 'Close_Trip');
2764     END IF;
2765 
2766     RAISE fnd_api.g_exc_error;
2767   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2768     IF (l_debug = 1) THEN
2769       DEBUG('Print shipping documents API failed with status U', 'Close_Trip');
2770     END IF;
2771 
2772     RAISE fnd_api.g_exc_unexpected_error;
2773   END IF;
2774 
2775   -- Check if there is an LPN loaded on a different Dock Door for the Trip. If so throw an error
2776        -- Error code in this case is
2777   BEGIN
2778     IF (l_debug = 1) THEN
2779       DEBUG('Check if there is an LPN loaded on different dock door for the Trip', 'Close_Trip');
2780     END IF;
2781 
2782     OPEN lpn_in_other_dock(p_trip_id);
2783     FETCH lpn_in_other_dock INTO l_loaded_dock_door, l_outermost_lpn;
2784 
2785     IF lpn_in_other_dock%FOUND THEN
2786       SELECT NAME
2787         INTO l_trip_name
2788         FROM wsh_trips_ob_grp_v
2789        WHERE trip_id = p_trip_id;
2790 
2791       -- Raise error and set the message to be shown in Mobile
2792       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2793       /*LPN LPN_NAME assigned to trip TRIP is loaded on another dock door DOCK . Hence close trip failed.*/
2794       fnd_message.set_token('LPN_NAME', l_outermost_lpn);
2795       fnd_message.set_token('TRIP', l_trip_name);
2796       fnd_message.set_token('DOCK', l_loaded_dock_door);
2797       /*Close Trip failed */
2798       fnd_msg_pub.ADD;
2799       x_return_status  := fnd_api.g_ret_sts_error;
2800       CLOSE lpn_in_other_dock;
2801       RETURN;
2802     END IF;
2803 
2804     CLOSE lpn_in_other_dock;
2805 
2806     IF (l_debug = 1) THEN
2807       DEBUG('All LPNs for the Trip are loaded on the same dock door', 'Close_Trip');
2808     END IF;
2809   EXCEPTION
2810     WHEN NO_DATA_FOUND THEN
2811       NULL;
2812   END;
2813 
2814   -- Get the pickup stop for the trip so  that it may be closed.
2815   IF (l_debug = 1) THEN
2816     DEBUG('Close Trip. Get the pickup stop of the trip', 'Close_Trip');
2817   END IF;
2818 
2819   SELECT stop_id
2820     INTO l_stop_id
2821     FROM wsh_trip_stops_ob_grp_v wts, wsh_delivery_legs_ob_grp_v wdl
2822    WHERE wts.trip_id = p_trip_id
2823      AND wts.stop_id = wdl.pick_up_stop_id
2824      AND ROWNUM < 2;
2825 
2826   IF (l_debug = 1) THEN
2827     DEBUG('Interface the Trip now. Trip Id :' || p_trip_id || '  Pick Up Stop Id: ' || l_stop_id, 'Close_Trip');
2828   END IF;
2829 
2830   wsh_trip_stops_pub.stop_action(
2831     p_api_version_number         => 1.0
2832   , p_init_msg_list              => l_init_msg_list
2833   , x_return_status              => l_return_status
2834   , x_msg_count                  => l_msg_count
2835   , x_msg_data                   => l_msg_data
2836   , p_action_code                => 'CLOSE'
2837   , p_stop_id                    => l_stop_id
2838   , p_trip_id                    => p_trip_id
2839   , p_defer_interface_flag       => 'N'
2840   );
2841 
2842   IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2843     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2844     fnd_message.set_token('TRIP_NAME', l_name);
2845     /*Failed to close the trip TRIP_NAME*/
2846     fnd_msg_pub.ADD;
2847 
2848     IF (l_debug = 1) THEN
2849       DEBUG('Stop_Action API failed with status E ', 'Close_Trip');
2850     END IF;
2851 
2852     RAISE fnd_api.g_exc_error;
2853   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2854     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2855     fnd_message.set_token('TRIP_NAME', l_name);
2856     /*Failed to close the trip TRIP_NAME*/
2857     fnd_msg_pub.ADD;
2858 
2859     IF (l_debug = 1) THEN
2860       DEBUG('Stop_Action API failed with status U', 'Close_Trip');
2861     END IF;
2862 
2863     RAISE fnd_api.g_exc_unexpected_error;
2864   END IF;
2865 
2866   /*8973130-starts */
2867   -- Get the drop off stop for the trip so  that it may be closed.
2868   IF (l_debug = 1) THEN
2869     DEBUG('Close Trip. Get the drop off stop of the trip', 'Close_Trip');
2870   END IF;
2871 
2872   SELECT stop_id
2873     INTO l_stop_id
2874     FROM wsh_trip_stops_ob_grp_v wts, wsh_delivery_legs_ob_grp_v wdl
2875    WHERE wts.trip_id = p_trip_id
2876      AND wts.stop_id = wdl.drop_off_stop_id
2877      AND ROWNUM < 2;
2878 
2879   IF (l_debug = 1) THEN
2880     DEBUG(' Trip Id :' || p_trip_id || '  Drop Off Stop Id: ' || l_stop_id, 'Close_Trip');
2881   END IF;
2882 
2883   wsh_trip_stops_pub.stop_action(
2884     p_api_version_number         => 1.0
2885   , p_init_msg_list              => l_init_msg_list
2886   , x_return_status              => l_return_status
2887   , x_msg_count                  => l_msg_count
2888   , x_msg_data                   => l_msg_data
2889   , p_action_code                => 'CLOSE'
2890   , p_stop_id                    => l_stop_id
2891   , p_trip_id                    => p_trip_id
2892   , p_defer_interface_flag       => 'N'
2893   );
2894 
2895   IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2896     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2897     fnd_message.set_token('TRIP_NAME', l_name);
2898     /*Failed to close the trip TRIP_NAME*/
2899     fnd_msg_pub.ADD;
2900 
2901     IF (l_debug = 1) THEN
2902       DEBUG('Stop_Action API failed with status E ', 'Close_Trip');
2903     END IF;
2904 
2905     RAISE fnd_api.g_exc_error;
2906   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2907     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2908     fnd_message.set_token('TRIP_NAME', l_name);
2909     /*Failed to close the trip TRIP_NAME*/
2910     fnd_msg_pub.ADD;
2911 
2912     IF (l_debug = 1) THEN
2913       DEBUG('Stop_Action API failed with status U', 'Close_Trip');
2914     END IF;
2915 
2916     RAISE fnd_api.g_exc_unexpected_error;
2917   END IF;
2918   /*8973130-Ends */
2919 
2920   OPEN delivery_details;
2921   l_del_index      := 1;
2922 
2923   LOOP
2924     FETCH delivery_details INTO l_delivery_id;
2925     EXIT WHEN delivery_details%NOTFOUND;
2926     l_delivery_details_tab(l_del_index)  := l_delivery_id;
2927     l_del_index                          := l_del_index + 1;
2928   END LOOP;
2929 
2930   wms_shipping_transaction_pub.print_label(l_delivery_details_tab, l_return_status);
2931 
2932   IF x_return_status <>(fnd_api.g_ret_sts_success) THEN
2933     fnd_message.set_name('INV', 'INV_RCV_CRT_PRINT_LAB_FAIL');
2934 
2935     IF (l_debug = 1) THEN
2936       DEBUG('Label Printing failed ', 'Close Trip');
2937     END IF;
2938 
2939     fnd_msg_pub.ADD;
2940     l_return_status  := 'W';
2941   END IF;
2942 
2943   -- Clean up trips which are closed
2944   l_trip_id        := p_trip_id;
2945   OPEN closed_trips;
2946 
2947   LOOP
2948     FETCH closed_trips INTO l_trip_id;
2949     EXIT WHEN closed_trips%NOTFOUND;
2950     wms_direct_ship_pvt.cleanup_temp_recs(
2951       x_msg_data                   => x_msg_data
2952     , x_msg_count                  => x_msg_count
2953     , x_return_status              => x_return_status
2954     , p_org_id                     => p_org_id
2955     , p_outermost_lpn_id           => NULL
2956     , p_trip_id                    => l_trip_id
2957     );
2958 
2959     IF l_return_status =(fnd_api.g_ret_sts_error) THEN
2960       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2961       /*Failed while running cleanup program */
2962       fnd_msg_pub.ADD;
2963 
2964       IF (l_debug = 1) THEN
2965         DEBUG('CLEANUP_TEMP_RECS API failed with status E ', 'Close_Trip');
2966       END IF;
2967 
2968       RAISE fnd_api.g_exc_error;
2969     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2970       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2971       /*Failed while running cleanup program */
2972       fnd_msg_pub.ADD;
2973 
2974       IF (l_debug = 1) THEN
2975         DEBUG('CLEANUP_TEMP_RECS API failed with status U', 'Close_Trip');
2976       END IF;
2977 
2978       RAISE fnd_api.g_exc_unexpected_error;
2979     END IF;
2980   END LOOP;
2981 
2982   CLOSE closed_trips;
2983 -- Call clean up API at this time. This API will create
2984 EXCEPTION
2985   WHEN no_ship_method_code THEN
2986     x_return_status  := fnd_api.g_ret_sts_error;
2987     fnd_message.set_name('WMS', 'WMS_SHIP_METHOD_CODE');
2988     fnd_message.set_token('TRIP_NAME', l_name);
2989     /* No Ship method code provided for the Trip .This is required */
2990     fnd_msg_pub.ADD;
2991     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2992     DEBUG('In exception no_ship_method_code - - errorcode ', 'Close Trip');
2993   WHEN fnd_api.g_exc_error THEN
2994     x_return_status  := fnd_api.g_ret_sts_error;
2995     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2996     ROLLBACK;
2997 
2998     -- Think of some messages here.
2999     IF (l_debug = 1) THEN
3000       DEBUG('Close Trip has failed :Error', 'Close_Trip');
3001     END IF;
3002   WHEN fnd_api.g_exc_unexpected_error THEN
3003     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3004     ROLLBACK;
3005 
3006     -- Think of some messages here.
3007     IF (l_debug = 1) THEN
3008       DEBUG('Close Trip has failed :Unexpected Error', 'Close_Trip');
3009     END IF;
3010 
3011     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3012   WHEN OTHERS THEN
3013     ROLLBACK;
3014     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3015 
3016     IF (l_debug = 1) THEN
3017       DEBUG('Close Trip has failed : Unexpected Error', 'Close_Trip');
3018     END IF;
3019 
3020     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3021 
3022     IF SQLCODE IS NOT NULL THEN
3023       IF (l_debug = 1) THEN
3024         DEBUG(SQLCODE, 'Close_Trip');
3025       END IF;
3026     END IF;
3027 END;
3028 
3029   --CLOSE_TRIP
3030 
3031 -- API Name         :
3032 -- Type             : Procedure
3033 -- Function         : This procedure does the following:
3034 --                    1.Change LPN context to "Resides in Inventory"
3035 --                    2.Unpack the LPN in Shipping
3036 --                    3.Remove inventory details from Shipping (Sub, Loc, Qty).
3037 --                    4.Remove the Reservation records
3038 --                    5.Reset the serial_number_control_code,current_status
3039 --                     ->if serial control code is "At SO Issue", reset current status to "Defined but not used"
3040 --                     ->if serial control code is "Predefined" or "At Receipt" reset status to "resides in stores"
3041 --                     ->Reset Group Mark Id
3042 -- Input Parameters :
3043 --   p_org_id             Organization Id
3044 --   p_outermost_lpn_id   Outermost LPN Id
3045 -- Output Parameters    :
3046 --   x_return_status      Standard Output Parameter
3047 --   x_msg_count          Standard Output Parameter
3048 --   x_msg_data           Standard Output Parameter
3049 
3050 PROCEDURE unload_truck(
3051   x_return_status    OUT NOCOPY    VARCHAR2
3052 , x_msg_count        OUT NOCOPY    NUMBER
3053 , x_msg_data         OUT NOCOPY    VARCHAR2
3054 , p_org_id           IN            NUMBER
3055 , p_outermost_lpn_id IN            NUMBER
3056 , p_relieve_rsv      IN            VARCHAR2 DEFAULT 'Y'/*added 3396821*/
3057 ) IS
3058   CURSOR wda_del_detail(l_outermost_dd_id NUMBER) IS
3059     SELECT     delivery_detail_id
3060           FROM wsh_delivery_assignments_v
3061     START WITH delivery_detail_id = l_outermost_dd_id
3062     CONNECT BY PRIOR delivery_detail_id = parent_delivery_detail_id;
3063 
3064   CURSOR wdd_del_detail(p_delivery_detail_id IN NUMBER) IS
3065     SELECT   wdd.delivery_detail_id
3066            , wdd.inventory_item_id
3067            , wdd.serial_number
3068            , wdd.source_line_id
3069            , wdd.requested_quantity
3070            , wdd.transaction_temp_id
3071         FROM wsh_delivery_details_ob_grp_v wdd
3072        WHERE wdd.organization_id = p_org_id
3073          AND wdd.container_flag <> 'Y'
3074          AND wdd.delivery_detail_id = p_delivery_detail_id
3075     ORDER BY wdd.source_line_id;
3076 
3077   CURSOR del_lpn IS
3078      SELECT lpn_id
3079      FROM wms_license_plate_numbers wlpn
3080      WHERE wlpn.outermost_lpn_id = p_outermost_lpn_id;
3081 
3082   l_del_lpn             del_lpn%ROWTYPE;
3083   l_del_det_id          NUMBER;
3084   l_status_code         VARCHAR2(2);
3085   l_init_msg_list       VARCHAR2(1)                             := fnd_api.g_true;
3086   l_del_id              NUMBER;
3087   l_trip_id             NUMBER;
3088   l_trip_name           VARCHAR2(30);
3089   l_delivery_detail_id  NUMBER;
3090   counter               NUMBER;
3091   x_error_code          NUMBER;
3092   l_return_status       VARCHAR2(1);
3093   l_msg_count           NUMBER;
3094   l_msg_data            VARCHAR2(20000);
3095   l_serial_ctrl_code    NUMBER                                  := 1;
3096   l_item_id             NUMBER;
3097   l_source_line_id      NUMBER;
3098   l_prev_src_line_id    NUMBER;
3099   l_min_del_detail_id   NUMBER                                  := 0;
3100   l_sum_req_qty         NUMBER                                  := 0;
3101   l_serial_number       mtl_serial_numbers.serial_number%TYPE;
3102   l_outermost_dd_id     NUMBER;
3103   l_req_qty             NUMBER;
3104   l_transaction_temp_id NUMBER;
3105 
3106   /* Mrana: 7/6/06: 5350778 : Added foll. variables */
3107   l_lot_ctrl_code       NUMBER                                  := 1;
3108   l_lot_divisible_flag  VARCHAR2(1)                             := NULL;
3109   l_relieve_rsv         VARCHAR2(1)                             := NULL;
3110   l_dual_uom_item       BOOLEAN := FALSE;
3111 
3112   TYPE delivery_detail_r IS RECORD(
3113     delivery_detail_id NUMBER
3114   , source_line_id     NUMBER
3115   );
3116 
3117   TYPE delivery_detail_t IS TABLE OF delivery_detail_r
3118     INDEX BY BINARY_INTEGER;
3119 
3120   l_delivery_detail_tab delivery_detail_t;
3121 
3122   l_lpn_del_det_tab wsh_util_core.id_tab_type;
3123 
3124   l_freight_costs      WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
3125 
3126   l_debug               NUMBER                                  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3127   l_lpn_tbl                     WMS_Data_Type_Definitions_PUB.LPNTableType;
3128   l_lpn_rec                     WMS_Data_Type_Definitions_PUB.LPNRecordType;
3129 
3130   cursor lpn_cur is
3131     select lpn_id, organization_id
3132     from wms_license_plate_numbers
3133     where outermost_lpn_id = p_outermost_lpn_id;
3134 
3135 
3136 BEGIN
3137   x_return_status  := fnd_api.g_ret_sts_success;
3138 
3139   IF (l_debug = 1) THEN
3140     DEBUG('p_org_id: ' || p_org_id, 'UNLOAD_TRUCK');
3141     DEBUG('p_outermost_lpn_id: ' || p_outermost_lpn_id, 'UNLOAD_TRUCK');
3142     DEBUG('p_relieve_rsv: ' || p_relieve_rsv, 'UNLOAD_TRUCK');
3143     DEBUG('Before deleting the freight record corresponding to LPN', 'UNLOAD_TRUCK');
3144   END IF;
3145 
3146   --patchset J.  Shipping API cleanup
3147   --delete freight costs
3148   BEGIN
3149      SELECT wdd.delivery_detail_id, wfc.freight_cost_id
3150        INTO l_freight_costs.delivery_detail_id,
3151        l_freight_costs.freight_cost_id
3152        FROM wsh_delivery_details_ob_grp_v wdd,
3153        wsh_freight_costs wfc
3154        WHERE wdd.lpn_id = p_outermost_lpn_id
3155        AND wdd.released_status = 'X'  -- For LPN reuse ER : 6845650
3156        AND wdd.delivery_detail_id = wfc.delivery_detail_id;
3157 
3158      IF l_debug = 1 THEN
3159     debug('About to call wsh_freight_costs_pub.delete_freight_costs','UNLOAD_TRUCK');
3160     debug('delivery_detail_id : '|| l_freight_costs.delivery_detail_id,'UNLOAD_TRUCK');
3161     debug('freight_cost_id    : '|| l_freight_costs.freight_cost_id, 'UNLOAD_TRUCK');
3162      END IF;
3163      wsh_freight_costs_pub.delete_freight_costs
3164        (p_api_version_number  => 1.0,
3165          p_init_msg_list       => l_init_msg_list,
3166          p_commit              => fnd_api.g_false,
3167          x_return_status       => l_return_status,
3168          x_msg_count           => l_msg_count,
3169          x_msg_data            => l_msg_data,
3170          p_pub_freight_costs   => l_freight_costs);
3171 
3172   EXCEPTION
3173      WHEN no_data_found THEN
3174     IF (l_debug = 1) THEN
3175       DEBUG('Could not find record in wsh_freight_cost', 'UNLOAD_TRUCK');
3176        END IF;
3177   END;
3178   --\Shipping API cleanup
3179 
3180   BEGIN
3181     SELECT wstt.delivery_id
3182          , wnd.status_code
3183       INTO l_del_id
3184          , l_status_code
3185       FROM wms_shipping_transaction_temp wstt, wsh_new_deliveries_ob_grp_v wnd
3186      WHERE wstt.outermost_lpn_id = p_outermost_lpn_id
3187        AND wstt.direct_ship_flag = 'Y'
3188        AND wstt.delivery_id = wnd.delivery_id
3189        AND ROWNUM = 1;
3190 
3191     IF (l_debug = 1) THEN
3192       DEBUG('The value of l_del_id and status are ' || l_del_id || '- ' || l_status_code, 'unload_truck');
3193     END IF;
3194 
3195     IF l_status_code = 'CO' THEN
3196       wsh_deliveries_pub.delivery_action(
3197         p_api_version_number         => 1.0
3198       , p_init_msg_list              => l_init_msg_list
3199       , x_return_status              => l_return_status
3200       , x_msg_count                  => l_msg_count
3201       , x_msg_data                   => l_msg_data
3202       , p_action_code                => 'RE-OPEN'
3203       , p_delivery_id                => l_del_id
3204       , p_sc_action_flag             => NULL
3205       , p_sc_intransit_flag          => NULL
3206       , p_sc_close_trip_flag         => NULL
3207       , p_sc_create_bol_flag         => NULL
3208       , p_sc_stage_del_flag          => NULL
3209       , p_sc_defer_interface_flag    => NULL
3210       , p_wv_override_flag           => NULL
3211       , x_trip_id                    => l_trip_id
3212       , x_trip_name                  => l_trip_name
3213       );
3214 
3215       IF (l_debug = 1) THEN
3216         DEBUG('The value of return status of delivery_actions is ' || l_return_status, 'unload_truck');
3217       END IF;
3218 
3219       IF (l_return_status IN(fnd_api.g_ret_sts_error)) THEN
3220         IF (l_debug = 1) THEN
3221           DEBUG('Reopening of delivery failed ' || l_del_id, 'UNLOAD_TRUCK');
3222         END IF;
3223 
3224         RAISE fnd_api.g_exc_error;
3225       ELSIF(l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
3226         IF (l_debug = 1) THEN
3227           DEBUG(' Reopening of delivery failed with Unexpected error' || l_del_id, 'UNLOAD_TRUCK');
3228         END IF;
3229 
3230         RAISE fnd_api.g_exc_unexpected_error;
3231       END IF;
3232     END IF;
3233   EXCEPTION
3234     WHEN NO_DATA_FOUND THEN
3235       NULL;
3236   END;
3237 
3238   --Get the delivery detail id corresponding to the outermost lpn_id
3239   SELECT wdd.delivery_detail_id
3240     INTO l_outermost_dd_id
3241     FROM wsh_delivery_details_ob_grp_v wdd
3242    WHERE lpn_id = p_outermost_lpn_id
3243    AND released_status = 'X';  -- For LPN reuse ER : 6845650
3244 
3245   counter          := 0;
3246 
3247   IF (l_debug = 1) THEN
3248     DEBUG('outermost delivery_detail_id: ' || l_outermost_dd_id, 'Unload Truck');
3249   END IF;
3250 
3251   --FOR l_delivery_detail_cur IN delivery_details LOOP
3252    --OPEN delivery_details(l_outermost_dd_id);
3253   FOR l_wda_del_detail IN wda_del_detail(l_outermost_dd_id) LOOP
3254     OPEN wdd_del_detail(l_wda_del_detail.delivery_detail_id);
3255 
3256     LOOP
3257       FETCH wdd_del_detail
3258       INTO l_delivery_detail_id,
3259            l_item_id,
3260            l_serial_number,
3261            l_source_line_id,
3262            l_req_qty,
3263            l_transaction_temp_id;
3264       EXIT WHEN wdd_del_detail%NOTFOUND;
3265 
3266       IF (l_debug = 1) THEN
3267         DEBUG('Entered the delivery details loop', 'Unload Truck');
3268       END IF;
3269 
3270       IF inv_cache.set_item_rec(p_org_id,l_item_id) THEN
3271          IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
3272             l_dual_uom_item := TRUE;
3273             IF (l_debug = 1) THEN
3274                DEBUG('Item is dual UOM', 'Unload Truck');
3275             END IF;
3276          ELSE
3277             l_dual_uom_item := FALSE;
3278             IF (l_debug = 1) THEN
3279                DEBUG('Item is NOT dual UOM', 'Unload Truck');
3280             END IF;
3281          END IF;
3282       ELSE
3283          IF (l_debug = 1) THEN
3284             DEBUG('Error from inv_cache.set_item_rec', 'Unload Truck');
3285          END IF;
3286          RAISE fnd_api.g_exc_unexpected_error;
3287       END IF;
3288 
3289       --Increment the loop counter
3290       counter                                            := counter + 1;
3291       l_delivery_detail_tab(counter).delivery_detail_id  := l_delivery_detail_id;
3292       l_delivery_detail_tab(counter).source_line_id      := l_source_line_id;
3293 
3294      /* Mrana: 7/6/06: 5350778 : Added lot_divisible_flag in the foll. query */
3295 
3296       SELECT serial_number_control_code,
3297              lot_control_code,
3298              lot_divisible_flag
3299         INTO l_serial_ctrl_code,
3300              l_lot_ctrl_code,
3301              l_lot_divisible_flag
3302         FROM mtl_system_items
3303        WHERE inventory_item_id = l_item_id
3304          AND organization_id = p_org_id;
3305 
3306       IF (l_debug = 1) THEN
3307         DEBUG('Serial Control Code of item: ' || l_item_id || ' : is: ' || l_serial_ctrl_code, 'Unload_Truck');
3308         DEBUG('l_lot_ctrl_code : ' || l_lot_ctrl_code , 'Unload_Truck');
3309         DEBUG('l_lot_divisible_flag :' || l_lot_divisible_flag , 'Unload_Truck');
3310         DEBUG('serial #:' || l_serial_number || ' source line id is: ' || l_source_line_id, 'Unload_Truck');
3311       END IF;
3312 
3313       IF l_serial_ctrl_code IN(2, 5) THEN
3314         BEGIN
3315           UPDATE mtl_serial_numbers
3316              SET current_status = 3
3317                , group_mark_id = NULL -- -1
3318            WHERE inventory_item_id = l_item_id
3319              AND current_organization_id = p_org_id
3320              AND(serial_number = l_serial_number
3321                  OR group_mark_id = l_transaction_temp_id); --bug#2829514
3322 
3323           IF (l_debug = 1) THEN
3324             DEBUG('Group mark id reset for serial with group mark id =' || l_transaction_temp_id, 'Unload_Truck');
3325             DEBUG('Reset the group mark Id for: ' || SQL%ROWCOUNT || ' serials', 'Unload_truck');
3326           END IF;
3327         EXCEPTION
3328           WHEN OTHERS THEN
3329             NULL;
3330         END;
3331       ELSIF l_serial_ctrl_code = 6 THEN
3332         BEGIN
3333           UPDATE mtl_serial_numbers
3334              SET current_status = 1
3335                , group_mark_id = NULL -- -1
3336            WHERE inventory_item_id = l_item_id
3337              AND current_organization_id = p_org_id
3338              AND(serial_number = l_serial_number
3339                  OR group_mark_id = l_transaction_temp_id); --bug#2829514
3340 
3341           IF (l_debug = 1) THEN
3342             DEBUG('Group mark id reset for serial with group mark id =' || l_transaction_temp_id, 'Unload_Truck');
3343             DEBUG('Reset the group mark Id for: ' || SQL%ROWCOUNT || ' serials', 'Unload_truck');
3344           END IF;
3345         EXCEPTION
3346           WHEN OTHERS THEN
3347             NULL;
3348         END;
3349       END IF;
3350 
3351       /* Mrana: 7/6/06: 5350778 :
3352        *  IF lot is not divisible, then we cannot relieve partial reservations,
3353        *  therefore we will always relieve reservations for suh items,whatever
3354        *  may be the form function parameter value in p_relieve_rsv */
3355 
3356       IF l_lot_ctrl_code = 2 THEN
3357          IF l_lot_divisible_flag = 'N' THEN
3358             l_relieve_rsv := 'Y' ;
3359          ELSE
3360             l_relieve_rsv := p_relieve_rsv;
3361          END IF ;
3362       ELSE
3363          l_relieve_rsv := p_relieve_rsv;
3364       END IF ;
3365 
3366       IF (l_debug = 1) THEN
3367         DEBUG('The delivery_detail_id is ' || l_delivery_detail_id, 'Unload_truck');
3368         DEBUG('l_relieve_rsv : ' || l_relieve_rsv, 'Unload_truck');
3369       END IF;
3370 
3371       --Return the delivery line to stock
3372       IF l_dual_uom_item THEN
3373         inv_shipping_transaction_pub.inv_line_return_to_stock(
3374           p_delivery_id                => NULL
3375         , p_delivery_line_id           => l_delivery_detail_id
3376         , p_shipped_quantity           => 0
3377         , p_sec_shipped_quantity       => 0
3378         , x_msg_data                   => x_msg_data
3379         , x_msg_count                  => x_msg_count
3380         , x_return_status              => l_return_status
3381         , p_commit_flag                => fnd_api.g_false
3382         ,p_relieve_rsv                 => l_relieve_rsv /*added 3396821*/
3383         );
3384       ELSE
3385         inv_shipping_transaction_pub.inv_line_return_to_stock(
3386           p_delivery_id                => NULL
3387         , p_delivery_line_id           => l_delivery_detail_id
3388         , p_shipped_quantity           => 0
3389         , x_msg_data                   => x_msg_data
3390         , x_msg_count                  => x_msg_count
3391         , x_return_status              => l_return_status
3392         , p_commit_flag                => fnd_api.g_false
3393         ,p_relieve_rsv                 => l_relieve_rsv /*added 3396821*/
3394         );
3395       END IF;
3396 
3397       IF l_return_status = fnd_api.g_ret_sts_error THEN
3398         fnd_message.set_name('WMS', 'WMS_RET_LINE_TO_STOCK');
3399         /* Failed to unload the delivery line */
3400         fnd_msg_pub.ADD;
3401 
3402         IF (l_debug = 1) THEN
3403           DEBUG('inv_line_return_to_stock API failed with status E ', 'Unload_Truck');
3404         END IF;
3405 
3406         RAISE fnd_api.g_exc_error;
3407       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3408         IF (l_debug = 1) THEN
3409           DEBUG('inv_line_return_to_stock API failed with status U', 'Unload_Truck');
3410         END IF;
3411 
3412         fnd_message.set_name('WMS', 'WMS_RET_LINE_TO_STOCK');
3413         /* Failed to unload the delivery line */
3414         fnd_msg_pub.ADD;
3415         RAISE fnd_api.g_exc_unexpected_error;
3416       END IF;
3417 
3418       IF (l_debug = 1) THEN
3419         DEBUG('inv_line_return_to_stock successful for iteration #:' || counter, 'Unload_truck');
3420       END IF;
3421 
3422 
3423       IF (l_debug = 1) THEN
3424         DEBUG('Update LPN Context to Reside in inventory', 'Unload_truck');
3425       END IF;
3426 
3427 
3428     --Store the source line Id for checking the next delivery detail
3429     END LOOP;
3430 
3431     CLOSE wdd_del_detail;
3432   END LOOP;
3433 
3434   /* Release 12 LPN Sync
3435      Call wms_container_pvt.modify_lpn API
3436        to update the context and remove the WDD records
3437        associated with those LPNs  */
3438   l_lpn_tbl.delete;
3439   FOR l_lpn IN lpn_cur LOOP
3440      l_lpn_rec.organization_id := l_lpn.organization_id;
3441      l_lpn_rec.lpn_id := l_lpn.lpn_id;
3442      l_lpn_rec.lpn_context := 1;
3443      l_lpn_tbl(nvl(l_lpn_tbl.last, 0)+1) := l_lpn_rec;
3444      IF (l_debug = 1) THEN
3445         debug('Add to l_lpn_tbl with lpn_rec of org_id'||l_lpn_rec.organization_id
3446                ||', lpn_id '||l_lpn_rec.lpn_id||', lpn_context '||l_lpn_rec.lpn_context, 'Unload_Truck');
3447      END IF;
3448   END LOOP;
3449   IF(l_debug = 1) THEN
3450      DEBUG('Calling WMS_CONTAINER_PVT.Modify_LPNs with caller WMS_DIRECTSHIP','Unload_Truck');
3451   END IF;
3452 
3453   WMS_CONTAINER_PVT.Modify_LPNs(
3454   	  p_api_version           => 1.0
3455   	, p_init_msg_list         => fnd_api.g_true
3456   	, p_commit                => fnd_api.g_false
3457   	, x_return_status         => l_return_status
3458   	, x_msg_count             => x_msg_count
3459   	, x_msg_data              => x_msg_data
3460   	, p_caller                => 'WMS_DIRECTSHIP'
3461   	, p_lpn_table             => l_lpn_tbl
3462   );
3463   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
3464        IF (l_debug = 1) THEN
3465   	 DEBUG('return error from WMS_CONTAINER_PVT.Modify_LPNs', 'Unload_Truck');
3466        END IF;
3467        RAISE fnd_api.g_exc_error;
3468   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3469        IF (l_debug = 1) THEN
3470   	 DEBUG('return error from WMS_CONTAINER_PVT.Modify_LPNs', 'Unload_Truck');
3471        END IF;
3472        RAISE fnd_api.g_exc_unexpected_error;
3473   ELSIF l_return_status = fnd_api.g_ret_sts_success THEN
3474        null;
3475   END IF;
3476   -- End of Release 12 change
3477 
3478   --Now, clean up the direct ship records from WSTT, WFCT, MSNT and WDST
3479   wms_direct_ship_pvt.cleanup_temp_recs(
3480     x_msg_data                   => x_msg_data
3481   , x_msg_count                  => x_msg_count
3482   , x_return_status              => l_return_status
3483   , p_org_id                     => p_org_id
3484   , p_outermost_lpn_id           => p_outermost_lpn_id
3485   , p_trip_id                    => NULL
3486   );
3487 
3488   IF l_return_status = fnd_api.g_ret_sts_error THEN
3489     fnd_message.set_name('WMS', 'WMS_CLEANUP_TEMP');
3490     /*Failed in cleanup program*/
3491     fnd_msg_pub.ADD;
3492 
3493     IF (l_debug = 1) THEN
3494       DEBUG('CLEANUP_TEMP_RECS API failed with status E ', 'Unload_truck');
3495     END IF;
3496 
3497     RAISE fnd_api.g_exc_error;
3498   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3499     fnd_message.set_name('WMS', 'WMS_CLEANUP_TEMP');
3500     /*Failed in cleanup program*/
3501     fnd_msg_pub.ADD;
3502 
3503     IF (l_debug = 1) THEN
3504       DEBUG('CLEANUP_TEMP_RECS failed with status U', 'Unload_truck');
3505     END IF;
3506 
3507     RAISE fnd_api.g_exc_unexpected_error;
3508   END IF;
3509 
3510   IF (l_debug = 1) THEN
3511      DEBUG('UNLOAD_TRUCK Completed successfully', 'Unload Truck');
3512   END IF;
3513 
3514   -- Commit if no errors
3515   COMMIT;
3516 EXCEPTION
3517   WHEN fnd_api.g_exc_error THEN
3518     x_return_status  := fnd_api.g_ret_sts_error;
3519     ROLLBACK;
3520     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3521 
3522     IF (l_debug = 1) THEN
3523       DEBUG('Execution Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3524     END IF;
3525   WHEN fnd_api.g_exc_unexpected_error THEN
3526     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3527     ROLLBACK;
3528     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3529 
3530     IF (l_debug = 1) THEN
3531       DEBUG('Unexpected Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3532     END IF;
3533   WHEN OTHERS THEN
3534     --x_error_code := 9999;
3535     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3536     ROLLBACK;
3537 
3538     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3539       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'UNLOAD_TRUCK');
3540     END IF;
3541 
3542     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3543 
3544     IF (l_debug = 1) THEN
3545       DEBUG('Unexpected Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3546     END IF;
3547 END unload_truck;
3548 
3549 PROCEDURE cleanup_temp_recs(
3550   x_return_status    OUT NOCOPY    VARCHAR2
3551 , x_msg_count        OUT NOCOPY    NUMBER
3552 , x_msg_data         OUT NOCOPY    VARCHAR2
3553 , p_org_id           IN            NUMBER
3554 , p_outermost_lpn_id IN            NUMBER
3555 , p_trip_id          IN            NUMBER
3556 , p_dock_door_id     IN            NUMBER DEFAULT NULL
3557 ) IS
3558   CURSOR outermost_lpn_cur IS
3559     SELECT DISTINCT outermost_lpn_id
3560                FROM wms_shipping_transaction_temp
3561               WHERE organization_id = p_org_id
3562                 AND trip_id = p_trip_id;
3563 
3564   CURSOR delivery_cur IS
3565     SELECT DISTINCT delivery_id
3566                FROM wms_shipping_transaction_temp
3567               WHERE organization_id = p_org_id
3568                 AND trip_id = p_trip_id;
3569 
3570   l_outermost_lpn_id NUMBER;
3571   l_debug            NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3572 BEGIN
3573   x_return_status  := fnd_api.g_ret_sts_success;
3574 
3575   IF p_trip_id IS NOT NULL THEN
3576     BEGIN
3577       --Delete the records for the LPN
3578       FOR l_lpn IN outermost_lpn_cur LOOP
3579         l_outermost_lpn_id  := l_lpn.outermost_lpn_id;
3580 
3581         DELETE FROM wms_freight_cost_temp
3582               WHERE organization_id = p_org_id
3583                 AND lpn_id = l_outermost_lpn_id;
3584 
3585         DELETE FROM mtl_serial_numbers_temp
3586               WHERE transaction_temp_id IN(SELECT transaction_temp_id
3587                                              FROM wms_direct_ship_temp
3588                                             WHERE lpn_id = p_outermost_lpn_id);
3589 
3590         DELETE FROM wms_direct_ship_temp
3591               WHERE organization_id = p_org_id
3592                 --AND    lpn_id = p_outermost_lpn_id;
3593                 /* part of bug fix 2538703 */
3594                 AND lpn_id = l_outermost_lpn_id;
3595       /* end of bug fix 2538703 */
3596       END LOOP;
3597 
3598       --Delete the records for the delivery
3599       FOR l_delivery IN delivery_cur LOOP
3600         DELETE FROM wms_freight_cost_temp
3601               WHERE organization_id = p_org_id
3602                 AND delivery_id = l_delivery.delivery_id;
3603       END LOOP;
3604 
3605       --Delete the trip records
3606       DELETE FROM wms_freight_cost_temp
3607             WHERE organization_id = p_org_id
3608               AND trip_id = p_trip_id;
3609 
3610       DELETE FROM wms_shipping_transaction_temp
3611             WHERE organization_id = p_org_id
3612               AND trip_id = p_trip_id;
3613 
3614       IF (l_debug = 1) THEN
3615         DEBUG('Deleted temp recs for the case when trip id is not passed', 'CLEANUP_TEMP_RECS');
3616       END IF;
3617     EXCEPTION
3618       WHEN OTHERS THEN
3619         NULL;
3620     END;
3621   ELSE
3622     --Delete the Shipping transaction and freight entries
3623     DELETE FROM wms_shipping_transaction_temp
3624           WHERE organization_id = p_org_id
3625             AND outermost_lpn_id = p_outermost_lpn_id;
3626 
3627     DELETE FROM wms_freight_cost_temp
3628           WHERE lpn_id = p_outermost_lpn_id;
3629 
3630     --Delete records from Mtl_Seral_Numbers_Temp if any
3631     BEGIN
3632       DELETE FROM mtl_serial_numbers_temp
3633             WHERE transaction_temp_id IN(SELECT transaction_temp_id
3634                                            FROM wms_direct_ship_temp
3635                                           WHERE lpn_id = p_outermost_lpn_id);
3636     EXCEPTION
3637       WHEN NO_DATA_FOUND THEN
3638         NULL;
3639     END;
3640 
3641     DELETE FROM wms_direct_ship_temp
3642           WHERE lpn_id = p_outermost_lpn_id;
3643 
3644     IF (l_debug = 1) THEN
3645       DEBUG('Deleted temp recs for the case when trip id is not passed', 'CLEANUP_TEMP_RECS');
3646     END IF;
3647   END IF;
3648 EXCEPTION
3649   WHEN OTHERS THEN
3650     --x_error_code := 9999;
3651     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3652     ROLLBACK;
3653 
3654     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3655       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'CLEANUP_TEMP');
3656     END IF;
3657 
3658     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3659 
3660     IF (l_debug = 1) THEN
3661       DEBUG('Unexpected Error in cleanup_temp:' || SUBSTR(SQLERRM, 1, 240), 'CLEANUP_TEMP_RECS');
3662     END IF;
3663 END cleanup_temp_recs;
3664 
3665 PROCEDURE get_global_values(
3666   x_userid        OUT NOCOPY NUMBER
3667 , x_logonid       OUT NOCOPY NUMBER
3668 , x_last_upd_date OUT NOCOPY DATE
3669 , x_current_date  OUT NOCOPY DATE
3670 ) IS
3671   x_progress VARCHAR2(3) := NULL;
3672   l_debug    NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3673 BEGIN
3674   x_progress       := 10;
3675   x_userid         := fnd_global.user_id;
3676   x_progress       := 20;
3677   x_logonid        := fnd_global.login_id;
3678   x_progress       := 30;
3679   x_last_upd_date  := SYSDATE;
3680   x_progress       := 40;
3681   x_current_date   := SYSDATE;
3682 EXCEPTION
3683   WHEN OTHERS THEN
3684     IF (l_debug = 1) THEN
3685       DEBUG('Error in WMS_DIRECT_SHIP_PVT.get_global_values: ' || x_progress);
3686     END IF;
3687 END get_global_values;
3688 
3689 PROCEDURE create_resv(
3690   x_return_status OUT NOCOPY    VARCHAR2
3691 , x_msg_count     OUT NOCOPY    NUMBER
3692 , x_msg_data      OUT NOCOPY    VARCHAR2
3693 , p_group_id      IN            NUMBER
3694 , p_org_id        IN            NUMBER
3695 ) IS
3696 BEGIN
3697   --bug #2782201
3698   NULL;
3699 /*
3700   This method is no more used from I patchset. The corresponding functionality can be found in
3701   process_lpn and process_line.
3702  */
3703 END create_resv;
3704 
3705 PROCEDURE validate_status_lpn_contents(
3706   x_return_status OUT NOCOPY    VARCHAR2
3707 , x_msg_count     OUT NOCOPY    NUMBER
3708 , x_msg_data      OUT NOCOPY    VARCHAR2
3709 , p_lpn_id        IN            NUMBER
3710 , p_org_id        IN            NUMBER
3711 , p_trx_type_id   IN            NUMBER
3712 ) IS
3713   CURSOR lpn_contents IS
3714     SELECT wlpn.lpn_id
3715          , wlpn.subinventory_code
3716          , wlpn.locator_id
3717          , wlc.lot_number
3718          , wlc.inventory_item_id
3719          , msn.serial_number
3720       FROM wms_lpn_contents wlc, wms_license_plate_numbers wlpn, mtl_serial_numbers msn
3721      WHERE wlpn.lpn_id = wlc.parent_lpn_id
3722        AND msn.lpn_id(+) = wlc.parent_lpn_id
3723        AND msn.inventory_item_id(+) = wlc.inventory_item_id
3724        AND msn.current_organization_id(+) = wlc.organization_id
3725        /* Bug# 3119461 Without the following joins for lot and rev
3726        ** we would get a cartesian product */
3727        AND NVL(msn.lot_number(+),'#NULL#') = NVL(wlc.lot_number,'#NULL#')
3728        AND NVL(msn.revision(+),'#NULL#') = NVL(wlc.revision,'#NULL#')
3729        AND wlpn.outermost_lpn_id = p_lpn_id
3730        AND wlpn.organization_id = p_org_id
3731        AND wlc.organization_id = p_org_id;
3732 
3733   l_lpn_contents        lpn_contents%ROWTYPE;
3734   l_return_status       VARCHAR2(1);
3735   l_msg_count           NUMBER;
3736   l_msg_data            VARCHAR2(20000);
3737   l_trx_status_enabled  NUMBER;
3738   l_lpn_status          VARCHAR2(1)            := 'Y';
3739   l_debug               NUMBER                 := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3740 BEGIN
3741   x_return_status  := fnd_api.g_ret_sts_success;
3742 
3743   SELECT NVL(status_control_flag,2)
3744     INTO l_trx_status_enabled
3745     FROM mtl_transaction_types
3746    WHERE transaction_type_id = p_trx_type_id;
3747 
3748   IF l_trx_status_enabled = 2 THEN
3749      -- Transaction type are not status controlled
3750      RETURN;
3751   END IF;
3752 
3753   OPEN lpn_contents;
3754 
3755   LOOP
3756     FETCH lpn_contents INTO l_lpn_contents;
3757     EXIT WHEN lpn_contents%NOTFOUND;
3758 
3759     l_lpn_status :=
3760       inv_material_status_grp.is_status_applicable(
3761         p_wms_installed              => 'TRUE'
3762       , p_trx_status_enabled         => l_trx_status_enabled
3763       , p_trx_type_id                => p_trx_type_id
3764       , p_organization_id            => p_org_id
3765       , p_inventory_item_id          => l_lpn_contents.inventory_item_id
3766       , p_sub_code                   => l_lpn_contents.subinventory_code
3767       , p_locator_id                 => l_lpn_contents.locator_id
3768       , p_lot_number                 => l_lpn_contents.lot_number
3769       , p_serial_number              => l_lpn_contents.serial_number
3770       , p_object_type                => 'A'
3771       );
3772 
3773     IF l_lpn_status <> 'Y' THEN
3774        x_return_status  := fnd_api.g_ret_sts_error;
3775        EXIT;
3776     END IF;
3777 
3778   END LOOP;
3779 
3780   CLOSE lpn_contents;
3781 EXCEPTION
3782   WHEN OTHERS THEN
3783     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3784 
3785     IF lpn_contents%ISOPEN THEN
3786        CLOSE lpn_contents;
3787     END IF;
3788 
3789     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3790       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'validate_status_lpn_contents');
3791     END IF;
3792 
3793     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3794 END validate_status_lpn_contents;
3795 
3796 PROCEDURE update_freight_cost(
3797   x_return_status OUT NOCOPY    VARCHAR2
3798 , x_msg_count     OUT NOCOPY    NUMBER
3799 , x_msg_data      OUT NOCOPY    VARCHAR2
3800 , p_lpn_id        IN            NUMBER
3801 ) IS
3802   CURSOR lpn_freight IS
3803     SELECT        ROWID
3804                 , freight_cost_type_id
3805                 , currency_code
3806                 , freight_amount
3807                 , conversion_type
3808              FROM wms_freight_cost_temp
3809             WHERE lpn_id = p_lpn_id
3810               AND freight_cost_id IS NULL
3811     FOR UPDATE OF freight_cost_id;
3812 
3813   CURSOR delivery_detail IS
3814     SELECT wdd.delivery_detail_id
3815       FROM wsh_delivery_details_ob_grp_v wdd
3816      WHERE lpn_id = p_lpn_id
3817      AND released_status = 'X';  -- For LPN reuse ER : 6845650
3818 
3819   l_freight_cost_rec wsh_freight_costs_pub.pubfreightcostrectype;
3820   l_delivery_detail  delivery_detail%ROWTYPE;
3821   l_lpn_freight      lpn_freight%ROWTYPE;
3822   l_init_msg_list    VARCHAR2(1)                                 := fnd_api.g_true;
3823   l_return_status    VARCHAR2(1);
3824   l_msg_count        NUMBER;
3825   l_msg_data         VARCHAR2(20000);
3826   l_freight_cost_id  NUMBER;
3827   l_debug            NUMBER                                      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3828 BEGIN
3829   x_return_status  := fnd_api.g_ret_sts_success;
3830   SAVEPOINT freight_cost;
3831   OPEN delivery_detail;
3832 
3833   LOOP
3834     FETCH delivery_detail INTO l_delivery_detail;
3835     EXIT WHEN delivery_detail%NOTFOUND;
3836   END LOOP;
3837 
3838   CLOSE delivery_detail;
3839   OPEN lpn_freight;
3840 
3841   LOOP
3842     FETCH lpn_freight INTO l_lpn_freight;
3843     EXIT WHEN lpn_freight%NOTFOUND;
3844     l_freight_cost_rec.freight_cost_type_id  := l_lpn_freight.freight_cost_type_id;
3845     l_freight_cost_rec.currency_code         := l_lpn_freight.currency_code;
3846     l_freight_cost_rec.unit_amount           := l_lpn_freight.freight_amount;
3847     l_freight_cost_rec.conversion_type_code  := l_lpn_freight.conversion_type;
3848     l_freight_cost_rec.delivery_detail_id    := l_delivery_detail.delivery_detail_id;
3849     l_freight_cost_rec.action_code           := 'CREATE';
3850     wsh_freight_costs_pub.create_update_freight_costs(
3851       p_api_version_number         => 1.0
3852     , p_init_msg_list              => l_init_msg_list
3853     , p_commit                     => fnd_api.g_false
3854     , x_return_status              => l_return_status
3855     , x_msg_count                  => l_msg_count
3856     , x_msg_data                   => l_msg_data
3857     , p_pub_freight_costs          => l_freight_cost_rec
3858     , p_action_code                => 'CREATE'
3859     , x_freight_cost_id            => l_freight_cost_id
3860     );
3861 
3862     IF l_return_status = fnd_api.g_ret_sts_error THEN
3863       IF (l_debug = 1) THEN
3864         DEBUG('Create_Update_Freight_Costs API completed status E ', 'update_freight_cost');
3865       END IF;
3866 
3867       RAISE fnd_api.g_exc_error;
3868     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3869       IF (l_debug = 1) THEN
3870         DEBUG('Create_Update_Freight_Costs API completed status U ', 'update_freight_cost');
3871       END IF;
3872 
3873       RAISE fnd_api.g_exc_unexpected_error;
3874     END IF;
3875 
3876     UPDATE wms_freight_cost_temp
3877        SET freight_cost_id = l_freight_cost_id
3878          , last_update_date = SYSDATE
3879          , last_updated_by = fnd_global.user_id
3880      WHERE ROWID = l_lpn_freight.ROWID;
3881   END LOOP;
3882 
3883   CLOSE lpn_freight;
3884 
3885   IF (l_debug = 1) THEN
3886     DEBUG('Update_Freight cost Update was successful', 'update_freight_cost');
3887   END IF;
3888 EXCEPTION
3889   WHEN fnd_api.g_exc_error THEN
3890     IF (l_debug = 1) THEN
3891       DEBUG('Update_Freight cost API failed with status (E)', 'update_freight_cost');
3892     END IF;
3893 
3894     IF lpn_freight%ISOPEN THEN
3895       CLOSE lpn_freight;
3896     END IF;
3897 
3898     IF delivery_detail%ISOPEN THEN
3899       CLOSE delivery_detail;
3900     END IF;
3901 
3902     x_return_status  := fnd_api.g_ret_sts_error;
3903     ROLLBACK TO freight_cost;
3904     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3905   WHEN fnd_api.g_exc_unexpected_error THEN
3906     IF lpn_freight%ISOPEN THEN
3907       CLOSE lpn_freight;
3908     END IF;
3909 
3910     IF delivery_detail%ISOPEN THEN
3911       CLOSE delivery_detail;
3912     END IF;
3913 
3914     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3915 
3916     IF (l_debug = 1) THEN
3917       DEBUG('Update_Freight cost API failed with status (U)', 'update_freight_cost');
3918     END IF;
3919 
3920     ROLLBACK TO freight_cost;
3921     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3922   WHEN OTHERS THEN
3923     IF lpn_freight%ISOPEN THEN
3924       CLOSE lpn_freight;
3925     END IF;
3926 
3927     IF delivery_detail%ISOPEN THEN
3928       CLOSE delivery_detail;
3929     END IF;
3930 
3931     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3932 
3933     IF (l_debug = 1) THEN
3934       DEBUG('Update_Freight cost API failed with status (U)', 'update_freight_cost');
3935     END IF;
3936 
3937     ROLLBACK TO freight_cost;
3938 
3939     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3940       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'update_freight_cost');
3941     END IF;
3942 
3943     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3944 END update_freight_cost;
3945 
3946 /******************************
3947 * Procedure Associates serial numbers with appropriate
3948 * Delivery lines.
3949 * Bug No 3390432 : Fix has changed the functionality for
3950 * Items with Serial numbers @ SO.
3951 * Serials Numbers of Items with Serial control code 6 is picked from
3952 * MSNT and inserted into MSN with appropriate values copied from
3953 * MSNT. When delivery lines are split transaction temp ids are changed
3954 * and appropriately updated into MSNT and into MSN as group mark id.
3955 ******************************/
3956 PROCEDURE explode_delivery_details(
3957   x_return_status              OUT NOCOPY    VARCHAR2
3958 , x_msg_count                  OUT NOCOPY    NUMBER
3959 , x_msg_data                   OUT NOCOPY    VARCHAR2
3960 --Bug No 3390432
3961 , x_transaction_temp_id        OUT NOCOPY    NUMBER
3962 , p_organization_id            IN            NUMBER
3963 , p_lpn_id                     IN            NUMBER
3964 , p_serial_number_control_code IN            NUMBER
3965 , p_delivery_detail_id         IN            NUMBER
3966 , p_quantity                   IN            NUMBER
3967 , p_transaction_temp_id        IN            NUMBER DEFAULT NULL
3968 , p_reservation_id             IN            NUMBER DEFAULT NULL
3969 , p_last_action                IN            VARCHAR2 DEFAULT 'U'
3970 ) IS
3971   l_running_quantity       NUMBER;
3972   l_split_status           VARCHAR2(30);
3973 
3974   TYPE serial_tab_type IS TABLE OF VARCHAR(30)
3975     INDEX BY BINARY_INTEGER;
3976 
3977   l_serial_numbers_table   serial_tab_type;
3978 
3979   CURSOR c_explode_detail IS
3980     SELECT msnt.ROWID
3981          , dd.requested_quantity
3982          , msnt.transaction_temp_id
3983          , dd.delivery_detail_id
3984          , msnt.fm_serial_number
3985          , msnt.to_serial_number
3986          , msnt.serial_prefix
3987          , dd.organization_id
3988          , dd.inventory_item_id
3989          , msnt.serial_attribute_category
3990          , msnt.origination_date
3991          , msnt.c_attribute1
3992          , msnt.c_attribute2
3993          , msnt.c_attribute3
3994          , msnt.c_attribute4
3995          , msnt.c_attribute5
3996          , msnt.c_attribute6
3997          , msnt.c_attribute7
3998          , msnt.c_attribute8
3999          , msnt.c_attribute9
4000          , msnt.c_attribute10
4001          , msnt.c_attribute11
4002          , msnt.c_attribute12
4003          , msnt.c_attribute13
4004          , msnt.c_attribute14
4005          , msnt.c_attribute15
4006          , msnt.c_attribute16
4007          , msnt.c_attribute17
4008          , msnt.c_attribute18
4009          , msnt.c_attribute19
4010          , msnt.c_attribute20
4011          , msnt.d_attribute1
4012          , msnt.d_attribute2
4013          , msnt.d_attribute3
4014          , msnt.d_attribute4
4015          , msnt.d_attribute5
4016          , msnt.d_attribute6
4017          , msnt.d_attribute7
4018          , msnt.d_attribute8
4019          , msnt.d_attribute9
4020          , msnt.d_attribute10
4021          , msnt.n_attribute1
4022          , msnt.n_attribute2
4023          , msnt.n_attribute3
4024          , msnt.n_attribute4
4025          , msnt.n_attribute5
4026          , msnt.n_attribute6
4027          , msnt.n_attribute7
4028          , msnt.n_attribute8
4029          , msnt.n_attribute9
4030          , msnt.n_attribute10
4031          , msnt.status_id
4032          , msnt.territory_code
4033          , msnt.time_since_new
4034          , msnt.cycles_since_new
4035          , msnt.time_since_overhaul
4036          , msnt.cycles_since_overhaul
4037          , msnt.time_since_repair
4038          , msnt.cycles_since_repair
4039          , msnt.time_since_visit
4040          , msnt.cycles_since_visit
4041          , msnt.time_since_mark
4042          , msnt.cycles_since_mark
4043          , msnt.number_of_repairs
4044       FROM wsh_delivery_details_ob_grp_v dd
4045          , mtl_serial_numbers_temp msnt
4046      WHERE delivery_detail_id = p_delivery_detail_id
4047        AND msnt.transaction_temp_id = p_transaction_temp_id;
4048 
4049   l_explode_detail         c_explode_detail%ROWTYPE;
4050   l_rowid                  ROWID;
4051 
4052   CURSOR c_serials(
4053     p_organization_id   NUMBER
4054   , p_inventory_item_id NUMBER
4055   , p_fm_serial_number  VARCHAR2
4056   , p_to_serial_number  VARCHAR2
4057   , p_serial_length     NUMBER
4058   ) IS
4059     SELECT serial_number
4060       FROM mtl_serial_numbers
4061      WHERE current_organization_id = p_organization_id
4062        AND inventory_item_id = p_inventory_item_id
4063        AND serial_number BETWEEN p_fm_serial_number AND p_to_serial_number
4064        AND current_status IN(1, 6)
4065        AND LENGTH(serial_number) = p_serial_length;
4066 
4067   CURSOR c_lpn_serial(p_organization_id NUMBER
4068                     , p_inventory_item_id NUMBER
4069                     , p_lpn_id NUMBER) IS
4070     SELECT serial_number,
4071            group_mark_id,
4072            reservation_id
4073       FROM mtl_serial_numbers a
4074      WHERE current_organization_id = p_organization_id
4075        AND inventory_item_id = p_inventory_item_id
4076        AND lpn_id = p_lpn_id
4077        AND current_status = 3
4078        AND (   NVL(group_mark_id, 0) < 1
4079            OR (NVL(group_mark_id, 0) = p_reservation_id)
4080            OR (reservation_id = p_reservation_id))
4081        ORDER BY  a.reservation_id
4082                , a.serial_number;
4083            -- Mrana: 08/30/06: bug:5446598
4084            -- Added or condition for group_mark_id, to honor serial Reservations.
4085            -- Added order by, so that the ones with group_mark_id are picked
4086            -- 1st. This is important where LPN qty is > serial_quantity and only
4087            -- a few serials are reserved.
4088            -- Note: After backorder with Retain reservations, the group_mark_id on MSN might not
4089            -- be equal to reservation_id, that is why the check for reservation_id
4090 
4091   -- added for patchset i end_item_unit_validation
4092   CURSOR c_lpn_serial_e(p_organization_id NUMBER
4093                       , p_inventory_item_id NUMBER
4094                       , p_lpn_id NUMBER
4095                       , p_eiun VARCHAR2) IS
4096     SELECT serial_number,
4097            group_mark_id,
4098            reservation_id
4099       FROM mtl_serial_numbers a
4100      WHERE current_organization_id = p_organization_id
4101        AND inventory_item_id = p_inventory_item_id
4102        AND lpn_id = p_lpn_id
4103        AND current_status = 3
4104        AND NVL(end_item_unit_number, '@@') = NVL(p_eiun, '@@')
4105        AND (   NVL(group_mark_id, 0) < 1
4106            OR (NVL(group_mark_id, 0) = p_reservation_id)
4107            OR (reservation_id = p_reservation_id))
4108        ORDER BY  a.reservation_id
4109                , a.serial_number;
4110            -- Mrana: 08/30/06: bug:5446598
4111            -- Added or condition for group_mark_id, to honor serial Reservations.
4112            -- Added order by, so that the ones with group_mark_id are picked
4113            -- 1st. This is important where LPN qty is > serial_quantity and only
4114            -- a few serials are reserved.
4115 
4116   l_new_delivery_detail_id NUMBER;
4117   l_range_serial           BOOLEAN                    := FALSE;
4118   l_split_const            NUMBER;
4119   l_real_serial_prefix     VARCHAR2(30);
4120   l_serial_numeric         NUMBER;
4121   l_serial_numeric_end     NUMBER;
4122   l_serial_quantity        NUMBER                     := 0;
4123   l_new_fm_serial          VARCHAR2(30);
4124   l_current_to_serial      VARCHAR2(30);
4125   l_serial_numeric_len     NUMBER;
4126   l_serial_number_current  VARCHAR2(30);
4127   l_loop_end               NUMBER;
4128   l_serial_number          VARCHAR2(30);
4129   j                        NUMBER;
4130   m                        NUMBER;
4131   n                        NUMBER;
4132   l_transaction_temp_id    NUMBER;
4133   l_inventory_item_id      NUMBER;
4134   l_return_status          VARCHAR2(1);
4135   l_group_mark_id          NUMBER;
4136   l_succes                 NUMBER;
4137   l_tot_line_qty           NUMBER;
4138   l_max_limit              BOOLEAN;
4139   l_end_item_unit_number   VARCHAR2(30);
4140   l_debug                  NUMBER                     := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4141   l_proc_msg               VARCHAR2(300);
4142   -- Bug No 3390432
4143   l_serial_number_str      VARCHAR2(3000);
4144   l_cur_group_mark_id      NUMBER;
4145   l_cur_reservation_id     NUMBER;
4146 BEGIN
4147   --x_return_status := fnd_api.g_ret_sts_success;
4148 
4149  IF (l_debug = 1) THEN
4150     DEBUG('p_organization_id             : ' || p_organization_id, 'EXPLODE_DELIVERY_DETAILS');
4151     DEBUG(' p_lpn_id                     : ' || p_lpn_id      , 'EXPLODE_DELIVERY_DETAILS');
4152     DEBUG(' p_serial_number_control_code : ' || p_serial_number_control_code , 'EXPLODE_DELIVERY_DETAILS');
4153     DEBUG(' p_delivery_detail_id         : ' || p_delivery_detail_id         , 'EXPLODE_DELIVERY_DETAILS');
4154     DEBUG(' p_quantity                   : ' || p_quantity                   , 'EXPLODE_DELIVERY_DETAILS');
4155     DEBUG(' p_transaction_temp_id        : ' || p_transaction_temp_id        , 'EXPLODE_DELIVERY_DETAILS');
4156     DEBUG(' p_reservation_id             : ' || p_reservation_id             , 'EXPLODE_DELIVERY_DETAILS');
4157     DEBUG(' p_last_action                : ' || p_last_action   , 'EXPLODE_DELIVERY_DETAILS');
4158  END IF;
4159 
4160   SELECT SUM(wdd2.requested_quantity)
4161     INTO l_tot_line_qty
4162     FROM wsh_delivery_details_ob_grp_v wdd1
4163        , wsh_delivery_details_ob_grp_v wdd2
4164    WHERE wdd1.delivery_detail_id = p_delivery_detail_id
4165      AND wdd2.source_header_id = wdd1.source_header_id
4166      AND wdd2.source_line_id = wdd1.source_line_id
4167      AND wdd2.source_code = wdd1.source_code
4168      AND wdd2.released_status IN('R', 'B', 'X')
4169      AND wdd2.container_flag = 'N';
4170 
4171   IF l_tot_line_qty > p_quantity THEN
4172     l_max_limit  := TRUE;
4173   ELSE
4174     l_max_limit  := FALSE;
4175   END IF;
4176 
4177   -- ideally group_mark_id should be equal to transaction_temp_id
4178   l_group_mark_id  := p_transaction_temp_id;
4179 
4180   IF (l_debug = 1) THEN
4181     DEBUG('l_group_mark_id=' || l_group_mark_id, 'EXPLODE_DELIVERY_DETAILS');
4182   END IF;
4183 
4184   IF p_serial_number_control_code = 6 THEN
4185     /***********************
4186     * Bug No 3390432
4187     * Changes are made only for Items with
4188     * Serial control code 6 (Serial @ SO Items)
4189     **********************/
4190     l_serial_quantity  := p_quantity;
4191 
4192     IF (l_debug = 1) THEN
4193       DEBUG('P_SERIAL_CONTROL_CODE=6', 'EXPLODE_DELIVERY_DETAILS');
4194     END IF;
4195 
4196     -- Serial Number is lying in MTL_SERIAL_NUMBERS_TEMP
4197     IF (l_debug = 1) THEN
4198       DEBUG('EXPLODE delviery detail id ' || TO_CHAR(p_delivery_detail_id), 'EXPLODE_DELIVERY_DETAILS');
4199     END IF;
4200 
4201     /***********************
4202     * Bug No 3390432
4203     * Create a new Transaction Temp Id.
4204     * This Transaction temp id will be associated
4205     * with the delivery line id at the end of processing
4206     * We will use this Transaction temp Id as the group Mark Id aslo.
4207     ********************/
4208     SELECT mtl_material_transactions_s.NEXTVAL
4209       INTO x_transaction_temp_id
4210       FROM DUAL;
4211 
4212     -- Update group_mark_id to reflect newly created transaction_temp_id
4213     l_group_mark_id  := x_transaction_temp_id;
4214 
4215     x_return_status    := wsh_util_core.g_ret_sts_success;
4216     l_split_const      := 1;
4217 
4218     OPEN c_explode_detail;
4219     m                  := 1;
4220 
4221     LOOP
4222       FETCH c_explode_detail INTO l_explode_detail;
4223       EXIT WHEN c_explode_detail%NOTFOUND;
4224 
4225       IF (l_debug = 1) THEN
4226         DEBUG('Fetch the first serial number' || l_explode_detail.fm_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4227       END IF;
4228 
4229       IF (l_explode_detail.fm_serial_number = l_explode_detail.to_serial_number) THEN
4230         IF (l_debug = 1) THEN
4231           DEBUG('From and To Serial Numbers are Same','EXPLODE_DELIVEY_DETAILS');
4232         END IF;
4233         UPDATE mtl_serial_numbers
4234            SET serial_attribute_category = l_explode_detail.serial_attribute_category
4235              , origination_date = l_explode_detail.origination_date
4236              , c_attribute1 = l_explode_detail.c_attribute1
4237              , c_attribute2 = l_explode_detail.c_attribute2
4238              , c_attribute3 = l_explode_detail.c_attribute3
4239              , c_attribute4 = l_explode_detail.c_attribute4
4240              , c_attribute5 = l_explode_detail.c_attribute5
4241              , c_attribute6 = l_explode_detail.c_attribute6
4242              , c_attribute7 = l_explode_detail.c_attribute7
4243              , c_attribute8 = l_explode_detail.c_attribute8
4244              , c_attribute9 = l_explode_detail.c_attribute9
4245              , c_attribute10 = l_explode_detail.c_attribute10
4246              , c_attribute11 = l_explode_detail.c_attribute11
4247              , c_attribute12 = l_explode_detail.c_attribute12
4248              , c_attribute13 = l_explode_detail.c_attribute13
4249              , c_attribute14 = l_explode_detail.c_attribute14
4250              , c_attribute15 = l_explode_detail.c_attribute15
4251              , c_attribute16 = l_explode_detail.c_attribute16
4252              , c_attribute17 = l_explode_detail.c_attribute17
4253              , c_attribute18 = l_explode_detail.c_attribute18
4254              , c_attribute19 = l_explode_detail.c_attribute19
4255              , c_attribute20 = l_explode_detail.c_attribute20
4256              , d_attribute1 = l_explode_detail.d_attribute1
4257              , d_attribute2 = l_explode_detail.d_attribute2
4258              , d_attribute3 = l_explode_detail.d_attribute3
4259              , d_attribute4 = l_explode_detail.d_attribute4
4260              , d_attribute5 = l_explode_detail.d_attribute5
4261              , d_attribute6 = l_explode_detail.d_attribute6
4262              , d_attribute7 = l_explode_detail.d_attribute7
4263              , d_attribute8 = l_explode_detail.d_attribute8
4264              , d_attribute9 = l_explode_detail.d_attribute9
4265              , d_attribute10 = l_explode_detail.d_attribute10
4266              , n_attribute1 = l_explode_detail.n_attribute1
4267              , n_attribute2 = l_explode_detail.n_attribute2
4268              , n_attribute3 = l_explode_detail.n_attribute3
4269              , n_attribute4 = l_explode_detail.n_attribute4
4270              , n_attribute5 = l_explode_detail.n_attribute5
4271              , n_attribute6 = l_explode_detail.n_attribute6
4272              , n_attribute7 = l_explode_detail.n_attribute7
4273              , n_attribute8 = l_explode_detail.n_attribute8
4274              , n_attribute9 = l_explode_detail.n_attribute9
4275              , n_attribute10 = l_explode_detail.n_attribute10
4276              , status_id = l_explode_detail.status_id
4277              , territory_code = l_explode_detail.territory_code
4278              , time_since_new = l_explode_detail.time_since_new
4279              , cycles_since_new = l_explode_detail.cycles_since_new
4280              , time_since_overhaul = l_explode_detail.time_since_overhaul
4281              , cycles_since_overhaul = l_explode_detail.cycles_since_overhaul
4282              , time_since_repair = l_explode_detail.time_since_repair
4283              , cycles_since_repair = l_explode_detail.cycles_since_repair
4284              , time_since_visit = l_explode_detail.time_since_visit
4285              , cycles_since_visit = l_explode_detail.cycles_since_visit
4286              , time_since_mark = l_explode_detail.time_since_mark
4287              , cycles_since_mark = l_explode_detail.cycles_since_mark
4288              , number_of_repairs = l_explode_detail.number_of_repairs
4289              , group_mark_id = l_group_mark_id
4290          WHERE current_organization_id = l_explode_detail.organization_id
4291            AND inventory_item_id = l_explode_detail.inventory_item_id
4292            AND serial_number = l_explode_detail.fm_serial_number;
4293 
4294         -- Copy all Processed serial number into the
4295         -- serial Table.
4296         l_serial_numbers_table(m)  := l_explode_detail.fm_serial_number;
4297 
4298         IF (l_debug = 1) THEN
4299           DEBUG(
4300             'After update mtl_serial_numbers with the attributes: c_attribute1 ' || l_explode_detail.c_attribute1
4301           , 'EXPLODE_DELIVERY_DETAILS'
4302           );
4303         END IF;
4304 
4305         m                          := m + 1;
4306         IF (l_debug = 1) THEN
4307           DEBUG('(1)After Updating Serial Number :'||l_serial_number||' m :'||m,'EXPLODE_DELIVEY_DETAILS');
4308         END IF;
4309       -- update req.
4310       ELSE
4311         l_range_serial        := TRUE;
4312         -- Get the next serial number
4313         --m               := m + 1;
4314         -- Determine the serial number prefix
4315         l_real_serial_prefix  := RTRIM(l_explode_detail.fm_serial_number, '0123456789');
4316         -- Determine the base numeric portion
4317         l_serial_numeric      := TO_NUMBER(SUBSTR(l_explode_detail.fm_serial_number,
4318                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4319         l_serial_numeric_end  := TO_NUMBER(SUBSTR(l_explode_detail.to_serial_number,
4320                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4321 
4322         IF (l_serial_numeric_end - l_serial_numeric) >= p_quantity THEN
4323           l_new_fm_serial      := l_real_serial_prefix ||(l_serial_numeric + l_serial_quantity);
4324           l_current_to_serial  := l_real_serial_prefix ||(l_serial_numeric + l_serial_quantity - 1);
4325           l_rowid              := l_explode_detail.ROWID;
4326 
4327           UPDATE mtl_serial_numbers_temp
4328              SET fm_serial_number = l_new_fm_serial
4329            WHERE ROWID = l_rowid;
4330 
4331 
4332           IF (l_debug = 1) THEN
4333             DEBUG( 'Creating New MSNT Record with fm_serial_number :' ||
4334                     l_explode_detail.fm_serial_number || ', to_serial :' ||
4335                     l_current_to_serial , 'EXPLODE_DELIVERY_DETAILS');
4336           END IF;
4337 
4338           l_succes             :=
4339             inv_trx_util_pub.insert_ser_trx(
4340               p_trx_tmp_id                 => l_explode_detail.transaction_temp_id
4341             , p_user_id                    => fnd_global.user_id
4342             , p_fm_ser_num                 => l_explode_detail.fm_serial_number
4343             , p_to_ser_num                 => l_current_to_serial
4344             , p_serial_attribute_category  => l_explode_detail.serial_attribute_category
4345             , p_orgination_date            => l_explode_detail.origination_date
4346             , p_c_attribute1               => l_explode_detail.c_attribute1
4347             , p_c_attribute2               => l_explode_detail.c_attribute2
4348             , p_c_attribute3               => l_explode_detail.c_attribute3
4349             , p_c_attribute4               => l_explode_detail.c_attribute4
4350             , p_c_attribute5               => l_explode_detail.c_attribute5
4351             , p_c_attribute6               => l_explode_detail.c_attribute6
4352             , p_c_attribute7               => l_explode_detail.c_attribute7
4353             , p_c_attribute8               => l_explode_detail.c_attribute8
4354             , p_c_attribute9               => l_explode_detail.c_attribute9
4355             , p_c_attribute10              => l_explode_detail.c_attribute10
4356             , p_c_attribute11              => l_explode_detail.c_attribute11
4357             , p_c_attribute12              => l_explode_detail.c_attribute12
4358             , p_c_attribute13              => l_explode_detail.c_attribute13
4359             , p_c_attribute14              => l_explode_detail.c_attribute14
4360             , p_c_attribute15              => l_explode_detail.c_attribute15
4361             , p_c_attribute16              => l_explode_detail.c_attribute16
4362             , p_c_attribute17              => l_explode_detail.c_attribute17
4363             , p_c_attribute18              => l_explode_detail.c_attribute18
4364             , p_c_attribute19              => l_explode_detail.c_attribute19
4365             , p_c_attribute20              => l_explode_detail.c_attribute20
4366             , p_d_attribute1               => l_explode_detail.d_attribute1
4367             , p_d_attribute2               => l_explode_detail.d_attribute2
4368             , p_d_attribute3               => l_explode_detail.d_attribute3
4369             , p_d_attribute4               => l_explode_detail.d_attribute4
4370             , p_d_attribute5               => l_explode_detail.d_attribute5
4371             , p_d_attribute6               => l_explode_detail.d_attribute6
4372             , p_d_attribute7               => l_explode_detail.d_attribute7
4373             , p_d_attribute8               => l_explode_detail.d_attribute8
4374             , p_d_attribute9               => l_explode_detail.d_attribute9
4375             , p_d_attribute10              => l_explode_detail.d_attribute10
4376             , p_n_attribute1               => l_explode_detail.n_attribute1
4377             , p_n_attribute2               => l_explode_detail.n_attribute2
4378             , p_n_attribute3               => l_explode_detail.n_attribute3
4379             , p_n_attribute4               => l_explode_detail.n_attribute4
4380             , p_n_attribute5               => l_explode_detail.n_attribute5
4381             , p_n_attribute6               => l_explode_detail.n_attribute6
4382             , p_n_attribute7               => l_explode_detail.n_attribute7
4383             , p_n_attribute8               => l_explode_detail.n_attribute8
4384             , p_n_attribute9               => l_explode_detail.n_attribute9
4385             , p_n_attribute10              => l_explode_detail.n_attribute10
4386             , p_status_id                  => l_explode_detail.status_id
4387             , p_territory_code             => l_explode_detail.territory_code
4388             , p_time_since_new             => l_explode_detail.time_since_new
4389             , p_cycles_since_new           => l_explode_detail.cycles_since_new
4390             , p_time_since_overhaul        => l_explode_detail.time_since_overhaul
4391             , p_cycles_since_overhaul      => l_explode_detail.cycles_since_overhaul
4392             , p_time_since_repair          => l_explode_detail.time_since_repair
4393             , p_cycles_since_repair        => l_explode_detail.cycles_since_repair
4394             , p_time_since_visit           => l_explode_detail.time_since_visit
4395             , p_cycles_since_visit         => l_explode_detail.cycles_since_visit
4396             , p_time_since_mark            => l_explode_detail.time_since_mark
4397             , p_cycles_since_mark          => l_explode_detail.cycles_since_mark
4398             , p_number_of_repairs          => l_explode_detail.number_of_repairs
4399             , x_proc_msg                   => l_proc_msg
4400             );
4401         ELSE
4402           l_serial_quantity    := l_serial_quantity +(l_serial_numeric_end - l_serial_numeric + 1);
4403           l_current_to_serial  := l_explode_detail.to_serial_number;
4404         END IF;
4405 
4406         -- Determine length of numeric portion
4407         l_serial_numeric_len  := LENGTH(SUBSTR(l_explode_detail.fm_serial_number,
4408                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4409 
4410         -- update the serial attributes in mtl_serial_numbers
4411         IF (l_debug = 1) THEN
4412           DEBUG(
4413                'Before update the mtl_serial_numbers with org '
4414             || TO_CHAR(l_explode_detail.organization_id)
4415             || ' item '
4416             || TO_CHAR(l_explode_detail.inventory_item_id)
4417             || ' serial '
4418             || l_explode_detail.fm_serial_number
4419           , 'EXPLODE_DELIVERY_DETAILS'
4420           );
4421         END IF;
4422 
4423         OPEN c_serials(
4424               l_explode_detail.organization_id
4425             , l_explode_detail.inventory_item_id
4426             , l_explode_detail.fm_serial_number
4427             , l_current_to_serial
4428             , LENGTH(l_explode_detail.fm_serial_number)
4429                       );
4430 
4431         -- skip the from serial number
4432         --FETCH c_serials
4433          --INTO l_serial_number;
4434         LOOP
4435           FETCH c_serials
4436            INTO l_serial_number;
4437 
4438           EXIT WHEN c_serials%NOTFOUND;
4439 
4440           -- update the serial attributes in mtl_serial_numbers
4441           UPDATE mtl_serial_numbers
4442              SET serial_attribute_category = l_explode_detail.serial_attribute_category
4443                , origination_date = l_explode_detail.origination_date
4444                , c_attribute1 = l_explode_detail.c_attribute1
4445                , c_attribute2 = l_explode_detail.c_attribute2
4446                , c_attribute3 = l_explode_detail.c_attribute3
4447                , c_attribute4 = l_explode_detail.c_attribute4
4448                , c_attribute5 = l_explode_detail.c_attribute5
4449                , c_attribute6 = l_explode_detail.c_attribute6
4450                , c_attribute7 = l_explode_detail.c_attribute7
4451                , c_attribute8 = l_explode_detail.c_attribute8
4452                , c_attribute9 = l_explode_detail.c_attribute9
4453                , c_attribute10 = l_explode_detail.c_attribute10
4454                , c_attribute11 = l_explode_detail.c_attribute11
4455                , c_attribute12 = l_explode_detail.c_attribute12
4456                , c_attribute13 = l_explode_detail.c_attribute13
4457                , c_attribute14 = l_explode_detail.c_attribute14
4458                , c_attribute15 = l_explode_detail.c_attribute15
4459                , c_attribute16 = l_explode_detail.c_attribute16
4460                , c_attribute17 = l_explode_detail.c_attribute17
4461                , c_attribute18 = l_explode_detail.c_attribute18
4462                , c_attribute19 = l_explode_detail.c_attribute19
4463                , c_attribute20 = l_explode_detail.c_attribute20
4464                , d_attribute1 = l_explode_detail.d_attribute1
4465                , d_attribute2 = l_explode_detail.d_attribute2
4466                , d_attribute3 = l_explode_detail.d_attribute3
4467                , d_attribute4 = l_explode_detail.d_attribute4
4468                , d_attribute5 = l_explode_detail.d_attribute5
4469                , d_attribute6 = l_explode_detail.d_attribute6
4470                , d_attribute7 = l_explode_detail.d_attribute7
4471                , d_attribute8 = l_explode_detail.d_attribute8
4472                , d_attribute9 = l_explode_detail.d_attribute9
4473                , d_attribute10 = l_explode_detail.d_attribute10
4474                , n_attribute1 = l_explode_detail.n_attribute1
4475                , n_attribute2 = l_explode_detail.n_attribute2
4476                , n_attribute3 = l_explode_detail.n_attribute3
4477                , n_attribute4 = l_explode_detail.n_attribute4
4478                , n_attribute5 = l_explode_detail.n_attribute5
4479                , n_attribute6 = l_explode_detail.n_attribute6
4480                , n_attribute7 = l_explode_detail.n_attribute7
4481                , n_attribute8 = l_explode_detail.n_attribute8
4482                , n_attribute9 = l_explode_detail.n_attribute9
4483                , n_attribute10 = l_explode_detail.n_attribute10
4484                , status_id = l_explode_detail.status_id
4485                , territory_code = l_explode_detail.territory_code
4486                , time_since_new = l_explode_detail.time_since_new
4487                , cycles_since_new = l_explode_detail.cycles_since_new
4488                , time_since_overhaul = l_explode_detail.time_since_overhaul
4489                , cycles_since_overhaul = l_explode_detail.cycles_since_overhaul
4490                , time_since_repair = l_explode_detail.time_since_repair
4491                , cycles_since_repair = l_explode_detail.cycles_since_repair
4492                , time_since_visit = l_explode_detail.time_since_visit
4493                , cycles_since_visit = l_explode_detail.cycles_since_visit
4494                , time_since_mark = l_explode_detail.time_since_mark
4495                , cycles_since_mark = l_explode_detail.cycles_since_mark
4496                , number_of_repairs = l_explode_detail.number_of_repairs
4497                , group_mark_id = l_group_mark_id
4498            WHERE current_organization_id = l_explode_detail.organization_id
4499              AND inventory_item_id = l_explode_detail.inventory_item_id
4500              AND serial_number = l_serial_number;
4501 
4502           -- Copy all Processed serial number into the
4503           -- serial Table.
4504           l_serial_numbers_table(m)  := l_serial_number;
4505           IF (l_debug = 1) THEN
4506             DEBUG('(2)After Updating Serial Number :'||l_serial_number||' m :'||m,'EXPLODE_DELIVEY_DETAILS');
4507           END IF;
4508 
4509           IF (l_debug = 1) THEN
4510             DEBUG('m=' || m || ' l_serial_number= ' || l_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4511             DEBUG(' p_quantity= ' || p_quantity, 'EXPLODE_DELIVERY_DETAILS');
4512           END IF;
4513 
4514           m                          := m + 1;
4515 
4516           IF (m > p_quantity) THEN
4517             IF (l_debug = 1) THEN
4518               DEBUG('(1) x_transaction_temp_id= ' || x_transaction_temp_id, 'EXPLODE_DELIVERY_DETAILS');
4519             END IF;
4520 
4521             EXIT;
4522           END IF;
4523         END LOOP;
4524 
4525         CLOSE c_serials;
4526       END IF;
4527 
4528       IF (l_debug = 1) THEN
4529         DEBUG('(2)m else =' || m || ' l_serial_number= ' || l_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4530         DEBUG('(2)p_quantity= ' || p_quantity, 'EXPLODE_DELIVERY_DETAILS');
4531       END IF;
4532 
4533       IF (m > p_quantity) THEN
4534         IF (l_debug = 1) THEN
4535           DEBUG('(2) x_transaction_temp_id= ' || x_transaction_temp_id, 'EXPLODE_DELIVERY_DETAILS');
4536         END IF;
4537 
4538         EXIT;
4539       END IF;
4540     END LOOP;
4541 
4542 
4543     /***************************
4544     * Update all the Processed serial numbers
4545     * with the new already generated transaction
4546     * temp id
4547     ****************************/
4548     l_running_quantity  := l_serial_numbers_table.COUNT;
4549 
4550     FOR j IN 1 .. l_running_quantity LOOP
4551       UPDATE mtl_serial_numbers_temp
4552          SET transaction_temp_id = x_transaction_temp_id
4553        WHERE transaction_temp_id = p_transaction_temp_id
4554          AND fm_serial_number = l_serial_numbers_table(j);
4555     END LOOP;
4556 
4557     CLOSE c_explode_detail;
4558   ELSIF(p_serial_number_control_code IN(2, 5)) THEN
4559     l_serial_quantity   := p_quantity;
4560 
4561     /******************
4562     ** Bug No 3678190
4563     ** Set the out transaction temp value to the current transaction temp value.
4564     ******************/
4565     x_transaction_temp_id := p_transaction_temp_id;
4566 
4567     IF (l_debug = 1) THEN
4568       DEBUG('l_serial_quantity= ' || l_serial_quantity, 'EXPLODE_DELIVERY_DETAILS');
4569     END IF;
4570 
4571     SELECT inventory_item_id
4572       INTO l_inventory_item_id
4573       FROM mtl_reservations
4574      WHERE reservation_id = p_reservation_id;
4575 
4576     IF (l_debug = 1) THEN
4577        DEBUG('l_inventory_item_id: ' || l_inventory_item_id, 'EXPLODE_DELIVERY_DETAILS');
4578     END IF;
4579     -- added for patchset-i end_item_unit_validation
4580     IF (g_cross_project_allowed = 'N') THEN
4581        IF (g_cross_unit_allowed = 'N') THEN
4582         -- get the end item unit number
4583           SELECT oel.end_item_unit_number
4584             INTO l_end_item_unit_number
4585             FROM oe_order_lines_all oel, wsh_delivery_details_ob_grp_v wdd
4586            WHERE wdd.delivery_detail_id = p_delivery_detail_id
4587              AND wdd.source_header_id = oel.header_id
4588              AND wdd.source_line_id = oel.line_id;
4589 
4590           IF (l_debug = 1) THEN
4591             DEBUG('EIUN=' || l_end_item_unit_number, 'EXPLODE_DELIVERY_DETAILS');
4592           END IF;
4593 
4594           OPEN c_lpn_serial_e(p_organization_id, l_inventory_item_id, p_lpn_id, l_end_item_unit_number);
4595           n  := 1;
4596 
4597           LOOP
4598             FETCH c_lpn_serial_e
4599              INTO l_serial_numbers_table(n),
4600                   l_cur_group_mark_id,
4601                   l_cur_reservation_id;
4602             EXIT WHEN c_lpn_serial_e%NOTFOUND;
4603 
4604             IF (l_debug = 1) THEN
4605                DEBUG('c_lpn_serial_e Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4606                DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4607                DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4608                DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4609             END IF;
4610             IF n >= l_serial_quantity THEN
4611               EXIT;
4612             END IF;
4613 
4614             n  := n + 1;
4615           END LOOP;
4616 
4617           CLOSE c_lpn_serial_e;
4618        ELSE
4619 
4620           OPEN c_lpn_serial(p_organization_id, l_inventory_item_id, p_lpn_id);
4621           n  := 1;
4622 
4623           LOOP
4624             FETCH c_lpn_serial --INTO l_serial_numbers_table(n);
4625              INTO l_serial_numbers_table(n),
4626                   l_cur_group_mark_id,
4627                   l_cur_reservation_id;
4628 
4629             EXIT WHEN c_lpn_serial%NOTFOUND;
4630 
4631             IF (l_debug = 1) THEN
4632                DEBUG('c_lpn_serial Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4633                DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4634                DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4635                DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4636             END IF;
4637             IF n >= l_serial_quantity THEN
4638               EXIT;
4639             END IF;
4640 
4641             n  := n + 1;
4642           END LOOP;
4643 
4644           CLOSE c_lpn_serial;
4645        END IF;
4646     END IF;
4647 
4648     OPEN c_lpn_serial(p_organization_id, l_inventory_item_id, p_lpn_id);
4649     n                   := 1;
4650 
4651     LOOP
4652       FETCH c_lpn_serial --INTO l_serial_numbers_table(n);
4653        INTO l_serial_numbers_table(n),
4654             l_cur_group_mark_id,
4655             l_cur_reservation_id;
4656 
4657       EXIT WHEN c_lpn_serial%NOTFOUND;
4658 
4659        IF (l_debug = 1) THEN
4660           DEBUG('c_lpn_serial Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4661           DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4662           DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4663           DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4664        END IF;
4665 
4666 
4667       IF n >= l_serial_quantity THEN
4668         EXIT;
4669       END IF;
4670 
4671       n  := n + 1;
4672     END LOOP;
4673 
4674     CLOSE c_lpn_serial;
4675     n                   := 1;
4676     l_running_quantity  := l_serial_numbers_table.COUNT;
4677 
4678 
4679     WHILE(l_running_quantity >= 1)
4680     LOOP
4681       IF (l_debug = 1) THEN
4682         DEBUG('Marking serial number ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4683       END IF;
4684 
4685      /* 5506223: Because of serial reservations, the following API call to
4686       *      mark the Serial numbers will not work. It does not allow already
4687       *      reserved (which are already marked) serials to be remarked with the
4688       *      new mark id of Transaction_temp_id of MSNT
4689       serial_check.inv_mark_serial(
4690         from_serial_number           => l_serial_numbers_table(n)
4691       , to_serial_number             => l_serial_numbers_table(n)
4692       , item_id                      => l_inventory_item_id
4693       , org_id                       => p_organization_id
4694       , hdr_id                       => l_group_mark_id
4695       , temp_id                      => NULL
4696       , lot_temp_id                  => NULL
4697       , success                      => l_succes
4698       );
4699 
4700       IF l_succes < 1 THEN
4701         IF (l_debug = 1) THEN
4702           DEBUG('inv_mark_serial ended with errors (E) ', 'EXPLODE_DELIVERY_DETAILS');
4703         END IF;
4704 
4705         RAISE fnd_api.g_exc_error;
4706       END IF; */
4707 
4708       /* 5506223: Because of the above, using direct update to MSN to set the
4709        *          group_mark_id */
4710 
4711       BEGIN
4712          UPDATE mtl_serial_numbers
4713             SET group_mark_id     = l_group_mark_id
4714           WHERE inventory_item_id = l_inventory_item_id
4715             AND serial_number     = l_serial_numbers_table(n)
4716             AND current_organization_id = p_organization_id;
4717 
4718          IF SQL%NOTFOUND THEN
4719             IF (l_debug = 1) THEN
4720               DEBUG( 'Error finding serial to mark : '|| l_serial_numbers_table(n) ,
4721                      'EXPLODE_DELIVERY_DETAILS');
4722               DEBUG( 'l_inventory_item_id : '|| l_inventory_item_id ,
4723                      'EXPLODE_DELIVERY_DETAILS');
4724               DEBUG( 'p_organization_id : '|| p_organization_id ,
4725                      'EXPLODE_DELIVERY_DETAILS');
4726             END IF;
4727 
4728             RAISE fnd_api.g_exc_error;
4729          END IF;
4730       END ;
4731       IF (l_debug = 1) THEN
4732         DEBUG( 'inserting serials ' || l_serial_numbers_table(n) || ' into msnt WITH temp_id ='
4733               || p_transaction_temp_id , 'EXPLODE_DELIVERY_DETAILS');
4734       END IF;
4735 
4736       l_succes            :=
4737         inv_trx_util_pub.insert_ser_trx(
4738           p_trx_tmp_id                 => p_transaction_temp_id
4739         , p_user_id                    => fnd_global.user_id
4740         , p_fm_ser_num                 => l_serial_numbers_table(n)
4741         , p_to_ser_num                 => l_serial_numbers_table(n)
4742         , x_proc_msg                   => l_proc_msg
4743         );
4744 
4745       IF (l_succes < 0) THEN
4746         IF (l_debug = 1) THEN
4747           DEBUG('insert_ser_trx ended with errors ' || l_proc_msg, 'EXPLODE_DELIVERY_DETAILS');
4748         END IF;
4749 
4750         RAISE fnd_api.g_exc_error;
4751       END IF;
4752 
4753       IF (l_debug = 1) THEN
4754         DEBUG('l_running_quantity (while_loop)= ' || l_running_quantity, 'EXPLODE_DELIVERY_DETAILS');
4755       END IF;
4756 
4757       l_running_quantity  := l_running_quantity - 1;
4758       n                   := n + 1;
4759     END LOOP;
4760   END IF; -- Serial Control Code If
4761 EXCEPTION
4762   WHEN fnd_api.g_exc_error THEN
4763     x_return_status  := fnd_api.g_ret_sts_error;
4764     ROLLBACK;
4765     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4766   WHEN fnd_api.g_exc_unexpected_error THEN
4767     x_return_status  := fnd_api.g_ret_sts_unexp_error;
4768     ROLLBACK;
4769     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4770   WHEN OTHERS THEN
4771     x_return_status  := wsh_util_core.g_ret_sts_unexp_error;
4772     wsh_util_core.default_handler('WSH_DELIVERY_DETAILS_ACTIONS.EXPLODE_DELIVERY_DETAILS');
4773 END explode_delivery_details;
4774 
4775 
4776 /* Function overship_staged_lines
4777 splits a staged line to overship the quantity
4778 and returns the delivery details of the overshipped Lines
4779 Added bug4128854*/
4780 
4781 FUNCTION Overship_Staged_Lines(
4782 p_source_header_id IN NUMBER,
4783 p_source_line_id IN NUMBER,
4784 p_lpn_id IN NUMBER,
4785 p_mso_id IN NUMBER,
4786 p_organization_id IN NUMBER,
4787 p_cont_instance_id IN NUMBER,
4788 p_dock_door_id IN NUMBER,
4789 x_return_status   OUT NOCOPY    VARCHAR2,
4790 x_msg_count       OUT NOCOPY    NUMBER,
4791 x_msg_data        OUT NOCOPY    VARCHAR2)
4792 RETURN DELIVERY_DETAIL_TAB IS
4793 
4794 --Local Variables
4795 l_reservation_record         inv_reservation_global.mtl_reservation_rec_type;
4796 l_delivery_detail_tab        DELIVERY_DETAIL_TAB;
4797 l_mtl_reservation_tbl        inv_reservation_global.mtl_reservation_tbl_type;
4798 l_mtl_reservation_tbl_count  NUMBER;
4799 l_reservation_tbl            inv_reservation_global.mtl_reservation_tbl_type;
4800 l_detail_tab                 WSH_UTIL_CORE.ID_TAB_TYPE;
4801 l_return_status              VARCHAR2(1);
4802 l_msg_data                   VARCHAR2(3000);
4803 l_msg_count                  NUMBER;
4804 l_error_code                 VARCHAR2(20000);
4805 l_debug                      NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
4806 l_shipping_attr              WSH_DELIVERY_DETAILS_PUB.ChangedAttributeTabType;
4807 l_shipping_attr_tab          wsh_interface.changedattributetabtype;
4808 l_delivery_detail_id         NUMBER;
4809 l_picked_qty                 NUMBER;
4810 l_sec_picked_qty             NUMBER;
4811 l_shipped_qty                NUMBER;
4812 l_sec_shipped_qty            NUMBER;
4813 l_commit                     VARCHAR2(1) := fnd_api.g_false;
4814 l_new_delivery_detail_id     NUMBER;
4815 l_init_msg_list              VARCHAR2(1) := fnd_api.g_false;
4816 l_trip_id                    NUMBER;
4817 l_oe_order_header_id         NUMBER;
4818 l_dual_uom_item              BOOLEAN := FALSE;
4819 
4820 CURSOR c_lpn_heirarchy(p_outermost_lpn_id NUMBER) IS
4821 SELECT lpn_id
4822   FROM wms_license_plate_numbers
4823  WHERE outermost_lpn_id = p_outermost_lpn_id
4824  ORDER BY lpn_id;
4825 
4826 BEGIN
4827        x_return_status  := fnd_api.g_ret_sts_success;
4828       /*Query Reservations for LPN to be staged*/
4829 
4830       IF (l_debug = 1) THEN
4831          DEBUG('Inside Overship_Staged_Lines ', 'Overship Staged Lines');
4832          DEBUG('Source Header: '||p_source_header_id || ' : Line : ' ||
4833                                   p_source_line_id, 'Overship Staged Lines');
4834          DEBUG('p_lpn_id: '||p_lpn_id, 'Overship Staged Lines');
4835          DEBUG('p_mso_id: '||p_mso_id, 'Overship Staged Lines');
4836          DEBUG('p_organization_id: '||p_organization_id, 'Overship Staged Lines');
4837          DEBUG('p_cont_instance_id: '||p_cont_instance_id, 'Overship Staged Lines');
4838          DEBUG('p_dock_door_id: '||p_dock_door_id, 'Overship Staged Lines');
4839       END IF;
4840 
4841       -- get oe_order_header_id from sales order header id
4842       inv_salesorder.get_oeheader_for_salesorder(
4843           p_salesorder_id              => p_mso_id
4844         , x_oe_header_id               => l_oe_order_header_id
4845         , x_return_status              => l_return_status
4846         );
4847 
4848       l_reservation_record.demand_source_header_id  := p_mso_id;
4849       l_reservation_record.demand_source_line_id    := p_source_line_id;
4850       l_reservation_record.demand_source_type_id    := get_demand_src_type(l_oe_order_header_id);
4851       l_reservation_record.supply_source_type_id    := inv_reservation_global.g_source_type_inv;
4852 
4853       l_mtl_reservation_tbl.DELETE;
4854       FOR l_lpn_rec IN c_lpn_heirarchy(p_lpn_id) LOOP
4855           l_reservation_record.lpn_id := l_lpn_rec.lpn_id;
4856 
4857           IF (l_debug = 1) THEN
4858               DEBUG('Before call to query_reservation, LPN ID ' || l_lpn_rec.lpn_id, 'Overship Staged Lines');
4859           END IF;
4860 
4861           inv_reservation_pub.query_reservation(
4862               p_api_version_number         => 1.0
4863             , p_init_msg_lst               => fnd_api.g_false
4864             , x_return_status              => l_return_status
4865             , x_msg_count                  => l_msg_count
4866             , x_msg_data                   => l_msg_data
4867             , p_query_input                => l_reservation_record
4868             , p_lock_records               => fnd_api.g_false
4869             , x_mtl_reservation_tbl        => l_reservation_tbl
4870             , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
4871             , x_error_code                 => l_error_code);
4872 
4873           IF l_return_status = fnd_api.g_ret_sts_error THEN
4874              IF (l_debug = 1) THEN
4875                  DEBUG('Error during query_reservation for mso ID ' || p_mso_id
4876                         || ', oe line ID ' || p_source_line_id, 'Overship Staged Lines');
4877              END IF;
4878 
4879              RAISE fnd_api.g_exc_error;
4880           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4881              IF (l_debug = 1) THEN
4882                 DEBUG('Unexpected error during query_reservation for mso ID ' || p_mso_id
4883                        || ', oe line ID ' || p_source_line_id, 'Overship Staged Lines');
4884              END IF;
4885 
4886              RAISE fnd_api.g_exc_unexpected_error;
4887           END IF;
4888 
4889           FOR i IN 1..l_mtl_reservation_tbl_count LOOP
4890               l_mtl_reservation_tbl(l_mtl_reservation_tbl.COUNT + 1) := l_reservation_tbl(i);
4891           END LOOP;
4892        END LOOP;
4893 
4894       l_mtl_reservation_tbl_count := l_mtl_reservation_tbl.COUNT;
4895 
4896       IF inv_cache.set_item_rec(p_organization_id,
4897                                 l_mtl_reservation_tbl(l_mtl_reservation_tbl.FIRST).inventory_item_id)
4898       THEN
4899          IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
4900             l_dual_uom_item := TRUE;
4901             IF (l_debug = 1) THEN
4902                DEBUG('Item is dual UOM', 'Overship Staged Lines');
4903             END IF;
4904          ELSE
4905             l_dual_uom_item := FALSE;
4906             IF (l_debug = 1) THEN
4907                DEBUG('Item is NOT dual UOM', 'Overship Staged Lines');
4908             END IF;
4909          END IF;
4910       ELSE
4911          IF (l_debug = 1) THEN
4912             DEBUG('Error from inv_cache.set_item_rec', 'Overship Staged Lines');
4913          END IF;
4914          RAISE fnd_api.g_exc_unexpected_error;
4915       END IF;
4916 
4917       IF (l_debug = 1) THEN
4918          DEBUG('After call to query_reservation.  Record count: '||l_mtl_reservation_tbl_count,
4919                'Overship Staged Lines');
4920       END IF;
4921 
4922       /*Now we have all reservations for the LPN for this sales order and line
4923         which donot have any open WDDs left, This means we need to split a staged WDD*/
4924 
4925       FOR i in 1..l_mtl_reservation_tbl_count LOOP
4926 
4927       /*Find the parent delivery detail to split from (since all details are staged already)*/
4928           SELECT  wdd.delivery_detail_id
4929                 , wdd.picked_quantity
4930                 , wdd.picked_quantity2
4931                 , wdd.shipped_quantity
4932                 , wdd.shipped_quantity2
4933           INTO    l_delivery_detail_id
4934                 , l_picked_qty
4935                 , l_sec_picked_qty
4936                 , l_shipped_qty
4937                 , l_sec_shipped_qty
4938           FROM wms_direct_ship_temp  wds,
4939                wsh_delivery_details_ob_grp_v  wdd
4940           WHERE wds.organization_id = p_organization_id
4941             AND wds.lpn_id = p_lpn_id
4942             AND wds.order_header_id = p_source_header_id
4943             AND wds.order_line_id = p_source_line_id
4944             AND wdd.organization_id = p_organization_id
4945             AND wdd.source_header_id = wds.order_header_id
4946             AND wdd.source_line_id = wds.order_line_id
4947             AND wdd.released_status = 'Y'
4948             AND wdd.container_flag = 'N'
4949             AND rownum =1;
4950 
4951      /* Now we have the delivery_Detail to split so first update the quantity from
4952         Reservation on the wdd and then split from it*/
4953 
4954       IF (l_debug = 1) THEN
4955          DEBUG('l_picked_qty: '||l_picked_qty, 'Overship Staged Lines');
4956          DEBUG('l_sec_picked_qty: '||l_sec_picked_qty, 'Overship Staged Lines');
4957          DEBUG('l_shipped_qty: '||l_shipped_qty, 'Overship Staged Lines');
4958          DEBUG('l_sec_shipped_qty: '||l_sec_shipped_qty, 'Overship Staged Lines');
4959          DEBUG('l_delivery_detail_id: '||l_delivery_detail_id, 'Overship Staged Lines');
4960          DEBUG('Before calling Update_shipping_attributes to update qty', 'Overship Staged Lines');
4961       END IF;
4962       l_shipping_attr_tab.DELETE;  --Added delete to prevent usage from previous loop bug4128854
4963       l_shipping_attr_tab(1).delivery_detail_id := l_delivery_detail_id;
4964       l_shipping_attr_tab(1).action_flag      := 'U';
4965       l_shipping_attr_tab(1).released_status  := 'R';
4966 
4967       wsh_interface.update_shipping_attributes(p_source_code => 'INV',
4968        p_changed_attributes         => l_shipping_attr_tab
4969       ,x_return_status              => l_return_status);
4970 
4971       l_shipping_attr_tab.DELETE;
4972       l_shipping_attr_tab(1).picked_quantity := l_picked_qty + l_mtl_reservation_tbl(i).primary_reservation_quantity;
4973       l_shipping_attr_tab(1).shipped_quantity := l_shipped_qty + l_mtl_reservation_tbl(i).primary_reservation_quantity;
4974 
4975       IF l_dual_uom_item THEN
4976          l_shipping_attr_tab(1).picked_quantity2 := l_sec_picked_qty
4977                                                     + l_mtl_reservation_tbl(i).secondary_reservation_quantity;
4978          l_shipping_attr_tab(1).shipped_quantity2 := l_sec_shipped_qty
4979                                                      + l_mtl_reservation_tbl(i).secondary_reservation_quantity;
4980       END IF;
4981 
4982       l_shipping_attr_tab(1).delivery_detail_id := l_delivery_detail_id;
4983       l_shipping_attr_tab(1).action_flag      := 'U';
4984       l_shipping_attr_tab(1).released_status  := 'Y';
4985 
4986       IF (l_debug = 1) THEN
4987          DEBUG('picked_quantity: '|| l_shipping_attr_tab(1).picked_quantity, 'Overship Staged Lines');
4988          DEBUG('shipped_quantity: '|| l_shipping_attr_tab(1).shipped_quantity, 'Overship Staged Lines');
4989          DEBUG('picked_quantity2: '|| l_shipping_attr_tab(1).picked_quantity2, 'Overship Staged Lines');
4990          DEBUG('shipped_quantity2: '|| l_shipping_attr_tab(1).shipped_quantity2, 'Overship Staged Lines');
4991          DEBUG('delivery_detail_id: '|| l_shipping_attr_tab(1).delivery_detail_id, 'Overship Staged Lines');
4992          DEBUG('Before calling Update_shipping_attributes to update qty', 'Overship Staged Lines');
4993       END IF;
4994 
4995       wsh_interface.update_shipping_attributes(p_source_code => 'INV',
4996        p_changed_attributes         => l_shipping_attr_tab
4997       ,x_return_status              => l_return_status);
4998 
4999       IF (l_debug = 1) THEN
5000          DEBUG('After calling Update_shipping_attributes', 'Overship Staged Lines');
5001       END IF;
5002 
5003       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5004          IF (l_debug = 1) THEN
5005             DEBUG('Return error from update shipping attributes 2', 'Overship Staged Lines');
5006          END IF;
5007 
5008          RAISE fnd_api.g_exc_error;
5009       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5010          IF (l_debug = 1) THEN
5011             DEBUG('Return unexpected error from update shipping attributes', 'Overship Staged Lines');
5012          END IF;
5013 
5014          RAISE fnd_api.g_exc_unexpected_error;
5015       ELSIF l_return_status = 'S' THEN
5016          IF (l_debug = 1) THEN
5017             DEBUG('Shipping attributes updated successfully','Overship Staged Lines');
5018          END IF;
5019       END IF;
5020 
5021      /*At this point we need to split from this delivery detail*/
5022       IF (l_debug = 1) THEN
5023          DEBUG('Before Split Line'||l_delivery_Detail_id,'Overship Staged Lines');
5024       END IF;
5025 
5026       WSH_DELIVERY_DETAILS_PUB.split_line
5027         (p_api_version     => 1.0,
5028          p_init_msg_list   => fnd_api.g_false,
5029          p_commit          => fnd_api.g_false,
5030          x_return_status   => l_return_status,
5031          x_msg_count       => l_msg_count,
5032          x_msg_data        => l_msg_data,
5033          p_from_detail_id  => l_delivery_detail_id,
5034          x_new_detail_id   => l_new_delivery_detail_id,
5035          x_split_quantity  => l_mtl_reservation_tbl(i).primary_reservation_quantity,
5036          x_split_quantity2 => l_mtl_reservation_tbl(i).secondary_reservation_quantity);
5037 
5038       IF (l_debug = 1) THEN
5039          DEBUG('After Split Line'||l_new_delivery_Detail_id,'Overship Staged Lines');
5040       END IF;
5041 
5042       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5043          IF (l_debug = 1) THEN
5044             DEBUG('Return error from split_delivery_details ', 'Overship Staged Lines');
5045          END IF;
5046 
5047          RAISE fnd_api.g_exc_error;
5048       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5049          IF (l_debug = 1) THEN
5050             DEBUG('Return unexpected error from split_delivery_details', 'Overship Staged Lines');
5051          END IF;
5052 
5053          RAISE fnd_api.g_exc_unexpected_error;
5054       END IF;
5055 
5056      /*Unassign from old container*/
5057 
5058       WSH_DELIVERY_DETAILS_ACTIONS.Unassign_Detail_from_Cont(
5059         P_DETAIL_ID     => l_new_delivery_detail_id,
5060         X_RETURN_STATUS => l_return_status);
5061 
5062       IF (l_debug = 1) THEN
5063          DEBUG('After Unassign'|| l_return_status, 'Overship Staged Lines');
5064       END IF;
5065 
5066       IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
5067          IF (l_debug = 1) THEN
5068              DEBUG('Error Unassign_Detail_from_Cont'|| l_return_status, 'Overship Staged Lines');
5069          END IF;
5070          FND_MESSAGE.SET_NAME('INV', 'INV_UNASSIGN_DEL_FAILURE');
5071          FND_MSG_PUB.ADD;
5072          RAISE FND_API.G_EXC_ERROR;
5073       END IF;
5074 
5075       /*Now update the new delivery_Detail with correct attributes*/
5076       l_shipping_attr_tab.DELETE;
5077       l_shipping_attr_tab(1).delivery_detail_id   := l_new_delivery_detail_id;
5078       l_shipping_attr_tab(1).lot_number           := l_mtl_reservation_tbl(i).lot_number;
5079       l_shipping_attr_tab(1).revision             := l_mtl_reservation_tbl(i).revision;
5080       l_shipping_attr_tab(1).subinventory         := l_mtl_reservation_tbl(i).subinventory_code;
5081       l_shipping_attr_tab(1).locator_id           := l_mtl_reservation_tbl(i).locator_id;
5082       l_shipping_attr_tab(1).ship_from_org_id     := l_mtl_reservation_tbl(i).organization_id;
5083       l_shipping_attr_tab(1).transfer_lpn_id      := l_mtl_reservation_tbl(i).lpn_id;
5084       l_shipping_attr_tab(1).picked_quantity      := l_mtl_reservation_tbl(i).primary_reservation_quantity;
5085       l_shipping_attr_tab(1).shipped_quantity     := l_mtl_reservation_tbl(i).primary_reservation_quantity;
5086 
5087       IF l_dual_uom_item THEN
5088          l_shipping_attr_tab(1).picked_quantity2 := l_mtl_reservation_tbl(i).secondary_reservation_quantity;
5089          l_shipping_attr_tab(1).shipped_quantity2 := l_mtl_reservation_tbl(i).secondary_reservation_quantity;
5090       END IF;
5091 
5092       l_shipping_attr_tab(1).action_flag      := 'U';
5093       l_shipping_attr_tab(1).released_status  := 'Y';
5094 
5095       IF (l_debug = 1) THEN
5096          DEBUG('l_shipping_attr_tab(1).delivery_detail_id   '||
5097                 l_shipping_attr_tab(1).delivery_detail_id   ,'Overship Staged Lines');
5098          DEBUG('l_shipping_attr_tab(1).lot_number           '||
5099                 l_shipping_attr_tab(1).lot_number           ,'Overship Staged Lines');
5100          DEBUG('l_shipping_attr_tab(1).revision             '||
5101                 l_shipping_attr_tab(1).revision             ,'Overship Staged Lines');
5102          DEBUG('l_shipping_attr_tab(1).subinventory         '||
5103                 l_shipping_attr_tab(1).subinventory         ,'Overship Staged Lines');
5104          DEBUG('l_shipping_attr_tab(1).locator_id           '||
5105                 l_shipping_attr_tab(1).locator_id           ,'Overship Staged Lines');
5106          DEBUG('l_shipping_attr_tab(1).ship_from_org_id     '||
5107                 l_shipping_attr_tab(1).ship_from_org_id     ,'Overship Staged Lines');
5108          DEBUG('l_shipping_attr_tab(1).transfer_lpn_id      '||
5109                 l_shipping_attr_tab(1).transfer_lpn_id      ,'Overship Staged Lines');
5110          DEBUG('l_shipping_attr_tab(1).picked_quantity      '||
5111                 l_shipping_attr_tab(1).picked_quantity      ,'Overship Staged Lines');
5112          DEBUG('l_shipping_attr_tab(1).shipped_quantity     '||
5113                 l_shipping_attr_tab(1).shipped_quantity     ,'Overship Staged Lines');
5114          DEBUG('l_shipping_attr_tab(1).picked_quantity2     '||
5115                 l_shipping_attr_tab(1).picked_quantity2     ,'Overship Staged Lines');
5116          DEBUG('l_shipping_attr_tab(1).shipped_quantity2    '||
5117                 l_shipping_attr_tab(1).shipped_quantity2    ,'Overship Staged Lines');
5118          DEBUG('l_shipping_attr_tab(1).action_flag      '||
5119                 l_shipping_attr_tab(1).action_flag      ,'Overship Staged Lines');
5120          DEBUG('l_shipping_attr_tab(1).released_status  '||
5121                 l_shipping_attr_tab(1).released_status  ,'Overship Staged Lines');
5122       END IF;
5123       /*Verify the Action Flag*/
5124 
5125       wsh_interface.update_shipping_attributes(
5126         p_source_code                => 'INV',
5127         p_changed_attributes         => l_shipping_attr_tab,
5128         x_return_status              => l_return_status);
5129 
5130       IF (l_debug = 1) THEN
5131          DEBUG('After update shipping attributes', 'Overship Staged Lines');
5132       END IF;
5133 
5134       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5135          IF (l_debug = 1) THEN
5136             DEBUG('Return error from update shipping attributes', 'Overship Staged Lines');
5137          END IF;
5138 
5139          RAISE fnd_api.g_exc_error;
5140       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5141          IF (l_debug = 1) THEN
5142             DEBUG('Return unexpected error from update shipping attributes','Overship Staged Lines');
5143          END IF;
5144 
5145          RAISE fnd_api.g_exc_unexpected_error;
5146       END IF;
5147 
5148       SELECT wds.ORGANIZATION_ID
5149          ,WDS.DOCK_DOOR_ID
5150          ,WDS.LPN_ID
5151          ,WDS.ORDER_HEADER_ID
5152          ,WDS.ORDER_LINE_ID
5153          ,WDS.LINE_ITEM_ID
5154          ,WDS.TRANSACTION_TEMP_ID
5155          ,WDD.DELIVERY_DETAIL_ID
5156          ,DECODE(WDD.REQUESTED_QUANTITY_UOM,MSI.PRIMARY_UOM_CODE
5157                                             ,WDD.REQUESTED_QUANTITY
5158                                             ,GREATEST(INV_CONVERT.INV_UM_CONVERT(
5159                                                              null
5160                                                             ,null
5161                                                             ,WDD.REQUESTED_QUANTITY
5162                                                             ,WDD.REQUESTED_QUANTITY_UOM
5163                                                             ,MSI.PRIMARY_UOM_CODE
5164                                                             ,null
5165                                                            ,null),0)) REQUESTED_QUANTITY
5166         ,MSI.PRIMARY_UOM_CODE
5167         ,WDD.REQUESTED_QUANTITY2
5168         ,MSI.SECONDARY_UOM_CODE
5169         ,MSI.LOT_CONTROL_CODE
5170         ,MSI.SERIAL_NUMBER_CONTROL_CODE
5171     INTO l_delivery_detail_tab(i).ORGANIZATION_ID
5172          ,l_delivery_detail_tab(i).DOCK_DOOR_ID
5173          ,l_delivery_detail_tab(i).LPN_ID
5174          ,l_delivery_detail_tab(i).ORDER_HEADER_ID
5175          ,l_delivery_detail_tab(i).ORDER_LINE_ID
5176          ,l_delivery_detail_tab(i).LINE_ITEM_ID
5177          ,l_delivery_detail_tab(i).TRANSACTION_TEMP_ID
5178          ,l_delivery_detail_tab(i).DELIVERY_DETAIL_ID
5179          ,l_delivery_detail_tab(i).REQUESTED_QUANTITY
5180          ,l_delivery_detail_tab(i).PRIMARY_UOM_CODE
5181          ,l_delivery_detail_tab(i).REQUESTED_QUANTITY2
5182          ,l_delivery_detail_tab(i).REQUESTED_QUANTITY_UOM2
5183          ,l_delivery_detail_tab(i).LOT_CONTROL_CODE
5184          ,l_delivery_detail_tab(i).SERIAL_NUMBER_CONTROL_CODE
5185     FROM WMS_DIRECT_SHIP_TEMP WDS,
5186          WSH_DELIVERY_DETAILS WDD,
5187          MTL_SYSTEM_ITEMS MSI
5188     WHERE WDS.ORGANIZATION_ID           = p_organization_id
5189        AND WDS.lpn_id                   = p_lpn_id
5190        AND WDS.ORDER_HEADER_ID          = p_source_header_id
5191        AND WDS.ORDER_LINE_ID            = p_source_line_id
5192        AND WDD.ORGANIZATION_ID          = p_organization_id
5193        AND WDD.SOURCE_HEADER_ID         = WDS.ORDER_HEADER_ID
5194        AND WDD.SOURCE_LINE_ID           = WDS.ORDER_LINE_ID
5195        AND MSI.ORGANIZATION_ID          = p_organization_id
5196        AND MSI.INVENTORY_ITEM_ID        = WDD.INVENTORY_ITEM_ID
5197        AND WDD.delivery_detail_id       = l_new_delivery_detail_id
5198        AND rownum = 1
5199     ORDER BY WDS.LINE_ITEM_ID;
5200     END LOOP;
5201     RETURN l_delivery_detail_tab;
5202 END overship_staged_lines;
5203 
5204 PROCEDURE stage_lpns(
5205   x_return_status   OUT NOCOPY    VARCHAR2
5206 , x_msg_count       OUT NOCOPY    NUMBER
5207 , x_msg_data        OUT NOCOPY    VARCHAR2
5208 , p_group_id        IN            NUMBER
5209 , p_organization_id IN            NUMBER
5210 , p_dock_door_id    IN            NUMBER
5211 ) IS
5212   CURSOR outer_lpn IS
5213     SELECT DISTINCT lpn_id
5214                FROM wms_direct_ship_temp
5215               WHERE GROUP_ID = p_group_id
5216                 AND organization_id = p_organization_id
5217                 AND dock_door_id = p_dock_door_id;
5218 
5219   CURSOR inner_lpn(p_lpn_id NUMBER) IS
5220     SELECT lpn_id
5221       FROM wms_license_plate_numbers
5222      WHERE outermost_lpn_id = p_lpn_id;
5223 
5224   CURSOR stage_lines(p_lpn_id NUMBER) IS
5225     SELECT wds.order_header_id
5226          , wds.order_line_id
5227          , sub.reservable_type
5228          , msi.inventory_item_id
5229          , msi.reservable_type
5230          , msi.lot_control_code
5231          , msi.serial_number_control_code
5232       FROM wms_direct_ship_temp wds
5233          , wms_license_plate_numbers wlpn
5234          , mtl_secondary_inventories sub
5235          , mtl_system_items msi
5236      WHERE wds.GROUP_ID = p_group_id
5237        AND wds.organization_id = p_organization_id
5238        AND wds.dock_door_id = p_dock_door_id
5239        AND wds.lpn_id = p_lpn_id
5240        AND wlpn.lpn_id = wds.lpn_id
5241        AND sub.organization_id = wds.organization_id
5242        AND wlpn.subinventory_code = sub.secondary_inventory_name
5243        AND msi.organization_id = wds.organization_id
5244        AND msi.inventory_item_id = wds.line_item_id;
5245 
5246   CURSOR loaded_trips(p_organization_id NUMBER, p_dock_door_id NUMBER) IS
5247     SELECT DISTINCT trip_id
5248                FROM wms_shipping_transaction_temp
5249               WHERE organization_id = p_organization_id
5250                 AND dock_door_id = p_dock_door_id
5251                 AND dock_appoint_flag = 'N'
5252                 AND direct_ship_flag = 'Y';
5253 
5254   CURSOR trip_for_delivery(p_organization_id NUMBER, p_dock_door_id NUMBER) IS
5255     SELECT DISTINCT delivery_id
5256                FROM wms_shipping_transaction_temp
5257               WHERE organization_id = p_organization_id
5258                 AND dock_door_id = p_dock_door_id
5259                 AND NVL(trip_id, 0) = 0
5260                 AND direct_ship_flag = 'Y';
5261 
5262   CURSOR stage_delivery_detail(p_organization_id NUMBER, p_lpn_id NUMBER) IS
5263     SELECT wdd.delivery_detail_id
5264       FROM wsh_delivery_details_ob_grp_v wdd
5265          , wms_direct_ship_temp wds
5266      WHERE wds.organization_id = p_organization_id
5267        AND wds.lpn_id = p_lpn_id
5268        AND wdd.source_header_id = wds.order_header_id
5269        AND wdd.source_line_id = wds.order_line_id
5270        AND wdd.released_status IN('R', 'B')
5271        AND wdd.picked_quantity > 0;
5272 
5273   /* Bug:2463967 */
5274   CURSOR delete_details(p_outermost_lpn_id NUMBER) IS
5275     SELECT delivery_detail_id
5276       FROM wsh_delivery_details_ob_grp_v wdd
5277          , wms_license_plate_numbers lpn
5278      WHERE lpn.outermost_lpn_id = p_outermost_lpn_id
5279        AND lpn.lpn_id = wdd.lpn_id
5280        AND wdd.released_status = 'X'; -- For LPN reuse ER : 6845650
5281 
5282   -- bug 4306508
5283   CURSOR lpn_heirarchy(p_outermost_lpn_id NUMBER) IS
5284      SELECT lpn_id
5285        FROM wms_license_plate_numbers
5286        WHERE outermost_lpn_id = p_outermost_lpn_id
5287        ORDER BY lpn_id;
5288 
5289 
5290   CURSOR c_Get_OTM_flag(v_del_id NUMBER) IS
5291      SELECT ignore_for_planning, tms_interface_flag
5292        FROM wsh_new_deliveries_ob_grp_v
5293        WHERE delivery_id = v_del_id ;
5294 
5295   CURSOR c_ds_ct_wt_gtemp_exists(
5296         p_organization_id   IN NUMBER
5297        ,p_item_id           IN NUMBER
5298        ,p_lpn_id            IN VARCHAR2
5299        ,p_lot_number        IN VARCHAR2
5300        ,p_revision          IN VARCHAR2
5301         ) IS
5302         SELECT 1 FROM dual
5303         WHERE EXISTS
5304         (SELECT 1
5305            FROM wms_ds_ct_wt_gtemp
5306           WHERE org_id = p_organization_id
5307             AND inventory_item_id = p_item_id
5308             AND NVL(inner_lpn_id, lpn_id) = p_lpn_id
5309             AND NVL(lot_number,'#NULL#') = NVL(p_lot_number,'#NULL#')
5310             AND NVL(revision,'#NULL#') = NVL(p_revision,'#NULL#')
5311         );
5312 
5313   l_delivery_detail_tab        delivery_detail_tab;
5314   l_new_delivery_detail_id     NUMBER;
5315   l_dummy_num_var              NUMBER;
5316 
5317   /* For query reservations */
5318   l_reservation_record         inv_reservation_global.mtl_reservation_rec_type;
5319   l_reservation_tbl            inv_reservation_global.mtl_reservation_tbl_type; -- bug 4306508
5320   l_mtl_reservation_tbl        inv_reservation_global.mtl_reservation_tbl_type;
5321   l_mtl_reservation_tbl_count  NUMBER;
5322   l_reservation_exists_flag    VARCHAR2(1);
5323   l_reserved_lpn               NUMBER;
5324   l_return_status              VARCHAR2(1);
5325   l_msg_count                  NUMBER;
5326   l_msg_data                   VARCHAR2(20000);
5327   l_error_code                 NUMBER;
5328   l_dummy_sn                   inv_reservation_global.serial_number_tbl_type;
5329   l_outer_lpn                  outer_lpn%ROWTYPE;
5330   l_inner_lpn                  inner_lpn%ROWTYPE;
5331   l_mso_id                     NUMBER;
5332   l_outermost_lpn_id           NUMBER;
5333   /* For quantity tree declaration */
5334   l_return_msg                 VARCHAR2(20000);
5335   l_header_id                  NUMBER;
5336   l_line_id                    NUMBER;
5337   l_sub_reservable             NUMBER;
5338   l_inventory_item_id          NUMBER;
5339   l_item_reservable            NUMBER;
5340   l_lot_control_code           NUMBER;
5341   l_serial_number_control_code NUMBER;
5342   l_shipping_attr              wsh_interface.changedattributetabtype;
5343   l_invpcinrectype             wsh_integration.invpcinrectype;
5344   l_rsv_index                  NUMBER;
5345   l_rsv_qty                    NUMBER;
5346   l_rsv_sec_qty                NUMBER;
5347   l_return                     NUMBER;
5348   /* Auto Create Trip  */
5349   l_cont_instance_id           NUMBER;
5350   l_cont_name                  VARCHAR2(50);
5351   l_delivery_detail_id         NUMBER;
5352   l_delivery_id                NUMBER;
5353   l_delivery_tab               wsh_util_core.id_tab_type;
5354 
5355   l_trip_id                    NUMBER;
5356   l_trip_name                  VARCHAR2(30);
5357   /* Patchset I */
5358   l_last_del_index             NUMBER;
5359   l_lpn_count                  NUMBER; --Added bug 4212476
5360 
5361   --MR:Added for 4440809
5362   l_total_lpn_rsv_qty          NUMBER;
5363   l_total_lpn_rsv_sec_qty      NUMBER;
5364   l_processing_staged_Lines    VARCHAR2(1);
5365 
5366   l_ds_ct_wt_gtemp_exists      BOOLEAN; --BUG16793878
5367 
5368   /* Auto Create Deliveries for Delivery Details that has no delivery associated yet */
5369   /* Check if there is any trip loaded on the dock door, if yes assign new deliveries to it
5370      else check if existing delivery_details have a trip already associated else
5371      create new trip
5372        */
5373 
5374    l_action_prms      wsh_interface_ext_grp.del_action_parameters_rectype;
5375    l_delivery_id_tab  wsh_util_core.id_tab_type;
5376    l_delivery_out_rec wsh_interface_ext_grp.del_action_out_rec_type;
5377 
5378    l_debug                      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5379    -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
5380    l_sql_query                  VARCHAR2(10000);
5381    TYPE delivery_details_type IS REF CURSOR;
5382    delivery_details           delivery_details_type;
5383 
5384    /******************************
5385    * Creating an out Parameter in order to comply with the
5386    * New Signature of Explode_delivery_details
5387    *******************************/
5388      l_out_transaction_temp_id    NUMBER;
5389 
5390 
5391    /**********************************
5392    * variable to hold the ignore_for_planning flag of WDD.
5393    * Added for g-log changes
5394    **********************************/
5395    l_ignore_for_planning        wsh_delivery_details.ignore_for_planning%type;
5396    l_tms_interface_flag         wsh_new_deliveries.TMS_INTERFACE_FLAG%type;
5397 
5398    l_dual_uom_item              BOOLEAN := FALSE;
5399 
5400 BEGIN
5401 
5402   IF (l_debug = 1) THEN
5403      DEBUG('Inside Stage LPNs..: ' , 'STAGE_LPN');
5404      DEBUG('p_group_id: ' || p_group_id , 'STAGE_LPN');
5405      DEBUG('p_organization_id: ' || p_organization_id , 'STAGE_LPN');
5406      DEBUG('p_dock_door_id: ' || p_dock_door_id , 'STAGE_LPN');
5407   END IF;
5408 
5409   --initalizing l_InvPCInRecType to use for updating wdd with transaction_temp_id
5410   l_invpcinrectype.transaction_id       := NULL;
5411   l_invpcinrectype.transaction_temp_id  := NULL;
5412   l_invpcinrectype.source_code          := 'INV';
5413   l_invpcinrectype.api_version_number   := 1.0;
5414   OPEN outer_lpn;
5415 
5416   LOOP
5417     FETCH outer_lpn INTO l_outer_lpn;
5418     EXIT WHEN outer_lpn%NOTFOUND;
5419     wms_direct_ship_pvt.create_update_containers(
5420       x_return_status              => l_return_status
5421     , x_msg_count                  => l_msg_count
5422     , x_msg_data                   => l_msg_data
5423     , p_org_id                     => p_organization_id
5424     , p_outermost_lpn_id           => l_outer_lpn.lpn_id
5425     );
5426 
5427     IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
5428       IF (l_debug = 1) THEN
5429         DEBUG('create_update_containers API failed with status E ', 'STAGE_LPN');
5430       END IF;
5431 
5432       RAISE fnd_api.g_exc_error;
5433     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5434       IF (l_debug = 1) THEN
5435         DEBUG('create_update_containers failed with status U', 'STAGE_LPN');
5436       END IF;
5437 
5438       RAISE fnd_api.g_exc_unexpected_error;
5439     END IF;
5440 
5441     SELECT delivery_detail_id
5442       INTO l_cont_instance_id
5443       FROM wsh_delivery_details_ob_grp_v
5444      WHERE lpn_id = l_outer_lpn.lpn_id
5445      AND released_status = 'X';   -- For LPN reuse ER : 6845650
5446 
5447     SELECT wdd.delivery_detail_id
5448       INTO l_delivery_detail_id
5449       FROM wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
5450      WHERE wds.organization_id = p_organization_id
5451        AND wds.lpn_id = l_outer_lpn.lpn_id
5452        AND wdd.source_header_id = wds.order_header_id
5453        AND wdd.source_line_id = wds.order_line_id
5454        AND wdd.released_status IN('R', 'B', 'Y') --Added 'Y' bug4128854
5455        AND ROWNUM = 1;
5456 
5457     IF (l_debug = 1) THEN
5458       DEBUG('The delivery_detail_id for the lpn_Id is ' || l_cont_instance_id, 'STAGE_LPN');
5459       DEBUG('The delivery_detail_id for the first line is ' || l_delivery_detail_id, 'STAGE_LPN');
5460     END IF;
5461 
5462     wms_direct_ship_pvt.container_nesting(
5463       x_return_status              => l_return_status
5464     , x_msg_count                  => l_msg_count
5465     , x_msg_data                   => l_msg_data
5466     , p_organization_id            => p_organization_id
5467     , p_outermost_lpn_id           => l_outer_lpn.lpn_id
5468     , p_action_code                => 'PACK');
5469 
5470     IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5471       IF (l_debug = 1) THEN
5472         DEBUG('container nesting API failed for outermost_lpn_id ' || l_outer_lpn.lpn_id, 'stage_lpns');
5473       END IF;
5474 
5475       RAISE fnd_api.g_exc_error;
5476     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5477       IF (l_debug = 1) THEN
5478         DEBUG('container nesting API failed with unexpected errors for outermost_lpn_id '
5479                || l_outer_lpn.lpn_id, 'stage_lpns');
5480       END IF;
5481 
5482       RAISE fnd_api.g_exc_unexpected_error;
5483     END IF;
5484 
5485     IF (l_debug = 1) THEN
5486       DEBUG('Container nesting completed', 'xxxx');
5487     END IF;
5488 
5489     wsh_container_actions.update_cont_hierarchy(
5490       p_del_detail_id              => l_delivery_detail_id
5491     , p_delivery_id                => NULL
5492     , p_container_instance_id      => l_cont_instance_id
5493     , x_return_status              => l_return_status
5494     );
5495 
5496     IF l_return_status <> wsh_util_core.g_ret_sts_success THEN
5497       fnd_message.set_name('WSH', 'WSH_CONT_UPD_ATTR_ERROR');
5498       fnd_message.set_token('CONT_NAME', l_cont_name);
5499     --x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
5500     END IF;
5501 
5502     IF (l_debug = 1) THEN
5503       DEBUG('The Update container_Hierarchy was successful for outer-lpn ' || l_outer_lpn.lpn_id, 'STAGE_LPN');
5504     END IF;
5505   END LOOP;
5506 
5507   CLOSE outer_lpn;
5508 
5509 
5510   OPEN outer_lpn;
5511 
5512   LOOP
5513     IF (l_debug = 1) THEN
5514       DEBUG('Open outer lpn loop', 'Stage_LPNS');
5515     END IF;
5516 
5517     FETCH outer_lpn INTO l_outer_lpn;
5518     EXIT WHEN outer_lpn%NOTFOUND;
5519     OPEN stage_lines(l_outer_lpn.lpn_id);
5520 
5521     IF (l_debug = 1) THEN
5522       DEBUG('Open stage loop LPN ID: ' || l_outer_lpn.lpn_id, 'Stage_LPNS');
5523     END IF;
5524 
5525     LOOP
5526       FETCH stage_lines
5527       INTO l_header_id
5528          , l_line_id
5529          , l_sub_reservable
5530          , l_inventory_item_id
5531          , l_item_reservable
5532          , l_lot_control_code
5533          , l_serial_number_control_code;
5534       EXIT WHEN stage_lines%NOTFOUND;
5535 
5536       IF (l_debug = 1) THEN
5537         DEBUG('Stage Line Loop Line_ID :' || l_line_id, 'Stage_LPNS');
5538       END IF;
5539 
5540       IF inv_cache.set_item_rec(p_organization_id, l_inventory_item_id) THEN
5541          IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
5542             l_dual_uom_item := TRUE;
5543             IF (l_debug = 1) THEN
5544                DEBUG('Item is dual UOM', 'Stage_LPNS');
5545             END IF;
5546          ELSE
5547             l_dual_uom_item := FALSE;
5548             IF (l_debug = 1) THEN
5549                DEBUG('Item is NOT dual UOM', 'Stage_LPNS');
5550             END IF;
5551          END IF;
5552       ELSE
5553          IF (l_debug = 1) THEN
5554             DEBUG('Error from inv_cache.set_item_rec', 'Stage_LPNS');
5555          END IF;
5556          RAISE fnd_api.g_exc_unexpected_error;
5557       END IF;
5558 
5559       IF (l_sub_reservable = 1
5560           AND l_item_reservable = 1) THEN
5561         l_mso_id                                       := inv_salesorder.get_salesorder_for_oeheader(l_header_id);
5562         l_reservation_record.demand_source_type_id     := get_demand_src_type(l_header_id);
5563         l_reservation_record.demand_source_header_id   := l_mso_id;
5564         l_reservation_record.demand_source_line_id     := l_line_id;
5565         l_reservation_record.demand_source_line_detail := NULL;
5566 
5567         --  bug 4306508 : need to handle the case for nested LPNs too
5568         l_mtl_reservation_tbl.DELETE;
5569         FOR l_lpn_rec IN lpn_heirarchy(l_outer_lpn.lpn_id) LOOP
5570 
5571            l_reservation_record.lpn_id                   := l_lpn_rec.lpn_id;
5572            -- bug 4271408
5573            --l_reservation_record.lpn_id                   := l_outer_lpn.lpn_id;
5574 
5575            IF (l_debug = 1) THEN
5576               DEBUG('Before call to reservation demand_source_line_id ' || l_line_id, 'STAGE_LPNs');
5577            END IF;
5578 
5579            inv_reservation_pub.query_reservation
5580              (p_api_version_number         => 1.0
5581               , p_init_msg_lst               => fnd_api.g_false
5582               , x_return_status              => l_return_status
5583               , x_msg_count                  => l_msg_count
5584               , x_msg_data                   => l_msg_data
5585               , p_query_input                => l_reservation_record
5586               , p_lock_records               => fnd_api.g_false
5587               , x_mtl_reservation_tbl        => l_reservation_tbl
5588               , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5589               , x_error_code                 => l_error_code);
5590 
5591            IF l_return_status = fnd_api.g_ret_sts_error THEN
5592               IF (l_debug = 1) THEN
5593                  DEBUG('Validation error during query of Reservations ' || l_header_id
5594                         || ' ' || l_line_id, 'STAGE_LPNS');
5595               END IF;
5596 
5597               RAISE fnd_api.g_exc_error;
5598             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5599               IF (l_debug = 1) THEN
5600                  DEBUG('Unexpected error during query of Reservations ' || l_header_id || ' '
5601                          || l_line_id, 'STAGE_LPNS');
5602               END IF;
5603 
5604               RAISE fnd_api.g_exc_unexpected_error;
5605            END IF;
5606 
5607            FOR i IN 1..l_mtl_reservation_tbl_count LOOP
5608               l_mtl_reservation_tbl(l_mtl_reservation_tbl.COUNT + 1) := l_reservation_tbl(i);
5609            END LOOP;
5610 
5611         END LOOP;
5612 
5613         l_mtl_reservation_tbl_count := l_mtl_reservation_tbl.COUNT;
5614 
5615       END IF;
5616 
5617 
5618       /* For each delivery details get the Reservation details and stage the delivery lines */
5619       IF (l_debug = 1) THEN
5620         DEBUG('After call to reservation No of Rsv Records: ' || l_mtl_reservation_tbl_count, 'STAGE_LPNs');
5621       END IF;
5622 
5623       -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
5624       l_sql_query :=
5625         '    SELECT   wds.organization_id ' ||
5626         '           , wds.dock_door_id ' ||
5627         '           , wds.lpn_id ' ||
5628         '           , wds.order_header_id ' ||
5629         '           , wds.order_line_id ' ||
5630         '           , wds.line_item_id ' ||
5631         '           , wds.transaction_temp_id ' ||
5632         '           , wdd.delivery_detail_id ' ||
5633         '           , DECODE( ' ||
5634         '               wdd.requested_quantity_uom ' ||
5635         '             , msi.primary_uom_code, wdd.requested_quantity ' ||
5636         '             , GREATEST( ' ||
5637         '                        inv_convert.inv_um_convert(NULL, ' ||
5638         '                        NULL, wdd.requested_quantity, wdd.requested_quantity_uom, ' ||
5639         '                        msi.primary_uom_code, NULL , NULL) ' ||
5640         '                     , 0 )) requested_quantity ' ||
5641         '           , msi.primary_uom_code ' ||
5642         '           , wdd.requested_quantity2 ' ||
5643         '           , msi.secondary_uom_code ' ||
5644         '           , msi.lot_control_code ' ||
5645         '           , msi.serial_number_control_code ' ||
5646         '           , msi.inventory_item_id ';
5647 
5648         IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL) THEN
5649           l_sql_query := l_sql_query || '           , msi.ont_pricing_qty_source ';
5650         ELSE
5651           l_sql_query := l_sql_query || '           , NULL ont_pricing_qty_source ';
5652         END IF;
5653 
5654     l_sql_query := l_sql_query ||
5655         '        FROM wms_direct_ship_temp wds, ' ||
5656         '             wsh_delivery_details_ob_grp_v wdd, ' ||
5657         '             mtl_system_items msi ' ||
5658         '       WHERE wds.organization_id = :p_organization_id ' ||
5659         '         AND wds.lpn_id = :p_lpn_id ' ||
5660         '         AND wds.order_header_id = :p_order_header_id ' ||
5661         '         AND wds.order_line_id = :p_order_line_id ' ||
5662         '         AND wdd.organization_id = :p_organization_id ' ||
5663         '         AND wdd.source_header_id = wds.order_header_id ' ||
5664         '         AND wdd.source_line_id = wds.order_line_id ' ||
5665         '         AND msi.organization_id = :p_organization_id ' ||
5666         '         AND msi.inventory_item_id = wdd.inventory_item_id ' ||
5667         '         AND wdd.released_status IN (''B'', ''R'') ' ||
5668         '         AND wdd.container_flag = ''N'' ' ||
5669         '    ORDER BY wds.line_item_id';
5670 
5671       -- OPEN delivery_details(l_outer_lpn.lpn_id, l_header_id, l_line_id);
5672       OPEN delivery_details FOR l_sql_query
5673         USING p_organization_id,
5674               l_outer_lpn.lpn_id,
5675               l_header_id,
5676               l_line_id,
5677               p_organization_id,
5678               p_organization_id;
5679 
5680       FETCH delivery_details BULK COLLECT
5681        INTO l_delivery_detail_tab;
5682 
5683       l_last_del_index  := l_delivery_detail_tab.COUNT;
5684 
5685       IF (l_debug = 1) THEN
5686          DEBUG('l_last_del_index=' || l_last_del_index, 'STAGE_LPN');
5687       END IF;
5688 
5689       CLOSE delivery_details;
5690 
5691       l_processing_staged_Lines  := 'N';
5692 
5693       IF l_delivery_detail_tab.COUNT = 0 then  --Added code bug 4128854 to overship a staged line
5694           l_delivery_detail_tab := Overship_Staged_Lines(
5695                      p_source_header_id =>l_header_id,
5696                      p_source_line_id =>l_line_id,
5697                      p_lpn_id =>l_outer_lpn.lpn_id,
5698                      p_mso_id =>l_mso_id,
5699                      p_organization_id => p_organization_id,
5700                      p_cont_instance_id => l_cont_instance_id,
5701                      p_dock_door_id => p_dock_door_id,
5702                      x_return_status   => l_return_status,
5703                      x_msg_count       => l_msg_count,
5704                      x_msg_data        => l_msg_data);
5705 
5706             l_processing_staged_Lines  := 'Y';
5707       END IF;
5708 
5709       IF (l_debug = 1) THEN
5710         DEBUG('No of delivery details for the demand_source: ' || l_delivery_detail_tab.COUNT, 'STAGE_LPN');
5711       END IF;
5712 
5713       IF (l_sub_reservable = 1
5714           AND l_item_reservable = 1) THEN
5715            -- If item = Vanilla or Lot Controlled THEN Query reservation and
5716            -- stage the delivery lines with the reserved Qty
5717         l_rsv_index   := 1;
5718         l_rsv_qty     := 0;
5719         l_rsv_sec_qty := 0;
5720         l_total_lpn_rsv_qty     := 0;
5721         l_total_lpn_rsv_sec_qty := 0;
5722 
5723         <<delivery_detail_loop>> -- 2529382
5724         FOR i IN 1 .. l_delivery_detail_tab.COUNT LOOP
5725           IF (l_debug = 1) THEN
5726             DEBUG('Clear the change attribute table ', 'Stage_LPNs');
5727           END IF;
5728 
5729           l_shipping_attr.DELETE;
5730           l_out_transaction_temp_id := null; --Bug 4592143
5731           l_lpn_count :=  0; /*Added bug4212476*/
5732           FOR j IN l_rsv_index .. l_mtl_reservation_tbl.COUNT LOOP
5733              IF l_mtl_reservation_tbl(j).lpn_id IS NOT NULL THEN
5734                 SELECT outermost_lpn_id
5735                   INTO l_outermost_lpn_id
5736                   FROM wms_license_plate_numbers
5737                   WHERE lpn_id = l_mtl_reservation_tbl(j).lpn_id;
5738 
5739                 IF (l_debug = 1) THEN
5740                    DEBUG('l_outermost_lpn_id: ' || l_outermost_lpn_id, 'Stage_LPNs');
5741                    DEBUG('l_outer_lpn.lpn_id: ' || l_outer_lpn.lpn_id, 'Stage_LPNs');
5742                 END IF;
5743                 IF l_outer_lpn.lpn_id = l_outermost_lpn_id THEN
5744                    l_lpn_count := l_lpn_count + 1;
5745                    IF i = 1 THEN  -- get the total per LPN only once for multiple eligible WDDs
5746                       l_total_lpn_rsv_qty := l_total_lpn_rsv_qty +
5747                              l_mtl_reservation_tbl(j).primary_reservation_quantity; --MR:added for 4440809
5748                       IF l_dual_uom_item THEN
5749                          l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty +
5750                                 l_mtl_reservation_tbl(j).secondary_reservation_quantity; --MR:added for 4440809
5751                       END IF;
5752                    END IF;
5753                    IF (l_debug = 1) THEN
5754                       DEBUG('l_total_lpn_rsv_qty : ' || l_total_lpn_rsv_qty || ':I:'|| i ,'Stage_LPNs');
5755                       DEBUG('l_total_lpn_rsv_sec_qty : ' || l_total_lpn_rsv_sec_qty || ':I:'|| i ,'Stage_LPNs');
5756                    END IF;
5757                 END IF;
5758                 IF (l_debug = 1) THEN
5759                    DEBUG('l_mtl_reservation_tbl(j).lpn_id: ' ||
5760                           l_mtl_reservation_tbl(j).lpn_id ,'Stage_LPNs');
5761                 END IF;
5762              END IF;
5763           END LOOP;     /*Added bug4212476*/
5764           IF (l_debug = 1) THEN
5765              DEBUG('l_lpn_count : ' || l_lpn_count ,'Stage_LPNs');
5766              DEBUG('l_processing_staged_lines : ' || l_processing_staged_lines ,'Stage_LPNs');
5767           END IF;
5768 
5769           FOR j IN l_rsv_index .. l_mtl_reservation_tbl.COUNT
5770           LOOP
5771             l_shipping_attr(1).pending_quantity := 0;
5772 			IF l_dual_uom_item THEN
5773 				l_shipping_attr(1).pending_quantity2 := 0;
5774             END IF;
5775             IF l_mtl_reservation_tbl(j).lpn_id IS NULL THEN
5776               GOTO next_resv_rec;
5777             END IF;
5778 
5779             IF (l_debug = 1) THEN
5780               DEBUG('Call the select statement for rsv lpn_id  ' || l_mtl_reservation_tbl(j).lpn_id, 'Stage_LPNs');
5781             END IF;
5782 
5783             SELECT outermost_lpn_id
5784               INTO l_outermost_lpn_id
5785               FROM wms_license_plate_numbers
5786              WHERE lpn_id = l_mtl_reservation_tbl(j).lpn_id;
5787 
5788             IF l_outer_lpn.lpn_id = l_outermost_lpn_id THEN
5789               IF (l_debug = 1) THEN
5790                 DEBUG('The Outermost LPN match ', 'Stage_LPNs');
5791                 DEBUG('last index is '||l_last_del_index,'Stage LPNs');
5792               END IF;
5793               IF l_last_del_index <> 0 THEN   -- Added bug 4128854
5794 
5795                  l_shipping_attr(1).source_line_id      := l_mtl_reservation_tbl(j).demand_source_line_id;
5796                  l_shipping_attr(1).ship_from_org_id    := l_mtl_reservation_tbl(j).organization_id;
5797                  l_shipping_attr(1).subinventory        := l_mtl_reservation_tbl(j).subinventory_code;
5798                  l_shipping_attr(1).revision            := l_mtl_reservation_tbl(j).revision;
5799                  l_shipping_attr(1).locator_id          := l_mtl_reservation_tbl(j).locator_id;
5800                  l_shipping_attr(1).lot_number          := l_mtl_reservation_tbl(j).lot_number;
5801                  l_shipping_attr(1).transfer_lpn_id     := l_mtl_reservation_tbl(j).lpn_id;
5802               END IF;
5803                  l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_tab(i).delivery_detail_id;
5804                  l_shipping_attr(1).inventory_item_id  := l_delivery_detail_tab(i).inventory_item_id;
5805 
5806             ELSE
5807              IF (l_debug = 1) THEN
5808                 DEBUG('LPN do not match outerlpn_id  ' || l_outer_lpn.lpn_id, 'Stage_LPNs');
5809               END IF;
5810 
5811               GOTO next_resv_rec;
5812             END IF;
5813 
5814             IF (l_debug = 1) THEN
5815               DEBUG('l_rsv_qty ' || l_rsv_qty, 'STAGE_LPN');
5816               DEBUG('l_rsv_sec_qty ' || l_rsv_sec_qty, 'STAGE_LPN');
5817             END IF;
5818 
5819             IF g_fulfillment_base = 'P' AND l_rsv_qty <= 0
5820             THEN
5821                l_rsv_qty := l_mtl_reservation_tbl(j).primary_reservation_quantity;
5822                 IF l_dual_uom_item THEN
5823 					l_rsv_sec_qty := l_mtl_reservation_tbl(j).secondary_reservation_quantity;
5824 				END IF;
5825             ELSIF g_fulfillment_base = 'S' AND l_rsv_sec_qty <= 0
5826             THEN
5827                l_rsv_qty := l_mtl_reservation_tbl(j).primary_reservation_quantity;
5828                l_rsv_sec_qty := l_mtl_reservation_tbl(j).secondary_reservation_quantity;
5829             END IF;
5830 
5831             -- Added for bug 4440809
5832             IF l_processing_staged_Lines = 'Y' THEN
5833                l_total_lpn_rsv_qty := l_rsv_qty;
5834                IF l_dual_uom_item THEN
5835                   l_total_lpn_rsv_sec_qty := l_rsv_sec_qty;
5836                END IF;
5837             END IF;
5838 
5839             IF (l_debug = 1) THEN
5840                   DEBUG('l_lpn_count: ' || l_lpn_count, 'STAGE_LPNS');
5841                   DEBUG('i : ' || i || ' :l_last_del_index: ' || l_last_del_index , 'STAGE_LPNS');
5842                   DEBUG('l_rsv_qty ' || l_rsv_qty , 'STAGE_LPNS');
5843                   DEBUG('l_rsv_sec_qty ' || l_rsv_sec_qty , 'STAGE_LPNS');
5844                   DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
5845                   DEBUG('l_total_lpn_rsv_sec_qty ' || l_total_lpn_rsv_sec_qty , 'STAGE_LPNS');
5846                   DEBUG('l_delivery_detail_tab(i).requested_quantity: '
5847                            || l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5848                   DEBUG('l_delivery_detail_tab(i).requested_quantity2: '
5849                            || l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
5850             END IF;
5851 
5852             IF g_fulfillment_base = 'P' THEN --{
5853 
5854                IF l_rsv_qty >= l_delivery_detail_tab(i).requested_quantity THEN --{
5855                   IF (l_debug = 1) THEN
5856                      DEBUG('Reservation quantity >= delivery detail quantity', 'STAGE_LPNS');
5857                   END IF;
5858                   -- Bug : 4440809 : Start
5859 
5860                   IF l_rsv_qty = l_delivery_detail_tab(i).requested_quantity THEN
5861                      IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
5862                         IF (i >= l_last_del_index) THEN
5863                            l_shipping_attr(1).action_flag      := 'M';
5864                            l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
5865                            IF l_dual_uom_item THEN
5866                               l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
5867                            END IF;
5868                         ELSE
5869                            l_shipping_attr(1).action_flag      := 'U';
5870                         END IF;
5871                      ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
5872                         l_shipping_attr(1).action_flag      := 'U';
5873                      ELSIF l_rsv_qty > l_total_lpn_rsv_qty  THEN
5874                         IF (l_debug = 1) THEN
5875                            DEBUG('l_rsv_qty > l_total_lpn_rsv_qty : ' , 'STAGE_LPNS');
5876                         END IF;
5877                         NULL; -- why shld this condition happen
5878                      END IF;
5879                   END IF;
5880 
5881                   l_shipping_attr(1).picked_quantity := l_rsv_qty;
5882                   IF l_dual_uom_item THEN
5883 					l_shipping_attr(1).picked_quantity2 := l_rsv_sec_qty;
5884                   END IF;
5885 				  /* bug 13582056 */
5886                   l_shipping_attr(1).released_status := 'Y';
5887 
5888                   IF l_rsv_qty > l_delivery_detail_tab(i).requested_quantity THEN
5889                      IF (i >= l_last_del_index) THEN
5890                         IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
5891                            l_shipping_attr(1).action_flag      := 'M';
5892                            l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
5893                            IF l_dual_uom_item THEN
5894                               l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
5895                            END IF;
5896                         ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
5897                            l_shipping_attr(1).action_flag      := 'U';
5898                         ELSIF l_rsv_qty > l_total_lpn_rsv_qty  THEN
5899                            IF (l_debug = 1) THEN
5900                               DEBUG('l_rsv_qty > l_total_lpn_rsv_qty : ' , 'STAGE_LPNS');
5901                            END IF;
5902                            NULL; -- why shld this condition happen
5903                         END IF;
5904                         l_delivery_detail_tab(i).requested_quantity := l_rsv_qty; -- **MR 01/04/06
5905                         l_total_lpn_rsv_qty := l_total_lpn_rsv_qty - l_rsv_qty;
5906                         l_rsv_qty := 0;
5907                         IF l_dual_uom_item THEN
5908                            l_delivery_detail_tab(i).requested_quantity2 := l_rsv_sec_qty;
5909                            l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
5910                            l_rsv_sec_qty := 0;
5911                         END IF;
5912                      ELSE
5913                         l_total_lpn_rsv_qty := l_total_lpn_rsv_qty
5914                                                - l_delivery_detail_tab(i).requested_quantity;
5915                         l_shipping_attr(1).picked_quantity := l_delivery_detail_tab(i).requested_quantity;
5916                         l_rsv_qty := l_rsv_qty - l_delivery_detail_tab(i).requested_quantity ;
5917                         l_shipping_attr(1).action_flag := 'U';
5918                         l_delivery_detail_tab(i).requested_quantity  := l_rsv_qty; -- **MR 01/04/06
5919                         IF l_dual_uom_item THEN
5920                            l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty
5921                                                       - l_delivery_detail_tab(i).requested_quantity2;
5922                            l_shipping_attr(1).picked_quantity2 := l_delivery_detail_tab(i).requested_quantity2;
5923                            l_rsv_sec_qty := l_rsv_sec_qty - l_delivery_detail_tab(i).requested_quantity2;
5924                            l_delivery_detail_tab(i).requested_quantity2 := l_rsv_sec_qty;
5925                         END IF;
5926                      END IF; -- (i >= l_last_del_index) THEN
5927                   ELSIF l_rsv_qty = l_delivery_detail_tab(i).requested_quantity THEN
5928                      l_total_lpn_rsv_qty := l_total_lpn_rsv_qty - l_rsv_qty;
5929                      l_rsv_qty := 0;
5930                      IF l_dual_uom_item THEN
5931                         l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
5932                         l_rsv_sec_qty := 0;
5933                      END IF;
5934                   END IF;
5935 
5936                   IF (l_debug = 1) THEN
5937                      DEBUG('l_shipping_attr(1).action_flag: ' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
5938                      DEBUG('l_shipping_attr(1).pending_quantity: ' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
5939                      DEBUG('l_shipping_attr(1).pending_quantity2: ' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');
5940                      DEBUG('l_shipping_attr(1).picked_quantity: ' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
5941                      DEBUG('l_shipping_attr(1).picked_quantity2: ' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
5942                      DEBUG('l_total_lpn_rsv_qty: '|| l_total_lpn_rsv_qty, 'STAGE_LPNS');
5943                      DEBUG('l_total_lpn_rsv_sec_qty: '|| l_total_lpn_rsv_sec_qty, 'STAGE_LPNS');
5944                      DEBUG('l_rsv_qty: '|| l_rsv_qty, 'STAGE_LPNS');
5945                      DEBUG('l_rsv_sec_qty: '|| l_rsv_sec_qty, 'STAGE_LPNS');
5946                      DEBUG('l_delivery_detail_tab(i).requested_quantity: '||
5947                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5948                      DEBUG('l_delivery_detail_tab(i).requested_quantity2: '||
5949                             l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
5950                   END IF;
5951                   -- Bug : 4440809 : End
5952 
5953                   IF (l_rsv_qty <= 0) THEN
5954                      l_rsv_index  := j + 1;
5955                   ELSE
5956                      l_rsv_index  := j;
5957                   END IF;
5958 
5959                   IF (l_debug = 1) THEN
5960                      DEBUG('J : ' || j  , 'STAGE_LPNS');
5961                      DEBUG('l_rsv_index: ' || l_rsv_index , 'STAGE_LPNS');
5962                      DEBUG('l_rsv_qty: ' || l_rsv_qty, 'STAGE_LPNS');
5963                      DEBUG('l_rsv_sec_qty: ' || l_rsv_sec_qty, 'STAGE_LPNS');
5964                   END IF;
5965 
5966                   IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial in MSN
5967                      IF (l_debug = 1) THEN
5968                         DEBUG('The serial number control code is in 2,5', 'stage_lpns');
5969                      END IF;
5970 
5971                      IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
5972                         SELECT mtl_material_transactions_s.NEXTVAL
5973                           INTO l_delivery_detail_tab(i).transaction_temp_id
5974                           FROM DUAL;
5975 
5976                         -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
5977                         UPDATE wms_direct_ship_temp
5978                            SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
5979                          WHERE organization_id = l_delivery_detail_tab(i).organization_id
5980                            AND lpn_id = l_delivery_detail_tab(i).lpn_id
5981                            AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
5982                            AND order_header_id = l_delivery_detail_tab(i).order_header_id
5983                            AND order_line_id = l_delivery_detail_tab(i).order_line_id;
5984 
5985                         IF (l_debug = 1) THEN
5986                            DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value=' ||
5987                                    l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
5988                         END IF;
5989                      END IF;
5990 
5991                      -- Mark Serial Numbers
5992                      -- ACTION overship l_delivery_detail_tab(i).requested_quantity >> overship qty
5993                      explode_delivery_details(
5994                              x_return_status              => l_return_status
5995                            , x_msg_data                   => l_msg_data
5996                            , x_msg_count                  => l_msg_count
5997                              --Bug No 3390432
5998                            , x_transaction_temp_id        => l_out_transaction_temp_id
5999                            , p_organization_id            => p_organization_id
6000                            , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6001                            , p_serial_number_control_code => l_serial_number_control_code
6002                            , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6003                            , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
6004                            , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6005                            , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6006                            , p_last_action                => l_shipping_attr(1).action_flag);
6007 
6008                      IF l_return_status = fnd_api.g_ret_sts_error THEN
6009                         IF (l_debug = 1) THEN
6010                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
6011                         END IF;
6012 
6013                         RAISE fnd_api.g_exc_error;
6014                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6015                         IF (l_debug = 1) THEN
6016                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
6017                         END IF;
6018 
6019                         RAISE fnd_api.g_exc_unexpected_error;
6020                      END IF;
6021                   ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
6022                      IF (l_debug = 1) THEN
6023                         DEBUG('The serial number control code is 6', 'stage_lpns');
6024                      END IF;
6025 
6026                     /*******************************
6027                      * Adding a new out Parameter to Comply with
6028                      * New Signature of Explode_delivery_details
6029                      ********************************/
6030                      explode_delivery_details(
6031                             x_return_status              => l_return_status
6032                           , x_msg_data                   => l_msg_data
6033                           , x_msg_count                  => l_msg_count
6034                            -- Bug No 3390432
6035                           , x_transaction_temp_id        => l_out_transaction_temp_id
6036                           , p_organization_id            => p_organization_id
6037                           , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6038                           , p_serial_number_control_code => l_serial_number_control_code
6039                           , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6040                           , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
6041                           , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6042                           , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6043                           , p_last_action                => l_shipping_attr(1).action_flag);
6044 
6045                      IF l_return_status = fnd_api.g_ret_sts_error THEN
6046                         IF (l_debug = 1) THEN
6047                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
6048                         END IF;
6049 
6050                         RAISE fnd_api.g_exc_error;
6051                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6052                         IF (l_debug = 1) THEN
6053                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
6054                         END IF;
6055 
6056                         RAISE fnd_api.g_exc_unexpected_error;
6057                      END IF;
6058                   END IF;
6059 
6060                   l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
6061                   l_shipping_attr(1).released_status   := 'Y';
6062 
6063                  /*************************************
6064                   * Bug No 3390432
6065                   * Use the new Transaction Temp Id to update WDD
6066                   **************************************/
6067                   IF (l_serial_number_control_code = 6) THEN
6068                      l_invpcinrectype.transaction_temp_id         := l_out_transaction_temp_id;
6069                      IF (l_debug = 1) THEN
6070                         DEBUG('AFTER updating l_invpcinrectype,  l_invpcinrectype.transaction_temp_id = '
6071                                || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6072                      END IF;
6073                   END IF;
6074 
6075                   IF (l_debug = 1) THEN
6076                      DEBUG('l_shipping_attr(1).action_flag :=' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
6077                      DEBUG('l_shipping_attr(1).source_line_id :=' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
6078                      DEBUG('l_shipping_attr(1).transfer_lpn_id :=' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
6079                      DEBUG('l_shipping_attr(1).delivery_detail_id :=' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
6080                      DEBUG('l_shipping_attr(1).picked_quantity :=' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
6081                      DEBUG('l_shipping_attr(1).picked_quantity2 :=' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
6082                      DEBUG('l_shipping_attr(1).pending_quantity :=' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
6083                      DEBUG('l_shipping_attr(1).pending_quantity2 :=' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');
6084                      DEBUG('l_delivery_detail_tab(i).requested_quantity :=' ||
6085                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6086                      DEBUG('l_delivery_detail_tab(i).requested_quantity2 :=' ||
6087                             l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
6088                      DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
6089                      DEBUG('l_InvPCInRecType.transaction_temp_id :=' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6090                   END IF;
6091 
6092                   --Call shipping api to set transaction_temp_id global variable
6093                   IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
6094                      IF (l_shipping_attr(1).picked_quantity > 1) THEN
6095                         l_shipping_attr(1).serial_number  := NULL;
6096 
6097                         IF (l_debug = 1) THEN
6098                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6099                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6100                         END IF;
6101 
6102                         wsh_integration.set_inv_pc_attributes(
6103                             p_in_attributes              => l_invpcinrectype
6104                           , x_return_status              => l_return_status
6105                           , x_msg_count                  => l_msg_data
6106                           , x_msg_data                   => l_msg_count
6107                           );
6108 
6109                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6110                            IF (l_debug = 1) THEN
6111                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6112                            END IF;
6113 
6114                            RAISE fnd_api.g_exc_error;
6115                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6116                            IF (l_debug = 1) THEN
6117                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6118                            END IF;
6119 
6120                            RAISE fnd_api.g_exc_unexpected_error;
6121                         END IF;
6122                      ELSE
6123                         BEGIN
6124                            SELECT fm_serial_number
6125                              INTO l_shipping_attr(1).serial_number
6126                              FROM mtl_serial_numbers_temp
6127                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6128                               AND ROWNUM < 2;
6129 
6130                            IF (l_debug = 1) THEN
6131                               DEBUG('found fm_serial='
6132                                      || l_shipping_attr(1).serial_number
6133                                      || ' for transaction_temp_id='
6134                                      || l_invpcinrectype.transaction_temp_id
6135                                    , 'STAGE_LPN'
6136                                    );
6137                            END IF;
6138 
6139                            DELETE FROM mtl_serial_numbers_temp
6140                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6141                         EXCEPTION
6142                            WHEN NO_DATA_FOUND THEN
6143                               IF (l_debug = 1) THEN
6144                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6145                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6146                               END IF;
6147                         END;
6148                      END IF;
6149                   END IF;
6150 
6151                   IF l_dual_uom_item THEN
6152                     OPEN c_ds_ct_wt_gtemp_exists(l_shipping_attr(1).ship_from_org_id
6153                                                 ,l_shipping_attr(1).inventory_item_id
6154                                                 ,l_shipping_attr(1).transfer_lpn_id
6155                                                 ,l_shipping_attr(1).lot_number
6156                                                 ,l_shipping_attr(1).revision);
6157 
6158                     FETCH c_ds_ct_wt_gtemp_exists INTO l_dummy_num_var;
6159 
6160                     IF c_ds_ct_wt_gtemp_exists%FOUND THEN
6161                         l_ds_ct_wt_gtemp_exists := TRUE;
6162                     ELSE
6163                         l_ds_ct_wt_gtemp_exists := FALSE;
6164                     END IF;
6165                     CLOSE c_ds_ct_wt_gtemp_exists;
6166 
6167                     IF l_ds_ct_wt_gtemp_exists THEN
6168                         IF (l_debug = 1) THEN
6169                         DEBUG('Calling get_catch_weight as record exists in WMS_DS_CT_WT_GTEMP', 'stage_lpns');
6170                         END IF;
6171                         l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT(
6172                                            l_shipping_attr(1).ship_from_org_id
6173                                          , l_shipping_attr(1).transfer_lpn_id
6174                                          , l_shipping_attr(1).inventory_item_id
6175                                          , l_shipping_attr(1).revision
6176                                          , l_shipping_attr(1).lot_number
6177                                          , l_shipping_attr(1).picked_quantity);
6178                     ELSE
6179                         IF NVL(l_shipping_attr(1).picked_quantity2, fnd_api.g_miss_num) = fnd_api.g_miss_num THEN
6180                             IF (l_debug = 1) THEN
6181                             DEBUG('Dual UoM items but no entry in WMS_DS_CT_WT_GTEMP so use uom_convert to get picked_quantity2', 'stage_lpns');
6182                             END IF;
6183                             l_shipping_attr(1).picked_quantity2 := inv_convert.inv_um_convert (
6184                                                                         item_id         => l_shipping_attr(1).inventory_item_id,
6185                                                                         lot_number      => l_shipping_attr(1).lot_number,
6186                                                                         organization_id => l_shipping_attr(1).ship_from_org_id,
6187                                                                         precision       => NULL,
6188                                                                         from_quantity   => l_shipping_attr(1).picked_quantity,
6189                                                                         from_unit       => inv_cache.item_rec.primary_uom_code,
6190                                                                         to_unit         => inv_cache.item_rec.secondary_uom_code,
6191                                                                         from_name       => NULL,
6192                                                                         to_name         => NULL);
6193                         END IF;
6194                     END IF;
6195                   ELSE
6196                     IF (l_debug = 1) THEN
6197                     DEBUG('Non dual item so call get_catch_weight if pricing is secondary', 'stage_lpns');
6198                     END IF;
6199                     IF(l_delivery_detail_tab(i).ont_pricing_qty_source='S') THEN
6200                         l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT(
6201                                            l_shipping_attr(1).ship_from_org_id
6202                                          , l_shipping_attr(1).transfer_lpn_id
6203                                          , l_shipping_attr(1).inventory_item_id
6204                                          , l_shipping_attr(1).revision
6205                                          , l_shipping_attr(1).lot_number
6206                                          , l_shipping_attr(1).picked_quantity);
6207                     END IF;
6208                   END IF;
6209 
6210                   IF(l_debug = 1) THEN
6211                     DEBUG('Before call to update shipping attributes l_shipping_attr(1).picked_quantity2 :=' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
6212                   END IF;
6213                   wsh_interface.update_shipping_attributes(
6214                       p_source_code        => 'INV'
6215                     , p_changed_attributes => l_shipping_attr
6216                     , x_return_status      => l_return_status);
6217 
6218                   IF (l_debug = 1) THEN
6219                      DEBUG('after update shipping attributes', 'stage_lpns');
6220                   END IF;
6221 
6222                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6223                      IF (l_debug = 1) THEN
6224                         DEBUG('return error from update shipping attributes 3', 'STAGE_LPN');
6225                      END IF;
6226 
6227                      RAISE fnd_api.g_exc_error;
6228                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6229                      IF (l_debug = 1) THEN
6230                         DEBUG('return unexpected error from update shipping attributes', 'STAGE_LPN');
6231                      END IF;
6232 
6233                      RAISE fnd_api.g_exc_unexpected_error;
6234                   END IF;
6235 
6236                   l_delivery_detail_tab(i).requested_quantity := 0;
6237                   IF l_dual_uom_item THEN
6238                      l_delivery_detail_tab(i).requested_quantity2 := 0;
6239                   END IF;
6240 
6241                   --
6242                   IF l_delivery_detail_tab(i).requested_quantity = 0
6243                      AND i < l_last_del_index THEN
6244                      EXIT;
6245                   END IF;
6246                ----End of IF-1
6247                ----Start of IF-2
6248                --}
6249                ELSIF l_rsv_qty < l_delivery_detail_tab(i).requested_quantity THEN --{
6250                   IF (l_debug = 1) THEN
6251                      DEBUG('The l_rsv_qty < requested_quantity ', 'STAGE_LPNs');
6252                      DEBUG('rsv lpn_id:  ' || l_mtl_reservation_tbl(j).lpn_id, 'Stage_LPNs');
6253                      DEBUG('l_rsv_qty ' || l_rsv_qty , 'STAGE_LPNS');
6254                      DEBUG('l_rsv_sec_qty ' || l_rsv_sec_qty , 'STAGE_LPNS');
6255                      DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
6256                      DEBUG('l_total_lpn_rsv_sec_qty ' || l_total_lpn_rsv_sec_qty , 'STAGE_LPNS');
6257                      DEBUG('l_delivery_detail_tab(i).requested_quantity: '
6258                              || l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6259                      DEBUG('l_delivery_detail_tab(i).requested_quantity2: '
6260                              || l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
6261                   END IF;
6262 
6263                   -- Bug : 4440809 : Start
6264                   IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
6265                      l_shipping_attr(1).action_flag      := 'M';
6266                      l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
6267                      IF l_dual_uom_item THEN
6268                         l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
6269                      END IF;
6270                   ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
6271                      IF l_delivery_detail_tab(i).requested_quantity - l_rsv_qty > 0
6272                      THEN
6273                         l_shipping_attr(1).action_flag       := 'M';
6274                         l_shipping_attr(1).pending_quantity :=
6275                                l_delivery_detail_tab(i).requested_quantity - l_rsv_qty;
6276                         IF l_dual_uom_item THEN
6277                            l_shipping_attr(1).pending_quantity2 :=
6278                                  -- l_delivery_detail_tab(i).requested_quantity2 - l_rsv_sec_qty; Commented for bug 14576405
6279 								 --Added for bug 14576405 start
6280                                   l_delivery_detail_tab(i).requested_quantity2 - inv_convert.inv_um_convert (
6281 																			  item_id => l_mtl_reservation_tbl(j).inventory_item_id ,
6282 																			  precision => null,
6283 																			  from_quantity => l_mtl_reservation_tbl(j).primary_reservation_quantity,
6284 																			  from_unit => l_mtl_reservation_tbl(j).PRIMARY_UOM_CODE,
6285 																			  to_unit => l_mtl_reservation_tbl(j).SECONDARY_UOM_CODE,
6286 																			  from_name => null,
6287 																			  to_name => null);
6288 								 --Added for bug 14576405 end
6289                         END IF;
6290                      ELSE
6291                         l_shipping_attr(1).action_flag      := 'U';
6292                      END IF;
6293                   END IF;-- l_rsv_qty and l_total_lpn_rsv_qty  THEN
6294 
6295                   l_shipping_attr(1).picked_quantity := l_rsv_qty;
6296                   l_shipping_attr(1).released_status := 'Y';
6297                   l_total_lpn_rsv_qty                := l_total_lpn_rsv_qty - l_rsv_qty;
6298 
6299                   IF l_dual_uom_item THEN
6300                      l_shipping_attr(1).picked_quantity2 := l_rsv_sec_qty;
6301                      l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
6302                   END IF;
6303 
6304                   IF (l_debug = 1) THEN
6305                      DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
6306                      DEBUG('l_total_lpn_rsv_sec_qty ' || l_total_lpn_rsv_sec_qty , 'STAGE_LPNS');
6307                      DEBUG('i=' || i || ' j= ' || j, 'xxxx');
6308                      DEBUG(' l_shipping_attr(1).action_flag :' || l_shipping_attr(1).action_flag, 'STAGE_LPNs');
6309                   END IF;
6310 
6311                   IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial Control Code
6312                      IF (l_debug = 1) THEN
6313                         DEBUG('The serial number control code is in 2,5', 'stage_lpns');
6314                      END IF;
6315 
6316                      IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
6317                         SELECT mtl_material_transactions_s.NEXTVAL
6318                           INTO l_delivery_detail_tab(i).transaction_temp_id
6319                           FROM DUAL;
6320 
6321                         l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
6322 
6323                         -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
6324                         UPDATE wms_direct_ship_temp
6325                            SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
6326                          WHERE organization_id = l_delivery_detail_tab(i).organization_id
6327                            AND lpn_id = l_delivery_detail_tab(i).lpn_id
6328                            AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
6329                            AND order_header_id = l_delivery_detail_tab(i).order_header_id
6330                            AND order_line_id = l_delivery_detail_tab(i).order_line_id;
6331 
6332                         IF (l_debug = 1) THEN
6333                            DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value='
6334                                    || l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
6335                         END IF;
6336                      END IF;
6337 
6338                     /**************************************
6339                      * Adding new out parameter to comply with new signature
6340                      ***************************************/
6341                      -- Mark Serial Numbers
6342                      explode_delivery_details(
6343                        x_return_status              => l_return_status
6344                      , x_msg_data                   => l_msg_data
6345                      , x_msg_count                  => l_msg_count
6346                        --Bug No 3390432
6347                      , x_transaction_temp_id        => l_out_transaction_temp_id
6348                      , p_organization_id            => p_organization_id
6349                      , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6350                      , p_serial_number_control_code => l_serial_number_control_code
6351                      , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6352                      , p_quantity                   => l_rsv_qty
6353                      , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6354                      , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6355                      , p_last_action                => l_shipping_attr(1).action_flag
6356                      );
6357 
6358                      IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6359                         IF (l_debug = 1) THEN
6360                            DEBUG('return error from update shipping attributes 3', 'stage_lpns');
6361                         END IF;
6362 
6363                         RAISE fnd_api.g_exc_error;
6364                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6365                         IF (l_debug = 1) THEN
6366                            DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
6367                         END IF;
6368 
6369                         RAISE fnd_api.g_exc_unexpected_error;
6370                      END IF;
6371                   ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
6372                      IF (l_debug = 1) THEN
6373                         DEBUG('The serial number control code is 6', 'stage_lpns');
6374                      END IF;
6375 
6376                     /*****************************************
6377                      * Retreving the new Transaction temp id to use in Splitting
6378                      * existing WDD line
6379                      ******************************************/
6380                      explode_delivery_details(
6381                        x_return_status              => l_return_status
6382                      , x_msg_data                   => l_msg_data
6383                      , x_msg_count                  => l_msg_count
6384                       --Bug No 3390432
6385                      , x_transaction_temp_id        => l_out_transaction_temp_id
6386                      , p_organization_id            => p_organization_id
6387                      , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6388                      , p_serial_number_control_code => l_serial_number_control_code
6389                      , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6390                      , p_quantity                   => l_rsv_qty
6391                      , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6392                      , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6393                      , p_last_action                => l_shipping_attr(1).action_flag
6394                      );
6395 
6396                      IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6397                         IF (l_debug = 1) THEN
6398                            DEBUG('return error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
6399                         END IF;
6400 
6401                         RAISE fnd_api.g_exc_error;
6402                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6403                         IF (l_debug = 1) THEN
6404                            DEBUG('return unexpected error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
6405                         END IF;
6406 
6407                         RAISE fnd_api.g_exc_unexpected_error;
6408                      END IF;
6409                   END IF; -- Serial Control Code
6410 
6411                  /*******************************************
6412                   * Bug No 3390432
6413                   * Update WDD with the  newly created Transaction_temp_id
6414                   ********************************************/
6415                   l_invpcinrectype.transaction_temp_id := l_out_transaction_temp_id;
6416                   l_shipping_attr(1).released_status   := 'Y';
6417 
6418                   IF (l_debug = 1) THEN
6419                      DEBUG('l_shipping_attr(1).action_flag        =' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
6420                      DEBUG('l_shipping_attr(1).source_line_id     =' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
6421                      DEBUG('l_shipping_attr(1).transfer_lpn_id    =' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
6422                      DEBUG('l_shipping_attr(1).delivery_detail_id =' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
6423                      DEBUG('l_shipping_attr(1).picked_quantity    =' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
6424 					 DEBUG('l_shipping_attr(1).picked_quantity2    =' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');--bug13582056
6425                      DEBUG('l_shipping_attr(1).pending_quantity   =' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
6426 					 DEBUG('l_shipping_attr(1).pending_quantity2   =' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');--bug13582056
6427                      DEBUG('l_delivery_detail_tab(i).requested_quantity =' ||
6428                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6429 					 DEBUG('l_delivery_detail_tab(i).requested_quantity2 =' ||
6430 							l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');--bug13582056
6431                      DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
6432                      DEBUG('l_InvPCInRecType.transaction_temp_id  =' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6433                   END IF;
6434 
6435                   -- Call shipping api to set transaction_temp_id global variable
6436                   IF ( l_invpcinrectype.transaction_temp_id IS NOT NULL ) THEN
6437                      IF ( l_shipping_attr(1).picked_quantity > 1 ) THEN
6438                         l_shipping_attr(1).serial_number  := NULL;
6439 
6440                         IF ( l_debug = 1 ) THEN
6441                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6442                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6443                         END IF;
6444 
6445                         WSH_INTEGRATION.Set_INV_PC_Attributes(
6446                           p_in_attributes => l_invpcinrectype
6447                         , x_return_status => l_return_status
6448                         , x_msg_count     => l_msg_data
6449                         , x_msg_data      => l_msg_count );
6450 
6451                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6452                            IF (l_debug = 1) THEN
6453                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6454                            END IF;
6455 
6456                            RAISE fnd_api.g_exc_error;
6457                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6458                            IF (l_debug = 1) THEN
6459                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6460                            END IF;
6461 
6462                            RAISE fnd_api.g_exc_unexpected_error;
6463                         END IF;
6464                      ELSE
6465                         BEGIN
6466                            SELECT fm_serial_number
6467                              INTO l_shipping_attr(1).serial_number
6468                              FROM mtl_serial_numbers_temp
6469                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6470                               AND ROWNUM < 2;
6471 
6472                            IF ( l_debug = 1 ) THEN
6473                               DEBUG('found fm_serial='|| l_shipping_attr(1).serial_number||
6474                                     ' for transaction_temp_id='||l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6475                            END IF;
6476 
6477                            DELETE FROM mtl_serial_numbers_temp
6478                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6479                         EXCEPTION
6480                            WHEN NO_DATA_FOUND THEN
6481                               IF (l_debug = 1) THEN
6482                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6483                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6484                               END IF;
6485                         END;
6486                      END IF;
6487                   END IF; -- for serial controlled items
6488 
6489                   --patchset J.  Shipping API cleanup
6490                   l_delivery_detail_tab(i).requested_quantity := l_delivery_detail_tab(i).requested_quantity - l_rsv_qty;
6491 
6492                   IF l_dual_uom_item THEN
6493                      l_delivery_detail_tab(i).requested_quantity2 := l_delivery_detail_tab(i).requested_quantity2 - l_rsv_sec_qty;
6494                   END IF;
6495 
6496                   --Call shipping api to set transaction_temp_id global variable
6497                   IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
6498                      IF (l_shipping_attr(1).picked_quantity > 1) THEN
6499                         l_shipping_attr(1).serial_number  := NULL;
6500 
6501                         IF (l_debug = 1) THEN
6502                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6503                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6504                         END IF;
6505 
6506                         wsh_integration.set_inv_pc_attributes(
6507                           p_in_attributes              => l_invpcinrectype
6508                         , x_return_status              => l_return_status
6509                         , x_msg_count                  => l_msg_data
6510                         , x_msg_data                   => l_msg_count
6511                         );
6512 
6513                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6514                            IF (l_debug = 1) THEN
6515                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6516                            END IF;
6517 
6518                            RAISE fnd_api.g_exc_error;
6519                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6520                            IF (l_debug = 1) THEN
6521                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6522                            END IF;
6523 
6524                            RAISE fnd_api.g_exc_unexpected_error;
6525                         END IF;
6526                      ELSE
6527                         BEGIN
6528                            SELECT fm_serial_number
6529                              INTO l_shipping_attr(1).serial_number
6530                              FROM mtl_serial_numbers_temp
6531                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6532                               AND ROWNUM < 2;
6533 
6534                            IF (l_debug = 1) THEN
6535                               DEBUG('found fm_serial='
6536                                      || l_shipping_attr(1).serial_number
6537                                      || ' for transaction_temp_id='
6538                                      || l_invpcinrectype.transaction_temp_id
6539                                    , 'STAGE_LPN'
6540                                    );
6541                            END IF;
6542 
6543                            DELETE FROM mtl_serial_numbers_temp
6544                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6545                         EXCEPTION
6546                            WHEN NO_DATA_FOUND THEN
6547                               IF (l_debug = 1) THEN
6548                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6549                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6550                               END IF;
6551                         END;
6552                      END IF;
6553                   END IF;
6554 
6555                   IF l_dual_uom_item THEN
6556                     OPEN c_ds_ct_wt_gtemp_exists(l_shipping_attr(1).ship_from_org_id
6557                                                 ,l_shipping_attr(1).inventory_item_id
6558                                                 ,l_shipping_attr(1).transfer_lpn_id
6559                                                 ,l_shipping_attr(1).lot_number
6560                                                 ,l_shipping_attr(1).revision);
6561 
6562                     FETCH c_ds_ct_wt_gtemp_exists INTO l_dummy_num_var;
6563 
6564                     IF c_ds_ct_wt_gtemp_exists%FOUND THEN
6565                         l_ds_ct_wt_gtemp_exists := TRUE;
6566                     ELSE
6567                         l_ds_ct_wt_gtemp_exists := FALSE;
6568                     END IF;
6569                     CLOSE c_ds_ct_wt_gtemp_exists;
6570 
6571                     IF l_ds_ct_wt_gtemp_exists THEN
6572                         IF (l_debug = 1) THEN
6573                         DEBUG('Calling get_catch_weight as record exists in WMS_DS_CT_WT_GTEMP', 'stage_lpns');
6574                         END IF;
6575                         l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT(
6576                                            l_shipping_attr(1).ship_from_org_id
6577                                          , l_shipping_attr(1).transfer_lpn_id
6578                                          , l_shipping_attr(1).inventory_item_id
6579                                          , l_shipping_attr(1).revision
6580                                          , l_shipping_attr(1).lot_number
6581                                          , l_shipping_attr(1).picked_quantity);
6582                     ELSE
6583                         IF NVL(l_shipping_attr(1).picked_quantity2, fnd_api.g_miss_num) = fnd_api.g_miss_num THEN
6584                             IF (l_debug = 1) THEN
6585                             DEBUG('Dual UoM items but no entry in WMS_DS_CT_WT_GTEMP so use uom_convert to get picked_quantity2', 'stage_lpns');
6586                             END IF;
6587                             l_shipping_attr(1).picked_quantity2 := inv_convert.inv_um_convert (
6588                                                                         item_id         => l_shipping_attr(1).inventory_item_id,
6589                                                                         lot_number      => l_shipping_attr(1).lot_number,
6590                                                                         organization_id => l_shipping_attr(1).ship_from_org_id,
6591                                                                         precision       => NULL,
6592                                                                         from_quantity   => l_shipping_attr(1).picked_quantity,
6593                                                                         from_unit       => inv_cache.item_rec.primary_uom_code,
6594                                                                         to_unit         => inv_cache.item_rec.secondary_uom_code,
6595                                                                         from_name       => NULL,
6596                                                                         to_name         => NULL);
6597                         END IF;
6598                     END IF;
6599                   ELSE
6600                     IF (l_debug = 1) THEN
6601                     DEBUG('Non dual item so call get_catch_weight if pricing is secondary', 'stage_lpns');
6602                     END IF;
6603                     IF(l_delivery_detail_tab(i).ont_pricing_qty_source='S') THEN
6604                         l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT(
6605                                            l_shipping_attr(1).ship_from_org_id
6606                                          , l_shipping_attr(1).transfer_lpn_id
6607                                          , l_shipping_attr(1).inventory_item_id
6608                                          , l_shipping_attr(1).revision
6609                                          , l_shipping_attr(1).lot_number
6610                                          , l_shipping_attr(1).picked_quantity);
6611                     END IF;
6612                   END IF;
6613 
6614                   IF (l_debug = 1) THEN
6615                      DEBUG('Call  update shipping attributes', 'stage_lpns');
6616                   END IF;
6617                   wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
6618                   , x_return_status              => l_return_status);
6619 
6620                   IF (l_debug = 1) THEN
6621                      DEBUG('after update shipping attributes', 'stage_lpns');
6622                   END IF;
6623 
6624                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6625                      IF (l_debug = 1) THEN
6626                         DEBUG('return error from update shipping attributes 2', 'stage_lpns');
6627                     END IF;
6628 
6629                     RAISE fnd_api.g_exc_error;
6630                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6631                      IF (l_debug = 1) THEN
6632                         DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
6633                      END IF;
6634 
6635                      RAISE fnd_api.g_exc_unexpected_error;
6636                   END IF;
6637 
6638                   --Since we have split the WDD line, we need to process
6639                   --this newly split line also.
6640                   --Setting these variables will process the newly split line
6641                  /* 4440809: l_delivery_detail_tab(i).delivery_detail_id := l_new_delivery_detail_id; */
6642                  /*****************************************
6643                   * Bug No 3390432
6644                   * Only changing the Transaction_temp_id of WDD to Null value when
6645                   * serial control code is 2 or 5
6646                   ******************************************/
6647                   IF (l_serial_number_control_code IN (2,5)) THEN -- Serial NOT in MSNT
6648                      l_delivery_detail_tab(i).transaction_temp_id := NULL;
6649                   END IF;
6650                   l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
6651 
6652                   l_rsv_index := j + 1;
6653                   l_rsv_qty   := 0;
6654 
6655                   IF l_dual_uom_item THEN
6656                      l_rsv_sec_qty := 0;
6657                   END IF;
6658                END IF; --} Res. Qty Match l_rsv_qty < l_delivery_detail_tab(i).requested_quantity
6659                -- IF-2
6660             --}
6661             ELSIF g_fulfillment_base = 'S' THEN --{
6662 
6663                IF l_rsv_sec_qty >= l_delivery_detail_tab(i).requested_quantity2 THEN --{
6664                   IF (l_debug = 1) THEN
6665                      DEBUG('Sec reservation quantity >= delivery detail quantity2', 'STAGE_LPNS');
6666                   END IF;
6667                   -- Bug : 4440809 : Start
6668 
6669                   IF l_rsv_sec_qty = l_delivery_detail_tab(i).requested_quantity2 THEN
6670                      IF l_rsv_sec_qty < l_total_lpn_rsv_sec_qty  THEN
6671                         IF (i >= l_last_del_index) THEN
6672                            l_shipping_attr(1).action_flag       := 'M';
6673                            l_shipping_attr(1).pending_quantity  := (l_total_lpn_rsv_qty - l_rsv_qty);
6674                            l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
6675                         ELSE
6676                            l_shipping_attr(1).action_flag := 'U';
6677                         END IF;
6678                      ELSIF l_rsv_sec_qty = l_total_lpn_rsv_sec_qty  THEN
6679                         l_shipping_attr(1).action_flag := 'U';
6680                      ELSIF l_rsv_sec_qty > l_total_lpn_rsv_sec_qty  THEN
6681                         IF (l_debug = 1) THEN
6682                            DEBUG('l_rsv_sec_qty > l_total_lpn_rsv_sec_qty : ' , 'STAGE_LPNS');
6683                         END IF;
6684                         NULL; -- why shld this condition happen
6685                      END IF;
6686                   END IF;
6687 
6688                   l_shipping_attr(1).picked_quantity  := l_rsv_qty;
6689                   l_shipping_attr(1).picked_quantity2 := l_rsv_sec_qty;
6690                   l_shipping_attr(1).released_status  := 'Y';
6691 
6692                   IF l_rsv_sec_qty > l_delivery_detail_tab(i).requested_quantity2 THEN
6693                      IF (i >= l_last_del_index) THEN
6694                         IF l_rsv_sec_qty < l_total_lpn_rsv_sec_qty  THEN
6695                            l_shipping_attr(1).action_flag       := 'M';
6696                            l_shipping_attr(1).pending_quantity  := (l_total_lpn_rsv_qty - l_rsv_qty);
6697                            l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
6698                         ELSIF l_rsv_sec_qty = l_total_lpn_rsv_sec_qty  THEN
6699                            l_shipping_attr(1).action_flag      := 'U';
6700                         ELSIF l_rsv_sec_qty > l_total_lpn_rsv_sec_qty  THEN
6701                            IF (l_debug = 1) THEN
6702                               DEBUG('l_rsv_sec_qty > l_total_lpn_rsv_sec_qty : ' , 'STAGE_LPNS');
6703                            END IF;
6704                            NULL; -- why shld this condition happen
6705                         END IF;
6706                         l_delivery_detail_tab(i).requested_quantity  := l_rsv_qty; -- **MR 01/04/06
6707                         l_delivery_detail_tab(i).requested_quantity2 := l_rsv_sec_qty;
6708                         l_total_lpn_rsv_qty := l_total_lpn_rsv_qty - l_rsv_qty;
6709                         l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
6710                         l_rsv_qty := 0;
6711                         l_rsv_sec_qty := 0;
6712                      ELSE
6713                         l_total_lpn_rsv_qty := l_total_lpn_rsv_qty
6714                                                - l_delivery_detail_tab(i).requested_quantity;
6715                         l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty
6716                                                    - l_delivery_detail_tab(i).requested_quantity2;
6717                         l_shipping_attr(1).picked_quantity := l_delivery_detail_tab(i).requested_quantity;
6718                         l_shipping_attr(1).picked_quantity2 := l_delivery_detail_tab(i).requested_quantity2;
6719                         l_rsv_qty := l_rsv_qty - l_delivery_detail_tab(i).requested_quantity;
6720                         l_rsv_sec_qty := l_rsv_sec_qty - l_delivery_detail_tab(i).requested_quantity2;
6721                         l_shipping_attr(1).action_flag := 'U';
6722                         l_delivery_detail_tab(i).requested_quantity := l_rsv_qty; -- **MR 01/04/06
6723                         l_delivery_detail_tab(i).requested_quantity2 := l_rsv_sec_qty;
6724                      END IF; -- (i >= l_last_del_index) THEN
6725                   ELSIF l_rsv_sec_qty = l_delivery_detail_tab(i).requested_quantity2 THEN
6726                      l_total_lpn_rsv_qty := l_total_lpn_rsv_qty - l_rsv_qty;
6727                      l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
6728                      l_rsv_qty := 0;
6729                      l_rsv_sec_qty := 0;
6730                   END IF;
6731 
6732                   IF (l_debug = 1) THEN
6733                      DEBUG('l_shipping_attr(1).action_flag: ' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
6734                      DEBUG('l_shipping_attr(1).pending_quantity: ' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
6735                      DEBUG('l_shipping_attr(1).pending_quantity2: ' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');
6736                      DEBUG('l_shipping_attr(1).picked_quantity: ' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
6737                      DEBUG('l_shipping_attr(1).picked_quantity2: ' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
6738                      DEBUG('l_total_lpn_rsv_qty: '|| l_total_lpn_rsv_qty, 'STAGE_LPNS');
6739                      DEBUG('l_total_lpn_rsv_sec_qty: '|| l_total_lpn_rsv_sec_qty, 'STAGE_LPNS');
6740                      DEBUG('l_rsv_qty: '|| l_rsv_qty, 'STAGE_LPNS');
6741                      DEBUG('l_rsv_sec_qty: '|| l_rsv_sec_qty, 'STAGE_LPNS');
6742                      DEBUG('l_delivery_detail_tab(i).requested_quantity: '||
6743                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6744                      DEBUG('l_delivery_detail_tab(i).requested_quantity2: '||
6745                             l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
6746                      DEBUG('l_shipping_attr(1).source_line_id :=' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
6747                      DEBUG('l_shipping_attr(1).transfer_lpn_id :=' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
6748                      DEBUG('l_shipping_attr(1).delivery_detail_id :=' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
6749                      DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
6750                   END IF;
6751                   -- Bug : 4440809 : End
6752 
6753                   IF (l_rsv_sec_qty <= 0) THEN
6754                      l_rsv_index := j + 1;
6755                   ELSE
6756                      l_rsv_index := j;
6757                   END IF;
6758 
6759                   IF (l_debug = 1) THEN
6760                      DEBUG('J : ' || j  , 'STAGE_LPNS');
6761                      DEBUG('l_rsv_index: ' || l_rsv_index , 'STAGE_LPNS');
6762                      DEBUG('l_rsv_qty: ' || l_rsv_qty, 'STAGE_LPNS');
6763                      DEBUG('l_rsv_sec_qty: ' || l_rsv_sec_qty, 'STAGE_LPNS');
6764                   END IF;
6765 
6766                   IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial in MSN
6767                      IF (l_debug = 1) THEN
6768                         DEBUG('The serial number control code is in 2,5', 'stage_lpns');
6769                      END IF;
6770 
6771                      IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
6772                         SELECT mtl_material_transactions_s.NEXTVAL
6773                           INTO l_delivery_detail_tab(i).transaction_temp_id
6774                           FROM DUAL;
6775 
6776                         -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
6777                         UPDATE wms_direct_ship_temp
6778                            SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
6779                          WHERE organization_id = l_delivery_detail_tab(i).organization_id
6780                            AND lpn_id = l_delivery_detail_tab(i).lpn_id
6781                            AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
6782                            AND order_header_id = l_delivery_detail_tab(i).order_header_id
6783                            AND order_line_id = l_delivery_detail_tab(i).order_line_id;
6784 
6785                         IF (l_debug = 1) THEN
6786                            DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value=' ||
6787                                    l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
6788                         END IF;
6789                      END IF;
6790 
6791                      -- Mark Serial Numbers
6792                      -- ACTION overship l_delivery_detail_tab(i).requested_quantity >> overship qty
6793                      explode_delivery_details(
6794                              x_return_status              => l_return_status
6795                            , x_msg_data                   => l_msg_data
6796                            , x_msg_count                  => l_msg_count
6797                              --Bug No 3390432
6798                            , x_transaction_temp_id        => l_out_transaction_temp_id
6799                            , p_organization_id            => p_organization_id
6800                            , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6801                            , p_serial_number_control_code => l_serial_number_control_code
6802                            , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6803                            , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
6804                            , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6805                            , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6806                            , p_last_action                => l_shipping_attr(1).action_flag);
6807 
6808                      IF l_return_status = fnd_api.g_ret_sts_error THEN
6809                         IF (l_debug = 1) THEN
6810                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
6811                         END IF;
6812 
6813                         RAISE fnd_api.g_exc_error;
6814                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6815                         IF (l_debug = 1) THEN
6816                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
6817                         END IF;
6818 
6819                         RAISE fnd_api.g_exc_unexpected_error;
6820                      END IF;
6821                   ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
6822                      IF (l_debug = 1) THEN
6823                         DEBUG('The serial number control code is 6', 'stage_lpns');
6824                      END IF;
6825 
6826                     /*******************************
6827                      * Adding a new out Parameter to Comply with
6828                      * New Signature of Explode_delivery_details
6829                      ********************************/
6830                      explode_delivery_details(
6831                             x_return_status              => l_return_status
6832                           , x_msg_data                   => l_msg_data
6833                           , x_msg_count                  => l_msg_count
6834                            -- Bug No 3390432
6835                           , x_transaction_temp_id        => l_out_transaction_temp_id
6836                           , p_organization_id            => p_organization_id
6837                           , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
6838                           , p_serial_number_control_code => l_serial_number_control_code
6839                           , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
6840                           , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
6841                           , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
6842                           , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
6843                           , p_last_action                => l_shipping_attr(1).action_flag);
6844 
6845                      IF l_return_status = fnd_api.g_ret_sts_error THEN
6846                         IF (l_debug = 1) THEN
6847                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
6848                         END IF;
6849 
6850                         RAISE fnd_api.g_exc_error;
6851                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6852                         IF (l_debug = 1) THEN
6853                            DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
6854                         END IF;
6855 
6856                         RAISE fnd_api.g_exc_unexpected_error;
6857                      END IF;
6858                   END IF;
6859 
6860                   l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
6861                   l_shipping_attr(1).released_status   := 'Y';
6862 
6863                  /*************************************
6864                   * Bug No 3390432
6865                   * Use the new Transaction Temp Id to update WDD
6866                   **************************************/
6867                   IF (l_serial_number_control_code = 6) THEN
6868                      l_invpcinrectype.transaction_temp_id         := l_out_transaction_temp_id;
6869                      IF (l_debug = 1) THEN
6870                         DEBUG('AFTER updating l_invpcinrectype,  l_invpcinrectype.transaction_temp_id = '
6871                                || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6872                      END IF;
6873                   END IF;
6874 
6875                   IF (l_debug = 1) THEN
6876                      DEBUG('l_shipping_attr(1).action_flag :=' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
6877                      DEBUG('l_shipping_attr(1).source_line_id :=' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
6878                      DEBUG('l_shipping_attr(1).transfer_lpn_id :=' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
6879                      DEBUG('l_shipping_attr(1).delivery_detail_id :=' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
6880                      DEBUG('l_shipping_attr(1).picked_quantity :=' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
6881                      DEBUG('l_shipping_attr(1).picked_quantity2 :=' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
6882                      DEBUG('l_shipping_attr(1).pending_quantity :=' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
6883                      DEBUG('l_shipping_attr(1).pending_quantity2 :=' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');
6884                      DEBUG('l_delivery_detail_tab(i).requested_quantity :=' ||
6885                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6886                      DEBUG('l_delivery_detail_tab(i).requested_quantity2 :=' ||
6887                             l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
6888                      DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
6889                      DEBUG('l_InvPCInRecType.transaction_temp_id :=' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6890                   END IF;
6891 
6892                   --Call shipping api to set transaction_temp_id global variable
6893                   IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
6894                      IF (l_shipping_attr(1).picked_quantity > 1) THEN
6895                         l_shipping_attr(1).serial_number  := NULL;
6896 
6897                         IF (l_debug = 1) THEN
6898                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6899                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6900                         END IF;
6901 
6902                         wsh_integration.set_inv_pc_attributes(
6903                             p_in_attributes              => l_invpcinrectype
6904                           , x_return_status              => l_return_status
6905                           , x_msg_count                  => l_msg_data
6906                           , x_msg_data                   => l_msg_count
6907                           );
6908 
6909                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6910                            IF (l_debug = 1) THEN
6911                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6912                            END IF;
6913 
6914                            RAISE fnd_api.g_exc_error;
6915                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6916                            IF (l_debug = 1) THEN
6917                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6918                            END IF;
6919 
6920                            RAISE fnd_api.g_exc_unexpected_error;
6921                         END IF;
6922                      ELSE
6923                         BEGIN
6924                            SELECT fm_serial_number
6925                              INTO l_shipping_attr(1).serial_number
6926                              FROM mtl_serial_numbers_temp
6927                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6928                               AND ROWNUM < 2;
6929 
6930                            IF (l_debug = 1) THEN
6931                               DEBUG('found fm_serial='
6932                                      || l_shipping_attr(1).serial_number
6933                                      || ' for transaction_temp_id='
6934                                      || l_invpcinrectype.transaction_temp_id
6935                                    , 'STAGE_LPN'
6936                                    );
6937                            END IF;
6938 
6939                            DELETE FROM mtl_serial_numbers_temp
6940                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6941                         EXCEPTION
6942                            WHEN NO_DATA_FOUND THEN
6943                               IF (l_debug = 1) THEN
6944                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6945                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6946                               END IF;
6947                         END;
6948                      END IF;
6949                   END IF;
6950 
6951                   wsh_interface.update_shipping_attributes(
6952                       p_source_code        => 'INV'
6953                     , p_changed_attributes => l_shipping_attr
6954                     , x_return_status      => l_return_status);
6955 
6956                   IF (l_debug = 1) THEN
6957                      DEBUG('after update shipping attributes', 'stage_lpns');
6958                   END IF;
6959 
6960                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6961                      IF (l_debug = 1) THEN
6962                         DEBUG('return error from update shipping attributes 3', 'STAGE_LPN');
6963                      END IF;
6964 
6965                      RAISE fnd_api.g_exc_error;
6966                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6967                      IF (l_debug = 1) THEN
6968                         DEBUG('return unexpected error from update shipping attributes', 'STAGE_LPN');
6969                      END IF;
6970 
6971                      RAISE fnd_api.g_exc_unexpected_error;
6972                   END IF;
6973 
6974                   l_delivery_detail_tab(i).requested_quantity := 0;
6975                   l_delivery_detail_tab(i).requested_quantity2 := 0;
6976 
6977                   --
6978                   IF l_delivery_detail_tab(i).requested_quantity2 = 0
6979                      AND i < l_last_del_index THEN
6980                      EXIT;
6981                   END IF;
6982                ----End of IF-1
6983                ----Start of IF-2
6984                --}
6985                ELSIF l_rsv_sec_qty < l_delivery_detail_tab(i).requested_quantity2 THEN --{
6986                   IF (l_debug = 1) THEN
6987                      DEBUG('The l_rsv_sec_qty < requested_quantity2 ', 'STAGE_LPNs');
6988                      DEBUG('rsv lpn_id:  ' || l_mtl_reservation_tbl(j).lpn_id, 'Stage_LPNs');
6989                      DEBUG('l_rsv_qty ' || l_rsv_qty , 'STAGE_LPNS');
6990                      DEBUG('l_rsv_sec_qty ' || l_rsv_sec_qty , 'STAGE_LPNS');
6991                      DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
6992                      DEBUG('l_total_lpn_rsv_sec_qty ' || l_total_lpn_rsv_sec_qty , 'STAGE_LPNS');
6993                      DEBUG('l_delivery_detail_tab(i).requested_quantity: '
6994                              || l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6995                      DEBUG('l_delivery_detail_tab(i).requested_quantity2: '
6996                              || l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
6997                   END IF;
6998 
6999                   IF l_rsv_sec_qty < l_total_lpn_rsv_sec_qty THEN
7000                      l_shipping_attr(1).action_flag      := 'M';
7001                      l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
7002                      l_shipping_attr(1).pending_quantity2 := (l_total_lpn_rsv_sec_qty - l_rsv_sec_qty);
7003                   ELSIF l_rsv_sec_qty = l_total_lpn_rsv_sec_qty  THEN
7004                      IF l_delivery_detail_tab(i).requested_quantity2 - l_rsv_sec_qty > 0
7005                      THEN
7006                         l_shipping_attr(1).action_flag := 'M';
7007                         l_shipping_attr(1).pending_quantity :=
7008                                l_delivery_detail_tab(i).requested_quantity - inv_convert.inv_um_convert (
7009 																			  item_id => l_mtl_reservation_tbl(j).inventory_item_id ,
7010 																			  precision => null,
7011 																			  from_quantity => l_mtl_reservation_tbl(j).secondary_reservation_quantity,
7012 																			  from_unit => l_mtl_reservation_tbl(j).SECONDARY_UOM_CODE,
7013 																			  to_unit => l_mtl_reservation_tbl(j).PRIMARY_UOM_CODE,
7014 																			  from_name => null,
7015 																			  to_name => null);
7016                         l_shipping_attr(1).pending_quantity2 :=
7017                                l_delivery_detail_tab(i).requested_quantity2 - l_rsv_sec_qty;
7018                      ELSE
7019                         l_shipping_attr(1).action_flag := 'U';
7020                      END IF;
7021                   END IF;-- l_rsv_qty and l_total_lpn_rsv_qty  THEN
7022 
7023                   l_shipping_attr(1).picked_quantity := l_rsv_qty;
7024                   l_shipping_attr(1).picked_quantity2 := l_rsv_sec_qty;
7025                   l_shipping_attr(1).released_status := 'Y';
7026                   l_total_lpn_rsv_qty := l_total_lpn_rsv_qty - l_rsv_qty;
7027                   l_total_lpn_rsv_sec_qty := l_total_lpn_rsv_sec_qty - l_rsv_sec_qty;
7028 
7029                   IF (l_debug = 1) THEN
7030                      DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
7031                      DEBUG('l_total_lpn_rsv_sec_qty ' || l_total_lpn_rsv_sec_qty , 'STAGE_LPNS');
7032                      DEBUG('i=' || i || ' j= ' || j, 'xxxx');
7033                      DEBUG(' l_shipping_attr(1).action_flag :' || l_shipping_attr(1).action_flag, 'STAGE_LPNs');
7034                   END IF;
7035 
7036                   IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial Control Code
7037                      IF (l_debug = 1) THEN
7038                         DEBUG('The serial number control code is in 2,5', 'stage_lpns');
7039                      END IF;
7040 
7041                      IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
7042                         SELECT mtl_material_transactions_s.NEXTVAL
7043                           INTO l_delivery_detail_tab(i).transaction_temp_id
7044                           FROM DUAL;
7045 
7046                         l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
7047 
7048                         -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
7049                         UPDATE wms_direct_ship_temp
7050                            SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
7051                          WHERE organization_id = l_delivery_detail_tab(i).organization_id
7052                            AND lpn_id = l_delivery_detail_tab(i).lpn_id
7053                            AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
7054                            AND order_header_id = l_delivery_detail_tab(i).order_header_id
7055                            AND order_line_id = l_delivery_detail_tab(i).order_line_id;
7056 
7057                         IF (l_debug = 1) THEN
7058                            DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value='
7059                                    || l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
7060                         END IF;
7061                      END IF;
7062 
7063                     /**************************************
7064                      * Adding new out parameter to comply with new signature
7065                      ***************************************/
7066                      -- Mark Serial Numbers
7067                      explode_delivery_details(
7068                        x_return_status              => l_return_status
7069                      , x_msg_data                   => l_msg_data
7070                      , x_msg_count                  => l_msg_count
7071                        --Bug No 3390432
7072                      , x_transaction_temp_id        => l_out_transaction_temp_id
7073                      , p_organization_id            => p_organization_id
7074                      , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
7075                      , p_serial_number_control_code => l_serial_number_control_code
7076                      , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
7077                      , p_quantity                   => l_rsv_qty
7078                      , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
7079                      , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
7080                      , p_last_action                => l_shipping_attr(1).action_flag
7081                      );
7082 
7083                      IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7084                         IF (l_debug = 1) THEN
7085                            DEBUG('return error from update shipping attributes 3', 'stage_lpns');
7086                         END IF;
7087 
7088                         RAISE fnd_api.g_exc_error;
7089                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7090                         IF (l_debug = 1) THEN
7091                            DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
7092                         END IF;
7093 
7094                         RAISE fnd_api.g_exc_unexpected_error;
7095                      END IF;
7096                   ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
7097                      IF (l_debug = 1) THEN
7098                         DEBUG('The serial number control code is 6', 'stage_lpns');
7099                      END IF;
7100 
7101                     /*****************************************
7102                      * Retreving the new Transaction temp id to use in Splitting
7103                      * existing WDD line
7104                      ******************************************/
7105                      explode_delivery_details(
7106                        x_return_status              => l_return_status
7107                      , x_msg_data                   => l_msg_data
7108                      , x_msg_count                  => l_msg_count
7109                       --Bug No 3390432
7110                      , x_transaction_temp_id        => l_out_transaction_temp_id
7111                      , p_organization_id            => p_organization_id
7112                      , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
7113                      , p_serial_number_control_code => l_serial_number_control_code
7114                      , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
7115                      , p_quantity                   => l_rsv_qty
7116                      , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
7117                      , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
7118                      , p_last_action                => l_shipping_attr(1).action_flag
7119                      );
7120 
7121                      IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7122                         IF (l_debug = 1) THEN
7123                            DEBUG('return error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
7124                         END IF;
7125 
7126                         RAISE fnd_api.g_exc_error;
7127                      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7128                         IF (l_debug = 1) THEN
7129                            DEBUG('return unexpected error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
7130                         END IF;
7131 
7132                         RAISE fnd_api.g_exc_unexpected_error;
7133                      END IF;
7134                   END IF; -- Serial Control Code
7135 
7136                  /*******************************************
7137                   * Bug No 3390432
7138                   * Update WDD with the  newly created Transaction_temp_id
7139                   ********************************************/
7140                   l_invpcinrectype.transaction_temp_id := l_out_transaction_temp_id;
7141                   l_shipping_attr(1).released_status   := 'Y';
7142 
7143                   IF (l_debug = 1) THEN
7144                      DEBUG('l_shipping_attr(1).action_flag =' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
7145                      DEBUG('l_shipping_attr(1).source_line_id =' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
7146                      DEBUG('l_shipping_attr(1).transfer_lpn_id =' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
7147                      DEBUG('l_shipping_attr(1).delivery_detail_id =' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
7148                      DEBUG('l_shipping_attr(1).picked_quantity =' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
7149                      DEBUG('l_shipping_attr(1).picked_quantity2 =' || l_shipping_attr(1).picked_quantity2, 'STAGE_LPNS');
7150                      DEBUG('l_shipping_attr(1).pending_quantity =' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
7151                      DEBUG('l_shipping_attr(1).pending_quantity2 =' || l_shipping_attr(1).pending_quantity2, 'STAGE_LPNS');
7152                      DEBUG('l_delivery_detail_tab(i).requested_quantity =' ||
7153                             l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
7154                      DEBUG('l_delivery_detail_tab(i).requested_quantity2 =' ||
7155                             l_delivery_detail_tab(i).requested_quantity2, 'STAGE_LPNS');
7156                      DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
7157                      DEBUG('l_InvPCInRecType.transaction_temp_id  =' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
7158                   END IF;
7159 
7160                   -- Call shipping api to set transaction_temp_id global variable
7161                   IF ( l_invpcinrectype.transaction_temp_id IS NOT NULL ) THEN
7162                      IF ( l_shipping_attr(1).picked_quantity > 1 ) THEN
7163                         l_shipping_attr(1).serial_number  := NULL;
7164 
7165                         IF ( l_debug = 1 ) THEN
7166                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
7167                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
7168                         END IF;
7169 
7170                         WSH_INTEGRATION.Set_INV_PC_Attributes(
7171                           p_in_attributes => l_invpcinrectype
7172                         , x_return_status => l_return_status
7173                         , x_msg_count     => l_msg_data
7174                         , x_msg_data      => l_msg_count );
7175 
7176                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7177                            IF (l_debug = 1) THEN
7178                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
7179                            END IF;
7180 
7181                            RAISE fnd_api.g_exc_error;
7182                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7183                            IF (l_debug = 1) THEN
7184                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
7185                            END IF;
7186 
7187                            RAISE fnd_api.g_exc_unexpected_error;
7188                         END IF;
7189                      ELSE
7190                         BEGIN
7191                            SELECT fm_serial_number
7192                              INTO l_shipping_attr(1).serial_number
7193                              FROM mtl_serial_numbers_temp
7194                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
7195                               AND ROWNUM < 2;
7196 
7197                            IF ( l_debug = 1 ) THEN
7198                               DEBUG('found fm_serial='|| l_shipping_attr(1).serial_number||
7199                                     ' for transaction_temp_id='||l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
7200                            END IF;
7201 
7202                            DELETE FROM mtl_serial_numbers_temp
7203                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
7204                         EXCEPTION
7205                            WHEN NO_DATA_FOUND THEN
7206                               IF (l_debug = 1) THEN
7207                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
7208                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
7209                               END IF;
7210                         END;
7211                      END IF;
7212                   END IF; -- for serial controlled items
7213 
7214                   --patchset J.  Shipping API cleanup
7215                   l_delivery_detail_tab(i).requested_quantity := l_delivery_detail_tab(i).requested_quantity - l_rsv_qty;
7216                   l_delivery_detail_tab(i).requested_quantity2 := l_delivery_detail_tab(i).requested_quantity2 - l_rsv_sec_qty;
7217 
7218                   --Call shipping api to set transaction_temp_id global variable
7219                   IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
7220                      IF (l_shipping_attr(1).picked_quantity > 1) THEN
7221                         l_shipping_attr(1).serial_number  := NULL;
7222 
7223                         IF (l_debug = 1) THEN
7224                            DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
7225                                   l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
7226                         END IF;
7227 
7228                         wsh_integration.set_inv_pc_attributes(
7229                           p_in_attributes              => l_invpcinrectype
7230                         , x_return_status              => l_return_status
7231                         , x_msg_count                  => l_msg_data
7232                         , x_msg_data                   => l_msg_count
7233                         );
7234 
7235                         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7236                            IF (l_debug = 1) THEN
7237                               DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
7238                            END IF;
7239 
7240                            RAISE fnd_api.g_exc_error;
7241                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7242                            IF (l_debug = 1) THEN
7243                               DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
7244                            END IF;
7245 
7246                            RAISE fnd_api.g_exc_unexpected_error;
7247                         END IF;
7248                      ELSE
7249                         BEGIN
7250                            SELECT fm_serial_number
7251                              INTO l_shipping_attr(1).serial_number
7252                              FROM mtl_serial_numbers_temp
7253                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
7254                               AND ROWNUM < 2;
7255 
7256                            IF (l_debug = 1) THEN
7257                               DEBUG('found fm_serial='
7258                                      || l_shipping_attr(1).serial_number
7259                                      || ' for transaction_temp_id='
7260                                      || l_invpcinrectype.transaction_temp_id
7261                                    , 'STAGE_LPN'
7262                                    );
7263                            END IF;
7264 
7265                            DELETE FROM mtl_serial_numbers_temp
7266                             WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
7267                         EXCEPTION
7268                            WHEN NO_DATA_FOUND THEN
7269                               IF (l_debug = 1) THEN
7270                                  DEBUG('No rows found in MSNT for transaction_temp_id=' ||
7271                                         l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
7272                               END IF;
7273                         END;
7274                      END IF;
7275                   END IF;
7276 
7277                   IF (l_debug = 1) THEN
7278                      DEBUG('Call  update shipping attributes', 'stage_lpns');
7279                   END IF;
7280                   wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
7281                   , x_return_status              => l_return_status);
7282 
7283                   IF (l_debug = 1) THEN
7284                      DEBUG('after update shipping attributes', 'stage_lpns');
7285                   END IF;
7286 
7287                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7288                      IF (l_debug = 1) THEN
7289                         DEBUG('return error from update shipping attributes 2', 'stage_lpns');
7290                     END IF;
7291 
7292                     RAISE fnd_api.g_exc_error;
7293                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7294                      IF (l_debug = 1) THEN
7295                         DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
7296                      END IF;
7297 
7298                      RAISE fnd_api.g_exc_unexpected_error;
7299                   END IF;
7300 
7301                   --Since we have split the WDD line, we need to process
7302                   --this newly split line also.
7303                   --Setting these variables will process the newly split line
7304                  /* 4440809: l_delivery_detail_tab(i).delivery_detail_id := l_new_delivery_detail_id; */
7305                  /*****************************************
7306                   * Bug No 3390432
7307                   * Only changing the Transaction_temp_id of WDD to Null value when
7308                   * serial control code is 2 or 5
7309                   ******************************************/
7310                   IF (l_serial_number_control_code IN (2,5)) THEN -- Serial NOT in MSNT
7311                      l_delivery_detail_tab(i).transaction_temp_id := NULL;
7312                   END IF;
7313                   l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
7314 
7315                   l_rsv_index := j + 1;
7316                   l_rsv_qty := 0;
7317                   l_rsv_sec_qty := 0;
7318                END IF; --} Res. Qty Match l_rsv_qty < l_delivery_detail_tab(i).requested_quantity
7319                -- IF-2
7320             END IF; --}  g_fulfillment_base = 'S'
7321 
7322             <<next_resv_rec>>
7323             IF (l_rsv_index > l_mtl_reservation_tbl.COUNT) THEN
7324               l_rsv_index := 0;
7325               l_rsv_qty := 0;
7326               l_rsv_sec_qty := 0;
7327               EXIT delivery_detail_loop; -- 2529382
7328             END IF;
7329 
7330           END LOOP; -- Reservation Loop
7331         END LOOP; -- Delivery Detail Loop
7332       ELSE -- Non Reservable
7333         IF (l_debug = 1) THEN
7334           DEBUG('Item or Sub in non reservable so stage the line without updating the inventory details');
7335         END IF;
7336 
7337         FOR i IN 1 .. l_delivery_detail_tab.COUNT LOOP
7338           l_shipping_attr.DELETE;
7339           l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_tab(i).delivery_detail_id;
7340           l_shipping_attr(1).released_status     := 'Y';
7341           wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
7342           , x_return_status              => l_return_status);
7343 
7344           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7345             IF (l_debug = 1) THEN
7346               DEBUG('Non-Reservable: return error from update shipping attributes', 'stage_lpns');
7347             END IF;
7348 
7349             RAISE fnd_api.g_exc_error;
7350           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7351             IF (l_debug = 1) THEN
7352               DEBUG('Non-Reservable: return unexpected error from update shipping attributes', 'stage_lpns');
7353             END IF;
7354 
7355             RAISE fnd_api.g_exc_unexpected_error;
7356           END IF;
7357         END LOOP; -- Non-Reservable delivery_details
7358       END IF; -- Reservable
7359     END LOOP; -- STAGE_LINE;
7360 
7361     CLOSE stage_lines;
7362   END LOOP; -- OUTER LPN
7363 
7364   CLOSE outer_lpn;
7365 
7366   BEGIN
7367     OPEN outer_lpn;
7368 
7369     LOOP
7370       FETCH outer_lpn INTO l_outer_lpn;
7371       EXIT WHEN outer_lpn%NOTFOUND;
7372 
7373       IF (l_debug = 1) THEN
7374         DEBUG('populate_wstt   ', 'stage_lpns');
7375       END IF;
7376 
7377       wms_shipping_transaction_pub.populate_wstt(
7378         x_return                     => l_return
7379       , x_msg_code                   => x_msg_data
7380       , p_organization_id            => p_organization_id
7381       , p_lpn_id                     => l_outer_lpn.lpn_id
7382       , p_trip_id                    => 0
7383       , p_dock_door_id               => p_dock_door_id
7384       , p_direct_ship_flag           => 'Y'
7385       );
7386 
7387       IF l_return = 1 THEN
7388         x_return_status  := fnd_api.g_ret_sts_error;
7389         RAISE fnd_api.g_exc_error;
7390       END IF;
7391 
7392       SELECT COUNT(*)
7393         INTO l_trip_id
7394         FROM wms_shipping_transaction_temp
7395        WHERE outermost_lpn_id = l_outer_lpn.lpn_id;
7396 
7397       IF (l_debug = 1) THEN
7398         DEBUG('After Populate WSTT, No of LPNs loaded :' || TO_CHAR(l_trip_id), 'stage_lpns');
7399       END IF;
7400 
7401       -- Update container Hierarchy so that container so that following cols has same value as delivery lines
7402       -- SHIP_TO_LOCATION_ID, SHIP_TO_LOCATION_ID, DELIVER_TO_LOCATION_ID
7403       BEGIN
7404 	 IF l_trip_id > 0 THEN
7405 	    UPDATE wms_license_plate_numbers
7406 	      SET lpn_context = 9
7407 	      WHERE outermost_lpn_id = l_outer_lpn.lpn_id;
7408 
7409 	    IF (l_debug = 1) THEN
7410 	       DEBUG('Update LPN Context to Loaded to Dock, LPN :' || TO_CHAR(l_outer_lpn.lpn_id), 'Stage_LPNs');
7411 	    END IF;
7412 	 END IF;
7413       EXCEPTION
7414 	 WHEN NO_DATA_FOUND THEN
7415 	    NULL;
7416       END;
7417 
7418       wms_shipping_transaction_pub.create_delivery(
7419 						  p_outermost_lpn_id           => l_outer_lpn.lpn_id
7420 						  , p_trip_id                    => NULL
7421 						  , p_organization_id            => p_organization_id
7422 						  , p_dock_door_id               => p_dock_door_id
7423 						  , x_delivery_id                => l_delivery_id
7424 						  , x_return_status              => l_return_status
7425 						  , x_message                    => l_msg_data
7426 						  , p_direct_ship_flag           => 'Y'
7427 						  );
7428 
7429      IF (l_debug = 1) THEN
7430         DEBUG('Create Delivery  :' || TO_CHAR(l_delivery_id) || ' Return Status ' || l_return_status, 'stage_lpns');
7431      END IF;
7432 
7433      IF l_return_status IN('S') THEN
7434 	IF (l_debug = 1) THEN
7435 	   DEBUG('return success from create_delivery ', 'stage_lpns');
7436 	END IF;
7437 
7438 	x_return_status  := fnd_api.g_ret_sts_success;
7439       ELSIF l_return_status IN('E') THEN
7440 	IF (l_debug = 1) THEN
7441 	   DEBUG('return error from create_delivery', 'stage_lpns');
7442 	END IF;
7443 
7444 	x_return_status  := fnd_api.g_ret_sts_error;
7445 	RAISE fnd_api.g_exc_error;
7446       ELSE
7447 	IF (l_debug = 1) THEN
7448 	   DEBUG('return unexpected error from create_delivery', 'stage_lpns');
7449 	END IF;
7450 
7451 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
7452 	RAISE fnd_api.g_exc_unexpected_error;
7453      END IF;
7454 
7455      --- <Changes for Delivery Merge>
7456      IF g_wms_current_release_level >= g_j_release_level THEN
7457 
7458 	l_action_prms.caller := 'WMS_DLMG';
7459 	l_action_prms.event := wsh_interface_ext_grp.g_start_of_shipping;
7460 	l_action_prms.action_code := 'ADJUST-PLANNED-FLAG';
7461 
7462 	l_delivery_id_tab(1) := l_delivery_id;
7463 
7464 	wsh_interface_ext_grp.delivery_action
7465 	  (p_api_version_number     => 1.0,
7466 	   p_init_msg_list          => fnd_api.g_false,
7467 	   p_commit                 => fnd_api.g_false,
7468 	   p_action_prms            => l_action_prms,
7469 	   p_delivery_id_tab        => l_delivery_id_tab,
7470 	   x_delivery_out_rec       => l_delivery_out_rec,
7471 	   x_return_status          => l_return_status,
7472 	   x_msg_count              => l_msg_count,
7473 	   x_msg_data               => l_msg_data);
7474 
7475 	IF (l_debug = 1) THEN
7476 	   debug('Called Adjust Planned Status with return status: ' || l_return_status, 'stage_lpns');
7477 	END IF;
7478 
7479 	-- Do not error out even if the call fails.
7480 
7481 
7482 	-- g-log changes
7483 
7484 	IF WSH_UTIL_CORE.GC3_IS_INSTALLED = 'Y' THEN
7485 	   IF (l_debug = 1) THEN
7486 	      debug('G-Log Changes: G-Log installed', 'LPN_CHECK');
7487 	   END IF;
7488 
7489 	   OPEN c_Get_OTM_flag(l_delivery_id);
7490 	   FETCH c_Get_OTM_flag INTO l_ignore_for_planning, l_tms_interface_flag;
7491 	   IF (l_debug = 1) THEN
7492 	      debug('l_ignore_for_planning : '|| l_ignore_for_planning, 'LPN_CHECK');
7493 	      debug('l_tms_interface_flag : '|| l_tms_interface_flag, 'LPN_CHECK');
7494 	   END IF;
7495 
7496 	   IF (c_Get_OTM_flag%NOTFOUND) THEN
7497 	      IF (l_debug = 1) THEN
7498 		 debug('No WDDs found for the delivery created ', 'LPN_CHECK');
7499 	      END IF;
7500 	   END IF;
7501 	   CLOSE c_Get_OTM_flag;
7502 
7503 	   --Important Note: Irrespective of the severity level of 'CR' exception for the delivery just
7504 	   --created, we have to mark the delivery to ignore_for_planning so that the
7505 	   --transaction goes through fine.
7506 	   -- Hence there is no call to WSH_INTERFACE_EXT_GRP.OTM_PRE_SHIP_CONFIRM().
7507 	   -- Here delivery was created in the backend for the line that the use
7508 	   -- chose to ship confirm. it IS ALL happening in the backend.
7509 
7510 	   IF l_ignore_for_planning = 'N' AND l_tms_interface_flag = 'CR' THEN
7511 	      l_action_prms.action_code := 'IGNORE_PLAN';
7512 	      wsh_interface_ext_grp.delivery_action
7513 		(p_api_version_number     => 1.0,
7514 		 p_init_msg_list          => fnd_api.g_false,
7515 		 p_commit                 => fnd_api.g_false,
7516 		 p_action_prms            => l_action_prms,
7517 		 p_delivery_id_tab        => l_delivery_id_tab,
7518 		 x_delivery_out_rec       => l_delivery_out_rec,
7519 		 x_return_status          => l_return_status,
7520 		 x_msg_count              => l_msg_count,
7521 		 x_msg_data               => l_msg_data);
7522 
7523 	      IF (l_debug = 1) THEN
7524 		 debug('Called wsh_interface_ext_grp.delivery_action with action_code IGNORE_PLAN and return status: ' || l_return_status, 'stage_lpns');
7525 	      END IF;
7526 
7527 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
7528 		 x_msg_data := 'WMS_DELIVERY_ACTION_FAIL';
7529 		 RAISE fnd_api.g_exc_error;
7530 	       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7531 		 x_msg_data := 'WMS_DELIVERY_ACTION_FAIL';
7532 		 RAISE fnd_api.g_exc_unexpected_error;
7533 	      END IF;
7534 	   END IF;
7535 
7536 	END IF; --g-log changes
7537 
7538      END IF;
7539      -- </Changes for delivery merge>
7540 
7541     END LOOP;
7542 
7543     CLOSE outer_lpn;
7544   END;        -- Populate WSTT and Update the LPN context
7545               -- Stage the delivery details that have been updated just now
7546   IF (l_debug = 1) THEN
7547      DEBUG('Exiting  Stage LPNs..: ' , 'STAGE_LPN');
7548   END IF;
7549        --COMMIT;
7550 EXCEPTION
7551    WHEN fnd_api.g_exc_error THEN
7552       x_return_status  := fnd_api.g_ret_sts_error;
7553 
7554       IF (l_debug = 1) THEN
7555 	 DEBUG('In exception type E', 'stage_lpns');
7556       END IF;
7557       --      ROLLBACK;
7558       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7559 
7560    WHEN fnd_api.g_exc_unexpected_error THEN
7561       x_return_status  := fnd_api.g_ret_sts_unexp_error;
7562 
7563       IF (l_debug = 1) THEN
7564 	 DEBUG('In exception type U', 'stage_lpns');
7565       END IF;
7566 
7567       --      ROLLBACK;
7568     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7569    WHEN OTHERS THEN
7570       x_return_status  := wsh_util_core.g_ret_sts_unexp_error;
7571 
7572       IF (l_debug = 1) THEN
7573 	 DEBUG('Exception When Others ' || SQLERRM, 'Stage_LPNS');
7574       END IF;
7575 END stage_lpns;
7576 
7577 PROCEDURE load_truck(
7578   x_return_status OUT NOCOPY    VARCHAR2
7579 , x_msg_data      OUT NOCOPY    VARCHAR2
7580 , x_msg_count     OUT NOCOPY    NUMBER
7581 , p_group_id      IN            NUMBER
7582 , p_org_id        IN            NUMBER
7583 , p_dock_door_id  IN            NUMBER
7584 ) IS
7585 BEGIN
7586   --bug #2782201
7587   NULL;
7588 /*
7589   This procedure is no more used from Patchset I. For corresponding functionality
7590   please look load_lpn.
7591  */
7592 END load_truck;
7593 
7594 PROCEDURE close_truck(
7595   x_return_status    OUT NOCOPY    VARCHAR2
7596 , x_msg_data         OUT NOCOPY    VARCHAR2
7597 , x_msg_count        OUT NOCOPY    NUMBER
7598 , x_error_code       OUT NOCOPY    NUMBER
7599 , x_missing_item_cur OUT NOCOPY    t_genref
7600 , p_dock_door_id     IN            NUMBER
7601 , p_group_id         IN            NUMBER
7602 , p_org_id           IN            NUMBER
7603 ) IS
7604   CURSOR trip_cursor IS
7605     SELECT DISTINCT trip_id
7606                FROM wms_shipping_transaction_temp
7607               WHERE dock_door_id = p_dock_door_id
7608                 AND organization_id = p_org_id
7609                 AND direct_ship_flag = 'Y';
7610 
7611   l_return_status       VARCHAR2(1);
7612   l_msg_count           NUMBER;
7613   l_msg_data            VARCHAR2(20000);
7614   l_trip_cursor         trip_cursor%ROWTYPE;
7615   l_vehicle_item_id     NUMBER;   --Bug#6878013
7616   l_debug               NUMBER                            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7617   l_ship_method_code    wsh_trips.ship_method_code%TYPE;
7618   l_enforce_ship_method VARCHAR2(1);
7619   l_trip_name           wsh_trips.NAME%TYPE;
7620   no_ship_method_code   EXCEPTION;
7621 
7622 BEGIN
7623   --SAVEPOINT closetruck;
7624   x_return_status  := fnd_api.g_ret_sts_success;
7625 
7626   IF (l_debug = 1) THEN
7627     inv_trx_util_pub.TRACE('In close truck program ', 'close_truck', 9);
7628     inv_trx_util_pub.TRACE('p_dock_door_id     : ' || p_dock_door_id     , 'close_truck', 9);
7629     inv_trx_util_pub.TRACE('p_group_id         : ' || p_group_id         , 'close_truck', 9);
7630     inv_trx_util_pub.TRACE('p_org_id           : ' || p_org_id           , 'close_truck', 9);
7631   END IF;
7632 
7633   IF (l_debug = 1) THEN
7634     DEBUG('Before call to Confirm All deliveries API close_truck');
7635   END IF;
7636 
7637   wms_direct_ship_pvt.confirm_all_deliveries(
7638     x_return_status              => l_return_status
7639   , x_msg_count                  => l_msg_count
7640   , x_msg_data                   => l_msg_data
7641   , x_missing_item_cur           => x_missing_item_cur
7642   , x_error_code                 => x_error_code
7643   , p_delivery_id                => NULL
7644   , p_net_weight                 => NULL
7645   , p_gross_weight               => NULL
7646   , p_wt_uom_code                => NULL
7647   , p_waybill                    => NULL
7648   , p_ship_method_code           => NULL
7649   , p_fob_code                   => NULL
7650   , p_fob_location_id            => NULL
7651   , p_freight_term_code          => NULL
7652   , p_freight_term_name          => NULL
7653   , p_intmed_shipto_loc_id       => NULL
7654   , p_org_id                     => p_org_id
7655   , p_dock_door_id               => p_dock_door_id
7656   );
7657 
7658   IF l_return_status = fnd_api.g_ret_sts_error THEN
7659     IF (l_debug = 1) THEN
7660       inv_trx_util_pub.TRACE('Confirm All deliveries API failed with status E ', 'close_truck', 9);
7661     END IF;
7662 
7663     IF (l_debug = 1) THEN
7664       DEBUG('Confirm All deliveries API failed with status E close_truck');
7665     END IF;
7666 
7667     RAISE fnd_api.g_exc_error;
7668   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7669     IF (l_debug = 1) THEN
7670       inv_trx_util_pub.TRACE('Confirm All deliveries failed with status U', 'close_truck', 9);
7671     END IF;
7672 
7673     IF (l_debug = 1) THEN
7674       DEBUG('Confirm All deliveries failed with status U close_truck');
7675     END IF;
7676 
7677     RAISE fnd_api.g_exc_unexpected_error;
7678   END IF;
7679 
7680   IF (l_debug = 1) THEN
7681     inv_trx_util_pub.TRACE('Confirm All deliveries successfully completed ', 'close_truck', 9);
7682   END IF;
7683 
7684   IF (l_debug = 1) THEN
7685      DEBUG('Confirm All deliveries successfully completed  close_truck');
7686      debug('Get the enforce_ship_method flag','close_truck');
7687   END IF;
7688 
7689   INV_SHIPPING_TRANSACTION_PUB.get_enforce_ship
7690     (p_org_id         => p_org_id
7691      ,x_enforce_ship  => l_enforce_ship_method
7692      ,x_return_status => l_return_status
7693      ,x_msg_data      => l_msg_data
7694      ,x_msg_count     => l_msg_count);
7695 
7696   IF (l_debug=1) THEN
7697      debug('get_enforce_ship returned status: ' || l_return_status,'Close_truck');
7698      debug('Enforce_ship_method flag: ' || l_enforce_ship_method,'Close_truck');
7699   END IF;
7700 
7701   OPEN trip_cursor;
7702 
7703   LOOP
7704     FETCH trip_cursor INTO l_trip_cursor;
7705     EXIT WHEN trip_cursor%NOTFOUND;
7706 
7707     IF (l_debug = 1) THEN
7708       DEBUG('Before call to Close Trip API ', 'close_truck');
7709     END IF;
7710 
7711     /*Bug 2980013:Check if the Ship method Code is populated if the Enforce Ship method Flag is set to Y*/
7712     BEGIN
7713       SELECT ship_method_code
7714            , NAME
7715         INTO l_ship_method_code
7716            , l_trip_name
7717         FROM wsh_trips_ob_grp_v
7718        WHERE trip_id = l_trip_cursor.trip_id;
7719     EXCEPTION
7720       WHEN NO_DATA_FOUND THEN
7721         l_ship_method_code  := NULL;
7722     END;
7723 
7724     IF l_enforce_ship_method = 'Y'
7725        AND l_ship_method_code IS NULL THEN
7726       RAISE no_ship_method_code;
7727     END IF;
7728 
7729      /*Bug6878013 If the Vehicle Item Id is not null, pass its value else pass NULL*/
7730      BEGIN
7731         SELECT vehicle_item_id
7732             INTO l_vehicle_item_id
7733             FROM wsh_trips_ob_grp_v
7734             WHERE trip_id = l_trip_cursor.trip_id;
7735       EXCEPTION
7736         WHEN NO_DATA_FOUND THEN
7737           l_vehicle_item_id := NULL;
7738       END;
7739 
7740       wms_direct_ship_pvt.close_trip(
7741         x_return_status              => l_return_status
7742       , x_msg_count                  => l_msg_count
7743       , x_msg_data                   => l_msg_data
7744       , p_trip_id                    => l_trip_cursor.trip_id
7745       , p_vehicle_item_id            => l_vehicle_item_id  --Bug#6878013
7746       , p_vehicle_num_prefix         => NULL
7747       , p_vehicle_num                => NULL
7748       , p_seal_code                  => NULL
7749       , p_document_set_id            => NULL
7750       , p_org_id                     => p_org_id
7751       , p_dock_door_id               => p_dock_door_id
7752       , p_ship_method_code           => l_ship_method_code
7753       );
7754 
7755       IF (l_debug = 1) THEN
7756         DEBUG('After call to Close Trip API close_truck');
7757       END IF;
7758 
7759       IF l_return_status = fnd_api.g_ret_sts_error THEN
7760         IF (l_debug = 1) THEN
7761           DEBUG('Close trip API failed with status E ', 'close_truck');
7762         END IF;
7763 
7764         RAISE fnd_api.g_exc_error;
7765       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7766         IF (l_debug = 1) THEN
7767           DEBUG('Close trip failed with status U', 'close_truck');
7768         END IF;
7769 
7770         RAISE fnd_api.g_exc_unexpected_error;
7771       END IF;
7772 
7773       IF (l_debug = 1) THEN
7774         DEBUG('Close trip API completed successfully ', 'close_truck');
7775       END IF;
7776 
7777   END LOOP;
7778 
7779   CLOSE trip_cursor;
7780 
7781   IF (l_debug = 1) THEN
7782     DEBUG('End of close truck ', 'close_truck', 9);
7783   END IF;
7784 EXCEPTION
7785   WHEN no_ship_method_code THEN
7786     x_return_status  := fnd_api.g_ret_sts_error;
7787     fnd_message.set_name('WMS', 'WMS_SHIP_METHOD_CODE');
7788     fnd_message.set_token('TRIP_NAME', l_trip_name);
7789     /* No Ship method code provided for the Trip .This is required */
7790     fnd_msg_pub.ADD;
7791     /*fnd_msg_pub.count_and_get( p_count => x_msg_count,
7792                           p_data  => x_msg_data
7793                         );*/
7794     DEBUG('In exception no_ship_method_code ', 'Close Trip');
7795   WHEN fnd_api.g_exc_error THEN
7796     x_return_status  := fnd_api.g_ret_sts_error;
7797     ROLLBACK; --TO closetruck;
7798     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7799   WHEN fnd_api.g_exc_unexpected_error THEN
7800     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7801     ROLLBACK; --TO closetruck;
7802     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7803   WHEN OTHERS THEN
7804     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7805     ROLLBACK; --TO closetruck;
7806     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7807 END;
7808 
7809   --CLOSE_TRUCK
7810 
7811 PROCEDURE get_lpn_available_quantity(
7812   x_return_status     OUT NOCOPY    VARCHAR2
7813 , x_msg_count         OUT NOCOPY    NUMBER
7814 , x_msg_data          OUT NOCOPY    VARCHAR2
7815 , p_organization_id   IN            NUMBER
7816 , p_lpn_id            IN            NUMBER
7817 , p_inventory_item_id IN            NUMBER
7818 , p_revision          IN            VARCHAR2
7819 , p_line_id           IN            NUMBER
7820 , p_header_id         IN            NUMBER
7821 , x_qoh               OUT NOCOPY    NUMBER
7822 , x_att               OUT NOCOPY    NUMBER
7823 ) IS
7824   CURSOR inner_lpn(p_lpn NUMBER) IS
7825     SELECT lpn_id
7826          , subinventory_code
7827          , locator_id
7828       FROM wms_license_plate_numbers
7829      WHERE outermost_lpn_id = p_lpn;
7830 
7831   l_rqoh                  NUMBER;
7832   l_qr                    NUMBER;
7833   l_qs                    NUMBER;
7834   l_atr                   NUMBER;
7835   l_revision_control      NUMBER;
7836   l_lot_control           NUMBER;
7837   l_serial_control        NUMBER;
7838   l_is_revision_control   BOOLEAN             := FALSE;
7839   l_is_lot_control        BOOLEAN             := FALSE;
7840   l_is_serial_control     BOOLEAN             := FALSE;
7841   l_inner_lpn             inner_lpn%ROWTYPE;
7842   l_msg_data              VARCHAR2(20000);
7843   l_demand_source_type_id NUMBER;
7844   l_debug                 NUMBER              := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7845 BEGIN
7846   x_return_status  := fnd_api.g_ret_sts_success;
7847 
7848   IF (l_debug = 1) THEN
7849     DEBUG('The value of lpn_id is ' || p_lpn_id, 'GET_LPN_AVAILABLE_QUANTITY');
7850     DEBUG('The value of p_inventory_item_id is ' || p_inventory_item_id, 'GET_LPN_AVAILABLE_QUANTITY');
7851     DEBUG('The value of p_revision is ' || NVL(p_revision, 'A'), 'GET_LPN_AVAILABLE_QUANTITY');
7852     DEBUG('The value of p_line_id is ' || p_line_id, 'GET_LPN_AVAILABLE_QUANTITY');
7853     DEBUG('The value of p_header_id is ' || p_header_id, 'GET_LPN_AVAILABLE_QUANTITY');
7854   END IF;
7855 
7856   inv_quantity_tree_pub.clear_quantity_cache;
7857 
7858   SELECT revision_qty_control_code
7859        , lot_control_code
7860        , serial_number_control_code
7861     INTO l_revision_control
7862        , l_lot_control
7863        , l_serial_control
7864     FROM mtl_system_items
7865    WHERE organization_id = p_organization_id
7866      AND inventory_item_id = p_inventory_item_id;
7867 
7868   IF (l_revision_control = 2) THEN
7869     l_is_revision_control  := TRUE;
7870   END IF;
7871 
7872   IF (l_lot_control = 2) THEN
7873     l_is_lot_control  := TRUE;
7874   END IF;
7875 
7876   IF (l_serial_control = 2
7877       OR l_serial_control = 5
7878       OR l_serial_control = 6) THEN
7879     l_is_serial_control  := TRUE;
7880   END IF;
7881 
7882   l_demand_source_type_id := get_demand_src_type(p_header_id);
7883 
7884   x_att            := 0;
7885   OPEN inner_lpn(p_lpn_id);
7886 
7887   LOOP
7888     FETCH inner_lpn INTO l_inner_lpn;
7889     EXIT WHEN inner_lpn%NOTFOUND;
7890     /* Bug 2440408: All the Trees in the cache have to be cleared so that trees are rebuild */
7891     inv_quantity_tree_pub.clear_quantity_cache;
7892     inv_quantity_tree_pub.query_quantities(
7893       p_api_version_number         => 1.0
7894     , p_init_msg_lst               => fnd_api.g_false
7895     , x_return_status              => x_return_status
7896     , x_msg_count                  => x_msg_count
7897     , x_msg_data                   => x_msg_data
7898     , p_organization_id            => p_organization_id
7899     , p_inventory_item_id          => p_inventory_item_id
7900     , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
7901     , p_is_revision_control        => l_is_revision_control
7902     , p_is_lot_control             => l_is_lot_control
7903     , p_is_serial_control          => l_is_serial_control
7904     , p_demand_source_type_id      => l_demand_source_type_id
7905     , p_demand_source_header_id    => inv_salesorder.get_salesorder_for_oeheader(p_header_id)
7906     , p_demand_source_line_id      => p_line_id
7907     , p_revision                   => p_revision
7908     , p_lot_number                 => NULL
7909     , p_subinventory_code          => NULL
7910     , p_locator_id                 => NULL
7911     , x_qoh                        => x_qoh
7912     , x_rqoh                       => l_rqoh
7913     , x_qr                         => l_qr
7914     , x_qs                         => l_qs
7915     , x_att                        => x_att
7916     , x_atr                        => l_atr
7917     , p_lpn_id                     => l_inner_lpn.lpn_id
7918     );
7919 
7920     IF (l_debug = 1) THEN
7921       DEBUG('The value of x_att is ' || x_att, 'GET_LPN_AVAILABLE_QUANTITY');
7922     END IF;
7923 
7924     -- If the qty tree returns and error raise an exception.
7925     IF x_return_status <> fnd_api.g_ret_sts_success THEN
7926       IF (l_debug = 1) THEN
7927         inv_log_util.TRACE('Qty Tree Failed' || l_msg_data, 'INV_VMI_VALIDATIONS', 9);
7928       END IF;
7929 
7930       RAISE fnd_api.g_exc_unexpected_error;
7931     END IF;
7932 
7933     IF (x_att > 0) THEN
7934       IF (l_debug = 1) THEN
7935         DEBUG('The value of x_att is - in if ' || x_att, 'GET_LPN_AVAILABLE_QUANTITY');
7936       END IF;
7937 
7938       x_return_status  := fnd_api.g_ret_sts_success;
7939       EXIT;
7940     END IF;
7941   END LOOP;
7942 
7943   x_return_status  := fnd_api.g_ret_sts_success;
7944 EXCEPTION
7945   WHEN OTHERS THEN
7946     IF (l_debug = 1) THEN
7947       inv_log_util.TRACE('When others Exception in get_available_vmi_quantity', 'GET_LPN_AVAILABLE_QUANTITY', 9);
7948     END IF;
7949 
7950     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7951     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7952     RETURN;
7953 END get_lpn_available_quantity;
7954 
7955 PROCEDURE create_update_containers(
7956   x_return_status    OUT NOCOPY    VARCHAR2
7957 , x_msg_count        OUT NOCOPY    NUMBER
7958 , x_msg_data         OUT nocopy    VARCHAR2
7959 , p_org_id           IN            NUMBER
7960 , p_outermost_lpn_id IN NUMBER
7961 , p_delivery_id      IN  NUMBER DEFAULT NULL
7962 ) IS
7963   CURSOR lpn_details IS
7964     SELECT lpn_id
7965          , license_plate_number
7966          , subinventory_code
7967          , locator_id
7968          , inventory_item_id
7969          , revision
7970          , lot_number
7971          , serial_number
7972          , gross_weight_uom_code
7973          , gross_weight
7974          , tare_weight_uom_code
7975          , tare_weight
7976          , content_volume_uom_code
7977          , content_volume
7978       -- Release 12 (K): LPN Synchronization
7979       -- Add following new columns
7980          , container_volume
7981          , container_volume_uom
7982          , organization_id
7983       FROM wms_license_plate_numbers
7984      WHERE organization_id = p_org_id
7985        AND outermost_lpn_id = p_outermost_lpn_id;
7986 
7987   l_lpn_ids            wsh_util_core.id_tab_type;
7988   l_container_ids      wsh_util_core.id_tab_type;
7989   l_lpn_id             NUMBER;
7990   l_segmentarray       fnd_flex_ext.segmentarray;
7991   l_count              NUMBER                                     := 0;
7992   l_net_weight         NUMBER;
7993   l_deliv_det_id       NUMBER;
7994   l_container_name     wsh_delivery_details.container_name%TYPE;
7995   l_container_flag     wsh_delivery_details.container_flag%TYPE;
7996   l_container_rec      wsh_container_grp.changedattributetabtype;
7997   -- added for bug 2529382
7998   l_status_code        VARCHAR2(2);
7999   l_container_new_name VARCHAR2(30);
8000   --Bug number:2701925:
8001   l_delivery_detail_id NUMBER;
8002   l_delivery_name VARCHAR2(30) := NULL;
8003   l_debug              NUMBER                                     := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8004 
8005   -- Release 12 (K): LPN Synchronization/Convergence
8006   -- Types needed for WSH_WMS_LPN_GRP.Create_Update_Containers
8007   l_lpn_cur       WMS_Data_Type_Definitions_PUB.LPNRecordType;
8008   l_wsh_dd_rec    WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Rec_Type;
8009   l_wsh_dd_upd_rec  WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Rec_Type;
8010   wsh_create_tbl  WSH_GLBL_VAR_STRCT_GRP.delivery_details_Attr_tbl_Type;
8011   wsh_update_tbl  WSH_GLBL_VAR_STRCT_GRP.delivery_details_Attr_tbl_Type;
8012   l_IN_rec        WSH_GLBL_VAR_STRCT_GRP.detailInRecType;
8013   l_OUT_rec       WSH_GLBL_VAR_STRCT_GRP.detailOutRecType;
8014 
8015 
8016 BEGIN
8017   x_return_status  := fnd_api.g_ret_sts_success;
8018 
8019   /* Release 12(K), changed to use l_lpn_cur as type of WMS_Data_Type_Definitions_PUB.LPNRecordType; */
8020   OPEN lpn_details;
8021   LOOP
8022     l_lpn_cur := null;
8023     FETCH lpn_details INTO
8024                l_lpn_cur.lpn_id
8025              , l_lpn_cur.license_plate_number
8026              , l_lpn_cur.subinventory_code
8027              , l_lpn_cur.locator_id
8028              , l_lpn_cur.inventory_item_id
8029              , l_lpn_cur.revision
8030              , l_lpn_cur.lot_number
8031              , l_lpn_cur.serial_number
8032              , l_lpn_cur.gross_weight_uom_code
8033              , l_lpn_cur.gross_weight
8034              , l_lpn_cur.tare_weight_uom_code
8035              , l_lpn_cur.tare_weight
8036              , l_lpn_cur.content_volume_uom_code
8037              , l_lpn_cur.content_volume
8038              , l_lpn_cur.container_volume
8039              , l_lpn_cur.container_volume_uom
8040              , l_lpn_cur.organization_id;
8041 
8042     IF lpn_details%NOTFOUND THEN
8043       IF (l_debug = 1) THEN
8044         DEBUG('No more LPNs found from lpn_details to process', 'create_update_containers');
8045       END IF;
8046       EXIT;
8047     ELSE
8048       IF (l_debug = 1) THEN
8049         DEBUG('Found from lpn_details lpn '||l_lpn_cur.license_plate_number||', lpn_id='||l_lpn_cur.lpn_id, 'create_update_containers');
8050       END IF;
8051     END IF;
8052 
8053 
8054   --FOR l_lpn_cur IN lpn_details LOOP -- Changed for Release 12
8055     --Reset the counter
8056     l_count                                := 0;
8057     l_lpn_id                               := l_lpn_cur.lpn_id;
8058 
8059 -- commented the below code for -- For LPN reuse ER : 6845650
8060 
8061     /* part of bug fix 2529382 */
8062 /*  BEGIN
8063       SELECT wdd.released_status
8064            , wdd.delivery_detail_id
8065         INTO l_status_code
8066            , l_delivery_detail_id
8067         FROM wsh_delivery_details_ob_grp_v wdd
8068        WHERE wdd.container_name = l_lpn_cur.license_plate_number
8069        AND wdd.released_status = 'X';  --Bug#6878521 Made change bt need to chk this
8070 
8071       IF l_status_code = 'C' THEN
8072         /* Release 12(K): LPN Synchronization
8073            1. Uniqueness constraint on WDD.container_name is removed
8074               So it is not required to append characters to the LPNs
8075               to get a new containers name
8076            2. Replace API call to wsh_container_grp.update_container
8077               with new API call WSH_WMS_LPN_GRP.Create_Update_Containers
8078            */
8079 /*
8080         IF (l_debug = 1) THEN
8081           DEBUG('Release status is C, Updating delivery detail to NULL out LPN_ID', 'create_update_containers');
8082           DEBUG(' Calling Create_Update_Containers with caller WMS, action code UPDATE_NULL', 'create_update_containers');
8083           DEBUG('  delivery_detail_id = '||l_delivery_detail_id);
8084         END IF;
8085 
8086         l_wsh_dd_upd_rec.delivery_detail_id := l_delivery_detail_id;
8087         l_wsh_dd_upd_rec.lpn_id := NULL;
8088 
8089         wsh_update_tbl(1) := l_wsh_dd_upd_rec;
8090 
8091         l_IN_rec.caller      := 'WMS';
8092         l_IN_rec.action_code := 'UPDATE_NULL';
8093 
8094         WSH_WMS_LPN_GRP.Create_Update_Containers (
8095           p_api_version     => 1.0
8096         , p_init_msg_list   => fnd_api.g_false
8097         , p_commit          => fnd_api.g_false
8098         , x_return_status   => x_return_status
8099         , x_msg_count       => x_msg_count
8100         , x_msg_data        => x_msg_data
8101         , p_detail_info_tab => wsh_update_tbl
8102         , p_IN_rec          => l_IN_rec
8103         , x_OUT_rec         => l_OUT_rec );
8104 
8105 
8106         IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
8107           IF (l_debug = 1) THEN
8108             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns error', 'create_update_containers');
8109           END IF;
8110 
8111           RAISE fnd_api.g_exc_error;
8112         ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
8113           IF (l_debug = 1) THEN
8114             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns unexpected error', 'create_update_containers');
8115           END IF;
8116 
8117           RAISE fnd_api.g_exc_unexpected_error;
8118         ELSE
8119           l_count  := 0;
8120           IF (l_debug = 1) THEN
8121             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns success, AF Container exists, set lpn_id to NULL', 'create_update_containers');
8122           END IF;
8123         END IF;
8124 
8125       ELSE
8126         l_count  := 1;
8127 
8128         IF (l_debug = 1) THEN
8129           DEBUG(' LPN with status ' || l_status_code || 'found in wdd. Check for data corruption');
8130         END IF;
8131       END IF;
8132     EXCEPTION
8133       WHEN NO_DATA_FOUND THEN
8134         l_count  := 0;
8135     END;
8136 
8137     /* end of bug fix 2529382 */
8138 
8139     IF (l_debug = 1) THEN
8140       DEBUG('LPN EXISTS: ' || l_count, 'create_update_containers');
8141     END IF;
8142 
8143     --If the LPN does not exist in in wsh_delivery_details, create a new container
8144     IF l_count = 0 THEN
8145 
8146       /* Release 12 (K): LPN Synchronization
8147          Replace the call to wsh_container_grp.create_containers
8148          with new API call to WSH_WMS_LPN_GRP.Create_Update_Containers
8149 
8150          The pre-R12 code was doing in two steps, create_container then update_container
8151          With the new API call, it replaces both previous API calls */
8152 
8153       /* Call function to create wsh delivery detail record */
8154       l_wsh_dd_rec := WMS_CONTAINER_PVT.To_DeliveryDetailsRecType(l_lpn_cur);
8155 
8156       /*Add l_wsh_dd_rec into record table */
8157 
8158       wsh_create_tbl(NVL(wsh_create_tbl.last, 0) + 1) := l_wsh_dd_rec;
8159 
8160     END IF;
8161 
8162   END LOOP;
8163 
8164   IF (l_debug = 1) THEN
8165     DEBUG('End of Loop of lpn_details, found '||wsh_create_tbl.count||' records in wsh_create_tbl to process', 'create_update_containers');
8166     DEBUG('Calling WSH_WMS_LPN_GRP.Create_Update_Containers with caller as WMS and CREATE', 'create_update_containers');
8167   END IF;
8168 
8169   l_IN_rec.caller      := 'WMS';
8170   l_IN_rec.action_code := 'CREATE';
8171 
8172   WSH_WMS_LPN_GRP.Create_Update_Containers (
8173     p_api_version     => 1.0
8174   , p_init_msg_list   => fnd_api.g_false
8175   , p_commit          => fnd_api.g_false
8176   , x_return_status   => x_return_status
8177   , x_msg_count       => x_msg_count
8178   , x_msg_data        => x_msg_data
8179   , p_detail_info_tab => wsh_create_tbl
8180   , p_IN_rec          => l_IN_rec
8181   , x_OUT_rec         => l_OUT_rec );
8182 
8183 
8184   IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
8185     IF (l_debug = 1) THEN
8186       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns error', 'create_update_containers');
8187     END IF;
8188 
8189     RAISE fnd_api.g_exc_error;
8190   ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
8191     IF (l_debug = 1) THEN
8192       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns unexpected error', 'create_update_containers');
8193     END IF;
8194 
8195     RAISE fnd_api.g_exc_unexpected_error;
8196   ELSE
8197     IF (l_debug = 1) THEN
8198       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns success', 'create_update_containers');
8199     END IF;
8200   END IF;
8201   -- End of Release 12 change
8202 
8203   IF (l_debug = 1) THEN
8204     DEBUG('The API return status is ' || x_return_status, 'create_update_containers');
8205   END IF;
8206 EXCEPTION
8207   WHEN fnd_api.g_exc_error THEN
8208     x_return_status  := fnd_api.g_ret_sts_error;
8209     ROLLBACK;
8210     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8211 
8212     IF (l_debug = 1) THEN
8213       DEBUG('Execution Error in Create_Update_Container:' || SUBSTR(SQLERRM, 1, 240), 9);
8214     END IF;
8215   WHEN fnd_api.g_exc_unexpected_error THEN
8216     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8217     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8218 
8219     IF (l_debug = 1) THEN
8220       DEBUG('Unexpected Error in Create_Update_Container:' || SUBSTR(SQLERRM, 1, 240), 'create_update_containers');
8221     END IF;
8222   WHEN OTHERS THEN
8223      --x_error_code := 9999;
8224      IF l_debug = 1 THEN
8225     debug('Others exception raised: ' ||  SUBSTR(SQLERRM, 1, 240),'create_update_containers');
8226      END IF;
8227     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8228     ROLLBACK;
8229 
8230     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
8231       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'create_update_containers');
8232     END IF;
8233 
8234     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8235 END create_update_containers;
8236 
8237 PROCEDURE update_shipped_quantity(
8238   x_return_status OUT NOCOPY    VARCHAR2
8239 , x_msg_count     OUT NOCOPY    NUMBER
8240 , x_msg_data      OUT NOCOPY    VARCHAR2
8241 , p_delivery_id   IN            NUMBER
8242 , p_org_id        IN            NUMBER DEFAULT NULL
8243 ) IS
8244   -- Cursor to get the delivery qty (sum of all the delivery_details )
8245   -- for the given delivery_id
8246 
8247   CURSOR delivery_item_qty IS
8248     SELECT   wdd.inventory_item_id
8249            , wdd.revision
8250            , wdd.lot_number
8251            , SUM(wdd.picked_quantity)
8252         FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
8253        WHERE wstt.delivery_id = p_delivery_id
8254          AND wdd.delivery_detail_id = wstt.delivery_detail_id
8255          AND wdd.released_status = 'Y'
8256     GROUP BY wdd.inventory_item_id, wdd.revision, wdd.lot_number
8257     ORDER BY wdd.inventory_item_id, wdd.revision, wdd.lot_number;
8258 
8259   -- Cursor to get the item qty in the LPN corresponding to the given delivery_id
8260   -- and the inventory item,revision
8261   CURSOR lpn_item_qty(p_delivery_id NUMBER, p_item_id NUMBER, p_revision VARCHAR2, p_lot_number VARCHAR2) IS
8262     SELECT SUM(wlc.quantity)
8263       FROM wms_lpn_contents wlc, wms_license_plate_numbers lpn,
8264       wms_shipping_transaction_temp wstt
8265       WHERE wlc.parent_lpn_id = lpn.lpn_id
8266       and wstt.delivery_id = p_delivery_id
8267       and lpn.outermost_lpn_id = wstt.outermost_lpn_id
8268       and wstt.inventory_item_id = p_item_id
8269       AND wstt.inventory_item_id = wlc.inventory_item_id
8270       AND NVL(wlc.revision, '#') = NVL(p_revision, '#')
8271       AND NVL(wlc.lot_number, '#') = NVL(p_lot_number, '#');
8272 
8273   l_item_id             NUMBER;
8274   l_revision            VARCHAR2(3);
8275 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
8276   l_lot_number          VARCHAR2(80);
8277   l_del_qty             NUMBER;
8278   l_shipping_attr       wsh_delivery_details_pub.changedattributetabtype;
8279   l_delivery_detail_id  NUMBER;
8280   l_picked_qty          NUMBER;
8281   l_extra_qty           NUMBER;
8282   l_lpn_qty             NUMBER;
8283   l_lpn_item_count      NUMBER;
8284   l_delivery_item_count NUMBER;
8285   l_init_msg_list       VARCHAR2(1)                                      := fnd_api.g_false;
8286   l_commit              VARCHAR2(1)                                      := fnd_api.g_false;
8287   l_return_status       VARCHAR2(1);
8288   l_msg_data            VARCHAR2(20000);
8289   l_msg_count           NUMBER;
8290   x_error_code          NUMBER;
8291   l_debug               NUMBER                                           := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8292 BEGIN
8293   x_return_status  := fnd_api.g_ret_sts_success;
8294 
8295   BEGIN
8296     --Query to get the total no. of distinct items in the lpn to be shipped
8297 
8298     SELECT   COUNT(COUNT(*))
8299         INTO l_lpn_item_count
8300         FROM wms_license_plate_numbers wlpn, wms_lpn_contents wlc, wms_shipping_transaction_temp wstt
8301        WHERE wstt.delivery_id = p_delivery_id
8302          AND wstt.outermost_lpn_id = wlpn.outermost_lpn_id
8303          AND wlpn.lpn_id = wlc.parent_lpn_id
8304     GROUP BY wlc.inventory_item_id, wlc.revision;
8305 
8306     --Query to get the total no. of distinct items in the delivery
8307 
8308     SELECT   COUNT(COUNT(*))
8309         INTO l_delivery_item_count
8310         FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
8311        WHERE wstt.delivery_id = p_delivery_id
8312          AND wdd.delivery_detail_id = wstt.delivery_detail_id
8313          AND wdd.released_status = 'Y'
8314     GROUP BY wdd.inventory_item_id, wdd.revision;
8315 
8316     --Check if lpn has items other than those for the delivery
8317 
8318     IF (l_lpn_item_count > l_delivery_item_count) THEN
8319       IF (l_debug = 1) THEN
8320         DEBUG('LPN contains items not belonging to the delivery.', 'Update_shipped_Quantity');
8321         DEBUG('Cannot ship', 'Update_shipped_Quantity');
8322       END IF;
8323 
8324       RAISE fnd_api.g_exc_error;
8325     END IF;
8326   EXCEPTION
8327     WHEN fnd_api.g_exc_error THEN
8328       -- Log a message for item count mismatch
8329       x_return_status  := fnd_api.g_ret_sts_error;
8330       x_error_code     := 10;
8331 
8332       IF (l_debug = 1) THEN
8333         DEBUG('LPN has more items than Delivery Details', 'Update_Shipped_Quantity');
8334         DEBUG('Some Items are not assigned to delivery details', 'Update_Shipped_Quantity');
8335       END IF;
8336   END; -- END Item Count
8337 
8338   OPEN delivery_item_qty;
8339 
8340   LOOP
8341     FETCH delivery_item_qty INTO l_item_id, l_revision, l_lot_number, l_del_qty;
8342     EXIT WHEN delivery_item_qty%NOTFOUND;
8343     OPEN lpn_item_qty(p_delivery_id, l_item_id, l_revision, l_lot_number);
8344     FETCH lpn_item_qty INTO l_lpn_qty;
8345     CLOSE lpn_item_qty;
8346 
8347     -- If LPN has more than delivery and we are shipping more.
8348     -- Update the delivery detail with the extra qty
8349 
8350     IF (l_lpn_qty > l_del_qty) THEN
8351       l_extra_qty                            := l_lpn_qty - l_del_qty;
8352 
8353       -- SELECT the first DELIVERY LINE returned from WDD
8354 
8355       BEGIN
8356         SELECT wdd.delivery_detail_id
8357              , wdd.picked_quantity
8358           INTO l_delivery_detail_id
8359              , l_picked_qty
8360           FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
8361          WHERE wstt.delivery_id = p_delivery_id
8362            AND wstt.inventory_item_id = l_item_id
8363            AND wdd.delivery_detail_id = wstt.delivery_detail_id
8364            AND wdd.inventory_item_id = wstt.inventory_item_id
8365            AND NVL(wdd.revision, '#') = NVL(l_revision, '#')
8366            AND NVL(wdd.lot_number, '#') = NVL(l_lot_number, '#')
8367            AND ROWNUM = 1;
8368       EXCEPTION
8369         WHEN NO_DATA_FOUND THEN
8370           NULL;
8371       END;
8372 
8373       -- Call WSH_DELIVERY_DETAILS to update the shipped_qty
8374       l_shipping_attr(1).shipped_quantity    := l_picked_qty + l_extra_qty;
8375       l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_id;
8376       wsh_delivery_details_pub.update_shipping_attributes(
8377         p_api_version_number         => 1.0
8378       , p_source_code                => 'OE'
8379       , p_init_msg_list              => l_init_msg_list
8380       , p_commit                     => l_commit
8381       , p_changed_attributes         => l_shipping_attr
8382       , x_return_status              => l_return_status
8383       , x_msg_count                  => l_msg_count
8384       , x_msg_data                   => l_msg_data
8385       );
8386 
8387       IF (l_debug = 1) THEN
8388         DEBUG('after calling Update_shipping_attributes', 'Update_shipped_Quantity');
8389       END IF;
8390 
8391       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
8392         IF (l_debug = 1) THEN
8393           DEBUG('return error from update shipping attributes 2', 'Update_shipped_Quantity');
8394         END IF;
8395 
8396         RAISE fnd_api.g_exc_error;
8397       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8398         IF (l_debug = 1) THEN
8399           DEBUG('return unexpected error from update shipping attributes', 'Update_shipped_Quantity');
8400         END IF;
8401 
8402         RAISE fnd_api.g_exc_unexpected_error;
8403       ELSIF l_return_status = 'S' THEN
8404         IF (l_debug = 1) THEN
8405           DEBUG('Shipping attributes updated successfully');
8406         END IF;
8407 
8408         x_return_status  := fnd_api.g_ret_sts_success;
8409       END IF;
8410     END IF; -- end if l_lpn_qty > l_del_qty
8411   END LOOP;
8412 
8413   CLOSE delivery_item_qty;
8414 EXCEPTION
8415   WHEN fnd_api.g_exc_error THEN
8416     x_return_status  := fnd_api.g_ret_sts_error;
8417     x_error_code     := 10;
8418     ROLLBACK;
8419     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8420 
8421     IF (l_debug = 1) THEN
8422       DEBUG('Execution Error in Update_shipped_Quantity', 'Update_shipped_Quantity');
8423       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
8424     END IF;
8425   WHEN fnd_api.g_exc_unexpected_error THEN
8426     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8427     x_error_code     := 20;
8428     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8429 
8430     IF (l_debug = 1) THEN
8431       DEBUG('Unexpected Error in Update_shipped_Quantity:', 'Update_shipped_Quantity');
8432       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
8433     END IF;
8434   WHEN OTHERS THEN
8435     x_error_code     := 30;
8436     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8437     ROLLBACK;
8438     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8439 
8440     IF (l_debug = 1) THEN
8441       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
8442     END IF;
8443 END update_shipped_quantity;
8444 
8445 
8446 
8447 PROCEDURE container_nesting(
8448   x_return_status    OUT NOCOPY    VARCHAR2
8449 , x_msg_count        OUT NOCOPY    VARCHAR2
8450 , x_msg_data         OUT NOCOPY    VARCHAR2
8451 , p_organization_id  IN            NUMBER
8452 , p_outermost_lpn_id IN            NUMBER
8453 , p_action_code      IN            VARCHAR2 DEFAULT 'PACK') IS
8454   CURSOR lpn_hierarchy IS
8455     SELECT DISTINCT NVL(parent_lpn_id, outermost_lpn_id)
8456                FROM wms_license_plate_numbers
8457               WHERE organization_id = p_organization_id
8458               AND outermost_lpn_id = p_outermost_lpn_id
8459               AND outermost_lpn_id <> lpn_id;
8460 
8461   CURSOR lpn_childs(l_parent_lpn_id NUMBER) IS
8462     SELECT lpn_id
8463       FROM wms_license_plate_numbers
8464      WHERE organization_id = p_organization_id
8465        AND parent_lpn_id = l_parent_lpn_id;
8466 
8467   CURSOR container_details(p_organization_id NUMBER, l_lpn_id NUMBER) IS
8468     SELECT delivery_detail_id
8469       FROM wsh_delivery_details_ob_grp_v
8470      WHERE organization_id = p_organization_id
8471        AND lpn_id = l_lpn_id
8472        AND released_status = 'X';  -- For LPN reuse ER : 6845650
8473 
8474   l_child_lpn_id      NUMBER;
8475   l_parent_lpn_id     NUMBER;
8476   l_par_del_det_id    NUMBER;
8477   l_child_del_det_id  NUMBER;
8478   l_child_del_det_tab wsh_util_core.id_tab_type;
8479   l_par_del_det_tab   wsh_util_core.id_tab_type;
8480 
8481   l_count             NUMBER                    := 0;
8482   l_parent_counter       NUMBER                    := 0;
8483   l_debug             NUMBER                    := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8484 
8485   l_dummy_number   NUMBER;
8486   l_container_flag VARCHAR2(1);
8487 
8488   l_delivery_id    NUMBER;
8489   l_delivery_planned_flag VARCHAR2(1);
8490   l_action_prms      wsh_interface_ext_grp.del_action_parameters_rectype;
8491   l_delivery_id_tab  wsh_util_core.id_tab_type;
8492   l_delivery_out_rec wsh_interface_ext_grp.del_action_out_rec_type;
8493 
8494   l_trip_action_prms WSH_INTERFACE_EXT_GRP.trip_action_parameters_rectype;
8495   l_trip_out_rec     WSH_INTERFACE_EXT_GRP.tripactionoutrectype;
8496   l_trip_id_tab      wsh_util_core.id_tab_type;
8497 
8498   -- Release 12 : LPN SyncUp
8499   l_wsh_lpn_id_tbl wsh_util_core.id_tab_type;
8500   l_wsh_del_det_id_tbl wsh_util_core.id_tab_type;
8501   l_wsh_action_prms WSH_GLBL_VAR_STRCT_GRP.dd_action_parameters_rec_type;
8502   l_wsh_defaults        WSH_GLBL_VAR_STRCT_GRP.dd_default_parameters_rec_type;
8503   l_wsh_action_out_rec  WSH_GLBL_VAR_STRCT_GRP.dd_action_out_rec_type;
8504 
8505 BEGIN
8506   x_return_status  := fnd_api.g_ret_sts_success;
8507 
8508   IF l_debug = 1 THEN
8509      debug('Entered container_nesting with p_action_code: ' ||p_action_code,
8510        'Container_Nesting');
8511   END IF;
8512 
8513   IF p_action_code = 'PACK' THEN
8514      l_container_flag := 'N';
8515    ELSIF p_action_code = 'UNPACK' THEN
8516      l_container_flag := 'Y';
8517    ELSE
8518      IF l_debug = 1 THEN
8519     debug('Invalid p_action_code passed in','Container_Nesting');
8520      END IF;
8521 
8522      RAISE fnd_api.g_exc_unexpected_error;
8523   END IF;
8524 
8525   --Loop through containers that have nested container
8526   OPEN lpn_hierarchy;
8527 
8528   LOOP
8529     FETCH lpn_hierarchy INTO l_parent_lpn_id;
8530     EXIT WHEN lpn_hierarchy%NOTFOUND;
8531 
8532 
8533 
8534     IF (l_debug = 1) THEN
8535       DEBUG('lpn:' || l_child_lpn_id || ' parent:' || l_parent_lpn_id, 'Container_Nesting');
8536     END IF;
8537 
8538 
8539     --Get the delivery detail id for the parent lpn
8540     OPEN container_details(p_organization_id, l_parent_lpn_id);
8541     FETCH container_details INTO l_par_del_det_id;
8542     CLOSE container_details;
8543 
8544     BEGIN
8545        SELECT 1
8546      INTO l_dummy_number
8547      FROM wms_shipping_transaction_temp
8548      WHERE parent_lpn_id = l_parent_lpn_id
8549      AND ROWNUM = 1;
8550 
8551        IF (l_debug = 1) THEN
8552       DEBUG('lpn_id ' || l_parent_lpn_id || ' attached to an item line', 'Container_Nesting');
8553       debug('No need to unassign from delivery','Container_nesting');
8554        END IF;
8555 
8556     EXCEPTION
8557        WHEN no_data_found THEN
8558       IF (l_debug = 1) THEN
8559          debug('no item line attached to lpn_id: ' || l_parent_lpn_id,'container_nesting');
8560          debug('put it in to unassign from delivery' || 'Container_nesting');
8561       END IF;
8562       l_parent_counter := l_parent_counter + 1;
8563       l_par_del_det_tab(l_parent_counter) := l_par_del_det_id;
8564     END;
8565 
8566     IF l_debug = 1 THEN
8567        debug('Parent lpn delivery_detail_id: ' || l_par_del_det_id, 'Container_Nesting');
8568     END IF;
8569 
8570     l_count  := 0;
8571 
8572     -- Release 12: Use new API
8573     --l_child_del_det_tab.DELETE;
8574     l_wsh_lpn_id_tbl.DELETE;
8575 
8576     --getting all the children containers of current parent container
8577     OPEN lpn_childs(l_parent_lpn_id);
8578     LOOP
8579       FETCH lpn_childs INTO l_child_lpn_id;
8580       EXIT WHEN lpn_childs%NOTFOUND;
8581       l_count                       := l_count + 1;
8582       --Get the delivery detail id for the child lpn
8583       -- Release 12, do not need to get delivery detail ID for child lpns
8584       --  use lpn_ids directly
8585       /*OPEN container_details(p_organization_id, l_child_lpn_id);
8586       FETCH container_details INTO l_child_del_det_id;
8587       CLOSE container_details;
8588       l_child_del_det_tab(l_count)  := l_child_del_det_id;
8589       IF (l_debug = 1) THEN
8590         DEBUG('child det:' || l_child_del_det_tab(l_count) || ' parent: ' || l_par_del_det_id, 'Container_Nesting');
8591       END IF;*/
8592 
8593       l_wsh_lpn_id_tbl(l_count) := l_child_lpn_id;
8594       IF (l_debug = 1) THEN
8595         DEBUG('child lpn:' || l_wsh_lpn_id_tbl(l_count) || ' parent: ' || l_par_del_det_id, 'Container_Nesting');
8596       END IF;
8597 
8598     END LOOP;-- lpn_childs loop
8599 
8600     CLOSE lpn_childs;
8601 
8602     IF l_wsh_lpn_id_tbl.COUNT > 0 THEN
8603       -- Relase 12: LPN SyncUp
8604       -- Replaced call to wsh_container_grp.container_actions
8605       --  with new API call WSH_WMS_LPN_GRP.Delivery_Detail_Action
8606       l_wsh_action_prms.caller                  := 'WMS';
8607       l_wsh_action_prms.action_code             := p_action_code;
8608       l_wsh_action_prms.lpn_rec.organization_id := p_organization_id;
8609       l_wsh_action_prms.lpn_rec.lpn_id          := l_parent_lpn_id;
8610 
8611       IF (l_debug = 1) THEN
8612         DEBUG('Calling WSH_WMS_LPN_GRP.Delivery_Detail_Action with ', 'Container_Nesting');
8613         DEBUG('  Caller: WMS, action_code:'||p_action_code, 'Container_Nesting');
8614         DEBUG('  organization_id:'||p_organization_id, 'Container_Nesting');
8615         DEBUG('  p_action_prms.lpn_rec.lpn_id='||l_parent_lpn_id,  'Container_Nesting');
8616         DEBUG('  p_lpn_id_tbl with '||l_wsh_lpn_id_tbl.count()||' records',  'Container_Nesting');
8617       END IF;
8618 
8619       WSH_WMS_LPN_GRP.Delivery_Detail_Action(
8620         p_api_version_number        => 1.0,
8621         p_init_msg_list             => fnd_api.g_false,
8622         p_commit                    => fnd_api.g_false,
8623         x_return_status             => x_return_status,
8624         x_msg_count                 => x_msg_count,
8625         x_msg_data                  => x_msg_data,
8626         p_lpn_id_tbl                => l_wsh_lpn_id_tbl,
8627         p_del_det_id_tbl            => l_wsh_del_det_id_tbl,
8628         p_action_prms               => l_wsh_action_prms,
8629         x_defaults                  => l_wsh_defaults,
8630         x_action_out_rec            => l_wsh_action_out_rec
8631       );
8632 
8633       IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
8634         --debug('WSH_Container_Grp.Container_Actions returns an exec. error','Pack_Lpns);
8635         IF (l_debug = 1) THEN
8636           DEBUG('Exec. error in packing' || SUBSTR(SQLERRM, 1, 240) || ' ret st:' || x_return_status, 'Container_Nesting');
8637         END IF;
8638 
8639         RAISE fnd_api.g_exc_error;
8640       ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
8641         --debug('WSH_Container_Grp.Container_Actions returns an unexp. error','Pack_Lpns);
8642         IF (l_debug = 1) THEN
8643           DEBUG('Unexp. error in packing', 'Container_Nesting');
8644         END IF;
8645 
8646         RAISE fnd_api.g_exc_unexpected_error;
8647       ELSE
8648         IF (l_debug = 1) THEN
8649           DEBUG('Nesting is success. Outermost LPN ID: ' || p_outermost_lpn_id, 'Container_Nesting');
8650         END IF;
8651       END IF;
8652     END IF;
8653   END LOOP; --lpn_hierarchy cursor loop
8654 
8655   CLOSE lpn_hierarchy;
8656 
8657 
8658   --if unpacking, also unassign the outer LPNs from delivery so that STF will
8659   --show the same state as before load.  The inner-most LPN will not be unassigned
8660   IF p_action_code = 'UNPACK' AND l_par_del_det_tab.COUNT > 0 THEN
8661        BEGIN
8662       --make sure delivery is assigned to the LPNs
8663       --Any of the LPN will do since they should be assigned to the same delivery
8664       SELECT wda.delivery_id
8665         INTO l_delivery_id
8666         FROM wsh_delivery_assignments_v wda
8667         WHERE wda.delivery_detail_id = l_par_del_det_tab(1)
8668         AND wda.delivery_id IS NOT NULL;
8669 
8670       DEBUG('Unpack delivery_id is: ' || l_delivery_id, 'Container_Nesting');
8671 
8672       SELECT wnd.planned_flag
8673         INTO l_delivery_planned_flag
8674         FROM wsh_new_deliveries_ob_grp_v wnd
8675         WHERE wnd.delivery_id = l_delivery_id;
8676 
8677       DEBUG('Unpack delivery planned status: ' || l_delivery_planned_flag,'Container_Nesting');
8678        EXCEPTION
8679       WHEN OTHERS THEN
8680          IF (l_debug = 1) THEN
8681         DEBUG('Unpack other exception..delivery not assign to LPN? ','Container_Nesting');
8682         debug(SQLERRM, 'Container_Nesting');
8683          END IF;
8684          RAISE fnd_api.g_exc_unexpected_error;
8685        END;
8686 
8687        IF l_delivery_planned_flag NOT IN ('Y','F') THEN
8688       DEBUG('Delivery not planned. Just unassign the outer LPN lines','Container_Nesting');
8689       wsh_container_grp.container_actions
8690         (p_api_version => 1.0,
8691          p_init_msg_list => G_TRUE,
8692          p_commit => G_FALSE,
8693          p_validation_level => fnd_api.g_valid_level_full,
8694          x_return_status => x_return_status,
8695          x_msg_count => x_msg_count,
8696          x_msg_data => x_msg_data,
8697          p_detail_tab => l_par_del_det_tab,
8698          p_delivery_flag => 'Y',
8699          p_action_code => 'UNASSIGN');
8700 
8701 
8702 
8703       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR)
8704         THEN
8705          debug('Unassign from delivery failed' , 'Container_Nesting');
8706          debug('wsh_container_grp.container_actions returned ' ||
8707            x_return_status,'Container_Nesting');
8708          RAISE fnd_api.g_exc_unexpected_error;
8709       END IF;
8710     ELSE
8711       DEBUG('Delivery is planned.','Container_Nesting');
8712       IF l_delivery_planned_flag = 'F' THEN
8713          DEBUG('Unfirming trip first','Container_Nesting');
8714          l_trip_action_prms.caller := 'WMS';
8715          l_trip_action_prms.action_code := 'UNPLAN';
8716 
8717          BEGIN
8718         SELECT DISTINCT trip_id
8719           INTO l_trip_id_tab(1)
8720           FROM wms_shipping_transaction_temp
8721           WHERE delivery_id = l_delivery_id;
8722 
8723         debug('Trip id: ' || l_trip_id_tab(1),'Container_Nesting');
8724          EXCEPTION
8725         WHEN OTHERS THEN
8726            debug('Cannot find trip ID?','Container_Nesting');
8727            debug(SQLERRM,'Container_Nesting');
8728            RAISE fnd_api.g_exc_unexpected_error;
8729          END;
8730 
8731          wsh_interface_ext_grp.trip_action
8732            (p_api_version_number     => 1.0,
8733         p_init_msg_list          => fnd_api.g_false,
8734         p_commit                 => fnd_api.g_false,
8735         p_action_prms            => l_trip_action_prms,
8736         p_entity_id_tab        => l_trip_id_tab,
8737         x_trip_out_rec       => l_trip_out_rec,
8738         x_return_status          => x_return_status,
8739         x_msg_count              => x_msg_count,
8740         x_msg_data               => x_msg_data
8741         );
8742 
8743          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
8744         DEBUG('Unfirming trip failed!','Container_Nesting');
8745         debug('msg_data: ' || x_msg_data,'Container_Nesting');
8746         RAISE fnd_api.g_exc_unexpected_error;
8747          END IF;
8748       END IF;
8749 
8750       l_action_prms.caller := 'WMS';
8751       l_action_prms.action_code := 'UNPLAN';
8752 
8753       l_delivery_id_tab(1) := l_delivery_id;
8754 
8755       DEBUG('Unfirm delivery','Container_Nesting');
8756       wsh_interface_ext_grp.delivery_action
8757         (p_api_version_number     => 1.0,
8758          p_init_msg_list          => fnd_api.g_false,
8759          p_commit                 => fnd_api.g_false,
8760          p_action_prms            => l_action_prms,
8761          p_delivery_id_tab        => l_delivery_id_tab,
8762          x_delivery_out_rec       => l_delivery_out_rec,
8763          x_return_status          => x_return_status,
8764          x_msg_count              => x_msg_count,
8765          x_msg_data               => x_msg_data);
8766 
8767       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
8768          DEBUG('Unfirming delivery failed!','Container_Nesting');
8769          debug('msg_data: ' || x_msg_data,'Container_Nesting');
8770          RAISE fnd_api.g_exc_unexpected_error;
8771       END IF;
8772 
8773       DEBUG('Unassign the lines from delivery','Container_Nesting');
8774       wsh_container_grp.container_actions
8775         (p_api_version => 1.0,
8776          p_init_msg_list => G_TRUE,
8777          p_commit => G_FALSE,
8778          p_validation_level => fnd_api.g_valid_level_full,
8779          x_return_status => x_return_status,
8780          x_msg_count => x_msg_count,
8781          x_msg_data => x_msg_data,
8782          p_detail_tab => l_par_del_det_tab,
8783          p_delivery_flag => 'Y',
8784          p_action_code => 'UNASSIGN');
8785 
8786       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
8787          DEBUG('Unassigning from delivery failed!','Container_Nesting');
8788          debug('msg_data: ' || x_msg_data,'Container_Nesting');
8789          RAISE fnd_api.g_exc_unexpected_error;
8790       END IF;
8791 
8792       IF l_delivery_planned_flag = 'Y' THEN
8793 
8794          l_action_prms.action_code := 'PLAN';
8795          DEBUG('Planned flag is Y need to firm back the delivery','Container_Nesting');
8796          wsh_interface_ext_grp.delivery_action
8797            (p_api_version_number     => 1.0,
8798         p_init_msg_list          => fnd_api.g_false,
8799         p_commit                 => fnd_api.g_false,
8800         p_action_prms            => l_action_prms,
8801         p_delivery_id_tab        => l_delivery_id_tab,
8802         x_delivery_out_rec       => l_delivery_out_rec,
8803         x_return_status          => x_return_status,
8804         x_msg_count              => x_msg_count,
8805         x_msg_data               => x_msg_data);
8806 
8807          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
8808         DEBUG('Firming delivery failed!','Container_Nesting');
8809         debug('msg_data: ' || x_msg_data,'Container_Nesting');
8810         RAISE fnd_api.g_exc_unexpected_error;
8811          END IF;
8812 
8813        else
8814          debug('Planned flag is F. Firm back the trip: ' || l_trip_id_tab(1),'Container_Nesting');
8815          l_trip_action_prms.action_code := 'FIRM';
8816 
8817          wsh_interface_ext_grp.trip_action
8818            (p_api_version_number     => 1.0,
8819         p_init_msg_list          => fnd_api.g_false,
8820         p_commit                 => fnd_api.g_false,
8821         p_action_prms            => l_trip_action_prms,
8822         p_entity_id_tab        => l_trip_id_tab,
8823         x_trip_out_rec       => l_trip_out_rec,
8824         x_return_status          => x_return_status,
8825         x_msg_count              => x_msg_count,
8826         x_msg_data               => x_msg_data
8827         );
8828 
8829          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
8830         DEBUG('Firming trip failed!','Container_Nesting');
8831         debug('msg_data: ' || x_msg_data,'Container_Nesting');
8832         RAISE fnd_api.g_exc_unexpected_error;
8833          END IF;
8834       END IF;
8835        END IF;
8836   END IF;--p_action_code = 'UNPACK'
8837 
8838 EXCEPTION
8839   WHEN fnd_api.g_exc_error THEN
8840     x_return_status  := fnd_api.g_ret_sts_error;
8841     ROLLBACK;
8842     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8843 
8844     IF container_details%ISOPEN THEN
8845       CLOSE container_details;
8846     END IF;
8847 
8848     IF lpn_hierarchy%ISOPEN THEN
8849       CLOSE lpn_hierarchy;
8850     END IF;
8851 
8852     IF (l_debug = 1) THEN
8853       DEBUG('Execution Error in lpn_hiearchy:' || x_msg_data || '-' || SUBSTR(SQLERRM, 1, 240), 'Container_Nesting');
8854     END IF;
8855   WHEN fnd_api.g_exc_unexpected_error THEN
8856     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8857     ROLLBACK;
8858     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8859 
8860     IF (l_debug = 1) THEN
8861       DEBUG('Unexpected Error in lpn_hiearchy:' || SUBSTR(SQLERRM, 1, 240) || SQLCODE, 'Container_Nesting');
8862     END IF;
8863 
8864     IF container_details%ISOPEN THEN
8865       CLOSE container_details;
8866     END IF;
8867 
8868     IF lpn_hierarchy%ISOPEN THEN
8869       CLOSE lpn_hierarchy;
8870     END IF;
8871   WHEN OTHERS THEN
8872     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8873     ROLLBACK;
8874     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8875 
8876     --debug('Unexpected Error in pack_lpns:' || substr(SQLERRM,1,240),9);
8877     IF (l_debug = 1) THEN
8878       DEBUG('When others in lpn_hiearchy:' || SUBSTR(SQLERRM, 1, 240) || SQLCODE, 'Container_Nesting');
8879     END IF;
8880 
8881     IF container_details%ISOPEN THEN
8882       CLOSE container_details;
8883     END IF;
8884 
8885     IF lpn_hierarchy%ISOPEN THEN
8886       CLOSE lpn_hierarchy;
8887     END IF;
8888 END container_nesting;
8889 
8890 PROCEDURE check_order_line_split(
8891   x_return_status OUT NOCOPY    VARCHAR2
8892 , x_msg_count     OUT NOCOPY    NUMBER
8893 , x_msg_data      OUT NOCOPY    VARCHAR2
8894 , x_error_code    OUT NOCOPY    NUMBER
8895 , p_delivery_id   IN            NUMBER
8896 ) IS
8897   CURSOR lpn_cur IS
8898     SELECT   parent_lpn_id
8899            , COUNT(parent_lpn_id) cnt
8900         FROM wms_shipping_transaction_temp
8901        WHERE delivery_id = p_delivery_id
8902     GROUP BY parent_lpn_id;
8903 
8904   l_lpn_cur          lpn_cur%ROWTYPE;
8905   l_par_del_det_id   NUMBER;
8906   l_count_del_assign NUMBER;
8907   lpn_name           VARCHAR2(30);
8908   l_debug            NUMBER            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8909 BEGIN
8910   x_return_status  := fnd_api.g_ret_sts_success;
8911   x_error_code     := NULL;
8912   OPEN lpn_cur;
8913 
8914   LOOP
8915     FETCH lpn_cur INTO l_lpn_cur;
8916     EXIT WHEN lpn_cur%NOTFOUND;
8917 
8918     SELECT delivery_detail_id
8919       INTO l_par_del_det_id
8920       FROM wsh_delivery_details_ob_grp_v
8921      WHERE lpn_id = l_lpn_cur.parent_lpn_id
8922      AND released_status = 'X'; -- For LPN reuse ER : 6845650
8923 
8924     SELECT COUNT(*)
8925       INTO l_count_del_assign
8926       FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd
8927      WHERE wda.parent_delivery_detail_id = l_par_del_det_id
8928        AND wda.delivery_detail_id = wdd.delivery_detail_id
8929        AND NVL(wdd.container_flag, 'N') = 'N';
8930 
8931     IF l_count_del_assign <> l_lpn_cur.cnt THEN
8932       SELECT license_plate_number
8933         INTO lpn_name
8934         FROM wms_license_plate_numbers
8935        WHERE lpn_id = l_lpn_cur.parent_lpn_id;
8936 
8937       fnd_message.set_name('WMS', 'WMS_ORDER_LINE_SPLIT');
8938       fnd_message.set_token('CONTAINER_NAME', lpn_name);
8939       fnd_msg_pub.ADD;
8940       x_error_code  := 7;
8941       RAISE fnd_api.g_exc_error;
8942 
8943       --dbms_output.put_line('Contents of the container '||lpn_name||' are split in order management');
8944       IF (l_debug = 1) THEN
8945         DEBUG('Contents of the container ' || lpn_name || ' are split in order management', 'CHECK_ORDER_LINE_SPLIT');
8946       END IF;
8947     END IF;
8948   END LOOP;
8949 
8950   CLOSE lpn_cur;
8951 EXCEPTION
8952   WHEN fnd_api.g_exc_error THEN
8953     x_return_status  := fnd_api.g_ret_sts_error;
8954 
8955     IF lpn_cur%ISOPEN THEN
8956       CLOSE lpn_cur;
8957     END IF;
8958 
8959     IF (l_debug = 1) THEN
8960       DEBUG('In Exception (expected error) - E ', 'CHECK_ORDER_LINE_SPLIT');
8961     END IF;
8962 
8963     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8964   WHEN fnd_api.g_exc_unexpected_error THEN
8965     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8966 
8967     IF lpn_cur%ISOPEN THEN
8968       CLOSE lpn_cur;
8969     END IF;
8970 
8971     IF (l_debug = 1) THEN
8972       DEBUG('In UnException (unexpected error) - U ', 'CHECK_ORDER_LINE_SPLIT');
8973     END IF;
8974 
8975     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8976   WHEN OTHERS THEN
8977     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8978 
8979     IF lpn_cur%ISOPEN THEN
8980       CLOSE lpn_cur;
8981     END IF;
8982 
8983     IF (l_debug = 1) THEN
8984       DEBUG('In Exception (When others) - U ', 'CHECK_ORDER_LINE_SPLIT');
8985     END IF;
8986 
8987     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8988 END check_order_line_split;
8989 
8990 PROCEDURE check_missing_item_cur(
8991   p_delivery_id     IN            NUMBER
8992 , p_dock_door_id    IN            NUMBER
8993 , p_organization_id IN            NUMBER
8994 , x_return_status   OUT NOCOPY    VARCHAR2
8995 , x_missing_count   OUT NOCOPY    NUMBER
8996 ) IS
8997   l_count NUMBER;
8998   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8999 BEGIN
9000   x_missing_count  := 0;
9001   x_return_status  := fnd_api.g_ret_sts_success;
9002 
9003   SELECT COUNT(*)
9004     INTO l_count
9005     FROM wsh_delivery_details_ob_grp_v wdd, wsh_delivery_assignments_v wda, mtl_system_items_kfv msik
9006    WHERE wda.delivery_detail_id = wdd.delivery_detail_id
9007      AND NVL(wdd.container_flag, 'N') = 'N'
9008      AND wda.delivery_id = p_delivery_id
9009      AND wdd.inventory_item_id = msik.inventory_item_id
9010      AND wdd.organization_id = msik.organization_id
9011      AND(
9012          (wda.parent_delivery_detail_id IS NULL
9013           AND msik.mtl_transactions_enabled_flag <> 'N')
9014          OR wdd.released_status IS NULL
9015          OR wdd.released_status NOT IN('X', 'Y')
9016         );
9017 
9018   IF l_count > 0 THEN
9019     x_missing_count  := l_count;
9020   END IF;
9021 EXCEPTION
9022   WHEN NO_DATA_FOUND THEN
9023     l_count  := 0;
9024   WHEN OTHERS THEN
9025     x_return_status  := fnd_api.g_ret_sts_unexp_error;
9026 
9027     IF (l_debug = 1) THEN
9028       DEBUG('Unexp-Error in missing item procedure ', 'CHECK_MISSING_ITEM_CUR');
9029     END IF;
9030 END check_missing_item_cur;
9031 
9032 PROCEDURE chk_del_for_direct_ship(
9033   x_return_status OUT NOCOPY    VARCHAR2
9034 , x_msg_count     OUT NOCOPY    NUMBER
9035 , x_msg_data      OUT NOCOPY    VARCHAR2
9036 , p_delivery_id   IN            NUMBER
9037 ) IS
9038   l_flag_n                NUMBER;
9039   l_num                   NUMBER;
9040 
9041   TYPE parent_del_detail_ids IS TABLE OF NUMBER
9042     INDEX BY BINARY_INTEGER;
9043 
9044   l_parent_del_detail_ids parent_del_detail_ids;
9045 
9046   CURSOR parent_del_detail IS
9047     SELECT delivery_detail_id
9048       FROM wsh_delivery_details_ob_grp_v
9049      WHERE lpn_id IN(SELECT parent_lpn_id
9050                        FROM wms_shipping_transaction_temp
9051                       WHERE delivery_id = p_delivery_id
9052                         AND direct_ship_flag = 'Y');
9053 
9054   CURSOR del_detail IS
9055     SELECT parent_delivery_detail_id
9056       FROM wsh_delivery_assignments_v
9057      WHERE delivery_id = p_delivery_id
9058        AND delivery_detail_id NOT IN(SELECT delivery_detail_id
9059                                        FROM wsh_delivery_details_ob_grp_v
9060                                       WHERE lpn_id IN(SELECT parent_lpn_id
9061                                                         FROM wms_shipping_transaction_temp
9062                                                        WHERE delivery_id = p_delivery_id
9063                                                          AND direct_ship_flag = 'Y'));
9064 
9065   l_debug                 NUMBER                := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9066 BEGIN
9067   x_return_status  := fnd_api.g_ret_sts_success;
9068 
9069   BEGIN
9070     SELECT NVL(SUM(DECODE(direct_ship_flag, 'N', 1)), 0) l_flag_n
9071       INTO l_flag_n
9072       FROM wms_shipping_transaction_temp wstt
9073      WHERE wstt.delivery_id = p_delivery_id
9074        AND dock_appoint_flag = 'N';
9075 
9076     IF l_flag_n = 0 THEN
9077       IF (l_debug = 1) THEN
9078         DEBUG('No records in wstt for direct ship flag =N', 'chk_del_for_direct_ship');
9079       END IF;
9080 
9081       RAISE NO_DATA_FOUND;
9082     ELSIF l_flag_n > 0 THEN
9083       IF (l_debug = 1) THEN
9084         DEBUG('Records in wstt for direct ship flag =N', 'chk_del_for_direct_ship');
9085       END IF;
9086 
9087       RAISE fnd_api.g_exc_error;
9088     END IF;
9089   --DBMS_OUTPUT.PUT_LINE('tHERE ARE NO LINES WITH DIRECT_FLAG =N');
9090   EXCEPTION
9091     WHEN NO_DATA_FOUND THEN
9092       BEGIN
9093         SELECT 1
9094           INTO l_num
9095           FROM DUAL
9096          WHERE EXISTS(
9097                  SELECT 1
9098                    FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd
9099                   WHERE wdd.delivery_detail_id = wda.delivery_detail_id
9100                     AND wda.delivery_id = p_delivery_id
9101                     AND wdd.released_status = 'Y'
9102                     AND NVL(container_flag, 'N') = 'N'
9103                     AND NOT EXISTS(
9104                          SELECT 1
9105                            FROM wms_shipping_transaction_temp wstt
9106                           WHERE wstt.delivery_detail_id = wdd.delivery_detail_id
9107                             AND wstt.delivery_id = p_delivery_id
9108                             AND wstt.direct_ship_flag = 'Y'
9109                             AND wstt.dock_appoint_flag = 'N'));
9110 
9111         IF (l_debug = 1) THEN
9112           DEBUG('This delivery has lines from other source othan than direct_ship', 'chk_del_for_direct_ship');
9113         END IF;
9114 
9115         --dbms_output.put_line('This delivery has lines from other source othan than direct_ship');
9116         IF (l_debug = 1) THEN
9117           DEBUG('Checking for line split', 'chk_del_for_direct_ship');
9118         END IF;
9119 
9120         l_parent_del_detail_ids.DELETE;
9121 
9122         FOR l_p_del_detail IN parent_del_detail LOOP
9123           l_parent_del_detail_ids(l_p_del_detail.delivery_detail_id)  := 1;
9124         END LOOP;
9125 
9126         --
9127         FOR l_del_detail IN del_detail LOOP
9128           IF NOT l_parent_del_detail_ids.EXISTS(l_del_detail.parent_delivery_detail_id) THEN
9129             IF (l_debug = 1) THEN
9130               DEBUG(
9131                 'Delivery line ' || l_del_detail.parent_delivery_detail_id || ' is packed in different container'
9132               , 'chk_del_for_direct_ship'
9133               );
9134               DEBUG('Check failed', 'chk_del_for_direct_ship');
9135             END IF;
9136 
9137             RAISE fnd_api.g_exc_error;
9138           END IF;
9139         END LOOP;
9140       EXCEPTION
9141         WHEN NO_DATA_FOUND THEN
9142           IF (l_debug = 1) THEN
9143             DEBUG('This delivery has lines that were loaded through Direct ship ', 'chk_del_for_direct_ship');
9144           END IF;
9145 
9146           NULL;
9147       END;
9148   END;
9149 EXCEPTION
9150   WHEN fnd_api.g_exc_error THEN
9151     x_return_status  := fnd_api.g_ret_sts_error;
9152     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9153   WHEN fnd_api.g_exc_unexpected_error THEN
9154     x_return_status  := fnd_api.g_ret_sts_unexp_error;
9155     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9156   WHEN OTHERS THEN
9157     x_return_status  := fnd_api.g_ret_sts_unexp_error;
9158     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9159 END chk_del_for_direct_ship;
9160 
9161 
9162 --Bug#5262108.This is to determine the qty remaining to pick
9163 -- considering the overship tolerance , existing reservation, etc.
9164 PROCEDURE Get_qty_to_pick(
9165    p_order_header_id  IN   NUMBER,
9166    p_order_line_id    IN   NUMBER,
9167    p_org_id           IN   NUMBER,
9168    x_return_status    OUT  NOCOPY VARCHAR2,
9169    x_prim_qty         OUT  NOCOPY NUMBER,
9170    x_sec_qty          OUT  NOCOPY NUMBER)
9171 IS
9172   l_allowed_flag              VARCHAR2(1);
9173   l_max_quantity_allowed      NUMBER := 0 ;
9174   l_max_sec_qty_allowed       NUMBER := 0 ;
9175   l_avail_req_quantity        NUMBER := 0 ;
9176   l_avail_req_sec_qty         NUMBER := 0 ;
9177   l_staged_qty                NUMBER := 0 ;
9178   l_staged_sec_qty            NUMBER := 0 ;
9179   l_total_resvd_qty           NUMBER := 0 ;
9180   l_total_resvd_sec_qty       NUMBER := 0 ;
9181   l_return_status             VARCHAR2(30);
9182   l_line_set_id               NUMBER  ;
9183   l_debug                     NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9184 BEGIN
9185         x_return_status := fnd_api.g_ret_sts_success;
9186         x_prim_qty := 0;
9187         x_sec_qty  := 0;
9188 
9189         IF (l_debug = 1) THEN
9190                 DEBUG('In Get_qty_to_pick  p_order_header_id '|| p_order_header_id , 'Get_qty_to_pick');
9191                 DEBUG('p_order_line_id '|| p_order_line_id ||',org:'||p_org_id, 'Get_qty_to_pick');
9192         END IF;
9193 
9194          --Call Shipping API to ger the qty remaining to pick
9195 	WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
9196                p_order_line_id         => p_order_line_id,
9197                p_quantity_to_pick      => 0,
9198                p_quantity2_to_pick     => 0,
9199                x_allowed_flag          => l_allowed_flag,
9200                x_max_quantity_allowed  => l_max_quantity_allowed,
9201                x_max_quantity2_allowed => l_max_sec_qty_allowed,
9202                x_avail_req_quantity    => l_avail_req_quantity,
9203                x_avail_req_quantity2   => l_avail_req_sec_qty,
9204                x_return_status         => l_return_status);
9205 
9206 	IF (l_debug = 1) THEN
9207         	DEBUG('l_allowed_flag='||l_allowed_flag,'Get_qty_to_pick');
9208           	DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Get_qty_to_pick');
9209           	DEBUG('l_max_sec_qty_allowed='||l_max_sec_qty_allowed,'Get_qty_to_pick');
9210           	DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Get_qty_to_pick');
9211           	DEBUG('l_avail_req_sec_qty='||l_avail_req_sec_qty,'Get_qty_to_pick');
9212           	DEBUG('l_return_status='||l_return_status,'Get_qty_to_pick');
9213         END IF;
9214 	IF l_return_status = fnd_api.g_ret_sts_error THEN
9215               IF (l_debug = 1) THEN
9216                 DEBUG('Check_Quantity_To_Pick API failed , ERROR ', 'Get_qty_to_pick');
9217               END IF;
9218               RAISE fnd_api.g_exc_error;
9219         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9220               IF (l_debug = 1) THEN
9221                 DEBUG('Check_Quantity_To_Pick API failed,UNEXPECTED ERROR', 'Get_qty_to_pick');
9222               END IF;
9223               RAISE fnd_api.g_exc_unexpected_error;
9224         END IF;
9225 
9226          SELECT nvl(wdd.source_line_set_id,wdd.source_line_id) into l_line_set_id
9227          FROM wsh_delivery_details_ob_grp_v  wdd
9228 	 WHERE wdd.source_header_id = p_order_header_id
9229          AND wdd.source_line_id= p_order_line_id
9230 	 AND rownum<2;
9231 
9232 	 IF (l_debug = 1) THEN
9233         	DEBUG('l_line_set_id/line_id : '  || l_line_set_id,'Get_qty_to_pick');
9234          END IF;
9235 
9236           --Calculate the staged qty for the line set which curr line belongs to.
9237           SELECT NVL(SUM(NVL(picked_quantity,0)),0)
9238                , NVL(SUM(NVL(picked_quantity2,0)),0)
9239             INTO l_staged_qty
9240                , l_staged_sec_qty
9241 	  FROM wsh_delivery_details_ob_grp_v  wdd
9242 	  WHERE source_header_id = p_order_header_id
9243 	  AND released_status='Y'
9244 	  AND nvl(source_line_set_id,source_line_id) =  l_line_set_id;
9245 
9246          IF (l_debug = 1) THEN
9247             DEBUG('l_staged_qty : ' || l_staged_qty,'Get_qty_to_pick');
9248             DEBUG('l_staged_sec_qty : ' || l_staged_sec_qty,'Get_qty_to_pick');
9249          END IF;
9250 
9251           --Calculate the reserved qty for the line set which curr line belongs to.
9252 	  SELECT NVL(SUM(NVL(primary_reservation_quantity,0)),0)
9253 	       , NVL(SUM(NVL(secondary_reservation_quantity,0)),0)
9254             INTO l_total_resvd_qty
9255                , l_total_resvd_sec_qty
9256           FROM mtl_reservations
9257           WHERE organization_id= p_org_id
9258 	  AND nvl(staged_flag,'N') = 'Y'
9259 	  AND demand_source_line_id in ( SELECT source_line_id
9260                                          FROM wsh_delivery_details_ob_grp_v  wdd
9261 	                                 WHERE source_header_id = p_order_header_id
9262 	                                 AND released_status='Y'
9263 	                                 AND nvl(source_line_set_id,source_line_id) =  l_line_set_id );
9264 
9265 
9266 	 IF (l_debug = 1) THEN
9267         	DEBUG('l_total_resvd_qty : ' || l_total_resvd_qty,'Get_qty_to_pick');
9268         	DEBUG('l_total_resvd_sec_qty : ' || l_total_resvd_sec_qty,'Get_qty_to_pick');
9269          END IF;
9270 
9271        x_prim_qty := NVL(l_max_quantity_allowed,0) + NVL(l_staged_qty,0)     - NVL(l_total_resvd_qty,0);
9272        x_sec_qty  := NVL(l_max_sec_qty_allowed,0)  + NVL(l_staged_sec_qty,0) - NVL(l_total_resvd_sec_qty,0);
9273 
9274 EXCEPTION
9275    WHEN OTHERS THEN
9276         x_return_status := fnd_api.g_ret_sts_unexp_error;
9277         IF (l_debug = 1) THEN
9278            DEBUG('OTHER EXCEPTION: ' || SQLERRM,'Get_qty_to_pick');
9279         END IF;
9280 END Get_qty_to_pick;
9281 
9282 /* Start of Patchset I procedures */
9283 
9284 /* This procedure process all the lines reserved against a LPN passed as parameter.
9285    Following tasks are performed here
9286    (a) table lpn_contents_tab is populated from l_lpn_contents_cur;
9287    (b) lpn material is checked for valid material status
9288    (c) each reserved line is checked if it is booked or not
9289    (d) PJM parameters are checked for each reserved line
9290    (e) delivery grouping rules are checked for each reserved line
9291    (f) User holds are checked for each reserved line
9292    (g) WDS is populated for all the reserved lines
9293  */
9294 
9295 PROCEDURE process_lpn(
9296   p_lpn_id                       IN            NUMBER
9297 , p_org_id                       IN            NUMBER
9298 , p_dock_door_id                 IN            NUMBER
9299 , p_fulfillment_base             IN            VARCHAR2
9300 , x_remaining_qty                OUT NOCOPY    NUMBER
9301 , x_sec_remaining_qty            OUT NOCOPY    NUMBER
9302 , x_num_line_processed           OUT NOCOPY    NUMBER
9303 , x_project_id                   OUT NOCOPY    NUMBER
9304 , x_task_id                      OUT NOCOPY    NUMBER
9305 , x_cross_project_allowed        OUT NOCOPY    VARCHAR2
9306 , x_cross_unit_allowed           OUT NOCOPY    VARCHAR2
9307 , x_group_by_customer_flag       OUT NOCOPY    VARCHAR2
9308 , x_group_by_fob_flag            OUT NOCOPY    VARCHAR2
9309 , x_group_by_freight_terms_flag  OUT NOCOPY    VARCHAR2
9310 , x_group_by_intmed_ship_flag    OUT NOCOPY    VARCHAR2
9311 , x_group_by_ship_method_flag    OUT NOCOPY    VARCHAR2
9312 , x_group_by_ship_to_loc_value   OUT NOCOPY    VARCHAR2
9313 , x_group_by_ship_from_loc_value OUT NOCOPY    VARCHAR2
9314 , x_group_by_customer_value      OUT NOCOPY    VARCHAR2
9315 , x_group_by_fob_value           OUT NOCOPY    VARCHAR2
9316 , x_group_by_freight_terms_value OUT NOCOPY    VARCHAR2
9317 , x_group_by_intmed_value        OUT NOCOPY    VARCHAR2
9318 , x_group_by_ship_method_value   OUT NOCOPY    VARCHAR2
9319 , x_return_status                OUT NOCOPY    VARCHAR2
9320 , x_msg_count                    OUT NOCOPY    NUMBER
9321 , x_msg_data                     OUT NOCOPY    VARCHAR2
9322 ) IS
9323   TYPE processed_line_rec IS RECORD(
9324     header_id            NUMBER
9325   , line_id              NUMBER
9326   , inventory_item_id    NUMBER
9327   , processed_flag       VARCHAR2(1)
9328   , serial_required_flag VARCHAR(1)
9329   , processed_quantity   NUMBER
9330   , sec_processed_qty    NUMBER
9331   );
9332 
9333   TYPE order_line_rec IS RECORD(
9334     header_id            NUMBER
9335   , line_id              NUMBER
9336   , inventory_item_id    NUMBER
9337   , revision             VARCHAR2(3)
9338   , ordered_quantity     NUMBER
9339   , sec_ordered_qty      NUMBER
9340   , project_id           NUMBER
9341   , task_id              NUMBER
9342   , serial_control_code  NUMBER
9343   , end_item_unit_number VARCHAR2(30)
9344   , flow_status_code     VARCHAR2(30)
9345   );
9346 
9347   TYPE processed_lines_tab IS TABLE OF processed_line_rec
9348     INDEX BY BINARY_INTEGER;
9349 
9350   -- local variables
9351   l_sub_reservable_type         NUMBER;
9352   l_lpn_contents_lookup_rec     lpn_contents_lookup_rec;
9353   l_group_by_ship_to_loc_flag   VARCHAR2(1)                                     := 'Y';
9354   l_group_by_ship_from_loc_flag VARCHAR2(1)                                     := 'Y';
9355 
9356   -- Bug# 3464013
9357   TYPE l_lpn_content_cur_rec_typ IS RECORD(
9358     lpn_id                       wms_license_plate_numbers.lpn_id%TYPE
9359   , subinventory_code            wms_license_plate_numbers.subinventory_code%TYPE
9360   , locator_id                   wms_license_plate_numbers.locator_id%TYPE
9361   , inventory_item_id            wms_lpn_contents.inventory_item_id%TYPE
9362   , revision                     wms_lpn_contents.revision%TYPE
9363   , lot_number                   wms_lpn_contents.lot_number%TYPE
9364   , quantity                     wms_lpn_contents.quantity%TYPE
9365   , secondary_quantity           wms_lpn_contents.secondary_quantity%TYPE
9366   , revision_control             VARCHAR2(5)
9367   , lot_control                  VARCHAR2(5)
9368   , serial_control               VARCHAR2(5)
9369   , serial_control_code          mtl_system_items_b.serial_number_control_code%TYPE
9370   , reservable_type              mtl_system_items_b.reservable_type%TYPE
9371   , end_item_unit_number         mtl_serial_numbers.end_item_unit_number%TYPE
9372   , ont_pricing_qty_source       VARCHAR2(30)
9373   );
9374   l_lpn_content_cur_rec         l_lpn_content_cur_rec_typ;
9375 
9376   l_qry_reservation_record      inv_reservation_global.mtl_reservation_rec_type;
9377   l_mtl_reservation_tab         inv_reservation_global.mtl_reservation_tbl_type;
9378 
9379   l_old_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type; --bug#5262108
9380   l_new_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type; --bug#5262108
9381   l_upd_dummy_sn                inv_reservation_global.serial_number_tbl_type;   --bug#5262108
9382 
9383   l_processed_lines_tab         processed_lines_tab;
9384   l_order_line_rec              order_line_rec;
9385   l_start_index                 NUMBER                                          := 1;
9386   l_end_index                   NUMBER                                          := 1;
9387   l_prev_item_id                NUMBER                                          := 0;
9388   l_num_line_processed          NUMBER                                          := 0;
9389   l_oe_order_header_id          NUMBER;
9390   l_return_status               VARCHAR2(30);
9391   l_msg_data                    VARCHAR2(20000);
9392   l_msg_count                   NUMBER;
9393   l_error_code                  NUMBER;
9394   l_temp_index                  NUMBER;
9395   l_mtl_reservation_tab_count   NUMBER;
9396   l_trx_temp_id                 NUMBER;
9397   l_order_line_status           NUMBER;
9398   l_demand_source_type_id       NUMBER;
9399 
9400   -- for end unit validation
9401 
9402   TYPE skipped_line_tab IS TABLE OF NUMBER
9403     INDEX BY BINARY_INTEGER;
9404 
9405   l_skipped_line_tab            skipped_line_tab;
9406   l_debug                       NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9407 
9408   -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
9409   l_sql_query                   VARCHAR2(10000);
9410   TYPE l_lpn_contents_cur_type IS REF CURSOR;
9411   l_lpn_contents_cur            l_lpn_contents_cur_type;
9412   l_qty_to_pick                 NUMBER := 0;  --Bug#5262108
9413   l_rsv_count                   NUMBER := NULL; -- 13830442
9414   l_sec_qty_to_pick             NUMBER := 0;
9415   l_dual_uom_item               BOOLEAN := FALSE;
9416 BEGIN
9417   IF (l_debug = 1) THEN
9418     DEBUG('Process_LPN called with parameters : p_lpn_id = ' || p_lpn_id || ' : p_org_id = ' || p_org_id, 'Process_LPN');
9419   END IF;
9420 
9421   x_return_status       := fnd_api.g_ret_sts_success;
9422 
9423   --clear all cached data structures
9424   g_lpn_contents_tab.DELETE;
9425   g_lpn_contents_lookup_tab.DELETE;
9426   g_total_lpn_quantity := 0;
9427   g_total_lpn_sec_qty := 0;
9428   g_checked_delivery_tab.DELETE;
9429   g_fulfillment_base := p_fulfillment_base;
9430   g_matl_stat_checked := FALSE;
9431   l_processed_lines_tab.DELETE;
9432   g_subinventory_code   := NULL;
9433   g_locator_id          := NULL;
9434   g_del_grp_rls_flags.DELETE;
9435   g_del_grp_rls_fld_value.DELETE;
9436   g_del_grp_rls_fld_temp.DELETE;
9437   g_checked_deliveries.DELETE;
9438   l_skipped_line_tab.DELETE;
9439   -- initializa who columns for session
9440   g_creation_date       := SYSDATE;
9441   g_created_by          := fnd_global.user_id;
9442   g_last_updated_by     := g_created_by;
9443   g_last_update_login   := fnd_global.login_id;
9444   g_last_update_date    := g_creation_date;
9445   -- cleaning temp data if the delivery was backordered for this lpn somethimes in past
9446   wms_direct_ship_pvt.cleanup_orphan_rec(p_org_id);
9447 
9448   IF (l_debug = 1) THEN
9449      DEBUG('After cleanup_orphan_rec', 'Process_LPN');
9450      DEBUG('g_fulfillment_base: ' || g_fulfillment_base, 'Process_LPN');
9451   END IF;
9452 
9453   -- check if the sub in which lpn resides is reservable or not
9454   BEGIN
9455     SELECT sub.reservable_type
9456       INTO l_sub_reservable_type
9457       FROM mtl_secondary_inventories sub
9458      WHERE sub.secondary_inventory_name = (SELECT subinventory_code
9459                                              FROM wms_license_plate_numbers
9460                                             WHERE lpn_id = p_lpn_id)
9461        AND organization_id = p_org_id;
9462 
9463     IF (l_debug = 1) THEN
9464       DEBUG('l_sub_reservable_type= ' || l_sub_reservable_type, 'Process_LPN');
9465     END IF;
9466   EXCEPTION
9467     WHEN NO_DATA_FOUND THEN
9468       IF (l_debug = 1) THEN
9469         DEBUG('Exception getting reservable type of sub', 'Process_LPN');
9470       END IF;
9471 
9472       RAISE fnd_api.g_exc_error;
9473   END;
9474 
9475   IF l_sub_reservable_type <> 1 THEN
9476     IF (l_debug = 1) THEN
9477       DEBUG('LPN containes non reservable items', 'Process_LPN');
9478     END IF;
9479 
9480     x_return_status  := fnd_api.g_ret_sts_error;
9481     fnd_message.set_name('WMS', 'WMS_SUB_NON_RESERVABLE');
9482     fnd_msg_pub.ADD;
9483     RETURN;
9484   ELSE
9485     IF (l_debug = 1) THEN
9486       DEBUG('Before getting LPN contents ', 'Process_LPN');
9487     END IF;
9488 
9489     IF(NVL(g_fulfillment_base, 'P') = 'S') THEN
9490         l_sql_query :=
9491         '    SELECT   wlpn.lpn_id ' ||
9492         '           , wlpn.subinventory_code ' ||
9493         '           , wlpn.locator_id ' ||
9494         '           , wlc.inventory_item_id ' ||
9495         '           , wlc.revision   ' ||
9496         '           , wlc.lot_number ' ||
9497         '           , SUM(wlc.primary_quantity) quantity ' ||
9498         '           , SUM(NVL(wlc.secondary_quantity,0)) ' ||
9499         '           , DECODE(msi.revision_qty_control_code, 2, ''TRUE'', ''FALSE'') revision_control ' ||
9500         '           , DECODE(msi.lot_control_code, 2, ''TRUE'', ''FALSE'') lot_control ' ||
9501         '           , DECODE(msi.serial_number_control_code, 1, ''FALSE'', ''TRUE'') serial_control ' ||
9502         '           , msi.serial_number_control_code serial_control_code ' ||
9503         '           , msi.reservable_type ' ||
9504         '           , NULL ' ||
9505         '           , msi.ont_pricing_qty_source ' ||
9506         '        FROM wms_lpn_contents wlc   ' ||
9507         '           , wms_license_plate_numbers wlpn ' ||
9508         '           , mtl_system_items_b msi ' ||
9509         '       WHERE msi.inventory_item_id = wlc.inventory_item_id ' ||
9510         '         AND wlpn.lpn_id = wlc.parent_lpn_id   ' ||
9511         '         AND wlpn.outermost_lpn_id = :p_lpn_id ' ||
9512         '         AND wlpn.organization_id = :p_org_id  ' ||
9513         '         AND msi.organization_id = :p_org_id   ' ||
9514         '    GROUP BY wlpn.lpn_id ' ||
9515         '           , wlpn.subinventory_code ' ||
9516         '           , wlpn.locator_id ' ||
9517         '           , wlc.inventory_item_id ' ||
9518         '           , wlc.revision   ' ||
9519         '           , wlc.lot_number ' ||
9520         '           , DECODE(msi.revision_qty_control_code, 2, ''TRUE'', ''FALSE'') ' ||
9521         '           , DECODE(msi.lot_control_code, 2, ''TRUE'', ''FALSE'')  ' ||
9522         '           , DECODE(msi.serial_number_control_code, 1, ''FALSE'', ''TRUE'')  ' ||
9523         '           , msi.serial_number_control_code ' ||
9524         '           , msi.reservable_type ' ||
9525         '           , msi.ont_pricing_qty_source ' ||
9526         '    ORDER BY wlc.inventory_item_id ';
9527     ELSE
9528     -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
9529         l_sql_query :=
9530         '    SELECT   wlpn.lpn_id ' ||
9531         '           , wlpn.subinventory_code ' ||
9532         '           , wlpn.locator_id ' ||
9533         '           , wlc.inventory_item_id ' ||
9534         '           , wlc.revision ' ||
9535         '           , wlc.lot_number ' ||
9536         '           , NVL(msn.quantity, wlc.primary_quantity) quantity ' ||
9537         '           , NVL(wlc.secondary_quantity,0) ' ||
9538         '           , DECODE(msi.revision_qty_control_code, 2, ''TRUE'', ''FALSE'') revision_control ' ||
9539         '           , DECODE(msi.lot_control_code, 2, ''TRUE'', ''FALSE'') lot_control ' ||
9540         '           , DECODE(msi.serial_number_control_code, 1, ''FALSE'', ''TRUE'') serial_control ' ||
9541         '           , msi.serial_number_control_code serial_control_code ' ||
9542         '           , msi.reservable_type ' ||
9543         '           , msn.end_item_unit_number ' ||
9544         '           , msi.ont_pricing_qty_source ' ||
9545         '        FROM wms_lpn_contents wlc ' ||
9546         '           , wms_license_plate_numbers wlpn ' ||
9547         '           , mtl_system_items_b msi ' ||
9548         '           , (SELECT   lpn_id ' ||
9549         '                     , revision ' ||
9550         '                     , lot_number ' ||
9551         '                     , inventory_item_id ' ||
9552         '                     , end_item_unit_number ' ||
9553         '                     , COUNT(1) quantity ' ||
9554         '                  FROM mtl_serial_numbers ' ||
9555         '                 WHERE lpn_id IN(SELECT lpn_id ' ||
9556         '                                   FROM wms_license_plate_numbers ' ||
9557         '                                  WHERE organization_id = :p_org_id ' ||
9558         '                                    AND outermost_lpn_id = :p_lpn_id) ' ||
9559         '              GROUP BY lpn_id, revision, lot_number, inventory_item_id, end_item_unit_number) msn ' ||
9560         '       WHERE msi.inventory_item_id = wlc.inventory_item_id ' ||
9561         '         AND wlpn.lpn_id = wlc.parent_lpn_id ' ||
9562         '         AND wlpn.outermost_lpn_id = :p_lpn_id ' ||
9563         '         AND wlpn.organization_id = :p_org_id ' ||
9564         '         AND msi.organization_id = :p_org_id ' ||
9565         '         AND msn.lpn_id(+) = wlc.parent_lpn_id ' ||
9566         '         AND msn.inventory_item_id(+) = wlc.inventory_item_id ' ||
9567         '         AND NVL(msn.lot_number(+),''#NULL#'') = NVL(wlc.lot_number,''#NULL#'') ' ||
9568         '         AND NVL(msn.revision(+),''#NULL#'') = NVL(wlc.revision,''#NULL#'') ' ||
9569         '    ORDER BY wlc.inventory_item_id ';
9570     END IF;
9571 
9572     -- This cursor fetches lpn contents data for all the inner lpns packed into outer lpn.
9573     IF((NVL(g_fulfillment_base, 'P') = 'S')) THEN
9574         OPEN l_lpn_contents_cur FOR l_sql_query USING p_lpn_id, p_org_id, p_org_id;
9575     ELSE
9576         OPEN l_lpn_contents_cur FOR l_sql_query USING p_org_id, p_lpn_id, p_lpn_id, p_org_id, p_org_id;
9577     END IF;
9578 
9579     LOOP
9580       FETCH l_lpn_contents_cur INTO l_lpn_content_cur_rec;
9581       EXIT WHEN l_lpn_contents_cur%NOTFOUND;
9582 
9583       -- check if item is no reservable stop processing lpn
9584       IF l_lpn_content_cur_rec.reservable_type <> 1 THEN
9585          IF (l_debug = 1) THEN
9586             DEBUG('LPN containes non reservable items', 'Process_LPN');
9587          END IF;
9588 
9589          x_return_status  := fnd_api.g_ret_sts_error;
9590          fnd_message.set_name('WMS', 'WMS_ITEM_NON_RESERVABLE');
9591          fnd_msg_pub.ADD;
9592          RETURN;
9593       END IF;
9594 
9595       l_end_index                                            := l_lpn_contents_cur%ROWCOUNT;
9596       g_lpn_contents_tab(l_end_index).lpn_id                 := l_lpn_content_cur_rec.lpn_id;
9597       g_lpn_contents_tab(l_end_index).subinventory_code      := l_lpn_content_cur_rec.subinventory_code;
9598       g_lpn_contents_tab(l_end_index).locator_id             := l_lpn_content_cur_rec.locator_id;
9599       g_lpn_contents_tab(l_end_index).inventory_item_id      := l_lpn_content_cur_rec.inventory_item_id;
9600       g_lpn_contents_tab(l_end_index).revision               := l_lpn_content_cur_rec.revision;
9601       g_lpn_contents_tab(l_end_index).lot_number             := l_lpn_content_cur_rec.lot_number;
9602       g_lpn_contents_tab(l_end_index).serial_control_code    := l_lpn_content_cur_rec.serial_control_code;
9603       g_lpn_contents_tab(l_end_index).quantity               := l_lpn_content_cur_rec.quantity;
9604       g_lpn_contents_tab(l_end_index).secondary_quantity     := l_lpn_content_cur_rec.secondary_quantity;
9605       g_lpn_contents_tab(l_end_index).end_item_unit_number   := l_lpn_content_cur_rec.end_item_unit_number;
9606       g_total_lpn_quantity                                   := g_total_lpn_quantity + l_lpn_content_cur_rec.quantity;
9607       IF NVL(l_lpn_content_cur_rec.secondary_quantity,0) > 0 THEN
9608          g_total_lpn_sec_qty := g_total_lpn_sec_qty + l_lpn_content_cur_rec.secondary_quantity;
9609       END IF;
9610 
9611       IF(g_fulfillment_base = 'S' AND NVL(l_lpn_content_cur_rec.secondary_quantity,0) = 0) THEN
9612         IF (l_debug = 1) THEN
9613         DEBUG('Fulfillment base is S but LPN content record has 0 sec - Invalid sec qty for FB=S so stop further processing', 'Process_LPN');
9614         END IF;
9615             x_return_status  := fnd_api.g_ret_sts_error;
9616             fnd_message.set_name('WMS','WMS_INVALID_SEC_QTY');
9617             fnd_msg_pub.ADD;
9618             RETURN;
9619       END IF;
9620 
9621       IF l_lpn_content_cur_rec.serial_control = 'TRUE' THEN
9622         g_lpn_contents_tab(l_end_index).serial_control  := TRUE;
9623       ELSE
9624         g_lpn_contents_tab(l_end_index).serial_control  := FALSE;
9625       END IF;
9626 
9627       IF l_lpn_content_cur_rec.lot_control = 'TRUE' THEN
9628         g_lpn_contents_tab(l_end_index).lot_control  := TRUE;
9629       ELSE
9630         g_lpn_contents_tab(l_end_index).lot_control  := FALSE;
9631       END IF;
9632 
9633       IF l_lpn_content_cur_rec.revision_control = 'TRUE' THEN
9634         g_lpn_contents_tab(l_end_index).revision_control  := TRUE;
9635       ELSE
9636         g_lpn_contents_tab(l_end_index).revision_control  := FALSE;
9637       END IF;
9638 
9639       -- populate l_lpn_contents_lookup_tab
9640       IF l_prev_item_id = 0 THEN
9641         l_prev_item_id  := l_lpn_content_cur_rec.inventory_item_id;
9642       END IF;
9643 
9644       IF (l_prev_item_id <> l_lpn_content_cur_rec.inventory_item_id) THEN
9645         l_start_index   := l_end_index;
9646         l_prev_item_id  := l_lpn_content_cur_rec.inventory_item_id;
9647       END IF;
9648 
9649       g_lpn_contents_lookup_tab(l_prev_item_id).start_index  := l_start_index;
9650       g_lpn_contents_lookup_tab(l_prev_item_id).end_index    := l_end_index;
9651 
9652       -- add current record quantity to g_total_lpn_quantity
9653       IF (l_debug = 1) THEN
9654          DEBUG('LPN_CONTENT_RECORDS', 'Process_LPN');
9655          DEBUG(
9656               'LPN_ID='
9657            || l_lpn_content_cur_rec.lpn_id
9658            || ' :ITEM_ID= '
9659            || l_lpn_content_cur_rec.inventory_item_id
9660            || ' :REVISION= '
9661            || l_lpn_content_cur_rec.revision
9662            || ' :LOT_NUMBER '
9663            || l_lpn_content_cur_rec.lot_number
9664            || ' :QUANTITY= '
9665            || l_lpn_content_cur_rec.quantity
9666            || ' :SEC QUANTITY= '
9667            || l_lpn_content_cur_rec.secondary_quantity
9668            || ' :EIUN= '
9669            || l_lpn_content_cur_rec.end_item_unit_number
9670          , 'Process_LPN'
9671          );
9672       END IF;
9673     END LOOP; -- l_lpn_content_cur
9674 
9675     CLOSE l_lpn_contents_cur;
9676 
9677     IF (l_debug = 1) THEN
9678        DEBUG('Number of LPN content records ' || g_lpn_contents_tab.COUNT, 'Process_LPN');
9679        DEBUG('G_total_lpn_quantity =' || g_total_lpn_quantity, 'Process_LPN');
9680        DEBUG('G_total_lpn_sec_qty =' || g_total_lpn_sec_qty, 'Process_LPN');
9681     END IF;
9682 
9683     IF g_lpn_contents_tab.COUNT = 0 THEN
9684       x_return_status  := fnd_api.g_ret_sts_error;
9685       fnd_message.set_name('WMS', 'WMS_EMPTY_LPN');
9686       fnd_msg_pub.ADD;
9687 
9688       IF (l_debug = 1) THEN
9689         DEBUG('LPN is empty', 'Process_LPN');
9690       END IF;
9691 
9692       RETURN;
9693     END IF;
9694 
9695     IF (l_debug = 1) THEN
9696       DEBUG('After getting LPN contents ', 'Process_LPN');
9697     END IF;
9698 
9699     -- set subinventory and locator
9700     g_subinventory_code      := g_lpn_contents_tab(1).subinventory_code;
9701     g_locator_id             := g_lpn_contents_tab(1).locator_id;
9702 
9703     -- get PJM parameters
9704     BEGIN
9705       SELECT allow_cross_proj_issues
9706            , allow_cross_unitnum_issues
9707         INTO g_cross_project_allowed
9708            , g_cross_unit_allowed
9709         FROM pjm_org_parameters
9710        WHERE organization_id = p_org_id;
9711 
9712       IF (l_debug = 1) THEN
9713         DEBUG('g_cross_project_allowed= ' || g_cross_project_allowed || ' g_cross_unit_allowed= ' || g_cross_unit_allowed, 'Process_LPN');
9714       END IF;
9715     EXCEPTION
9716       WHEN NO_DATA_FOUND THEN
9717         g_cross_project_allowed  := 'Y';
9718         g_cross_unit_allowed     := 'Y';
9719       WHEN OTHERS THEN
9720         IF (l_debug = 1) THEN
9721           DEBUG('Exception getting PJM Parameters ', 'Process_LPN');
9722         END IF;
9723 
9724         x_return_status  := fnd_api.g_ret_sts_error;
9725         RAISE fnd_api.g_exc_error;
9726     END;
9727 
9728     x_cross_project_allowed  := g_cross_project_allowed;
9729     x_cross_unit_allowed     := g_cross_unit_allowed;
9730 
9731     IF (l_debug = 1) THEN
9732       DEBUG('g_cross_project_allowed= ' || g_cross_project_allowed, 'Process_LPN');
9733       DEBUG('g_cross_unit_allowed= ' || g_cross_unit_allowed, 'Process_LPN');
9734     END IF;
9735 
9736     -- get project and task id
9737     IF g_cross_project_allowed = 'N' THEN
9738       BEGIN
9739         SELECT project_id
9740              , task_id
9741           INTO g_project_id
9742              , g_task_id
9743           FROM mtl_item_locations
9744          WHERE inventory_location_id = g_lpn_contents_tab(1).locator_id;
9745 
9746         IF (l_debug = 1) THEN
9747           DEBUG('g_project_id= ' || g_project_id || ' g_task_id= ' || g_task_id, 'Process_LPN');
9748         END IF;
9749       EXCEPTION
9750         WHEN OTHERS THEN
9751           IF (l_debug = 1) THEN
9752             DEBUG('Exception getting Project and Task Ids ', 'Process_LPN');
9753           END IF;
9754 
9755           x_return_status  := fnd_api.g_ret_sts_error;
9756           RAISE fnd_api.g_exc_error;
9757       END; -- get project and task id
9758 
9759       x_project_id  := g_project_id;
9760       x_task_id     := g_task_id;
9761     END IF;
9762 
9763     -- get Delivery Grouping Rule flags
9764     BEGIN
9765       SELECT group_by_customer_flag
9766            , group_by_fob_flag
9767            , group_by_freight_terms_flag
9768            , group_by_intmed_ship_to_flag
9769            , group_by_ship_method_flag
9770         INTO x_group_by_customer_flag
9771            , x_group_by_fob_flag
9772            , x_group_by_freight_terms_flag
9773            , x_group_by_intmed_ship_flag
9774            , x_group_by_ship_method_flag
9775         FROM wsh_shipping_parameters
9776        WHERE organization_id = p_org_id;
9777 
9778       IF (l_debug = 1) THEN
9779         DEBUG('l_group_by_customer_flag= ' || x_group_by_customer_flag, 'Process_LPN');
9780         DEBUG('l_group_by_fob_flag= ' || x_group_by_fob_flag, 'Process_LPN');
9781         DEBUG('l_group_by_freight_terms_flag= ' || x_group_by_freight_terms_flag, 'Process_LPN');
9782         DEBUG('l_group_by_intmed_ship_flag= ' || x_group_by_intmed_ship_flag, 'Process_LPN');
9783         DEBUG('l_group_by_ship_method_flag= ' || x_group_by_ship_method_flag, 'Process_LPN');
9784       END IF;
9785 
9786       -- populate delivery grouping flags table
9787       g_del_grp_rls_flags(1)  := l_group_by_ship_from_loc_flag;
9788       g_del_grp_rls_flags(2)  := l_group_by_ship_to_loc_flag;
9789       g_del_grp_rls_flags(3)  := x_group_by_customer_flag;
9790       g_del_grp_rls_flags(4)  := x_group_by_fob_flag;
9791       g_del_grp_rls_flags(5)  := x_group_by_freight_terms_flag;
9792       g_del_grp_rls_flags(6)  := x_group_by_intmed_ship_flag;
9793       g_del_grp_rls_flags(7)  := x_group_by_ship_method_flag;
9794     EXCEPTION
9795       WHEN NO_DATA_FOUND THEN
9796         IF (l_debug = 1) THEN
9797           DEBUG('No data found for delivery grouping flags', 'Process_LPN');
9798         END IF;
9799 
9800         x_return_status  := fnd_api.g_ret_sts_error;
9801         RAISE fnd_api.g_exc_error;
9802       WHEN OTHERS THEN
9803         IF (l_debug = 1) THEN
9804           DEBUG('Exception getting delivery grouping rule flags', 'Process_LPN');
9805         END IF;
9806 
9807         x_return_status  := fnd_api.g_ret_sts_error;
9808         RAISE fnd_api.g_exc_error;
9809     END;
9810 
9811     -- start process lines reserved against this lpn
9812     IF (l_debug = 1) THEN
9813       DEBUG('Starting processing of reserved lines', 'Process_LPN');
9814     END IF;
9815 
9816     FOR c_index IN 1 .. g_lpn_contents_tab.COUNT LOOP
9817 		 --bug13830442 ,starts
9818 		BEGIN  /*Performance improvement by avoiding query_reservation calls*/
9819 			IF (c_index =1 OR ( g_lpn_contents_tab(c_index).inventory_item_id <> g_lpn_contents_tab(c_index-1).inventory_item_id) ) THEN
9820 				SELECT count(1) INTO  l_rsv_count FROM mtl_reservations mr
9821 				WHERE mr.organization_id=p_org_id
9822 				AND mr.inventory_item_id = g_lpn_contents_tab(c_index).inventory_item_id
9823 				AND mr.supply_source_type_id =inv_reservation_global.g_source_type_inv;
9824 			END IF;
9825 		EXCEPTION
9826 		WHEN OTHERS THEN
9827 			IF (l_debug = 1) THEN
9828 				DEBUG('exception happened while checking existance of MR , so lets do the query_resv api call ', 'Process_LPN');
9829 			END IF;
9830 			l_rsv_count := 1 ;  --exception happened, so lets do the query_resv api call.
9831 		END ;
9832 			IF (l_debug = 1) THEN
9833 			DEBUG('Number of mtl_reservation records for item/org ie  l_rsv_count : '||l_rsv_count, 'Process_LPN');
9834 			END IF;
9835 
9836 		IF l_mtl_reservation_tab.COUNT > 0 THEN
9837 			l_mtl_reservation_tab.DELETE;
9838 		END IF;
9839 
9840     /*If there are no mr record for the item, there is no use of calling query_resv api*/
9841 	IF (l_rsv_count > 0 )  THEN
9842 
9843       -- query reservations for a line
9844       IF (l_debug = 1) THEN
9845         DEBUG('Querying reservations for lpn_contents_rec ' || c_index, 'Process_LPN');
9846       END IF;
9847 
9848       IF inv_cache.set_item_rec(p_org_id, g_lpn_contents_tab(c_index).inventory_item_id) THEN
9849          IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
9850             l_dual_uom_item := TRUE;
9851             IF (l_debug = 1) THEN
9852                DEBUG('Item is dual UOM', 'Process_LPN');
9853             END IF;
9854          ELSE
9855             l_dual_uom_item := FALSE;
9856             IF (l_debug = 1) THEN
9857                DEBUG('Item is NOT dual UOM', 'Process_LPN');
9858             END IF;
9859          END IF;
9860       ELSE
9861          IF (l_debug = 1) THEN
9862             DEBUG('Error from inv_cache.set_item_rec', 'Process_LPN');
9863          END IF;
9864          RAISE fnd_api.g_exc_unexpected_error;
9865       END IF;
9866 
9867       l_qry_reservation_record.inventory_item_id      := g_lpn_contents_tab(c_index).inventory_item_id;
9868       l_qry_reservation_record.revision               := g_lpn_contents_tab(c_index).revision;
9869       l_qry_reservation_record.lot_number             := g_lpn_contents_tab(c_index).lot_number;
9870       l_qry_reservation_record.lpn_id                 := g_lpn_contents_tab(c_index).lpn_id;
9871       l_qry_reservation_record.supply_source_type_id  := inv_reservation_global.g_source_type_inv;
9872 
9873       inv_reservation_pub.query_reservation(
9874         p_api_version_number         => 1.0
9875       , p_init_msg_lst               => fnd_api.g_false
9876       , x_return_status              => l_return_status
9877       , x_msg_count                  => l_msg_count
9878       , x_msg_data                   => l_msg_data
9879       , p_query_input                => l_qry_reservation_record
9880       , p_lock_records               => fnd_api.g_false
9881       , x_mtl_reservation_tbl        => l_mtl_reservation_tab
9882       , x_mtl_reservation_tbl_count  => l_mtl_reservation_tab_count
9883       , x_error_code                 => l_error_code
9884       );
9885 
9886       IF l_return_status = fnd_api.g_ret_sts_error THEN
9887         IF (l_debug = 1) THEN
9888           DEBUG('Reservation query api failed', 'Process_LPN');
9889         END IF;
9890 
9891         RAISE fnd_api.g_exc_error;
9892       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9893         IF (l_debug = 1) THEN
9894           DEBUG('Reservation query api failed', 'Process_LPN');
9895         END IF;
9896 
9897         RAISE fnd_api.g_exc_unexpected_error;
9898       END IF;
9899 	END IF; --bug13830442
9900 
9901       IF (l_debug = 1) THEN
9902          DEBUG('Reservation records found = ' || l_mtl_reservation_tab.COUNT, 'Process_LPN');
9903       END IF;
9904 
9905       IF l_mtl_reservation_tab.COUNT > 0 THEN
9906         FOR resv_index IN 1 .. l_mtl_reservation_tab.COUNT LOOP
9907           --  fetch line data
9908           IF l_mtl_reservation_tab(resv_index).demand_source_type_id NOT IN (2,8) THEN
9909              IF (l_debug = 1) THEN
9910                 DEBUG('Non-Sales Order reservation exists, error out', 'Process_LPN');
9911              END IF;
9912                 fnd_message.set_name('WMS', 'WMS_LINE_NOT_BOOKED');
9913                 fnd_msg_pub.ADD;
9914              RAISE fnd_api.g_exc_error;
9915           END IF;
9916 
9917           BEGIN
9918             SELECT oel.header_id
9919                  , oel.line_id
9920                  , oel.inventory_item_id
9921                  , oel.item_revision
9922                  , DECODE(
9923                      oel.order_quantity_uom
9924                    , msi.primary_uom_code, oel.ordered_quantity
9925                    , GREATEST(
9926                        inv_convert.inv_um_convert(oel.inventory_item_id, NULL,
9927                         msi.organization_id, 5,
9928                         oel.ordered_quantity, oel.order_quantity_uom, msi.primary_uom_code, NULL
9929                        , NULL), 0)
9930                    ) ordered_quantity
9931                  , oel.ordered_quantity2
9932                  , oel.project_id
9933                  , oel.task_id
9934                  , msi.serial_number_control_code
9935                  , oel.end_item_unit_number
9936                  , oel.flow_status_code
9937               INTO l_order_line_rec
9938               FROM oe_order_lines_all oel, mtl_system_items msi
9939              WHERE msi.organization_id = p_org_id
9940                AND oel.inventory_item_id = msi.inventory_item_id
9941                AND line_id = l_mtl_reservation_tab(resv_index).demand_source_line_id;
9942           EXCEPTION
9943             WHEN NO_DATA_FOUND THEN
9944               IF (l_debug = 1) THEN
9945                 DEBUG('No data found for line', 'Process_LPN');
9946               END IF;
9947 
9948               x_return_status  := fnd_api.g_ret_sts_error;
9949               RAISE fnd_api.g_exc_error;
9950             WHEN OTHERS THEN
9951               IF (l_debug = 1) THEN
9952                 DEBUG('Exception getting data for line ', 'Process_LPN');
9953               END IF;
9954 
9955               x_return_status  := fnd_api.g_ret_sts_error;
9956               RAISE fnd_api.g_exc_error;
9957           END;
9958 
9959           IF (l_debug = 1) THEN
9960              DEBUG('Processing line: ', 'Process_LPN');
9961              DEBUG('l_order_line_rec.header_id: ' || l_order_line_rec.header_id, 'Process_LPN');
9962              DEBUG('l_order_line_rec.line_id: ' || l_order_line_rec.line_id, 'Process_LPN');
9963              DEBUG('l_order_line_rec.inventory_item_id: ' || l_order_line_rec.inventory_item_id, 'Process_LPN');
9964              DEBUG('l_order_line_rec.revision: ' || l_order_line_rec.revision, 'Process_LPN');
9965              DEBUG('l_order_line_rec.ordered_quantity: ' || l_order_line_rec.ordered_quantity, 'Process_LPN');
9966              DEBUG('l_order_line_rec.sec_ordered_qty: ' || l_order_line_rec.sec_ordered_qty, 'Process_LPN');
9967              DEBUG('l_order_line_rec.project_id: ' || l_order_line_rec.project_id, 'Process_LPN');
9968              DEBUG('l_order_line_rec.task_id: ' || l_order_line_rec.task_id, 'Process_LPN');
9969              DEBUG('l_order_line_rec.serial_control_code: ' ||
9970                     l_order_line_rec.serial_control_code, 'Process_LPN');
9971              DEBUG('l_order_line_rec.end_item_unit_number: ' ||
9972                     l_order_line_rec.end_item_unit_number, 'Process_LPN');
9973              DEBUG('l_order_line_rec.flow_status_code: ' ||
9974                     l_order_line_rec.flow_status_code, 'Process_LPN');
9975           END IF;
9976 
9977           --Bug 2909327:Changed the call to check wsh_delivery_details instead of oel*/
9978           BEGIN
9979             SELECT 1
9980               INTO l_order_line_status
9981               FROM DUAL
9982              WHERE EXISTS(SELECT delivery_detail_id
9983                             FROM wsh_delivery_details_ob_grp_v
9984                            WHERE released_status IN('R','B')
9985                              AND source_line_id = l_order_line_rec.line_id)
9986                 -- bug 4232713 - Need to consider staged WDD lines for overpicking scenarios
9987                 OR EXISTS(SELECT wdd.delivery_detail_id
9988                             FROM wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
9989                            WHERE wdd.released_status = 'Y'
9990                              AND wdd.source_line_id = wds.order_line_id
9991                              AND wds.order_line_id = l_order_line_rec.line_id);
9992           EXCEPTION
9993             WHEN NO_DATA_FOUND THEN
9994               IF (l_debug = 1) THEN
9995                 DEBUG('Order Line not booked', 'Process_LPN');
9996               END IF;
9997 
9998               x_return_status  := fnd_api.g_ret_sts_error;
9999               fnd_message.set_name('WMS', 'WMS_LINE_NOT_BOOKED');
10000               fnd_msg_pub.ADD;
10001               RETURN;
10002           END;
10003           IF (l_debug = 1) THEN
10004              DEBUG('l_order_line_status: '|| l_order_line_status, 'Process_LPN');
10005              DEBUG('g_cross_project_allowed: '|| g_cross_project_allowed, 'Process_LPN');
10006           END IF;
10007 
10008           -- check PJM Parameters
10009           IF g_cross_project_allowed <> 'Y' THEN
10010             IF g_project_id <> l_order_line_rec.project_id
10011                OR g_task_id <> l_order_line_rec.task_id THEN
10012               IF (l_debug = 1) THEN
10013                 DEBUG('Validation of PJM parameters failed', 'Process_LPN');
10014               END IF;
10015 
10016               x_return_status  := fnd_api.g_ret_sts_error;
10017               fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
10018               fnd_msg_pub.ADD;
10019               RETURN;
10020             END IF;
10021           END IF;
10022 
10023           IF (l_debug = 1) THEN
10024               DEBUG('validate_end_unit_num_at ' ,'Process_LPN');
10025           END IF;
10026           -- check end_item_unit_num
10027           IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(c_index, Trim(l_order_line_rec.end_item_unit_number)) THEN --bug11659908
10028             l_skipped_line_tab(l_order_line_rec.line_id)  := 1;
10029 
10030             IF (l_debug = 1) THEN
10031               DEBUG('line ' || l_order_line_rec.line_id || ' put into skipped line', 'Process_LPN');
10032             END IF;
10033 
10034             GOTO skip_line; -- line
10035           ELSE
10036             IF l_skipped_line_tab.EXISTS(l_order_line_rec.line_id) THEN
10037               l_skipped_line_tab.DELETE(l_order_line_rec.line_id);
10038 
10039               IF (l_debug = 1) THEN
10040                 DEBUG('line ' || l_order_line_rec.line_id || ' removed from skipped lines', 'Process_LPN');
10041               END IF;
10042 
10043               -- check if lpn has enough quantity to satisfy reservation for end unit num
10044               IF g_lpn_contents_tab(c_index).quantity < l_mtl_reservation_tab(resv_index).primary_reservation_quantity THEN
10045                 IF (l_debug = 1) THEN
10046                   DEBUG('LPN quantity is less then the reserved quantity', 'Process_LPN');
10047                 END IF;
10048 
10049                 x_return_status  := fnd_api.g_ret_sts_error;
10050                 fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
10051                 fnd_msg_pub.ADD;
10052                 RETURN;
10053               END IF;
10054             END IF;
10055           END IF;
10056 
10057           -- validate delivery grouping rules
10058           IF NOT wms_direct_ship_pvt.validate_del_grp_rules(l_num_line_processed, l_order_line_rec.header_id, l_order_line_rec.line_id) THEN
10059             IF (l_debug = 1) THEN
10060               DEBUG('Delivery grouping failed', 'Process_LPN');
10061             END IF;
10062 
10063             x_return_status  := fnd_api.g_ret_sts_error;
10064             fnd_message.set_name('WMS', 'WMS_DEL_GRP_FAILED');
10065             fnd_msg_pub.ADD;
10066             RETURN;
10067           END IF;
10068 
10069           -- check delivery for direct ship
10070           IF NOT wms_direct_ship_pvt.validate_del_for_ds(p_lpn_id, p_org_id, p_dock_door_id, l_order_line_rec.header_id
10071                 , l_order_line_rec.line_id) THEN
10072             IF (l_debug = 1) THEN
10073               DEBUG('Checl del for direct ship failed', 'Process_LPN');
10074             END IF;
10075 
10076             x_return_status  := fnd_api.g_ret_sts_error;
10077             fnd_message.set_name('WMS', 'WMS_DEL_LINES_MIX');
10078             fnd_msg_pub.ADD;
10079             RETURN;
10080           END IF;
10081 
10082           IF l_num_line_processed = 0 THEN                                -- needs to be set only once
10083                                            -- set out parameters
10084             x_group_by_ship_from_loc_value  := g_del_grp_rls_fld_temp(1);
10085             x_group_by_ship_to_loc_value    := g_del_grp_rls_fld_temp(2);
10086             x_group_by_customer_value       := g_del_grp_rls_fld_temp(3);
10087             x_group_by_fob_value            := g_del_grp_rls_fld_temp(4);
10088             x_group_by_freight_terms_value  := g_del_grp_rls_fld_temp(5);
10089             x_group_by_intmed_value         := g_del_grp_rls_fld_temp(6);
10090             x_group_by_ship_method_value    := g_del_grp_rls_fld_temp(7);
10091 
10092             IF (l_debug = 1) THEN
10093               DEBUG('Delivery grouping field values set to return parameters', 'Process_LPN');
10094             END IF;
10095           END IF;
10096 
10097           -- check holds
10098           check_holds(
10099             p_order_header_id            => l_order_line_rec.header_id
10100           , p_order_line_id              => l_order_line_rec.line_id
10101           , x_return_status              => l_return_status
10102           , x_msg_count                  => l_msg_count
10103           , x_msg_data                   => l_msg_data
10104           );
10105 
10106           IF l_return_status = fnd_api.g_ret_sts_error THEN
10107             IF (l_debug = 1) THEN
10108               DEBUG('Check Holds Failed', 'Process_LPN');
10109             END IF;
10110 
10111             x_return_status  := fnd_api.g_ret_sts_error;
10112             fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
10113             fnd_msg_pub.ADD;
10114             RETURN;
10115           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10116             IF (l_debug = 1) THEN
10117               DEBUG('Check holds Failed :Unexpected', 'Process_LPN');
10118             END IF;
10119 
10120             x_return_status  := fnd_api.g_ret_sts_error;
10121             fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
10122             fnd_msg_pub.ADD;
10123             RETURN;
10124           ELSE
10125             IF (l_debug = 1) THEN
10126               DEBUG('Check Holds Succeeded', 'Process LPN');
10127             END IF;
10128           END IF;
10129 
10130           IF NVL(OE_DUAL_UOM_UTIL.get_fulfillment_base(l_mtl_reservation_tab(resv_index).demand_source_line_id), 'P')
10131              <> g_fulfillment_base
10132           THEN
10133              -- Mixed fulfillment modes detected, error out
10134              IF (l_debug = 1) THEN
10135                 DEBUG('Cannot mix fulfillment modes', 'Process_LPN');
10136              END IF;
10137              fnd_message.set_name('WMS', 'WMS_LPN_FB_COMINGLE');
10138              fnd_msg_pub.ADD;
10139              RAISE fnd_api.g_exc_error;
10140           END IF;
10141 
10142           -- get oe_order_header_id from sales order header id
10143           inv_salesorder.get_oeheader_for_salesorder(
10144             p_salesorder_id              => l_mtl_reservation_tab(resv_index).demand_source_header_id
10145           , x_oe_header_id               => l_oe_order_header_id
10146           , x_return_status              => l_return_status
10147           );
10148 
10149           IF (l_debug = 1) THEN
10150             DEBUG('l_oe_order_header_id = ' || l_oe_order_header_id, 'Process_LPN');
10151             DEBUG('l_oe_order_line_id = ' || l_mtl_reservation_tab(resv_index).demand_source_line_id, 'Process_LPN');
10152           END IF;
10153 
10154           --Bug5262108. Begin. Check qty to pick considering overship tolerance.
10155           l_return_status := fnd_api.g_ret_sts_success;
10156           WMS_DIRECT_SHIP_PVT.Get_qty_to_pick(
10157               p_order_header_id => l_oe_order_header_id,
10158               p_order_line_id   => l_mtl_reservation_tab(resv_index).demand_source_line_id,
10159               p_org_id          => p_org_id,
10160               x_return_status   => l_return_status,
10161               x_prim_qty        => l_qty_to_pick,
10162               x_sec_qty         => l_sec_qty_to_pick);
10163 
10164           IF l_return_status <> fnd_api.g_ret_sts_success THEN
10165              IF (l_debug = 1) THEN
10166                 DEBUG('Get_qty_to_pick returned error status: ' || l_return_status, 'Process_LPN');
10167              END IF;
10168              RAISE fnd_api.g_exc_unexpected_error;
10169           END IF;
10170 
10171           IF (l_debug = 1) THEN
10172              DEBUG('l_qty_to_pick = ' || l_qty_to_pick, 'Process_LPN');
10173              DEBUG('l_sec_qty_to_pick = ' || l_sec_qty_to_pick, 'Process_LPN');
10174              DEBUG('reservation_id= ' || l_mtl_reservation_tab(resv_index).reservation_id, 'Process_LPN');
10175              DEBUG('primary_reservation_quantity='||l_mtl_reservation_tab(resv_index).primary_reservation_quantity, 'Process_LPN');
10176              DEBUG('secondary_reservation_quantity='||l_mtl_reservation_tab(resv_index).secondary_reservation_quantity, 'Process_LPN');
10177              DEBUG('g_fulfillment_base = ' || g_fulfillment_base, 'Process_LPN');
10178           END IF;
10179 
10180           -- Need to update the reservation.
10181           IF ( (g_fulfillment_base = 'P' AND l_qty_to_pick > 0)
10182                AND (l_mtl_reservation_tab(resv_index).primary_reservation_quantity > l_qty_to_pick) )
10183           THEN
10184              l_mtl_reservation_tab(resv_index).primary_reservation_quantity := l_qty_to_pick;
10185              l_old_upd_resv_rec.reservation_id := l_mtl_reservation_tab(resv_index).reservation_id;
10186              l_new_upd_resv_rec.primary_reservation_quantity := l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10187 
10188              IF l_dual_uom_item THEN
10189                 l_mtl_reservation_tab(resv_index).secondary_reservation_quantity := l_sec_qty_to_pick;
10190                 l_new_upd_resv_rec.secondary_reservation_quantity := l_sec_qty_to_pick;
10191              END IF;
10192 
10193              IF l_mtl_reservation_tab(resv_index).reservation_uom_code = l_mtl_reservation_tab(resv_index).primary_uom_code THEN
10194                 l_new_upd_resv_rec.reservation_quantity := l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10195              ELSIF l_dual_uom_item AND
10196                    l_mtl_reservation_tab(resv_index).reservation_uom_code
10197                      = NVL(l_mtl_reservation_tab(resv_index).secondary_uom_code,'@@@')
10198              THEN
10199                 l_new_upd_resv_rec.reservation_quantity := l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10200              ELSE
10201                 l_new_upd_resv_rec.reservation_quantity :=
10202                       inv_convert.inv_um_convert(
10203                           item_id           => l_mtl_reservation_tab(resv_index).inventory_item_id
10204                         , lot_number        => l_mtl_reservation_tab(resv_index).lot_number
10205                         , organization_id   => l_mtl_reservation_tab(resv_index).organization_id
10206                         , precision         => NULL
10207                         , from_quantity     => l_mtl_reservation_tab(resv_index).primary_reservation_quantity
10208                         , from_unit         => l_mtl_reservation_tab(resv_index).primary_uom_code
10209                         , to_unit           => l_mtl_reservation_tab(resv_index).reservation_uom_code
10210                         , from_name         => NULL
10211                         , to_name           => NULL);
10212                 l_mtl_reservation_tab(resv_index).reservation_quantity := l_new_upd_resv_rec.reservation_quantity;
10213              END IF;
10214 
10215              l_new_upd_resv_rec.staged_flag :='Y';
10216 
10217              inv_reservation_pub.update_reservation(
10218                  p_api_version_number         => 1.0
10219                , p_init_msg_lst               => fnd_api.g_false
10220                , x_return_status              => l_return_status
10221                , x_msg_count                  => l_msg_count
10222                , x_msg_data                   => l_msg_data
10223                , p_original_rsv_rec           => l_old_upd_resv_rec
10224                , p_to_rsv_rec                 => l_new_upd_resv_rec
10225                , p_original_serial_number     => l_upd_dummy_sn
10226                , p_to_serial_number           => l_upd_dummy_sn
10227                , p_validation_flag            => fnd_api.g_true
10228                );
10229           ELSIF g_fulfillment_base = 'S' THEN
10230              IF l_sec_qty_to_pick > 0
10231                 AND (l_mtl_reservation_tab(resv_index).secondary_reservation_quantity > l_sec_qty_to_pick
10232                     OR l_mtl_reservation_tab(resv_index).secondary_reservation_quantity <> g_lpn_contents_tab(c_index).secondary_quantity)
10233              THEN
10234                 l_mtl_reservation_tab(resv_index).secondary_reservation_quantity := LEAST(l_sec_qty_to_pick, g_lpn_contents_tab(c_index).secondary_quantity);
10235 
10236           IF (l_debug = 1) THEN
10237              DEBUG('g_lpn_contents_tab(c_index).quantity = ' || g_lpn_contents_tab(c_index).quantity, 'Process_LPN');
10238              DEBUG('g_lpn_contents_tab(c_index).secondary_quantity = ' || g_lpn_contents_tab(c_index).secondary_quantity, 'Process_LPN');
10239              DEBUG('reservation_id = ' || l_mtl_reservation_tab(resv_index).reservation_id, 'Process_LPN');
10240              DEBUG('primary_reservation_quantity = '||l_mtl_reservation_tab(resv_index).primary_reservation_quantity, 'Process_LPN');
10241              DEBUG('secondary_reservation_quantity = '||l_mtl_reservation_tab(resv_index).secondary_reservation_quantity, 'Process_LPN');
10242           END IF;
10243 
10244                 IF(l_mtl_reservation_tab(resv_index).secondary_reservation_quantity <> g_lpn_contents_tab(c_index).secondary_quantity) THEN
10245                     l_mtl_reservation_tab(resv_index).primary_reservation_quantity := ROUND(((g_lpn_contents_tab(c_index).quantity * l_mtl_reservation_tab(resv_index).secondary_reservation_quantity)
10246                                                                                       /g_lpn_contents_tab(c_index).secondary_quantity), 5);
10247 
10248                     IF (l_debug = 1) THEN
10249                     DEBUG('The sec qty to pick in LPN is same as sec res qty but pri res qty >= LPN pri qty so update res with pri qty ' || l_mtl_reservation_tab(resv_index).primary_reservation_quantity, 'Process_LPN');
10250                     END IF;
10251                 ELSE
10252                     l_mtl_reservation_tab(resv_index).primary_reservation_quantity := NVL(g_lpn_contents_tab(c_index).quantity,0);
10253                 END IF;
10254 
10255                 l_old_upd_resv_rec.reservation_id := l_mtl_reservation_tab(resv_index).reservation_id;
10256                 l_new_upd_resv_rec.secondary_reservation_quantity := l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10257                 l_new_upd_resv_rec.primary_reservation_quantity := l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10258 
10259                 IF l_mtl_reservation_tab(resv_index).reservation_uom_code = l_mtl_reservation_tab(resv_index).secondary_uom_code THEN
10260                    l_new_upd_resv_rec.reservation_quantity := l_new_upd_resv_rec.secondary_reservation_quantity;
10261                 ELSIF l_mtl_reservation_tab(resv_index).reservation_uom_code = l_mtl_reservation_tab(resv_index).primary_uom_code THEN
10262                    l_new_upd_resv_rec.reservation_quantity := l_mtl_reservation_tab(resv_index).primary_reservation_quantity ;
10263                 ELSE
10264                    l_new_upd_resv_rec.reservation_quantity :=
10265                          inv_convert.inv_um_convert(
10266                              item_id           => l_mtl_reservation_tab(resv_index).inventory_item_id
10267                            , lot_number        => l_mtl_reservation_tab(resv_index).lot_number
10268                            , organization_id   => l_mtl_reservation_tab(resv_index).organization_id
10269                            , precision         => NULL
10270                            , from_quantity     => l_mtl_reservation_tab(resv_index).secondary_reservation_quantity
10271                            , from_unit         => l_mtl_reservation_tab(resv_index).secondary_uom_code
10272                            , to_unit           => l_mtl_reservation_tab(resv_index).reservation_uom_code
10273                            , from_name         => NULL
10274                            , to_name           => NULL);
10275                    l_mtl_reservation_tab(resv_index).reservation_quantity := l_new_upd_resv_rec.reservation_quantity;
10276                 END IF;
10277 
10278                 l_new_upd_resv_rec.staged_flag :='Y';
10279 
10280                 inv_reservation_pub.update_reservation(
10281                     p_api_version_number         => 1.0
10282                   , p_init_msg_lst               => fnd_api.g_false
10283                   , x_return_status              => l_return_status
10284                   , x_msg_count                  => l_msg_count
10285                   , x_msg_data                   => l_msg_data
10286                   , p_original_rsv_rec           => l_old_upd_resv_rec
10287                   , p_to_rsv_rec                 => l_new_upd_resv_rec
10288                   , p_original_serial_number     => l_upd_dummy_sn
10289                   , p_to_serial_number           => l_upd_dummy_sn
10290                   , p_validation_flag            => fnd_api.g_true
10291                   );
10292              END IF;
10293           ELSIF ( (g_fulfillment_base = 'P' AND l_qty_to_pick > 0)
10294                   OR (g_fulfillment_base = 'S' AND l_sec_qty_to_pick > 0) )
10295           THEN
10296              inv_staged_reservation_util.update_staged_flag(
10297                  x_return_status              => l_return_status,
10298                  x_msg_count                  => l_msg_count ,
10299                  x_msg_data                   => l_msg_data,
10300                  p_reservation_id             => l_mtl_reservation_tab(resv_index).reservation_id ,
10301                  p_staged_flag                => 'Y'
10302                  );
10303           ELSE
10304              EXIT;
10305           END IF;
10306 
10307           IF l_return_status = fnd_api.g_ret_sts_error THEN
10308              IF (l_debug = 1) THEN
10309                 DEBUG('Update reservation failed for reservation_id ' || l_old_upd_resv_rec.reservation_id, 'Process_LPN');
10310              END IF;
10311              RAISE fnd_api.g_exc_error;
10312           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10313              IF (l_debug = 1) THEN
10314                 DEBUG('Update reservation failed for reservation_id with status U' ||l_old_upd_resv_rec.reservation_id, 'Process_LPN');
10315              END IF;
10316              RAISE fnd_api.g_exc_unexpected_error;
10317           END IF;
10318           --Bug#5262108.ends
10319 
10320           -- update processed quantity
10321           IF l_processed_lines_tab.EXISTS(l_mtl_reservation_tab(resv_index).demand_source_line_id) THEN
10322              -- set processed quantity
10323              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity :=
10324                  l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
10325                + l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10326              IF l_dual_uom_item THEN
10327                 l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).sec_processed_qty :=
10328                     l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).sec_processed_qty
10329                   + l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10330              END IF;
10331           ELSE
10332              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
10333                                 := l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10334              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).header_id
10335                                 := l_oe_order_header_id;
10336              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).line_id
10337                                 := l_mtl_reservation_tab(resv_index).demand_source_line_id;
10338              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).inventory_item_id
10339                                 := l_mtl_reservation_tab(resv_index).inventory_item_id;
10340              IF l_dual_uom_item THEN
10341                 l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).sec_processed_qty
10342                                    := l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10343              END IF;
10344 
10345              IF g_lpn_contents_tab(c_index).serial_control_code = 6 THEN
10346                 l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).serial_required_flag  := 'Y';
10347              ELSE
10348                 l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).serial_required_flag  := 'N';
10349              END IF;
10350              IF (l_debug = 1) THEN
10351                 DEBUG('Serial control code = ' || g_lpn_contents_tab(c_index).serial_control_code, 'Process_LPN');
10352              END IF;
10353 
10354              -- increment the number of lines processed
10355              l_num_line_processed := l_num_line_processed + 1;
10356           END IF;
10357 
10358           -- update lpn_content_rec and g_total_lpn_quantity,g_total_lpn_sec_qty
10359           g_lpn_contents_tab(c_index).quantity :=
10360                g_lpn_contents_tab(c_index).quantity - l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10361           g_total_lpn_quantity := g_total_lpn_quantity - l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
10362 
10363           IF l_dual_uom_item THEN
10364              g_lpn_contents_tab(c_index).secondary_quantity := g_lpn_contents_tab(c_index).secondary_quantity
10365                                                                - l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10366              g_total_lpn_sec_qty := g_total_lpn_sec_qty - l_mtl_reservation_tab(resv_index).secondary_reservation_quantity;
10367           END IF;
10368 
10369           IF ( (g_fulfillment_base = 'P')
10370                AND (l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
10371                     = l_order_line_rec.ordered_quantity) )
10372           THEN
10373              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_flag  := 'Y';
10374           ELSIF( (g_fulfillment_base = 'S')
10375                 AND (NVL(l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).sec_processed_qty,0)
10376                      = NVL(l_order_line_rec.sec_ordered_qty,0)))
10377           THEN
10378              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_flag  := 'Y';
10379           ELSE
10380              l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_flag  := 'N';
10381           END IF;
10382 
10383           IF (l_debug = 1) THEN
10384              DEBUG(
10385                'Processed quantity=' || l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
10386              , 'Process_LPN'
10387              );
10388              DEBUG(
10389                'Sec processed quantity=' || l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).sec_processed_qty
10390              , 'Process_LPN'
10391              );
10392           END IF;
10393 
10394           <<skip_line>>
10395           IF (l_debug = 1) THEN
10396              DEBUG('<<skip_line>> marker', 'Process_LPN');
10397           END IF;
10398         END LOOP; --l_mtl_reservation_tab.count
10399       END IF; -- l_mtl_reservation_tab.count>0
10400     END LOOP;           -- for g_lpn_contents_tab
10401               -- if there is any record in l_skipped_line_tab then fail because for atleat one line
10402               -- end unit validation failed
10403 
10404     IF l_skipped_line_tab.COUNT > 0 THEN
10405       IF (l_debug = 1) THEN
10406         DEBUG('End item unit validation failed', 'Process_LPN');
10407       END IF;
10408 
10409       x_return_status  := fnd_api.g_ret_sts_error;
10410       fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
10411       fnd_msg_pub.ADD;
10412       RETURN;
10413     END IF;
10414 
10415     IF (l_debug = 1) THEN
10416       DEBUG('All reserved lines processed. Num= ' || l_num_line_processed, 'Process_LPN');
10417     END IF;
10418 
10419     x_num_line_processed := l_num_line_processed;
10420     x_remaining_qty      := g_total_lpn_quantity;
10421     x_sec_remaining_qty  := g_total_lpn_sec_qty;
10422 
10423     IF (l_debug = 1) THEN
10424        DEBUG('G_total_lpn_quantity =' || g_total_lpn_quantity, 'Process_LPN');
10425        DEBUG('G_total_lpn_sec_qty  =' || g_total_lpn_sec_qty,  'Process_LPN');
10426     END IF;
10427 
10428     -- get group_id
10429     BEGIN
10430       SELECT mtl_material_transactions_s.NEXTVAL
10431         INTO g_group_id
10432         FROM DUAL;
10433     EXCEPTION
10434       WHEN NO_DATA_FOUND THEN
10435         g_group_id  := 0;
10436     END;
10437 
10438     IF (l_debug = 1) THEN
10439        DEBUG('Group Id= ' || g_group_id, 'Process_LPN');
10440     END IF;
10441 
10442     -- insert data into wds
10443     IF l_num_line_processed > 0 THEN
10444        IF (l_debug = 1) THEN
10445            DEBUG('Inserting records into WDS', 'Process_LPN');
10446        END IF;
10447 
10448        l_temp_index  := l_processed_lines_tab.FIRST;
10449 
10450        WHILE l_temp_index IS NOT NULL LOOP
10451           IF (l_debug = 1) THEN
10452               DEBUG('l_temp_index = ' || l_temp_index, 'Process_LPN');
10453               DEBUG('header_id= ' || l_processed_lines_tab(l_temp_index).header_id, '');
10454               DEBUG('line_id= ' || l_processed_lines_tab(l_temp_index).line_id, '');
10455               DEBUG('processed_flag= ' || l_processed_lines_tab(l_temp_index).processed_flag, '');
10456               DEBUG('processed_quantity= ' || l_processed_lines_tab(l_temp_index).processed_quantity, '');
10457               DEBUG('serial_required_flag= ' || l_processed_lines_tab(l_temp_index).serial_required_flag, '');
10458           END IF;
10459 
10460           IF NOT g_matl_stat_checked THEN
10461              l_demand_source_type_id := get_demand_src_type(l_processed_lines_tab(l_temp_index).header_id);
10462 
10463              IF l_demand_source_type_id = 2 THEN
10464                 wms_direct_ship_pvt.validate_status_lpn_contents(
10465                     x_return_status => l_return_status
10466                   , x_msg_count     => l_msg_count
10467                   , x_msg_data      => l_msg_data
10468                   , p_lpn_id        => p_lpn_id
10469                   , p_org_id        => p_org_id
10470                   , p_trx_type_id   => 52
10471                   );
10472              ELSIF l_demand_source_type_id = 8 THEN
10473                 wms_direct_ship_pvt.validate_status_lpn_contents(
10474                     x_return_status => l_return_status
10475                   , x_msg_count     => l_msg_count
10476                   , x_msg_data      => l_msg_data
10477                   , p_lpn_id        => p_lpn_id
10478                   , p_org_id        => p_org_id
10479                   , p_trx_type_id   => 53
10480                   );
10481              END IF;
10482 
10483              IF l_return_status = fnd_api.g_ret_sts_error THEN
10484                 IF (l_debug = 1) THEN
10485                    DEBUG('Validate Status LPN Contents procedure failed with error status E ', 'Process_LPN');
10486                 END IF;
10487 
10488                 x_return_status  := fnd_api.g_ret_sts_error;
10489                 fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
10490                 fnd_msg_pub.ADD;
10491                 RETURN;
10492              ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10493                 IF (l_debug = 1) THEN
10494                    DEBUG('Validate Status LPN Contents procedure failed with error status U ', 'Process_LPN');
10495                 END IF;
10496 
10497                 x_return_status  := fnd_api.g_ret_sts_error;
10498                 fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
10499                 fnd_msg_pub.ADD;
10500                 RETURN;
10501              END IF;
10502              g_matl_stat_checked := TRUE;
10503           END IF;
10504 
10505           IF (l_processed_lines_tab(l_temp_index).serial_required_flag = 'Y') THEN
10506              BEGIN
10507                 SELECT mtl_material_transactions_s.NEXTVAL
10508                   INTO l_trx_temp_id
10509                   FROM DUAL;
10510              EXCEPTION
10511                 WHEN NO_DATA_FOUND THEN
10512                    IF (l_debug = 1) THEN
10513                       DEBUG('Unable to get transaction_temp_id', 'Process_LPN');
10514                    END IF;
10515 
10516                    x_return_status  := fnd_api.g_ret_sts_error;
10517                    RAISE fnd_api.g_exc_error;
10518              END;
10519           ELSE
10520              l_trx_temp_id  := NULL;
10521           END IF;
10522 
10523           INSERT INTO wms_direct_ship_temp(
10524                       group_id
10525                     , organization_id
10526                     , dock_door_id
10527                     , lpn_id
10528                     , order_header_id
10529                     , order_line_id
10530                     , line_item_id
10531                     , processed_quantity
10532                     , secondary_processed_qty
10533                     , processed_flag
10534                     , serial_required_flag
10535                     , transaction_temp_id
10536                     , creation_date
10537                     , created_by
10538                     , last_update_date
10539                     , last_updated_by
10540                     , last_update_login
10541                     )
10542              VALUES ( g_group_id
10543                     , p_org_id
10544                     , p_dock_door_id
10545                     , p_lpn_id
10546                     , l_processed_lines_tab(l_temp_index).header_id
10547                     , l_processed_lines_tab(l_temp_index).line_id
10548                     , l_processed_lines_tab(l_temp_index).inventory_item_id
10549                     , l_processed_lines_tab(l_temp_index).processed_quantity
10550                     , NVL(l_processed_lines_tab(l_temp_index).sec_processed_qty,0)
10551                     , l_processed_lines_tab(l_temp_index).processed_flag
10552                     , l_processed_lines_tab(l_temp_index).serial_required_flag
10553                     , l_trx_temp_id
10554                     , g_creation_date
10555                     , g_created_by
10556                     , g_last_update_date
10557                     , g_last_updated_by
10558                     , g_last_update_login
10559                     );
10560 
10561           l_temp_index  := l_processed_lines_tab.NEXT(l_temp_index);
10562        END LOOP;
10563     END IF;
10564   END IF; --l_sub_reservable_type
10565 
10566   IF (l_debug = 1) THEN
10567      DEBUG('Completed successfully ', 'Process_LPN');
10568   END IF;
10569 EXCEPTION
10570   WHEN OTHERS THEN
10571     IF (l_debug = 1) THEN
10572        DEBUG('Unexpected error occured: ' || SQLERRM, 'Process_LPN');
10573     END IF;
10574 
10575     x_return_status  := fnd_api.g_ret_sts_unexp_error;
10576     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
10577     fnd_msg_pub.ADD;
10578 END process_lpn;
10579 
10580 /* This procedure creates reservations for a line if it is not there and
10581    inserts processed line record into WDS.
10582 */
10583 
10584 PROCEDURE process_line(
10585   p_lpn_id                 IN            NUMBER
10586 , p_org_id                 IN            NUMBER
10587 , p_dock_door_id           IN            NUMBER
10588 , p_order_header_id        IN            NUMBER
10589 , p_order_line_id          IN            NUMBER
10590 , p_inventory_item_id      IN            NUMBER
10591 , p_revision               IN            VARCHAR2
10592 , p_end_item_unit_number   IN            VARCHAR2
10593 , p_ordered_quantity       IN            NUMBER
10594 , p_sec_ordered_qty        IN            NUMBER
10595 , p_processed_quantity     IN            NUMBER
10596 , p_sec_processed_qty      IN            NUMBER
10597 , p_date_requested         IN            DATE
10598 , x_remaining_quantity     OUT NOCOPY    NUMBER
10599 , x_sec_remaining_qty      OUT NOCOPY    NUMBER
10600 , x_return_status          OUT NOCOPY    VARCHAR2
10601 , x_msg_count              OUT NOCOPY    NUMBER
10602 , x_msg_data               OUT NOCOPY    VARCHAR2
10603 ) IS
10604   -- for query reservation
10605   l_qry_reservation_record    inv_reservation_global.mtl_reservation_rec_type;
10606   l_mtl_reservation_tab       inv_reservation_global.mtl_reservation_tbl_type;
10607   l_mtl_reservation_tab_temp  inv_reservation_global.mtl_reservation_tbl_type;
10608   l_mtl_reservation_tab_count NUMBER;
10609   l_mtl_resv_tab_count_temp   NUMBER;
10610   l_return_status             VARCHAR2(10);
10611   l_msg_count                 NUMBER;
10612   l_msg_data                  VARCHAR2(20000);
10613   l_error_code                NUMBER;
10614   -- genaral
10615   l_resv_rec                  inv_reservation_global.mtl_reservation_rec_type;
10616   l_demand_source_header_id   NUMBER;
10617   l_demand_source_type_id     NUMBER;
10618   l_quantity_reserved         NUMBER                                          := 0;
10619   l_sec_qty_reserved          NUMBER                                          := 0;
10620   l_quantity_to_transfer_resv NUMBER                                          := 0;
10621   l_sec_qty_to_transfer_resv  NUMBER                                          := 0;
10622   l_quantity_create_resv      NUMBER                                          := 0;
10623   l_sec_qty_create_resv       NUMBER                                          := 0;
10624   l_qty_reserved_tmp          NUMBER                                          := 0;
10625   l_sec_qty_res_tmp           NUMBER                                          := 0;
10626   l_create_reservation_flag   VARCHAR2(1)                                     := 'N';
10627   l_reservable_type           NUMBER;
10628   l_processed_quantity        NUMBER                                          := p_processed_quantity;
10629   l_processed_sec_qty         NUMBER                                          := NVL(p_sec_processed_qty,0);
10630   l_start_index               NUMBER;
10631   l_end_index                 NUMBER;
10632   l_lpn_cont_rec              lpn_content_rec;
10633   l_serial_required_flag      VARCHAR2(1)                                     := 'N';
10634   l_trx_temp_id               NUMBER;
10635   l_temp_var                  NUMBER;
10636   -- For quantity tree declaration
10637   l_transactable_qty          NUMBER;
10638   l_sec_transactable_qty      NUMBER;
10639   l_qoh                       NUMBER;
10640   l_sqoh                      NUMBER;
10641   l_rqoh                      NUMBER;
10642   l_srqoh                     NUMBER;
10643   l_qr                        NUMBER;
10644   l_sqr                       NUMBER;
10645   l_qs                        NUMBER;
10646   l_sqs                       NUMBER;
10647   l_atr                       NUMBER;
10648   l_satr                      NUMBER;
10649   l_grade_code                VARCHAR2(150) := NULL;
10650   -- For transfer reservation
10651   l_old_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
10652   l_new_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
10653   l_dummy_sn                  inv_reservation_global.serial_number_tbl_type;
10654   l_new_rsv_id                NUMBER;
10655   l_current_rsv_rem_qty       NUMBER;
10656   l_curr_rsv_rem_sec_qty      NUMBER := 0;
10657   -- For create_reservation
10658   l_reservation_record        inv_reservation_global.mtl_reservation_rec_type;
10659   l_quantity_reserved_tmp     NUMBER;
10660   l_sec_qty_reserved_tmp      NUMBER;
10661   l_reservation_id            NUMBER;
10662   -- For Update reservation
10663   l_upd_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
10664   l_old_upd_resv_rec          inv_reservation_global.mtl_reservation_rec_type;
10665   l_upd_dummy_sn              inv_reservation_global.serial_number_tbl_type;
10666   l_resv_id                   NUMBER;
10667   l_other_resv_qty            NUMBER;
10668   l_other_resv_sec_qty        NUMBER;
10669   l_debug                     NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10670   l_count_wdst                NUMBER; --Bug#4546137.
10671   l_overship_case             VARCHAR2(1) := 'N' ;   --Bug#5262108
10672   l_qty_to_pick               NUMBER; --Bug#5262108.
10673   l_sec_qty_to_pick           NUMBER;
10674   l_dual_uom_item             BOOLEAN := FALSE;
10675   l_deviation_allowed         BOOLEAN := FALSE;
10676   l_lot_conv_exists           BOOLEAN := FALSE;
10677   l_mtlstat_prev_unchk        BOOLEAN := FALSE;
10678   l_dummy_num                 NUMBER;
10679 
10680   --13830442
10681   l_revision_controlled       BOOLEAN;
10682   l_lot_controlled            BOOLEAN;
10683   l_serial_controlled         BOOLEAN;
10684   l_att                       NUMBER;
10685   l_satt                      NUMBER;
10686   l_tree_id                   NUMBER;
10687 
10688   l_lpn_contents_tab_bkp          lpn_contents_tab;
10689 
10690   CURSOR c_lot_conv_exists(
10691      p_organization_id  IN  NUMBER
10692    , p_item_id          IN  NUMBER
10693    , p_lot_number       IN  VARCHAR2
10694    ) IS
10695      SELECT 1 FROM dual
10696       WHERE EXISTS
10697           ( SELECT 1
10698               FROM mtl_lot_uom_class_conversions
10699              WHERE organization_id   = p_organization_id
10700                AND inventory_item_id = p_item_id
10701                AND lot_number        = p_lot_number
10702                AND (disable_date IS NULL or disable_date > SYSDATE)
10703           );
10704 
10705 BEGIN
10706   IF (l_debug = 1) THEN
10707      DEBUG('Procedure Process_Line called with parameters....', 'Process_Line');
10708      DEBUG('p_lpn_id ' || p_lpn_id, 'Process_Line');
10709      DEBUG('p_org_id ' || p_org_id, 'Process_Line');
10710      DEBUG('p_order_header_id ' || p_order_header_id, 'Process_Line');
10711      DEBUG('p_order_line_id ' || p_order_line_id, 'Process_Line');
10712      DEBUG('p_inventory_item_id ' || p_inventory_item_id, 'Process_Line');
10713      DEBUG('p_revision ' || p_revision, 'Process_Line');
10714      DEBUG('p_end_item_unit_number ' || p_end_item_unit_number, 'Process_Line');
10715      DEBUG('p_ordered_quantity ' || p_ordered_quantity, 'Process_Line');
10716      DEBUG('p_sec_ordered_qty ' || p_sec_ordered_qty, 'Process_Line');
10717      DEBUG('p_processed_quantity ' || p_processed_quantity, 'Process_Line');
10718      DEBUG('p_sec_processed_qty ' || p_sec_processed_qty, 'Process_Line');
10719   END IF;
10720 
10721   l_lpn_contents_tab_bkp := g_lpn_contents_tab; --Taking a backup
10722 
10723   -- get start_index and end_index for inventory_item_id in g_lpn_contents_tab
10724   IF g_lpn_contents_lookup_tab.EXISTS(p_inventory_item_id) THEN
10725     l_start_index  := g_lpn_contents_lookup_tab(p_inventory_item_id).start_index;
10726     l_end_index    := g_lpn_contents_lookup_tab(p_inventory_item_id).end_index;
10727 
10728     -- set serial required flag
10729     IF g_lpn_contents_tab(l_start_index).serial_control_code = 6 THEN
10730       l_serial_required_flag  := 'Y';
10731 
10732       IF (l_debug = 1) THEN
10733         DEBUG('Serial control code = ' || g_lpn_contents_tab(l_start_index).serial_control_code, 'Process_Line');
10734       END IF;
10735     ELSE
10736       l_serial_required_flag  := 'N';
10737 
10738       IF (l_debug = 1) THEN
10739         DEBUG('Serial control code = ' || g_lpn_contents_tab(l_start_index).serial_control_code, 'Process_Line');
10740       END IF;
10741     END IF;
10742   ELSE
10743     IF (l_debug = 1) THEN
10744       DEBUG('Line item not found in LPN', 'Process_Line');
10745     END IF;
10746 
10747     x_return_status  := fnd_api.g_ret_sts_error;
10748     fnd_message.set_name('WMS', 'WMS_ITEM_NOT_AVAILABLE');
10749     fnd_msg_pub.ADD;
10750     RETURN;
10751   END IF;
10752 
10753   --ACTION
10754   -- validate end unit
10755   IF NOT wms_direct_ship_pvt.validate_end_unit_num(p_inventory_item_id, Trim(p_end_item_unit_number)) THEN       --bug#11659908
10756     IF (l_debug = 1) THEN
10757       DEBUG('End item unit validation failed', 'Process_Line');
10758     END IF;
10759 
10760     x_return_status  := fnd_api.g_ret_sts_error;
10761     fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
10762     fnd_msg_pub.ADD;
10763     RETURN;
10764   END IF;
10765 
10766   -- checkk delivery for irect ship
10767   IF NOT wms_direct_ship_pvt.validate_del_for_ds(p_lpn_id, p_org_id, p_dock_door_id, p_order_line_id, p_order_header_id) THEN
10768     IF (l_debug = 1) THEN
10769       DEBUG('Checl del for direct ship failed', 'Process_Line');
10770     END IF;
10771 
10772     x_return_status  := fnd_api.g_ret_sts_error;
10773     fnd_message.set_name('WMS', 'WMS_DEL_LINES_MIX');
10774     fnd_msg_pub.ADD;
10775     RETURN;
10776   END IF;
10777 
10778   -- check holds
10779   check_holds(
10780     p_order_header_id            => p_order_header_id
10781   , p_order_line_id              => p_order_line_id
10782   , x_return_status              => l_return_status
10783   , x_msg_count                  => l_msg_count
10784   , x_msg_data                   => l_msg_data
10785   );
10786 
10787   IF l_return_status = fnd_api.g_ret_sts_error THEN
10788     IF (l_debug = 1) THEN
10789       DEBUG('Check Holds Failed', 'Process_Line');
10790     END IF;
10791 
10792     x_return_status  := fnd_api.g_ret_sts_error;
10793     fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
10794     fnd_msg_pub.ADD;
10795     RETURN;
10796   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10797     IF (l_debug = 1) THEN
10798       DEBUG('Check Holds Failed', 'Process_Line');
10799     END IF;
10800 
10801     x_return_status  := fnd_api.g_ret_sts_error;
10802     fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
10803     fnd_msg_pub.ADD;
10804     RETURN;
10805   ELSE
10806     IF (l_debug = 1) THEN
10807       DEBUG('Check Holds Succeeded', 'Process Line');
10808     END IF;
10809   END IF;
10810 
10811   -- get demand source type
10812   l_demand_source_type_id := get_demand_src_type(p_order_header_id);
10813   IF (l_debug = 1) THEN
10814      DEBUG('l_demand_source_type_id = ' || l_demand_source_type_id, 'Process_Line');
10815   END IF;
10816 
10817   IF inv_cache.set_item_rec(p_org_id, p_inventory_item_id) THEN
10818      IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
10819         l_dual_uom_item := TRUE;
10820         IF (l_debug = 1) THEN
10821            DEBUG('Item is dual UOM','Process_Line');
10822         END IF;
10823         IF NVL(inv_cache.item_rec.dual_uom_deviation_high,0) > 0
10824            OR NVL(inv_cache.item_rec.dual_uom_deviation_low,0) > 0
10825         THEN
10826            l_deviation_allowed := TRUE;
10827            IF (l_debug = 1) THEN
10828               DEBUG('Deviation is allowed','Process_Line');
10829            END IF;
10830         ELSE
10831            l_deviation_allowed := FALSE;
10832            IF (l_debug = 1) THEN
10833               DEBUG('Deviation is NOT allowed','Process_Line');
10834            END IF;
10835         END IF;
10836      ELSE
10837         l_dual_uom_item := FALSE;
10838         l_deviation_allowed := FALSE;
10839         IF (l_debug = 1) THEN
10840            DEBUG('Item is NOT dual UOM','Process_Line');
10841         END IF;
10842      END IF;
10843   ELSE
10844      IF (l_debug = 1) THEN
10845         DEBUG('Error from inv_cache.set_item_rec','Process_Line');
10846      END IF;
10847      RAISE fnd_api.g_exc_unexpected_error;
10848   END IF;
10849 
10850   IF NVL(OE_DUAL_UOM_UTIL.get_fulfillment_base(p_order_line_id), 'P') <> g_fulfillment_base
10851   THEN
10852      -- Mixed fulfillment modes detected, error out
10853      IF (l_debug = 1) THEN
10854         DEBUG('Cannot mix fulfillment modes', 'Process_Line');
10855      END IF;
10856      fnd_message.set_name('WMS', 'WMS_LPN_FB_COMINGLE');
10857      fnd_msg_pub.ADD;
10858      RAISE fnd_api.g_exc_error;
10859   END IF;
10860 
10861   IF NOT g_matl_stat_checked THEN
10862      l_mtlstat_prev_unchk := TRUE;
10863      IF l_demand_source_type_id = 2 THEN
10864         wms_direct_ship_pvt.validate_status_lpn_contents(
10865             x_return_status => l_return_status
10866           , x_msg_count     => l_msg_count
10867           , x_msg_data      => l_msg_data
10868           , p_lpn_id        => p_lpn_id
10869           , p_org_id        => p_org_id
10870           , p_trx_type_id   => 52
10871           );
10872      ELSIF l_demand_source_type_id = 8 THEN
10873         wms_direct_ship_pvt.validate_status_lpn_contents(
10874             x_return_status => l_return_status
10875           , x_msg_count     => l_msg_count
10876           , x_msg_data      => l_msg_data
10877           , p_lpn_id        => p_lpn_id
10878           , p_org_id        => p_org_id
10879           , p_trx_type_id   => 53
10880           );
10881      END IF;
10882 
10883      IF l_return_status = fnd_api.g_ret_sts_error THEN
10884         IF (l_debug = 1) THEN
10885            DEBUG('Validate Status LPN Contents procedure failed with error status E ', 'Process_Line');
10886         END IF;
10887 
10888         x_return_status  := fnd_api.g_ret_sts_error;
10889         fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
10890         fnd_msg_pub.ADD;
10891         RETURN;
10892      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10893         IF (l_debug = 1) THEN
10894            DEBUG('Validate Status LPN Contents procedure failed with error status U ', 'Process_Line');
10895         END IF;
10896 
10897         x_return_status  := fnd_api.g_ret_sts_error;
10898         fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
10899         fnd_msg_pub.ADD;
10900         RETURN;
10901      END IF;
10902      g_matl_stat_checked := TRUE;
10903   END IF;
10904 
10905   --Bug#5262108. This to decide if it is overship or not.
10906   --If no WDD is present to be staged, the qty will be zero to
10907   --facilitate overshipping
10908   IF (g_fulfillment_base = 'P' AND p_ordered_quantity = 0 AND p_processed_quantity = 0)
10909      OR ((g_fulfillment_base = 'S') AND (NVL(p_sec_ordered_qty,0) = 0) AND (NVL(p_sec_processed_qty,0) = 0))
10910   THEN
10911      l_overship_case := 'Y';
10912   END IF;
10913 
10914   -- query reservations for header_id, line_id
10915   l_demand_source_header_id                        := inv_salesorder.get_salesorder_for_oeheader(p_order_header_id);
10916   l_qry_reservation_record.demand_source_header_id := l_demand_source_header_id;
10917   l_qry_reservation_record.demand_source_line_id   := p_order_line_id;
10918   l_qry_reservation_record.demand_source_type_id   := l_demand_source_type_id;
10919   l_qry_reservation_record.supply_source_type_id   := inv_reservation_global.g_source_type_inv;
10920 
10921   IF (l_debug = 1) THEN
10922      DEBUG('g_fulfillment_base: ' || g_fulfillment_base, 'Process_Line');
10923      DEBUG('l_overship_case: ' || l_overship_case, 'Process_Line');
10924      DEBUG('Before call to query reservation', 'Process_Line');
10925   END IF;
10926 
10927   inv_reservation_pub.query_reservation(
10928     p_api_version_number         => 1.0
10929   , p_init_msg_lst               => fnd_api.g_false
10930   , x_return_status              => l_return_status
10931   , x_msg_count                  => l_msg_count
10932   , x_msg_data                   => l_msg_data
10933   , p_query_input                => l_qry_reservation_record
10934   , p_lock_records               => fnd_api.g_false
10935   , x_mtl_reservation_tbl        => l_mtl_reservation_tab_temp
10936   , x_mtl_reservation_tbl_count  => l_mtl_resv_tab_count_temp
10937   , x_error_code                 => l_error_code
10938   );
10939 
10940   IF l_return_status = fnd_api.g_ret_sts_error THEN
10941     IF (l_debug = 1) THEN
10942       DEBUG('Query reservation failed', 'Process_Line');
10943     END IF;
10944 
10945     RAISE fnd_api.g_exc_error;
10946   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10947     IF (l_debug = 1) THEN
10948       DEBUG('Query reservation failed', 'Process_Line');
10949     END IF;
10950 
10951     RAISE fnd_api.g_exc_unexpected_error;
10952   END IF;
10953 
10954   IF (l_debug = 1) THEN
10955     DEBUG('After call to query reservation', 'Process_Line');
10956     DEBUG('reservation records before sub and loc filter = ' || l_mtl_resv_tab_count_temp, 'Process_Line');
10957   END IF;
10958 
10959   -- filter reservation records based on sub and locator
10960   l_mtl_reservation_tab_count := 0;
10961 
10962   IF l_mtl_resv_tab_count_temp > 0 THEN
10963      l_other_resv_qty := 0;
10964      IF l_dual_uom_item THEN
10965         l_other_resv_sec_qty := 0;
10966      END IF;
10967 
10968      FOR rec IN 1 .. l_mtl_resv_tab_count_temp LOOP
10969          l_resv_rec  := l_mtl_reservation_tab_temp(rec);
10970 
10971          IF ( (l_resv_rec.subinventory_code = g_subinventory_code OR l_resv_rec.subinventory_code IS NULL)
10972             AND (l_resv_rec.locator_id = g_locator_id OR l_resv_rec.locator_id IS NULL) )
10973          THEN
10974             l_mtl_reservation_tab_count                         := l_mtl_reservation_tab_count + 1;
10975             l_mtl_reservation_tab(l_mtl_reservation_tab_count)  := l_mtl_reservation_tab_temp(rec);
10976          ELSE
10977             l_other_resv_qty := l_other_resv_qty + l_resv_rec.primary_reservation_quantity;
10978             IF l_dual_uom_item THEN
10979                l_other_resv_sec_qty := l_other_resv_sec_qty + l_resv_rec.secondary_reservation_quantity;
10980             END IF;
10981          END IF;
10982      END LOOP; -- for rec
10983   END IF; --l_mtl_reservation_tab_count_temp>0
10984 
10985   IF (l_debug = 1) THEN
10986      DEBUG('reservation records after sub and loc filter = ' || l_mtl_reservation_tab_count, 'Process_Line');
10987      DEBUG('l_other_resv_qty=' || l_other_resv_qty, 'Process_Line');
10988      DEBUG('l_other_resv_sec_qty=' || l_other_resv_sec_qty, 'Process_Line');
10989   END IF;
10990 
10991   -- calculate quantity_reserved, quantity_transfer_resv and quantity_create_resv
10992   IF l_mtl_reservation_tab_count > 0 THEN
10993      FOR resv_rec IN 1 .. l_mtl_reservation_tab_count LOOP
10994          IF l_mtl_reservation_tab(resv_rec).lpn_id IS NOT NULL THEN   /* 3322799 */
10995             IF (l_mtl_reservation_tab(resv_rec).lpn_id = p_lpn_id) THEN
10996                l_quantity_reserved := l_quantity_reserved + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
10997                IF l_dual_uom_item THEN
10998                   l_sec_qty_reserved := l_sec_qty_reserved + l_mtl_reservation_tab(resv_rec).secondary_reservation_quantity;
10999                END IF;
11000             ELSIF l_mtl_reservation_tab(resv_rec).staged_flag = 'Y' THEN --Bug#5262108. Added ELSE part.
11001                l_other_resv_qty := l_other_resv_qty + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
11002                IF l_dual_uom_item THEN
11003                   l_other_resv_sec_qty := l_other_resv_sec_qty
11004                                           + l_mtl_reservation_tab(resv_rec).secondary_reservation_quantity;
11005                END IF;
11006             END IF;
11007          ELSE
11008             l_quantity_to_transfer_resv := l_quantity_to_transfer_resv
11009                                            + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
11010             IF l_dual_uom_item THEN
11011                l_sec_qty_to_transfer_resv := l_sec_qty_to_transfer_resv
11012                                              + l_mtl_reservation_tab(resv_rec).secondary_reservation_quantity;
11013             END IF;
11014          END IF;
11015      END LOOP; --l_mtl_reservation_tab_count
11016 
11017      IF ( ( (g_fulfillment_base = 'P')
11018             AND ((l_quantity_to_transfer_resv + l_quantity_reserved) < p_ordered_quantity) )
11019           OR
11020             ( (g_fulfillment_base = 'S')
11021             AND ((l_sec_qty_to_transfer_resv + l_sec_qty_reserved) < NVL(p_sec_ordered_qty,0)) ) )
11022      THEN
11023         l_quantity_create_resv := (p_ordered_quantity - l_other_resv_qty)
11024                                    - (l_quantity_to_transfer_resv + l_quantity_reserved);
11025         IF l_dual_uom_item THEN
11026            l_sec_qty_create_resv := (p_sec_ordered_qty - l_other_resv_sec_qty)
11027                                      - (l_sec_qty_to_transfer_resv + l_sec_qty_reserved);
11028         END IF;
11029         l_create_reservation_flag := 'Y';
11030 
11031         -- bug5262108. This is because more reservation becoz of overship.
11032         IF ( (g_fulfillment_base = 'P' AND l_quantity_create_resv < 0)
11033              OR (g_fulfillment_base = 'S' AND l_sec_qty_create_resv < 0) )
11034         THEN
11035            l_overship_case := 'Y' ;
11036         END IF;
11037 
11038         IF (l_debug = 1) THEN
11039            DEBUG('l_overship_case: ' || l_overship_case, 'Process_Line');
11040            DEBUG('Reserved quantity is less than the requeated quantity ', 'Process_Line');
11041         END IF;
11042      ELSE
11043         l_quantity_create_resv    := 0;
11044         l_sec_qty_create_resv     := 0;
11045         l_create_reservation_flag := 'N';
11046      END IF;
11047 
11048      IF (l_debug = 1) THEN
11049         DEBUG('l_quantity_reserved: ' || l_quantity_reserved, 'Process_Line');
11050         DEBUG('l_sec_qty_reserved: ' || l_sec_qty_reserved, 'Process_Line');
11051         DEBUG('l_quantity_to_transfer_resv: ' || l_quantity_to_transfer_resv, 'Process_Line');
11052         DEBUG('l_sec_qty_to_transfer_resv: ' || l_sec_qty_to_transfer_resv, 'Process_Line');
11053         DEBUG('l_quantity_create_resv: ' || l_quantity_create_resv, 'Process_Line');
11054         DEBUG('l_sec_qty_create_resv: ' || l_sec_qty_create_resv, 'Process_Line');
11055      END IF;
11056   ELSE -- no reservation found reservation has to be created for all the quantity
11057      IF (l_debug = 1) THEN
11058         DEBUG('No reservation record found', 'Process_Line');
11059      END IF;
11060 
11061      l_quantity_create_resv := p_ordered_quantity - NVL(l_other_resv_qty,0);
11062      IF l_dual_uom_item THEN
11063         l_sec_qty_create_resv := p_sec_ordered_qty - NVL(l_other_resv_sec_qty,0);
11064      END IF;
11065 
11066      l_create_reservation_flag := 'Y';
11067 
11068      IF (l_debug = 1) THEN
11069         DEBUG('l_quantity_create_resv=' || l_quantity_create_resv, 'Process_Line');
11070         DEBUG('l_sec_qty_create_resv=' || l_sec_qty_create_resv, 'Process_Line');
11071      END IF;
11072   END IF;        --l_mtl_reservation_tab_count>0
11073 
11074   -- transfer reservations
11075   IF l_mtl_reservation_tab_count > 0 THEN --{
11076      FOR resv_rec IN 1 .. l_mtl_reservation_tab_count LOOP --{
11077          l_resv_rec  := l_mtl_reservation_tab(resv_rec);
11078 
11079          -- bug 4285681
11080          l_current_rsv_rem_qty := l_resv_rec.primary_reservation_quantity;
11081          IF l_dual_uom_item THEN
11082             l_curr_rsv_rem_sec_qty := l_resv_rec.secondary_reservation_quantity;
11083          END IF;
11084 
11085          IF l_resv_rec.lpn_id IS NULL THEN --{
11086             FOR lpnc_rec IN l_start_index .. l_end_index LOOP --{
11087                 l_lpn_cont_rec  := g_lpn_contents_tab(lpnc_rec);
11088 
11089                 IF ( ( (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity > 0)
11090                        OR (g_fulfillment_base = 'S' AND l_lpn_cont_rec.secondary_quantity > 0) )
11091                      AND (l_resv_rec.revision = l_lpn_cont_rec.revision OR l_resv_rec.revision IS NULL)
11092                      AND (l_resv_rec.lot_number = l_lpn_cont_rec.lot_number OR l_resv_rec.lot_number IS NULL)
11093                      AND wms_direct_ship_pvt.validate_end_unit_num_at(lpnc_rec, Trim(p_end_item_unit_number) ) --bug 11659908
11094                    )
11095                 THEN --{
11096                    inv_quantity_tree_pub.clear_quantity_cache;
11097 
11098 					/* bug 14211350 */
11099 					IF (l_lpn_cont_rec.lot_control=TRUE) THEN
11100 					BEGIN
11101 						SELECT grade_code
11102 						INTO l_grade_code
11103 						FROM mtl_lot_numbers
11104 						WHERE lot_number         =  l_lpn_cont_rec.lot_number
11105 						AND   inventory_item_id  =  l_lpn_cont_rec.inventory_item_id
11106 						AND   organization_id    =  p_org_id;
11107 						IF (l_debug = 1) THEN
11108 							DEBUG('l_grade_code =' || l_grade_code, 'Process_Line');
11109 						END IF;
11110 					EXCEPTION
11111 						WHEN NO_DATA_FOUND THEN
11112 							IF (l_debug = 1) THEN
11113 							DEBUG('Exception getting l_grade_code ', 'Process_Line');
11114 							END IF;
11115 						RAISE fnd_api.g_exc_unexpected_error;
11116 					END;
11117 					END IF;
11118 					/* bug 14211350 */
11119 
11120                    -- query quantity tree
11121                    IF (l_debug = 1) THEN
11122                       DEBUG('Before query quantity tree ', 'Process_Line');
11123                       DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Process_Line');
11124                       DEBUG('p_order_line_id =' || p_order_line_id, 'Process_Line');
11125                       DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Process_Line');
11126                       DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Process_Line');
11127                       DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Process_Line');
11128                       DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Process_Line');
11129                       DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11130                    END IF;
11131 
11132                    inv_quantity_tree_pub.query_quantities(
11133                      p_api_version_number         => 1.0
11134                    , p_init_msg_lst               => fnd_api.g_false
11135                    , x_return_status              => l_return_status
11136                    , x_msg_count                  => l_msg_count
11137                    , x_msg_data                   => l_msg_data
11138                    , p_organization_id            => p_org_id
11139                    , p_inventory_item_id          => p_inventory_item_id
11140                      -- inv_quantity_tree_pub.g_reservation_mode  Changed bug 4128854
11141                    , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
11142                    , p_is_revision_control        => l_lpn_cont_rec.revision_control
11143                    , p_is_lot_control             => l_lpn_cont_rec.lot_control
11144                    , p_is_serial_control          => l_lpn_cont_rec.serial_control
11145                    , p_grade_code                 => l_grade_code
11146                    , p_demand_source_type_id      => l_demand_source_type_id
11147                    , p_demand_source_header_id    => l_demand_source_header_id
11148                    , p_demand_source_line_id      => p_order_line_id
11149                    , p_revision                   => l_lpn_cont_rec.revision
11150                    , p_lot_number                 => l_lpn_cont_rec.lot_number
11151                    , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
11152                    , p_locator_id                 => l_lpn_cont_rec.locator_id
11153                    , x_qoh                        => l_qoh
11154                    , x_rqoh                       => l_rqoh
11155                    , x_qr                         => l_qr
11156                    , x_qs                         => l_qs
11157                    , x_att                        => l_transactable_qty
11158                    , x_atr                        => l_atr
11159                    , x_sqoh                       => l_sqoh
11160                    , x_srqoh                      => l_srqoh
11161                    , x_sqr                        => l_sqr
11162                    , x_sqs                        => l_sqs
11163                    , x_satt                       => l_sec_transactable_qty
11164                    , x_satr                       => l_satr
11165                    , p_lpn_id                     => l_lpn_cont_rec.lpn_id
11166                    );
11167 
11168                    IF l_return_status = fnd_api.g_ret_sts_error THEN
11169                       IF (l_debug = 1) THEN
11170                          DEBUG( 'Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
11171                                 || p_order_header_id
11172                                 || ' '
11173                                 || p_order_line_id
11174                               , 'Process_Line'
11175                               );
11176                       END IF;
11177 
11178                       RAISE fnd_api.g_exc_error;
11179                    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11180                       IF (l_debug = 1) THEN
11181                          DEBUG( 'Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
11182                                 || p_order_header_id
11183                                 || ' '
11184                                 || p_order_line_id
11185                               , 'Process_Line'
11186                               );
11187                       END IF;
11188 
11189                       RAISE fnd_api.g_exc_unexpected_error;
11190                    END IF;
11191 
11192                    IF (l_debug = 1) THEN
11193                       DEBUG('Query tree Return status is ' || l_return_status, 'Process_Line');
11194                       DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Process_Line');
11195                       DEBUG('l_sec_transactable_qty= ' || l_sec_transactable_qty, 'Process_Line');
11196                    END IF;
11197 
11198                    -- bug 4285681
11199                    IF(NVL(l_transactable_qty, 0) > NVL(g_lpn_contents_tab(lpnc_rec).quantity,0)) THEN
11200                     l_transactable_qty :=  g_lpn_contents_tab(lpnc_rec).quantity;
11201                     IF l_dual_uom_item THEN
11202                         l_sec_transactable_qty := g_lpn_contents_tab(lpnc_rec).secondary_quantity;
11203                     END IF;
11204 				   END IF;
11205 
11206                    IF ( (g_fulfillment_base = 'P' AND l_transactable_qty > 0)
11207                         OR (g_fulfillment_base = 'S' AND NVL(l_sec_transactable_qty,0) > 0) ) --{
11208                    THEN
11209                       l_old_resv_rec.reservation_id                := l_resv_rec.reservation_id;
11210                       l_old_upd_resv_rec.reservation_id            := l_resv_rec.reservation_id;
11211 
11212                       IF (l_debug = 1) THEN
11213                          DEBUG('The old reservation_id is ' || l_old_resv_rec.reservation_id, 'Process_Line');
11214                       END IF;
11215 
11216                       l_new_resv_rec.subinventory_code             := g_subinventory_code;
11217                       l_new_resv_rec.locator_id                    := g_locator_id;
11218                       l_new_resv_rec.organization_id               := p_org_id;
11219                       l_new_resv_rec.inventory_item_id             := p_inventory_item_id;
11220                       l_new_resv_rec.lot_number                    := l_lpn_cont_rec.lot_number;
11221                       l_new_resv_rec.revision                      := l_lpn_cont_rec.revision;
11222                       l_new_resv_rec.demand_source_header_id       := l_demand_source_header_id;
11223                       l_new_resv_rec.demand_source_line_id         := p_order_line_id;
11224                       l_new_resv_rec.lpn_id                        := l_lpn_cont_rec.lpn_id;
11225 
11226                    IF (l_debug = 1) THEN
11227                       DEBUG('l_quantity_to_transfer_resv                = ' || l_quantity_to_transfer_resv, 'Process_Line');
11228                       DEBUG('l_sec_qty_to_transfer_resv                 = ' || l_sec_qty_to_transfer_resv, 'Process_Line');
11229                       DEBUG('l_resv_rec.primary_reservation_quantity    = ' || l_resv_rec.primary_reservation_quantity, 'Process_Line');
11230                       DEBUG('l_resv_rec.secondary_reservation_quantity  = ' || l_resv_rec.secondary_reservation_quantity, 'Process_Line');
11231                    END IF;
11232 
11233                     IF((NVL(l_transactable_qty,0) < NVL(l_resv_rec.primary_reservation_quantity,0))
11234                         AND (NVL(l_transactable_qty,0) < NVL(l_quantity_to_transfer_resv,0))) THEN
11235                             l_new_resv_rec.primary_reservation_quantity := l_transactable_qty;
11236                             IF l_dual_uom_item THEN
11237                                 l_new_resv_rec.secondary_reservation_quantity := l_sec_transactable_qty;
11238                             END IF;
11239                     ELSIF ((NVL(l_resv_rec.primary_reservation_quantity,0) < NVL(l_quantity_to_transfer_resv,0))
11240                             AND (NVL(l_resv_rec.primary_reservation_quantity,0) < NVL(l_transactable_qty,0))) THEN
11241                             l_new_resv_rec.primary_reservation_quantity := l_resv_rec.primary_reservation_quantity;
11242                             IF l_dual_uom_item THEN
11243                                 l_new_resv_rec.secondary_reservation_quantity := l_resv_rec.secondary_reservation_quantity;
11244                             END IF;
11245                     ELSE
11246                             l_new_resv_rec.primary_reservation_quantity := l_quantity_to_transfer_resv;
11247                             IF l_dual_uom_item THEN
11248                                 l_new_resv_rec.secondary_reservation_quantity := l_sec_qty_to_transfer_resv;
11249                             END IF;
11250                     END IF;
11251 
11252                     IF (l_debug = 1) THEN
11253                       DEBUG('l_new_resv_rec.primary_reservation_quantity   = ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
11254                       DEBUG('l_new_resv_rec.secondary_reservation_quantity = ' || l_new_resv_rec.secondary_reservation_quantity, 'Process_Line');
11255                     END IF;
11256 
11257                     IF(g_fulfillment_base = 'S') THEN
11258                         IF(NVL(l_new_resv_rec.secondary_reservation_quantity,0) = l_sec_transactable_qty) THEN
11259                             l_new_resv_rec.primary_reservation_quantity := l_transactable_qty;
11260                             IF (l_debug = 1) THEN
11261                             DEBUG('Fulfillment base is S and sec res = LPN sec qty so l_new_resv_rec.primary_reservation_quantity = ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
11262                             END IF;
11263                         ELSE
11264                             l_new_resv_rec.primary_reservation_quantity := ROUND((l_transactable_qty * l_new_resv_rec.secondary_reservation_quantity)/l_sec_transactable_qty, 5);
11265                             IF (l_debug = 1) THEN
11266                             DEBUG('Fulfillment base is S and sec res <> LPN sec qty so l_new_resv_rec.primary_reservation_quantity = ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
11267                             END IF;
11268                         END IF;
11269                             IF(NVL(l_new_resv_rec.primary_reservation_quantity,0) <> NVL(l_resv_rec.primary_reservation_quantity,0)) THEN
11270                                 l_upd_resv_rec.primary_reservation_quantity := l_new_resv_rec.primary_reservation_quantity;
11271                                 inv_reservation_pub.update_reservation(
11272                                   p_api_version_number         => 1.0
11273                                 , p_init_msg_lst               => fnd_api.g_false
11274                                 , x_return_status              => l_return_status
11275                                 , x_msg_count                  => l_msg_count
11276                                 , x_msg_data                   => l_msg_data
11277                                 , p_original_rsv_rec           => l_old_upd_resv_rec
11278                                 , p_to_rsv_rec                 => l_upd_resv_rec
11279                                 , p_original_serial_number     => l_upd_dummy_sn
11280                                 , p_to_serial_number           => l_upd_dummy_sn
11281                                 , p_validation_flag            => fnd_api.g_true
11282                                 , p_over_reservation_flag      => 3
11283                                 );
11284                             END IF;
11285                     END IF;
11286 
11287                       IF l_resv_rec.reservation_uom_code = l_resv_rec.primary_uom_code THEN
11288                          l_new_resv_rec.reservation_quantity := l_new_resv_rec.primary_reservation_quantity;
11289                       ELSIF l_dual_uom_item
11290                             AND l_resv_rec.reservation_uom_code = NVL(l_resv_rec.secondary_uom_code,'@@@')
11291                       THEN
11292                          l_new_resv_rec.reservation_quantity := l_new_resv_rec.secondary_reservation_quantity;
11293                       ELSE
11294                          IF g_fulfillment_base = 'S' THEN
11295                             l_new_resv_rec.reservation_quantity :=
11296                               inv_convert.inv_um_convert(
11297                                   item_id           => p_inventory_item_id
11298                                 , lot_number        => l_resv_rec.lot_number
11299                                 , organization_id   => p_org_id
11300                                 , precision         => NULL
11301                                 , from_quantity     => l_new_resv_rec.secondary_reservation_quantity
11302                                 , from_unit         => l_resv_rec.secondary_uom_code
11303                                 , to_unit           => l_resv_rec.reservation_uom_code
11304                                 , from_name         => NULL
11305                                 , to_name           => NULL);
11306                          ELSE
11307                             l_new_resv_rec.reservation_quantity :=
11308                               inv_convert.inv_um_convert(
11309                                   item_id           => p_inventory_item_id
11310                                 , lot_number        => l_lpn_cont_rec.lot_number
11311                                 , organization_id   => p_org_id
11312                                 , precision         => NULL
11313                                 , from_quantity     => l_new_resv_rec.primary_reservation_quantity
11314                                 , from_unit         => l_resv_rec.primary_uom_code
11315                                 , to_unit           => l_resv_rec.reservation_uom_code
11316                                 , from_name         => NULL
11317                                 , to_name           => NULL);
11318                          END IF;
11319                       END IF;
11320 
11321                       l_new_resv_rec.staged_flag                   := 'Y' ; --Bug#5262108
11322 
11323                       IF (l_debug = 1) THEN
11324                          DEBUG('Before call to transfer reservation ', 'Process_Line');
11325                          DEBUG('l_new_resv_rec.primary_reservation_quantity: ' || l_new_resv_rec.primary_reservation_quantity,
11326                                'Process_Line');
11327                          DEBUG('l_new_resv_rec.secondary_reservation_quantity: ' || l_new_resv_rec.secondary_reservation_quantity,
11328                                'Process_Line');
11329                          DEBUG('l_new_resv_rec.reservation_quantity: ' || l_new_resv_rec.reservation_quantity, 'Process_Line');
11330                       END IF;
11331 
11332                       inv_reservation_pub.transfer_reservation(
11333                           p_api_version_number         => 1.0
11334                         , p_init_msg_lst               => fnd_api.g_true
11335                         , x_return_status              => l_return_status
11336                         , x_msg_count                  => l_msg_count
11337                         , x_msg_data                   => l_msg_data
11338                         , p_is_transfer_supply         => fnd_api.g_true
11339                         , p_original_rsv_rec           => l_old_resv_rec
11340                         , p_to_rsv_rec                 => l_new_resv_rec
11341                         , p_original_serial_number     => l_dummy_sn -- no serial contorl
11342                         , p_to_serial_number           => l_dummy_sn -- no serial control
11343                         , p_validation_flag            => fnd_api.g_true
11344                         , x_to_reservation_id          => l_new_rsv_id
11345                         , p_over_reservation_flag      => 3
11346                         );
11347 
11348                       IF l_return_status = fnd_api.g_ret_sts_error THEN
11349                          IF (l_debug = 1) THEN
11350                             DEBUG('Unexpected error during transfer of Reservations, Line_id '
11351                                    || p_order_line_id || ' LPN_ID= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11352                          END IF;
11353 
11354                          RAISE fnd_api.g_exc_error;
11355                       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11356                          IF (l_debug = 1) THEN
11357                             DEBUG('Unexpected error during transfer of Reservations, Line_id '
11358                                    || p_order_line_id || ' LPN_ID= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11359                          END IF;
11360 
11361                          RAISE fnd_api.g_exc_unexpected_error;
11362                       END IF;
11363 
11364                       IF (l_debug = 1) THEN
11365                          DEBUG('Transfer reservation successful, reservation_id= ' || l_new_rsv_id, 'Process_Line');
11366                          DEBUG('l_current_rsv_rem_qty= ' || l_current_rsv_rem_qty, 'Process_Line');
11367                          DEBUG('l_curr_rsv_rem_sec_qty= ' || l_curr_rsv_rem_sec_qty, 'Process_Line');
11368                          DEBUG('g_lpn_contents_tab(lpnc_rec).quantity= ' || g_lpn_contents_tab(lpnc_rec).quantity,
11369                                'Process_Line');
11370                          DEBUG('g_lpn_contents_tab(lpnc_rec).secondary_quantity= ' || g_lpn_contents_tab(lpnc_rec).secondary_quantity,
11371                                'Process_Line');
11372                          DEBUG('l_processed_quantity= ' || l_processed_quantity, 'Process_Line');
11373                          DEBUG('l_processed_sec_qty= ' || l_processed_sec_qty, 'Process_Line');
11374                          DEBUG('g_total_lpn_quantity= ' || g_total_lpn_quantity, 'Process_Line');
11375                          DEBUG('g_total_lpn_sec_qty= ' || g_total_lpn_sec_qty, 'Process_Line');
11376                       END IF;
11377 
11378                       --update quantities
11379                       l_quantity_to_transfer_resv := l_quantity_to_transfer_resv
11380                                                      - l_new_resv_rec.primary_reservation_quantity;
11381                       -- bug 4285681
11382                       l_current_rsv_rem_qty := l_current_rsv_rem_qty - l_new_resv_rec.primary_reservation_quantity;
11383                       g_lpn_contents_tab(lpnc_rec).quantity := g_lpn_contents_tab(lpnc_rec).quantity
11384                                                                - l_new_resv_rec.primary_reservation_quantity;
11385                       g_total_lpn_quantity := g_total_lpn_quantity - l_new_resv_rec.primary_reservation_quantity;
11386                       l_processed_quantity := l_processed_quantity + l_new_resv_rec.primary_reservation_quantity;
11387 
11388                       IF l_dual_uom_item THEN
11389                          l_sec_qty_to_transfer_resv := l_sec_qty_to_transfer_resv
11390                                                        - l_new_resv_rec.secondary_reservation_quantity;
11391                          l_curr_rsv_rem_sec_qty := l_curr_rsv_rem_sec_qty
11392                                                    - l_new_resv_rec.secondary_reservation_quantity;
11393                          g_lpn_contents_tab(lpnc_rec).secondary_quantity := g_lpn_contents_tab(lpnc_rec).secondary_quantity
11394                                                                             - l_new_resv_rec.secondary_reservation_quantity;
11395                          g_total_lpn_sec_qty := g_total_lpn_sec_qty - l_new_resv_rec.secondary_reservation_quantity;
11396                          l_processed_sec_qty := l_processed_sec_qty + l_new_resv_rec.secondary_reservation_quantity;
11397                       END IF;
11398 
11399                       IF (l_debug = 1) THEN
11400                          DEBUG('l_quantity_to_transfer_resv= ' || l_quantity_to_transfer_resv, 'Process_Line');
11401                          DEBUG('l_current_rsv_rem_qty= ' || l_current_rsv_rem_qty, 'Process_Line');
11402                          DEBUG('g_lpn_contents_tab(lpnc_rec).quantity= ' || g_lpn_contents_tab(lpnc_rec).quantity, 'Process_Line');
11403                          DEBUG('l_new_resv_rec.primary_reservation_quantity= ' || l_new_resv_rec.primary_reservation_quantity,
11404                                'Process_Line');
11405                          DEBUG('l_processed_quantity= ' || l_processed_quantity, 'Process_Line');
11406                          DEBUG('g_total_lpn_quantity= ' || g_total_lpn_quantity, 'Process_Line');
11407                          DEBUG('l_sec_qty_to_transfer_resv= ' || l_sec_qty_to_transfer_resv, 'Process_Line');
11408                          DEBUG('l_curr_rsv_rem_sec_qty= ' || l_curr_rsv_rem_sec_qty, 'Process_Line');
11409                          DEBUG('g_lpn_contents_tab(lpnc_rec).secondary_quantity= ' || g_lpn_contents_tab(lpnc_rec).secondary_quantity,
11410                                'Process_Line');
11411                          DEBUG('l_new_resv_rec.secondary_reservation_quantity= ' || l_new_resv_rec.secondary_reservation_quantity,
11412                                'Process_Line');
11413                          DEBUG('l_processed_sec_qty= ' || l_processed_sec_qty, 'Process_Line');
11414                          DEBUG('g_total_lpn_sec_qty= ' || g_total_lpn_sec_qty, 'Process_Line');
11415                       END IF;
11416 
11417                       -- bug 4285681
11418                       -- if line processed then exit
11419                       IF ( (g_fulfillment_base = 'P')
11420                            AND (l_quantity_to_transfer_resv = 0 OR l_current_rsv_rem_qty <= 0) )
11421                          OR
11422                          ( (g_fulfillment_base = 'S')
11423                            AND (l_sec_qty_to_transfer_resv = 0 OR l_curr_rsv_rem_sec_qty <= 0) )
11424                       THEN
11425                          EXIT;
11426                       END IF; -- l_quantity_to_transfer_resv=0
11427                    END IF; --} l_transactable_qty>0
11428                 END IF; --} lpn_cont_rec
11429             END LOOP; --} l_start_index..l_end_index
11430          END IF; --} lpn_id is null
11431      END LOOP; --} 1..l_mtl_reservation_tab_count
11432   END IF; --} l_mtl_reservation_tab_count>0
11433 
11434   --Bug#5262108.Begin.
11435   --This is to handle overship case. Once the requested qty for the line is satisfied,
11436   --we will try to overship against the line.
11437   IF l_overship_case = 'Y' THEN
11438      l_return_status := fnd_api.g_ret_sts_success;
11439      WMS_DIRECT_SHIP_PVT.Get_qty_to_pick(
11440          p_order_header_id => p_order_header_id,
11441          p_order_line_id   => p_order_line_id,
11442          p_org_id          => p_org_id,
11443          x_return_status   => l_return_status,
11444          x_prim_qty        => l_qty_to_pick,
11445          x_sec_qty         => l_sec_qty_to_pick);
11446 
11447      IF l_return_status <> fnd_api.g_ret_sts_success THEN
11448         IF (l_debug = 1) THEN
11449            DEBUG('Get_qty_to_pick returned error status: ' || l_return_status, 'Process_Line');
11450         END IF;
11451         RAISE fnd_api.g_exc_unexpected_error;
11452      END IF;
11453 
11454 
11455      IF (l_debug = 1) THEN
11456         DEBUG('l_qty_to_pick '||l_qty_to_pick,'Process_Line');
11457         DEBUG('l_sec_qty_to_pick '||l_sec_qty_to_pick,'Process_Line');
11458      END IF;
11459      --Calculate the quantity to create reservation.
11460      IF(g_fulfillment_base = 'P' AND NVL(l_qty_to_pick,0) < NVL(g_total_lpn_quantity, 0))THEN
11461         l_quantity_create_resv := l_qty_to_pick;
11462         IF l_dual_uom_item THEN
11463             l_sec_qty_create_resv  := l_sec_qty_to_pick;
11464         END IF;
11465      ELSIF(g_fulfillment_base = 'S' AND NVL(l_sec_qty_to_pick,0) < NVL(g_total_lpn_sec_qty, 0)) THEN
11466             l_quantity_create_resv := ROUND((l_sec_qty_to_pick * g_total_lpn_quantity)/g_total_lpn_sec_qty,5);
11467             l_sec_qty_create_resv  := l_sec_qty_to_pick;
11468      ELSE
11469         l_quantity_create_resv := g_total_lpn_quantity;
11470         IF l_dual_uom_item THEN
11471             l_sec_qty_create_resv  := g_total_lpn_sec_qty;
11472         END IF;
11473      END IF;
11474 
11475      IF (l_debug = 1) THEN
11476         DEBUG('l_quantity_create_resv '||l_quantity_create_resv ,'Process_Line');
11477         DEBUG('l_sec_qty_create_resv '||l_sec_qty_create_resv ,'Process_Line');
11478      END IF;
11479   END IF;  --Bug#5262108
11480 
11481   -- update or create reservations
11482   IF ( (g_fulfillment_base = 'P' AND l_quantity_create_resv > 0)
11483        OR (g_fulfillment_base = 'S' AND l_sec_qty_create_resv > 0) ) --{
11484   THEN
11485       --Bug 13830442
11486 		inv_quantity_tree_pub.clear_quantity_cache; --Clear qty trees to start with a fresh new one.
11487      FOR lpnc_rec IN l_start_index .. l_end_index LOOP --{
11488          IF (l_debug = 1) THEN
11489             DEBUG('Quantity for create reservation= ' || l_quantity_create_resv, 'Process_Line');
11490             DEBUG('Sec quantity for create reservation= ' || l_sec_qty_create_resv, 'Process_Line');
11491             DEBUG('lpnc_rec=' || lpnc_rec, 'Process_Line');
11492             DEBUG('p_end_item_unit_number=' || p_end_item_unit_number, 'Process_Line');
11493          END IF;
11494 
11495          l_lpn_cont_rec  := g_lpn_contents_tab(lpnc_rec);
11496 
11497          IF l_dual_uom_item AND l_lpn_cont_rec.lot_number IS NOT NULL THEN
11498             OPEN c_lot_conv_exists(p_org_id,p_inventory_item_id,l_lpn_cont_rec.lot_number);
11499             FETCH c_lot_conv_exists INTO l_dummy_num;
11500 
11501             IF c_lot_conv_exists%FOUND THEN
11502                l_lot_conv_exists := TRUE;
11503             ELSE
11504                l_lot_conv_exists := FALSE;
11505             END IF;
11506 			CLOSE c_lot_conv_exists;
11507          ELSE
11508             l_lot_conv_exists := FALSE;
11509          END IF;
11510 
11511          IF ( ( (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity > 0)
11512                 OR (g_fulfillment_base = 'S' AND l_lpn_cont_rec.secondary_quantity > 0) )
11513               AND (p_revision = l_lpn_cont_rec.revision OR p_revision IS NULL)
11514               AND wms_direct_ship_pvt.validate_end_unit_num_at(lpnc_rec, TRIM(p_end_item_unit_number)) --bug 11659908
11515             )
11516          THEN --{
11517             --inv_quantity_tree_pub.clear_quantity_cache;
11518 
11519             -- query quantity tree
11520             IF (l_debug = 1) THEN
11521                DEBUG('Before query quantity tree ', 'Process_Line');
11522                DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Process_Line');
11523                DEBUG('p_order_line_id =' || p_order_line_id, 'Process_Line');
11524                DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Process_Line');
11525                DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Process_Line');
11526                DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Process_Line');
11527                DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Process_Line');
11528                DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11529                DEBUG('l_lpn_cont_rec.quantity=' || l_lpn_cont_rec.quantity, 'Process_Line');
11530                DEBUG('l_lpn_cont_rec.secondary_quantity=' || l_lpn_cont_rec.secondary_quantity, 'Process_Line');
11531             END IF;
11532 
11533             inv_quantity_tree_pub.query_quantities(
11534               p_api_version_number         => 1.0
11535             , p_init_msg_lst               => fnd_api.g_false
11536             , x_return_status              => l_return_status
11537             , x_msg_count                  => l_msg_count
11538             , x_msg_data                   => l_msg_data
11539             , p_organization_id            => p_org_id
11540             , p_inventory_item_id          => p_inventory_item_id
11541             , p_tree_mode                  => inv_quantity_tree_pub.g_reservation_mode
11542             , p_is_revision_control        => l_lpn_cont_rec.revision_control
11543             , p_is_lot_control             => l_lpn_cont_rec.lot_control
11544             , p_is_serial_control          => l_lpn_cont_rec.serial_control
11545             , p_grade_code                 => NULL
11546             , p_demand_source_type_id      => l_demand_source_type_id
11547             , p_demand_source_header_id    => l_demand_source_header_id
11548             , p_demand_source_line_id      => p_order_line_id
11549             , p_revision                   => l_lpn_cont_rec.revision
11550             , p_lot_number                 => l_lpn_cont_rec.lot_number
11551             , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
11552             , p_locator_id                 => l_lpn_cont_rec.locator_id
11553             , x_qoh                        => l_qoh
11554             , x_rqoh                       => l_rqoh
11555             , x_qr                         => l_qr
11556             , x_qs                         => l_qs
11557             , x_att                        => l_transactable_qty
11558             , x_atr                        => l_atr
11559             , x_sqoh                       => l_sqoh
11560             , x_srqoh                      => l_srqoh
11561             , x_sqr                        => l_sqr
11562             , x_sqs                        => l_sqs
11563             , x_satt                       => l_sec_transactable_qty
11564             , x_satr                       => l_satr
11565             , p_lpn_id                     => l_lpn_cont_rec.lpn_id
11566             );
11567 
11568             IF l_return_status = fnd_api.g_ret_sts_error THEN
11569                IF (l_debug = 1) THEN
11570                   DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
11571                          || p_order_header_id || ', ' || p_order_line_id , 'Process_Line');
11572                END IF;
11573 
11574                RAISE fnd_api.g_exc_error;
11575             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11576                IF (l_debug = 1) THEN
11577                   DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
11578                          || p_order_header_id || ', ' || p_order_line_id, 'Process_Line');
11579                END IF;
11580 
11581                RAISE fnd_api.g_exc_unexpected_error;
11582             END IF;
11583 
11584             IF (l_debug = 1) THEN
11585                DEBUG('Query tree Return status is ' || l_return_status, 'Process_Line');
11586                DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Process_Line');
11587                DEBUG('l_sec_transactable_qty= ' || l_sec_transactable_qty, 'Process_Line');
11588                DEBUG('l_quantity_create_resv= ' || l_quantity_create_resv, 'Process_Line');
11589                DEBUG('l_sec_qty_create_resv = ' || l_sec_qty_create_resv, 'Process_Line');
11590             END IF;
11591 
11592             IF (g_fulfillment_base = 'P' AND l_transactable_qty > 0)
11593                OR (g_fulfillment_base = 'S' AND NVL(l_sec_transactable_qty,0) > 0) --{
11594             THEN
11595                --
11596                -- Psuedo-code for deriving secondary quantity, and adjusting
11597                -- primary quantity if fulfillment is based on secondary
11598                --
11599                -- IF dual uom item
11600                --    IF primary-based fulfillment
11601                --       Primary is lesser of transactable, qty_to_create_resv
11602                --
11603                --       Adjust secondary as follows:
11604                --
11605                --       IF lot-spec UOM conv exists
11606                --          sec qty = lot-conv qty from primary
11607                --       ELSE
11608                --          IF deviation allowed
11609                --             Use secondary transactable in same ratio as primary
11610                --          ELSE
11611                --             USE lesser of sec transactable / sec to create resv
11612                --          END IF
11613                --       END IF;
11614                --    ELSE
11615                --       Secondary is lesser of transactable, sec_qty_to_create_resv
11616                --
11617                --       Adjust primary as follows:
11618                --
11619                --       IF lot-spec UOM conv exists
11620                --          primary qty = lot-conv qty from secondary
11621                --       ELSE
11622                --          IF deviation allowed
11623                --             Use primary transactable in same ratio as secondary:
11624                --                 (secondary reserved / secondary transactable)
11625                --                  * primary transactable
11626                --          ELSE
11627                --             Primary is lesser of transactable, qty_to_create_resv
11628                --          END IF
11629                --       END IF;
11630                --    END IF
11631                -- ELSE
11632                --    Primary is lesser of transactable, qty_to_create_resv
11633                -- END IF
11634                --
11635                IF l_dual_uom_item THEN --{
11636                   IF g_fulfillment_base = 'P' THEN --{
11637                      l_reservation_record.primary_reservation_quantity :=
11638                            LEAST(l_transactable_qty, l_quantity_create_resv); --Bug#5262108
11639                      IF l_lot_conv_exists THEN
11640                         l_reservation_record.secondary_reservation_quantity :=
11641                               inv_convert.inv_um_convert(
11642                                   item_id           => p_inventory_item_id
11643                                 , lot_number        => l_lpn_cont_rec.lot_number
11644                                 , organization_id   => p_org_id
11645                                 , precision         => NULL
11646                                 , from_quantity     => l_reservation_record.primary_reservation_quantity
11647                                 , from_unit         => inv_cache.item_rec.primary_uom_code
11648                                 , to_unit           => inv_cache.item_rec.secondary_uom_code
11649                                 , from_name         => NULL
11650                                 , to_name           => NULL);
11651                      ELSE
11652                         IF l_deviation_allowed THEN
11653                            l_reservation_record.secondary_reservation_quantity :=
11654                                  ROUND((l_reservation_record.primary_reservation_quantity / l_transactable_qty)
11655                                        * l_sec_transactable_qty
11656                                        , 5);
11657                         ELSE
11658                             IF(NVL(l_transactable_qty,0) < NVL(l_quantity_create_resv,0)) THEN
11659                                 l_reservation_record.secondary_reservation_quantity := l_sec_transactable_qty;
11660                             ELSE
11661                                 l_reservation_record.secondary_reservation_quantity := l_sec_qty_create_resv;
11662                             END IF;
11663                         END IF;
11664                      END IF;
11665                   --}
11666                   ELSE
11667                      --{ g_fulfillment_base = 'S'
11668                      l_reservation_record.secondary_reservation_quantity :=
11669                            LEAST(l_sec_transactable_qty, l_sec_qty_create_resv);
11670 
11671                      IF l_deviation_allowed THEN
11672                         l_reservation_record.primary_reservation_quantity :=
11673                            ROUND(((l_reservation_record.secondary_reservation_quantity * l_transactable_qty) / l_sec_transactable_qty), 5);
11674                      ELSE
11675                          IF(NVL(l_sec_transactable_qty,0) < NVL(l_sec_qty_create_resv,0)) THEN
11676                              l_reservation_record.primary_reservation_quantity := l_transactable_qty;
11677                          ELSE
11678                              l_reservation_record.primary_reservation_quantity := l_quantity_create_resv;
11679                          END IF;
11680                      END IF;
11681                   END IF; --}
11682                --}
11683                ELSE
11684                   -- Not a dual UOM item
11685                   l_reservation_record.primary_reservation_quantity :=
11686                         LEAST(l_transactable_qty, l_quantity_create_resv); --Bug#5262108
11687                   l_reservation_record.secondary_reservation_quantity := NULL;
11688                END IF;
11689 
11690                -- No need for UOM conversion here since rsv UOM is hard-coded below to primary
11691                l_reservation_record.reservation_quantity := l_reservation_record.primary_reservation_quantity;
11692 
11693                IF (l_debug = 1) THEN
11694                   DEBUG('Quantity to create reservation= ' || l_reservation_record.primary_reservation_quantity, 'Process_Line');
11695                   DEBUG('Sec quantity to create reservation= ' || l_reservation_record.secondary_reservation_quantity, 'Process_Line');
11696                END IF;
11697 
11698                l_reservation_record.organization_id             := p_org_id;
11699                l_reservation_record.inventory_item_id           := p_inventory_item_id;
11700                l_reservation_record.demand_source_header_id     := l_demand_source_header_id;
11701                l_reservation_record.demand_source_line_id       := p_order_line_id;
11702                l_reservation_record.reservation_uom_id          := NULL;
11703                l_reservation_record.reservation_uom_code        := inv_cache.item_rec.primary_uom_code;
11704                l_reservation_record.primary_uom_code            := inv_cache.item_rec.primary_uom_code;
11705                IF l_dual_uom_item THEN
11706                   l_reservation_record.secondary_uom_code := inv_cache.item_rec.secondary_uom_code;
11707                ELSE
11708                   l_reservation_record.secondary_uom_code := NULL;
11709                END IF;
11710                l_reservation_record.primary_uom_id              := NULL;
11711                l_reservation_record.secondary_uom_id            := NULL;
11712                l_reservation_record.supply_source_type_id       := 13;
11713                l_reservation_record.demand_source_type_id       := l_demand_source_type_id;
11714                l_reservation_record.ship_ready_flag             := 2;
11715                l_reservation_record.attribute1                  := NULL;
11716                l_reservation_record.attribute2                  := NULL;
11717                l_reservation_record.attribute3                  := NULL;
11718                l_reservation_record.attribute4                  := NULL;
11719                l_reservation_record.attribute5                  := NULL;
11720                l_reservation_record.attribute6                  := NULL;
11721                l_reservation_record.attribute7                  := NULL;
11722                l_reservation_record.attribute8                  := NULL;
11723                l_reservation_record.attribute9                  := NULL;
11724                l_reservation_record.attribute10                 := NULL;
11725                l_reservation_record.attribute11                 := NULL;
11726                l_reservation_record.attribute12                 := NULL;
11727                l_reservation_record.attribute13                 := NULL;
11728                l_reservation_record.attribute14                 := NULL;
11729                l_reservation_record.attribute15                 := NULL;
11730                l_reservation_record.attribute_category          := NULL;
11731                l_reservation_record.lpn_id                      := l_lpn_cont_rec.lpn_id;
11732                l_reservation_record.pick_slip_number            := NULL;
11733                l_reservation_record.lot_number_id               := NULL;
11734                l_reservation_record.lot_number                  := l_lpn_cont_rec.lot_number;
11735                l_reservation_record.locator_id                  := l_lpn_cont_rec.locator_id;
11736                l_reservation_record.subinventory_id             := NULL;
11737                l_reservation_record.subinventory_code           := g_subinventory_code;
11738                l_reservation_record.revision                    := l_lpn_cont_rec.revision;
11739                l_reservation_record.supply_source_line_detail   := NULL;
11740                l_reservation_record.supply_source_name          := NULL;
11741                l_reservation_record.supply_source_line_id       := p_order_line_id;
11742                l_reservation_record.supply_source_header_id     := l_demand_source_header_id;
11743                l_reservation_record.external_source_line_id     := NULL;
11744                l_reservation_record.external_source_code        := NULL;
11745                l_reservation_record.autodetail_group_id         := NULL;
11746                l_reservation_record.demand_source_delivery      := NULL;
11747                l_reservation_record.demand_source_name          := NULL;
11748                l_reservation_record.requirement_date            := p_date_requested;
11749                l_reservation_record.staged_flag                 := 'Y' ;  -- Bug#5262108
11750                l_reservation_record.secondary_detailed_quantity := NULL;  -- Bug 7482123
11751                l_reservation_record.detailed_quantity           := NULL;  -- Bug 7482123
11752 
11753                IF (l_debug = 1) THEN
11754                   DEBUG('Before call to create reservation', 'Process_Line');
11755                END IF;
11756 
11757                inv_reservation_pub.create_reservation(
11758                  x_return_status               => l_return_status
11759                , x_msg_count                   => l_msg_count
11760                , x_msg_data                    => l_msg_data
11761                , x_serial_number               => l_dummy_sn
11762                , x_quantity_reserved           => l_quantity_reserved_tmp
11763                , x_secondary_quantity_reserved => l_sec_qty_reserved_tmp
11764                , x_reservation_id              => l_reservation_id
11765                , p_api_version_number          => 1.0
11766                , p_init_msg_lst                => fnd_api.g_false
11767                , p_rsv_rec                     => l_reservation_record
11768                , p_partial_reservation_flag    => fnd_api.g_true
11769                , p_force_reservation_flag      => fnd_api.g_true
11770                , p_serial_number               => l_dummy_sn
11771                , p_validation_flag             => fnd_api.g_true
11772                , p_over_reservation_flag       => 3
11773                );
11774 
11775                IF l_return_status = fnd_api.g_ret_sts_error THEN
11776                   IF (l_debug = 1) THEN
11777                      DEBUG('Create reservation failed for lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11778                   END IF;
11779 
11780                   RAISE fnd_api.g_exc_error;
11781                ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11782                   IF (l_debug = 1) THEN
11783                      DEBUG('Unexpected error during create of Reservations lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
11784                   END IF;
11785 
11786                   RAISE fnd_api.g_exc_unexpected_error;
11787                END IF;
11788 
11789                IF (l_debug = 1) THEN
11790                   DEBUG('Create reservations is successful ' || l_reservation_id, 'Process_Line');
11791                END IF;
11792 
11793                -- update quantities
11794                g_lpn_contents_tab(lpnc_rec).quantity := g_lpn_contents_tab(lpnc_rec).quantity
11795                                                         - l_reservation_record.primary_reservation_quantity;
11796                l_processed_quantity := l_processed_quantity + l_reservation_record.primary_reservation_quantity;
11797                g_total_lpn_quantity := g_total_lpn_quantity - l_reservation_record.primary_reservation_quantity;
11798 
11799                IF l_dual_uom_item THEN
11800                   g_lpn_contents_tab(lpnc_rec).secondary_quantity := g_lpn_contents_tab(lpnc_rec).secondary_quantity
11801                                                                      - l_reservation_record.secondary_reservation_quantity;
11802                   l_processed_sec_qty := l_processed_sec_qty + l_reservation_record.secondary_reservation_quantity;
11803                   g_total_lpn_sec_qty := g_total_lpn_sec_qty - l_reservation_record.secondary_reservation_quantity;
11804                END IF;
11805 
11806                IF l_create_reservation_flag = 'Y' THEN
11807                   l_quantity_create_resv := l_quantity_create_resv - l_reservation_record.primary_reservation_quantity;
11808                   IF l_dual_uom_item THEN
11809                      l_sec_qty_create_resv := l_sec_qty_create_resv
11810                                               - l_reservation_record.secondary_reservation_quantity;
11811                   END IF;
11812                ELSE
11813                   l_qty_reserved_tmp := l_qty_reserved_tmp + l_reservation_record.primary_reservation_quantity;
11814                   IF l_dual_uom_item THEN
11815                      l_sec_qty_res_tmp := l_sec_qty_res_tmp + l_reservation_record.secondary_reservation_quantity;
11816                   END IF;
11817                END IF;
11818 
11819 			--Bug 13830442
11820 				IF (l_debug = 1) THEN
11821 				DEBUG('Updating qty tree with qty:'|| l_reservation_record.primary_reservation_quantity , 'Process_Line');
11822 				END IF;
11823 
11824 			inv_quantity_tree_pub.update_quantities(
11825               p_api_version_number         => 1.0
11826             , p_init_msg_lst               => fnd_api.g_false
11827             , x_return_status              => l_return_status
11828             , x_msg_count                  => l_msg_count
11829             , x_msg_data                   => l_msg_data
11830             , p_organization_id            => p_org_id
11831             , p_inventory_item_id          => p_inventory_item_id
11832             , p_tree_mode                  => inv_quantity_tree_pub.g_reservation_mode
11833             , p_is_revision_control        => l_lpn_cont_rec.revision_control
11834             , p_is_lot_control             => l_lpn_cont_rec.lot_control
11835             , p_is_serial_control          => l_lpn_cont_rec.serial_control
11836             , p_demand_source_type_id      => l_demand_source_type_id
11837             , p_demand_source_header_id    => l_demand_source_header_id
11838             , p_demand_source_line_id      => p_order_line_id
11839             , p_revision                   => l_lpn_cont_rec.revision
11840             , p_lot_number                 => l_lpn_cont_rec.lot_number
11841             , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
11842             , p_locator_id                 => l_lpn_cont_rec.locator_id
11843             , p_primary_quantity           => -1*l_reservation_record.primary_reservation_quantity
11844             , p_secondary_quantity         => NVL(-1*l_reservation_record.secondary_reservation_quantity,0)
11845             , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
11846             , x_qoh                        => l_qoh
11847             , x_rqoh                       => l_rqoh
11848             , x_qr                         => l_qr
11849             , x_qs                         => l_qs
11850             , x_att                        => l_att
11851             , x_atr                        => l_atr
11852 			, x_sqoh                       => l_sqoh
11853             , x_srqoh                      => l_srqoh
11854             , x_sqr                        => l_sqr
11855             , x_sqs                        => l_sqs
11856             , x_satt                       => l_satt
11857             , x_satr                       => l_satr
11858             , p_lpn_id                     => l_lpn_cont_rec.lpn_id
11859             );
11860 
11861 
11862                IF (g_fulfillment_base = 'P' AND l_quantity_create_resv = 0)
11863                   OR (g_fulfillment_base = 'S' AND l_sec_qty_create_resv = 0)
11864                THEN
11865                   EXIT;
11866                ELSIF ( (g_fulfillment_base = 'P') AND ((p_ordered_quantity - l_qty_reserved_tmp) = 0) )
11867                      OR ( (g_fulfillment_base = 'S') AND ((NVL(p_sec_ordered_qty,0) - l_sec_qty_res_tmp) = 0) )
11868                THEN
11869                   EXIT;
11870                END IF;
11871             END IF; --} l_transactable_qty > 0
11872          END IF; --} l_lpn_cont_rec
11873 
11874          IF (l_debug = 1) THEN
11875             DEBUG('g_lpn_contents_tab(' || lpnc_rec || ').quantity=' || g_lpn_contents_tab(lpnc_rec).quantity, 'Process_Line');
11876          END IF;
11877      END LOOP; --} lpnc_rec
11878   END IF; --} l_quantity_create_resv > 0
11879 
11880   x_remaining_quantity := g_total_lpn_quantity;
11881   x_sec_remaining_qty  := g_total_lpn_sec_qty;
11882 
11883   IF (l_debug = 1) THEN
11884      DEBUG('x_remaining_quantity=' || x_remaining_quantity, 'Process_Line');
11885      DEBUG('x_sec_remaining_qty= ' || x_sec_remaining_qty,  'Process_Line');
11886   END IF;
11887 
11888   -- INSERT DATA INTO WDS
11889   IF (g_fulfillment_base = 'P' AND l_processed_quantity > 0)
11890      OR (g_fulfillment_base = 'S' AND l_processed_sec_qty > 0) --{
11891   THEN
11892      IF (l_debug = 1) THEN
11893         DEBUG('Inserting data into WDS', 'Process_Line');
11894      END IF;
11895 
11896      -- if record exist then update
11897      BEGIN --{
11898         SELECT order_line_id
11899           INTO l_temp_var
11900           FROM wms_direct_ship_temp
11901          WHERE organization_id = p_org_id
11902            AND dock_door_id = p_dock_door_id
11903            AND lpn_id = p_lpn_id
11904            AND order_header_id = p_order_header_id
11905            AND order_line_id = p_order_line_id;
11906 
11907         -- no excaption means record exist
11908         UPDATE wms_direct_ship_temp
11909            SET processed_flag = 'Y'
11910              , processed_quantity = l_processed_quantity
11911              , secondary_processed_qty = l_processed_sec_qty
11912          WHERE organization_id = p_org_id
11913            AND dock_door_id = p_dock_door_id
11914            AND lpn_id = p_lpn_id
11915            AND order_header_id = p_order_header_id
11916            AND order_line_id = p_order_line_id;
11917      EXCEPTION
11918         WHEN NO_DATA_FOUND THEN
11919            -- first time line is being processed
11920            IF (l_serial_required_flag = 'Y') THEN
11921               BEGIN
11922                  SELECT mtl_material_transactions_s.NEXTVAL
11923                    INTO l_trx_temp_id
11924                    FROM DUAL;
11925               EXCEPTION
11926                  WHEN NO_DATA_FOUND THEN
11927                     IF (l_debug = 1) THEN
11928                        DEBUG('Error getting transaction_temp_id', 'Process_Line');
11929                     END IF;
11930                     x_return_status  := fnd_api.g_ret_sts_error;
11931                     RAISE fnd_api.g_exc_error;
11932               END;
11933            ELSE
11934               l_trx_temp_id  := NULL;
11935            END IF;
11936 
11937            INSERT INTO wms_direct_ship_temp(
11938                        group_id
11939                      , organization_id
11940                      , dock_door_id
11941                      , lpn_id
11942                      , order_header_id
11943                      , order_line_id
11944                      , line_item_id
11945                      , processed_quantity
11946                      , secondary_processed_qty
11947                      , processed_flag
11948                      , serial_required_flag
11949                      , transaction_temp_id
11950                      , creation_date
11951                      , created_by
11952                      , last_update_date
11953                      , last_updated_by
11954                      , last_update_login
11955                      )
11956               VALUES ( g_group_id
11957                      , p_org_id
11958                      , p_dock_door_id
11959                      , p_lpn_id
11960                      , p_order_header_id
11961                      , p_order_line_id
11962                      , p_inventory_item_id
11963                      , l_processed_quantity
11964                      , l_processed_sec_qty
11965                      , 'Y'
11966                      , l_serial_required_flag
11967                      , l_trx_temp_id
11968                      , g_creation_date
11969                      , g_created_by
11970                      , g_last_update_date
11971                      , g_last_updated_by
11972                      , g_last_update_login
11973                      );
11974      END; --} update / insert
11975   END IF; --} l_processed_quantity > 0
11976 
11977   x_return_status := fnd_api.g_ret_sts_success;
11978 
11979 EXCEPTION
11980   WHEN OTHERS THEN
11981     IF (l_debug = 1) THEN
11982       DEBUG('Unexpected error occured', 'Process_Line');
11983     END IF;
11984 
11985 	g_lpn_contents_tab := l_lpn_contents_tab_bkp;
11986 	inv_quantity_tree_pub.clear_quantity_cache;
11987 	IF (l_debug = 1) THEN
11988 		DEBUG('Cleared qty tree cache and reset the g_lpn_contents_tab', 'Process_Line');
11989 	END IF;
11990 
11991     -- Material status was previously unchecked
11992     -- so restore old value
11993     IF l_mtlstat_prev_unchk THEN
11994        g_matl_stat_checked := FALSE;
11995     END IF;
11996 
11997     x_return_status  := fnd_api.g_ret_sts_unexp_error;
11998     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LINE');
11999     fnd_msg_pub.ADD;
12000 END process_line;
12001 
12002 /* This functions validates the line being packect into LPN for
12003    Delivery grouping rules.
12004  */
12005 
12006 FUNCTION validate_del_grp_rules(p_line_processed IN NUMBER, p_header_id IN NUMBER, p_line_id IN NUMBER)
12007   RETURN BOOLEAN IS
12008   CURSOR del_detail IS
12009     SELECT ship_from_location_id
12010          , ship_to_location_id
12011          , customer_id
12012          , intmed_ship_to_location_id
12013          , ship_method_code
12014          , fob_code
12015          , freight_terms_code
12016       FROM wsh_delivery_details_ob_grp_v
12017      WHERE source_header_id = p_header_id
12018        AND source_line_id = p_line_id;
12019 
12020   l_del_detail_rec del_detail%ROWTYPE;
12021   l_count          NUMBER               := 0;
12022   l_debug          NUMBER               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12023 BEGIN
12024   IF (l_debug = 1) THEN
12025     DEBUG('p_line_processed= ' || p_line_processed, 'Validate_Del_Grp_Rules');
12026     DEBUG('p_header_id= ' || p_header_id, 'Validate_Del_Grp_Rules');
12027     DEBUG('p_line_id= ' || p_line_id, 'Validate_Del_Grp_Rules');
12028   END IF;
12029 
12030   OPEN del_detail;
12031 
12032   LOOP
12033     FETCH del_detail INTO l_del_detail_rec;
12034     EXIT WHEN del_detail%NOTFOUND;
12035     -- populate delivery grouping fld values into temp table
12036     g_del_grp_rls_fld_temp(1)  := l_del_detail_rec.ship_from_location_id;
12037     g_del_grp_rls_fld_temp(2)  := l_del_detail_rec.ship_to_location_id;
12038     g_del_grp_rls_fld_temp(3)  := l_del_detail_rec.customer_id;
12039     g_del_grp_rls_fld_temp(4)  := l_del_detail_rec.fob_code;
12040     g_del_grp_rls_fld_temp(5)  := l_del_detail_rec.freight_terms_code;
12041     g_del_grp_rls_fld_temp(6)  := l_del_detail_rec.intmed_ship_to_location_id;
12042     g_del_grp_rls_fld_temp(7)  := l_del_detail_rec.ship_method_code;
12043 
12044     IF (l_debug = 1) THEN
12045       DEBUG('g_del_grp_rls_fld_temp populated', 'Validate_Del_Grp_Rules');
12046     END IF;
12047 
12048     l_count                    := l_count + 1;
12049     EXIT; -- only one records values are enough
12050   END LOOP;
12051 
12052   IF l_count = 0 THEN
12053     IF (l_debug = 1) THEN
12054       DEBUG('Record not found in wdd for line_id=' || p_line_id, 'Validate_Del_Grp_Rules');
12055     END IF;
12056 
12057     RAISE fnd_api.g_exc_error;
12058   END IF;
12059 
12060   IF p_line_processed = 0 THEN
12061     -- loading first line no need to do comparision
12062     -- just populate g_del_grp_rls_fld_value from
12063     -- g_del_grp_rls_fld_temp
12064     g_del_grp_rls_fld_value(1)  := g_del_grp_rls_fld_temp(1);
12065     g_del_grp_rls_fld_value(2)  := g_del_grp_rls_fld_temp(2);
12066     g_del_grp_rls_fld_value(3)  := g_del_grp_rls_fld_temp(3);
12067     g_del_grp_rls_fld_value(4)  := g_del_grp_rls_fld_temp(4);
12068     g_del_grp_rls_fld_value(5)  := g_del_grp_rls_fld_temp(5);
12069     g_del_grp_rls_fld_value(6)  := g_del_grp_rls_fld_temp(6);
12070     g_del_grp_rls_fld_value(7)  := g_del_grp_rls_fld_temp(7);
12071 
12072     IF (l_debug = 1) THEN
12073       DEBUG('SUCCESS', 'Validate_Del_Grp_Rules');
12074     END IF;
12075 
12076     RETURN TRUE;
12077   ELSE
12078     -- compare values from first line to current line
12079     FOR l_flag IN 1 .. 7 LOOP
12080       IF g_del_grp_rls_flags(l_flag) = 'Y' THEN
12081         -- comparision needed
12082         IF g_del_grp_rls_fld_value(l_flag) <> g_del_grp_rls_fld_temp(l_flag) THEN
12083           IF (l_debug = 1) THEN
12084             DEBUG('g_del_grp_rls_fld_value(l_flag)=' || g_del_grp_rls_fld_value(l_flag), 'Validate_Del_Grp_Rules');
12085             DEBUG('g_del_grp_rls_fld_temp(l_flag)=' || g_del_grp_rls_fld_temp(l_flag), 'Validate_Del_Grp_Rules');
12086             DEBUG('FAILURE', 'Validate_Del_Grp_Rules');
12087           END IF;
12088 
12089           RETURN FALSE;
12090         END IF;
12091       END IF;
12092     END LOOP;
12093 
12094     IF (l_debug = 1) THEN
12095       DEBUG('SUCCESS', 'Validate_Del_Grp_Rules');
12096     END IF;
12097 
12098     RETURN TRUE;
12099   END IF;
12100 EXCEPTION
12101   WHEN OTHERS THEN
12102     IF (l_debug = 1) THEN
12103       DEBUG('Exception occured', 'Validate_Del_Grp_Rules');
12104     END IF;
12105 
12106     RAISE fnd_api.g_exc_error;
12107 END validate_del_grp_rules;
12108 
12109 /*
12110   This method checks that there should be no record for this lpn in wstt (delivery)
12111   having direct_ship_flag N or loaded by some other method than direct ship.
12112  */
12113 
12114 FUNCTION validate_del_for_ds(
12115     p_lpn_id        IN  NUMBER
12116   , p_org_id        IN  NUMBER
12117   , p_dock_door_id  IN  NUMBER
12118   , p_header_id     IN  NUMBER
12119   , p_line_id       IN  NUMBER
12120   ) RETURN BOOLEAN IS
12121 
12122   CURSOR del_cur IS
12123     SELECT wda.delivery_id
12124       FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
12125      WHERE wds.organization_id = p_org_id
12126        AND wds.dock_door_id = p_dock_door_id
12127        AND wds.lpn_id = p_lpn_id
12128        AND wds.order_header_id = p_header_id
12129        AND wds.order_line_id = p_line_id
12130        AND wds.order_line_id = wdd.source_line_id
12131        AND wds.order_header_id = wdd.source_header_id
12132        AND wdd.delivery_detail_id = wda.delivery_detail_id;
12133 
12134   l_del_cur_rec   del_cur%ROWTYPE;
12135   l_return_status VARCHAR2(3);
12136   l_msg_count     NUMBER;
12137   l_msg_data      VARCHAR(20000);
12138   l_result        BOOLEAN           := TRUE;
12139   l_debug         NUMBER            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12140 
12141   l_chk_del_exc   EXCEPTION;
12142 
12143 BEGIN
12144   IF (l_debug = 1) THEN
12145     DEBUG('Checking delivery for direct ship', 'Validate_Del_For_DS');
12146     DEBUG('delivery id:'||l_del_cur_rec.delivery_id, 'Validate_Del_For_DS');
12147   END IF;
12148 
12149   OPEN del_cur;
12150 
12151   LOOP
12152     FETCH del_cur INTO l_del_cur_rec;
12153     EXIT WHEN del_cur%NOTFOUND;
12154 
12155     -- first checjk cache table
12156     IF l_del_cur_rec.delivery_id IS NOT NULL THEN
12157        IF (l_debug = 1) THEN
12158           DEBUG('Checking delivery ID ' || l_del_cur_rec.delivery_id,
12159                 'Validate_Del_For_DS');
12160        END IF;
12161 
12162        IF g_checked_deliveries.EXISTS(l_del_cur_rec.delivery_id) THEN
12163           l_result := TRUE;
12164        ELSE -- not already validated go throug normal procedure
12165           wms_direct_ship_pvt.chk_del_for_direct_ship(
12166             x_return_status              => l_return_status
12167           , x_msg_count                  => l_msg_count
12168           , x_msg_data                   => l_msg_data
12169           , p_delivery_id                => l_del_cur_rec.delivery_id
12170           );
12171 
12172           IF l_return_status = fnd_api.g_ret_sts_error THEN
12173              IF (l_debug = 1) THEN
12174                 DEBUG('l_return_status from chk_del_for_direct_ship: ' || l_return_status,
12175                       'Validate_Del_For_DS');
12176              END IF;
12177              l_result := FALSE;
12178 
12179              RAISE l_chk_del_exc;
12180           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12181              IF (l_debug = 1) THEN
12182                 DEBUG('l_return_status from chk_del_for_direct_ship: ' || l_return_status,
12183                       'Validate_Del_For_DS');
12184              END IF;
12185              l_result := FALSE;
12186 
12187              RAISE l_chk_del_exc;
12188           END IF;
12189 
12190           IF (l_debug = 1) THEN
12191              DEBUG('Delivery ID ' || l_del_cur_rec.delivery_id ||
12192                    ' validated.  Adding to g_checked_deliveries.',
12193                    'Validate_Del_For_DS');
12194           END IF;
12195 
12196           -- success
12197           -- put delivery into cache table
12198           g_checked_deliveries(l_del_cur_rec.delivery_id) := 1;
12199        END IF;
12200     END IF;
12201   END LOOP;
12202 
12203   CLOSE del_cur;
12204 
12205   RETURN l_result;
12206 EXCEPTION
12207   WHEN l_chk_del_exc THEN
12208      IF (l_debug = 1) THEN
12209         DEBUG('Error status from wms_direct_ship_pvt.chk_del_for_direct_ship', 'Validate_Del_For_DS');
12210      END IF;
12211 
12212      IF del_cur%ISOPEN THEN
12213         CLOSE del_cur;
12214      END IF;
12215 
12216      RETURN FALSE;
12217 
12218   WHEN OTHERS THEN
12219      IF (l_debug = 1) THEN
12220         DEBUG('Exception occured', 'Validate_Del_For_DS');
12221      END IF;
12222 
12223      RAISE fnd_api.g_exc_error;
12224 END validate_del_for_ds;
12225 
12226 /*
12227   This procedure perform the following processing for a lpn.
12228   1. Update staged flag of all reservations for all the lines packed into LPN.
12229   2. Stage LPN
12230   3. Update Freight Cost for LPN
12231  */
12232 
12233  /* bug#2798970 */
12234  /*
12235   jaysingh : The initail code of this procedure was doing wrong reservation staging.
12236              If two different LPNs was reserved against one line and we try to ship
12237        one LPN through DS, reservations for both the LPNs was staged. We should
12238        stage reservations only for LPNs which are inner LPNs of the outer LPN
12239        and the outermost LPN itself.
12240 */
12241 
12242    PROCEDURE load_lpn(
12243 		      x_return_status OUT NOCOPY    VARCHAR2
12244 		      , x_msg_count     OUT NOCOPY    NUMBER
12245 		      , x_msg_data      OUT NOCOPY    VARCHAR2
12246 		      , p_lpn_id        IN            NUMBER
12247 		      , p_org_id        IN            NUMBER
12248 		      , p_dock_door_id  IN            NUMBER
12249 		      ) IS
12250  --
12251   TYPE number_table IS TABLE OF NUMBER
12252     INDEX BY BINARY_INTEGER;
12253 
12254   --
12255   CURSOR wds_cur IS
12256     SELECT wds.order_header_id
12257          , wds.order_line_id
12258       FROM wms_direct_ship_temp wds
12259      WHERE wds.organization_id = p_org_id
12260        AND wds.dock_door_id = p_dock_door_id
12261        AND wds.lpn_id = p_lpn_id;
12262 
12263   --
12264   CURSOR lpn_cur IS
12265     SELECT lpn_id
12266       FROM wms_license_plate_numbers
12267      WHERE outermost_lpn_id = p_lpn_id
12268        AND organization_id = p_org_id;
12269 
12270   -- query reservations
12271   l_qry_reservation_record    inv_reservation_global.mtl_reservation_rec_type;
12272   l_mtl_reservation_tab       inv_reservation_global.mtl_reservation_tbl_type;
12273   l_mtl_reservation_tab_count NUMBER;
12274   l_lpn_ids_tab               number_table;
12275   l_return_status             VARCHAR2(3);
12276   l_msg_count                 NUMBER;
12277   l_msg_data                  VARCHAR2(20000);
12278   l_error_code                NUMBER;
12279   l_debug                     NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12280 BEGIN
12281   IF (l_debug = 1) THEN
12282     DEBUG('Procedure Load_LPN called with parameters', 'Load_LPN');
12283     DEBUG('p_org_id=' || p_org_id, 'Load_LPN');
12284     DEBUG('p_dock_door_id=' || p_dock_door_id, 'Load_LPN');
12285     DEBUG('p_lpn_id=' || p_lpn_id, 'Load_LPN');
12286   END IF;
12287 
12288   -- stage reservations
12289   IF (l_debug = 1) THEN
12290     DEBUG('Staging reservations', 'Load_LPN');
12291   END IF;
12292 
12293   -- get all lpn ids.
12294   IF l_lpn_ids_tab.COUNT > 0 THEN -- clear the cache
12295     l_lpn_ids_tab.DELETE;
12296   END IF;
12297 
12298   FOR l_lpn_cur IN lpn_cur LOOP
12299     l_lpn_ids_tab(l_lpn_cur.lpn_id)  := 0;
12300   END LOOP;
12301 
12302   -- for each line in WDS from p_lpn_id
12303   FOR l_wds_cur IN wds_cur LOOP
12304     l_qry_reservation_record.demand_source_line_id    := l_wds_cur.order_line_id;
12305     l_qry_reservation_record.demand_source_header_id  := inv_salesorder.get_salesorder_for_oeheader(l_wds_cur.order_header_id);
12306     l_qry_reservation_record.demand_source_type_id    := get_demand_src_type(l_wds_cur.order_header_id);
12307     l_qry_reservation_record.supply_source_type_id    := inv_reservation_global.g_source_type_inv;
12308 
12309     IF (l_debug = 1) THEN
12310       DEBUG('Before call to query reservation', 'Load_LPN');
12311     END IF;
12312 
12313     inv_reservation_pub.query_reservation(
12314       p_api_version_number         => 1.0
12315     , p_init_msg_lst               => fnd_api.g_false
12316     , x_return_status              => l_return_status
12317     , x_msg_count                  => l_msg_count
12318     , x_msg_data                   => l_msg_data
12319     , p_query_input                => l_qry_reservation_record
12320     , p_lock_records               => fnd_api.g_false
12321     , x_mtl_reservation_tbl        => l_mtl_reservation_tab
12322     , x_mtl_reservation_tbl_count  => l_mtl_reservation_tab_count
12323     , x_error_code                 => l_error_code
12324     );
12325 
12326     IF l_return_status = fnd_api.g_ret_sts_error THEN
12327       IF (l_debug = 1) THEN
12328         DEBUG('Query reservation failed', 'Load_LPN');
12329       END IF;
12330 
12331       RAISE fnd_api.g_exc_error;
12332     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12333       IF (l_debug = 1) THEN
12334         DEBUG('Query reservation failed', 'Load_LPN');
12335       END IF;
12336 
12337       RAISE fnd_api.g_exc_unexpected_error;
12338     END IF;
12339 
12340     IF (l_debug = 1) THEN
12341       DEBUG('After call to query reservation', 'Load_LPN');
12342     END IF;
12343 
12344     -- now stage reservations which has lpn_id present in l_lpn_ids_tab.
12345     FOR l_rec IN 1 .. l_mtl_reservation_tab_count LOOP
12346       IF l_lpn_ids_tab.EXISTS(l_mtl_reservation_tab(l_rec).lpn_id) THEN
12347         -- everything fine stage the reservation
12348         inv_staged_reservation_util.update_staged_flag(
12349           x_return_status              => l_return_status
12350         , x_msg_count                  => l_msg_count
12351         , x_msg_data                   => l_msg_data
12352         , p_reservation_id             => l_mtl_reservation_tab(l_rec).reservation_id
12353         , p_staged_flag                => 'Y'
12354         );
12355 
12356         IF l_return_status = fnd_api.g_ret_sts_error THEN
12357           IF (l_debug = 1) THEN
12358             DEBUG('Update_staged_flag failed with error', 'Load_LPN');
12359           END IF;
12360 
12361           RAISE fnd_api.g_exc_error;
12362         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12363           IF (l_debug = 1) THEN
12364             DEBUG('Update_staged_flag failed with exception', 'Load_LPN');
12365           END IF;
12366 
12367           RAISE fnd_api.g_exc_unexpected_error;
12368         END IF;
12369 
12370         IF (l_debug = 1) THEN
12371           DEBUG(
12372                'Reservation staged for resv_id='
12373             || l_mtl_reservation_tab(l_rec).reservation_id
12374             || ' : lpn_id='
12375             || l_mtl_reservation_tab(l_rec).lpn_id
12376             || ' : line_id='
12377             || l_mtl_reservation_tab(l_rec).demand_source_line_id
12378           , 'Load_LPN'
12379           );
12380         END IF;
12381       END IF;
12382     END LOOP; -- stage
12383   END LOOP; -- wds_cur
12384 
12385   IF (l_debug = 1) THEN
12386     DEBUG('Staging reservations completed', 'Load_LPN');
12387   END IF;
12388 
12389   -- stage lpns
12390   IF (l_debug = 1) THEN
12391     DEBUG('Staging LPNs ', 'Load_LPN');
12392   END IF;
12393 
12394   wms_direct_ship_pvt.stage_lpns(
12395     x_return_status              => l_return_status
12396   , x_msg_count                  => l_msg_count
12397   , x_msg_data                   => l_msg_data
12398   , p_group_id                   => g_group_id
12399   , p_organization_id            => p_org_id
12400   , p_dock_door_id               => p_dock_door_id
12401   );
12402 
12403   IF l_return_status = fnd_api.g_ret_sts_error THEN
12404      x_msg_data := l_msg_data;
12405      IF (l_debug = 1) THEN
12406 	DEBUG('Stage LPN API failed with status E ', 'Load_LPN');
12407      END IF;
12408 
12409      RAISE fnd_api.g_exc_error;
12410    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12411      x_msg_data := l_msg_data;
12412      IF (l_debug = 1) THEN
12413 	DEBUG('Stage LPN API failed with status U', 'Load_LPN');
12414      END IF;
12415 
12416      RAISE fnd_api.g_exc_unexpected_error;
12417   END IF;
12418 
12419   IF (l_debug = 1) THEN
12420     DEBUG('Staging LPNs completed', 'Load_LPN');
12421   END IF;
12422 
12423   -- update freight cost
12424   IF (l_debug = 1) THEN
12425     DEBUG('Updating freight cost', 'Load_LPN');
12426   END IF;
12427 
12428   wms_direct_ship_pvt.update_freight_cost(x_return_status => l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data
12429   , p_lpn_id                     => p_lpn_id);
12430 
12431   IF l_return_status = fnd_api.g_ret_sts_error THEN
12432     IF (l_debug = 1) THEN
12433       DEBUG('Update Freight Cost API failed with status E ', 'Load_LPN');
12434     END IF;
12435 
12436     RAISE fnd_api.g_exc_error;
12437   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12438     IF (l_debug = 1) THEN
12439       DEBUG('Update Freight Cost failed with status U', 'Load_LPN');
12440     END IF;
12441 
12442     RAISE fnd_api.g_exc_unexpected_error;
12443   END IF;
12444 
12445   IF (l_debug = 1) THEN
12446     DEBUG('Update freight cost completed', 'Load_LPN');
12447     DEBUG('LPN loaded successfully', 'Load_LPN');
12448   END IF;
12449 
12450   -- All processing complete for this LPN, so reset fulfillment base
12451   g_fulfillment_base := 'P';
12452 
12453   x_return_status  := fnd_api.g_ret_sts_success;
12454 EXCEPTION
12455   WHEN OTHERS THEN
12456     IF (l_debug = 1) THEN
12457       DEBUG('Exception loading lpn', 'Load_LPN');
12458     END IF;
12459 
12460     x_return_status  := fnd_api.g_ret_sts_unexp_error;
12461     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
12462     fnd_msg_pub.ADD;
12463 END load_lpn;
12464 
12465 /*
12466   This procedure distributes the un-used quantity in the lpn among all the loaded lines.
12467   First it checks if for a lpn_content record a exact matching reservation is found the
12468   update the existing reservation else create new reservation.
12469  */
12470 
12471 PROCEDURE perform_overship_distribution(
12472   p_lpn_id        IN            NUMBER
12473 , p_org_id        IN            NUMBER
12474 , p_dock_door_id  IN            NUMBER
12475 , x_return_status OUT NOCOPY    VARCHAR2
12476 , x_msg_count     OUT NOCOPY    NUMBER
12477 , x_msg_data      OUT NOCOPY    VARCHAR2
12478 ) IS
12479   CURSOR loaded_lines(p_item_id IN NUMBER) IS
12480     SELECT wds.order_header_id
12481          , wds.order_line_id
12482          , oel.item_revision revision
12483          , oel.end_item_unit_number
12484          , oel.request_date
12485          , msi.primary_uom_code
12486          , msi.secondary_uom_code
12487       FROM wms_direct_ship_temp wds, oe_order_lines_all oel, mtl_system_items_kfv msi
12488      WHERE wds.organization_id = p_org_id
12489        AND wds.GROUP_ID = g_group_id
12490        AND wds.dock_door_id = p_dock_door_id
12491        AND wds.lpn_id = p_lpn_id
12492        AND wds.line_item_id = p_item_id
12493        AND oel.header_id = wds.order_header_id
12494        AND oel.line_id = wds.order_line_id
12495        AND msi.organization_id = wds.organization_id
12496        AND msi.inventory_item_id = wds.line_item_id;
12497 
12498   -- bug#2830138
12499   TYPE lpn_cont_qty_used IS TABLE OF NUMBER
12500     INDEX BY BINARY_INTEGER;
12501 
12502   l_lpn_cont_qty_used       lpn_cont_qty_used;
12503   l_lpn_sec_qty_used        lpn_cont_qty_used;
12504   l_old_total_lpn_qty       NUMBER := g_total_lpn_quantity;
12505   l_old_tot_lpn_sec_qty     NUMBER := g_total_lpn_sec_qty;
12506   l_loaded_lines_rec        loaded_lines%ROWTYPE;
12507   l_lpn_cont_rec            lpn_content_rec;
12508   -- for query reservation
12509   l_qry_reservation_record  inv_reservation_global.mtl_reservation_rec_type;
12510   l_mtl_reservation_tab     inv_reservation_global.mtl_reservation_tbl_type;
12511   l_mtl_resv_tab_count      NUMBER;
12512   -- general
12513   l_order_line_id           NUMBER;
12514   l_order_header_id         NUMBER;
12515 
12516    /* Commented for bug 5262108
12517   -- to query tolerance
12518   l_minmaxinrectype         wsh_integration.minmaxinrectype;
12519   l_minmaxoutrectype        wsh_integration.minmaxoutrectype;
12520   l_minmaxinoutrectype      wsh_integration.minmaxinoutrectype; */
12521   l_max_shippable_quantity  NUMBER;
12522   l_max_shippable_sec_qty   NUMBER;
12523   l_total_resvd_qty         NUMBER;
12524   l_total_resvd_sec_qty     NUMBER;
12525   -- For create_reservation
12526   l_reservation_record      inv_reservation_global.mtl_reservation_rec_type;
12527   l_quantity_reserved_tmp   NUMBER;
12528   l_sec_qty_reserved_tmp    NUMBER;
12529   l_reservation_id          NUMBER;
12530   l_dummy_sn                inv_reservation_global.serial_number_tbl_type;
12531   l_demand_source_header_id NUMBER;
12532   l_demand_source_type_id   NUMBER;
12533   -- For Update reservation
12534   l_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type;
12535   l_old_upd_resv_rec        inv_reservation_global.mtl_reservation_rec_type;
12536   l_upd_dummy_sn            inv_reservation_global.serial_number_tbl_type;
12537   l_resv_id                 NUMBER;
12538   l_qty_updt_resv           NUMBER;
12539   l_sec_qty_updt_resv       NUMBER;
12540   l_qty_overship            NUMBER;
12541   l_sec_qty_overship        NUMBER;
12542   -- For quantity tree declaration
12543   l_transactable_qty        NUMBER;
12544   l_qoh                     NUMBER;
12545   l_rqoh                    NUMBER;
12546   l_qr                      NUMBER;
12547   l_qs                      NUMBER;
12548   l_atr                     NUMBER;
12549   l_sec_transactable_qty    NUMBER;
12550   l_sqoh                     NUMBER;
12551   l_srqoh                    NUMBER;
12552   l_sqr                      NUMBER;
12553   l_sqs                      NUMBER;
12554   l_satr                     NUMBER;
12555   -- for pjm validation
12556   l_end_item_unit_number    VARCHAR2(30);
12557   l_return_status           VARCHAR2(10);
12558   l_msg_count               NUMBER;
12559   l_msg_data                VARCHAR2(20000);
12560   l_error_code              NUMBER;
12561   l_temp_count              NUMBER;
12562   l_debug                   NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12563    --Added for bug 5262108
12564   l_allowed_flag            VARCHAR2(1);
12565   l_max_quantity_allowed    NUMBER;
12566   l_max_sec_qty_allowed     NUMBER;
12567   l_avail_req_quantity      NUMBER;
12568   l_avail_req_sec_qty       NUMBER;
12569   l_staged_qty              NUMBER := 0;  -- Bug#5262108
12570   l_staged_sec_qty          NUMBER := 0;
12571   l_dual_uom_item           BOOLEAN := FALSE;
12572 BEGIN
12573   IF (l_debug = 1) THEN
12574      DEBUG('p_lpn_id=' || p_lpn_id, 'Perform_Overship_Distribution');
12575      DEBUG('p_org_id=' || p_org_id, 'Perform_Overship_Distribution');
12576      DEBUG('p_dock_door_id=' || p_dock_door_id, 'Perform_Overship_Distribution');
12577      DEBUG('lpn_content_record count=' || g_lpn_contents_tab.COUNT, 'Perform_Overship_Distribution');
12578   END IF;
12579 
12580   x_return_status  := fnd_api.g_ret_sts_success;
12581 
12582   -- clear cache
12583   l_lpn_cont_qty_used.DELETE;
12584   l_lpn_sec_qty_used.DELETE;
12585 
12586   FOR l_index IN 1 .. g_lpn_contents_tab.COUNT LOOP --{
12587       l_lpn_cont_rec  := g_lpn_contents_tab(l_index);
12588 
12589       IF (l_debug = 1) THEN
12590          DEBUG('lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
12591       END IF;
12592 
12593       IF (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity > 0)
12594          OR (g_fulfillment_base = 'S' AND NVL(l_lpn_cont_rec.secondary_quantity,0) > 0) --{
12595       THEN -- overship needed
12596          IF inv_cache.set_item_rec(p_org_id, l_lpn_cont_rec.inventory_item_id) THEN
12597             IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
12598                l_dual_uom_item := TRUE;
12599                IF (l_debug = 1) THEN
12600                   DEBUG('Item is dual UOM', 'Perform_Overship_Distribution');
12601                END IF;
12602             ELSE
12603                l_dual_uom_item := FALSE;
12604                IF (l_debug = 1) THEN
12605                   DEBUG('Item is NOT dual UOM', 'Perform_Overship_Distribution');
12606                END IF;
12607             END IF;
12608          ELSE
12609             IF (l_debug = 1) THEN
12610                DEBUG('Error from inv_cache.set_item_rec', 'Perform_Overship_Distribution');
12611             END IF;
12612             RAISE fnd_api.g_exc_unexpected_error;
12613          END IF;
12614 
12615          l_qry_reservation_record.lpn_id                 := l_lpn_cont_rec.lpn_id;
12616          l_qry_reservation_record.inventory_item_id      := l_lpn_cont_rec.inventory_item_id;
12617          l_qry_reservation_record.revision               := l_lpn_cont_rec.revision;
12618          l_qry_reservation_record.lot_number             := l_lpn_cont_rec.lot_number;
12619          l_qry_reservation_record.supply_source_type_id  := inv_reservation_global.g_source_type_inv;
12620 
12621          IF (l_debug = 1) THEN
12622             DEBUG('Before call to query reservation', 'Perform_Overship_Distribution');
12623          END IF;
12624 
12625          inv_reservation_pub.query_reservation(
12626              p_api_version_number         => 1.0
12627            , p_init_msg_lst               => fnd_api.g_false
12628            , x_return_status              => l_return_status
12629            , x_msg_count                  => l_msg_count
12630            , x_msg_data                   => l_msg_data
12631            , p_query_input                => l_qry_reservation_record
12632            , p_lock_records               => fnd_api.g_false
12633            , x_mtl_reservation_tbl        => l_mtl_reservation_tab
12634            , x_mtl_reservation_tbl_count  => l_mtl_resv_tab_count
12635            , x_error_code                 => l_error_code
12636            );
12637 
12638          IF l_return_status = fnd_api.g_ret_sts_error THEN
12639             IF (l_debug = 1) THEN
12640                DEBUG('Query reservation failed', 'Perform_Overship_Distribution');
12641             END IF;
12642 
12643             RAISE fnd_api.g_exc_error;
12644          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12645             IF (l_debug = 1) THEN
12646                DEBUG('Query reservation failed', 'Perform_Overship_Distribution');
12647             END IF;
12648 
12649             RAISE fnd_api.g_exc_unexpected_error;
12650          END IF;
12651 
12652          IF (l_debug = 1) THEN
12653             DEBUG('After call to query reservation', 'Perform_Overship_Distribution');
12654          END IF;
12655 
12656          -- if reservation exists try to overship on already reaserved line
12657          IF (l_debug = 1) THEN
12658             DEBUG('l_mtl_resv_tab_count= ' || l_mtl_resv_tab_count, 'Perform_Overship_Distribution');
12659          END IF;
12660 
12661          IF l_mtl_resv_tab_count > 0 THEN --{
12662             FOR l_index_r IN 1 .. l_mtl_resv_tab_count LOOP --{
12663                 IF l_mtl_reservation_tab(l_index_r).demand_source_type_id NOT IN (2,8) THEN
12664                    IF (l_debug = 1) THEN
12665                       DEBUG('Non-Sales Order reservation exists, error out', 'Perform_Overship_Distribution');
12666                    END IF;
12667                     fnd_message.set_name('WMS', 'WMS_LINE_NOT_BOOKED');
12668                     fnd_msg_pub.ADD;
12669                    RAISE fnd_api.g_exc_error;
12670                 END IF;
12671 
12672                 l_order_line_id := l_mtl_reservation_tab(l_index_r).demand_source_line_id;
12673                 -- get oe_order_header_id from sales order header id
12674                 inv_salesorder.get_oeheader_for_salesorder(
12675                     p_salesorder_id => l_mtl_reservation_tab(l_index_r).demand_source_header_id
12676                   , x_oe_header_id  => l_order_header_id
12677                   , x_return_status => l_return_status
12678                   );
12679                 l_demand_source_type_id := get_demand_src_type(l_order_header_id);
12680 
12681                 IF (l_debug = 1) THEN
12682                    DEBUG('l_order_line_id = ' || l_order_line_id, 'Perform_Overship_Distribution');
12683                    DEBUG('l_demand_source_type_id = ' || l_demand_source_type_id, 'Perform_Overship_Distribution');
12684                    DEBUG('l_oe_order_header_id = ' || l_order_header_id, 'Perform_Overship_Distribution');
12685                 END IF;
12686 
12687                 l_demand_source_header_id := l_mtl_reservation_tab(l_index_r).demand_source_header_id;
12688 
12689                 -- validate end unit
12690                 BEGIN
12691                    SELECT end_item_unit_number
12692                      INTO l_end_item_unit_number
12693                      FROM oe_order_lines_all
12694                     WHERE line_id = l_order_line_id;
12695 
12696                    IF (l_debug = 1) THEN
12697                       DEBUG('l_end_item_unit_number=' || l_end_item_unit_number, 'Perform_Overship_Distribution');
12698                    END IF;
12699 
12700                    IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(l_index, Trim(l_end_item_unit_number)) THEN --bug11659908
12701                       IF (l_debug = 1) THEN
12702                          DEBUG('End unit not matching, skipping line', 'Perform_Overship_Distribution');
12703                       END IF;
12704                       EXIT;
12705                    END IF;
12706                 EXCEPTION
12707                    WHEN NO_DATA_FOUND THEN
12708                       IF (l_debug = 1) THEN
12709                          DEBUG('No data found for line=' || l_order_line_id, 'Perform_Overship_Distribution');
12710                       END IF;
12711 
12712                       RAISE fnd_api.g_exc_error;
12713                 END;
12714 
12715                 IF (l_debug = 1) THEN
12716                    DEBUG('Before call to WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick', 'Perform_Overship_Distribution');
12717                    DEBUG('l_order_line_id=' || l_order_line_id, 'Perform_Overship_Distribution');
12718                 END IF;
12719 
12720                 -- Begin Bug 5262108
12721                 WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
12722                     p_order_line_id         => l_order_line_id
12723                   , p_quantity_to_pick      => 0
12724                   , p_quantity2_to_pick     => 0
12725                   , x_allowed_flag          => l_allowed_flag
12726                   , x_max_quantity_allowed  => l_max_quantity_allowed
12727                   , x_max_quantity2_allowed => l_max_sec_qty_allowed
12728                   , x_avail_req_quantity    => l_avail_req_quantity
12729                   , x_avail_req_quantity2   => l_avail_req_sec_qty
12730                   , x_return_status         => l_return_status);
12731 
12732                 IF (l_debug = 1) THEN
12733                    DEBUG('l_allowed_flag='||l_allowed_flag,'Perform_Overship_Distribution');
12734                    DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Perform_Overship_Distribution');
12735                    DEBUG('l_max_sec_qty_allowed='||l_max_sec_qty_allowed,'Perform_Overship_Distribution');
12736                    DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Perform_Overship_Distribution');
12737                    DEBUG('l_avail_req_sec_qty='||l_avail_req_sec_qty,'Perform_Overship_Distribution');
12738                    DEBUG('l_return_status='||l_return_status,'Perform_Overship_Distribution');
12739                 END IF;
12740 
12741                 -- End bug 5262108
12742 
12743                 IF l_return_status = fnd_api.g_ret_sts_error THEN
12744                    IF (l_debug = 1) THEN
12745                       DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
12746                    END IF;
12747 
12748                    RAISE fnd_api.g_exc_error;
12749                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12750                    IF (l_debug = 1) THEN
12751                       DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
12752                    END IF;
12753 
12754                    RAISE fnd_api.g_exc_unexpected_error;
12755                 END IF;
12756 
12757                 -- commented for 5262108  l_max_shippable_quantity := l_minmaxoutrectype.max_remaining_quantity; --Removed trunc bug3994915
12758 
12759                 l_max_shippable_quantity := l_max_quantity_allowed;  -- 5262108
12760                 IF l_dual_uom_item THEN
12761                    l_max_shippable_sec_qty := l_max_sec_qty_allowed;
12762                 END IF;
12763 
12764                 IF (l_debug = 1) THEN
12765                    DEBUG('max_remaining_quantity = ' || l_max_quantity_allowed, 'Perform_Overship_Distribution');
12766                 END IF;
12767 
12768                 -- get total reserved quantity for this line
12769                 BEGIN
12770                    SELECT NVL(SUM(primary_reservation_quantity),0)
12771                         , NVL(SUM(secondary_reservation_quantity),0)
12772                      INTO l_total_resvd_qty
12773                         , l_total_resvd_sec_qty
12774                      FROM mtl_reservations
12775                     WHERE demand_source_header_id = l_mtl_reservation_tab(l_index_r).demand_source_header_id
12776                       AND demand_source_line_id = l_order_line_id
12777                       AND demand_source_type_id = l_demand_source_type_id
12778                       AND demand_source_line_detail IS NULL
12779                       AND NVL(staged_flag,'N') = 'Y'; --Bug#5262108
12780                 EXCEPTION
12781                    WHEN OTHERS THEN
12782                       IF (l_debug = 1) THEN
12783                          DEBUG('Exception getting total reserved quantity for line = ' || l_order_line_id, 'Perform_Overship_Distribution');
12784                       END IF;
12785 
12786                       RAISE fnd_api.g_exc_unexpected_error;
12787                 END;
12788 
12789                 inv_quantity_tree_pub.clear_quantity_cache;
12790 
12791                 -- query quantity tree
12792                 IF (l_debug = 1) THEN
12793                    DEBUG('Before query quantity tree ', 'Perform_Overship_Distribution');
12794                    DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Perform_Overship_Distribution');
12795                    DEBUG('l_order_line_id =' || l_order_line_id, 'Perform_Overship_Distribution');
12796                    DEBUG('l_demand_source_type_id =' || l_demand_source_type_id, 'Perform_Overship_Distribution');
12797                    DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Perform_Overship_Distribution');
12798                    DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Perform_Overship_Distribution');
12799                    DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Perform_Overship_Distribution');
12800                    DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Perform_Overship_Distribution');
12801                    DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
12802                 END IF;
12803 
12804                 inv_quantity_tree_pub.query_quantities(
12805                   p_api_version_number         => 1.0
12806                 , p_init_msg_lst               => fnd_api.g_false
12807                 , x_return_status              => l_return_status
12808                 , x_msg_count                  => l_msg_count
12809                 , x_msg_data                   => l_msg_data
12810                 , p_organization_id            => p_org_id
12811                 , p_inventory_item_id          => l_lpn_cont_rec.inventory_item_id
12812                 , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
12813                 , p_is_revision_control        => l_lpn_cont_rec.revision_control
12814                 , p_is_lot_control             => l_lpn_cont_rec.lot_control
12815                 , p_is_serial_control          => l_lpn_cont_rec.serial_control
12816                 , p_grade_code                 => NULL
12817                 , p_demand_source_type_id      => l_demand_source_type_id
12818                 , p_demand_source_header_id    => l_demand_source_header_id
12819                 , p_demand_source_line_id      => l_order_line_id
12820                 , p_revision                   => l_lpn_cont_rec.revision
12821                 , p_lot_number                 => l_lpn_cont_rec.lot_number
12822                 , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
12823                 , p_locator_id                 => l_lpn_cont_rec.locator_id
12824                 , x_qoh                        => l_qoh
12825                 , x_rqoh                       => l_rqoh
12826                 , x_qr                         => l_qr
12827                 , x_qs                         => l_qs
12828                 , x_att                        => l_transactable_qty
12829                 , x_atr                        => l_atr
12830                 , x_sqoh                       => l_sqoh
12831                 , x_srqoh                      => l_srqoh
12832                 , x_sqr                        => l_sqr
12833                 , x_sqs                        => l_sqs
12834                 , x_satt                       => l_sec_transactable_qty
12835                 , x_satr                       => l_satr
12836                 , p_lpn_id                     => l_lpn_cont_rec.lpn_id
12837                 );
12838 
12839                 IF l_return_status = fnd_api.g_ret_sts_error THEN
12840                    IF (l_debug = 1) THEN
12841                       DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
12842                    END IF;
12843 
12844                    RAISE fnd_api.g_exc_error;
12845                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12846                    IF (l_debug = 1) THEN
12847                       DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
12848                    END IF;
12849 
12850                    RAISE fnd_api.g_exc_unexpected_error;
12851                 END IF;
12852 
12853                 IF (l_debug = 1) THEN
12854                   DEBUG('Query tree Return status is ' || l_return_status, 'Perform_Overship_Distribution');
12855                   DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Perform_Overship_Distribution');
12856                   DEBUG('l_sec_transactable_qty= ' || l_sec_transactable_qty, 'Perform_Overship_Distribution');
12857                   DEBUG('After query quantity tree ', 'Perform_Overship_Distribution');
12858                 END IF;
12859 
12860                 IF (g_fulfillment_base = 'P' AND l_transactable_qty > 0)
12861                    OR (g_fulfillment_base = 'S' AND NVL(l_sec_transactable_qty,0) > 0) --{
12862                 THEN
12863                    -- updating existing reservation record
12864                    -- Bug#5262108.query the staged qty.
12865                    SELECT NVL(SUM(picked_quantity),0)
12866                         , NVL(SUM(picked_quantity2),0)
12867                      INTO l_staged_qty
12868                         , l_staged_sec_qty
12869                      FROM wsh_delivery_details_ob_grp_v
12870                     WHERE source_header_id = l_order_header_id
12871                       AND source_line_id = l_order_line_id
12872                       AND released_status = 'Y';
12873 
12874                 IF (l_debug = 1) THEN
12875                   DEBUG('l_lpn_cont_rec.secondary_quantity= ' || l_lpn_cont_rec.secondary_quantity, 'Perform_Overship_Distribution');
12876                   DEBUG('l_max_shippable_sec_qty= ' || l_max_shippable_sec_qty, 'Perform_Overship_Distribution');
12877                   DEBUG('l_staged_sec_qty= ' || l_staged_sec_qty, 'Perform_Overship_Distribution');
12878                   DEBUG('l_total_resvd_sec_qty= ' || l_total_resvd_sec_qty, 'Perform_Overship_Distribution');
12879                   DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Perform_Overship_Distribution');
12880                 END IF;
12881 
12882                    IF(g_fulfillment_base = 'S' AND l_dual_uom_item) THEN
12883                         IF((NVL(l_lpn_cont_rec.secondary_quantity,0) <= NVL(l_sec_transactable_qty,0)) AND
12884                             (NVL(l_lpn_cont_rec.secondary_quantity,0) <= NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0))) THEN
12885                                 l_sec_qty_overship := l_lpn_cont_rec.secondary_quantity;
12886                                 l_qty_overship := l_lpn_cont_rec.quantity;
12887                         ELSIF((NVL(l_sec_transactable_qty, 0) <= NVL(l_lpn_cont_rec.secondary_quantity,0)) AND
12888                                 (NVL(l_sec_transactable_qty, 0) <= NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0))) THEN
12889                                 l_sec_qty_overship := NVL(l_sec_transactable_qty,0);
12890                                 l_qty_overship := l_transactable_qty;
12891                         ELSE
12892                                 l_sec_qty_overship := NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0);
12893                                 l_qty_overship := ROUND(((l_transactable_qty * l_sec_qty_overship)/l_sec_transactable_qty),5);
12894                         END IF;
12895 
12896                         IF(l_sec_qty_overship = l_lpn_cont_rec.secondary_quantity) THEN --If entire sec is being shipped then consume entire primary as is.
12897                             l_qty_overship := l_lpn_cont_rec.quantity;
12898                         END IF;
12899 
12900                    ELSE
12901                         IF((NVL(l_lpn_cont_rec.quantity,0) <= NVL(l_transactable_qty,0)) AND
12902                             (NVL(l_lpn_cont_rec.quantity,0) <= NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0))) THEN
12903                                 l_qty_overship := l_lpn_cont_rec.quantity;
12904                                 IF l_dual_uom_item THEN
12905                                     l_sec_qty_overship := l_lpn_cont_rec.secondary_quantity;
12906                                 END IF;
12907                         ELSIF((NVL(l_transactable_qty, 0) <= NVL(l_lpn_cont_rec.quantity,0)) AND
12908                                 (NVL(l_transactable_qty, 0) <= NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0))) THEN
12909                                 l_qty_overship := NVL(l_transactable_qty,0);
12910                                 IF l_dual_uom_item THEN
12911                                     l_sec_qty_overship := l_sec_transactable_qty;
12912                                 END IF;
12913                         ELSE
12914                                 l_qty_overship := NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0);
12915                                 IF l_dual_uom_item THEN
12916                                     l_sec_qty_overship := NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0);
12917                                 END IF;
12918                         END IF;
12919 
12920                         IF(l_qty_overship = l_lpn_cont_rec.quantity) THEN --If entire pri is being shipped then consume entire sec as is.
12921                             l_sec_qty_overship := l_lpn_cont_rec.secondary_quantity;
12922                         END IF;
12923 
12924                    END IF;
12925 
12926                    l_qty_updt_resv := l_mtl_reservation_tab(l_index_r).primary_reservation_quantity + l_qty_overship;
12927                    IF l_dual_uom_item THEN
12928                       l_sec_qty_updt_resv := l_mtl_reservation_tab(l_index_r).secondary_reservation_quantity + l_sec_qty_overship;
12929                    END IF;
12930 
12931                    IF (l_debug = 1) THEN
12932                       DEBUG('l_staged_qty  ' || l_staged_qty , 'Perform_Overship_Distribution');
12933                       DEBUG('l_lpn_cont_rec.quantity  ' || l_lpn_cont_rec.quantity, 'Perform_Overship_Distribution');
12934                       DEBUG('l_total_resvd_qty '||l_total_resvd_qty, 'Perform_Overship_Distribution');
12935                       DEBUG('l_qty_overship '||l_qty_overship, 'Perform_Overship_Distribution');
12936                       DEBUG('l_sec_qty_overship '||l_sec_qty_overship, 'Perform_Overship_Distribution');
12937                       DEBUG('l_qty_updt_resv '||l_qty_updt_resv, 'Perform_Overship_Distribution');
12938                       DEBUG('l_sec_qty_updt_resv '||l_sec_qty_updt_resv, 'Perform_Overship_Distribution');
12939                    END IF;
12940 
12941                    IF (g_fulfillment_base = 'P' AND l_qty_overship > 0)
12942                       OR (g_fulfillment_base = 'S' AND NVL(l_sec_qty_overship,0) > 0) --{
12943                    THEN
12944                       l_old_upd_resv_rec.reservation_id := l_mtl_reservation_tab(l_index_r).reservation_id;
12945                       l_upd_resv_rec.primary_reservation_quantity := l_qty_updt_resv;
12946 
12947                       IF l_dual_uom_item THEN
12948                          l_upd_resv_rec.secondary_reservation_quantity := l_sec_qty_updt_resv;
12949                       END IF;
12950 
12951                       IF l_mtl_reservation_tab(l_index_r).reservation_uom_code =
12952                          l_mtl_reservation_tab(l_index_r).primary_uom_code
12953                       THEN
12954                          l_upd_resv_rec.reservation_quantity := l_qty_updt_resv;
12955                       ELSIF l_dual_uom_item
12956                             AND l_mtl_reservation_tab(l_index_r).reservation_uom_code =
12957                                 NVL(l_mtl_reservation_tab(l_index_r).secondary_uom_code,'@@@')
12958                       THEN
12959                          l_upd_resv_rec.reservation_quantity := l_sec_qty_updt_resv;
12960                       ELSE
12961                          IF g_fulfillment_base = 'S' THEN
12962                             l_upd_resv_rec.reservation_quantity :=
12963                               inv_convert.inv_um_convert(
12964                                   item_id           => l_lpn_cont_rec.inventory_item_id
12965                                 , lot_number        => l_lpn_cont_rec.lot_number
12966                                 , organization_id   => p_org_id
12967                                 , precision         => NULL
12968                                 , from_quantity     => l_sec_qty_updt_resv
12969                                 , from_unit         => l_mtl_reservation_tab(l_index_r).secondary_uom_code
12970                                 , to_unit           => l_mtl_reservation_tab(l_index_r).reservation_uom_code
12971                                 , from_name         => NULL
12972                                 , to_name           => NULL);
12973                          ELSE
12974                             l_upd_resv_rec.reservation_quantity :=
12975                               inv_convert.inv_um_convert(
12976                                   item_id           => l_lpn_cont_rec.inventory_item_id
12977                                 , lot_number        => l_lpn_cont_rec.lot_number
12978                                 , organization_id   => p_org_id
12979                                 , precision         => NULL
12980                                 , from_quantity     => l_qty_updt_resv
12981                                 , from_unit         => l_mtl_reservation_tab(l_index_r).primary_uom_code
12982                                 , to_unit           => l_mtl_reservation_tab(l_index_r).reservation_uom_code
12983                                 , from_name         => NULL
12984                                 , to_name           => NULL);
12985                          END IF;
12986                       END IF;
12987 
12988                       IF (l_debug = 1) THEN
12989                          DEBUG('The reservation_id to update = ' || l_old_upd_resv_rec.reservation_id, 'Perform_Overship_Distribution');
12990                          DEBUG('Before call to update reservation', 'Perform_Overship_Distribution');
12991                       END IF;
12992 
12993                       inv_reservation_pub.update_reservation(
12994                         p_api_version_number         => 1.0
12995                       , p_init_msg_lst               => fnd_api.g_false
12996                       , x_return_status              => l_return_status
12997                       , x_msg_count                  => l_msg_count
12998                       , x_msg_data                   => l_msg_data
12999                       , p_original_rsv_rec           => l_old_upd_resv_rec
13000                       , p_to_rsv_rec                 => l_upd_resv_rec
13001                       , p_original_serial_number     => l_upd_dummy_sn
13002                       , p_to_serial_number           => l_upd_dummy_sn
13003                       , p_validation_flag            => fnd_api.g_true
13004                       , p_over_reservation_flag      => 3
13005                       );
13006 
13007                       IF l_return_status = fnd_api.g_ret_sts_error THEN
13008                          IF (l_debug = 1) THEN
13009                             DEBUG('Update reservation failed ', 'Perform_Overship_Distribution');
13010                          END IF;
13011 
13012                          RAISE fnd_api.g_exc_error;
13013                       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13014                          IF (l_debug = 1) THEN
13015                             DEBUG('Update reservation failed', 'Perform_Overship_Distribution');
13016                          END IF;
13017 
13018                          RAISE fnd_api.g_exc_unexpected_error;
13019                       END IF;
13020 
13021                       IF (l_debug = 1) THEN
13022                          DEBUG('Update reservation is successful ', 'Perform_Overship_Distribution');
13023                          DEBUG('After call to update reservation', 'Perform_Overship_Distribution');
13024                       END IF;
13025 
13026                       -- update cache quantities
13027                       g_lpn_contents_tab(l_index).quantity := g_lpn_contents_tab(l_index).quantity - l_qty_overship;
13028                       l_lpn_cont_rec.quantity              := l_lpn_cont_rec.quantity - l_qty_overship;
13029                       g_total_lpn_quantity                 := g_total_lpn_quantity - l_qty_overship;
13030 
13031                       IF l_dual_uom_item THEN
13032                          g_lpn_contents_tab(l_index).secondary_quantity :=
13033                              g_lpn_contents_tab(l_index).secondary_quantity - l_sec_qty_overship;
13034                          l_lpn_cont_rec.secondary_quantity := l_lpn_cont_rec.secondary_quantity - l_sec_qty_overship;
13035                          g_total_lpn_sec_qty := g_total_lpn_sec_qty - l_sec_qty_overship;
13036                       END IF;
13037 
13038                       -- bug#2830138
13039                       -- make backup of quantities used
13040                       -- will be used if overship has to be rollbacked
13041                       IF l_lpn_cont_qty_used.EXISTS(l_index) THEN
13042                          l_lpn_cont_qty_used(l_index) := l_lpn_cont_qty_used(l_index) + l_qty_overship;
13043                       ELSE
13044                          l_lpn_cont_qty_used(l_index) := l_qty_overship;
13045                       END IF;
13046 
13047                       IF l_dual_uom_item THEN
13048                          IF l_lpn_sec_qty_used.EXISTS(l_index) THEN
13049                             l_lpn_sec_qty_used(l_index) := l_lpn_sec_qty_used(l_index) + l_sec_qty_overship;
13050                          ELSE
13051                             l_lpn_sec_qty_used(l_index) := l_sec_qty_overship;
13052                          END IF;
13053                       END IF;
13054 
13055                       -- update wds for processed_quantity=processed_quantity+g_total_lpn_quantity
13056                       BEGIN
13057                          UPDATE wms_direct_ship_temp
13058                             SET processed_quantity = (processed_quantity + l_qty_overship)
13059                               , secondary_processed_qty = (NVL(secondary_processed_qty,0) + NVL(l_sec_qty_overship,0))
13060                           WHERE organization_id = p_org_id
13061                             AND group_id = g_group_id
13062                             AND lpn_id = p_lpn_id
13063                             AND order_line_id = l_order_line_id;
13064                       EXCEPTION
13065                          WHEN OTHERS THEN
13066                             IF (l_debug = 1) THEN
13067                                DEBUG('Exception updating WDS', 'Perform_Overship_Distribution');
13068                             END IF;
13069 
13070                             RAISE fnd_api.g_exc_unexpected_error;
13071                       END;
13072                    END IF; --} l_qty_overship >0
13073 
13074                    -- if all quantity consumed exit to lpn_content loop
13075 
13076                    IF (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity = 0)
13077                       OR (g_fulfillment_base = 'S' AND NVL(l_lpn_cont_rec.secondary_quantity,0) = 0)
13078                    THEN
13079                       EXIT;
13080                    END IF;
13081                 END IF; --} l_transactable_qty>0
13082             END LOOP; --} loop 1 .. l_mtl_resv_tab_count
13083          END IF; --} l_mtl_resv_tab_count > 0
13084 
13085          IF (l_debug = 1) THEN
13086             DEBUG('Reservation update completed', 'Perform_Overship_Distribution');
13087          END IF;
13088 
13089          /* Bug#6062135. If still qty left in LPN, it may be because of multipple WLC records.
13090             We have to see if theyc an be over shipped */
13091 
13092          -- if more quantity remaining in lpn_content record the create new reservation
13093          IF (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity > 0)
13094             OR (g_fulfillment_base = 'S' AND NVL(l_lpn_cont_rec.secondary_quantity,0) > 0) --{
13095          THEN
13096             -- get all lines loaded to this lpn
13097             OPEN loaded_lines(l_lpn_cont_rec.inventory_item_id);
13098             LOOP --{
13099                FETCH loaded_lines INTO l_loaded_lines_rec;
13100                EXIT WHEN loaded_lines%NOTFOUND;
13101 
13102                IF (l_debug = 1) THEN
13103                   DEBUG('Inside create reservation part', 'Perform_Overship_Distribution');
13104                END IF;
13105 
13106                --validate end unit
13107                IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(l_index, TRIM(l_loaded_lines_rec.end_item_unit_number)) THEN --bug11659908
13108                   IF (l_debug = 1) THEN
13109                      DEBUG('End unit not matching, skipping line', 'Perform_Overship_Distribution');
13110                   END IF;
13111 
13112                   EXIT;
13113                END IF;
13114 
13115                IF (l_loaded_lines_rec.revision = l_lpn_cont_rec.revision --{
13116                   OR l_loaded_lines_rec.revision IS NULL) THEN
13117                   -- get demand source header id
13118                   l_demand_source_header_id := inv_salesorder.get_salesorder_for_oeheader(l_loaded_lines_rec.order_header_id);
13119                   l_demand_source_type_id   := get_demand_src_type(l_loaded_lines_rec.order_header_id);
13120                   l_order_line_id           := l_loaded_lines_rec.order_line_id;
13121 
13122                   -- get total reserved quantity for line
13123                   BEGIN
13124                      SELECT NVL(SUM(primary_reservation_quantity),0)
13125                           , NVL(SUM(secondary_reservation_quantity),0)
13126                        INTO l_total_resvd_qty
13127                           , l_total_resvd_sec_qty
13128                        FROM mtl_reservations
13129                       WHERE demand_source_header_id = l_demand_source_header_id
13130                         AND demand_source_line_id = l_order_line_id
13131                         AND demand_source_type_id = l_demand_source_type_id
13132                         AND demand_source_line_detail IS NULL
13133                         AND NVL(staged_flag,'N') = 'Y' ;
13134                   EXCEPTION
13135                      WHEN OTHERS THEN
13136                         IF (l_debug = 1) THEN
13137                            DEBUG('Exception getting total reserved quantity for line = ' || l_order_line_id, 'Perform_Overship_Distribution');
13138                         END IF;
13139 
13140                         RAISE fnd_api.g_exc_unexpected_error;
13141                   END;
13142 
13143                   -- Begin Bug 4908571
13144                   WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
13145                       p_order_line_id         => l_order_line_id
13146                     , p_quantity_to_pick      => 0
13147                     , p_quantity2_to_pick     => 0
13148                     , x_allowed_flag          => l_allowed_flag
13149                     , x_max_quantity_allowed  => l_max_quantity_allowed
13150                     , x_max_quantity2_allowed => l_max_sec_qty_allowed
13151                     , x_avail_req_quantity    => l_avail_req_quantity
13152                     , x_avail_req_quantity2   => l_avail_req_sec_qty
13153                     , x_return_status         => l_return_status);
13154 
13155                   IF (l_debug = 1) THEN
13156                      DEBUG('l_allowed_flag='||l_allowed_flag,'Perform_Overship_Distribution');
13157                      DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Perform_Overship_Distribution');
13158                      DEBUG('l_max_sec_qty_allowed='||l_max_sec_qty_allowed,'Perform_Overship_Distribution');
13159                      DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Perform_Overship_Distribution');
13160                      DEBUG('l_avail_req_sec_qty='||l_avail_req_sec_qty,'Perform_Overship_Distribution');
13161                      DEBUG('l_return_status='||l_return_status,'Perform_Overship_Distribution');
13162                   END IF;
13163 
13164                   -- End bug 4908571
13165 
13166                   IF l_return_status = fnd_api.g_ret_sts_error THEN
13167                      IF (l_debug = 1) THEN
13168                         DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
13169                      END IF;
13170 
13171                      RAISE fnd_api.g_exc_error;
13172                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13173                      IF (l_debug = 1) THEN
13174                         DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
13175                      END IF;
13176 
13177                      RAISE fnd_api.g_exc_unexpected_error;
13178                   END IF;
13179 
13180                   inv_quantity_tree_pub.clear_quantity_cache;
13181 
13182                   -- query quantity tree
13183                   IF (l_debug = 1) THEN
13184                      DEBUG('Before query quantity tree ', 'Perform_Overship_Distribution');
13185                      DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Perform_Overship_Distribution');
13186                      DEBUG('l_order_line_id =' || l_order_line_id, 'Perform_Overship_Distribution');
13187                      DEBUG('l_demand_source_type_id =' || l_demand_source_type_id, 'Perform_Overship_Distribution');
13188                      DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Perform_Overship_Distribution');
13189                      DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Perform_Overship_Distribution');
13190                      DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Perform_Overship_Distribution');
13191                      DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Perform_Overship_Distribution');
13192                      DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
13193                   END IF;
13194 
13195                   inv_quantity_tree_pub.query_quantities(
13196                     p_api_version_number         => 1.0
13197                   , p_init_msg_lst               => fnd_api.g_false
13198                   , x_return_status              => l_return_status
13199                   , x_msg_count                  => l_msg_count
13200                   , x_msg_data                   => l_msg_data
13201                   , p_organization_id            => p_org_id
13202                   , p_inventory_item_id          => l_lpn_cont_rec.inventory_item_id
13203                   , p_tree_mode                  => inv_quantity_tree_pub.g_reservation_mode
13204                   , p_is_revision_control        => l_lpn_cont_rec.revision_control
13205                   , p_is_lot_control             => l_lpn_cont_rec.lot_control
13206                   , p_is_serial_control          => l_lpn_cont_rec.serial_control
13207                   , p_grade_code                 => NULL
13208                   , p_demand_source_type_id      => l_demand_source_type_id
13209                   , p_demand_source_header_id    => l_demand_source_header_id
13210                   , p_demand_source_line_id      => l_order_line_id
13211                   , p_revision                   => l_lpn_cont_rec.revision
13212                   , p_lot_number                 => l_lpn_cont_rec.lot_number
13213                   , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
13214                   , p_locator_id                 => l_lpn_cont_rec.locator_id
13215                   , x_qoh                        => l_qoh
13216                   , x_rqoh                       => l_rqoh
13217                   , x_qr                         => l_qr
13218                   , x_qs                         => l_qs
13219                   , x_att                        => l_transactable_qty
13220                   , x_atr                        => l_atr
13221                   , x_sqoh                       => l_sqoh
13222                   , x_srqoh                      => l_srqoh
13223                   , x_sqr                        => l_sqr
13224                   , x_sqs                        => l_sqs
13225                   , x_satt                       => l_sec_transactable_qty
13226                   , x_satr                       => l_satr
13227                   , p_lpn_id                     => l_lpn_cont_rec.lpn_id
13228                   );
13229 
13230                   IF l_return_status = fnd_api.g_ret_sts_error THEN
13231                      IF (l_debug = 1) THEN
13232                         DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
13233                      END IF;
13234 
13235                      RAISE fnd_api.g_exc_error;
13236                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13237                      IF (l_debug = 1) THEN
13238                         DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
13239                      END IF;
13240 
13241                      RAISE fnd_api.g_exc_unexpected_error;
13242                   END IF;
13243 
13244                   IF (l_debug = 1) THEN
13245                      DEBUG('Query tree Return status is ' || l_return_status, 'Perform_Overship_Distribution');
13246                      DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Perform_Overship_Distribution');
13247                      DEBUG('After query quantity tree ', 'Perform_Overship_Distribution');
13248                   END IF;
13249 
13250                   IF (g_fulfillment_base = 'P' AND l_transactable_qty > 0)
13251                      OR (g_fulfillment_base = 'S' AND NVL(l_sec_transactable_qty,0) > 0) --{
13252                   THEN
13253                      l_max_shippable_quantity  := l_max_quantity_allowed;
13254                      IF l_dual_uom_item THEN
13255                         l_max_shippable_sec_qty := l_max_sec_qty_allowed;
13256                      END IF;
13257 
13258                      IF (l_debug = 1) THEN
13259                         DEBUG('max_remaining_quantity = ' || l_max_quantity_allowed, 'Perform_Overship_Distribution');
13260                         DEBUG('max_remaining_sec_qty = ' || l_max_sec_qty_allowed, 'Perform_Overship_Distribution');
13261                      END IF;
13262 
13263                      -- Bug#6071394.query the staged qty.
13264                      SELECT NVL(SUM(picked_quantity),0)
13265                           , NVL(SUM(picked_quantity2),0)
13266                        INTO l_staged_qty
13267                           , l_staged_sec_qty
13268                        FROM wsh_delivery_details_ob_grp_v
13269                       WHERE source_header_id = l_order_header_id
13270                         AND source_line_id = l_order_line_id
13271                         AND released_status = 'Y';
13272 
13273                      IF (l_debug = 1) THEN
13274                         DEBUG(' l_total_resvd_qty = ' || l_total_resvd_qty, 'Perform_Overship_Distribution');
13275                         DEBUG(' l_total_resvd_sec_qty = ' || l_total_resvd_sec_qty, 'Perform_Overship_Distribution');
13276                         DEBUG(' l_staged_qty = ' || l_staged_qty, 'Perform_Overship_Distribution');
13277                         DEBUG(' l_staged_sec_qty = ' || l_staged_sec_qty, 'Perform_Overship_Distribution');
13278                         DEBUG(' l_transactable_qty = ' || l_transactable_qty, 'Perform_Overship_Distribution');
13279                         DEBUG(' l_sec_transactable_qty = ' || l_sec_transactable_qty, 'Perform_Overship_Distribution');
13280                         DEBUG(' l_lpn_cont_rec.secondary_quantity = ' || l_lpn_cont_rec.secondary_quantity, 'Perform_Overship_Distribution');
13281                         DEBUG(' l_lpn_cont_rec.quantity = ' || l_lpn_cont_rec.quantity, 'Perform_Overship_Distribution');
13282                      END IF;
13283 
13284                    IF(g_fulfillment_base = 'S' AND l_dual_uom_item) THEN
13285                         IF((NVL(l_lpn_cont_rec.secondary_quantity,0) <= NVL(l_sec_transactable_qty,0)) AND
13286                             (NVL(l_lpn_cont_rec.secondary_quantity,0) <= NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0))) THEN
13287                                 l_sec_qty_overship := l_lpn_cont_rec.secondary_quantity;
13288                                 l_qty_overship := l_lpn_cont_rec.quantity;
13289                         ELSIF((NVL(l_sec_transactable_qty, 0) <= NVL(l_lpn_cont_rec.secondary_quantity,0)) AND
13290                                 (NVL(l_sec_transactable_qty, 0) <= NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0))) THEN
13291                                 l_sec_qty_overship := NVL(l_sec_transactable_qty,0);
13292                                 l_qty_overship := l_transactable_qty;
13293                         ELSE
13294                                 l_sec_qty_overship := NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0);
13295                                 l_qty_overship := ROUND(((l_transactable_qty * l_sec_qty_overship)/l_sec_transactable_qty),5);
13296                         END IF;
13297 
13298                    ELSE
13299                         IF((NVL(l_lpn_cont_rec.quantity,0) <= NVL(l_transactable_qty,0)) AND
13300                             (NVL(l_lpn_cont_rec.quantity,0) <= NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0))) THEN
13301                                 l_qty_overship := l_lpn_cont_rec.quantity;
13302                                 IF l_dual_uom_item THEN
13303                                     l_sec_qty_overship := l_lpn_cont_rec.secondary_quantity;
13304                                 END IF;
13305                         ELSIF((NVL(l_transactable_qty, 0) <= NVL(l_lpn_cont_rec.quantity,0)) AND
13306                                 (NVL(l_transactable_qty, 0) <= NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0))) THEN
13307                                 l_qty_overship := NVL(l_transactable_qty,0);
13308                                 IF l_dual_uom_item THEN
13309                                     l_sec_qty_overship := l_sec_transactable_qty;
13310                                 END IF;
13311                         ELSE
13312                                 l_qty_overship := NVL((l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty),0);
13313                                 IF l_dual_uom_item THEN
13314                                     l_sec_qty_overship := NVL((l_max_shippable_sec_qty + l_staged_sec_qty - l_total_resvd_sec_qty),0);
13315                                 END IF;
13316                         END IF;
13317                    END IF;
13318 
13319                      IF (g_fulfillment_base = 'P' AND l_qty_overship > 0)
13320                         OR (g_fulfillment_base = 'S' AND NVL(l_sec_qty_overship,0) > 0) --{
13321                      THEN
13322                         -- create reservation
13323                         l_reservation_record.primary_reservation_quantity := l_qty_overship;
13324 
13325                         IF l_dual_uom_item THEN
13326                            l_reservation_record.secondary_reservation_quantity := l_sec_qty_overship;
13327                         END IF;
13328 
13329                         -- No need for UOM conversion here since rsv UOM is hard-coded below to primary
13330                         l_reservation_record.reservation_quantity := l_qty_overship;
13331 
13332                         IF (l_debug = 1) THEN
13333                            DEBUG('Quantity to create reservation= ' || l_reservation_record.primary_reservation_quantity
13334                                 ,'Perform_Overship_Distribution');
13335                         END IF;
13336 
13337                         l_reservation_record.organization_id               := p_org_id;
13338                         l_reservation_record.inventory_item_id             := l_lpn_cont_rec.inventory_item_id;
13339                         l_reservation_record.demand_source_header_id       := l_demand_source_header_id;
13340                         l_reservation_record.demand_source_line_id         := l_loaded_lines_rec.order_line_id;
13341                         l_reservation_record.reservation_uom_id            := NULL;
13342                         l_reservation_record.reservation_uom_code          := l_loaded_lines_rec.primary_uom_code;
13343                         l_reservation_record.primary_uom_code              := l_loaded_lines_rec.primary_uom_code;
13344                         IF l_dual_uom_item THEN
13345                            l_reservation_record.secondary_uom_code := l_loaded_lines_rec.secondary_uom_code;
13346                         ELSE
13347                            l_reservation_record.secondary_uom_code := NULL;
13348                         END IF;
13349                         l_reservation_record.primary_uom_id                := NULL;
13350                         l_reservation_record.secondary_uom_id              := NULL;
13351                         l_reservation_record.supply_source_type_id         := 13;
13352                         l_reservation_record.demand_source_type_id         := l_demand_source_type_id;
13353                         l_reservation_record.ship_ready_flag               := 2;
13354                         l_reservation_record.attribute1                    := NULL;
13355                         l_reservation_record.attribute2                    := NULL;
13356                         l_reservation_record.attribute3                    := NULL;
13357                         l_reservation_record.attribute4                    := NULL;
13358                         l_reservation_record.attribute5                    := NULL;
13359                         l_reservation_record.attribute6                    := NULL;
13360                         l_reservation_record.attribute7                    := NULL;
13361                         l_reservation_record.attribute8                    := NULL;
13362                         l_reservation_record.attribute9                    := NULL;
13363                         l_reservation_record.attribute10                   := NULL;
13364                         l_reservation_record.attribute11                   := NULL;
13365                         l_reservation_record.attribute12                   := NULL;
13366                         l_reservation_record.attribute13                   := NULL;
13367                         l_reservation_record.attribute14                   := NULL;
13368                         l_reservation_record.attribute15                   := NULL;
13369                         l_reservation_record.attribute_category            := NULL;
13370                         l_reservation_record.lpn_id                        := l_lpn_cont_rec.lpn_id;
13371                         l_reservation_record.pick_slip_number              := NULL;
13372                         l_reservation_record.lot_number_id                 := NULL;
13373                         l_reservation_record.lot_number                    := l_lpn_cont_rec.lot_number;
13374                         l_reservation_record.locator_id                    := l_lpn_cont_rec.locator_id;
13375                         l_reservation_record.subinventory_id               := NULL;
13376                         l_reservation_record.subinventory_code             := g_subinventory_code;
13377                         l_reservation_record.revision                      := l_lpn_cont_rec.revision;
13378                         l_reservation_record.supply_source_line_detail     := NULL;
13379                         l_reservation_record.supply_source_name            := NULL;
13380                         l_reservation_record.supply_source_line_id         := l_loaded_lines_rec.order_line_id;
13381                         l_reservation_record.supply_source_header_id       := l_demand_source_header_id;
13382                         l_reservation_record.external_source_line_id       := NULL;
13383                         l_reservation_record.external_source_code          := NULL;
13384                         l_reservation_record.autodetail_group_id           := NULL;
13385                         l_reservation_record.demand_source_delivery        := NULL;
13386                         l_reservation_record.demand_source_name            := NULL;
13387                         l_reservation_record.requirement_date              := l_loaded_lines_rec.request_date;
13388                         --BUG14846296
13389                         l_reservation_record.staged_flag                   := 'Y' ;  -- Bug#5262108
13390                         l_reservation_record.secondary_detailed_quantity   := NULL;  -- Bug 7482123
13391                         l_reservation_record.detailed_quantity             := NULL;  -- Bug 7482123
13392 
13393                         IF (l_debug = 1) THEN
13394                            DEBUG('Before call to create reservation', 'Perform_Overship_Distribution');
13395                         END IF;
13396 
13397                         inv_reservation_pub.create_reservation(
13398                           x_return_status               => l_return_status
13399                         , x_msg_count                   => l_msg_count
13400                         , x_msg_data                    => l_msg_data
13401                         , x_serial_number               => l_dummy_sn
13402                         , x_quantity_reserved           => l_quantity_reserved_tmp
13403                         , x_secondary_quantity_reserved => l_sec_qty_reserved_tmp
13404                         , x_reservation_id              => l_reservation_id
13405                         , p_api_version_number          => 1.0
13406                         , p_init_msg_lst                => fnd_api.g_false
13407                         , p_rsv_rec                     => l_reservation_record
13408                         , p_partial_reservation_flag    => fnd_api.g_true
13409                         , p_force_reservation_flag      => fnd_api.g_true
13410                         , p_serial_number               => l_dummy_sn
13411                         , p_validation_flag             => fnd_api.g_true
13412                         , p_over_reservation_flag       => 3
13413                         );
13414 
13415                         IF l_return_status = fnd_api.g_ret_sts_error THEN
13416                            IF (l_debug = 1) THEN
13417                               DEBUG('Create reservation failed for lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
13418                            END IF;
13419 
13420                            RAISE fnd_api.g_exc_error;
13421                         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13422                            IF (l_debug = 1) THEN
13423                               DEBUG('Unexpected error during create of Reservations lpn_id= ' || l_lpn_cont_rec.lpn_id
13424                             , 'Perform_Overship_Distribution');
13425                            END IF;
13426 
13427                            RAISE fnd_api.g_exc_unexpected_error;
13428                         END IF;
13429 
13430                         IF (l_debug = 1) THEN
13431                            DEBUG('Create reservations is successful ' || l_reservation_id, 'Perform_Overship_Distribution');
13432                            DEBUG('After call to create reservation', 'Perform_Overship_Distribution');
13433                         END IF;
13434 
13435                         -- update cache quantities
13436                         g_lpn_contents_tab(l_index).quantity := g_lpn_contents_tab(l_index).quantity - l_qty_overship;
13437                         l_lpn_cont_rec.quantity := l_lpn_cont_rec.quantity - l_qty_overship;
13438                         g_total_lpn_quantity := g_total_lpn_quantity - l_qty_overship;
13439 
13440                         IF l_dual_uom_item THEN
13441                            g_lpn_contents_tab(l_index).secondary_quantity :=
13442                                g_lpn_contents_tab(l_index).secondary_quantity - l_sec_qty_overship;
13443                            l_lpn_cont_rec.secondary_quantity := l_lpn_cont_rec.secondary_quantity - l_sec_qty_overship;
13444                            g_total_lpn_sec_qty := g_total_lpn_sec_qty - l_sec_qty_overship;
13445                         END IF;
13446 
13447                         -- bug#2830138
13448                         -- make backup of quantities used
13449                         -- will be used if overship has to be rollbacked
13450                         IF l_lpn_cont_qty_used.EXISTS(l_index) THEN
13451                            l_lpn_cont_qty_used(l_index) := l_lpn_cont_qty_used(l_index) + l_qty_overship;
13452                         ELSE
13453                            l_lpn_cont_qty_used(l_index) := l_qty_overship;
13454                         END IF;
13455 
13456                         IF l_dual_uom_item THEN
13457                            IF l_lpn_sec_qty_used.EXISTS(l_index) THEN
13458                               l_lpn_sec_qty_used(l_index) := l_lpn_sec_qty_used(l_index) + l_sec_qty_overship;
13459                            ELSE
13460                               l_lpn_sec_qty_used(l_index) := l_sec_qty_overship;
13461                            END IF;
13462                         END IF;
13463 
13464                         -- if all quantity consumed exit to lpn_content loop
13465                         -- update wds for processed_quantity=processed_quantity+g_total_lpn_quantity
13466                         BEGIN
13467                            UPDATE wms_direct_ship_temp
13468                               SET processed_quantity = (processed_quantity + l_qty_overship)
13469                                 , secondary_processed_qty = (NVL(secondary_processed_qty,0) + NVL(l_sec_qty_overship,0))
13470                             WHERE organization_id = p_org_id
13471                               AND group_id = g_group_id
13472                               AND lpn_id = p_lpn_id
13473                               AND order_line_id = l_order_line_id;
13474                         EXCEPTION
13475                            WHEN OTHERS THEN
13476                               IF (l_debug = 1) THEN
13477                                  DEBUG('Exception updating WDS', 'Perform_Overship_Distribution');
13478                               END IF;
13479 
13480                               RAISE fnd_api.g_exc_unexpected_error;
13481                         END;
13482                      END IF; --} l_qty_overship > 0
13483                   END IF; --} l_transactable_qty>0
13484 
13485                   IF (g_fulfillment_base = 'P' AND l_lpn_cont_rec.quantity = 0)
13486                      OR (g_fulfillment_base = 'S' AND NVL(l_lpn_cont_rec.secondary_quantity,0) = 0)
13487                   THEN
13488                      EXIT;
13489                   END IF;
13490                END IF; --} revision
13491             END LOOP; --} loaded lines
13492 
13493             CLOSE loaded_lines;
13494          END IF; --} quantity > 0
13495 
13496          -- if still more quantity left lpn cannot be loaded
13497 
13498          IF (l_debug = 1) THEN
13499             DEBUG('l_lpn_cont_rec.quantity=' || l_lpn_cont_rec.quantity, 'Perform_Overship_Distribution');
13500             DEBUG('l_lpn_cont_rec.secondary_quantity=' || l_lpn_cont_rec.secondary_quantity, 'Perform_Overship_Distribution');
13501          END IF;
13502 
13503          IF ((g_fulfillment_base = 'P') AND l_lpn_cont_rec.quantity > 0)
13504             OR (g_fulfillment_base = 'S' AND l_lpn_cont_rec.secondary_quantity > 0)
13505          THEN
13506             IF (l_debug = 1) THEN
13507                DEBUG('Lpn cannot be loaded, unused quantity found', 'Perform_Overship_Distribution');
13508             END IF;
13509 
13510             x_return_status  := fnd_api.g_ret_sts_error;
13511 
13512             -- bug#2830138
13513             -- revert back the changes to lpn_contents from backup
13514             IF l_lpn_cont_qty_used.COUNT > 0 THEN
13515                l_temp_count := l_lpn_cont_qty_used.FIRST;
13516 
13517                WHILE l_temp_count IS NOT NULL LOOP
13518                   g_lpn_contents_tab(l_temp_count).quantity := g_lpn_contents_tab(l_temp_count).quantity + l_lpn_cont_qty_used(l_temp_count);
13519                   IF l_lpn_sec_qty_used.EXISTS(l_temp_count) THEN
13520                      g_lpn_contents_tab(l_temp_count).secondary_quantity :=
13521                          g_lpn_contents_tab(l_temp_count).secondary_quantity - l_lpn_sec_qty_used(l_temp_count);
13522                   END IF;
13523                   l_temp_count := l_lpn_cont_qty_used.NEXT(l_temp_count);
13524                END LOOP;
13525 
13526                g_total_lpn_quantity := l_old_total_lpn_qty;
13527                g_total_lpn_sec_qty := l_old_tot_lpn_sec_qty;
13528 
13529                IF (l_debug = 1) THEN
13530                   DEBUG('lpn contents restored from backup', 'Perform_Overship_Distribution');
13531                   DEBUG('g_total_lpn_quantity=' || g_total_lpn_quantity, 'Perform_Overship_Distribution');
13532                   DEBUG('g_total_lpn_sec_qty=' || g_total_lpn_sec_qty, 'Perform_Overship_Distribution');
13533                END IF;
13534             END IF;
13535 
13536             fnd_message.set_name('WMS', 'WMS_LPN_NOT_CONSUMED');
13537             fnd_msg_pub.ADD;
13538             RETURN;
13539          END IF;
13540       END IF; --} quantity >0
13541 
13542       IF (l_debug = 1) THEN
13543          DEBUG('l_index= ' || l_index, 'Perform_Overship_Distribution');
13544       END IF;
13545   END LOOP; --} g_lpn_contents_tab
13546 
13547   x_return_status  := fnd_api.g_ret_sts_success;
13548 
13549 EXCEPTION
13550   WHEN OTHERS THEN
13551     IF (l_debug = 1) THEN
13552       DEBUG('Exception occured', 'Perform_Overship_Distribution');
13553     END IF;
13554 
13555     x_return_status  := fnd_api.g_ret_sts_unexp_error;
13556     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
13557     fnd_msg_pub.ADD;
13558 END perform_overship_distribution;
13559 
13560 /*
13561    This procedure checks any type of hold applied on the order line.
13562  */
13563 
13564 PROCEDURE check_holds(
13565   p_order_header_id IN            NUMBER
13566 , p_order_line_id   IN            NUMBER
13567 , x_return_status   OUT NOCOPY    VARCHAR2
13568 , x_msg_count       OUT NOCOPY    NUMBER
13569 , x_msg_data        OUT NOCOPY    VARCHAR2
13570 ) IS
13571   l_delivery_detail_id NUMBER;
13572   l_return_status      VARCHAR2(10);
13573   l_msg_count          NUMBER;
13574   l_msg_data           VARCHAR2(20000);
13575   l_debug              NUMBER          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13576 BEGIN
13577   IF (l_debug = 1) THEN
13578     DEBUG('In the check holds procedure', 'Check_holds');
13579     DEBUG('header id:' || p_order_header_id, 'Check_holds');
13580     DEBUG('line id:' || p_order_line_id, 'Check_Holds');
13581   END IF;
13582 
13583   x_return_status  := fnd_api.g_ret_sts_success;
13584 
13585   SELECT delivery_detail_id
13586     INTO l_delivery_detail_id
13587     FROM wsh_delivery_details_ob_grp_v
13588    WHERE source_header_id = p_order_header_id
13589      AND source_line_id = p_order_line_id
13590      AND ROWNUM = 1;
13591 
13592   IF (l_debug = 1) THEN
13593     DEBUG('delivery detial id:' || l_delivery_detail_id, 'Check_Holds');
13594   END IF;
13595 
13596   wsh_details_validations.check_credit_holds(p_detail_id => l_delivery_detail_id, p_activity_type => 'PICK', p_source_code => 'OE'
13597   , x_return_status              => l_return_status);
13598 
13599   IF l_return_status = fnd_api.g_ret_sts_error THEN
13600     IF (l_debug = 1) THEN
13601       DEBUG('Credit Check holds API failed with status E ', 'Check_holds');
13602     END IF;
13603 
13604     RAISE fnd_api.g_exc_error;
13605   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13606     IF (l_debug = 1) THEN
13607       DEBUG('Check Credit Holds API failed with status U', 'Check_holds');
13608     END IF;
13609 
13610     RAISE fnd_api.g_exc_unexpected_error;
13611   END IF;
13612 EXCEPTION
13613   WHEN fnd_api.g_exc_error THEN
13614     x_return_status  := fnd_api.g_ret_sts_error;
13615 
13616     IF (l_debug = 1) THEN
13617       DEBUG('fnd_api.g_exe_error ' || SQLERRM, 'Check_holds');
13618     END IF;
13619 
13620     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
13621   WHEN fnd_api.g_exc_unexpected_error THEN
13622     x_return_status  := fnd_api.g_ret_sts_unexp_error;
13623 
13624     IF (l_debug = 1) THEN
13625       DEBUG('FND_API.G_UNEXPECTED_ERR' || SQLERRM, 'check_holds');
13626     END IF;
13627 
13628     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
13629   WHEN OTHERS THEN
13630     x_return_status  := fnd_api.g_ret_sts_unexp_error;
13631 
13632     IF (l_debug = 1) THEN
13633       DEBUG('FND_API.G_UNEXPECTED_ERR' || SQLERRM, 'check_holds');
13634     END IF;
13635 
13636     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
13637 END check_holds;
13638 
13639 /* Bug 2994099:This procedure cleanup all the temp data for a backordered delivery for this lpn */
13640 
13641 PROCEDURE cleanup_orphan_rec(p_org_id IN NUMBER) IS
13642   PRAGMA AUTONOMOUS_TRANSACTION;
13643 
13644   CURSOR c_wstt_closed_del IS
13645   SELECT DISTINCT wstt.outermost_lpn_id, wstt.delivery_id, wstt.trip_id
13646     FROM wms_shipping_transaction_temp  wstt
13647        , wsh_new_deliveries_ob_grp_v    wnd
13648    WHERE wstt.organization_id = p_org_id
13649      AND wnd.delivery_id = wstt.delivery_id
13650      AND wnd.status_code IN ('CL','IT');
13651 
13652   TYPE t_num_tab  IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
13653   l_lpn_tab       t_num_tab;
13654   l_del_tab       t_num_tab;
13655   l_trip_tab      t_num_tab;
13656 
13657   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13658 BEGIN
13659   OPEN c_wstt_closed_del;
13660   FETCH c_wstt_closed_del BULK COLLECT
13661    INTO l_lpn_tab, l_del_tab, l_trip_tab;
13662 
13663   CLOSE c_wstt_closed_del;
13664 
13665   IF l_lpn_tab.COUNT > 0 THEN
13666      IF (l_debug = 1) THEN
13667         DEBUG('Orphaned record count: ' || l_lpn_tab.COUNT, 'Cleanup_Orphan_Rec');
13668      END IF;
13669 
13670      FORALL i IN l_lpn_tab.FIRST .. l_lpn_tab.LAST
13671         DELETE wms_direct_ship_temp
13672          WHERE lpn_id = l_lpn_tab(i);
13673 
13674      FORALL i IN l_lpn_tab.FIRST .. l_lpn_tab.LAST
13675         DELETE wms_freight_cost_temp
13676          WHERE lpn_id = l_lpn_tab(i);
13677 
13678      FORALL i IN l_del_tab.FIRST .. l_del_tab.LAST
13679         DELETE wms_freight_cost_temp
13680          WHERE delivery_id = l_del_tab(i);
13681 
13682      FORALL i IN l_trip_tab.FIRST .. l_trip_tab.LAST
13683         DELETE wms_freight_cost_temp
13684          WHERE trip_id IN l_trip_tab(i);
13685 
13686      FORALL i IN l_lpn_tab.FIRST .. l_lpn_tab.LAST
13687         DELETE wms_shipping_transaction_temp
13688          WHERE outermost_lpn_id = l_lpn_tab(i);
13689 
13690   ELSE
13691      IF (l_debug = 1) THEN
13692         DEBUG('No data cleanup required', 'Cleanup_Orphan_Rec');
13693      END IF;
13694   END IF;
13695 
13696   COMMIT;
13697 EXCEPTION
13698   WHEN OTHERS THEN
13699     IF (l_debug = 1) THEN
13700        DEBUG('Exception occured', 'Cleanup_Orphan_Rec');
13701     END IF;
13702 
13703     IF c_wstt_closed_del%ISOPEN THEN
13704        CLOSE c_wstt_closed_del;
13705     END IF;
13706 
13707     RAISE fnd_api.g_exc_unexpected_error;
13708 END cleanup_orphan_rec;
13709 
13710 /*
13711  This function finds out if there is any record in lpn contents having
13712  available quantity >0 and end_item_unit_number=p_end_unit_number
13713  */
13714 
13715 FUNCTION validate_end_unit_num(p_item_id IN NUMBER, p_end_unit_number IN VARCHAR2)
13716   RETURN BOOLEAN IS
13717   l_start_index NUMBER;
13718   l_end_index   NUMBER;
13719   l_result      BOOLEAN := FALSE;
13720   l_debug       NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13721 BEGIN
13722   IF (l_debug = 1) THEN
13723     DEBUG('p_item_id=' || p_item_id, 'Validate_End_Unit_Num');
13724     DEBUG('p_end_unit_number=' || p_end_unit_number, 'Validate_End_Unit_Num');
13725   END IF;
13726 
13727   l_start_index  := g_lpn_contents_lookup_tab(p_item_id).start_index;
13728   l_end_index    := g_lpn_contents_lookup_tab(p_item_id).end_index;
13729 
13730   IF g_lpn_contents_tab(l_start_index).serial_control_code NOT IN(2, 5) THEN
13731     IF (l_debug = 1) THEN
13732       DEBUG('SUCCESS', 'Validate_End_Unit_Num');
13733     END IF;
13734 
13735     RETURN TRUE;
13736   ELSE
13737     IF g_cross_unit_allowed = 'Y' THEN
13738       IF (l_debug = 1) THEN
13739         DEBUG('SUCCESS', 'Validate_End_Unit_Num');
13740       END IF;
13741 
13742       RETURN TRUE;
13743     ELSE
13744       FOR l_rec IN l_start_index .. l_end_index LOOP
13745         IF (l_debug = 1) THEN
13746           DEBUG('EIUN lpn=' || g_lpn_contents_tab(l_rec).end_item_unit_number, 'Validate_End_Unit_Num');
13747         END IF;
13748 
13749         IF g_lpn_contents_tab(l_rec).quantity > 0 THEN
13750           IF NVL(g_lpn_contents_tab(l_rec).end_item_unit_number, '@@') = NVL(p_end_unit_number, '@@') THEN
13751             IF (l_debug = 1) THEN
13752               DEBUG('SUCCESS', 'Validate_End_Unit_Num');
13753             END IF;
13754 
13755             l_result  := TRUE;
13756           END IF;
13757         END IF;
13758       END LOOP;
13759     END IF;
13760   END IF;
13761 
13762   RETURN l_result;
13763 END validate_end_unit_num;
13764 
13765 /* This function finds out that the item in g_lpn_contents_tab at index is having available quantity
13766    and its end_item_unit_number matches p_end_unit_number.
13767 */
13768 
13769 FUNCTION validate_end_unit_num_at(p_index IN NUMBER, p_end_unit_number IN VARCHAR2)
13770   RETURN BOOLEAN IS
13771   l_result BOOLEAN := FALSE;
13772   l_debug  NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13773 BEGIN
13774   IF (l_debug = 1) THEN
13775     DEBUG('p_index=' || p_index, 'Validate_End_Unit_Num_At');
13776     DEBUG('p_end_unit_number=' || p_end_unit_number, 'Validate_End_Unit_Num_At');
13777   END IF;
13778 
13779   IF g_lpn_contents_tab(p_index).serial_control_code NOT IN(2, 5) THEN
13780     IF (l_debug = 1) THEN
13781       DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
13782     END IF;
13783 
13784     RETURN TRUE;
13785   ELSE
13786     IF g_cross_unit_allowed = 'Y' THEN
13787       IF (l_debug = 1) THEN
13788         DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
13789       END IF;
13790 
13791       RETURN TRUE;
13792     ELSE
13793       IF g_lpn_contents_tab(p_index).quantity > 0 THEN
13794         IF NVL(g_lpn_contents_tab(p_index).end_item_unit_number, '@@') = NVL(p_end_unit_number, '@@') THEN
13795           IF (l_debug = 1) THEN
13796             DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
13797           END IF;
13798 
13799           l_result  := TRUE;
13800         END IF;
13801       END IF;
13802     END IF;
13803   END IF;
13804 
13805   RETURN l_result;
13806 END validate_end_unit_num_at;
13807 
13808 /* End of Patchset I procedures */
13809 
13810 PROCEDURE is_ct_wt_enabled(p_lpn_id IN NUMBER,
13811                            p_org_id IN NUMBER,
13812                            x_ct_wt_enabled  OUT NOCOPY NUMBER,
13813                            x_return_status  OUT NOCOPY VARCHAR2,
13814                            x_msg_count      OUT NOCOPY NUMBER,
13815                            x_msg_data       OUT NOCOPY VARCHAR2) IS
13816 
13817 l_debug  NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
13818 l_ct_wt_enabled NUMBER := 0;
13819 BEGIN
13820     IF (l_debug = 1) THEN
13821        DEBUG('Value of lpn_id is = ' || p_lpn_id, 'is_ct_wt_enabled');
13822        DEBUG('Value of org_id is = ' || p_org_id, 'is_ct_wt_enabled');
13823     END IF;
13824 
13825       SELECT 1
13826       INTO   l_ct_wt_enabled
13827       FROM   wms_license_plate_numbers wlpn
13828       WHERE  wlpn.organization_id = p_org_id
13829       AND    wlpn.lpn_id = p_lpn_id
13830       AND    EXISTS ( SELECT 'x'
13831                         FROM wms_license_plate_numbers  wlpn2
13832                            , wms_lpn_contents           wlc
13833                            , mtl_system_items           msi
13834                        WHERE wlpn2.outermost_lpn_id = wlpn.outermost_lpn_id
13835                          AND wlc.parent_lpn_id      = wlpn2.lpn_id
13836                          AND msi.inventory_item_id  = wlc.inventory_item_id
13837                          AND msi.organization_id    = wlc.organization_id
13838                          AND (  (msi.tracking_quantity_ind = 'PS' AND msi.secondary_default_ind <> 'F')
13839                              OR (msi.tracking_quantity_ind = 'P'  AND msi.ont_pricing_qty_source = 'S')
13840                              ));
13841     IF(l_ct_wt_enabled = 1)THEN
13842         x_ct_wt_enabled := 1;
13843     ELSE
13844         x_ct_wt_enabled := 0;
13845     END IF;
13846     IF (l_debug = 1) THEN
13847        DEBUG('Value of x_ct_wt_enabled is = ' || x_ct_wt_enabled, 'is_ct_wt_enabled');
13848     END IF;
13849     x_return_status := FND_API.G_RET_STS_SUCCESS;
13850 EXCEPTION
13851     WHEN NO_DATA_FOUND THEN
13852         IF (l_debug = 1) THEN
13853           DEBUG('No data found for LPN i.e no dual uom or pricing sec item in LPN', 'is_ct_wt_enabled');
13854         END IF;
13855         x_ct_wt_enabled := 0;
13856     WHEN OTHERS THEN
13857         IF (l_debug = 1) THEN
13858         DEBUG('Exception occured', 'is_ct_wt_enabled');
13859         END IF;
13860         x_ct_wt_enabled := 0;
13861 END is_ct_wt_enabled;
13862 
13863 FUNCTION GET_CATCH_WEIGHT
13864                         (P_ORG_ID IN NUMBER
13865                         ,P_LPN_ID IN NUMBER
13866                         ,P_INVENTORY_ITEM_ID IN NUMBER
13867                         ,P_REVISION IN VARCHAR2
13868                         ,P_LOT_NUMBER IN VARCHAR2
13869                         ,P_PICKED_QUANTITY_IN_PRI_UOM IN NUMBER
13870                         ) RETURN NUMBER IS
13871 CURSOR C        (C_ORG_ID IN NUMBER
13872                 ,C_LPN_ID IN NUMBER
13873                 ,C_INVENTORY_ITEM_ID IN NUMBER
13874                 ,C_REVISION IN VARCHAR2
13875                 ,C_LOT_NUMBER IN VARCHAR2)
13876         IS
13877         SELECT
13878                 t.ORG_ID
13879                 ,t.LPN_ID
13880                 ,t.INNER_LPN_ID
13881                 ,t.INVENTORY_ITEM_ID
13882                 ,t.REVISION
13883                 ,t.LOT_NUMBER
13884                 ,DECODE(t.PICKED_UOM_CODE, msi.primary_uom_code, t.PICKED_QUANTITY
13885                 ,GREATEST(inv_convert.inv_um_convert(NULL, NULL,
13886                   t.PICKED_QUANTITY, t.PICKED_UOM_CODE, msi.primary_uom_code,
13887                   NULL, NULL), 0)) PICKED_QUANTITY_IN_PRI_UOM
13888                 ,t.SECONDARY_UOM_CODE
13889                 ,t.SECONDARY_QUANTITY
13890         FROM mtl_system_items msi, WMS_DS_CT_WT_GTEMP t
13891         WHERE t.INVENTORY_ITEM_ID = C_INVENTORY_ITEM_ID /* THIS HAS INDEX */
13892         AND t.ORG_ID = C_ORG_ID
13893         AND msi.INVENTORY_ITEM_ID = t.INVENTORY_ITEM_ID
13894         AND msi.ORGANIZATION_ID = t.ORG_ID
13895         AND NVL(t.INNER_LPN_ID, t.LPN_ID) = C_LPN_ID
13896         AND NVL(t.REVISION,'#NULL#') = NVL(C_REVISION,'#NULL#')
13897         AND NVL(t.LOT_NUMBER,'#NULL#') = NVL(C_LOT_NUMBER,'#NULL#')
13898         AND t.SECONDARY_QUANTITY IS NOT NULL;
13899   CTWT_REC C%rowtype;
13900   RET_VAL NUMBER := NULL;
13901 BEGIN
13902   IF (G_debug=1) THEN
13903     debug('P_ORG_ID = ' || P_ORG_ID, 'GET_CATCH_WEIGHT');
13904     debug('P_LPN_ID = ' || P_LPN_ID, 'GET_CATCH_WEIGHT');
13905     debug('P_INVENTORY_ITEM_ID = ' || P_INVENTORY_ITEM_ID, 'GET_CATCH_WEIGHT');
13906     debug('P_REVISION = ' || P_REVISION, 'GET_CATCH_WEIGHT');
13907     debug('P_LOT_NUMBER = ' || P_LOT_NUMBER, 'GET_CATCH_WEIGHT');
13908     debug('P_PICKED_QUANTITY_IN_PRI_UOM = ' || P_PICKED_QUANTITY_IN_PRI_UOM, 'GET_CATCH_WEIGHT');
13909   END IF;
13910   OPEN C (P_ORG_ID, P_LPN_ID, P_INVENTORY_ITEM_ID, P_REVISION, P_LOT_NUMBER);
13911   FETCH C INTO CTWT_REC;
13912   IF C%NOTFOUND THEN
13913     ret_val := null;
13914   ELSE
13915     IF P_PICKED_QUANTITY_IN_PRI_UOM = CTWT_REC.PICKED_QUANTITY_IN_PRI_UOM  THEN
13916       -- LPN Qty and Delivery Detail Line Qty matches
13917       RET_VAL := CTWT_REC.SECONDARY_QUANTITY;
13918     ELSE
13919       -- LPN Qty and Delivery Detail Line Qty doesn't match.
13920       -- Hence calculate/derive the
13921       -- Delivery Line's sec_qty as follows
13922       RET_VAL  := ((P_PICKED_QUANTITY_IN_PRI_UOM / CTWT_REC.PICKED_QUANTITY_IN_PRI_UOM) * CTWT_REC.SECONDARY_QUANTITY);
13923     END IF;
13924   END IF;
13925 
13926   IF ( RET_VAL < 0 ) THEN
13927     RET_VAL := NULL;
13928   END IF;
13929   IF (G_debug=1) THEN
13930     debug('Calculated SECONDARY_QUANTITY = ' || RET_VAL, 'GET_CATCH_WEIGHT');
13931   END IF;
13932   RETURN RET_VAL;
13933 END GET_CATCH_WEIGHT;
13934 
13935 END WMS_DIRECT_SHIP_PVT;