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.21.12010000.2 2008/08/25 06:50:16 anviswan 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                                 );
38 
39 -- This table containes records of type lpn_content_rec. The table is
40 -- constructed from the output of cursor lpn_content_cur
41 TYPE lpn_contents_tab IS TABLE OF lpn_content_rec INDEX BY BINARY_INTEGER;
42 
43 -- This record form the lpn_contents_lookup_tab.
44 -- Here start_index and end_index specify record locations in lpn_contents_tab.
45 TYPE lpn_contents_lookup_rec IS RECORD (start_index NUMBER, end_index NUMBER);
46 
47 -- This table stores the lookup index values corresponding to a item in
48 -- lpn_contents_tab
49 TYPE lpn_contents_lookup_tab IS TABLE OF lpn_contents_lookup_rec INDEX BY BINARY_INTEGER;
50 
51 -- This table will stores all the checked deliveries for the current LPN
52 TYPE checked_delivery_tab IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
53 
54 -- for delivery grouping validations
55 TYPE del_grp_rls_flags IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
56 TYPE del_grp_rls_fld IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;
57 -- table of checked deliveries
58 TYPE checked_deliveries IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
59 
60 TYPE delivery_detail_rec IS RECORD(
61     organization_id            NUMBER
62   , dock_door_id               NUMBER
63   , lpn_id                     NUMBER
64   , order_header_id            NUMBER
65   , order_line_id              NUMBER
66   , line_item_id               NUMBER
67   , transaction_temp_id        NUMBER
68   , delivery_detail_id         NUMBER
69   , requested_quantity         NUMBER
70   , primary_uom_code           VARCHAR2(3)
71   , lot_control_code           NUMBER
72   , serial_number_control_code NUMBER
73   , inventory_item_id          NUMBER
74   , ont_pricing_qty_source     VARCHAR2(30)
75   ); -- Added bug4128854
76 
77 TYPE delivery_detail_tab IS TABLE OF delivery_detail_rec
78     INDEX BY BINARY_INTEGER; --Added bug 4128854
79 /* global variables added for Patchset I */
80 
81 g_lpn_contents_tab lpn_contents_tab;
82 g_lpn_contents_lookup_tab lpn_contents_lookup_tab;
83 g_total_lpn_quantity NUMBER;
84 g_checked_delivery_tab checked_delivery_tab;
85 g_project_id NUMBER;
86 g_task_id NUMBER;
87 g_cross_project_allowed VARCHAR2(1);
88 g_cross_unit_allowed VARCHAR2(1);
89 g_subinventory_code VARCHAR2(30);
90 g_locator_id NUMBER;
91 g_group_id NUMBER;
92 -- who columns
93 g_last_update_date DATE;
94 g_last_updated_by NUMBER;
95 g_last_update_login NUMBER;
96 g_created_by NUMBER;
97 g_creation_date DATE;
98 -- used for validating delivery grouping rules
99 g_del_grp_rls_flags del_grp_rls_flags;
100 g_del_grp_rls_fld_value del_grp_rls_fld;
101 g_del_grp_rls_fld_temp del_grp_rls_fld;
102 -- checked deliveries for lpn
103 g_checked_deliveries checked_deliveries;
104 
105 G_RET_STS_SUCCESS      VARCHAR2(1) := FND_API.g_ret_sts_success;
106 G_RET_STS_ERROR        VARCHAR2(1) := FND_API.g_ret_sts_error;
107 G_RET_STS_UNEXP_ERROR  VARCHAR2(1) := FND_API.G_RET_STS_UNEXP_ERROR;
108 G_FALSE                VARCHAR2(1) := FND_API.G_FALSE;
109 G_TRUE                 VARCHAR2(1) := FND_API.G_TRUE;
110 G_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
111 
112 PROCEDURE DEBUG(p_message       IN VARCHAR2,
113                 p_module   IN VARCHAR2 ,
114                 p_level         IN VARCHAR2 DEFAULT 9) IS
115     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
116 BEGIN
117 
118 IF (l_debug = 1) THEN
119    INV_TRX_UTIL_PUB.TRACE( P_MESG =>P_MESSAGE
120                        ,P_MOD => p_module
121                        ,p_level => p_level
122                        );
123 END IF;
124  --dbms_output.put_line(p_message);
125 END; -- DEBUG
126 
127 PROCEDURE process_mobile_msg IS
128    l_msg_data VARCHAR2(2000);
129    l_msg_count NUMBER;
130    l_dummy_number NUMBER;
131 
132    l_app_short_name VARCHAR2(20);
133    l_msg_name       VARCHAR2(50);
134 BEGIN
135    debug('Entered procedure','process_mobile_msg');
136 
137    l_msg_count := fnd_msg_pub.count_msg;
138    debug('Msg Count: ' || l_msg_count,'process_mobile_msg');
139 
140    FOR i IN 1..l_msg_count LOOP
141       fnd_msg_pub.get(p_msg_index => i,
142               p_data => l_msg_data,
143               p_msg_index_out => l_dummy_number);
144 
145       debug('i: ' || i || ' index_out: ' || l_dummy_number || ' encoded_data: ' || l_msg_data,'process_mobile_msg');
146 
147       fnd_message.parse_encoded(ENCODED_MESSAGE => l_msg_data,
148                 APP_SHORT_NAME  => l_app_short_name,
149                 MESSAGE_NAME    => l_msg_name);
150 
151       debug('App_short_name: ' || l_app_short_name || ' Msg_name: ' || l_msg_name,'process_mobile_msg');
152 
153       IF ((l_msg_name <> 'WSH_DET_INV_INT_SUBMITTED')) THEN
154      fnd_msg_pub.delete_msg(p_msg_index=>i);
155      debug('Deleted message at position: ' || i,'process_mobile_msg');
156       END IF;
157    END LOOP;
158 EXCEPTION
159    WHEN OTHERS THEN
160       debug('Exception raised!','process_mobile_msg');
161       NULL;
162 END process_mobile_msg;
163 
164 PROCEDURE GET_TRIPSTOP_INFO(x_tripstop_info OUT NOCOPY t_genref
165                               ,p_trip_id IN NUMBER
166                                 ,p_org_id IN NUMBER)  IS
167     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
168 BEGIN
169 /*Bug 2900813: Added code to fetch even Ship Method code and Enforce Ship Method Flag*/
170 OPEN x_tripstop_info FOR
171   SELECT         wt.trip_id
172                 ,wt.name
173                 ,wt.vehicle_item_id
174                 ,msi.concatenated_segments
175                 ,wt.vehicle_num_prefix
176                 ,wt.vehicle_number
177                 ,wts.departure_seal_code
178                 ,WMS_DIRECT_SHIP_PVT.GET_ENFORCE_SHIP
179                 ,WMS_DIRECT_SHIP_PVT.GET_SHIPMETHOD_MEANING(wt.ship_method_code)
180                 ,wt.ship_method_code
181     FROM  wsh_trips_ob_grp_v wt
182         ,wsh_trip_stops_ob_grp_v wts
183         ,mtl_system_items_kfv msi
184   WHERE wt.trip_id = p_trip_id
185   AND   wt.trip_id = wts.trip_id
186   AND   (wt.vehicle_item_id = msi.inventory_item_id(+)
187           AND      msi.organization_id(+) = p_org_id)
188   AND   ROWNUM < 2;
189 END GET_TRIPSTOP_INFO;
190 
191 
192 PROCEDURE GET_DELIVERY_INFO(x_delivery_info OUT NOCOPY t_genref,
193                            p_delivery_id IN NUMBER,
194          p_org_id IN NUMBER)  IS
195 
196     l_debug   NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
197     l_trip_id NUMBER;
198     l_trip_name wsh_trips.name%TYPE;
199 
200 BEGIN
201 open x_delivery_info for
202     SELECT wnd.delivery_id
203           ,wnd.name
204           ,WMS_DIRECT_SHIP_PVT.GET_DELIVERY_LPN(wnd.delivery_id)
205           ,nvl(wnd.net_weight, 0)
206           ,nvl(wnd.gross_weight, 0)
207           ,wnd.weight_uom_code
208           ,wnd.waybill
209           ,WMS_DIRECT_SHIP_PVT.GET_SHIPMETHOD_MEANING(wnd.ship_method_code)
210           ,wnd.ship_method_code
211           ,WMS_DIRECT_SHIP_PVT.GET_FOBLOC_CODE_MEANING(wnd.fob_code)
212           ,wnd.fob_location_id
213           ,WMS_DIRECT_SHIP_PVT.GET_FOB_LOCATION(wnd.fob_location_id)
214           ,wnd.freight_terms_code
215           ,WMS_DIRECT_SHIP_PVT.GET_FREIGHT_TERM(wnd.freight_terms_code)
216           ,WMS_DIRECT_SHIP_PVT.GET_FOB_LOCATION(wnd.INTMED_SHIP_TO_LOCATION_ID)
217           ,WMS_DIRECT_SHIP_PVT.GET_BOL(wnd.delivery_id)
218           ,nvl(wnd.status_code,'OP')
219           ,WMS_DIRECT_SHIP_PVT.GET_ENFORCE_SHIP
220           ,wts1.trip_id --2767767
221 FROM wsh_new_deliveries_ob_grp_v wnd,
222       -- 2767767
223 (SELECT wdl.delivery_id,wts.trip_id
224         FROM wsh_delivery_legs_ob_grp_v wdl,
225              wsh_trip_stops_ob_grp_v wts
226         WHERE wdl.delivery_id=p_delivery_id
227         AND   wdl.pick_up_stop_id=wts.stop_id
228         AND ROWNUM=1) wts1 -- end 2767767
229 WHERE wnd.delivery_id = p_delivery_id
230 and   wnd.delivery_id=wts1.delivery_id(+); --2767767
231 
232 /* bug # 2994098 */
233 -- If a delivery is ship confirmed from STF without closing the trip
234 -- then trip id should be updated in wstt because user may try to close the trip from mobile.
235 BEGIN
236     SELECT wts.trip_id,wt.name
237     INTO l_trip_id,l_trip_name
238         FROM wsh_delivery_legs_ob_grp_v wdl,
239              wsh_trip_stops_ob_grp_v wts,
240          wsh_trips_ob_grp_v wt
241         WHERE wdl.delivery_id=p_delivery_id
242         AND wdl.pick_up_stop_id=wts.stop_id
243          AND wt.trip_id=wts.trip_id
244         AND ROWNUM=1;
245     IF l_trip_id IS NOT NULL THEN
246         UPDATE wms_shipping_transaction_temp
247         SET trip_id=l_trip_id,trip_name=l_trip_name
248         WHERE delivery_id=p_delivery_id and trip_id is null;
249     END IF;
250 
251  EXCEPTION
252     WHEN OTHERS THEN
253     debug('trip_id update on wstt failed','get_delivery_info');
254         NULL;
255  END;
256 END GET_DELIVERY_INFO;
257 
258 -- This Function concatenates all LPN NAME that are part of Delivery
259 FUNCTION GET_DELIVERY_LPN(p_delivery_id IN NUMBER ) RETURN VARCHAR2 IS
260   CURSOR delivery_lpn IS
261   SELECT distinct license_plate_number
262   FROM  wms_shipping_transaction_temp wstt
263        ,wms_license_plate_numbers wlpn
264   WHERE wstt.delivery_id = p_delivery_id
265   AND   wstt.outermost_lpn_id = wlpn.lpn_id
266   AND   wstt.direct_ship_flag = 'Y';
267 l_lpn_string  VARCHAR2(30);
268 l_lpn_concat  VARCHAR2(2000);
269     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
270 BEGIN
271   OPEN delivery_lpn;
272   LOOP
273     FETCH delivery_lpn into l_lpn_string;
274         EXIT WHEN delivery_lpn%NOTFOUND;
275    IF delivery_lpn%ROWCOUNT = 1 THEN
276      l_lpn_concat := l_lpn_string;
277    ELSE
278         l_lpn_concat := l_lpn_concat || '|' || l_lpn_string;
279    END IF;
280   END LOOP;
281   CLOSE delivery_lpn;
282   RETURN l_lpn_concat;
283 END;  -- GET_DELIVERY_LPN
284 
285 FUNCTION GET_SHIPMETHOD_MEANING(p_ship_method_code  IN  VARCHAR2)
286 RETURN  VARCHAR2  IS
287 l_ship_method_meaning VARCHAR2(80);
288     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
289 BEGIN
290   IF p_ship_method_code IS NULL THEN
291     RETURN '';
292   ELSE
293      SELECT meaning
294      INTO l_ship_method_meaning
295      FROM fnd_lookup_values_vl
296      WHERE lookup_type = 'SHIP_METHOD'
297      AND   view_application_id = 3
298      AND   lookup_code = p_ship_method_code;
299   END IF;
300      RETURN l_ship_method_meaning;
301   EXCEPTION
302      WHEN OTHERS THEN
303         RETURN '';
304 END GET_SHIPMETHOD_MEANING;
305 
306 FUNCTION GET_FOBLOC_CODE_MEANING(p_fob_code  IN  VARCHAR2)
307 RETURN VARCHAR2 IS
308  l_fob_loc_meaning  VARCHAR2(80);
309     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
310 BEGIN
311   IF p_fob_code IS NULL THEN
312      RETURN '';
313   ELSE
314      SELECT meaning
315          INTO l_fob_loc_meaning
316      FROM  ar_lookups
317      WHERE lookup_type = 'FOB'
318      AND SYSDATE BETWEEN nvl(start_date_active,sysdate) AND nvl(end_date_active,sysdate)
319      AND  enabled_flag = 'Y'
320      AND  lookup_code = p_fob_code;
321   END IF;
322     RETURN l_fob_loc_meaning;
323 EXCEPTION
324      WHEN OTHERS THEN
325         RETURN '';
326 END GET_FOBLOC_CODE_MEANING;
327 
328 FUNCTION GET_FOB_LOCATION(p_fob_location_id IN NUMBER)
329 RETURN VARCHAR2  IS
330  l_fob_location VARCHAR2(80);
331     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
332 BEGIN
333   IF p_fob_location_id IS NULL THEN
334     RETURN '';
335   ELSE
336     SELECT description
337         INTO l_fob_location
338         FROM wsh_hr_locations_v
339    WHERE location_id = p_fob_location_id;
340   END IF;
341     RETURN l_fob_location;
342 EXCEPTION
343     WHEN OTHERS THEN
344          RETURN '';
345 END GET_FOB_LOCATION;
346 
347 FUNCTION GET_FREIGHT_TERM(p_freight_term_code VARCHAR2)
348 RETURN VARCHAR2 IS
349   l_freight_term  VARCHAR2(80);
350     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
351 BEGIN
352   IF p_freight_term_code IS NULL THEN
353      RETURN '';
354   ELSE
355     SELECT freight_terms
356     INTO l_freight_term
357     FROM oe_frght_terms_active_v
358     WHERE freight_terms_code = p_freight_term_code;
359   END IF;
360     RETURN      l_freight_term;
361 EXCEPTION
362     WHEN OTHERS THEN
363          RETURN '';
364 END GET_FREIGHT_TERM;
365 
366 FUNCTION GET_BOL(p_delivery_id  NUMBER)
367 RETURN NUMBER IS
368   l_BOL         NUMBER;
369     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
370 BEGIN
371   IF p_delivery_id IS NULL THEN
372      RETURN '';
373   ELSE
374     SELECT wdi.SEQUENCE_NUMBER
375     INTO l_BOL
376     FROM wsh_document_instances wdi
377             ,wsh_delivery_legs_ob_grp_v      wdl
378         WHERE wdl.delivery_id = p_delivery_id
379         AND   wdi.entity_id   = wdl.delivery_leg_id
380         AND   wdi.entity_name = 'WSH_DELIVERY_LEGS'
381         AND   rownum < 2;
382         -- Right now datamodel sugget that  WDI - WDL >- WND. So for 1 delivery multiple delivery leg
383         -- multiple BOL. Desktop Application does not support this still I would like to show one BOL
384   END IF;
385     RETURN      l_BOL;
386 EXCEPTION
387     WHEN OTHERS THEN
388          RETURN '';
389 END GET_BOL;
390 
391 FUNCTION get_enforce_ship RETURN VARCHAR2 IS
392    l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
393    x_enforce_ship VARCHAR2(1);
394    x_return_status VARCHAR2(1);
395    x_param_info WSH_SHIPPING_PARAMS_GRP.Global_Params_Rec;
396 BEGIN
397    IF l_debug = 1 THEN
398       debug('Calling Shipping API get_global_parameters','get_enforce_ship');
399    END IF;
400 
401    WSH_SHIPPING_PARAMS_GRP.get_global_parameters
402      (x_global_param_info=>x_param_info,
403       x_return_status => x_return_status);
404 
405    x_enforce_ship := x_param_info.ENFORCE_SHIP_METHOD;
406 
407    IF (l_debug = 1) THEN
408       debug('Shipping API returned status: ' || x_return_status,'get_enforce_ship');
409       debug('Enforce ship Y/N : ' || x_enforce_ship, 'get_enforce_ship');
410    END IF;
411 
412    IF x_enforce_ship IS NULL THEN
413       x_enforce_ship := 'N';
414    END IF;
415 
416    RETURN x_enforce_ship;
417 EXCEPTION
418    WHEN OTHERS THEN
419       IF (l_debug = 1) THEN
420      debug('Others exception raised','get_enforce_ship');
421       END IF;
422       RETURN 'N';
423 END get_enforce_ship;
424 
425 PROCEDURE CHECK_DELIVERY(x_return_status        OUT NOCOPY VARCHAR2
426                         ,x_msg_count            OUT NOCOPY NUMBER
427                         ,x_msg_data             OUT NOCOPY VARCHAR2
428                         ,x_error_code           OUT NOCOPY NUMBER
429                         ,p_delivery_id          IN  NUMBER
430                         ,p_org_id               IN  NUMBER
431                         ,p_dock_door_id         IN  NUMBER
432                  )
433 IS
434 temp_val         NUMBER;
435 l_ship_set       VARCHAR2(2000) := NULL;
436 l_error_msg      VARCHAR2(20000) := NULL;
437 l_return_status  VARCHAR2(1);
438 l_msg_data       VARCHAR2(3000);
439 l_msg_count      NUMBER;
440 x_error_msg      VARCHAR2(20000);
441 x_delivery_name  VARCHAR2(30);
442 x_outermost_lpn  VARCHAR2(10);
443 l_loaded_dock_door  VARCHAR2(2000);
444 unspec_ship_set_exists  EXCEPTION;
445 incomplete_delivery     EXCEPTION;
446 no_ship_method_code     EXCEPTION;
447 check_ord_line_split    EXCEPTION;
448 delivery_lines_mix      EXCEPTION;
449 l_del_name            VARCHAR2(30);
450 l_enforce_ship_method  VARCHAR2(1);
451 l_ship_method_code     VARCHAR2(30);
452 l_missing_count        NUMBER;
453 CURSOR lpn_in_other_dock(p_delivery_id  NUMBER) IS
454   SELECT milk.concatenated_segments
455        , wstt.outermost_lpn
456   FROM mtl_item_locations_kfv milk
457       ,wms_shipping_transaction_temp wstt
458   WHERE wstt.delivery_id           = p_delivery_id
459   AND   wstt.organization_id       = p_org_id
460   AND   wstt.dock_appoint_flag     = 'N'
461   AND   wstt.direct_ship_flag      = 'Y'
462   AND   wstt.dock_door_id          <> p_dock_door_id
463   AND   milk.organization_id       = p_org_id
464   AND   milk.inventory_location_id =wstt.dock_door_id;
465 
466     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
467 BEGIN
468    IF (l_debug = 1) THEN
469       debug('In check deliveries','Check_Delivery');
470       debug('p_delivery_id  : ' || p_delivery_id ,'Check_Delivery');
471       debug('p_org_id       : ' || p_org_id      ,'Check_Delivery');
472       debug('p_dock_door_id : ' || p_dock_door_id,'Check_Delivery');
473    END IF;
474    x_return_Status := fnd_api.g_ret_sts_success;
475    x_error_code := 0;  -- everything is fine
476    -- This procedure does all validation for a Delivery before it can be CONFIRMED
477    -- x_error_code = 1  Missing Item Calling program should call Missing Item cursor
478    -- x_error_code = 2  Material Status does not allow Sales Order Issue transaction
479    -- x_error_code = 3  Shipset Validation Failed. x_ship_set will be populated with
480    -- Shipset Name, show it on mobile page
481    -- x_error_code = 4  For the delivery there are some LPNs loaded on different Dock
482    -- x_error_code = 5  Delivery could not be locked. Error
483    -- x_error_code = 6  No Ship_method_code is provided when it is required. Error
484    -- x_error_code = 7  Lines were split in order management . Error
485    --x_error_code  =8   All delivery Delivery Lines are not from the direct_ship source
486    -- Locked the record first, so that others will not able to ship the same delivery
487    IF p_delivery_id IS NOT NULL THEN
488       BEGIN
489          SELECT name
490          INTO l_del_name
491          FROM wsh_new_deliveries_ob_grp_v
492          WHERE delivery_id =p_delivery_id;
493       EXCEPTION
494          WHEN no_data_found THEN
495             NULL;
496       END;
497    END IF;
498    BEGIN
499       SELECT 1
500       INTO temp_val
501       FROM wsh_new_deliveries_ob_grp_v
502       WHERE delivery_id = p_delivery_id
503       FOR UPDATE NOWAIT;
504       IF (l_debug = 1) THEN
505          debug('Lock delivery successful','Check_Delivery');
506       END IF;
507    EXCEPTION WHEN others THEN
508           x_return_Status := fnd_api.g_ret_sts_error;
509           fnd_message.set_name('WMS','WMS_DEL_LOCK_FAILURE');
510           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
511           /*Failed to aquire locks on wsh_new_deliveries for <DELIVERY_NAME>*/
512           fnd_msg_pub.add;
513           fnd_msg_pub.count_and_get( p_count => x_msg_count,
514                                      p_data  => x_msg_data
515                                     );
516           /*Failed to lock the delivery */
517           x_error_code := 5;
518           IF (l_debug = 1) THEN
519              debug('Lock delivery unsuccessful','Check_Delivery');
520           END IF;
521           RETURN;
522    END;
523 
524    -- First check if the entire delivery is ready to be ship confirmed
525    -- If No means there are some Missing Item  exists. Show Missing Item page
526 /*   INV_SHIPPING_TRANSACTION_PUB.CHECK_ENTIRE_EZ_DELIVERY(
527                                  p_delivery_id     => p_delivery_id,
528                                  x_return_Status   => l_return_status,
529                                  x_error_msg       => l_error_msg);*/
530      wms_direct_ship_pvt.CHECK_MISSING_ITEM_CUR(
531                                  p_delivery_id  => p_delivery_id
532                                  ,p_dock_door_id => p_dock_door_id
533                                  ,p_organization_id => p_org_id
534                                  ,x_return_Status  => l_return_status
535                                  ,x_missing_count  => l_missing_count
536                               );
537 
538         IF l_return_Status =  'S' AND l_missing_count > 0 then
539      IF (l_debug = 1) THEN
540         debug('There are Missing Items','CHECK_DELIVERY');
541      END IF;
542      fnd_msg_pub.count_and_get( p_count => x_msg_count,
543                                 p_data  => x_msg_data
544                                 );
545      x_return_Status := fnd_api.g_ret_sts_error;
546      x_error_code := 1; -- not entire delivery is ready
547      rollback;
548      return;
549    ELSIF l_return_status <> 'S' THEN
550      x_return_Status := fnd_api.g_ret_sts_unexp_error;
551      rollback;
552      return;
553    END IF;
554 
555    IF (l_debug = 1) THEN
556       debug('Call to Iwms_direct_ship_pvt.CHECK_MISSING_ITEM_CUR successful ','Check_Delivery');
557    END IF;
558         -- Check Delivery Status (Material Status
559    INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS
560              (p_delivery_id    => p_delivery_id
561              ,x_return_Status => l_return_status
562              ,x_error_msg     => x_error_msg);
563       IF l_return_status = fnd_api.g_ret_sts_error THEN
564          fnd_message.set_name('WMS','WMS_NO_ISSUE_TRX');
565           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
566          /*Material Status prevents the sales order issue transaction for one
567            or more items in the delivery <DELIVERY_NAME>*/
568          FND_MSG_PUB.ADD;
569          IF (l_debug = 1) THEN
570             debug('Call to INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS ' ||
571                   'returned with Status E '||P_DELIVERY_ID,'Check_Delivery');
572          END IF;
573          x_error_code := 2;      -- status doesn't allow ship confirm
574         RAISE incomplete_delivery;
575       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
576           fnd_message.set_name('WMS','WMS_NO_ISSUE_TRX');
577           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
578          /*Material Status prevents the sales order issue transaction for one or
579            more items in the delivery <DELIVERY_NAME>*/
580           FND_MSG_PUB.ADD;
581           x_error_code := 2;
582          RAISE fnd_api.g_exc_unexpected_error;
583       END IF;
584       IF (l_debug = 1) THEN
585          debug('INV_SHIPPING_TRANSACTION_PUB.CHECK_DELIVERY_STATUS was successful','Check_Delivery');
586       END IF;
587         -- Check for Ship Set
588         -- x_return_status (l_return_status = 'C' then Shipset was Null,
589         -- 'E' means Shipset Validation failed. 'U' means Unexpected Error
590         WMS_SHIPPING_TRANSACTION_PUB.SHIP_SET_CHECK( p_trip_id => null,
591                             p_dock_door_id => p_dock_door_id,
592                             p_organization_id => p_org_id,
593                             x_ship_set      => l_ship_set,
594                             x_return_Status => l_return_status,
595                             x_error_msg     => l_error_msg,
596                             p_direct_ship_flag => 'Y' );
597      IF l_return_status = fnd_api.g_ret_sts_error THEN
598           IF (l_debug = 1) THEN
599              debug('WMS_SHIPPING_TRANSACTION_PUB.ship_set_check returned with status' ||
600                    ' E- shipset  '||l_ship_set,'Check_Delivery');
601           END IF;
602           FND_MESSAGE.SET_NAME('INV', 'WMS_WSH_SHIPSET_FORCED');
603           FND_MESSAGE.SET_TOKEN('SHIP_SET_NAME', l_ship_set);
604           FND_MSG_PUB.ADD;
605           RAISE unspec_ship_set_exists;
606       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
607           FND_MESSAGE.SET_NAME('INV', 'WMS_SHIPSET_FAILED');
608           FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
609           /*Ship set validation for the delivery <DELIVERY_NAME> has failed*/
610           FND_MSG_PUB.ADD;
611           IF (l_debug = 1) THEN
612              debug('WMS_SHIPPING_TRANSACTION_PUB.ship_set_check returned with status U','Check_Delivery');
613           END IF;
614            RAISE fnd_api.g_exc_unexpected_error;
615       END IF;
616       IF (l_debug = 1) THEN
617          debug('Call to WMS_SHIPPING_TRANSACTION_PUB.SHIP_SET_CHECK was successful ','Check_Delivery');
618       END IF;
619         -- Check if delivery has some delivery_details (LPNs) loaded on different dock door
620      OPEN lpn_in_other_dock(p_delivery_id);
621      FETCH lpn_in_other_dock INTO l_loaded_dock_door,x_outermost_lpn;
622      IF lpn_in_other_dock%FOUND THEN
623          IF (l_debug = 1) THEN
624             debug('Cursor lpn_in_other_dock returned a row ','Check_Delivery');
625          END IF;
626          BEGIN
627               SELECT name
628                  INTO x_delivery_name
629                  FROM wsh_new_deliveries_ob_grp_v
630                  WHERE delivery_id = p_delivery_id;
631               FND_MESSAGE.SET_NAME('WMS','WMS_LPN_OTHER_DOCK');
632               FND_MESSAGE.SET_TOKEN('LPN_NAME',x_outermost_lpn);
633               FND_MESSAGE.SET_TOKEN('DOCK',l_loaded_dock_door);
634               /* LPN <LPN_NAME> in this delivery <DELIVERY_NAME> is loaded at another dock door <DOCK>*/
635               FND_MSG_PUB.ADD;
636                fnd_msg_pub.count_and_get( p_count => x_msg_count,
637                                           p_data  => x_msg_data
638                                         );
639               l_return_status := fnd_api.g_ret_sts_error;
640               x_error_code    :=  4;
641               IF (l_debug = 1) THEN
642                  debug('lpn is in other dock door lpn,dock_door and del_id are '
643                         || x_outermost_lpn ||' - ' ||l_loaded_dock_door || ' - '
644                         ||p_delivery_id, 'Check_Delivery');
645               END IF;
646          EXCEPTION
647             WHEN no_data_found THEN
648                IF (l_debug = 1) THEN
649                   debug('In No data found -Check_Deliveries','Check_Delivery');
650                END IF;
651                null;
652          END;
653      END IF;
654      CLOSE lpn_in_other_dock;
655 
656      wms_direct_ship_pvt.chk_del_for_direct_ship(x_return_status =>l_return_status
657                                                  ,x_msg_count    => l_msg_count
658                                                  ,x_msg_data     => l_msg_data
659                                                  ,p_delivery_id  => p_delivery_id
660                                                     );
661       IF l_return_status = fnd_api.g_ret_sts_error THEN
662          IF (l_debug = 1) THEN
663             debug('Call to chk_del_for_direct_ship returned with Status E '||P_DELIVERY_ID,'Check_Delivery');
664          END IF;
665          RAISE delivery_lines_mix;
666       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
667           IF (l_debug = 1) THEN
668              debug('Call to chk_del_for_direct_ship returned with Status U '||P_DELIVERY_ID,'Check_Delivery');
669           END IF;
670           RAISE fnd_api.g_exc_unexpected_error;
671       END IF;
672    EXCEPTION
673         WHEN unspec_ship_set_exists THEN
674             x_return_status := fnd_api.g_ret_sts_error;
675             x_error_code := 3;
676             IF (l_debug = 1) THEN
677                debug('In exception unspec_ship_set_exists -Check_Deliveries - errorcode '
678                       ||x_error_code,'Check_Delivery');
679             END IF;
680             --  Get message count and data
681             fnd_msg_pub.count_and_get
682               (  p_count => x_msg_count
683                , p_data  => x_msg_data);
684 
685         WHEN incomplete_delivery THEN
686            x_return_status := fnd_api.g_ret_sts_error;
687            fnd_msg_pub.count_and_get( p_count => x_msg_count,
688                                     p_data  => x_msg_data
689                                   );
690             x_error_code := 2;
691             IF (l_debug = 1) THEN
692                debug('In exception incomplete_delivery -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
693             END IF;
694 
695         WHEN no_ship_method_code THEN
696            x_return_status := fnd_api.g_ret_sts_error;
697            FND_MESSAGE.SET_NAME('WMS','WMS_SHIP_METHOD_CODE');
698            FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
699            /* No Ship method code provided for the delivery DELIVERY_NAME .This is required */
700            FND_MSG_PUB.ADD;
701            fnd_msg_pub.count_and_get( p_count => x_msg_count,
702                                     p_data  => x_msg_data
703                                   );
704             IF (l_debug = 1) THEN
705                debug('In exception no_ship_method_code -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
706             END IF;
707             x_error_code := 6;
708         WHEN check_ord_line_split THEN
709            x_return_status := fnd_api.g_ret_sts_error;
710            x_error_code := 7;
711            fnd_msg_pub.count_and_get
712               (  p_count => x_msg_count
713                , p_data  => x_msg_data
714                );
715         WHEN delivery_lines_mix THEN
716            x_return_status := fnd_api.g_ret_sts_error;
717            x_error_code := 8;
718             FND_MESSAGE.SET_NAME('WMS','WMS_DEL_LINES_MIX');
719            FND_MESSAGE.SET_TOKEN('DELIVERY_NAME', l_del_name);
720            /* The Delivery also has lines that were not staged through Direct Ship*/
721            FND_MSG_PUB.ADD;
722            fnd_msg_pub.count_and_get
723               (  p_count => x_msg_count
724                , p_data  => x_msg_data
725                );
726         WHEN fnd_api.g_exc_unexpected_error THEN
727          x_return_status := fnd_api.g_ret_sts_unexp_error ;
728           --  Get message count and data
729             fnd_msg_pub.count_and_get
730               (  p_count => x_msg_count
731                , p_data  => x_msg_data
732                );
733             IF (l_debug = 1) THEN
734                debug('In unexpected  -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
735             END IF;
736 
737         WHEN OTHERS THEN
738             x_return_status := fnd_api.g_ret_sts_unexp_error;
739             fnd_msg_pub.count_and_get
740               (  p_count => x_msg_count
741                , p_data  => x_msg_data
742                );
743                x_error_code := 5;
744             IF (l_debug = 1) THEN
745                debug('In exception when others -Check_Deliveries - errorcode '||x_error_code,'Check_Delivery');
746             END IF;
747 
748 END CHECK_DELIVERY;
749 
750 PROCEDURE UPDATE_DELIVERY(
751               x_return_status        OUT NOCOPY VARCHAR2
752              ,x_msg_count            OUT NOCOPY NUMBER
753              ,x_msg_data             OUT NOCOPY VARCHAR2
754              ,p_delivery_id             IN  NUMBER
755              ,p_net_weight                 IN  NUMBER
756              ,p_gross_weight            IN  NUMBER
757              ,p_wt_uom_code             IN  VARCHAR2
758              ,p_waybill                    IN  VARCHAR2
759              ,p_ship_method_code      IN  VARCHAR2
760              ,p_fob_code                   IN  VARCHAR2
761              ,p_fob_location_id      IN  NUMBER
762              ,p_freight_term_code     IN         VARCHAR2
763              ,p_freight_term_name     IN  VARCHAR2
764              ,p_intmed_shipto_loc_id  IN  NUMBER
765                          )IS
766 
767 l_init_msg_list                  VARCHAR2(1) :=FND_API.G_TRUE;
768 l_return_status                  VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
769 l_msg_count                      NUMBER;
770 l_msg_data                       VARCHAR2(20000);
771 l_fob_location_id      NUMBER := P_fob_location_id;
772 l_intmed_shipto_loc_id NUMBER :=p_intmed_shipto_loc_id;
773 
774 
775 l_freight_cost_rec                WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
776 l_delivery_info                   WSH_DELIVERIES_PUB.Delivery_Pub_Rec_Type;
777 l_row_id                          ROWID;
778 l_delivery_id                     NUMBER;
779 l_freight_cost_id                 NUMBER;
780 l_delivery_name                   VARCHAR2(30);
781 l_name                            VARCHAR2(30);
782 l_del_name                        VARCHAR2(30); /* fOR MESSAGE TOKEN */
783 l_status_code                     VARCHAR2(3);
784 l_trip_id                         NUMBER; -- 2767767
785 
786 CURSOR delivery_freight IS
787 SELECT  ROWID
788         ,FREIGHT_COST_TYPE_ID
789         ,CURRENCY_CODE
790                   ,FREIGHT_AMOUNT
791                   ,CONVERSION_TYPE
792         FROM  WMS_FREIGHT_COST_TEMP
793         WHERE delivery_id = p_delivery_id
794           AND FREIGHT_COST_ID IS NULL
795         FOR UPDATE OF FREIGHT_COST_ID;
796     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
797 BEGIN
798     x_return_status := fnd_api.g_ret_sts_success;
799 
800  IF p_delivery_id IS NOT NULL THEN
801      SELECT status_code
802      INTO l_status_code
803      FROM wsh_new_deliveries_ob_grp_v
804      WHERE delivery_id = p_delivery_id;
805 
806      IF l_status_code IN ('CL','CO','IT') THEN
807         RETURN ;
808      END IF;
809   ELSE
810      RETURN;
811   END IF;
812 
813     IF (l_debug = 1) THEN
814        debug('In Update delivery procedure ','update_Delivery');
815        DEBUG('p_delivery_id          : '||p_delivery_id,'update_Delivery');
816        DEBUG('p_ship_method_code     : '||p_ship_method_code,'update_Delivery');
817        DEBUG('p_FREIGHT_TERM_CODE    : '||p_FREIGHT_TERM_CODE,'update_Delivery');
818        DEBUG('p_fob_code             : '||p_fob_code,'update_Delivery');
819        DEBUG('p_fob_location_id      : '||p_fob_location_id,'update_Delivery');
820        DEBUG('p_waybill              : '||p_waybill,'update_Delivery');
821        DEBUG('p_net_weight           : '||p_net_weight,'update_Delivery');
822        DEBUG('p_gross_weight         : '||p_gross_weight,'update_Delivery');
823        DEBUG('p_wt_uom_code          : '||p_wt_uom_code,'update_Delivery');
824        DEBUG('p_intmed_shipto_loc_id : '||p_intmed_shipto_loc_id,'update_Delivery');
825     END IF;
826 
827     IF p_delivery_id IS NOT NULL  THEN
828        BEGIN
829           SELECT name
830              INTO l_del_name
831              FROM wsh_new_deliveries_ob_grp_v
832              WHERE delivery_id = p_delivery_id;
833        EXCEPTION
834           WHEN NO_DATA_FOUND THEN
835              NULL;
836        END;
837     END IF;
838 
839 OPEN delivery_freight;
840 LOOP
841   FETCH delivery_freight into l_row_id
842                              ,l_freight_cost_rec.FREIGHT_COST_TYPE_ID
843                              ,l_freight_cost_rec.CURRENCY_CODE
844                              ,l_freight_cost_rec.UNIT_AMOUNT
845                              ,l_freight_cost_rec.CONVERSION_TYPE_CODE;
846 
847        l_freight_cost_rec.delivery_ID     :=p_delivery_id;
848        l_freight_cost_rec.action_code     := 'CREATE';
849   EXIT WHEN delivery_freight%NOTFOUND;
850 
851   WSH_FREIGHT_COSTS_PUB.Create_Update_Freight_Costs (
852                          p_api_version_number     =>1.0
853                        , p_init_msg_list          =>l_init_msg_list
854                        , p_commit                 => FND_API.G_FALSE
855                        , x_return_status          => l_return_status
856                        , x_msg_count              => l_msg_count
857                        , x_msg_data               => l_msg_data
858                        , p_pub_freight_costs      => l_freight_cost_rec
859                        , p_action_code            => 'CREATE'
860                        , x_freight_cost_id        => l_freight_cost_id
861                        );
862 
863   IF( l_return_status IN(fnd_api.g_ret_sts_error)) THEN
864      FND_MESSAGE.SET_NAME('WMS','WMS_CREATE_FREIGHT_FAIL');
865      FND_MESSAGE.SET_TOKEN('OBJ','Delivery');
866      FND_MESSAGE.SET_TOKEN('VAL',l_del_name);
867      /* Creation of freight cost for OBJ VAL has failed  */
868      fnd_msg_pub.add;
869      IF (l_debug = 1) THEN
870         debug('Update of Freight Cost for Del is failed ','UPDATE_DELIVERY');
871      END IF;
872      raise FND_API.G_EXC_ERROR;
873   ELSIF (l_return_status = 'U' ) THEN
874      FND_MESSAGE.SET_TOKEN('OBJ','Delivery');
875      FND_MESSAGE.SET_TOKEN('VAL',l_del_name);
876      /* Creation of freight cost for <OBJ> <VAL> has failed  */
877       fnd_msg_pub.add;
878        IF (l_debug = 1) THEN
879           debug('Update of Freight Cost for Del is succ ','UPDATE_DELIVERY');
880        END IF;
881        raise fnd_api.g_exc_unexpected_error;
882   END IF;
883 
884   UPDATE WMS_FREIGHT_COST_TEMP
885   SET freight_cost_id = l_freight_cost_id,
886       last_update_date=  SYSDATE,
887       last_updated_by =  FND_GLOBAL.USER_ID
888   WHERE rowid = l_row_id;
889 
890 END LOOP;
891 CLOSE delivery_freight;
892    IF l_intmed_shipto_loc_id =0 THEN
893       l_intmed_shipto_loc_id := NULL;
894    END IF;
895    IF l_fob_location_id =0  THEN
896        l_fob_location_id:=NULL;
897    END IF;
898    IF (p_net_weight             IS NOT NULL OR
899        p_gross_weight           IS NOT NULL OR
900        p_wt_uom_code            IS NOT NULL OR
901        p_waybill                IS NOT NULL OR
902        p_ship_method_code       IS NOT NULL OR
903        p_fob_code               IS NOT NULL OR
904        p_fob_location_id        IS NOT NULL OR
905        p_freight_term_code      IS NOT NULL OR
906        p_freight_term_name      IS NOT NULL OR
907        p_intmed_shipto_loc_id   IS NOT NULL
908           )  THEN
909 
910        -- 2767767
911        -- if delivery is assigned to trip update of ship method is not allowed
912        BEGIN
913               SELECT distinct wts.trip_id
914               INTO l_trip_id
915               FROM wsh_delivery_legs_ob_grp_v wdl,
916                    wsh_trip_stops_ob_grp_v wts
917               WHERE wdl.delivery_id=p_delivery_id
918               AND   wdl.pick_up_stop_id=wts.stop_id
919               AND rownum=1;
920             IF (l_debug = 1) THEN
921                 debug('Delivery '||p_delivery_id||' is assigned to trip '||l_trip_id,'Update Delivery');
922              END IF;
923           EXCEPTION
924               WHEN NO_DATA_FOUND THEN
925               l_trip_id:=NULL;
926           END;
927 
928 
929           l_delivery_info.DELIVERY_ID                            := p_delivery_id;
930           -- 2767767
931           IF l_trip_id IS  NULL THEN
932              l_delivery_info.SHIP_METHOD_CODE                       := p_ship_method_code;
933               IF (l_debug = 1) THEN
934                 debug('Delivery ' ||p_delivery_id||' is assigned to trip '
935                       ||l_trip_id ||' and hence not updating ship method','Update Delivery');
936               END IF;
937           END IF;
938           l_delivery_info.FREIGHT_TERMS_CODE        := p_freight_term_code;
939           l_delivery_info.FOB_CODE                  := p_fob_code;
940           l_delivery_info.FOB_LOCATION_ID           := l_fob_location_id;
941           l_delivery_info.WAYBILL                   := p_waybill;
942           l_delivery_info.NET_WEIGHT                := p_net_weight;
943           l_delivery_info.GROSS_WEIGHT              := p_gross_weight;
944           l_delivery_info.WEIGHT_UOM_CODE           := p_wt_uom_code;
945           l_delivery_info.INTMED_SHIP_TO_LOCATION_ID:=l_intmed_shipto_loc_id;
946 
947            WSH_DELIVERIES_PUB.Create_Update_Delivery(
948                        p_api_version_number                => 1.0
949                       ,p_init_msg_list                     => l_init_msg_list
950                       ,x_return_status                     => l_return_status
951                       ,x_msg_count                         => l_msg_count
952                       ,x_msg_data                          => l_msg_data
953                       ,p_action_code                       => 'UPDATE'
954                       ,p_delivery_info                     => l_delivery_info
955                       ,p_delivery_name                     => l_delivery_name
956                       ,x_delivery_id                       => l_delivery_id
957                       ,x_name                              => l_name);
958 
959              IF( l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
960                 IF (l_debug = 1) THEN
961                    debug('Create _Update_Delivery has errored ','UPDATE_DELIVERY');
962                 END IF;
963                 FND_MESSAGE.SET_NAME('WMS','WMS_UPDATE_DELIVERY_FAILED');
964                 FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
965                /* Updation of delivery <DELIVERY_NAME> has failed */
966                 fnd_msg_pub.add;
967                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
968              ELSIF (l_return_status IN (fnd_api.g_ret_sts_error)) THEN
969                 FND_MESSAGE.SET_NAME('WMS','UPDATE_DELIVERY_FAILED');
970                 FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
971                /* Updation of delivery <DELIVERY_NAME> has failed */
972                 fnd_msg_pub.add;
973                 raise FND_API.G_EXC_ERROR;
974              END IF;
975    END IF;
976    IF (l_debug = 1) THEN
977       debug('Update delivery completed successfully');
978    END IF;
979 EXCEPTION
980    WHEN fnd_api.g_exc_error THEN
981       x_return_status := fnd_api.g_ret_sts_error;
982        fnd_msg_pub.count_and_get( p_count => x_msg_count,
983                                   p_data  => x_msg_data
984                                   );
985       ROLLBACK;
986 
987       IF delivery_freight%ISOPEN THEN
988              CLOSE delivery_freight;
989       END IF;
990 
991    WHEN fnd_api.g_exc_unexpected_error THEN
992       x_return_status := fnd_api.g_ret_sts_unexp_error;
993        fnd_msg_pub.count_and_get( p_count => x_msg_count,
994                                   p_data  => x_msg_data
995                                   );
996       ROLLBACK;
997 
998       IF delivery_freight%ISOPEN THEN
999              CLOSE delivery_freight;
1000       END IF;
1001       IF (l_debug = 1) THEN
1002          debug('Update of Delivery has failed :Unexpected Error','Update Delivery');
1003       END IF;
1004 
1005    WHEN OTHERS THEN
1006       ROLLBACK;
1007       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1008         fnd_msg_pub.count_and_get( p_count => x_msg_count,
1009                                    p_data  => x_msg_data
1010                                 );
1011       IF delivery_freight%ISOPEN THEN
1012              CLOSE delivery_freight;
1013       END IF;
1014       IF (l_debug = 1) THEN
1015          debug('Update of Delivery has failed : Unexpected Error '||SQLERRM,'Update_Delivery');
1016       END IF;
1017 
1018           IF SQLCODE IS NOT NULL THEN
1019             IF (l_debug = 1) THEN
1020                debug(SQLCODE,'Update_Delivery');
1021             END IF;
1022           END IF;
1023 
1024 END; -- UPDATE_DELIVERY
1025 
1026 PROCEDURE MISSING_ITEM_CUR( x_missing_item_cur  OUT NOCOPY t_genref
1027                             ,p_delivery_id         IN  NUMBER
1028                             ,p_dock_door_id        IN  NUMBER
1029                            ,p_organization_id   IN  NUMBER
1030                                    ) IS
1031 l_count NUMBER;
1032     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1033 BEGIN
1034    IF (l_debug = 1) THEN
1035       Debug( ' in missing_item_check', 'MISSING_ITEM_CUR');
1036    END IF;
1037    l_count := 0;
1038 
1039   IF ( p_delivery_id <> 0 ) then
1040         IF (l_debug = 1) THEN
1041            Debug( 'p_dock_door_id : ' || p_dock_door_id, 'MISSING_ITEM_CUR');
1042            Debug( 'p_organization_id : ' || p_organization_id, 'MISSING_ITEM_CUR');
1043         END IF;
1044     BEGIN
1045        OPEN x_missing_item_cur FOR
1046        select wnd.name,
1047               wdd.delivery_detail_id,
1048               wdd.inventory_item_id,
1049               wdd.requested_quantity,
1050               msik.concatenated_segments,
1051               msik.description
1052        from wsh_delivery_details_ob_grp_v wdd
1053            ,wsh_delivery_assignments_v wda
1054            ,wsh_new_deliveries_ob_grp_v wnd
1055            ,mtl_system_items_kfv msik
1056        where wnd.delivery_id = p_delivery_id
1057        AND   wnd.delivery_id = wda.delivery_id
1058        AND   wda.delivery_detail_id = wdd.delivery_detail_id
1059        AND   wdd.lpn_id is null
1060        AND   wdd.inventory_item_id = msik.inventory_item_id
1061        AND   wdd.organization_id = msik.organization_id
1062        AND   ((wda.parent_delivery_detail_id is null
1063                AND msik.mtl_transactions_enabled_flag <> 'N')
1064                OR wdd.released_status is null
1065                OR wdd.released_status NOT IN ('X', 'Y'));
1066 
1067         IF (l_debug = 1) THEN
1068            debug('Right Cur: Delivery Id : '||p_delivery_id||' Org Id '
1069                   ||p_organization_id||' Dock Door '||p_dock_door_id,'Missing_Item_Cur');
1070         END IF;
1071         RETURN;
1072     EXCEPTION
1073        WHEN NO_DATA_FOUND THEN
1074         IF (l_debug = 1) THEN
1075            debug('Dummy Cur: Delivery Id : '||p_delivery_id||' Org Id '
1076                   ||p_organization_id||' Dock Door '||p_dock_door_id,'Missing_Item_Cur');
1077         END IF;
1078         open x_missing_item_cur FOR select 1 from dual;
1079         return;
1080     END;
1081   END IF;
1082 END MISSING_ITEM_CUR;
1083 
1084 PROCEDURE SHIP_CONFIRM(
1085               x_return_status          OUT  NOCOPY VARCHAR2
1086               ,x_msg_count             OUT  NOCOPY NUMBER
1087               ,x_msg_data                   OUT  NOCOPY VARCHAR2
1088               ,x_missing_item_cur      OUT  NOCOPY t_genref
1089               ,x_error_code              OUT  NOCOPY NUMBER
1090               ,p_delivery_id             IN  NUMBER
1091               ,p_net_weight              IN  NUMBER     DEFAULT NULL
1092               ,p_gross_weight            IN  NUMBER     DEFAULT NULL
1093               ,p_wt_uom_code             IN  VARCHAR2   DEFAULT NULL
1094               ,p_waybill                    IN  VARCHAR2        DEFAULT NULL
1095               ,p_ship_method_code      IN  VARCHAR2     DEFAULT NULL
1096               ,p_fob_code                   IN  VARCHAR2        DEFAULT NULL
1097               ,p_fob_location_id       IN  NUMBER       DEFAULT NULL
1098               ,p_freight_term_code     IN  VARCHAR2     DEFAULT NULL
1099               ,p_freight_term_name     IN  VARCHAR2     DEFAULT NULL
1100               ,p_intmed_shipto_loc_id  IN  NUMBER       DEFAULT NULL
1101               ,p_org_id                     IN  NUMBER  DEFAULT NULL
1102               ,p_dock_door_id            IN  NUMBER     DEFAULT NULL
1103               ) IS
1104 l_init_msg_list                 VARCHAR2(1) :=FND_API.G_TRUE;
1105 l_return_status                 VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
1106 l_msg_count                     NUMBER;
1107 l_msg_data                      VARCHAR2(20000);
1108 
1109 l_ship_set                      VARCHAR2(2000) := NULL;
1110 l_error_msg                     VARCHAR2(20000) := NULL;
1111 l_trip_id                       NUMBER;
1112 l_trip_name                     VARCHAR2(30);
1113 unspec_ship_set_exists          EXCEPTION;
1114 incomplete_delivery             EXCEPTION;
1115 l_error_code                    NUMBER;
1116 x_ret_code                      number;
1117 l_status_code                   varchar2(3);
1118 l_del_name                      VARCHAR2(30);
1119 l_msg_table                     WSH_INTEGRATION.MSG_TABLE ;
1120 l_count                         NUMBER := 0;
1121 
1122 -- varajago: Added the following 3, for 5204686
1123 l_enforce_shipmethod            VARCHAR2(1) ;
1124 l_trip_shipmethod_code          VARCHAR2(30);
1125 l_trip_shipmethod_meaning       VARCHAR2(80);
1126 
1127 
1128 l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1129 
1130 l_ignore_for_planning     wsh_delivery_details.ignore_for_planning%type;
1131 l_tms_interface_flag      wsh_new_deliveries.TMS_INTERFACE_FLAG%type;
1132 l_otm_trip_id             wsh_trips.trip_id%type;
1133 l_otm_carrier_id          wsh_trips.carrier_id%type;
1134 l_otm_ship_method_code    wsh_trips.ship_method_code%type;
1135 l_otm_mode                wsh_trips.mode_of_transport%type;
1136 l_otm_plan_name           wsh_trips.tp_plan_name%type;
1137 
1138 l_severity          WSH_EXCEPTIONS.SEVERITY%TYPE;
1139 l_exception_name    WSH_EXCEPTIONS.EXCEPTION_NAME%TYPE;
1140 l_exception_text    WSH_EXCEPTIONS.MESSAGE%TYPE;
1141 
1142 l_otm_ifp_del_ids wsh_util_core.id_tab_type;
1143 
1144 l_num_warn         NUMBER :=0;
1145 l_num_error       NUMBER :=0;
1146 
1147 
1148 CURSOR c_Get_Trip(v_del_id NUMBER) IS
1149    select wt.trip_id , wt.carrier_id, wt.ship_method_code, wt.mode_of_transport,
1150      wt.tp_plan_name -- glog proj
1151      from wsh_delivery_legs wdl, wsh_trip_stops wts, wsh_trips wt
1152      where wdl.pick_up_stop_id=wts.stop_id
1153      and wdl.delivery_id=v_del_id
1154      and wts.trip_id=wt.trip_id;
1155 
1156   CURSOR c_get_delivery_info IS
1157      SELECT status_code,NAME,ignore_for_planning, tms_interface_flag
1158        FROM wsh_new_deliveries_ob_grp_v
1159        WHERE delivery_id = p_delivery_id;
1160 
1161 BEGIN
1162   x_return_status := FND_API.G_RET_STS_SUCCESS;
1163   x_ret_code := 0;
1164   IF (l_debug = 1) THEN
1165      debug('p_delivery_id          : '||p_delivery_id,'ship_confirm');
1166      debug('p_net_weight           : '||p_net_weight,'ship_confirm');
1167      debug('p_gross_weight         : '||p_gross_weight,'ship_confirm');
1168      debug('p_wt_uom_code          : '||p_wt_uom_code,'ship_confirm');
1169      debug('p_waybill              : '||p_waybill,'ship_confirm');
1170      debug('p_ship_method_code     : '||p_ship_method_code,'ship_confirm');
1171      debug('p_fob_code             : '||p_fob_code,'ship_confirm');
1172      debug('p_fob_location_id      : '||p_fob_location_id,'ship_confirm');
1173      debug('p_freight_term_code    : '||p_freight_term_code,'ship_confirm');
1174      debug('p_freight_term_name    : '||p_freight_term_name,'ship_confirm');
1175      debug('p_intmed_shipto_loc_id : '||p_intmed_shipto_loc_id,'ship_confirm');
1176      debug('p_org_id               : '||p_org_id,'ship_confirm');
1177      debug('p_dock_door_id         : '||p_dock_door_id,'ship_confirm');
1178   END IF;
1179 
1180   IF p_delivery_id IS NOT NULL THEN
1181 
1182      OPEN c_get_delivery_info;
1183      FETCH c_get_delivery_info INTO l_status_code,l_del_name, l_ignore_for_planning, l_tms_interface_flag;
1184 
1185      IF (c_get_delivery_info%NOTFOUND) THEN
1186 	NULL;
1187      END IF;
1188 
1189      CLOSE c_get_delivery_info;
1190 
1191 
1192      IF l_status_code IN ('CL','CO','IT') THEN
1193 	RETURN ;
1194      END IF;
1195    ELSE
1196      RETURN;
1197   END IF;
1198 
1199   IF (    p_net_weight          IS NOT NULL OR
1200 	  p_gross_weight        IS NOT NULL OR
1201 	  p_wt_uom_code         IS NOT NULL OR
1202 	  p_waybill             IS NOT NULL OR
1203 	  p_ship_method_code    IS NOT NULL OR
1204 	  p_fob_code    IS NOT NULL OR
1205 	  p_fob_location_id     IS NOT NULL OR
1206 	  p_freight_term_code   IS NOT NULL OR
1207 	  p_freight_term_name   IS NOT NULL OR
1208 	  p_intmed_shipto_loc_id IS NOT NULL
1209 	  )  THEN
1210      WMS_DIRECT_SHIP_PVT.UPDATE_DELIVERY(
1211               x_return_status          => l_return_status
1212               ,x_msg_count             => l_msg_count
1213               ,x_msg_data              => l_msg_data
1214               ,p_delivery_id             => p_delivery_id
1215               ,p_net_weight              => p_net_weight
1216               ,p_gross_weight            => p_gross_weight
1217               ,p_wt_uom_code             => p_wt_uom_code
1218               ,p_waybill                         => p_waybill
1219               ,p_ship_method_code      => p_ship_method_code
1220               ,p_fob_code                   => p_fob_code
1221               ,p_fob_location_id              => p_fob_location_id
1222               ,p_freight_term_code     => p_freight_term_code
1223               ,p_freight_term_name     => p_freight_term_name
1224               ,p_intmed_shipto_loc_id  => p_intmed_shipto_loc_id
1225               );
1226      IF l_return_status = fnd_api.g_ret_sts_error THEN
1227 	IF (l_debug = 1) THEN
1228 	   DEBUG('Update Delivery API failed with status E ','SHIP_CONFIRM');
1229 	END IF;
1230 	RAISE fnd_api.g_exc_error;
1231       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1232 	IF (l_debug = 1) THEN
1233 	   DEBUG('Update Delivery API failed with status U','SHIP_CONFIRM');
1234 	 END IF;
1235 	 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1236      END IF;
1237   END IF;
1238 
1239   -- Check for material status, shipset and missing item
1240   IF (l_debug = 1) THEN
1241      debug('WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM. Check the delivery for the correctness ' || ' before Confirming it','Ship_confirm');
1242   END IF;
1243   WMS_DIRECT_SHIP_PVT.CHECK_DELIVERY(
1244 				     x_return_status        => l_return_status
1245 				     ,x_msg_count            => l_msg_count
1246 				     ,x_msg_data        => l_msg_data
1247 				     ,x_error_code           => l_error_code
1248 				     ,p_delivery_id          => p_delivery_id
1249 				     ,p_org_id          => p_org_id
1250 				     ,p_dock_door_id         => p_dock_door_id
1251 				     );
1252 
1253   IF l_error_code = 1 THEN  -- Case of Missing Item, Call Missing Item Cursor
1254      IF (l_debug = 1) THEN
1255 	debug(' Ship_Confirm. Missing Item Error','Ship_Confirm');
1256      END IF;
1257      WMS_DIRECT_SHIP_PVT.MISSING_ITEM_CUR(
1258 					  x_missing_item_cur      => x_missing_item_cur
1259 					  ,p_delivery_id            => p_delivery_id
1260 					  ,p_organization_id        => p_org_id
1261 					  ,p_dock_door_id   => p_dock_door_id);
1262      x_error_code := l_error_code;
1263      x_return_status := FND_API.G_RET_STS_ERROR;
1264      fnd_msg_pub.count_and_get( p_count => x_msg_count,
1265 				p_data  => x_msg_data);
1266      RETURN;
1267    ELSIF (l_error_code in (2,3,4,5,6,7,8) ) THEN
1268      IF (l_debug = 1) THEN
1269         debug(' Ship_Confirm Error. ShipSet, LPN Loaded on different Dock,ship_method_code ','Ship_Confirm');
1270      END IF;
1271      x_error_code := l_error_code;
1272      x_return_status := l_return_status;
1273      fnd_msg_pub.count_and_get( p_count => x_msg_count,
1274                                 p_data  => x_msg_data);
1275      RETURN;
1276   END IF ;
1277 
1278 
1279   /* Note: We do not need this update to ignore for planning here again becs things are taken care of during
1280   the stage_lpn() API. At this time, we will always have the delivery - either existing or created in stage_LPN()
1281     there is no check on exception severity in stage_lpn().
1282 
1283   -- Glog Changes
1284   IF WSH_UTIL_CORE.GC3_IS_INSTALLED = 'Y' AND nvl(l_ignore_for_planning, 'N') = 'N' THEN
1285 
1286      l_otm_trip_id  := 0;
1287      l_otm_carrier_id   := 0;
1288      l_otm_ship_method_code := NULL;
1289      l_otm_mode     := NULL;
1290      l_otm_plan_name    := NULL;
1291 
1292      OPEN c_GET_TRIP(p_delivery_id);
1293      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;
1294 
1295      IF (c_GET_TRIP%NOTFOUND) THEN
1296 	l_otm_ship_method_code := p_ship_method_code;
1297      END IF;
1298 
1299      CLOSE c_GET_TRIP;
1300 
1301      IF l_debug = 1 THEN
1302 	debug(' otm_plan_name : ' || l_otm_plan_name, 'Ship Confirm');
1303 	debug(' otm_trip_id : '|| l_otm_trip_id, 'Ship Confirm');
1304 	debug(' tms_interface_flag : ' ||l_tms_interface_flag, 'Ship Confirm');
1305      END IF;
1306 
1307      --
1308      -- The 'null' tp_plan_name indicates that the delivery is not
1309      -- associated to an OTM plan. Hence ignore the delivery for planning.
1310      -- (CR->NS and AW->DR for 'Not assigned' to an OTM trip Delivery).
1311      -- This update for the ignore_for_plan to 'Y' is needed to autocreate the trip
1312      -- for this delivery during ship conform.
1313      --
1314 
1315      WSH_INTERFACE_EXT_GRP.OTM_PRE_SHIP_CONFIRM(
1316 					    p_delivery_id  =>  p_delivery_id,
1317 					    p_tms_interface_flag  => l_tms_interface_flag,
1318 					    p_trip_id   => l_otm_trip_id,
1319 					    x_return_status =>  l_return_status);
1320   END IF;
1321 */
1322 
1323   -- Create the Trip for the delivery is it does not exists already
1324   IF (l_debug = 1) THEN
1325      debug(' Ship_Confirm. Check Delivery was success, Create Trip if needed for delivery','Ship_Confirm');
1326   END IF;
1327 
1328   IF (l_debug = 1) THEN
1329      debug(' Ship_Confirm. Calling CREATE_TRIP','Ship_Confirm');
1330      debug('p_org_id  '||p_org_id);
1331      debug('p_dock_door_id '||p_dock_door_id);
1332   END IF;
1333    WMS_DIRECT_SHIP_PVT.CREATE_TRIP(
1334              x_return_status    => l_return_status
1335              ,p_organization_id  => p_org_id
1336              ,p_dock_door_id     => p_dock_door_id
1337        ,p_delivery_id      =>p_delivery_id /* bug 2741857 */
1338              ,p_direct_ship_flag => 'Y'
1339              );
1340      IF l_return_status = fnd_api.g_ret_sts_error THEN
1341         IF (l_debug = 1) THEN
1342            debug(' Ship_Confirm. Create Trip Failed','Ship_Confirm');
1343         END IF;
1344         fnd_message.set_name('WMS','WMS_CREATE_TRIP_FAIL');
1345         FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1346         /* Failed to create trip for the delivery DELIVERY_NAME */
1347         FND_MSG_PUB.ADD;
1348         RAISE fnd_api.g_exc_error;
1349      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1350         fnd_message.set_name('WMS','WMS_CREATE_TRIP_FAIL');
1351         FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1352         FND_MSG_PUB.ADD;
1353         IF (l_debug = 1) THEN
1354            debug(' Ship_Confirm. Create Trip Failed Unexpected Error','Ship_Confirm');
1355         END IF;
1356         RAISE fnd_api.g_exc_unexpected_error;
1357      END IF;
1358       IF (l_debug = 1) THEN
1359          debug('before calling Ship Confirm','Ship Confirm');
1360       END IF;
1361 
1362       INV_SHIPPING_TRANSACTION_PUB.get_shipmethod_details
1363                     ( p_org_id                  => p_org_id
1364                      ,p_delivery_id             => p_delivery_id
1365                      ,p_enforce_shipmethod      => l_enforce_shipmethod
1366                      ,p_trip_id                 => l_trip_id
1367                      ,x_trip_shipmethod_code    => l_trip_shipmethod_code
1368                      ,x_trip_shipmethod_meaning => l_trip_shipmethod_meaning
1369                      ,x_return_status           => l_return_status
1370                      ,x_msg_data                => l_msg_data
1371                      ,x_msg_count               => l_msg_count);
1372 
1373       IF (l_debug = 1) THEN
1374           debug('get_shipmethod_details.' ,'Ship_Confirm');
1375           debug('l_return_status: '|| l_return_status,'Ship_Confirm');
1376           debug('l_msg_data: '     || l_msg_data,     'Ship_Confirm');
1377           debug('l_msg_count: '    || l_msg_count,    'Ship_Confirm');
1378           debug('l_enforce_shipmethod: '      || l_enforce_shipmethod,    'Ship_Confirm');
1379           debug('l_trip_id: '      || l_trip_id,    'Ship_Confirm');
1380           debug('l_trip_shipmethod_code: '    || l_trip_shipmethod_code,   'Ship_Confirm');
1381           debug('l_trip_shipmethod_meaning: ' || l_trip_shipmethod_meaning,'Ship_Confirm');
1382       END IF;
1383       IF l_return_status = fnd_api.g_ret_sts_error THEN
1384             RAISE fnd_api.g_exc_error;
1385       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1386             RAISE fnd_api.g_exc_unexpected_error;
1387       END IF;
1388 
1389       -- If everything is fine then confirm the delivery
1390 
1391       --Set global to say shipping via Direct Ship instead of desktop
1392       --Added so that WMS_SHIPPING_PUB.DEL_WSTT_RECS_BY_DELIVERY_ID will
1393       --not fail in Shipping's API.
1394       wms_globals.g_ship_confirm_method := 'DIRECT';
1395       WSH_DELIVERIES_PUB.Delivery_Action ( p_api_version_number     => 1.0
1396                    ,p_init_msg_list          => l_init_msg_list
1397                    ,x_return_status          => l_return_status
1398                     ,x_msg_count              => l_msg_count
1399                     ,x_msg_data               => l_msg_data
1400                     ,p_action_code            => 'CONFIRM'
1401                     ,p_delivery_id            => p_delivery_id
1402                     ,p_sc_action_flag         => 'S'
1403                     ,p_sc_intransit_flag      => 'N'
1404                     ,p_sc_close_trip_flag     => 'N'
1405                     ,p_sc_create_bol_flag     => 'Y'
1406                     ,p_sc_stage_del_flag      => 'Y'
1407                     ,p_sc_defer_interface_flag => 'Y'
1408 		    ,p_sc_trip_ship_method       => l_trip_shipmethod_code -- added for 5204688
1409                     ,p_wv_override_flag       => 'N'
1410                     ,x_trip_id                =>  l_trip_id
1411             ,x_trip_name              =>  l_trip_name);
1412       wms_globals.g_ship_confirm_method := NULL;
1413 
1414      IF (l_debug = 1) THEN
1415         debug('after calling Ship confirm','Ship confirm');
1416      END IF;
1417      IF (l_return_status=fnd_api.g_ret_sts_success) THEN
1418      l_count:=0;
1419 
1420      FOR i in 1..WSH_INTEGRATION.G_MSG_TABLE.COUNT LOOP
1421             IF (WSH_INTEGRATION.G_MSG_TABLE(i).MESSAGE_TYPE = 'W' ) THEN
1422                l_count := l_count + 1;
1423                l_msg_table(l_count) := WSH_INTEGRATION.G_MSG_TABLE(i);
1424             END IF;
1425     END LOOP;
1426     IF (l_debug = 1) THEN
1427        debug('before calling process shipping warning msgs','ship_confirm');
1428     END IF;
1429     WMS_SHIPPING_MESSAGES.PROCESS_SHIPPING_WARNING_MSGS(x_return_status  => l_return_status ,
1430                                                             x_msg_count      => l_msg_count ,
1431                                                             x_msg_data       => l_msg_data,
1432                                                             p_commit         => FND_API.g_false,
1433                                                             x_shipping_msg_tab  => l_msg_table);
1434 
1435     FOR i in 1..l_msg_table.count LOOP
1436               IF ( l_msg_table(i).MESSAGE_TYPE = 'E' ) THEN
1437                  --l_error_exists := TRUE;
1438                 l_return_status:=fnd_api.g_ret_sts_error;
1439                  FOR j in 1..WSH_INTEGRATION.G_MSG_TABLE.COUNT LOOP
1440                     IF (l_msg_table(i).message_name = WSH_INTEGRATION.G_MSG_TABLE(j).MESSAGE_NAME) THEN
1441                        WSH_INTEGRATION.G_MSG_TABLE(j).MESSAGE_TYPE := 'E';
1442                     END IF;
1443                  END LOOP;
1444               END IF;
1445            END LOOP;
1446            l_msg_table.delete;
1447      END IF;
1448     IF( l_return_status IN (fnd_api.g_ret_sts_error)) THEN
1449        FND_MESSAGE.SET_NAME('WMS','WMS_CONFIRM_DEL_FAIL');
1450        FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1451        -- Ship Confirm of delivey <DELIVERY_NAME> failed
1452        FND_MSG_PUB.ADD;
1453        IF (l_debug = 1) THEN
1454           debug(' Ship_Confirm. Confirm Delivery is failing from WSH_DELIVERY_PUB','Ship_Confirm');
1455        END IF;
1456        raise FND_API.G_EXC_ERROR;
1457 
1458     ELSIF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
1459        FND_MESSAGE.SET_NAME('WMS','WMS_CONFIRM_DEL_FAIL');
1460        FND_MESSAGE.SET_TOKEN('DELIVERY_NAME',l_del_name);
1461        /* cONFIRM DELIVERY FAILED*/
1462        FND_MSG_PUB.ADD;
1463        IF (l_debug = 1) THEN
1464           debug(' Ship_Confirm. Confirm Delivery is failing from WSH_DELIVERY_PUB, Unexpected error','Ship_Confirm');
1465        END IF;
1466        raise FND_API.G_EXC_UNEXPECTED_ERROR;
1467     END IF;
1468 
1469     --Ship confirm successful.  Display only Interface Trip stop message from shipping
1470     process_mobile_msg;
1471     x_msg_count := fnd_msg_pub.count_msg;
1472 EXCEPTION
1473    WHEN fnd_api.g_exc_error THEN
1474       x_return_status := fnd_api.g_ret_sts_error;
1475       IF (l_debug = 1) THEN
1476          debug('In Exception (expected error) - E ','Ship_Confirm');
1477       END IF;
1478          fnd_msg_pub.count_and_get
1479             ( p_count => x_msg_count,
1480               p_data  => x_msg_data
1481               );
1482    WHEN fnd_api.g_exc_unexpected_error THEN
1483       x_return_status := fnd_api.g_ret_sts_unexp_error;
1484       IF (l_debug = 1) THEN
1485          debug('In Exception (unexpected error) - U ','Ship_Confirm');
1486       END IF;
1487          fnd_msg_pub.count_and_get
1488             ( p_count => x_msg_count,
1489               p_data  => x_msg_data
1490               );
1491    WHEN OTHERS THEN
1492       x_return_status := fnd_api.g_ret_sts_unexp_error;
1493       IF (l_debug = 1) THEN
1494          debug('In Exception (others)' ||SQLERRM,'Ship_Confirm');
1495       END IF;
1496          fnd_msg_pub.count_and_get
1497             ( p_count => x_msg_count,
1498               p_data  => x_msg_data
1499               );
1500 END;  -- SHIP_CONFIRM
1501 
1502 -- Called when User presses Trip Info Button or CLOSE TRUCK Button
1503 
1504 PROCEDURE CONFIRM_ALL_DELIVERIES(
1505               x_return_status        OUT NOCOPY VARCHAR2
1506              ,x_msg_count            OUT NOCOPY NUMBER
1507              ,x_msg_data             OUT NOCOPY VARCHAR2
1508              ,x_missing_item_cur     OUT NOCOPY t_genref
1509              ,x_error_code           OUT  NOCOPY NUMBER
1510              ,p_delivery_id          IN  NUMBER
1511              ,p_net_weight           IN  NUMBER
1512              ,p_gross_weight         IN  NUMBER
1513              ,p_wt_uom_code          IN  VARCHAR2
1514              ,p_waybill              IN  VARCHAR2
1515              ,p_ship_method_code     IN  VARCHAR2
1516              ,p_fob_code             IN  VARCHAR2
1517              ,p_fob_location_id      IN  NUMBER
1518              ,p_freight_term_code    IN  VARCHAR2
1519              ,p_freight_term_name    IN  VARCHAR2
1520              ,p_intmed_shipto_loc_id IN  NUMBER
1521              ,p_org_id               IN  NUMBER
1522              ,p_dock_door_id         IN  NUMBER)IS
1523 l_init_msg_list                                          VARCHAR2(1) :=FND_API.G_TRUE;
1524 l_return_status                                          VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
1525 l_msg_count                                                      NUMBER;
1526 l_msg_data                                                       VARCHAR2(20000);
1527 l_rowid                                                          ROWID;
1528 l_delivery_id                                            NUMBER;
1529 l_delivery_name                                          VARCHAR2(30);
1530 l_name                                                   VARCHAR2(30);
1531 l_ship_set                                               NUMBER;
1532 l_t_genref                                              t_genref;
1533 l_error_code                                             NUMBER;
1534 l_trip_id                                                NUMBER;
1535 l_trip_name                                              VARCHAR2(30);
1536 CURSOR loaded_deliveries IS
1537   SELECT DISTINCT WSTT.delivery_id
1538   FROM WMS_SHIPPING_TRANSACTION_TEMP wstt
1539       ,WSH_NEW_DELIVERIES_OB_GRP_V WND
1540   WHERE wstt.organization_id = p_org_id
1541   AND   wstt.dock_door_id = p_dock_door_id
1542   AND   wstt.dock_appoint_flag = 'N'
1543   AND   nvl(wstt.direct_ship_flag,'N') = 'Y'
1544   AND   wstt.delivery_id = wnd.delivery_id
1545   AND   wnd.status_code = 'OP';
1546 
1547 --
1548 CURSOR ALL_DELIVERIES IS
1549     SELECT DELIVERY_ID FROM WMS_SHIPPING_TRANSACTION_TEMP
1550     WHERE DELIVERY_ID IS NOT NULL
1551     AND TRIP_ID IS NULL
1552     AND ORGANIZATION_ID=P_ORG_ID
1553     AND DOCK_DOOR_ID=P_DOCK_DOOR_ID
1554     AND NVL(DIRECT_SHIP_FLAG,'N')='Y';
1555 
1556     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1557 BEGIN
1558 
1559 -- Call this API from Trip Info page of the Delivery Info. If thats the case
1560 -- then Delivery might have been changed. First update it then Shipconfirm all
1561 -- delivery loaded on the dock.
1562 IF (l_debug = 1) THEN
1563    debug('Confirm All Deliveries: Begin delivery_id'||to_char(p_delivery_id),'CONFIRM_ALL_DELIVERIES');
1564 END IF;
1565 x_return_status:= FND_API.G_RET_STS_SUCCESS;
1566        IF (l_debug = 1) THEN
1567           debug('The value OF p_delivery_id        is '||p_delivery_id,'Confirm_All_Delivery');
1568           debug('The value OF p_net_weight is '||p_net_weight,'Confirm_All_Delivery');
1569           debug('The value OF p_gross_weight       is '||p_gross_weight,'Confirm_All_Delivery');
1570           debug('The value OF p_wt_uom_code        is '||p_wt_uom_code,'Confirm_All_Delivery');
1571           debug('The value OF p_waybill    is '||p_waybill,'Confirm_All_Delivery');
1572           debug('The value OF p_ship_method_code   is '||p_ship_method_code,'Confirm_All_Delivery');
1573           debug('The value OF p_fob_code   is '||p_fob_code,'Confirm_All_Delivery');
1574           debug('The value OF p_fob_location_id    is '||p_fob_location_id,'Confirm_All_Delivery');
1575           debug('The value OF p_freight_term_code  is '||p_freight_term_code,'Confirm_All_Delivery');
1576           debug('The value OF p_freight_term_name  is '||p_freight_term_name,'Confirm_All_Delivery');
1577           debug('The value OF p_intmed_shipto_loc_id       is '||p_intmed_shipto_loc_id,'Confirm_All_Delivery');
1578           debug('The value OF p_org_id     is '||p_org_id,'Confirm_All_Delivery');
1579           debug('The value OF p_dock_door_id       is '||p_dock_door_id,'Confirm_All_Delivery');
1580        END IF;
1581 
1582  IF (p_net_weight              IS NOT NULL OR
1583           p_gross_weight       IS NOT NULL OR
1584           p_wt_uom_code        IS NOT NULL OR
1585           p_waybill            IS NOT NULL OR
1586           p_ship_method_code   IS NOT NULL OR
1587           p_fob_code           IS NOT NULL OR
1588           p_fob_location_id    IS NOT NULL OR
1589           p_freight_term_code  IS NOT NULL OR
1590           p_freight_term_name      IS NOT NULL OR
1591           p_intmed_shipto_loc_id   IS NOT NULL
1592           )  THEN
1593     WMS_DIRECT_SHIP_PVT.UPDATE_DELIVERY(
1594               x_return_status        => l_return_status
1595               ,x_msg_count           => l_msg_count
1596               ,x_msg_data            => l_msg_data
1597               ,p_delivery_id                     => p_delivery_id
1598               ,p_net_weight                      => p_net_weight
1599               ,p_gross_weight               => p_gross_weight
1600               ,p_wt_uom_code                     => p_wt_uom_code
1601               ,p_waybill                                 => p_waybill
1602               ,p_ship_method_code        => p_ship_method_code
1603               ,p_fob_code                      => p_fob_code
1604               ,p_fob_location_id                 => p_fob_location_id
1605               ,p_freight_term_code   => p_freight_term_code
1606               ,p_freight_term_name   => p_freight_term_name
1607               ,p_intmed_shipto_loc_id => p_intmed_shipto_loc_id
1608               );
1609     IF (l_debug = 1) THEN
1610        debug('Return Status from Update_Delivery: '||l_return_status, ' Confirm_All_Deliveries');
1611     END IF;
1612            IF l_return_status = fnd_api.g_ret_sts_error THEN
1613               IF (l_debug = 1) THEN
1614                  debug('Update Delivery API failed with status E ','Confirm_All_deliveries');
1615               END IF;
1616               RAISE fnd_api.g_exc_error;
1617            ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1618               IF (l_debug = 1) THEN
1619                  debug('Update Delivery API failed with status U','Confirm_All_Deliveries');
1620               END IF;
1621               raise FND_API.G_EXC_UNEXPECTED_ERROR;
1622            END IF;
1623  END IF;
1624  /* bug # 2994098 */
1625     -- If a delivery is ship confirmed from STF without closing the trip
1626     -- then trip id should be updated in wstt because user may try to close the trip from mobile.
1627     BEGIN
1628     FOR l_deliveries IN all_deliveries
1629     LOOP
1630        BEGIN
1631          SELECT wts.trip_id,wt.name
1632          INTO l_trip_id,l_trip_name
1633          FROM wsh_delivery_legs_ob_grp_v wdl,
1634               wsh_trip_stops_ob_grp_v wts,
1635               wsh_trips_ob_grp_v wt
1636         WHERE wdl.delivery_id=l_deliveries.delivery_id
1637         AND   wdl.pick_up_stop_id=wts.stop_id
1638         AND   wt.trip_id=wts.trip_id
1639         AND  ROWNUM=1;
1640         IF l_trip_id IS NOT NULL THEN
1641             UPDATE wms_shipping_transaction_temp
1642             SET trip_id=l_trip_id,trip_name=l_trip_name
1643             WHERE delivery_id=l_deliveries.delivery_id AND trip_id IS NULL;
1644         END IF;
1645        EXCEPTION
1646         WHEN NO_DATA_FOUND THEN
1647         debug('trip_id update on wstt failed','CONFIRM_ALL_DELIVERIES');
1648          END;
1649     END LOOP;
1650     EXCEPTION
1651         WHEN OTHERS THEN
1652          debug('trip_id update on wstt failed','CONFIRM_ALL_DELIVERIES');
1653     END;
1654 
1655 
1656   IF (p_delivery_id IS NOT NULL) THEN
1657      IF (l_debug = 1) THEN
1658         debug('P_Delivery_Id is not null ','Confirm_All_Deliveries');
1659      END IF;
1660      WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM(
1661                   x_return_status   =>l_return_status
1662                   ,x_msg_count       =>l_msg_count
1663                   ,x_msg_data        =>l_msg_data
1664                   ,x_missing_item_cur=>l_t_genref
1665                   ,x_error_code      =>l_error_code
1666                   ,p_delivery_id     =>p_delivery_id
1667                   ,p_org_id          =>p_org_id
1668                   ,p_dock_door_id    =>p_dock_door_id
1669                   );
1670       x_missing_item_cur := l_t_genref;
1671       x_error_code       := l_error_code;
1672 
1673      IF (l_debug = 1) THEN
1674         debug('Return Status - Error_code from Ship_Confirm 1: '||l_return_status
1675               ||' - '||l_error_code, ' Confirm_All_Deliveries');
1676      END IF;
1677      IF l_return_status = fnd_api.g_ret_sts_error THEN
1678                 IF (l_debug = 1) THEN
1679                    debug('Ship Confirm API failed with status E ','Confirm_All_deliveries');
1680                 END IF;
1681                 RAISE fnd_api.g_exc_error;
1682      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1683                 IF (l_debug = 1) THEN
1684                    debug('Ship Confirm API failed with status U','Confirm_All_Deliveries');
1685                 END IF;
1686                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
1687      END IF;
1688   END IF;
1689   IF ((p_org_id IS NOT NULL) AND (p_dock_door_id IS NOT NULL)) THEN
1690      IF (l_debug = 1) THEN
1691         debug('Confirm All Deliveries: Org id and Dock Door is not null loop ' ||
1692               'loaded deliveries','Confirm_All_Deliveries');
1693      END IF;
1694    OPEN loaded_deliveries;
1695    LOOP
1696      FETCH loaded_deliveries into l_delivery_id;
1697      EXIT WHEN loaded_deliveries%NOTFOUND;
1698      IF (l_debug = 1) THEN
1699         debug('Confirm All Deliveries: Delivery Id '||to_char(l_delivery_id),'Confirm_All_Deliveries');
1700      END IF;
1701      WMS_DIRECT_SHIP_PVT.SHIP_CONFIRM(
1702            x_return_status   =>l_return_status
1703            ,x_msg_count       =>l_msg_count
1704            ,x_msg_data       =>l_msg_data
1705            ,x_missing_item_cur=>l_t_genref
1706            ,x_error_code      =>l_error_code
1707            ,p_delivery_id     =>l_delivery_id
1708            ,p_org_id         =>p_org_id
1709            ,p_dock_door_id    =>p_dock_door_id
1710            );
1711       x_missing_item_cur := l_t_genref;
1712       x_error_code       := l_error_code;
1713       IF (l_debug = 1) THEN
1714          debug('Return Status - Error_code from Ship_Confirm 2: '||l_return_status
1715                ||' - '||l_error_code, ' Confirm_All_Deliveries');
1716       END IF;
1717      IF l_return_status = fnd_api.g_ret_sts_error THEN
1718         IF (l_debug = 1) THEN
1719            debug('Ship Confirm API failed with status E ','Confirm_All_deliveries');
1720         END IF;
1721         RAISE fnd_api.g_exc_error;
1722      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1723         IF (l_debug = 1) THEN
1724            debug('Ship Confirm API failed with status U','Confirm_All_Deliveries');
1725         END IF;
1726         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1727      END IF;
1728    END LOOP;
1729    close loaded_deliveries;
1730 END IF;
1731 
1732 EXCEPTION
1733    WHEN fnd_api.g_exc_error THEN
1734       x_return_status := fnd_api.g_ret_sts_error;
1735       IF loaded_deliveries%isopen THEN
1736          CLOSE loaded_deliveries;
1737       END IF ;
1738       IF (l_debug = 1) THEN
1739          debug('In Exception (expected error) - E ','Confirm_All_Deliveries');
1740       END IF;
1741          fnd_msg_pub.count_and_get
1742             ( p_count => x_msg_count,
1743               p_data  => x_msg_data
1744               );
1745    WHEN fnd_api.g_exc_unexpected_error THEN
1746       x_return_status := fnd_api.g_ret_sts_unexp_error;
1747       IF loaded_deliveries%isopen THEN
1748          CLOSE loaded_deliveries;
1749       END IF ;
1750       IF (l_debug = 1) THEN
1751          debug('In Exception (unexpected error) - U ','Confirm_All_Deliveries');
1752       END IF;
1753          fnd_msg_pub.count_and_get
1754             ( p_count => x_msg_count,
1755               p_data  => x_msg_data
1756               );
1757    WHEN OTHERS THEN
1758       x_return_status := fnd_api.g_ret_sts_unexp_error;
1759       IF loaded_deliveries%isopen THEN
1760          CLOSE loaded_deliveries;
1761       END IF ;
1762       IF (l_debug = 1) THEN
1763          debug('In Exception (others)' ||SQLERRM,'Confirm_All_Deliveries');
1764       END IF;
1765          fnd_msg_pub.count_and_get
1766             ( p_count => x_msg_count,
1767               p_data  => x_msg_data
1768               );
1769 
1770 END; -- CONFIRM_DELIVERY
1771 
1772 PROCEDURE CREATE_TRIP(x_return_status    OUT NOCOPY VARCHAR2,
1773                       p_organization_id  IN  NUMBER,
1774                       p_dock_door_id     IN  NUMBER,
1775                       p_delivery_id      IN NUMBER,
1776                       p_direct_ship_flag IN  VARCHAR2 DEFAULT 'N'
1777                       ) IS
1778 l_del_tab            WSH_UTIL_CORE.ID_TAB_TYPE;
1779 l_auto_trip_del      WSH_UTIL_CORE.ID_TAB_TYPE;
1780 l_auto_trip_index    NUMBER:=1;
1781 --l_delivery_id        NUMBER;
1782 l_return_Status      VARCHAR2(1);
1783 l_del_index          NUMBER;
1784 l_del_count          NUMBER;
1785 l_trip_id                NUMBER;
1786 l_chk_trip_id        NUMBER;
1787 l_trip_name          VARCHAR2(30);
1788 l_msg_count          NUMBER;
1789 l_msg_data           VARCHAR2(20000);
1790 l_out_trip_id        VARCHAR2(30);
1791 l_out_trip_name      VARCHAR2(30);
1792 
1793     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1794 BEGIN
1795    x_return_status:= fnd_api.g_ret_sts_success;
1796    wms_globals.g_ship_confirm_method := 'DIRECT';
1797    IF (l_debug = 1) THEN
1798       debug('Begin Create Trip ','CREATE_TRIP');
1799       debug('p_organization_id  :  ' || p_organization_id,'CREATE_TRIP');
1800       debug('p_dock_door_id     :  ' || p_dock_door_id,'CREATE_TRIP');
1801       debug('p_delivery_id      :  ' || p_delivery_id,'CREATE_TRIP');
1802       debug('p_direct_ship_flag :  ' || p_direct_ship_flag,'CREATE_TRIP');
1803    END IF;
1804    l_del_index := 1;
1805       -- Add the following code in case user created Trip manually from desktop
1806       BEGIN
1807          SELECT wts.trip_id
1808          INTO l_trip_id
1809          FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
1810          WHERE wdl.delivery_id = p_delivery_id --l_delivery_id /* bug 2741857 */
1811            and wdl.pick_up_stop_id = wts.stop_id;
1812 
1813          IF l_trip_id IS NOT NULL THEN
1814            UPDATE wms_shipping_transaction_temp
1815             SET    trip_id = l_trip_id,
1816                    last_update_date =  SYSDATE,
1817                    last_updated_by  =  FND_GLOBAL.USER_ID
1818             WHERE  delivery_id = p_delivery_id;--l_delivery_id; /* bug 2741857 */
1819          END IF;
1820       EXCEPTION
1821          WHEN NO_DATA_FOUND THEN
1822          IF (l_debug = 1) THEN
1823             debug('Exception part: '||p_delivery_id,'CREATE_TRIP');
1824          END IF;
1825             l_trip_id := null;
1826             l_del_tab(l_del_index) := p_delivery_id; --l_delivery_id; /* bug 2741857 */
1827             l_del_index := l_del_index + 1;
1828       END;
1829 IF (l_debug = 1) THEN
1830    debug('End Loop thru all delivery loaded on the dock '||l_del_tab.count,'CREATE_TRIP');
1831 END IF;
1832    -- Commit the update of delivery with proper trip ids
1833   BEGIN
1834      SELECT wt.trip_id
1835      INTO l_chk_trip_id
1836      FROM WMS_SHIPPING_TRANSACTION_TEMP wstt
1837          ,WSH_TRIPS_OB_GRP_V wt
1838      WHERE
1839           wt.trip_id = wstt.trip_id
1840       AND  organization_id    = p_organization_id
1841       AND status_code         = 'OP'
1842       AND dock_door_id       = p_dock_door_id
1843       AND dock_appoint_flag  = 'N'
1844       AND nvl(direct_ship_flag,'N') = 'Y'
1845       AND ROWNUM =1;
1846   EXCEPTION
1847    WHEN NO_DATA_FOUND THEN
1848       l_chk_trip_id := NULL;
1849   END;
1850 
1851   IF l_chk_trip_id IS NOT NULL AND (l_del_tab.COUNT > 0) THEN
1852       FOR i IN 1..l_del_tab.count LOOP
1853           IF (l_debug = 1) THEN
1854              debug('The value of l_chk_trip_id is '||l_chk_trip_id,'create_trip');
1855           END IF;
1856          WSH_DELIVERIES_PUB.Delivery_Action
1857            ( p_api_version_number     => 1.0,
1858              p_init_msg_list          => FND_API.G_TRUE,
1859              x_return_status          => l_return_status,
1860              x_msg_count              => l_msg_count,
1861              x_msg_data               => l_msg_data,
1862              p_action_code            => 'ASSIGN-TRIP',
1863              p_delivery_id            => l_del_tab(i),
1864              p_asg_trip_id            => l_chk_trip_id ,
1865              x_trip_id                => l_out_trip_id,
1866              X_TRIP_NAME              => l_out_trip_name );
1867          IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
1868             IF (l_debug = 1) THEN
1869                debug('UPDATE WSTT with the trip_id assigned','CREATE_TRIP');
1870             END IF;
1871                UPDATE WMS_SHIPPING_TRANSACTION_TEMP
1872                SET trip_id   = l_chk_trip_id
1873                WHERE delivery_id = l_del_tab(i);
1874          ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
1875                l_auto_trip_del(l_auto_trip_index) := l_del_tab(i);
1876                l_auto_trip_index := l_auto_trip_index +1;
1877          ELSIF  l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1878             IF (l_debug = 1) THEN
1879                debug('Return unexpected error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP',
1880                      'WMS_DIRECT_SHIP_PVT.Create_Trip');
1881             END IF;
1882             RAISE fnd_api.g_exc_unexpected_error;
1883          END IF;
1884       END LOOP;
1885   ELSIF l_del_tab.COUNT >0 THEN
1886       IF (l_debug = 1) THEN
1887          debug('No open trip exists.Hence auto creating trip ','create_trip');
1888       END IF;
1889       WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP(p_del_rows      => l_del_tab,
1890                                         x_trip_id       => l_trip_id,
1891                                         x_trip_name     => l_trip_name,
1892                                         x_return_status => l_return_status);
1893       IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
1894          IF (l_debug = 1) THEN
1895             debug('UPDATE WSTT with the trip created','CREATE_TRIP');
1896          END IF;
1897          FOR k in 1..l_del_tab.COUNT LOOP
1898             UPDATE WMS_SHIPPING_TRANSACTION_TEMP
1899             SET trip_id   = l_trip_id
1900             WHERE delivery_id = l_del_tab(k);
1901          END LOOP;
1902       ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
1903          IF (l_debug = 1) THEN
1904             debug('Return error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP','WMS_DIRECT_SHIP_PVT.Create_Trip');
1905          END IF;
1906          RAISE fnd_api.g_exc_error;
1907       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1908          IF (l_debug = 1) THEN
1909             debug('Return unexpected error from WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP',
1910                      'WMS_DIRECT_SHIP_PVT.Create_Trip');
1911          END IF;
1912          RAISE fnd_api.g_exc_unexpected_error;
1913       END IF;
1914   END IF;
1915 
1916   IF l_auto_trip_del.count >0  THEN
1917       IF (l_debug = 1) THEN
1918          debug('Assignment of delivery failed.Hence auto creating trip for the failed deliveries  ',
1919                      'create_trip');
1920       END IF;
1921      WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP(p_del_rows      => l_auto_trip_del,
1922                                         x_trip_id       => l_trip_id,
1923                                         x_trip_name     => l_trip_name,
1924                                         x_return_status => l_return_status);
1925      IF l_return_status IN (fnd_api.g_ret_sts_success) THEN
1926         IF (l_debug = 1) THEN
1927            debug('UPDATE WSTT with the trip for failed assignments','CREATE_TRIP');
1928         END IF;
1929 
1930         FOR j in 1..l_auto_trip_del.count LOOP
1931            UPDATE WMS_SHIPPING_TRANSACTION_TEMP
1932            SET trip_id   = l_trip_id
1933            WHERE delivery_id = l_auto_trip_del(j);
1934         END LOOP;
1935 
1936       ELSIF l_return_status IN (fnd_api.g_ret_sts_error) THEN
1937          IF (l_debug = 1) THEN
1938             debug('The API WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP returned status E ','WMS_DIRECT_SHIP_PVT.Create_Trip');
1939          END IF;
1940          RAISE fnd_api.g_exc_error;
1941       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1942          IF (l_debug = 1) THEN
1943             debug('The API WSH_TRIPS_ACTIONS.AUTOCREATE_TRIP returned status U ','WMS_DIRECT_SHIP_PVT.Create_Trip');
1944          END IF;
1945          RAISE fnd_api.g_exc_unexpected_error;
1946       END IF;
1947   END IF;
1948 
1949   wms_globals.g_ship_confirm_method := null;
1950 EXCEPTION
1951    WHEN fnd_api.g_exc_error THEN
1952       x_return_status := fnd_api.g_ret_sts_error;
1953       wms_globals.g_ship_confirm_method := NULL;
1954       IF (l_debug = 1) THEN
1955          debug('In Exception (expected error) - E ','CREATE_TRIP');
1956       END IF;
1957          fnd_msg_pub.count_and_get
1958             ( p_count => l_msg_count,
1959               p_data  => l_msg_data
1960               );
1961    WHEN fnd_api.g_exc_unexpected_error THEN
1962       x_return_status := fnd_api.g_ret_sts_unexp_error;
1963       wms_globals.g_ship_confirm_method := NULL;
1964       IF (l_debug = 1) THEN
1965          debug('In Exception (unexpected error) - U ','Create_Trip');
1966       END IF;
1967          fnd_msg_pub.count_and_get
1968             ( p_count => l_msg_count,
1969               p_data  => l_msg_data
1970               );
1971    WHEN others THEN
1972       x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
1973       wms_globals.g_ship_confirm_method := NULL;
1974       IF (l_debug = 1) THEN
1975      debug('Exception When Others'||SQLERRM,'WMS_DIRECT_SHIP.Create_Trips');
1976       END IF;
1977 END CREATE_TRIP; -- END CREATE_TRIP
1978 
1979 PROCEDURE UPDATE_TRIPSTOP(
1980               x_return_status        OUT NOCOPY VARCHAR2
1981               ,x_msg_count            OUT NOCOPY NUMBER
1982               ,x_msg_data             OUT NOCOPY VARCHAR2
1983               ,p_trip_id              IN  NUMBER
1984               ,p_vehicle_item_id      IN  NUMBER
1985               ,p_vehicle_num_prefix   IN  VARCHAR2
1986               ,p_vehicle_num          IN  VARCHAR2
1987               ,p_seal_code            IN  VARCHAR2
1988               ,p_org_id               IN  NUMBER   DEFAULT NULL
1989               ,p_dock_door_id         IN  NUMBER   DEFAULT NULL
1990               ,p_ship_method_code     IN VARCHAR2 DEFAULT NULL)
1991  IS
1992 
1993 l_init_msg_list                VARCHAR2(1) :=FND_API.G_TRUE;
1994 l_return_status                VARCHAR2(1) :=FND_API.G_RET_STS_SUCCESS;
1995 l_msg_count                    NUMBER;
1996 l_msg_data                     VARCHAR2(20000);
1997 
1998 l_freight_cost_rec             WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
1999 l_stop_info                    WSH_TRIP_STOPS_PUB.Trip_Stop_Pub_Rec_Type;
2000 p_trip_info                    WSH_TRIPS_PUB.Trip_Pub_Rec_Type;
2001 l_freight_cost_id              NUMBER;
2002 l_stop_id                      NUMBER;
2003 l_trip_id                      NUMBER;
2004 l_trip_name                    VARCHAR2(30);
2005 counter                        NUMBER;
2006 l_row_id                       rowid;
2007 l_name                         VARCHAR2(30);
2008 l_vehicle_item_id              NUMBER;
2009 l_org_id                       NUMBER;
2010 l_dock_door_id                 NUMBER;
2011 
2012 CURSOR trip_freight IS
2013    SELECT  ROWID
2014           ,FREIGHT_COST_TYPE_ID
2015           ,CURRENCY_CODE
2016           ,FREIGHT_AMOUNT
2017           ,CONVERSION_TYPE
2018    FROM  WMS_FREIGHT_COST_TEMP
2019    WHERE TRIP_ID = p_trip_id
2020    AND   FREIGHT_COST_ID IS NULL
2021    FOR UPDATE OF FREIGHT_COST_ID NOWAIT;
2022 
2023  CURSOR stops IS
2024  SELECT 'x'
2025  FROM   wsh_trip_stops_ob_grp_v
2026  WHERE  trip_id = p_trip_id
2027  FOR UPDATE  NOWAIT;
2028 
2029  CURSOR trips IS
2030  SELECT 'x'
2031  FROM   wsh_trips_ob_grp_v
2032  WHERE trip_id = p_trip_id
2033  FOR UPDATE NOWAIT;
2034 
2035     l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2036 BEGIN
2037    IF (l_debug = 1) THEN
2038       debug('Begin UPDATE_TRIPSTOP ','UPDATE_TRIPSTOP');
2039       debug('p_trip_id            : ' || p_trip_id              , 'UPDATE_TRIPSTOP');
2040       debug('p_vehicle_item_id    : ' || p_vehicle_item_id      , 'UPDATE_TRIPSTOP');
2041       debug('p_vehicle_num_prefix : ' || p_vehicle_num_prefix   , 'UPDATE_TRIPSTOP');
2042       debug('p_vehicle_num        : ' || p_vehicle_num          , 'UPDATE_TRIPSTOP');
2043       debug('p_seal_code          : ' || p_seal_code            , 'UPDATE_TRIPSTOP');
2044       debug('p_org_id             : ' || p_org_id               , 'UPDATE_TRIPSTOP');
2045       debug('p_dock_door_id       : ' || p_dock_door_id         , 'UPDATE_TRIPSTOP');
2046       debug('p_ship_method_code   : ' || p_ship_method_code     , 'UPDATE_TRIPSTOP');
2047    END IF;
2048    IF p_trip_id <>0 THEN
2049       l_trip_id := p_trip_id;
2050    END IF;
2051    IF p_vehicle_item_id <>0 THEN
2052       l_vehicle_item_id := p_vehicle_item_id;
2053    END IF;
2054    IF p_org_id <> 0 THEN
2055       l_org_id := p_org_id;
2056    END IF;
2057    IF p_dock_door_id <> 0 THEN
2058       l_dock_door_id := p_dock_door_id;
2059    END IF;
2060 
2061    l_return_status := fnd_api.g_ret_sts_success;
2062    IF (l_debug = 1) THEN
2063       debug('In the procedure update trip_stop','trip_Stop');
2064    END IF;
2065 
2066    IF (l_debug = 1) THEN
2067      debug('Update Trip Ship method:'||p_ship_method_code,'Update_tripstop');
2068   END IF;
2069 
2070 -- Update_TripStop will be called from the save button of the Trip-Stop info page.
2071 -- It should update the data entered in the page and also update the freight cost entered
2072 -- if any.
2073 -- When updating the frieght cost info ensure that freight is not entered before.
2074 
2075 -- Check if there is any frieght cost info entered in the page, update it for trip else --create a new
2076 --IF ( p_trip_id IS NULL ) THEN
2077    IF (l_trip_id IS NULL) THEN
2078 
2079   IF (l_debug = 1) THEN
2080      debug('Update Trip No Trip Id is passed','Update_tripstop');
2081   END IF;
2082   RETURN;
2083 ELSE
2084    BEGIN
2085       SELECT name
2086          INTO l_name
2087          FROM wsh_trips_ob_grp_v
2088        WHERE trip_id=l_trip_id;
2089    EXCEPTION
2090       WHEN no_data_found THEN
2091          RETURN;
2092    END;
2093 END IF;
2094 OPEN trip_freight;
2095 LOOP
2096   FETCH trip_freight
2097    into l_row_id
2098         ,l_freight_cost_rec.FREIGHT_COST_TYPE_ID
2099         ,l_freight_cost_rec.CURRENCY_CODE
2100         ,l_freight_cost_rec.UNIT_AMOUNT
2101         ,l_freight_cost_rec.CONVERSION_TYPE_CODE;
2102 
2103        --l_freight_cost_rec.TRIP_ID     :=p_trip_id;
2104     l_freight_cost_rec.TRIP_ID:=l_trip_id;
2105     l_freight_cost_rec.action_code := 'CREATE';
2106   EXIT WHEN trip_freight%NOTFOUND;
2107 
2108   --patchst J.  Shipping API cleanup
2109   WSH_FREIGHT_COSTS_PUB.Create_Update_Freight_Costs
2110     (p_api_version_number     =>1.0
2111      , p_init_msg_list          =>l_init_msg_list
2112      , p_commit                 => FND_API.G_FALSE
2113      , x_return_status          => l_return_status
2114      , x_msg_count                      => l_msg_count
2115      , x_msg_data               => l_msg_DATA
2116      , p_pub_freight_costs      => l_freight_cost_rec
2117      , p_action_code            => 'CREATE'
2118      , x_freight_cost_id        => l_freight_cost_id
2119      );
2120   IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2121      FND_MESSAGE.SET_NAME('WMS','WMS_CREATE_FREIGHT_FAIL');
2122      FND_MESSAGE.SET_TOKEN('OBJ','Trip');
2123      FND_MESSAGE.SET_TOKEN('VAL',l_name);
2124      /* Creation of freight cost for <OBJ> <VAL> has failed  */
2125 
2126      FND_MSG_PUB.ADD;
2127      IF (l_debug = 1) THEN
2128     debug('Create_Update Freight Cost API failed with status E ','Update Trip Stop');
2129      END IF;
2130      RAISE fnd_api.g_exc_error;
2131    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2132      FND_MESSAGE.SET_NAME('WMS','CREATE_FREIGHT_FAIL');
2133      FND_MSG_PUB.ADD;
2134      IF (l_debug = 1) THEN
2135     debug('Update Freight Cost failed with status U','Update TripStop');
2136      END IF;
2137      raise FND_API.G_EXC_UNEXPECTED_ERROR;
2138   END IF;
2139   --\Shipping API cleanup
2140 
2141   UPDATE WMS_FREIGHT_COST_TEMP
2142   SET freight_cost_id   = l_freight_cost_id
2143       ,last_update_date  =  SYSDATE
2144       ,last_updated_by   =  FND_GLOBAL.USER_ID
2145   WHERE rowid = l_row_id;
2146 
2147 END LOOP;
2148 CLOSE trip_freight;
2149 
2150 -- Freight has been updated/ created. Now Update the Trip Stop info which is being entered by user.
2151 -- Seal Code is store in Trip- Stop so we need to updated it. Rest of the info are stored in Trips
2152 
2153 
2154          SELECT stop_id
2155          INTO   l_stop_id
2156          FROM   WSH_TRIP_STOPS_OB_GRP_V wts, WSH_DELIVERY_LEGS_OB_GRP_V wdl
2157         -- WHERE  wts.trip_id = p_trip_id
2158             WHERE wts.trip_id=l_trip_id
2159          AND    wts.stop_id = wdl.pick_up_stop_id
2160        AND ROWNUM <2;
2161 
2162 
2163 
2164    IF (p_seal_code IS NOT NULL ) THEN
2165       --patchset J.  Shipping API cleanup
2166       IF (l_debug = 1) THEN
2167          debug('Call WSH_TRIP_STOPS_PUB.Create_Update_Stop to Update the Seal Code','Update Trip Stop');
2168       END IF;
2169       l_stop_info.stop_id              := l_stop_id;
2170       l_stop_info.trip_id              :=l_trip_id;
2171       l_stop_info.DEPARTURE_SEAL_CODE  := p_seal_code;
2172       WSH_TRIP_STOPS_PUB.Create_Update_Stop(
2173                          p_api_version_number           => 1.0
2174                         ,p_init_msg_list            => l_init_msg_list
2175                         ,x_return_status            => l_return_status
2176                         ,x_msg_count                => l_msg_count
2177                         ,x_msg_data                 => l_msg_data
2178                         ,p_action_code              => 'UPDATE'
2179                         ,p_stop_info                => l_stop_info
2180                         ,p_trip_id                  => l_trip_id
2181                         ,x_stop_id                  => l_stop_id);
2182 
2183          IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2184             FND_MESSAGE.SET_NAME('WMS','WMS_UPD_STOP_FAIL');
2185             FND_MESSAGE.SET_TOKEN('TRIP_NAME',l_name);
2186           -- Failed to Create/update Trip Stop for the trip <TRIP_NAME>
2187             fnd_msg_pub.ADD;
2188             IF (l_debug = 1) THEN
2189                debug('Create_Update_Stop API failed with status E ','Update Trip Stop');
2190             END IF;
2191             RAISE fnd_api.g_exc_error;
2192          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2193             FND_MESSAGE.SET_NAME('WMS','WMS_UPD_STOP_FAIL');
2194             FND_MESSAGE.SET_TOKEN('TRIP_NAME',l_name);
2195           -- Failed to Create/update Trip Stop for the trip <TRIP_NAME>
2196             fnd_msg_pub.ADD;
2197             IF (l_debug = 1) THEN
2198                debug('Create_Update_Stop failed with status U','Update TripStop');
2199             END IF;
2200             raise FND_API.G_EXC_UNEXPECTED_ERROR;
2201          END IF;
2202      --\Shipping API cleanup
2203   END IF; -- Update Stop
2204 
2205  -- After Updating the Stop with Seal code, Update the Trip with Vehicle_item_id etc.
2206 
2207    IF (l_debug = 1) THEN
2208        debug('Update Trip also ','Update_Trip_Stop');
2209     END IF;
2210   p_trip_info.trip_id                                    :=l_trip_id;
2211  p_trip_info.VEHICLE_ITEM_ID         :=l_vehicle_item_id;
2212  p_trip_info.vehicle_organization_id  :=l_org_id;
2213  p_trip_info.VEHICLE_NUMBER                      := p_vehicle_num;
2214  p_trip_info.VEHICLE_NUM_PREFIX                  := p_vehicle_num_prefix;
2215  p_trip_info.last_update_date           := SYSDATE;
2216  p_trip_info.last_updated_by            :=FND_GLOBAL.USER_ID;
2217  p_trip_info.last_update_login          := FND_GLOBAL.USER_ID;
2218  p_trip_info.ship_method_code           :=p_ship_method_code;    --Bug 2980013:Updating the Ship Method Code*/
2219 
2220  IF (l_debug = 1) THEN
2221     debug('Call to Create_update_Trip','Update_Trip_Stop');
2222  END IF;
2223 
2224           WSH_TRIPS_PUB.Create_Update_Trip  (
2225                         p_api_version_number  => 1.0
2226                        ,p_init_msg_list       => l_init_msg_list
2227                        ,x_return_status       => l_return_status
2228                        ,x_msg_count           => l_msg_count
2229                        ,x_msg_data            => l_msg_data
2230                        ,p_action_code         => 'UPDATE'
2231                        ,p_trip_info           => p_trip_info
2232                        ,x_trip_id             => l_trip_id
2233                        ,x_trip_name           => l_trip_name);
2234          IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2235             IF (l_debug = 1) THEN
2236                debug('Create_Update_Trip API failed with status E ','Update Trip Stop');
2237             END IF;
2238             RAISE fnd_api.g_exc_error;
2239          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2240             IF (l_debug = 1) THEN
2241                debug('Create_Update_Trip failed with status U','Update TripStop');
2242             END IF;
2243             raise FND_API.G_EXC_UNEXPECTED_ERROR;
2244          END IF;
2245 
2246     -- Update Trip
2247 
2248 COMMIT;
2249 
2250 EXCEPTION
2251    WHEN fnd_api.g_exc_error THEN
2252       x_return_status := fnd_api.g_ret_sts_error;
2253       IF (l_debug = 1) THEN
2254          debug('In exception (E) ','Update_Trip_Stop');
2255       END IF;
2256       ROLLBACK;
2257       IF trip_freight%ISOPEN THEN
2258              CLOSE trip_freight;
2259       END IF;
2260       fnd_msg_pub.count_and_get
2261             ( p_count => x_msg_count,
2262               p_data  => x_msg_data
2263             );
2264 
2265    WHEN fnd_api.g_exc_unexpected_error THEN
2266       x_return_status := fnd_api.g_ret_sts_unexp_error;
2267       IF (l_debug = 1) THEN
2268          debug('Update of Trip Stop has failed :Unexpected Error','Update_Trip_Stop');
2269       END IF;
2270       ROLLBACK;
2271       IF trip_freight%ISOPEN THEN
2272              CLOSE trip_freight;
2273       END IF;
2274       fnd_msg_pub.count_and_get
2275             ( p_count => x_msg_count,
2276               p_data  => x_msg_data
2277             );
2278 
2279    WHEN OTHERS THEN
2280       ROLLBACK;
2281       x_return_status := fnd_api.g_ret_sts_unexp_error ;
2282       IF (l_debug = 1) THEN
2283          debug('Update of Trip has failed : Unexpected Error '||SQLERRM,'Update_trip_Stop');
2284       END IF;
2285       IF trip_freight%ISOPEN THEN
2286              CLOSE trip_freight;
2287       END IF;
2288           IF SQLCODE IS NOT NULL THEN
2289             IF (l_debug = 1) THEN
2290                debug(SQLCODE,'Update_Trip_Stop');
2291             END IF;
2292           END IF;
2293 
2294 END;
2295 
2296 PROCEDURE print_shipping_document(
2297   x_return_status      OUT NOCOPY    VARCHAR2
2298 , x_msg_count          OUT NOCOPY    NUMBER
2299 , x_msg_data           OUT NOCOPY    VARCHAR2
2300 , p_trip_id            IN            NUMBER
2301 , p_vehicle_item_id    IN            NUMBER
2302 , p_vehicle_num_prefix IN            VARCHAR2
2303 , p_vehicle_num        IN            VARCHAR2
2304 , p_seal_code          IN            VARCHAR2
2305 , p_document_set_id    IN            NUMBER
2306 , p_org_id             IN            NUMBER DEFAULT NULL
2307 , p_dock_door_id       IN            NUMBER DEFAULT NULL
2308 , p_ship_method_code   IN            VARCHAR2 DEFAULT NULL
2309 ) IS
2310   CURSOR stop_cur IS
2311     SELECT wdl.pick_up_stop_id pick_up_stop_id
2312       FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
2313      WHERE wts.trip_id = p_trip_id
2314        AND wdl.pick_up_stop_id = wts.stop_id;
2315 
2316   CURSOR del_cur IS
2317     SELECT delivery_id
2318       FROM wsh_delivery_legs_ob_grp_v wdl, wsh_trip_stops_ob_grp_v wts
2319      WHERE wts.trip_id = p_trip_id
2320        AND wdl.pick_up_stop_id = wts.stop_id;
2321 
2322   l_del_cur             del_cur%ROWTYPE;
2323   l_stop_cur            stop_cur%ROWTYPE;
2324   l_init_msg_list       VARCHAR2(1)                             := fnd_api.g_true;
2325   l_return_status       VARCHAR2(1)                             := fnd_api.g_ret_sts_success;
2326   l_msg_count           NUMBER;
2327   l_msg_data            VARCHAR2(20000);
2328   l_report_set_id       NUMBER;
2329   l_trip_id             wsh_util_core.id_tab_type;
2330   l_stop_id             wsh_util_core.id_tab_type;
2331   l_delivery_id         wsh_util_core.id_tab_type;
2332   l_document_param_info wsh_document_sets.document_set_tab_type;
2333   l_count1              NUMBER;
2334   l_count2              NUMBER;
2335   l_dummy               VARCHAR2(20000);
2336   l_num                 NUMBER;
2337   l_debug               NUMBER                                  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2338 
2339 BEGIN
2340   x_return_status  := fnd_api.g_ret_sts_success;
2341 
2342   IF (l_debug = 1) THEN
2343       debug('Begin print_shipping_document ','print_shipping_document');
2344       debug('p_trip_id            : ' || p_trip_id              , 'print_shipping_document');
2345       debug('p_vehicle_item_id    : ' || p_vehicle_item_id      , 'print_shipping_document');
2346       debug('p_vehicle_num_prefix : ' || p_vehicle_num_prefix   , 'print_shipping_document');
2347       debug('p_vehicle_num        : ' || p_vehicle_num          , 'print_shipping_document');
2348       debug('p_seal_code          : ' || p_seal_code            , 'print_shipping_document');
2349       debug('p_document_set_id    : ' || p_document_set_id      , 'print_shipping_document');
2350       debug('p_org_id             : ' || p_org_id               , 'print_shipping_document');
2351       debug('p_dock_door_id       : ' || p_dock_door_id         , 'print_shipping_document');
2352       debug('p_ship_method_code   : ' || p_ship_method_code     , 'print_shipping_document');
2353   END IF;
2354 
2355   wms_direct_ship_pvt.update_tripstop(
2356     x_return_status              => l_return_status
2357   , x_msg_count                  => l_msg_count
2358   , x_msg_data                   => l_msg_data
2359   , p_trip_id                    => p_trip_id
2360   , p_vehicle_item_id            => p_vehicle_item_id
2361   , p_vehicle_num_prefix         => p_vehicle_num_prefix
2362   , p_vehicle_num                => p_vehicle_num
2363   , p_seal_code                  => p_seal_code
2364   , p_org_id                     => p_org_id
2365   , p_dock_door_id               => p_dock_door_id
2366   , p_ship_method_code           => p_ship_method_code
2367   );
2368 
2369   IF l_return_status = fnd_api.g_ret_sts_error THEN
2370     IF (l_debug = 1) THEN
2371       DEBUG('Update tripstop API failed with status E ', 'Print_shipping_dcouments');
2372     END IF;
2373 
2374     RAISE fnd_api.g_exc_error;
2375   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2376     IF (l_debug = 1) THEN
2377       DEBUG('Update tripstop API failed with status U ', 'Print_shipping_dcouments');
2378     END IF;
2379 
2380     RAISE fnd_api.g_exc_unexpected_error;
2381   END IF;
2382 
2383   IF p_document_set_id = 0 THEN
2384     l_report_set_id  := NULL;
2385   ELSE
2386     l_report_set_id  := p_document_set_id;
2387   END IF;
2388 
2389   IF (l_report_set_id IS NULL) THEN
2390      BEGIN
2391       SELECT delivery_report_set_id
2392         INTO l_report_set_id
2393         FROM wsh_shipping_parameters
2394        WHERE organization_id = p_org_id
2395          AND ROWNUM < 2;
2396     EXCEPTION
2397       WHEN NO_DATA_FOUND THEN
2398         l_report_set_id  := NULL;
2399     -- Return an error message that says no default Document is defined for the org
2400     --- So Document printing will not happen
2401     END;
2402   END IF;
2403 
2404   IF (l_report_set_id IS NOT NULL) THEN
2405     OPEN del_cur;
2406 
2407     LOOP
2408       FETCH del_cur INTO l_del_cur;
2409       EXIT WHEN del_cur%NOTFOUND;
2410       l_delivery_id(del_cur%ROWCOUNT)    := l_del_cur.delivery_id;
2411     END LOOP;
2412 
2413     CLOSE del_cur;
2414 
2415     IF (l_debug = 1) THEN
2416       DEBUG('Call WSH_DOCUMENT_SETS.Print_Document_Sets  Document Set Id ' ||
2417              l_report_set_id, 'Print_Shipping_Documents');
2418     END IF;
2419 
2420     l_count1      := 0;
2421     l_count2      := 0;
2422     l_count1      := fnd_msg_pub.count_msg();
2423 
2424     IF (l_debug = 1) THEN
2425       DEBUG('Call WSH_DOCUMENT_SETS.Print_document_Sets for all the deliveries');
2426       DEBUG('Msg table count' || l_count1, 'Print_Shipping_document');
2427     END IF;
2428 
2429     wsh_document_sets.print_document_sets(
2430       p_report_set_id              => l_report_set_id
2431     , p_organization_id            => p_org_id
2432     , p_trip_ids                   => l_trip_id
2433     , p_stop_ids                   => l_stop_id
2434     , p_delivery_ids               => l_delivery_id
2435     , p_document_param_info        => l_document_param_info
2436     , x_return_status              => l_return_status
2437     );
2438     l_count2      := fnd_msg_pub.count_msg();
2439 
2440     IF (l_debug = 1) THEN
2441       DEBUG('Msg table count' || l_count2, 'Print_Shipping_document');
2442     END IF;
2443 
2444     IF l_count2 > l_count1 THEN
2445       l_count1  := l_count1 + 2;
2446 
2447       FOR i IN REVERSE l_count1 .. l_count2 LOOP
2448         l_dummy  := fnd_msg_pub.get(i, fnd_api.g_true);
2449 
2450         IF (l_debug = 1) THEN
2451           DEBUG('Messages are' || l_dummy, 'Print_Shipping_document');
2452         END IF;
2453 
2454         fnd_msg_pub.delete_msg(i);
2455       END LOOP;
2456     END IF;
2457 
2458     IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2459       fnd_message.set_name('WMS', 'WMS_PRINT_DOC_SET_FAIL');
2460       /* Failed to print the documents set */
2461       fnd_msg_pub.ADD;
2462 
2463       IF (l_debug = 1) THEN
2464         DEBUG('Print Document_sets API failed with status E ', 'Print_shipping_dcouments');
2465       END IF;
2466     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2467       fnd_message.set_name('WMS', 'WMS_PRINT_DOC_SET_FAIL');
2468       /* Failed to print the documents set */
2469       fnd_msg_pub.ADD;
2470 
2471       IF (l_debug = 1) THEN
2472         DEBUG('Print Document_sets API failed with status U ', 'Print_shipping_dcouments');
2473       END IF;
2474     END IF;
2475 
2476   END IF; -- Print report set
2477 EXCEPTION
2478   WHEN fnd_api.g_exc_error THEN
2479     x_return_status  := fnd_api.g_ret_sts_error;
2480 
2481     IF (l_debug = 1) THEN
2482       DEBUG('Print Document has failed :Error', 'Print_shipping_dcouments');
2483     END IF;
2484 
2485     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2486     ROLLBACK;
2487   -- Think of some messages here.
2488   WHEN fnd_api.g_exc_unexpected_error THEN
2489     IF (l_debug = 1) THEN
2490       DEBUG('Print Document has failed with status U', 'Print_shipping_dcouments');
2491     END IF;
2492 
2493     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2494     ROLLBACK;
2495     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2496 
2497     -- Think of some messages here.
2498     IF (l_debug = 1) THEN
2499       DEBUG('Print Document has failed :Unexpected Error', 'Print_shipping_dcouments');
2500     END IF;
2501   WHEN OTHERS THEN
2502     ROLLBACK;
2503     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2504 
2505     IF (l_debug = 1) THEN
2506       DEBUG('Print Document has failed : Unexpected Error', 'Print_shipping_dcouments');
2507     END IF;
2508 
2509     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2510 
2511     IF SQLCODE IS NOT NULL THEN
2512       IF (l_debug = 1) THEN
2513         DEBUG(SQLCODE, 'Print_shipping_dcouments');
2514       END IF;
2515     END IF;
2516 END;
2517 
2518   -- PRINT_SHIPPING_DOCUMENT
2519 
2520 PROCEDURE close_trip(
2521   x_return_status      OUT NOCOPY    VARCHAR2
2522 , x_msg_count          OUT NOCOPY    NUMBER
2523 , x_msg_data           OUT NOCOPY    VARCHAR2
2524 , p_trip_id            IN            NUMBER
2525 , p_vehicle_item_id    IN            NUMBER
2526 , p_vehicle_num_prefix IN            VARCHAR2
2527 , p_vehicle_num        IN            VARCHAR2
2528 , p_seal_code          IN            VARCHAR2
2529 , p_document_set_id    IN            NUMBER
2530 , p_org_id             IN            NUMBER DEFAULT NULL
2531 , p_dock_door_id       IN            NUMBER DEFAULT NULL
2532 , p_ship_method_code   IN            VARCHAR2 DEFAULT NULL
2533 ) IS
2534   l_init_msg_list        VARCHAR2(1)               := fnd_api.g_true;
2535   l_return_status        VARCHAR2(1)               := fnd_api.g_ret_sts_success;
2536   l_msg_count            NUMBER;
2537   l_msg_data             VARCHAR2(20000);
2538   l_stop_id              NUMBER;
2539   l_trip_name            VARCHAR2(30);
2540   l_trip_id              NUMBER;
2541   l_outermost_lpn        VARCHAR2(30);
2542   l_loaded_dock_door     VARCHAR2(2000);
2543   l_name                 VARCHAR2(30);
2544   l_delivery_details_tab wsh_util_core.id_tab_type;
2545   l_delivery_id          NUMBER;
2546   l_del_index            NUMBER;
2547   l_enforce_ship_method  VARCHAR2(1);
2548   no_ship_method_code    EXCEPTION;
2549 
2550   CURSOR lpn_in_other_dock(p_trip_id NUMBER) IS
2551     SELECT DISTINCT milk.concatenated_segments
2552                   , wstt.outermost_lpn
2553                FROM mtl_item_locations_kfv milk, wms_shipping_transaction_temp wstt
2554               WHERE wstt.trip_id = p_trip_id
2555                 AND wstt.organization_id = p_org_id
2556                 AND wstt.dock_appoint_flag = 'N'
2557                 AND wstt.direct_ship_flag = 'Y'
2558                 AND wstt.dock_door_id <> p_dock_door_id
2559                 AND milk.organization_id = p_org_id
2560                 AND milk.inventory_location_id = wstt.dock_door_id;
2561 
2562   CURSOR closed_trips IS
2563     SELECT DISTINCT wstt.trip_id
2564                FROM wms_shipping_transaction_temp wstt, wsh_trips_ob_grp_v wt
2565               WHERE wstt.organization_id = p_org_id
2566                 AND wstt.dock_door_id = p_dock_door_id
2567                 AND wt.trip_id = wstt.trip_id
2568                 AND wt.status_code IN('CL', 'IT');
2569 
2570   CURSOR delivery_details IS
2571     SELECT DISTINCT wstt.delivery_id
2572                FROM wms_shipping_transaction_temp wstt
2573               WHERE wstt.organization_id = p_org_id
2574                 AND wstt.dock_door_id = p_dock_door_id
2575                 AND wstt.trip_id = p_trip_id;
2576 
2577   l_debug                NUMBER                    := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2578 BEGIN
2579   IF (l_debug = 1) THEN
2580     DEBUG('p_trip_id            : ' || p_trip_id, 'close_trip');
2581     DEBUG('p_vehicle_item_id    : ' || p_vehicle_item_id, 'close_trip');
2582     DEBUG('p_vehicle_num_prefix : ' || p_vehicle_num_prefix, 'close_trip');
2583     DEBUG('p_vehicle_num        : ' || p_vehicle_num, 'close_trip');
2584     DEBUG('p_seal_code          : ' || p_seal_code, 'close_trip');
2585     DEBUG('p_document_set_id    : ' || p_document_set_id, 'close_trip');
2586     DEBUG('p_org_id             : ' || p_org_id, 'close_trip');
2587     DEBUG('p_dock_door_id       : ' || p_dock_door_id, 'close_trip');
2588     DEBUG('p_ship_method_code   : ' || p_ship_method_code, 'close_trip');
2589   END IF;
2590 
2591   x_return_status  := fnd_api.g_ret_sts_success;
2592   fnd_msg_pub.initialize;
2593 
2594   IF (l_debug = 1) THEN
2595     DEBUG('Begin Close Trip ', 'CLOSE_TRIP');
2596   END IF;
2597 
2598   -- Call Document Printing API which takes care of Updating the delivery and Printing the Doc.
2599   IF (l_debug = 1) THEN
2600     DEBUG('Call Print Document ', 'CLOSE_TRIP');
2601   END IF;
2602 
2603   IF p_trip_id IS NULL
2604      OR p_trip_id = 0 THEN
2605     IF (l_debug = 1) THEN
2606       DEBUG('The value for trip_id passed is zero. Exiting out ', 'close_trip');
2607     END IF;
2608 
2609     RAISE fnd_api.g_exc_error;
2610   ELSE
2611     BEGIN
2612       SELECT NAME
2613         INTO l_name
2614         FROM wsh_trips_ob_grp_v
2615        WHERE trip_id = p_trip_id;
2616     EXCEPTION
2617       WHEN NO_DATA_FOUND THEN
2618         NULL;
2619     END;
2620   END IF;
2621 
2622   IF (l_debug=1) then
2623      DEBUG('Checking Ship Method Validity.', 'Close_Trip');
2624   END IF;
2625   /* Checking if the Ship Method has been entered if the enforce Ship method flag is set to Y*/
2626 
2627   INV_SHIPPING_TRANSACTION_PUB.get_enforce_ship
2628     (p_org_id         => p_org_id
2629      ,x_enforce_ship  => l_enforce_ship_method
2630      ,x_return_status => l_return_status
2631      ,x_msg_data      => l_msg_data
2632      ,x_msg_count     => l_msg_count);
2633 
2634   IF (l_debug=1) THEN
2635      debug('get_enforce_ship returned status: ' || l_return_status,'Close_Trip');
2636      debug('Enforce_ship_method flag: ' || l_enforce_ship_method,'Close_trip');
2637   END IF;
2638 
2639   IF l_enforce_ship_method = 'Y'
2640      AND p_ship_method_code IS NULL THEN
2641     RAISE no_ship_method_code;
2642   END IF;
2643 
2644   wms_direct_ship_pvt.print_shipping_document(
2645     x_return_status              => l_return_status
2646   , x_msg_count                  => l_msg_count
2647   , x_msg_data                   => l_msg_data
2648   , p_trip_id                    => p_trip_id
2649   , p_vehicle_item_id            => p_vehicle_item_id
2650   , p_vehicle_num_prefix         => p_vehicle_num_prefix
2651   , p_vehicle_num                => p_vehicle_num
2652   , p_seal_code                  => p_seal_code
2653   , p_document_set_id            => p_document_set_id
2654   , p_org_id                     => p_org_id
2655   , p_dock_door_id               => p_dock_door_id
2656   , p_ship_method_code           => p_ship_method_code
2657   );
2658 
2659   IF l_return_status = fnd_api.g_ret_sts_error THEN
2660     IF (l_debug = 1) THEN
2661       DEBUG('Print shipping documents API failed with status E ', 'Close_Trip');
2662     END IF;
2663 
2664     RAISE fnd_api.g_exc_error;
2665   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2666     IF (l_debug = 1) THEN
2667       DEBUG('Print shipping documents API failed with status U', 'Close_Trip');
2668     END IF;
2669 
2670     RAISE fnd_api.g_exc_unexpected_error;
2671   END IF;
2672 
2673   -- Check if there is an LPN loaded on a different Dock Door for the Trip. If so throw an error
2674        -- Error code in this case is
2675   BEGIN
2676     IF (l_debug = 1) THEN
2677       DEBUG('Check if there is an LPN loaded on different dock door for the Trip', 'Close_Trip');
2678     END IF;
2679 
2680     OPEN lpn_in_other_dock(p_trip_id);
2681     FETCH lpn_in_other_dock INTO l_loaded_dock_door, l_outermost_lpn;
2682 
2683     IF lpn_in_other_dock%FOUND THEN
2684       SELECT NAME
2685         INTO l_trip_name
2686         FROM wsh_trips_ob_grp_v
2687        WHERE trip_id = p_trip_id;
2688 
2689       -- Raise error and set the message to be shown in Mobile
2690       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2691       /*LPN LPN_NAME assigned to trip TRIP is loaded on another dock door DOCK . Hence close trip failed.*/
2692       fnd_message.set_token('LPN_NAME', l_outermost_lpn);
2693       fnd_message.set_token('TRIP', l_trip_name);
2694       fnd_message.set_token('DOCK', l_loaded_dock_door);
2695       /*Close Trip failed */
2696       fnd_msg_pub.ADD;
2697       x_return_status  := fnd_api.g_ret_sts_error;
2698       CLOSE lpn_in_other_dock;
2699       RETURN;
2700     END IF;
2701 
2702     CLOSE lpn_in_other_dock;
2703 
2704     IF (l_debug = 1) THEN
2705       DEBUG('All LPNs for the Trip are loaded on the same dock door', 'Close_Trip');
2706     END IF;
2707   EXCEPTION
2708     WHEN NO_DATA_FOUND THEN
2709       NULL;
2710   END;
2711 
2712   -- Get the pickup stop for the trip so  that it may be closed.
2713   IF (l_debug = 1) THEN
2714     DEBUG('Close Trip. Get the pickup stop of the trip', 'Close_Trip');
2715   END IF;
2716 
2717   SELECT stop_id
2718     INTO l_stop_id
2719     FROM wsh_trip_stops_ob_grp_v wts, wsh_delivery_legs_ob_grp_v wdl
2720    WHERE wts.trip_id = p_trip_id
2721      AND wts.stop_id = wdl.pick_up_stop_id
2722      AND ROWNUM < 2;
2723 
2724   IF (l_debug = 1) THEN
2725     DEBUG('Interface the Trip now. Trip Id :' || p_trip_id || '  Pick Up Stop Id: ' || l_stop_id, 'Close_Trip');
2726   END IF;
2727 
2728   wsh_trip_stops_pub.stop_action(
2729     p_api_version_number         => 1.0
2730   , p_init_msg_list              => l_init_msg_list
2731   , x_return_status              => l_return_status
2732   , x_msg_count                  => l_msg_count
2733   , x_msg_data                   => l_msg_data
2734   , p_action_code                => 'CLOSE'
2735   , p_stop_id                    => l_stop_id
2736   , p_trip_id                    => p_trip_id
2737   , p_defer_interface_flag       => 'N'
2738   );
2739 
2740   IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
2741     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2742     fnd_message.set_token('TRIP_NAME', l_name);
2743     /*Failed to close the trip TRIP_NAME*/
2744     fnd_msg_pub.ADD;
2745 
2746     IF (l_debug = 1) THEN
2747       DEBUG('Stop_Action API failed with status E ', 'Close_Trip');
2748     END IF;
2749 
2750     RAISE fnd_api.g_exc_error;
2751   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2752     fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2753     fnd_message.set_token('TRIP_NAME', l_name);
2754     /*Failed to close the trip TRIP_NAME*/
2755     fnd_msg_pub.ADD;
2756 
2757     IF (l_debug = 1) THEN
2758       DEBUG('Stop_Action API failed with status U', 'Close_Trip');
2759     END IF;
2760 
2761     RAISE fnd_api.g_exc_unexpected_error;
2762   END IF;
2763 
2764   OPEN delivery_details;
2765   l_del_index      := 1;
2766 
2767   LOOP
2768     FETCH delivery_details INTO l_delivery_id;
2769     EXIT WHEN delivery_details%NOTFOUND;
2770     l_delivery_details_tab(l_del_index)  := l_delivery_id;
2771     l_del_index                          := l_del_index + 1;
2772   END LOOP;
2773 
2774   wms_shipping_transaction_pub.print_label(l_delivery_details_tab, l_return_status);
2775 
2776   IF x_return_status <>(fnd_api.g_ret_sts_success) THEN
2777     fnd_message.set_name('INV', 'INV_RCV_CRT_PRINT_LAB_FAIL');
2778 
2779     IF (l_debug = 1) THEN
2780       DEBUG('Label Printing failed ', 'Close Trip');
2781     END IF;
2782 
2783     fnd_msg_pub.ADD;
2784     l_return_status  := 'W';
2785   END IF;
2786 
2787   -- Clean up trips which are closed
2788   l_trip_id        := p_trip_id;
2789   OPEN closed_trips;
2790 
2791   LOOP
2792     FETCH closed_trips INTO l_trip_id;
2793     EXIT WHEN closed_trips%NOTFOUND;
2794     wms_direct_ship_pvt.cleanup_temp_recs(
2795       x_msg_data                   => x_msg_data
2796     , x_msg_count                  => x_msg_count
2797     , x_return_status              => x_return_status
2798     , p_org_id                     => p_org_id
2799     , p_outermost_lpn_id           => NULL
2800     , p_trip_id                    => l_trip_id
2801     );
2802 
2803     IF l_return_status =(fnd_api.g_ret_sts_error) THEN
2804       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2805       /*Failed while running cleanup program */
2806       fnd_msg_pub.ADD;
2807 
2808       IF (l_debug = 1) THEN
2809         DEBUG('CLEANUP_TEMP_RECS API failed with status E ', 'Close_Trip');
2810       END IF;
2811 
2812       RAISE fnd_api.g_exc_error;
2813     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2814       fnd_message.set_name('WMS', 'WMS_CLOSE_TRIP_FAIL');
2815       /*Failed while running cleanup program */
2816       fnd_msg_pub.ADD;
2817 
2818       IF (l_debug = 1) THEN
2819         DEBUG('CLEANUP_TEMP_RECS API failed with status U', 'Close_Trip');
2820       END IF;
2821 
2822       RAISE fnd_api.g_exc_unexpected_error;
2823     END IF;
2824   END LOOP;
2825 
2826   CLOSE closed_trips;
2827 -- Call clean up API at this time. This API will create
2828 EXCEPTION
2829   WHEN no_ship_method_code THEN
2830     x_return_status  := fnd_api.g_ret_sts_error;
2831     fnd_message.set_name('WMS', 'WMS_SHIP_METHOD_CODE');
2832     fnd_message.set_token('TRIP_NAME', l_name);
2833     /* No Ship method code provided for the Trip .This is required */
2834     fnd_msg_pub.ADD;
2835     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2836     DEBUG('In exception no_ship_method_code - - errorcode ', 'Close Trip');
2837   WHEN fnd_api.g_exc_error THEN
2838     x_return_status  := fnd_api.g_ret_sts_error;
2839     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2840     ROLLBACK;
2841 
2842     -- Think of some messages here.
2843     IF (l_debug = 1) THEN
2844       DEBUG('Close Trip has failed :Error', 'Close_Trip');
2845     END IF;
2846   WHEN fnd_api.g_exc_unexpected_error THEN
2847     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2848     ROLLBACK;
2849 
2850     -- Think of some messages here.
2851     IF (l_debug = 1) THEN
2852       DEBUG('Close Trip has failed :Unexpected Error', 'Close_Trip');
2853     END IF;
2854 
2855     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2856   WHEN OTHERS THEN
2857     ROLLBACK;
2858     x_return_status  := fnd_api.g_ret_sts_unexp_error;
2859 
2860     IF (l_debug = 1) THEN
2861       DEBUG('Close Trip has failed : Unexpected Error', 'Close_Trip');
2862     END IF;
2863 
2864     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2865 
2866     IF SQLCODE IS NOT NULL THEN
2867       IF (l_debug = 1) THEN
2868         DEBUG(SQLCODE, 'Close_Trip');
2869       END IF;
2870     END IF;
2871 END;
2872 
2873   --CLOSE_TRIP
2874 
2875 -- API Name         :
2876 -- Type             : Procedure
2877 -- Function         : This procedure does the following:
2878 --                    1.Change LPN context to "Resides in Inventory"
2879 --                    2.Unpack the LPN in Shipping
2880 --                    3.Remove inventory details from Shipping (Sub, Loc, Qty).
2881 --                    4.Remove the Reservation records
2882 --                    5.Reset the serial_number_control_code,current_status
2883 --                     ->if serial control code is "At SO Issue", reset current status to "Defined but not used"
2884 --                     ->if serial control code is "Predefined" or "At Receipt" reset status to "resides in stores"
2885 --                     ->Reset Group Mark Id
2886 -- Input Parameters :
2887 --   p_org_id             Organization Id
2888 --   p_outermost_lpn_id   Outermost LPN Id
2889 -- Output Parameters    :
2890 --   x_return_status      Standard Output Parameter
2891 --   x_msg_count          Standard Output Parameter
2892 --   x_msg_data           Standard Output Parameter
2893 
2894 PROCEDURE unload_truck(
2895   x_return_status    OUT NOCOPY    VARCHAR2
2896 , x_msg_count        OUT NOCOPY    NUMBER
2897 , x_msg_data         OUT NOCOPY    VARCHAR2
2898 , p_org_id           IN            NUMBER
2899 , p_outermost_lpn_id IN            NUMBER
2900 , p_relieve_rsv      IN            VARCHAR2 DEFAULT 'Y'/*added 3396821*/
2901 ) IS
2902   CURSOR wda_del_detail(l_outermost_dd_id NUMBER) IS
2903     SELECT     delivery_detail_id
2904           FROM wsh_delivery_assignments_v
2905     START WITH delivery_detail_id = l_outermost_dd_id
2906     CONNECT BY PRIOR delivery_detail_id = parent_delivery_detail_id;
2907 
2908   CURSOR wdd_del_detail(p_delivery_detail_id IN NUMBER) IS
2909     SELECT   wdd.delivery_detail_id
2910            , wdd.inventory_item_id
2911            , wdd.serial_number
2912            , wdd.source_line_id
2913            , wdd.requested_quantity
2914            , wdd.transaction_temp_id
2915         FROM wsh_delivery_details_ob_grp_v wdd
2916        WHERE wdd.organization_id = p_org_id
2917          AND wdd.container_flag <> 'Y'
2918          AND wdd.delivery_detail_id = p_delivery_detail_id
2919     ORDER BY wdd.source_line_id;
2920 
2921   CURSOR del_lpn IS
2922      SELECT lpn_id
2923      FROM wms_license_plate_numbers wlpn
2924      WHERE wlpn.outermost_lpn_id = p_outermost_lpn_id;
2925 
2926   l_del_lpn             del_lpn%ROWTYPE;
2927   l_del_det_id          NUMBER;
2928   l_status_code         VARCHAR2(2);
2929   l_init_msg_list       VARCHAR2(1)                             := fnd_api.g_true;
2930   l_del_id              NUMBER;
2931   l_trip_id             NUMBER;
2932   l_trip_name           VARCHAR2(30);
2933   l_delivery_detail_id  NUMBER;
2934   counter               NUMBER;
2935   x_error_code          NUMBER;
2936   l_return_status       VARCHAR2(1);
2937   l_msg_count           NUMBER;
2938   l_msg_data            VARCHAR2(20000);
2939   l_serial_ctrl_code    NUMBER                                  := 1;
2940   l_item_id             NUMBER;
2941   l_source_line_id      NUMBER;
2942   l_prev_src_line_id    NUMBER;
2943   l_min_del_detail_id   NUMBER                                  := 0;
2944   l_sum_req_qty         NUMBER                                  := 0;
2945   l_serial_number       mtl_serial_numbers.serial_number%TYPE;
2946   l_outermost_dd_id     NUMBER;
2947   l_req_qty             NUMBER;
2948   l_transaction_temp_id NUMBER;
2949 
2950   /* Mrana: 7/6/06: 5350778 : Added foll. variables */
2951   l_lot_ctrl_code       NUMBER                                  := 1;
2952   l_lot_divisible_flag  VARCHAR2(1)                             := NULL;
2953   l_relieve_rsv         VARCHAR2(1)                             := NULL;
2954 
2955   TYPE delivery_detail_r IS RECORD(
2956     delivery_detail_id NUMBER
2957   , source_line_id     NUMBER
2958   );
2959 
2960   TYPE delivery_detail_t IS TABLE OF delivery_detail_r
2961     INDEX BY BINARY_INTEGER;
2962 
2963   l_delivery_detail_tab delivery_detail_t;
2964 
2965   l_lpn_del_det_tab wsh_util_core.id_tab_type;
2966 
2967   l_freight_costs      WSH_FREIGHT_COSTS_PUB.PubFreightCostRecType;
2968 
2969   l_debug               NUMBER                                  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2970   l_lpn_tbl                     WMS_Data_Type_Definitions_PUB.LPNTableType;
2971   l_lpn_rec                     WMS_Data_Type_Definitions_PUB.LPNRecordType;
2972 
2973   cursor lpn_cur is
2974     select lpn_id, organization_id
2975     from wms_license_plate_numbers
2976     where outermost_lpn_id = p_outermost_lpn_id;
2977 
2978 
2979 BEGIN
2980   x_return_status  := fnd_api.g_ret_sts_success;
2981 
2982   IF (l_debug = 1) THEN
2983     DEBUG('p_org_id: ' || p_org_id, 'UNLOAD_TRUCK');
2984     DEBUG('p_outermost_lpn_id: ' || p_outermost_lpn_id, 'UNLOAD_TRUCK');
2985     DEBUG('p_relieve_rsv: ' || p_relieve_rsv, 'UNLOAD_TRUCK');
2986     DEBUG('Before deleting the freight record corresponding to LPN', 'UNLOAD_TRUCK');
2987   END IF;
2988 
2989   --patchset J.  Shipping API cleanup
2990   --delete freight costs
2991   BEGIN
2992      SELECT wdd.delivery_detail_id, wfc.freight_cost_id
2993        INTO l_freight_costs.delivery_detail_id,
2994        l_freight_costs.freight_cost_id
2995        FROM wsh_delivery_details_ob_grp_v wdd,
2996        wsh_freight_costs wfc
2997        WHERE wdd.lpn_id = p_outermost_lpn_id
2998        AND wdd.released_status = 'X'  -- For LPN reuse ER : 6845650
2999        AND wdd.delivery_detail_id = wfc.delivery_detail_id;
3000 
3001      IF l_debug = 1 THEN
3002     debug('About to call wsh_freight_costs_pub.delete_freight_costs','UNLOAD_TRUCK');
3003     debug('delivery_detail_id : '|| l_freight_costs.delivery_detail_id,'UNLOAD_TRUCK');
3004     debug('freight_cost_id    : '|| l_freight_costs.freight_cost_id, 'UNLOAD_TRUCK');
3005      END IF;
3006      wsh_freight_costs_pub.delete_freight_costs
3007        (p_api_version_number  => 1.0,
3008          p_init_msg_list       => l_init_msg_list,
3009          p_commit              => fnd_api.g_false,
3010          x_return_status       => l_return_status,
3011          x_msg_count           => l_msg_count,
3012          x_msg_data            => l_msg_data,
3013          p_pub_freight_costs   => l_freight_costs);
3014 
3015   EXCEPTION
3016      WHEN no_data_found THEN
3017     IF (l_debug = 1) THEN
3018       DEBUG('Could not find record in wsh_freight_cost', 'UNLOAD_TRUCK');
3019        END IF;
3020   END;
3021   --\Shipping API cleanup
3022 
3023   BEGIN
3024     SELECT wstt.delivery_id
3025          , wnd.status_code
3026       INTO l_del_id
3027          , l_status_code
3028       FROM wms_shipping_transaction_temp wstt, wsh_new_deliveries_ob_grp_v wnd
3029      WHERE wstt.outermost_lpn_id = p_outermost_lpn_id
3030        AND wstt.direct_ship_flag = 'Y'
3031        AND wstt.delivery_id = wnd.delivery_id
3032        AND ROWNUM = 1;
3033 
3034     IF (l_debug = 1) THEN
3035       DEBUG('The value of l_del_id and status are ' || l_del_id || '- ' || l_status_code, 'unload_truck');
3036     END IF;
3037 
3038     IF l_status_code = 'CO' THEN
3039       wsh_deliveries_pub.delivery_action(
3040         p_api_version_number         => 1.0
3041       , p_init_msg_list              => l_init_msg_list
3042       , x_return_status              => l_return_status
3043       , x_msg_count                  => l_msg_count
3044       , x_msg_data                   => l_msg_data
3045       , p_action_code                => 'RE-OPEN'
3046       , p_delivery_id                => l_del_id
3047       , p_sc_action_flag             => NULL
3048       , p_sc_intransit_flag          => NULL
3049       , p_sc_close_trip_flag         => NULL
3050       , p_sc_create_bol_flag         => NULL
3051       , p_sc_stage_del_flag          => NULL
3052       , p_sc_defer_interface_flag    => NULL
3053       , p_wv_override_flag           => NULL
3054       , x_trip_id                    => l_trip_id
3055       , x_trip_name                  => l_trip_name
3056       );
3057 
3058       IF (l_debug = 1) THEN
3059         DEBUG('The value of return status of delivery_actions is ' || l_return_status, 'unload_truck');
3060       END IF;
3061 
3062       IF (l_return_status IN(fnd_api.g_ret_sts_error)) THEN
3063         IF (l_debug = 1) THEN
3064           DEBUG('Reopening of delivery failed ' || l_del_id, 'UNLOAD_TRUCK');
3065         END IF;
3066 
3067         RAISE fnd_api.g_exc_error;
3068       ELSIF(l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
3069         IF (l_debug = 1) THEN
3070           DEBUG(' Reopening of delivery failed with Unexpected error' || l_del_id, 'UNLOAD_TRUCK');
3071         END IF;
3072 
3073         RAISE fnd_api.g_exc_unexpected_error;
3074       END IF;
3075     END IF;
3076   EXCEPTION
3077     WHEN NO_DATA_FOUND THEN
3078       NULL;
3079   END;
3080 
3081   --Get the delivery detail id corresponding to the outermost lpn_id
3082   SELECT wdd.delivery_detail_id
3083     INTO l_outermost_dd_id
3084     FROM wsh_delivery_details_ob_grp_v wdd
3085    WHERE lpn_id = p_outermost_lpn_id
3086    AND released_status = 'X';  -- For LPN reuse ER : 6845650
3087 
3088   counter          := 0;
3089 
3090   IF (l_debug = 1) THEN
3091     DEBUG('outermost delivery_detail_id: ' || l_outermost_dd_id, 'Unload Truck');
3092   END IF;
3093 
3094   --FOR l_delivery_detail_cur IN delivery_details LOOP
3095    --OPEN delivery_details(l_outermost_dd_id);
3096   FOR l_wda_del_detail IN wda_del_detail(l_outermost_dd_id) LOOP
3097     OPEN wdd_del_detail(l_wda_del_detail.delivery_detail_id);
3098 
3099     LOOP
3100       FETCH wdd_del_detail
3101       INTO l_delivery_detail_id,
3102            l_item_id,
3103            l_serial_number,
3104            l_source_line_id,
3105            l_req_qty,
3106            l_transaction_temp_id;
3107       EXIT WHEN wdd_del_detail%NOTFOUND;
3108 
3109       IF (l_debug = 1) THEN
3110         DEBUG('Entered the delivery details loop', 'Unload Truck');
3111       END IF;
3112 
3113       --Increment the loop counter
3114       counter                                            := counter + 1;
3115       l_delivery_detail_tab(counter).delivery_detail_id  := l_delivery_detail_id;
3116       l_delivery_detail_tab(counter).source_line_id      := l_source_line_id;
3117 
3118      /* Mrana: 7/6/06: 5350778 : Added lot_divisible_flag in the foll. query */
3119 
3120       SELECT serial_number_control_code,
3121              lot_control_code,
3122              lot_divisible_flag
3123         INTO l_serial_ctrl_code,
3124              l_lot_ctrl_code,
3125              l_lot_divisible_flag
3126         FROM mtl_system_items
3127        WHERE inventory_item_id = l_item_id
3128          AND organization_id = p_org_id;
3129 
3130       IF (l_debug = 1) THEN
3131         DEBUG('Serial Control Code of item: ' || l_item_id || ' : is: ' || l_serial_ctrl_code, 'Unload_Truck');
3132         DEBUG('l_lot_ctrl_code : ' || l_lot_ctrl_code , 'Unload_Truck');
3133         DEBUG('l_lot_divisible_flag :' || l_lot_divisible_flag , 'Unload_Truck');
3134         DEBUG('serial #:' || l_serial_number || ' source line id is: ' || l_source_line_id, 'Unload_Truck');
3135       END IF;
3136 
3137       IF l_serial_ctrl_code IN(2, 5) THEN
3138         BEGIN
3139           UPDATE mtl_serial_numbers
3140              SET current_status = 3
3141                , group_mark_id = NULL -- -1
3142            WHERE inventory_item_id = l_item_id
3143              AND current_organization_id = p_org_id
3144              AND(serial_number = l_serial_number
3145                  OR group_mark_id = l_transaction_temp_id); --bug#2829514
3146 
3147           IF (l_debug = 1) THEN
3148             DEBUG('Group mark id reset for serial with group mark id =' || l_transaction_temp_id, 'Unload_Truck');
3149             DEBUG('Reset the group mark Id for: ' || SQL%ROWCOUNT || ' serials', 'Unload_truck');
3150           END IF;
3151         EXCEPTION
3152           WHEN OTHERS THEN
3153             NULL;
3154         END;
3155       ELSIF l_serial_ctrl_code = 6 THEN
3156         BEGIN
3157           UPDATE mtl_serial_numbers
3158              SET current_status = 1
3159                , group_mark_id = NULL -- -1
3160            WHERE inventory_item_id = l_item_id
3161              AND current_organization_id = p_org_id
3162              AND(serial_number = l_serial_number
3163                  OR group_mark_id = l_transaction_temp_id); --bug#2829514
3164 
3165           IF (l_debug = 1) THEN
3166             DEBUG('Group mark id reset for serial with group mark id =' || l_transaction_temp_id, 'Unload_Truck');
3167             DEBUG('Reset the group mark Id for: ' || SQL%ROWCOUNT || ' serials', 'Unload_truck');
3168           END IF;
3169         EXCEPTION
3170           WHEN OTHERS THEN
3171             NULL;
3172         END;
3173       END IF;
3174 
3175       /* Mrana: 7/6/06: 5350778 :
3176        *  IF lot is not divisible, then we cannot relieve partial reservations,
3177        *  therefore we will always relieve reservations for suh items,whatever
3178        *  may be the form function parameter value in p_relieve_rsv */
3179 
3180       IF l_lot_ctrl_code = 2 THEN
3181          IF l_lot_divisible_flag = 'N' THEN
3182             l_relieve_rsv := 'Y' ;
3183          ELSE
3184             l_relieve_rsv := p_relieve_rsv;
3185          END IF ;
3186       ELSE
3187          l_relieve_rsv := p_relieve_rsv;
3188       END IF ;
3189 
3190       IF (l_debug = 1) THEN
3191         DEBUG('The delivery_detail_id is ' || l_delivery_detail_id, 'Unload_truck');
3192         DEBUG('l_relieve_rsv : ' || l_relieve_rsv, 'Unload_truck');
3193       END IF;
3194 
3195       --Return the delivery line to stock
3196       inv_shipping_transaction_pub.inv_line_return_to_stock(
3197         p_delivery_id                => NULL
3198       , p_delivery_line_id           => l_delivery_detail_id
3199       , p_shipped_quantity           => 0
3200       , x_msg_data                   => x_msg_data
3201       , x_msg_count                  => x_msg_count
3202       , x_return_status              => l_return_status
3203       , p_commit_flag                => fnd_api.g_false
3204       ,p_relieve_rsv                 => l_relieve_rsv /*added 3396821*/
3205       );
3206 
3207       IF l_return_status = fnd_api.g_ret_sts_error THEN
3208         fnd_message.set_name('WMS', 'WMS_RET_LINE_TO_STOCK');
3209         /* Failed to unload the delivery line */
3210         fnd_msg_pub.ADD;
3211 
3212         IF (l_debug = 1) THEN
3213           DEBUG('inv_line_return_to_stock API failed with status E ', 'Unload_Truck');
3214         END IF;
3215 
3216         RAISE fnd_api.g_exc_error;
3217       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3218         IF (l_debug = 1) THEN
3219           DEBUG('inv_line_return_to_stock API failed with status U', 'Unload_Truck');
3220         END IF;
3221 
3222         fnd_message.set_name('WMS', 'WMS_RET_LINE_TO_STOCK');
3223         /* Failed to unload the delivery line */
3224         fnd_msg_pub.ADD;
3225         RAISE fnd_api.g_exc_unexpected_error;
3226       END IF;
3227 
3228       IF (l_debug = 1) THEN
3229         DEBUG('inv_line_return_to_stock successful for iteration #:' || counter, 'Unload_truck');
3230       END IF;
3231 
3232 
3233       IF (l_debug = 1) THEN
3234         DEBUG('Update LPN Context to Reside in inventory', 'Unload_truck');
3235       END IF;
3236 
3237 
3238     --Store the source line Id for checking the next delivery detail
3239     END LOOP;
3240 
3241     CLOSE wdd_del_detail;
3242   END LOOP;
3243 
3244   /* Release 12 LPN Sync
3245      Call wms_container_pvt.modify_lpn API
3246        to update the context and remove the WDD records
3247        associated with those LPNs  */
3248   l_lpn_tbl.delete;
3249   FOR l_lpn IN lpn_cur LOOP
3250      l_lpn_rec.organization_id := l_lpn.organization_id;
3251      l_lpn_rec.lpn_id := l_lpn.lpn_id;
3252      l_lpn_rec.lpn_context := 1;
3253      l_lpn_tbl(nvl(l_lpn_tbl.last, 0)+1) := l_lpn_rec;
3254      IF (l_debug = 1) THEN
3255         debug('Add to l_lpn_tbl with lpn_rec of org_id'||l_lpn_rec.organization_id
3256                ||', lpn_id '||l_lpn_rec.lpn_id||', lpn_context '||l_lpn_rec.lpn_context, 'Unload_Truck');
3257      END IF;
3258   END LOOP;
3259   IF(l_debug = 1) THEN
3260      DEBUG('Calling WMS_CONTAINER_PVT.Modify_LPNs with caller WMS_DIRECTSHIP','Unload_Truck');
3261   END IF;
3262 
3263   WMS_CONTAINER_PVT.Modify_LPNs(
3264   	  p_api_version           => 1.0
3265   	, p_init_msg_list         => fnd_api.g_true
3266   	, p_commit                => fnd_api.g_false
3267   	, x_return_status         => l_return_status
3268   	, x_msg_count             => x_msg_count
3269   	, x_msg_data              => x_msg_data
3270   	, p_caller                => 'WMS_DIRECTSHIP'
3271   	, p_lpn_table             => l_lpn_tbl
3272   );
3273   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
3274        IF (l_debug = 1) THEN
3275   	 DEBUG('return error from WMS_CONTAINER_PVT.Modify_LPNs', 'Unload_Truck');
3276        END IF;
3277        RAISE fnd_api.g_exc_error;
3278   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3279        IF (l_debug = 1) THEN
3280   	 DEBUG('return error from WMS_CONTAINER_PVT.Modify_LPNs', 'Unload_Truck');
3281        END IF;
3282        RAISE fnd_api.g_exc_unexpected_error;
3283   ELSIF l_return_status = fnd_api.g_ret_sts_success THEN
3284        null;
3285   END IF;
3286   -- End of Release 12 change
3287 
3288   --Now, clean up the direct ship records from WSTT, WFCT, MSNT and WDST
3289   wms_direct_ship_pvt.cleanup_temp_recs(
3290     x_msg_data                   => x_msg_data
3291   , x_msg_count                  => x_msg_count
3292   , x_return_status              => l_return_status
3293   , p_org_id                     => p_org_id
3294   , p_outermost_lpn_id           => p_outermost_lpn_id
3295   , p_trip_id                    => NULL
3296   );
3297 
3298   IF l_return_status = fnd_api.g_ret_sts_error THEN
3299     fnd_message.set_name('WMS', 'WMS_CLEANUP_TEMP');
3300     /*Failed in cleanup program*/
3301     fnd_msg_pub.ADD;
3302 
3303     IF (l_debug = 1) THEN
3304       DEBUG('CLEANUP_TEMP_RECS API failed with status E ', 'Unload_truck');
3305     END IF;
3306 
3307     RAISE fnd_api.g_exc_error;
3308   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3309     fnd_message.set_name('WMS', 'WMS_CLEANUP_TEMP');
3310     /*Failed in cleanup program*/
3311     fnd_msg_pub.ADD;
3312 
3313     IF (l_debug = 1) THEN
3314       DEBUG('CLEANUP_TEMP_RECS failed with status U', 'Unload_truck');
3315     END IF;
3316 
3317     RAISE fnd_api.g_exc_unexpected_error;
3318   END IF;
3319 
3320   IF (l_debug = 1) THEN
3321      DEBUG('UNLOAD_TRUCK Completed successfully', 'Unload Truck');
3322   END IF;
3323 
3324   -- Commit if no errors
3325   COMMIT;
3326 EXCEPTION
3327   WHEN fnd_api.g_exc_error THEN
3328     x_return_status  := fnd_api.g_ret_sts_error;
3329     ROLLBACK;
3330     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3331 
3332     IF (l_debug = 1) THEN
3333       DEBUG('Execution Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3334     END IF;
3335   WHEN fnd_api.g_exc_unexpected_error THEN
3336     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3337     ROLLBACK;
3338     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3339 
3340     IF (l_debug = 1) THEN
3341       DEBUG('Unexpected Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3342     END IF;
3343   WHEN OTHERS THEN
3344     --x_error_code := 9999;
3345     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3346     ROLLBACK;
3347 
3348     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3349       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'UNLOAD_TRUCK');
3350     END IF;
3351 
3352     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3353 
3354     IF (l_debug = 1) THEN
3355       DEBUG('Unexpected Error in Unload_Truck:' || SUBSTR(SQLERRM, 1, 240), 'Unload_Truck');
3356     END IF;
3357 END unload_truck;
3358 
3359 PROCEDURE cleanup_temp_recs(
3360   x_return_status    OUT NOCOPY    VARCHAR2
3361 , x_msg_count        OUT NOCOPY    NUMBER
3362 , x_msg_data         OUT NOCOPY    VARCHAR2
3363 , p_org_id           IN            NUMBER
3364 , p_outermost_lpn_id IN            NUMBER
3365 , p_trip_id          IN            NUMBER
3366 , p_dock_door_id     IN            NUMBER DEFAULT NULL
3367 ) IS
3368   CURSOR outermost_lpn_cur IS
3369     SELECT DISTINCT outermost_lpn_id
3370                FROM wms_shipping_transaction_temp
3371               WHERE organization_id = p_org_id
3372                 AND trip_id = p_trip_id;
3373 
3374   CURSOR delivery_cur IS
3375     SELECT DISTINCT delivery_id
3376                FROM wms_shipping_transaction_temp
3377               WHERE organization_id = p_org_id
3378                 AND trip_id = p_trip_id;
3379 
3380   l_outermost_lpn_id NUMBER;
3381   l_debug            NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3382 BEGIN
3383   x_return_status  := fnd_api.g_ret_sts_success;
3384 
3385   IF p_trip_id IS NOT NULL THEN
3386     BEGIN
3387       --Delete the records for the LPN
3388       FOR l_lpn IN outermost_lpn_cur LOOP
3389         l_outermost_lpn_id  := l_lpn.outermost_lpn_id;
3390 
3391         DELETE FROM wms_freight_cost_temp
3392               WHERE organization_id = p_org_id
3393                 AND lpn_id = l_outermost_lpn_id;
3394 
3395         DELETE FROM mtl_serial_numbers_temp
3396               WHERE transaction_temp_id IN(SELECT transaction_temp_id
3397                                              FROM wms_direct_ship_temp
3398                                             WHERE lpn_id = p_outermost_lpn_id);
3399 
3400         DELETE FROM wms_direct_ship_temp
3401               WHERE organization_id = p_org_id
3402                 --AND    lpn_id = p_outermost_lpn_id;
3403                 /* part of bug fix 2538703 */
3404                 AND lpn_id = l_outermost_lpn_id;
3405       /* end of bug fix 2538703 */
3406       END LOOP;
3407 
3408       --Delete the records for the delivery
3409       FOR l_delivery IN delivery_cur LOOP
3410         DELETE FROM wms_freight_cost_temp
3411               WHERE organization_id = p_org_id
3412                 AND delivery_id = l_delivery.delivery_id;
3413       END LOOP;
3414 
3415       --Delete the trip records
3416       DELETE FROM wms_freight_cost_temp
3417             WHERE organization_id = p_org_id
3418               AND trip_id = p_trip_id;
3419 
3420       DELETE FROM wms_shipping_transaction_temp
3421             WHERE organization_id = p_org_id
3422               AND trip_id = p_trip_id;
3423 
3424       IF (l_debug = 1) THEN
3425         DEBUG('Deleted temp recs for the case when trip id is not passed', 'CLEANUP_TEMP_RECS');
3426       END IF;
3427     EXCEPTION
3428       WHEN OTHERS THEN
3429         NULL;
3430     END;
3431   ELSE
3432     --Delete the Shipping transaction and freight entries
3433     DELETE FROM wms_shipping_transaction_temp
3434           WHERE organization_id = p_org_id
3435             AND outermost_lpn_id = p_outermost_lpn_id;
3436 
3437     DELETE FROM wms_freight_cost_temp
3438           WHERE lpn_id = p_outermost_lpn_id;
3439 
3440     --Delete records from Mtl_Seral_Numbers_Temp if any
3441     BEGIN
3442       DELETE FROM mtl_serial_numbers_temp
3443             WHERE transaction_temp_id IN(SELECT transaction_temp_id
3444                                            FROM wms_direct_ship_temp
3445                                           WHERE lpn_id = p_outermost_lpn_id);
3446     EXCEPTION
3447       WHEN NO_DATA_FOUND THEN
3448         NULL;
3449     END;
3450 
3451     DELETE FROM wms_direct_ship_temp
3452           WHERE lpn_id = p_outermost_lpn_id;
3453 
3454     IF (l_debug = 1) THEN
3455       DEBUG('Deleted temp recs for the case when trip id is not passed', 'CLEANUP_TEMP_RECS');
3456     END IF;
3457   END IF;
3458 EXCEPTION
3459   WHEN OTHERS THEN
3460     --x_error_code := 9999;
3461     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3462     ROLLBACK;
3463 
3464     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3465       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'CLEANUP_TEMP');
3466     END IF;
3467 
3468     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3469 
3470     IF (l_debug = 1) THEN
3471       DEBUG('Unexpected Error in cleanup_temp:' || SUBSTR(SQLERRM, 1, 240), 'CLEANUP_TEMP_RECS');
3472     END IF;
3473 END cleanup_temp_recs;
3474 
3475 PROCEDURE get_global_values(
3476   x_userid        OUT NOCOPY NUMBER
3477 , x_logonid       OUT NOCOPY NUMBER
3478 , x_last_upd_date OUT NOCOPY DATE
3479 , x_current_date  OUT NOCOPY DATE
3480 ) IS
3481   x_progress VARCHAR2(3) := NULL;
3482   l_debug    NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3483 BEGIN
3484   x_progress       := 10;
3485   x_userid         := fnd_global.user_id;
3486   x_progress       := 20;
3487   x_logonid        := fnd_global.login_id;
3488   x_progress       := 30;
3489   x_last_upd_date  := SYSDATE;
3490   x_progress       := 40;
3491   x_current_date   := SYSDATE;
3492 EXCEPTION
3493   WHEN OTHERS THEN
3494     IF (l_debug = 1) THEN
3495       DEBUG('Error in WMS_DIRECT_SHIP_PVT.get_global_values: ' || x_progress);
3496     END IF;
3497 END get_global_values;
3498 
3499 PROCEDURE create_resv(
3500   x_return_status OUT NOCOPY    VARCHAR2
3501 , x_msg_count     OUT NOCOPY    NUMBER
3502 , x_msg_data      OUT NOCOPY    VARCHAR2
3503 , p_group_id      IN            NUMBER
3504 , p_org_id        IN            NUMBER
3505 ) IS
3506 BEGIN
3507   --bug #2782201
3508   NULL;
3509 /*
3510   This method is no more used from I patchset. The corresponding functionality can be found in
3511   process_lpn and process_line.
3512  */
3513 END create_resv;
3514 
3515 PROCEDURE validate_status_lpn_contents(
3516   x_return_status OUT NOCOPY    VARCHAR2
3517 , x_msg_count     OUT NOCOPY    NUMBER
3518 , x_msg_data      OUT NOCOPY    VARCHAR2
3519 , p_lpn_id        IN            NUMBER
3520 , p_org_id        IN            NUMBER
3521 ) IS
3522   CURSOR lpn_contents IS
3523     SELECT wlpn.lpn_id
3524          , wlpn.subinventory_code
3525          , wlpn.locator_id
3526          , wlc.lot_number
3527          , wlc.inventory_item_id
3528          , msn.serial_number
3529       FROM wms_lpn_contents wlc, wms_license_plate_numbers wlpn, mtl_serial_numbers msn
3530      WHERE wlpn.lpn_id = wlc.parent_lpn_id
3531        AND msn.lpn_id(+) = wlc.parent_lpn_id
3532        AND msn.inventory_item_id(+) = wlc.inventory_item_id
3533        AND msn.current_organization_id(+) = wlc.organization_id
3534        /* Bug# 3119461 Without the following joins for lot and rev
3535        ** we would get a cartesian product */
3536        AND NVL(msn.lot_number(+),'#NULL#') = NVL(wlc.lot_number,'#NULL#')
3537        AND NVL(msn.revision(+),'#NULL#') = NVL(wlc.revision,'#NULL#')
3538        AND wlpn.outermost_lpn_id = p_lpn_id
3539        AND wlpn.organization_id = p_org_id
3540        AND wlc.organization_id = p_org_id;
3541 
3542   l_lpn_contents       lpn_contents%ROWTYPE;
3543   l_return_status      VARCHAR2(1);
3544   l_msg_count          NUMBER;
3545   l_msg_data           VARCHAR2(20000);
3546   l_trx_status_enabled NUMBER;
3547   l_lpn_status         VARCHAR2(1)            := 'Y';
3548   l_debug              NUMBER                 := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3549 BEGIN
3550   x_return_status  := fnd_api.g_ret_sts_success;
3551 
3552   SELECT status_control_flag
3553     INTO l_trx_status_enabled
3554     FROM mtl_transaction_types
3555    WHERE transaction_type_id = 52;  /* Transaction_type_id is hardcoded to 52 ..sales order staging transfer*/
3556 
3557   OPEN lpn_contents;
3558 
3559   LOOP
3560     FETCH lpn_contents INTO l_lpn_contents;
3561     EXIT WHEN lpn_contents%NOTFOUND;
3562     l_lpn_status  :=
3563       inv_material_status_grp.is_status_applicable(
3564         p_wms_installed              => 'TRUE'
3565       , p_trx_status_enabled         => l_trx_status_enabled
3566       , p_trx_type_id                => 52
3567       , p_organization_id            => p_org_id
3568       , p_inventory_item_id          => l_lpn_contents.inventory_item_id
3569       , p_sub_code                   => l_lpn_contents.subinventory_code
3570       , p_locator_id                 => l_lpn_contents.locator_id
3571       , p_lot_number                 => l_lpn_contents.lot_number
3572       , p_serial_number              => l_lpn_contents.serial_number
3573       , p_object_type                => 'A'
3574       );
3575 
3576     IF l_lpn_status <> 'Y' THEN
3577       x_return_status  := fnd_api.g_ret_sts_error;
3578       EXIT;
3579     END IF;
3580   END LOOP;
3581 
3582   CLOSE lpn_contents;
3583 EXCEPTION
3584   WHEN OTHERS THEN
3585     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3586 
3587     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3588       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'validate_status_lpn_contents');
3589     END IF;
3590 
3591     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3592 END validate_status_lpn_contents;
3593 
3594 PROCEDURE update_freight_cost(
3595   x_return_status OUT NOCOPY    VARCHAR2
3596 , x_msg_count     OUT NOCOPY    NUMBER
3597 , x_msg_data      OUT NOCOPY    VARCHAR2
3598 , p_lpn_id        IN            NUMBER
3599 ) IS
3600   CURSOR lpn_freight IS
3601     SELECT        ROWID
3602                 , freight_cost_type_id
3603                 , currency_code
3604                 , freight_amount
3605                 , conversion_type
3606              FROM wms_freight_cost_temp
3607             WHERE lpn_id = p_lpn_id
3608               AND freight_cost_id IS NULL
3609     FOR UPDATE OF freight_cost_id;
3610 
3611   CURSOR delivery_detail IS
3612     SELECT wdd.delivery_detail_id
3613       FROM wsh_delivery_details_ob_grp_v wdd
3614      WHERE lpn_id = p_lpn_id
3615      AND released_status = 'X';  -- For LPN reuse ER : 6845650
3616 
3617   l_freight_cost_rec wsh_freight_costs_pub.pubfreightcostrectype;
3618   l_delivery_detail  delivery_detail%ROWTYPE;
3619   l_lpn_freight      lpn_freight%ROWTYPE;
3620   l_init_msg_list    VARCHAR2(1)                                 := fnd_api.g_true;
3621   l_return_status    VARCHAR2(1);
3622   l_msg_count        NUMBER;
3623   l_msg_data         VARCHAR2(20000);
3624   l_freight_cost_id  NUMBER;
3625   l_debug            NUMBER                                      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3626 BEGIN
3627   x_return_status  := fnd_api.g_ret_sts_success;
3628   SAVEPOINT freight_cost;
3629   OPEN delivery_detail;
3630 
3631   LOOP
3632     FETCH delivery_detail INTO l_delivery_detail;
3633     EXIT WHEN delivery_detail%NOTFOUND;
3634   END LOOP;
3635 
3636   CLOSE delivery_detail;
3637   OPEN lpn_freight;
3638 
3639   LOOP
3640     FETCH lpn_freight INTO l_lpn_freight;
3641     EXIT WHEN lpn_freight%NOTFOUND;
3642     l_freight_cost_rec.freight_cost_type_id  := l_lpn_freight.freight_cost_type_id;
3643     l_freight_cost_rec.currency_code         := l_lpn_freight.currency_code;
3644     l_freight_cost_rec.unit_amount           := l_lpn_freight.freight_amount;
3645     l_freight_cost_rec.conversion_type_code  := l_lpn_freight.conversion_type;
3646     l_freight_cost_rec.delivery_detail_id    := l_delivery_detail.delivery_detail_id;
3647     l_freight_cost_rec.action_code           := 'CREATE';
3648     wsh_freight_costs_pub.create_update_freight_costs(
3649       p_api_version_number         => 1.0
3650     , p_init_msg_list              => l_init_msg_list
3651     , p_commit                     => fnd_api.g_false
3652     , x_return_status              => l_return_status
3653     , x_msg_count                  => l_msg_count
3654     , x_msg_data                   => l_msg_data
3655     , p_pub_freight_costs          => l_freight_cost_rec
3656     , p_action_code                => 'CREATE'
3657     , x_freight_cost_id            => l_freight_cost_id
3658     );
3659 
3660     IF l_return_status = fnd_api.g_ret_sts_error THEN
3661       IF (l_debug = 1) THEN
3662         DEBUG('Create_Update_Freight_Costs API completed status E ', 'update_freight_cost');
3663       END IF;
3664 
3665       RAISE fnd_api.g_exc_error;
3666     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3667       IF (l_debug = 1) THEN
3668         DEBUG('Create_Update_Freight_Costs API completed status U ', 'update_freight_cost');
3669       END IF;
3670 
3671       RAISE fnd_api.g_exc_unexpected_error;
3672     END IF;
3673 
3674     UPDATE wms_freight_cost_temp
3675        SET freight_cost_id = l_freight_cost_id
3676          , last_update_date = SYSDATE
3677          , last_updated_by = fnd_global.user_id
3678      WHERE ROWID = l_lpn_freight.ROWID;
3679   END LOOP;
3680 
3681   CLOSE lpn_freight;
3682 
3683   IF (l_debug = 1) THEN
3684     DEBUG('Update_Freight cost Update was successful', 'update_freight_cost');
3685   END IF;
3686 EXCEPTION
3687   WHEN fnd_api.g_exc_error THEN
3688     IF (l_debug = 1) THEN
3689       DEBUG('Update_Freight cost API failed with status (E)', 'update_freight_cost');
3690     END IF;
3691 
3692     IF lpn_freight%ISOPEN THEN
3693       CLOSE lpn_freight;
3694     END IF;
3695 
3696     IF delivery_detail%ISOPEN THEN
3697       CLOSE delivery_detail;
3698     END IF;
3699 
3700     x_return_status  := fnd_api.g_ret_sts_error;
3701     ROLLBACK TO freight_cost;
3702     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3703   WHEN fnd_api.g_exc_unexpected_error THEN
3704     IF lpn_freight%ISOPEN THEN
3705       CLOSE lpn_freight;
3706     END IF;
3707 
3708     IF delivery_detail%ISOPEN THEN
3709       CLOSE delivery_detail;
3710     END IF;
3711 
3712     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3713 
3714     IF (l_debug = 1) THEN
3715       DEBUG('Update_Freight cost API failed with status (U)', 'update_freight_cost');
3716     END IF;
3717 
3718     ROLLBACK TO freight_cost;
3719     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3720   WHEN OTHERS THEN
3721     IF lpn_freight%ISOPEN THEN
3722       CLOSE lpn_freight;
3723     END IF;
3724 
3725     IF delivery_detail%ISOPEN THEN
3726       CLOSE delivery_detail;
3727     END IF;
3728 
3729     x_return_status  := fnd_api.g_ret_sts_unexp_error;
3730 
3731     IF (l_debug = 1) THEN
3732       DEBUG('Update_Freight cost API failed with status (U)', 'update_freight_cost');
3733     END IF;
3734 
3735     ROLLBACK TO freight_cost;
3736 
3737     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3738       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'update_freight_cost');
3739     END IF;
3740 
3741     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3742 END update_freight_cost;
3743 
3744 /******************************
3745 * Procedure Associates serial numbers with appropriate
3746 * Delivery lines.
3747 * Bug No 3390432 : Fix has changed the functionality for
3748 * Items with Serial numbers @ SO.
3749 * Serials Numbers of Items with Serial control code 6 is picked from
3750 * MSNT and inserted into MSN with appropriate values copied from
3751 * MSNT. When delivery lines are split transaction temp ids are changed
3752 * and appropriately updated into MSNT and into MSN as group mark id.
3753 ******************************/
3754 PROCEDURE explode_delivery_details(
3755   x_return_status              OUT NOCOPY    VARCHAR2
3756 , x_msg_count                  OUT NOCOPY    NUMBER
3757 , x_msg_data                   OUT NOCOPY    VARCHAR2
3758 --Bug No 3390432
3759 , x_transaction_temp_id        OUT NOCOPY    NUMBER
3760 , p_organization_id            IN            NUMBER
3761 , p_lpn_id                     IN            NUMBER
3762 , p_serial_number_control_code IN            NUMBER
3763 , p_delivery_detail_id         IN            NUMBER
3764 , p_quantity                   IN            NUMBER
3765 , p_transaction_temp_id        IN            NUMBER DEFAULT NULL
3766 , p_reservation_id             IN            NUMBER DEFAULT NULL
3767 , p_last_action                IN            VARCHAR2 DEFAULT 'U'
3768 ) IS
3769   l_running_quantity       NUMBER;
3770   l_split_status           VARCHAR2(30);
3771 
3772   TYPE serial_tab_type IS TABLE OF VARCHAR(30)
3773     INDEX BY BINARY_INTEGER;
3774 
3775   l_serial_numbers_table   serial_tab_type;
3776 
3777   CURSOR c_explode_detail IS
3778     SELECT msnt.ROWID
3779          , dd.requested_quantity
3780          , msnt.transaction_temp_id
3781          , dd.delivery_detail_id
3782          , msnt.fm_serial_number
3783          , msnt.to_serial_number
3784          , msnt.serial_prefix
3785          , dd.organization_id
3786          , dd.inventory_item_id
3787          , msnt.serial_attribute_category
3788          , msnt.origination_date
3789          , msnt.c_attribute1
3790          , msnt.c_attribute2
3791          , msnt.c_attribute3
3792          , msnt.c_attribute4
3793          , msnt.c_attribute5
3794          , msnt.c_attribute6
3795          , msnt.c_attribute7
3796          , msnt.c_attribute8
3797          , msnt.c_attribute9
3798          , msnt.c_attribute10
3799          , msnt.c_attribute11
3800          , msnt.c_attribute12
3801          , msnt.c_attribute13
3802          , msnt.c_attribute14
3803          , msnt.c_attribute15
3804          , msnt.c_attribute16
3805          , msnt.c_attribute17
3806          , msnt.c_attribute18
3807          , msnt.c_attribute19
3808          , msnt.c_attribute20
3809          , msnt.d_attribute1
3810          , msnt.d_attribute2
3811          , msnt.d_attribute3
3812          , msnt.d_attribute4
3813          , msnt.d_attribute5
3814          , msnt.d_attribute6
3815          , msnt.d_attribute7
3816          , msnt.d_attribute8
3817          , msnt.d_attribute9
3818          , msnt.d_attribute10
3819          , msnt.n_attribute1
3820          , msnt.n_attribute2
3821          , msnt.n_attribute3
3822          , msnt.n_attribute4
3823          , msnt.n_attribute5
3824          , msnt.n_attribute6
3825          , msnt.n_attribute7
3826          , msnt.n_attribute8
3827          , msnt.n_attribute9
3828          , msnt.n_attribute10
3829          , msnt.status_id
3830          , msnt.territory_code
3831          , msnt.time_since_new
3832          , msnt.cycles_since_new
3833          , msnt.time_since_overhaul
3834          , msnt.cycles_since_overhaul
3835          , msnt.time_since_repair
3836          , msnt.cycles_since_repair
3837          , msnt.time_since_visit
3838          , msnt.cycles_since_visit
3839          , msnt.time_since_mark
3840          , msnt.cycles_since_mark
3841          , msnt.number_of_repairs
3842       FROM wsh_delivery_details_ob_grp_v dd
3843          , mtl_serial_numbers_temp msnt
3844      WHERE delivery_detail_id = p_delivery_detail_id
3845        AND msnt.transaction_temp_id = p_transaction_temp_id;
3846 
3847   l_explode_detail         c_explode_detail%ROWTYPE;
3848   l_rowid                  ROWID;
3849 
3850   CURSOR c_serials(
3851     p_organization_id   NUMBER
3852   , p_inventory_item_id NUMBER
3853   , p_fm_serial_number  VARCHAR2
3854   , p_to_serial_number  VARCHAR2
3855   , p_serial_length     NUMBER
3856   ) IS
3857     SELECT serial_number
3858       FROM mtl_serial_numbers
3859      WHERE current_organization_id = p_organization_id
3860        AND inventory_item_id = p_inventory_item_id
3861        AND serial_number BETWEEN p_fm_serial_number AND p_to_serial_number
3862        AND current_status IN(1, 6)
3863        AND LENGTH(serial_number) = p_serial_length;
3864 
3865   CURSOR c_lpn_serial(p_organization_id NUMBER
3866                     , p_inventory_item_id NUMBER
3867                     , p_lpn_id NUMBER) IS
3868     SELECT serial_number,
3869            group_mark_id,
3870            reservation_id
3871       FROM mtl_serial_numbers a
3872      WHERE current_organization_id = p_organization_id
3873        AND inventory_item_id = p_inventory_item_id
3874        AND lpn_id = p_lpn_id
3875        AND current_status = 3
3876        AND (   NVL(group_mark_id, 0) < 1
3877            OR (NVL(group_mark_id, 0) = p_reservation_id)
3878            OR (reservation_id = p_reservation_id))
3879        ORDER BY  a.reservation_id
3880                , a.serial_number;
3881            -- Mrana: 08/30/06: bug:5446598
3882            -- Added or condition for group_mark_id, to honor serial Reservations.
3883            -- Added order by, so that the ones with group_mark_id are picked
3884            -- 1st. This is important where LPN qty is > serial_quantity and only
3885            -- a few serials are reserved.
3886            -- Note: After backorder with Retain reservations, the group_mark_id on MSN might not
3887            -- be equal to reservation_id, that is why the check for reservation_id
3888 
3889   -- added for patchset i end_item_unit_validation
3890   CURSOR c_lpn_serial_e(p_organization_id NUMBER
3891                       , p_inventory_item_id NUMBER
3892                       , p_lpn_id NUMBER
3893                       , p_eiun VARCHAR2) IS
3894     SELECT serial_number,
3895            group_mark_id,
3896            reservation_id
3897       FROM mtl_serial_numbers a
3898      WHERE current_organization_id = p_organization_id
3899        AND inventory_item_id = p_inventory_item_id
3900        AND lpn_id = p_lpn_id
3901        AND current_status = 3
3902        AND NVL(end_item_unit_number, '@@') = NVL(p_eiun, '@@')
3903        AND (   NVL(group_mark_id, 0) < 1
3904            OR (NVL(group_mark_id, 0) = p_reservation_id)
3905            OR (reservation_id = p_reservation_id))
3906        ORDER BY  a.reservation_id
3907                , a.serial_number;
3908            -- Mrana: 08/30/06: bug:5446598
3909            -- Added or condition for group_mark_id, to honor serial Reservations.
3910            -- Added order by, so that the ones with group_mark_id are picked
3911            -- 1st. This is important where LPN qty is > serial_quantity and only
3912            -- a few serials are reserved.
3913 
3914   l_new_delivery_detail_id NUMBER;
3915   l_range_serial           BOOLEAN                    := FALSE;
3916   l_split_const            NUMBER;
3917   l_real_serial_prefix     VARCHAR2(30);
3918   l_serial_numeric         NUMBER;
3919   l_serial_numeric_end     NUMBER;
3920   l_serial_quantity        NUMBER                     := 0;
3921   l_new_fm_serial          VARCHAR2(30);
3922   l_current_to_serial      VARCHAR2(30);
3923   l_serial_numeric_len     NUMBER;
3924   l_serial_number_current  VARCHAR2(30);
3925   l_loop_end               NUMBER;
3926   l_serial_number          VARCHAR2(30);
3927   j                        NUMBER;
3928   m                        NUMBER;
3929   n                        NUMBER;
3930   l_transaction_temp_id    NUMBER;
3931   l_inventory_item_id      NUMBER;
3932   l_return_status          VARCHAR2(1);
3933   l_group_mark_id          NUMBER;
3934   l_succes                 NUMBER;
3935   l_tot_line_qty           NUMBER;
3936   l_max_limit              BOOLEAN;
3937   l_end_item_unit_number   VARCHAR2(30);
3938   l_debug                  NUMBER                     := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3939   l_proc_msg               VARCHAR2(300);
3940   -- Bug No 3390432
3941   l_serial_number_str      VARCHAR2(3000);
3942   l_cur_group_mark_id      NUMBER;
3943   l_cur_reservation_id     NUMBER;
3944 BEGIN
3945   --x_return_status := fnd_api.g_ret_sts_success;
3946 
3947  IF (l_debug = 1) THEN
3948     DEBUG('p_organization_id             : ' || p_organization_id, 'EXPLODE_DELIVERY_DETAILS');
3949     DEBUG(' p_lpn_id                     : ' || p_lpn_id      , 'EXPLODE_DELIVERY_DETAILS');
3950     DEBUG(' p_serial_number_control_code : ' || p_serial_number_control_code , 'EXPLODE_DELIVERY_DETAILS');
3951     DEBUG(' p_delivery_detail_id         : ' || p_delivery_detail_id         , 'EXPLODE_DELIVERY_DETAILS');
3952     DEBUG(' p_quantity                   : ' || p_quantity                   , 'EXPLODE_DELIVERY_DETAILS');
3953     DEBUG(' p_transaction_temp_id        : ' || p_transaction_temp_id        , 'EXPLODE_DELIVERY_DETAILS');
3954     DEBUG(' p_reservation_id             : ' || p_reservation_id             , 'EXPLODE_DELIVERY_DETAILS');
3955     DEBUG(' p_last_action                : ' || p_last_action   , 'EXPLODE_DELIVERY_DETAILS');
3956  END IF;
3957 
3958   SELECT SUM(wdd2.requested_quantity)
3959     INTO l_tot_line_qty
3960     FROM wsh_delivery_details_ob_grp_v wdd1
3961        , wsh_delivery_details_ob_grp_v wdd2
3962    WHERE wdd1.delivery_detail_id = p_delivery_detail_id
3963      AND wdd2.source_header_id = wdd1.source_header_id
3964      AND wdd2.source_line_id = wdd1.source_line_id
3965      AND wdd2.source_code = wdd1.source_code
3966      AND wdd2.released_status IN('R', 'B', 'X')
3967      AND wdd2.container_flag = 'N';
3968 
3969   IF l_tot_line_qty > p_quantity THEN
3970     l_max_limit  := TRUE;
3971   ELSE
3972     l_max_limit  := FALSE;
3973   END IF;
3974 
3975   -- ideally group_mark_id should be equal to transaction_temp_id
3976   l_group_mark_id  := p_transaction_temp_id;
3977 
3978   IF (l_debug = 1) THEN
3979     DEBUG('l_group_mark_id=' || l_group_mark_id, 'EXPLODE_DELIVERY_DETAILS');
3980   END IF;
3981 
3982   IF p_serial_number_control_code = 6 THEN
3983     /***********************
3984     * Bug No 3390432
3985     * Changes are made only for Items with
3986     * Serial control code 6 (Serial @ SO Items)
3987     **********************/
3988     l_serial_quantity  := p_quantity;
3989 
3990     IF (l_debug = 1) THEN
3991       DEBUG('P_SERIAL_CONTROL_CODE=6', 'EXPLODE_DELIVERY_DETAILS');
3992     END IF;
3993 
3994     -- Serial Number is lying in MTL_SERIAL_NUMBERS_TEMP
3995     IF (l_debug = 1) THEN
3996       DEBUG('EXPLODE delviery detail id ' || TO_CHAR(p_delivery_detail_id), 'EXPLODE_DELIVERY_DETAILS');
3997     END IF;
3998 
3999     /***********************
4000     * Bug No 3390432
4001     * Create a new Transaction Temp Id.
4002     * This Transaction temp id will be associated
4003     * with the delivery line id at the end of processing
4004     * We will use this Transaction temp Id as the group Mark Id aslo.
4005     ********************/
4006     SELECT mtl_material_transactions_s.NEXTVAL
4007       INTO x_transaction_temp_id
4008       FROM DUAL;
4009 
4010     -- Update group_mark_id to reflect newly created transaction_temp_id
4011     l_group_mark_id  := x_transaction_temp_id;
4012 
4013     x_return_status    := wsh_util_core.g_ret_sts_success;
4014     l_split_const      := 1;
4015 
4016     OPEN c_explode_detail;
4017     m                  := 1;
4018 
4019     LOOP
4020       FETCH c_explode_detail INTO l_explode_detail;
4021       EXIT WHEN c_explode_detail%NOTFOUND;
4022 
4023       IF (l_debug = 1) THEN
4024         DEBUG('Fetch the first serial number' || l_explode_detail.fm_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4025       END IF;
4026 
4027       IF (l_explode_detail.fm_serial_number = l_explode_detail.to_serial_number) THEN
4028         IF (l_debug = 1) THEN
4029           DEBUG('From and To Serial Numbers are Same','EXPLODE_DELIVEY_DETAILS');
4030         END IF;
4031         UPDATE mtl_serial_numbers
4032            SET serial_attribute_category = l_explode_detail.serial_attribute_category
4033              , origination_date = l_explode_detail.origination_date
4034              , c_attribute1 = l_explode_detail.c_attribute1
4035              , c_attribute2 = l_explode_detail.c_attribute2
4036              , c_attribute3 = l_explode_detail.c_attribute3
4037              , c_attribute4 = l_explode_detail.c_attribute4
4038              , c_attribute5 = l_explode_detail.c_attribute5
4039              , c_attribute6 = l_explode_detail.c_attribute6
4040              , c_attribute7 = l_explode_detail.c_attribute7
4041              , c_attribute8 = l_explode_detail.c_attribute8
4042              , c_attribute9 = l_explode_detail.c_attribute9
4043              , c_attribute10 = l_explode_detail.c_attribute10
4044              , c_attribute11 = l_explode_detail.c_attribute11
4045              , c_attribute12 = l_explode_detail.c_attribute12
4046              , c_attribute13 = l_explode_detail.c_attribute13
4047              , c_attribute14 = l_explode_detail.c_attribute14
4048              , c_attribute15 = l_explode_detail.c_attribute15
4049              , c_attribute16 = l_explode_detail.c_attribute16
4050              , c_attribute17 = l_explode_detail.c_attribute17
4051              , c_attribute18 = l_explode_detail.c_attribute18
4052              , c_attribute19 = l_explode_detail.c_attribute19
4053              , c_attribute20 = l_explode_detail.c_attribute20
4054              , d_attribute1 = l_explode_detail.d_attribute1
4055              , d_attribute2 = l_explode_detail.d_attribute2
4056              , d_attribute3 = l_explode_detail.d_attribute3
4057              , d_attribute4 = l_explode_detail.d_attribute4
4058              , d_attribute5 = l_explode_detail.d_attribute5
4059              , d_attribute6 = l_explode_detail.d_attribute6
4060              , d_attribute7 = l_explode_detail.d_attribute7
4061              , d_attribute8 = l_explode_detail.d_attribute8
4062              , d_attribute9 = l_explode_detail.d_attribute9
4063              , d_attribute10 = l_explode_detail.d_attribute10
4064              , n_attribute1 = l_explode_detail.n_attribute1
4065              , n_attribute2 = l_explode_detail.n_attribute2
4066              , n_attribute3 = l_explode_detail.n_attribute3
4067              , n_attribute4 = l_explode_detail.n_attribute4
4068              , n_attribute5 = l_explode_detail.n_attribute5
4069              , n_attribute6 = l_explode_detail.n_attribute6
4070              , n_attribute7 = l_explode_detail.n_attribute7
4071              , n_attribute8 = l_explode_detail.n_attribute8
4072              , n_attribute9 = l_explode_detail.n_attribute9
4073              , n_attribute10 = l_explode_detail.n_attribute10
4074              , status_id = l_explode_detail.status_id
4075              , territory_code = l_explode_detail.territory_code
4076              , time_since_new = l_explode_detail.time_since_new
4077              , cycles_since_new = l_explode_detail.cycles_since_new
4078              , time_since_overhaul = l_explode_detail.time_since_overhaul
4079              , cycles_since_overhaul = l_explode_detail.cycles_since_overhaul
4080              , time_since_repair = l_explode_detail.time_since_repair
4081              , cycles_since_repair = l_explode_detail.cycles_since_repair
4082              , time_since_visit = l_explode_detail.time_since_visit
4083              , cycles_since_visit = l_explode_detail.cycles_since_visit
4084              , time_since_mark = l_explode_detail.time_since_mark
4085              , cycles_since_mark = l_explode_detail.cycles_since_mark
4086              , number_of_repairs = l_explode_detail.number_of_repairs
4087              , group_mark_id = l_group_mark_id
4088          WHERE current_organization_id = l_explode_detail.organization_id
4089            AND inventory_item_id = l_explode_detail.inventory_item_id
4090            AND serial_number = l_explode_detail.fm_serial_number;
4091 
4092         -- Copy all Processed serial number into the
4093         -- serial Table.
4094         l_serial_numbers_table(m)  := l_explode_detail.fm_serial_number;
4095 
4096         IF (l_debug = 1) THEN
4097           DEBUG(
4098             'After update mtl_serial_numbers with the attributes: c_attribute1 ' || l_explode_detail.c_attribute1
4099           , 'EXPLODE_DELIVERY_DETAILS'
4100           );
4101         END IF;
4102 
4103         m                          := m + 1;
4104         IF (l_debug = 1) THEN
4105           DEBUG('(1)After Updating Serial Number :'||l_serial_number||' m :'||m,'EXPLODE_DELIVEY_DETAILS');
4106         END IF;
4107       -- update req.
4108       ELSE
4109         l_range_serial        := TRUE;
4110         -- Get the next serial number
4111         --m               := m + 1;
4112         -- Determine the serial number prefix
4113         l_real_serial_prefix  := RTRIM(l_explode_detail.fm_serial_number, '0123456789');
4114         -- Determine the base numeric portion
4115         l_serial_numeric      := TO_NUMBER(SUBSTR(l_explode_detail.fm_serial_number,
4116                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4117         l_serial_numeric_end  := TO_NUMBER(SUBSTR(l_explode_detail.to_serial_number,
4118                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4119 
4120         IF (l_serial_numeric_end - l_serial_numeric) >= p_quantity THEN
4121           l_new_fm_serial      := l_real_serial_prefix ||(l_serial_numeric + l_serial_quantity);
4122           l_current_to_serial  := l_real_serial_prefix ||(l_serial_numeric + l_serial_quantity - 1);
4123           l_rowid              := l_explode_detail.ROWID;
4124 
4125           UPDATE mtl_serial_numbers_temp
4126              SET fm_serial_number = l_new_fm_serial
4127            WHERE ROWID = l_rowid;
4128 
4129 
4130           IF (l_debug = 1) THEN
4131             DEBUG( 'Creating New MSNT Record with fm_serial_number :' ||
4132                     l_explode_detail.fm_serial_number || ', to_serial :' ||
4133                     l_current_to_serial , 'EXPLODE_DELIVERY_DETAILS');
4134           END IF;
4135 
4136           l_succes             :=
4137             inv_trx_util_pub.insert_ser_trx(
4138               p_trx_tmp_id                 => l_explode_detail.transaction_temp_id
4139             , p_user_id                    => fnd_global.user_id
4140             , p_fm_ser_num                 => l_explode_detail.fm_serial_number
4141             , p_to_ser_num                 => l_current_to_serial
4142             , p_serial_attribute_category  => l_explode_detail.serial_attribute_category
4143             , p_orgination_date            => l_explode_detail.origination_date
4144             , p_c_attribute1               => l_explode_detail.c_attribute1
4145             , p_c_attribute2               => l_explode_detail.c_attribute2
4146             , p_c_attribute3               => l_explode_detail.c_attribute3
4147             , p_c_attribute4               => l_explode_detail.c_attribute4
4148             , p_c_attribute5               => l_explode_detail.c_attribute5
4149             , p_c_attribute6               => l_explode_detail.c_attribute6
4150             , p_c_attribute7               => l_explode_detail.c_attribute7
4151             , p_c_attribute8               => l_explode_detail.c_attribute8
4152             , p_c_attribute9               => l_explode_detail.c_attribute9
4153             , p_c_attribute10              => l_explode_detail.c_attribute10
4154             , p_c_attribute11              => l_explode_detail.c_attribute11
4155             , p_c_attribute12              => l_explode_detail.c_attribute12
4156             , p_c_attribute13              => l_explode_detail.c_attribute13
4157             , p_c_attribute14              => l_explode_detail.c_attribute14
4158             , p_c_attribute15              => l_explode_detail.c_attribute15
4159             , p_c_attribute16              => l_explode_detail.c_attribute16
4160             , p_c_attribute17              => l_explode_detail.c_attribute17
4161             , p_c_attribute18              => l_explode_detail.c_attribute18
4162             , p_c_attribute19              => l_explode_detail.c_attribute19
4163             , p_c_attribute20              => l_explode_detail.c_attribute20
4164             , p_d_attribute1               => l_explode_detail.d_attribute1
4165             , p_d_attribute2               => l_explode_detail.d_attribute2
4166             , p_d_attribute3               => l_explode_detail.d_attribute3
4167             , p_d_attribute4               => l_explode_detail.d_attribute4
4168             , p_d_attribute5               => l_explode_detail.d_attribute5
4169             , p_d_attribute6               => l_explode_detail.d_attribute6
4170             , p_d_attribute7               => l_explode_detail.d_attribute7
4171             , p_d_attribute8               => l_explode_detail.d_attribute8
4172             , p_d_attribute9               => l_explode_detail.d_attribute9
4173             , p_d_attribute10              => l_explode_detail.d_attribute10
4174             , p_n_attribute1               => l_explode_detail.n_attribute1
4175             , p_n_attribute2               => l_explode_detail.n_attribute2
4176             , p_n_attribute3               => l_explode_detail.n_attribute3
4177             , p_n_attribute4               => l_explode_detail.n_attribute4
4178             , p_n_attribute5               => l_explode_detail.n_attribute5
4179             , p_n_attribute6               => l_explode_detail.n_attribute6
4180             , p_n_attribute7               => l_explode_detail.n_attribute7
4181             , p_n_attribute8               => l_explode_detail.n_attribute8
4182             , p_n_attribute9               => l_explode_detail.n_attribute9
4183             , p_n_attribute10              => l_explode_detail.n_attribute10
4184             , p_status_id                  => l_explode_detail.status_id
4185             , p_territory_code             => l_explode_detail.territory_code
4186             , p_time_since_new             => l_explode_detail.time_since_new
4187             , p_cycles_since_new           => l_explode_detail.cycles_since_new
4188             , p_time_since_overhaul        => l_explode_detail.time_since_overhaul
4189             , p_cycles_since_overhaul      => l_explode_detail.cycles_since_overhaul
4190             , p_time_since_repair          => l_explode_detail.time_since_repair
4191             , p_cycles_since_repair        => l_explode_detail.cycles_since_repair
4192             , p_time_since_visit           => l_explode_detail.time_since_visit
4193             , p_cycles_since_visit         => l_explode_detail.cycles_since_visit
4194             , p_time_since_mark            => l_explode_detail.time_since_mark
4195             , p_cycles_since_mark          => l_explode_detail.cycles_since_mark
4196             , p_number_of_repairs          => l_explode_detail.number_of_repairs
4197             , x_proc_msg                   => l_proc_msg
4198             );
4199         ELSE
4200           l_serial_quantity    := l_serial_quantity +(l_serial_numeric_end - l_serial_numeric + 1);
4201           l_current_to_serial  := l_explode_detail.to_serial_number;
4202         END IF;
4203 
4204         -- Determine length of numeric portion
4205         l_serial_numeric_len  := LENGTH(SUBSTR(l_explode_detail.fm_serial_number,
4206                                  NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4207 
4208         -- update the serial attributes in mtl_serial_numbers
4209         IF (l_debug = 1) THEN
4210           DEBUG(
4211                'Before update the mtl_serial_numbers with org '
4212             || TO_CHAR(l_explode_detail.organization_id)
4213             || ' item '
4214             || TO_CHAR(l_explode_detail.inventory_item_id)
4215             || ' serial '
4216             || l_explode_detail.fm_serial_number
4217           , 'EXPLODE_DELIVERY_DETAILS'
4218           );
4219         END IF;
4220 
4221         OPEN c_serials(
4222               l_explode_detail.organization_id
4223             , l_explode_detail.inventory_item_id
4224             , l_explode_detail.fm_serial_number
4225             , l_current_to_serial
4226             , LENGTH(l_explode_detail.fm_serial_number)
4227                       );
4228 
4229         -- skip the from serial number
4230         --FETCH c_serials
4231          --INTO l_serial_number;
4232         LOOP
4233           FETCH c_serials
4234            INTO l_serial_number;
4235 
4236           EXIT WHEN c_serials%NOTFOUND;
4237 
4238           -- update the serial attributes in mtl_serial_numbers
4239           UPDATE mtl_serial_numbers
4240              SET serial_attribute_category = l_explode_detail.serial_attribute_category
4241                , origination_date = l_explode_detail.origination_date
4242                , c_attribute1 = l_explode_detail.c_attribute1
4243                , c_attribute2 = l_explode_detail.c_attribute2
4244                , c_attribute3 = l_explode_detail.c_attribute3
4245                , c_attribute4 = l_explode_detail.c_attribute4
4246                , c_attribute5 = l_explode_detail.c_attribute5
4247                , c_attribute6 = l_explode_detail.c_attribute6
4248                , c_attribute7 = l_explode_detail.c_attribute7
4249                , c_attribute8 = l_explode_detail.c_attribute8
4250                , c_attribute9 = l_explode_detail.c_attribute9
4251                , c_attribute10 = l_explode_detail.c_attribute10
4252                , c_attribute11 = l_explode_detail.c_attribute11
4253                , c_attribute12 = l_explode_detail.c_attribute12
4254                , c_attribute13 = l_explode_detail.c_attribute13
4255                , c_attribute14 = l_explode_detail.c_attribute14
4256                , c_attribute15 = l_explode_detail.c_attribute15
4257                , c_attribute16 = l_explode_detail.c_attribute16
4258                , c_attribute17 = l_explode_detail.c_attribute17
4259                , c_attribute18 = l_explode_detail.c_attribute18
4260                , c_attribute19 = l_explode_detail.c_attribute19
4261                , c_attribute20 = l_explode_detail.c_attribute20
4262                , d_attribute1 = l_explode_detail.d_attribute1
4263                , d_attribute2 = l_explode_detail.d_attribute2
4264                , d_attribute3 = l_explode_detail.d_attribute3
4265                , d_attribute4 = l_explode_detail.d_attribute4
4266                , d_attribute5 = l_explode_detail.d_attribute5
4267                , d_attribute6 = l_explode_detail.d_attribute6
4268                , d_attribute7 = l_explode_detail.d_attribute7
4269                , d_attribute8 = l_explode_detail.d_attribute8
4270                , d_attribute9 = l_explode_detail.d_attribute9
4271                , d_attribute10 = l_explode_detail.d_attribute10
4272                , n_attribute1 = l_explode_detail.n_attribute1
4273                , n_attribute2 = l_explode_detail.n_attribute2
4274                , n_attribute3 = l_explode_detail.n_attribute3
4275                , n_attribute4 = l_explode_detail.n_attribute4
4276                , n_attribute5 = l_explode_detail.n_attribute5
4277                , n_attribute6 = l_explode_detail.n_attribute6
4278                , n_attribute7 = l_explode_detail.n_attribute7
4279                , n_attribute8 = l_explode_detail.n_attribute8
4280                , n_attribute9 = l_explode_detail.n_attribute9
4281                , n_attribute10 = l_explode_detail.n_attribute10
4282                , status_id = l_explode_detail.status_id
4283                , territory_code = l_explode_detail.territory_code
4284                , time_since_new = l_explode_detail.time_since_new
4285                , cycles_since_new = l_explode_detail.cycles_since_new
4286                , time_since_overhaul = l_explode_detail.time_since_overhaul
4287                , cycles_since_overhaul = l_explode_detail.cycles_since_overhaul
4288                , time_since_repair = l_explode_detail.time_since_repair
4289                , cycles_since_repair = l_explode_detail.cycles_since_repair
4290                , time_since_visit = l_explode_detail.time_since_visit
4291                , cycles_since_visit = l_explode_detail.cycles_since_visit
4292                , time_since_mark = l_explode_detail.time_since_mark
4293                , cycles_since_mark = l_explode_detail.cycles_since_mark
4294                , number_of_repairs = l_explode_detail.number_of_repairs
4295                , group_mark_id = l_group_mark_id
4296            WHERE current_organization_id = l_explode_detail.organization_id
4297              AND inventory_item_id = l_explode_detail.inventory_item_id
4298              AND serial_number = l_serial_number;
4299 
4300           -- Copy all Processed serial number into the
4301           -- serial Table.
4302           l_serial_numbers_table(m)  := l_serial_number;
4303           IF (l_debug = 1) THEN
4304             DEBUG('(2)After Updating Serial Number :'||l_serial_number||' m :'||m,'EXPLODE_DELIVEY_DETAILS');
4305           END IF;
4306 
4307           IF (l_debug = 1) THEN
4308             DEBUG('m=' || m || ' l_serial_number= ' || l_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4309             DEBUG(' p_quantity= ' || p_quantity, 'EXPLODE_DELIVERY_DETAILS');
4310           END IF;
4311 
4312           m                          := m + 1;
4313 
4314           IF (m > p_quantity) THEN
4315             IF (l_debug = 1) THEN
4316               DEBUG('(1) x_transaction_temp_id= ' || x_transaction_temp_id, 'EXPLODE_DELIVERY_DETAILS');
4317             END IF;
4318 
4319             EXIT;
4320           END IF;
4321         END LOOP;
4322 
4323         CLOSE c_serials;
4324       END IF;
4325 
4326       IF (l_debug = 1) THEN
4327         DEBUG('(2)m else =' || m || ' l_serial_number= ' || l_serial_number, 'EXPLODE_DELIVERY_DETAILS');
4328         DEBUG('(2)p_quantity= ' || p_quantity, 'EXPLODE_DELIVERY_DETAILS');
4329       END IF;
4330 
4331       IF (m > p_quantity) THEN
4332         IF (l_debug = 1) THEN
4333           DEBUG('(2) x_transaction_temp_id= ' || x_transaction_temp_id, 'EXPLODE_DELIVERY_DETAILS');
4334         END IF;
4335 
4336         EXIT;
4337       END IF;
4338     END LOOP;
4339 
4340 
4341     /***************************
4342     * Update all the Processed serial numbers
4343     * with the new already generated transaction
4344     * temp id
4345     ****************************/
4346     l_running_quantity  := l_serial_numbers_table.COUNT;
4347 
4348     FOR j IN 1 .. l_running_quantity LOOP
4349       UPDATE mtl_serial_numbers_temp
4350          SET transaction_temp_id = x_transaction_temp_id
4351        WHERE transaction_temp_id = p_transaction_temp_id
4352          AND fm_serial_number = l_serial_numbers_table(j);
4353     END LOOP;
4354 
4355     CLOSE c_explode_detail;
4356   ELSIF(p_serial_number_control_code IN(2, 5)) THEN
4357     l_serial_quantity   := p_quantity;
4358 
4359     /******************
4360     ** Bug No 3678190
4361     ** Set the out transaction temp value to the current transaction temp value.
4362     ******************/
4363     x_transaction_temp_id := p_transaction_temp_id;
4364 
4365     IF (l_debug = 1) THEN
4366       DEBUG('l_serial_quantity= ' || l_serial_quantity, 'EXPLODE_DELIVERY_DETAILS');
4367     END IF;
4368 
4369     SELECT inventory_item_id
4370       INTO l_inventory_item_id
4371       FROM mtl_reservations
4372      WHERE reservation_id = p_reservation_id;
4373 
4374     IF (l_debug = 1) THEN
4375        DEBUG('l_inventory_item_id: ' || l_inventory_item_id, 'EXPLODE_DELIVERY_DETAILS');
4376     END IF;
4377     -- added for patchset-i end_item_unit_validation
4378     IF (g_cross_project_allowed = 'N') THEN
4379        IF (g_cross_unit_allowed = 'N') THEN
4380         -- get the end item unit number
4381           SELECT oel.end_item_unit_number
4382             INTO l_end_item_unit_number
4383             FROM oe_order_lines_all oel, wsh_delivery_details_ob_grp_v wdd
4384            WHERE wdd.delivery_detail_id = p_delivery_detail_id
4385              AND wdd.source_header_id = oel.header_id
4386              AND wdd.source_line_id = oel.line_id;
4387 
4388           IF (l_debug = 1) THEN
4389             DEBUG('EIUN=' || l_end_item_unit_number, 'EXPLODE_DELIVERY_DETAILS');
4390           END IF;
4391 
4392           OPEN c_lpn_serial_e(p_organization_id, l_inventory_item_id, p_lpn_id, l_end_item_unit_number);
4393           n  := 1;
4394 
4395           LOOP
4396             FETCH c_lpn_serial_e
4397              INTO l_serial_numbers_table(n),
4398                   l_cur_group_mark_id,
4399                   l_cur_reservation_id;
4400             EXIT WHEN c_lpn_serial_e%NOTFOUND;
4401 
4402             IF (l_debug = 1) THEN
4403                DEBUG('c_lpn_serial_e Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4404                DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4405                DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4406                DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4407             END IF;
4408             IF n >= l_serial_quantity THEN
4409               EXIT;
4410             END IF;
4411 
4412             n  := n + 1;
4413           END LOOP;
4414 
4415           CLOSE c_lpn_serial_e;
4416        ELSE
4417 
4418           OPEN c_lpn_serial(p_organization_id, l_inventory_item_id, p_lpn_id);
4419           n  := 1;
4420 
4421           LOOP
4422             FETCH c_lpn_serial --INTO l_serial_numbers_table(n);
4423              INTO l_serial_numbers_table(n),
4424                   l_cur_group_mark_id,
4425                   l_cur_reservation_id;
4426 
4427             EXIT WHEN c_lpn_serial%NOTFOUND;
4428 
4429             IF (l_debug = 1) THEN
4430                DEBUG('c_lpn_serial Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4431                DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4432                DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4433                DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4434             END IF;
4435             IF n >= l_serial_quantity THEN
4436               EXIT;
4437             END IF;
4438 
4439             n  := n + 1;
4440           END LOOP;
4441 
4442           CLOSE c_lpn_serial;
4443        END IF;
4444     END IF;
4445 
4446     OPEN c_lpn_serial(p_organization_id, l_inventory_item_id, p_lpn_id);
4447     n                   := 1;
4448 
4449     LOOP
4450       FETCH c_lpn_serial --INTO l_serial_numbers_table(n);
4451        INTO l_serial_numbers_table(n),
4452             l_cur_group_mark_id,
4453             l_cur_reservation_id;
4454 
4455       EXIT WHEN c_lpn_serial%NOTFOUND;
4456 
4457        IF (l_debug = 1) THEN
4458           DEBUG('c_lpn_serial Cursor (n): ' || n, 'EXPLODE_DELIVERY_DETAILS');
4459           DEBUG('l_serial_numbers_table(n): ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4460           DEBUG('l_cur_group_mark_id: ' || l_cur_group_mark_id,'EXPLODE_DELIVERY_DETAILS');
4461           DEBUG('l_cur_reservation_id: ' || l_cur_reservation_id, 'EXPLODE_DELIVERY_DETAILS');
4462        END IF;
4463 
4464 
4465       IF n >= l_serial_quantity THEN
4466         EXIT;
4467       END IF;
4468 
4469       n  := n + 1;
4470     END LOOP;
4471 
4472     CLOSE c_lpn_serial;
4473     n                   := 1;
4474     l_running_quantity  := l_serial_numbers_table.COUNT;
4475 
4476 
4477     WHILE(l_running_quantity >= 1)
4478     LOOP
4479       IF (l_debug = 1) THEN
4480         DEBUG('Marking serial number ' || l_serial_numbers_table(n), 'EXPLODE_DELIVERY_DETAILS');
4481       END IF;
4482 
4483      /* 5506223: Because of serial reservations, the following API call to
4484       *      mark the Serial numbers will not work. It does not allow already
4485       *      reserved (which are already marked) serials to be remarked with the
4486       *      new mark id of Transaction_temp_id of MSNT
4487       serial_check.inv_mark_serial(
4488         from_serial_number           => l_serial_numbers_table(n)
4489       , to_serial_number             => l_serial_numbers_table(n)
4490       , item_id                      => l_inventory_item_id
4491       , org_id                       => p_organization_id
4492       , hdr_id                       => l_group_mark_id
4493       , temp_id                      => NULL
4494       , lot_temp_id                  => NULL
4495       , success                      => l_succes
4496       );
4497 
4498       IF l_succes < 1 THEN
4499         IF (l_debug = 1) THEN
4500           DEBUG('inv_mark_serial ended with errors (E) ', 'EXPLODE_DELIVERY_DETAILS');
4501         END IF;
4502 
4503         RAISE fnd_api.g_exc_error;
4504       END IF; */
4505 
4506       /* 5506223: Because of the above, using direct update to MSN to set the
4507        *          group_mark_id */
4508 
4509       BEGIN
4510          UPDATE mtl_serial_numbers
4511             SET group_mark_id     = l_group_mark_id
4512           WHERE inventory_item_id = l_inventory_item_id
4513             AND serial_number     = l_serial_numbers_table(n)
4514             AND current_organization_id = p_organization_id;
4515 
4516          IF SQL%NOTFOUND THEN
4517             IF (l_debug = 1) THEN
4518               DEBUG( 'Error finding serial to mark : '|| l_serial_numbers_table(n) ,
4519                      'EXPLODE_DELIVERY_DETAILS');
4520               DEBUG( 'l_inventory_item_id : '|| l_inventory_item_id ,
4521                      'EXPLODE_DELIVERY_DETAILS');
4522               DEBUG( 'p_organization_id : '|| p_organization_id ,
4523                      'EXPLODE_DELIVERY_DETAILS');
4524             END IF;
4525 
4526             RAISE fnd_api.g_exc_error;
4527          END IF;
4528       END ;
4529       IF (l_debug = 1) THEN
4530         DEBUG( 'inserting serials ' || l_serial_numbers_table(n) || ' into msnt WITH temp_id ='
4531               || p_transaction_temp_id , 'EXPLODE_DELIVERY_DETAILS');
4532       END IF;
4533 
4534       l_succes            :=
4535         inv_trx_util_pub.insert_ser_trx(
4536           p_trx_tmp_id                 => p_transaction_temp_id
4537         , p_user_id                    => fnd_global.user_id
4538         , p_fm_ser_num                 => l_serial_numbers_table(n)
4539         , p_to_ser_num                 => l_serial_numbers_table(n)
4540         , x_proc_msg                   => l_proc_msg
4541         );
4542 
4543       IF (l_succes < 0) THEN
4544         IF (l_debug = 1) THEN
4545           DEBUG('insert_ser_trx ended with errors ' || l_proc_msg, 'EXPLODE_DELIVERY_DETAILS');
4546         END IF;
4547 
4548         RAISE fnd_api.g_exc_error;
4549       END IF;
4550 
4551       IF (l_debug = 1) THEN
4552         DEBUG('l_running_quantity (while_loop)= ' || l_running_quantity, 'EXPLODE_DELIVERY_DETAILS');
4553       END IF;
4554 
4555       l_running_quantity  := l_running_quantity - 1;
4556       n                   := n + 1;
4557     END LOOP;
4558   END IF; -- Serial Control Code If
4559 EXCEPTION
4560   WHEN fnd_api.g_exc_error THEN
4561     x_return_status  := fnd_api.g_ret_sts_error;
4562     ROLLBACK;
4563     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4564   WHEN fnd_api.g_exc_unexpected_error THEN
4565     x_return_status  := fnd_api.g_ret_sts_unexp_error;
4566     ROLLBACK;
4567     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4568   WHEN OTHERS THEN
4569     x_return_status  := wsh_util_core.g_ret_sts_unexp_error;
4570     wsh_util_core.default_handler('WSH_DELIVERY_DETAILS_ACTIONS.EXPLODE_DELIVERY_DETAILS');
4571 END explode_delivery_details;
4572 
4573 
4574 /* Function overship_staged_lines
4575 splits a staged line to overship the quantity
4576 and returns the delivery details of the overshipped Lines
4577 Added bug4128854*/
4578 
4579 FUNCTION Overship_Staged_Lines(
4580 p_source_header_id IN NUMBER,
4581 p_source_line_id IN NUMBER,
4582 p_lpn_id IN NUMBER,
4583 p_mso_id IN NUMBER,
4584 p_organization_id IN NUMBER,
4585 p_cont_instance_id IN NUMBER,
4586 p_dock_door_id IN NUMBER,
4587 x_return_status   OUT NOCOPY    VARCHAR2,
4588 x_msg_count       OUT NOCOPY    NUMBER,
4589 x_msg_data        OUT NOCOPY    VARCHAR2)
4590 RETURN DELIVERY_DETAIL_TAB IS
4591 
4592 --Local Variables
4593 l_reservation_record         inv_reservation_global.mtl_reservation_rec_type;
4594 l_delivery_detail_tab        DELIVERY_DETAIL_TAB;
4595 l_mtl_reservation_tbl        inv_reservation_global.mtl_reservation_tbl_type;
4596 l_mtl_reservation_tbl_count  NUMBER;
4597 l_detail_tab                 WSH_UTIL_CORE.ID_TAB_TYPE;
4598 l_return_status              VARCHAR2(1);
4599 l_msg_data                   VARCHAR2(3000);
4600 l_msg_count                  NUMBER;
4601 l_error_code                 VARCHAR2(20000);
4602 l_debug                      NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
4603 l_transaction_temp_id        NUMBER;
4604 l_shipping_attr              WSH_DELIVERY_DETAILS_PUB.ChangedAttributeTabType;
4605 l_shipping_attr_tab          wsh_interface.changedattributetabtype;
4606 l_delivery_detail_id         NUMBER;
4607 l_picked_qty                 NUMBER;
4608 l_shipped_qty                NUMBER;
4609 l_primary_uom_code           VARCHAR2(3);
4610 l_lot_control_code           NUMBER;
4611 l_serial_number_control_code NUMBER;
4612 l_commit                     VARCHAR2(1) := fnd_api.g_false;
4613 l_new_delivery_detail_id     NUMBER;
4614 l_dummy_num_var              NUMBER;
4615 l_init_msg_list              VARCHAR2(1) := fnd_api.g_false;
4616 l_trip_id                    NUMBER;
4617 BEGIN
4618        x_return_status  := fnd_api.g_ret_sts_success;
4619       /*Query Reservations for LPN to be staged*/
4620 
4621       IF (l_debug = 1) THEN
4622          DEBUG('Inside Overship_Staged_Lines ', 'Overship Staged Lines');
4623          DEBUG('Source Header: '||p_source_header_id || ' : Line : ' ||
4624                                   p_source_line_id, 'Overship Staged Lines');
4625          DEBUG('p_lpn_id: '||p_lpn_id, 'Overship Staged Lines');
4626          DEBUG('p_mso_id: '||p_mso_id, 'Overship Staged Lines');
4627          DEBUG('p_organization_id: '||p_organization_id, 'Overship Staged Lines');
4628          DEBUG('p_cont_instance_id: '||p_cont_instance_id, 'Overship Staged Lines');
4629          DEBUG('p_dock_door_id: '||p_dock_door_id, 'Overship Staged Lines');
4630       END IF;
4631 
4632 
4633       l_reservation_record.demand_source_header_id  := p_mso_id;
4634       l_reservation_record.demand_source_line_id    := p_source_line_id;
4635       l_reservation_record.lpn_id                   := p_lpn_id;
4636       l_reservation_record.supply_source_type_id    := inv_reservation_global.g_source_type_inv;
4637 
4638       IF (l_debug = 1) THEN
4639         DEBUG('Before call to reservation for LPN ID'||p_lpn_id, 'Overship Staged Lines');
4640       END IF;
4641 
4642       inv_reservation_pub.query_reservation(
4643           p_api_version_number         => 1.0
4644         , p_init_msg_lst               => fnd_api.g_false
4645         , x_return_status              => l_return_status
4646         , x_msg_count                  => l_msg_count
4647         , x_msg_data                   => l_msg_data
4648         , p_query_input                => l_reservation_record
4649         , p_lock_records               => fnd_api.g_false
4650         , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
4651         , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
4652         , x_error_code                 => l_error_code
4653         );
4654 
4655       IF (l_debug = 1) THEN
4656         DEBUG('After call to reservation for LPN ID'||p_lpn_id ||
4657               'records retrived'||l_mtl_reservation_tbl_count, 'Overship Staged Lines');
4658       END IF;
4659 
4660 
4661       IF l_return_status = fnd_api.g_ret_sts_error THEN
4662         IF (l_debug = 1) THEN
4663             DEBUG('Validation error during query of Reservations ' || p_source_header_id ||
4664                   ' ' || p_source_line_id, 'Overship Staged Lines');
4665         END IF;
4666 
4667         RAISE fnd_api.g_exc_error;
4668       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4669         IF (l_debug = 1) THEN
4670             DEBUG('Unexpected error during query of Reservations ' || p_source_header_id ||
4671                   ' ' || p_source_line_id, 'Overship Staged Lines');
4672         END IF;
4673 
4674         RAISE fnd_api.g_exc_unexpected_error;
4675       END IF;
4676 
4677       /*Now we have all reservations for the LPN for this sales order and line
4678         which donot have any open WDDs left, This means we need to split a staged WDD*/
4679 
4680       FOR i in 1..l_mtl_reservation_tbl_count LOOP
4681 
4682       /*Find the parent delivery detail to split from (since all details are staged already)*/
4683           SELECT  wds.transaction_temp_id
4684                 , wdd.delivery_detail_id
4685                 , wdd.picked_quantity
4686                 , wdd.shipped_quantity
4687                 , msi.primary_uom_code
4688                 , msi.lot_control_code
4689                 , msi.serial_number_control_code
4690           INTO  l_transaction_temp_id
4691                 , l_delivery_detail_id
4692                 , l_picked_qty
4693                 , l_shipped_qty
4694                 , l_primary_uom_code
4695                 , l_lot_control_code
4696                 , l_serial_number_control_code
4697           FROM wms_direct_ship_temp wds,
4698                wsh_delivery_details wdd,
4699                mtl_system_items msi
4700           WHERE wds.organization_id = p_organization_id
4701             AND wds.lpn_id = p_lpn_id
4702             AND wds.order_header_id = p_source_header_id
4703             AND wds.order_line_id = p_source_line_id
4704             AND wdd.organization_id = p_organization_id
4705             AND wdd.source_header_id = wds.order_header_id
4706             AND wdd.source_line_id = wds.order_line_id
4707             AND msi.organization_id = p_organization_id
4708             AND msi.inventory_item_id = wdd.inventory_item_id
4709             AND wdd.released_status = 'Y'
4710             AND wdd.container_flag = 'N'
4711             --AND wdd.split_from_delivery_detail_id is null
4712             AND rownum =1;
4713 
4714      /* Now we have the delivery_Detail to split so first update the quantity from
4715         Reservation on the wdd and then split from it*/
4716 
4717       DEBUG('Before calling Update_shipping_attributes to update qty'||l_picked_qty, 'Overship Staged Lines');
4718       l_shipping_attr_tab.DELETE;  --Added delete to prevent usage from previous loop bug4128854
4719       l_shipping_attr_tab(1).delivery_detail_id := l_delivery_detail_id;
4720       l_shipping_attr_tab(1).action_flag      := 'U';
4721       l_shipping_attr_tab(1).released_status  := 'R';
4722 
4723       wsh_interface.update_shipping_attributes(p_source_code => 'INV',
4724        p_changed_attributes         => l_shipping_attr_tab
4725       ,x_return_status              => l_return_status);
4726 
4727       l_shipping_attr_tab.DELETE;
4728       l_shipping_attr_tab(1).picked_quantity    := l_picked_qty + l_mtl_reservation_tbl(i).reservation_quantity;
4729       l_shipping_attr_tab(1).shipped_quantity    := l_picked_qty + l_mtl_reservation_tbl(i).reservation_quantity;
4730       l_shipping_attr_tab(1).delivery_detail_id := l_delivery_detail_id;
4731       l_shipping_attr_tab(1).action_flag      := 'U';
4732       l_shipping_attr_tab(1).released_status  := 'Y';
4733       DEBUG('Before calling Update_shipping_attributes to update qty'||
4734              l_shipping_attr_tab(1).shipped_quantity , 'Overship Staged Lines');
4735 
4736       wsh_interface.update_shipping_attributes(p_source_code => 'INV',
4737        p_changed_attributes         => l_shipping_attr_tab
4738       ,x_return_status              => l_return_status);
4739 
4740       IF (l_debug = 1) THEN
4741         DEBUG('After calling Update_shipping_attributes', 'Overship Staged Lines');
4742       END IF;
4743 
4744       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4745         IF (l_debug = 1) THEN
4746           DEBUG('Return error from update shipping attributes 2', 'Overship Staged Lines');
4747         END IF;
4748 
4749         RAISE fnd_api.g_exc_error;
4750       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4751         IF (l_debug = 1) THEN
4752           DEBUG('Return unexpected error from update shipping attributes', 'Overship Staged Lines');
4753         END IF;
4754 
4755         RAISE fnd_api.g_exc_unexpected_error;
4756       ELSIF l_return_status = 'S' THEN
4757         IF (l_debug = 1) THEN
4758           DEBUG('Shipping attributes updated successfully','Overship Staged Lines');
4759         END IF;
4760       END IF;
4761 
4762      /*At this point we need to split from this delivery detail*/
4763       DEBUG('Before Split Line'||l_delivery_Detail_id,'Overship Staged Lines');
4764 
4765       WSH_DELIVERY_DETAILS_PUB.split_line
4766         (p_api_version    => 1.0,
4767          p_init_msg_list  => fnd_api.g_false,
4768          p_commit         => fnd_api.g_false,
4769          x_return_status  => l_return_status,
4770          x_msg_count      => l_msg_count,
4771          x_msg_data       => l_msg_data,
4772          p_from_detail_id => l_delivery_detail_id,
4773          x_new_detail_id  => l_new_delivery_detail_id,
4774          x_split_quantity => l_mtl_reservation_tbl(i).reservation_quantity,
4775          x_split_quantity2=> l_dummy_num_var);
4776 
4777        DEBUG('After Split Line'||l_new_delivery_Detail_id,'Overship Staged Lines');
4778 
4779       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4780         IF (l_debug = 1) THEN
4781             DEBUG('Return error from split_delivery_details ', 'Overship Staged Lines');
4782         END IF;
4783 
4784         RAISE fnd_api.g_exc_error;
4785       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4786         IF (l_debug = 1) THEN
4787             DEBUG('Return unexpected error from split_delivery_details', 'Overship Staged Lines');
4788         END IF;
4789 
4790         RAISE fnd_api.g_exc_unexpected_error;
4791       END IF;
4792 
4793              /*Unassign from old container*/
4794 
4795       WSH_DELIVERY_DETAILS_ACTIONS.Unassign_Detail_from_Cont(
4796         P_DETAIL_ID     => l_new_delivery_detail_id,
4797         X_RETURN_STATUS => l_return_status);
4798 
4799         DEBUG('After Unassign'|| l_return_status, 'Overship Staged Lines');
4800 
4801       IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
4802          IF (l_debug = 1) THEN
4803              DEBUG('Error Unassign_Detail_from_Cont'|| l_return_status, 'Overship Staged Lines');
4804          END IF;
4805          FND_MESSAGE.SET_NAME('INV', 'INV_UNASSIGN_DEL_FAILURE');
4806          FND_MSG_PUB.ADD;
4807          RAISE FND_API.G_EXC_ERROR;
4808       END IF;
4809 
4810 
4811 
4812       /*Now update the new delivery_Detail with correct attributes*/
4813       l_shipping_attr_tab.DELETE;
4814       l_shipping_attr_tab(1).delivery_detail_id   := l_new_delivery_detail_id;
4815       l_shipping_attr_tab(1).lot_number           := l_mtl_reservation_tbl(i).lot_number;
4816       l_shipping_attr_tab(1).revision             := l_mtl_reservation_tbl(i).revision;
4817       l_shipping_attr_tab(1).subinventory         := l_mtl_reservation_tbl(i).subinventory_code;
4818       l_shipping_attr_tab(1).locator_id           := l_mtl_reservation_tbl(i).locator_id;
4819       l_shipping_attr_tab(1).ship_from_org_id     := l_mtl_reservation_tbl(i).organization_id;
4820       l_shipping_attr_tab(1).transfer_lpn_id      := l_mtl_reservation_tbl(i).lpn_id;
4821       l_shipping_attr_tab(1).picked_quantity      := l_mtl_reservation_tbl(i).reservation_quantity;
4822       l_shipping_attr_tab(1).shipped_quantity     := l_mtl_reservation_tbl(i).reservation_quantity;
4823       l_shipping_attr_tab(1).action_flag      := 'U';
4824       l_shipping_attr_tab(1).released_status  := 'Y';
4825 
4826       IF (l_debug = 1) THEN
4827          DEBUG('l_shipping_attr_tab(1).delivery_detail_id   '||
4828                 l_shipping_attr_tab(1).delivery_detail_id   ,'Overship Staged Lines');
4829          DEBUG('l_shipping_attr_tab(1).lot_number           '||
4830                 l_shipping_attr_tab(1).lot_number           ,'Overship Staged Lines');
4831          DEBUG('l_shipping_attr_tab(1).revision             '||
4832                 l_shipping_attr_tab(1).revision             ,'Overship Staged Lines');
4833          DEBUG('l_shipping_attr_tab(1).subinventory         '||
4834                 l_shipping_attr_tab(1).subinventory         ,'Overship Staged Lines');
4835          DEBUG('l_shipping_attr_tab(1).locator_id           '||
4836                 l_shipping_attr_tab(1).locator_id           ,'Overship Staged Lines');
4837          DEBUG('l_shipping_attr_tab(1).ship_from_org_id     '||
4838                 l_shipping_attr_tab(1).ship_from_org_id     ,'Overship Staged Lines');
4839          DEBUG('l_shipping_attr_tab(1).transfer_lpn_id      '||
4840                 l_shipping_attr_tab(1).transfer_lpn_id      ,'Overship Staged Lines');
4841          DEBUG('l_shipping_attr_tab(1).picked_quantity      '||
4842                 l_shipping_attr_tab(1).picked_quantity      ,'Overship Staged Lines');
4843          DEBUG('l_shipping_attr_tab(1).shipped_quantity     '||
4844                 l_shipping_attr_tab(1).shipped_quantity     ,'Overship Staged Lines');
4845          DEBUG('l_shipping_attr_tab(1).action_flag      '||
4846                 l_shipping_attr_tab(1).action_flag      ,'Overship Staged Lines');
4847          DEBUG('l_shipping_attr_tab(1).released_status  '||
4848                 l_shipping_attr_tab(1).released_status  ,'Overship Staged Lines');
4849       END IF;
4850       /*Verify the Action Flag*/
4851 
4852       wsh_interface.update_shipping_attributes(
4853         p_source_code                => 'INV',
4854         p_changed_attributes         => l_shipping_attr_tab,
4855         x_return_status              => l_return_status);
4856 
4857       IF (l_debug = 1) THEN
4858         DEBUG('After update shipping attributes', 'Overship Staged Lines');
4859       END IF;
4860 
4861       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4862         IF (l_debug = 1) THEN
4863             DEBUG('Return error from update shipping attributes', 'Overship Staged Lines');
4864         END IF;
4865 
4866         RAISE fnd_api.g_exc_error;
4867       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4868         IF (l_debug = 1) THEN
4869             DEBUG('Return unexpected error from update shipping attributes','Overship Staged Lines');
4870         END IF;
4871 
4872         RAISE fnd_api.g_exc_unexpected_error;
4873       END IF;
4874 
4875       SELECT wds.ORGANIZATION_ID
4876          ,WDS.DOCK_DOOR_ID
4877          ,WDS.LPN_ID
4878          ,WDS.ORDER_HEADER_ID
4879          ,WDS.ORDER_LINE_ID
4880          ,WDS.LINE_ITEM_ID
4881          ,WDS.TRANSACTION_TEMP_ID
4882          ,WDD.DELIVERY_DETAIL_ID
4883          ,DECODE(WDD.REQUESTED_QUANTITY_UOM,MSI.PRIMARY_UOM_CODE
4884                                             ,WDD.REQUESTED_QUANTITY
4885                                             ,GREATEST(INV_CONVERT.INV_UM_CONVERT(
4886                                                              null
4887                                                             ,null
4888                                                             ,WDD.REQUESTED_QUANTITY
4889                                                             ,WDD.REQUESTED_QUANTITY_UOM
4890                                                             ,MSI.PRIMARY_UOM_CODE
4891                                                             ,null
4892                                                            ,null),0)) REQUESTED_QUANTITY
4893         ,MSI.PRIMARY_UOM_CODE
4894         ,MSI.LOT_CONTROL_CODE
4895         ,MSI.SERIAL_NUMBER_CONTROL_CODE
4896     INTO l_delivery_detail_tab(i).ORGANIZATION_ID
4897          ,l_delivery_detail_tab(i).DOCK_DOOR_ID
4898          ,l_delivery_detail_tab(i).LPN_ID
4899          ,l_delivery_detail_tab(i).ORDER_HEADER_ID
4900          ,l_delivery_detail_tab(i).ORDER_LINE_ID
4901          ,l_delivery_detail_tab(i).LINE_ITEM_ID
4902          ,l_delivery_detail_tab(i).TRANSACTION_TEMP_ID
4903          ,l_delivery_detail_tab(i).DELIVERY_DETAIL_ID
4904          ,l_delivery_detail_tab(i).REQUESTED_QUANTITY
4905          ,l_delivery_detail_tab(i).PRIMARY_UOM_CODE
4906          ,l_delivery_detail_tab(i).LOT_CONTROL_CODE
4907          ,l_delivery_detail_tab(i).SERIAL_NUMBER_CONTROL_CODE
4908     FROM WMS_DIRECT_SHIP_TEMP WDS,
4909          WSH_DELIVERY_DETAILS WDD,
4910          MTL_SYSTEM_ITEMS MSI
4911     WHERE WDS.ORGANIZATION_ID           = p_organization_id
4912        AND WDS.lpn_id                   = p_lpn_id
4913        AND WDS.ORDER_HEADER_ID          = p_source_header_id
4914        AND WDS.ORDER_LINE_ID            = p_source_line_id
4915        AND WDD.ORGANIZATION_ID          = p_organization_id
4916        AND WDD.SOURCE_HEADER_ID         = WDS.ORDER_HEADER_ID
4917        AND WDD.SOURCE_LINE_ID           = WDS.ORDER_LINE_ID
4918        AND MSI.ORGANIZATION_ID          = p_organization_id
4919        AND MSI.INVENTORY_ITEM_ID        = WDD.INVENTORY_ITEM_ID
4920        AND WDD.delivery_detail_id       = l_new_delivery_detail_id
4921        AND rownum = 1
4922     ORDER BY WDS.LINE_ITEM_ID;
4923     END LOOP;
4924     RETURN l_delivery_detail_tab;
4925 END overship_staged_lines;
4926 
4927 PROCEDURE stage_lpns(
4928   x_return_status   OUT NOCOPY    VARCHAR2
4929 , x_msg_count       OUT NOCOPY    NUMBER
4930 , x_msg_data        OUT NOCOPY    VARCHAR2
4931 , p_group_id        IN            NUMBER
4932 , p_organization_id IN            NUMBER
4933 , p_dock_door_id    IN            NUMBER
4934 ) IS
4935   CURSOR outer_lpn IS
4936     SELECT DISTINCT lpn_id
4937                FROM wms_direct_ship_temp
4938               WHERE GROUP_ID = p_group_id
4939                 AND organization_id = p_organization_id
4940                 AND dock_door_id = p_dock_door_id;
4941 
4942   CURSOR inner_lpn(p_lpn_id NUMBER) IS
4943     SELECT lpn_id
4944       FROM wms_license_plate_numbers
4945      WHERE outermost_lpn_id = p_lpn_id;
4946 
4947   CURSOR stage_lines(p_lpn_id NUMBER) IS
4948     SELECT wds.order_header_id
4949          , wds.order_line_id
4950          , sub.reservable_type
4951          , msi.reservable_type
4952          , msi.lot_control_code
4953          , msi.serial_number_control_code
4954       FROM wms_direct_ship_temp wds
4955          , wms_license_plate_numbers wlpn
4956          , mtl_secondary_inventories sub
4957          , mtl_system_items msi
4958      WHERE wds.GROUP_ID = p_group_id
4959        AND wds.organization_id = p_organization_id
4960        AND wds.dock_door_id = p_dock_door_id
4961        AND wds.lpn_id = p_lpn_id
4962        AND wlpn.lpn_id = wds.lpn_id
4963        AND sub.organization_id = wds.organization_id
4964        AND wlpn.subinventory_code = sub.secondary_inventory_name
4965        AND msi.organization_id = wds.organization_id
4966        AND msi.inventory_item_id = wds.line_item_id;
4967 
4968   CURSOR loaded_trips(p_organization_id NUMBER, p_dock_door_id NUMBER) IS
4969     SELECT DISTINCT trip_id
4970                FROM wms_shipping_transaction_temp
4971               WHERE organization_id = p_organization_id
4972                 AND dock_door_id = p_dock_door_id
4973                 AND dock_appoint_flag = 'N'
4974                 AND direct_ship_flag = 'Y';
4975 
4976   CURSOR trip_for_delivery(p_organization_id NUMBER, p_dock_door_id NUMBER) IS
4977     SELECT DISTINCT delivery_id
4978                FROM wms_shipping_transaction_temp
4979               WHERE organization_id = p_organization_id
4980                 AND dock_door_id = p_dock_door_id
4981                 AND NVL(trip_id, 0) = 0
4982                 AND direct_ship_flag = 'Y';
4983 
4984   CURSOR stage_delivery_detail(p_organization_id NUMBER, p_lpn_id NUMBER) IS
4985     SELECT wdd.delivery_detail_id
4986       FROM wsh_delivery_details_ob_grp_v wdd
4987          , wms_direct_ship_temp wds
4988      WHERE wds.organization_id = p_organization_id
4989        AND wds.lpn_id = p_lpn_id
4990        AND wdd.source_header_id = wds.order_header_id
4991        AND wdd.source_line_id = wds.order_line_id
4992        AND wdd.released_status IN('R', 'B')
4993        AND wdd.picked_quantity > 0;
4994 
4995   /* Bug:2463967 */
4996   CURSOR delete_details(p_outermost_lpn_id NUMBER) IS
4997     SELECT delivery_detail_id
4998       FROM wsh_delivery_details_ob_grp_v wdd
4999          , wms_license_plate_numbers lpn
5000      WHERE lpn.outermost_lpn_id = p_outermost_lpn_id
5001        AND lpn.lpn_id = wdd.lpn_id
5002        AND wdd.released_status = 'X'; -- For LPN reuse ER : 6845650
5003 
5004   -- bug 4306508
5005   CURSOR lpn_heirarchy(p_outermost_lpn_id NUMBER) IS
5006      SELECT lpn_id
5007        FROM wms_license_plate_numbers
5008        WHERE outermost_lpn_id = p_outermost_lpn_id
5009        ORDER BY lpn_id;
5010 
5011 
5012   CURSOR c_Get_OTM_flag(v_del_id NUMBER) IS
5013      SELECT ignore_for_planning, tms_interface_flag
5014        FROM wsh_new_deliveries_ob_grp_v
5015        WHERE delivery_id = v_del_id ;
5016 
5017   l_delivery_detail_tab        delivery_detail_tab;
5018   l_new_delivery_detail_id     NUMBER;
5019   l_dummy_num_var              NUMBER;
5020 
5021   /* For query reservations */
5022   l_reservation_record         inv_reservation_global.mtl_reservation_rec_type;
5023   l_reservation_tbl            inv_reservation_global.mtl_reservation_tbl_type; -- bug 4306508
5024   l_mtl_reservation_tbl        inv_reservation_global.mtl_reservation_tbl_type;
5025   l_mtl_reservation_tbl_count  NUMBER;
5026   l_reservation_exists_flag    VARCHAR2(1);
5027   l_reserved_lpn               NUMBER;
5028   l_return_status              VARCHAR2(1);
5029   l_msg_count                  NUMBER;
5030   l_msg_data                   VARCHAR2(20000);
5031   l_error_code                 NUMBER;
5032   l_dummy_sn                   inv_reservation_global.serial_number_tbl_type;
5033   l_outer_lpn                  outer_lpn%ROWTYPE;
5034   l_inner_lpn                  inner_lpn%ROWTYPE;
5035   l_mso_id                     NUMBER;
5036   l_outermost_lpn_id           NUMBER;
5037   /* For quantity tree declaration */
5038   l_return_msg                 VARCHAR2(20000);
5039   l_header_id                  NUMBER;
5040   l_line_id                    NUMBER;
5041   l_sub_reservable             NUMBER;
5042   l_item_reservable            NUMBER;
5043   l_lot_control_code           NUMBER;
5044   l_serial_number_control_code NUMBER;
5045   l_shipping_attr              wsh_interface.changedattributetabtype;
5046   l_invpcinrectype             wsh_integration.invpcinrectype;
5047   l_rsv_index                  NUMBER;
5048   l_rsv_qty                    NUMBER;
5049   i                            NUMBER;
5050   l_return                     NUMBER;
5051   /* Auto Create Trip  */
5052   l_cont_instance_id           NUMBER;
5053   l_cont_name                  VARCHAR2(50);
5054   l_delivery_detail_id         NUMBER;
5055   l_delivery_id                NUMBER;
5056   l_delivery_tab               wsh_util_core.id_tab_type;
5057 
5058   l_trip_id                    NUMBER;
5059   l_trip_name                  VARCHAR2(30);
5060   /* Patchset I */
5061   l_last_del_index             NUMBER;
5062   l_lpn_count                  NUMBER; --Added bug 4212476
5063 
5064   --MR:Added for 4440809
5065   l_total_lpn_rsv_qty          NUMBER;
5066   l_processing_staged_Lines    VARCHAR2(1);
5067 
5068   /* Auto Create Deliveries for Delivery Details that has no delivery associated yet */
5069   /* Check if there is any trip loaded on the dock door, if yes assign new deliveries to it
5070      else check if existing delivery_details have a trip already associated else
5071      create new trip
5072        */
5073 
5074    l_action_prms      wsh_interface_ext_grp.del_action_parameters_rectype;
5075    l_delivery_id_tab  wsh_util_core.id_tab_type;
5076    l_delivery_out_rec wsh_interface_ext_grp.del_action_out_rec_type;
5077 
5078    l_debug                      NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5079    -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
5080    l_sql_query                  VARCHAR2(10000);
5081    TYPE delivery_details_type IS REF CURSOR;
5082    delivery_details           delivery_details_type;
5083 
5084    /******************************
5085    * Creating an out Parameter in order to comply with the
5086    * New Signature of Explode_delivery_details
5087    *******************************/
5088      l_out_transaction_temp_id    NUMBER;
5089 
5090 
5091    /**********************************
5092    * variable to hold the ignore_for_planning flag of WDD.
5093    * Added for g-log changes
5094    **********************************/
5095    l_ignore_for_planning        wsh_delivery_details.ignore_for_planning%type;
5096    l_tms_interface_flag         wsh_new_deliveries.TMS_INTERFACE_FLAG%type;
5097 
5098 
5099 
5100 BEGIN
5101 
5102   IF (l_debug = 1) THEN
5103      DEBUG('Inside Stage LPNs..: ' , 'STAGE_LPN');
5104      DEBUG('p_group_id: ' || p_group_id , 'STAGE_LPN');
5105      DEBUG('p_organization_id: ' || p_organization_id , 'STAGE_LPN');
5106      DEBUG('p_dock_door_id: ' || p_dock_door_id , 'STAGE_LPN');
5107   END IF;
5108 
5109   --initalizing l_InvPCInRecType to use for updating wdd with transaction_temp_id
5110   l_invpcinrectype.transaction_id       := NULL;
5111   l_invpcinrectype.transaction_temp_id  := NULL;
5112   l_invpcinrectype.source_code          := 'INV';
5113   l_invpcinrectype.api_version_number   := 1.0;
5114   OPEN outer_lpn;
5115 
5116   LOOP
5117     FETCH outer_lpn INTO l_outer_lpn;
5118     EXIT WHEN outer_lpn%NOTFOUND;
5119     wms_direct_ship_pvt.create_update_containers(
5120       x_return_status              => l_return_status
5121     , x_msg_count                  => l_msg_count
5122     , x_msg_data                   => l_msg_data
5123     , p_org_id                     => p_organization_id
5124     , p_outermost_lpn_id           => l_outer_lpn.lpn_id
5125     );
5126 
5127     IF l_return_status IN(fnd_api.g_ret_sts_error) THEN
5128       IF (l_debug = 1) THEN
5129         DEBUG('create_update_containers API failed with status E ', 'STAGE_LPN');
5130       END IF;
5131 
5132       RAISE fnd_api.g_exc_error;
5133     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5134       IF (l_debug = 1) THEN
5135         DEBUG('create_update_containers failed with status U', 'STAGE_LPN');
5136       END IF;
5137 
5138       RAISE fnd_api.g_exc_unexpected_error;
5139     END IF;
5140 
5141     SELECT delivery_detail_id
5142       INTO l_cont_instance_id
5143       FROM wsh_delivery_details_ob_grp_v
5144      WHERE lpn_id = l_outer_lpn.lpn_id
5145      AND released_status = 'X';   -- For LPN reuse ER : 6845650
5146 
5147     SELECT wdd.delivery_detail_id
5148       INTO l_delivery_detail_id
5149       FROM wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
5150      WHERE wds.organization_id = p_organization_id
5151        AND wds.lpn_id = l_outer_lpn.lpn_id
5152        AND wdd.source_header_id = wds.order_header_id
5153        AND wdd.source_line_id = wds.order_line_id
5154        AND wdd.released_status IN('R', 'B', 'Y') --Added 'Y' bug4128854
5155        AND ROWNUM = 1;
5156 
5157     IF (l_debug = 1) THEN
5158       DEBUG('The delivery_detail_id for the lpn_Id is ' || l_cont_instance_id, 'STAGE_LPN');
5159       DEBUG('The delivery_detail_id for the first line is ' || l_delivery_detail_id, 'STAGE_LPN');
5160     END IF;
5161 
5162     wms_direct_ship_pvt.container_nesting(
5163       x_return_status              => l_return_status
5164     , x_msg_count                  => l_msg_count
5165     , x_msg_data                   => l_msg_data
5166     , p_organization_id            => p_organization_id
5167     , p_outermost_lpn_id           => l_outer_lpn.lpn_id
5168     , p_action_code                => 'PACK');
5169 
5170     IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5171       IF (l_debug = 1) THEN
5172         DEBUG('container nesting API failed for outermost_lpn_id ' || l_outer_lpn.lpn_id, 'stage_lpns');
5173       END IF;
5174 
5175       RAISE fnd_api.g_exc_error;
5176     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5177       IF (l_debug = 1) THEN
5178         DEBUG('container nesting API failed with unexpected errors for outermost_lpn_id '
5179                || l_outer_lpn.lpn_id, 'stage_lpns');
5180       END IF;
5181 
5182       RAISE fnd_api.g_exc_unexpected_error;
5183     END IF;
5184 
5185     IF (l_debug = 1) THEN
5186       DEBUG('Container nesting completed', 'xxxx');
5187     END IF;
5188 
5189     wsh_container_actions.update_cont_hierarchy(
5190       p_del_detail_id              => l_delivery_detail_id
5191     , p_delivery_id                => NULL
5192     , p_container_instance_id      => l_cont_instance_id
5193     , x_return_status              => l_return_status
5194     );
5195 
5196     IF l_return_status <> wsh_util_core.g_ret_sts_success THEN
5197       fnd_message.set_name('WSH', 'WSH_CONT_UPD_ATTR_ERROR');
5198       fnd_message.set_token('CONT_NAME', l_cont_name);
5199     --x_return_status := WSH_UTIL_CORE.G_RET_STS_UNEXP_ERROR;
5200     END IF;
5201 
5202     IF (l_debug = 1) THEN
5203       DEBUG('The Update container_Hierarchy was successful for outer-lpn ' || l_outer_lpn.lpn_id, 'STAGE_LPN');
5204     END IF;
5205   END LOOP;
5206 
5207   CLOSE outer_lpn;
5208 
5209 
5210   OPEN outer_lpn;
5211 
5212   LOOP
5213     IF (l_debug = 1) THEN
5214       DEBUG('Open outer lpn loop', 'Stage_LPNS');
5215     END IF;
5216 
5217     FETCH outer_lpn INTO l_outer_lpn;
5218     EXIT WHEN outer_lpn%NOTFOUND;
5219     OPEN stage_lines(l_outer_lpn.lpn_id);
5220 
5221     IF (l_debug = 1) THEN
5222       DEBUG('Open stage loop LPN ID: ' || l_outer_lpn.lpn_id, 'Stage_LPNS');
5223     END IF;
5224 
5225     LOOP
5226       FETCH stage_lines
5227       INTO l_header_id
5228          , l_line_id
5229          , l_sub_reservable
5230          , l_item_reservable
5231          , l_lot_control_code
5232          , l_serial_number_control_code;
5233       EXIT WHEN stage_lines%NOTFOUND;
5234 
5235       IF (l_debug = 1) THEN
5236         DEBUG('Stage Line Loop Line_ID :' || l_line_id, 'Stage_LPNS');
5237       END IF;
5238 
5239       IF (l_sub_reservable = 1
5240           AND l_item_reservable = 1) THEN
5241         l_mso_id                                       := inv_salesorder.get_salesorder_for_oeheader(l_header_id);
5242         l_reservation_record.demand_source_header_id   := l_mso_id;
5243         l_reservation_record.demand_source_line_id     := l_line_id;
5244         l_reservation_record.demand_source_line_detail := NULL;
5245 
5246         --  bug 4306508 : need to handle the case for nested LPNs too
5247         l_mtl_reservation_tbl.DELETE;
5248         FOR l_lpn_rec IN lpn_heirarchy(l_outer_lpn.lpn_id) LOOP
5249 
5250            l_reservation_record.lpn_id                   := l_lpn_rec.lpn_id;
5251            -- bug 4271408
5252            --l_reservation_record.lpn_id                   := l_outer_lpn.lpn_id;
5253 
5254            IF (l_debug = 1) THEN
5255               DEBUG('Before call to reservation demand_source_line_id ' || l_line_id, 'STAGE_LPNs');
5256            END IF;
5257 
5258            inv_reservation_pub.query_reservation
5259              (p_api_version_number         => 1.0
5260               , p_init_msg_lst               => fnd_api.g_false
5261               , x_return_status              => l_return_status
5262               , x_msg_count                  => l_msg_count
5263               , x_msg_data                   => l_msg_data
5264               , p_query_input                => l_reservation_record
5265               , p_lock_records               => fnd_api.g_false
5266               , x_mtl_reservation_tbl        => l_reservation_tbl
5267               , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5268               , x_error_code                 => l_error_code);
5269 
5270            IF l_return_status = fnd_api.g_ret_sts_error THEN
5271               IF (l_debug = 1) THEN
5272                  DEBUG('Validation error during query of Reservations ' || l_header_id
5273                         || ' ' || l_line_id, 'STAGE_LPNS');
5274               END IF;
5275 
5276               RAISE fnd_api.g_exc_error;
5277             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5278               IF (l_debug = 1) THEN
5279                  DEBUG('Unexpected error during query of Reservations ' || l_header_id || ' '
5280                          || l_line_id, 'STAGE_LPNS');
5281               END IF;
5282 
5283               RAISE fnd_api.g_exc_unexpected_error;
5284            END IF;
5285 
5286            FOR i IN 1..l_mtl_reservation_tbl_count LOOP
5287 
5288               l_mtl_reservation_tbl(l_mtl_reservation_tbl.COUNT + 1) := l_reservation_tbl(i);
5289 
5290            END LOOP;
5291 
5292         END LOOP;
5293 
5294         l_mtl_reservation_tbl_count := l_mtl_reservation_tbl.COUNT;
5295 
5296       END IF;
5297 
5298 
5299       /* For each delivery details get the Reservation details and stage the delivery lines */
5300       IF (l_debug = 1) THEN
5301         DEBUG('After call to reservation No of Rsv Records: ' || l_mtl_reservation_tbl_count, 'STAGE_LPNs');
5302       END IF;
5303 
5304       i                 := 1;
5305       -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
5306       l_sql_query :=
5307         '    SELECT   wds.organization_id ' ||
5308         '           , wds.dock_door_id ' ||
5309         '           , wds.lpn_id ' ||
5310         '           , wds.order_header_id ' ||
5311         '           , wds.order_line_id ' ||
5312         '           , wds.line_item_id ' ||
5313         '           , wds.transaction_temp_id ' ||
5314         '           , wdd.delivery_detail_id ' ||
5315         '           , DECODE( ' ||
5316         '               wdd.requested_quantity_uom ' ||
5317         '             , msi.primary_uom_code, wdd.requested_quantity ' ||
5318         '             , GREATEST( ' ||
5319         '                        inv_convert.inv_um_convert(NULL, ' ||
5320         '                        NULL, wdd.requested_quantity, wdd.requested_quantity_uom, ' ||
5321         '                        msi.primary_uom_code, NULL , NULL) ' ||
5322         '                     , 0 )) requested_quantity ' ||
5323         '           , msi.primary_uom_code ' ||
5324         '           , msi.lot_control_code ' ||
5325         '           , msi.serial_number_control_code ' ||
5326         '           , msi.inventory_item_id ';
5327 
5328         IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL) THEN
5329           l_sql_query := l_sql_query || '           , msi.ont_pricing_qty_source ';
5330         ELSE
5331           l_sql_query := l_sql_query || '           , NULL ont_pricing_qty_source ';
5332         END IF;
5333 
5334     l_sql_query := l_sql_query ||
5335         '        FROM wms_direct_ship_temp wds, ' ||
5336         '             wsh_delivery_details_ob_grp_v wdd, ' ||
5337         '             mtl_system_items msi ' ||
5338         '       WHERE wds.organization_id = :p_organization_id ' ||
5339         '         AND wds.lpn_id = :p_lpn_id ' ||
5340         '         AND wds.order_header_id = :p_order_header_id ' ||
5341         '         AND wds.order_line_id = :p_order_line_id ' ||
5342         '         AND wdd.organization_id = :p_organization_id ' ||
5343         '         AND wdd.source_header_id = wds.order_header_id ' ||
5344         '         AND wdd.source_line_id = wds.order_line_id ' ||
5345         '         AND msi.organization_id = :p_organization_id ' ||
5346         '         AND msi.inventory_item_id = wdd.inventory_item_id ' ||
5347         '         AND wdd.released_status IN (''B'', ''R'') ' ||
5348         '         AND wdd.container_flag = ''N'' ' ||
5349         '    ORDER BY wds.line_item_id';
5350 
5351       -- OPEN delivery_details(l_outer_lpn.lpn_id, l_header_id, l_line_id);
5352       OPEN delivery_details FOR l_sql_query
5353         USING p_organization_id,
5354               l_outer_lpn.lpn_id,
5355               l_header_id,
5356               l_line_id,
5357               p_organization_id,
5358               p_organization_id;
5359 
5360       LOOP
5361         FETCH delivery_details
5362          INTO l_delivery_detail_tab(i).organization_id
5363             , l_delivery_detail_tab(i).dock_door_id
5364             , l_delivery_detail_tab(i).lpn_id
5365             , l_delivery_detail_tab(i).order_header_id
5366             , l_delivery_detail_tab(i).order_line_id
5367             , l_delivery_detail_tab(i).line_item_id
5368             , l_delivery_detail_tab(i).transaction_temp_id
5369             , l_delivery_detail_tab(i).delivery_detail_id
5370             , l_delivery_detail_tab(i).requested_quantity
5371             , l_delivery_detail_tab(i).primary_uom_code
5372             , l_delivery_detail_tab(i).lot_control_code
5373             , l_delivery_detail_tab(i).serial_number_control_code
5374             , l_delivery_detail_tab(i).inventory_item_id
5375             , l_delivery_detail_tab(i).ont_pricing_qty_source;
5376         EXIT WHEN delivery_details%NOTFOUND;
5377 
5378         IF (l_debug = 1) THEN
5379            DEBUG('delivery_detail_id: ' || l_delivery_detail_tab(i).delivery_detail_id
5380                   || ' i: ' || i, 'STAGE_LPNS');
5381         END IF;
5382 
5383         i  := i + 1;
5384       END LOOP;
5385 
5386       l_last_del_index  := l_delivery_detail_tab.COUNT;
5387 
5388       IF (l_debug = 1) THEN
5389         DEBUG('l_last_del_index=' || l_last_del_index, 'STAGE_LPN');
5390       END IF;
5391 
5392       CLOSE delivery_details;
5393 
5394       l_processing_staged_Lines  := 'N';
5395 
5396       IF i = 1 then  --Added code bug 4128854 to overship a staged line
5397           l_delivery_detail_tab := Overship_Staged_Lines(
5398                      p_source_header_id =>l_header_id,
5399                      p_source_line_id =>l_line_id,
5400                      p_lpn_id =>l_outer_lpn.lpn_id,
5401                      p_mso_id =>l_mso_id,
5402                      p_organization_id => p_organization_id,
5403                      p_cont_instance_id => l_cont_instance_id,
5404                      p_dock_door_id => p_dock_door_id,
5405                      x_return_status   => l_return_status,
5406                      x_msg_count       => l_msg_count,
5407                      x_msg_data        => l_msg_data);
5408 
5409             l_processing_staged_Lines  := 'Y';
5410             --l_last_del_index  := l_delivery_detail_tab.COUNT;
5411       END IF;
5412 
5413       IF (l_debug = 1) THEN
5414         DEBUG('No of delivery details for the demand_source: ' || l_delivery_detail_tab.COUNT, 'STAGE_LPN');
5415       END IF;
5416 
5417       IF (l_sub_reservable = 1
5418           AND l_item_reservable = 1) THEN
5419            -- If item = Vanilla or Lot Controlled THEN Query reservation and
5420            -- stage the delivery lines with the reserved Qty
5421         i            := 1;
5422         l_rsv_index  := 1;
5423         l_rsv_qty    := 0;
5424         l_total_lpn_rsv_qty    := 0;
5425 
5426         <<delivery_detail_loop>> -- 2529382
5427         FOR i IN 1 .. l_delivery_detail_tab.COUNT LOOP
5428           IF (l_debug = 1) THEN
5429             DEBUG('Clear the change attribute table ', 'Stage_LPNs');
5430           END IF;
5431 
5432           l_shipping_attr.DELETE;
5433           l_out_transaction_temp_id := null; --Bug 4592143
5434           l_lpn_count :=  0; /*Added bug4212476*/
5435           FOR j IN l_rsv_index .. l_mtl_reservation_tbl.COUNT LOOP
5436              IF l_mtl_reservation_tbl(j).lpn_id IS NOT NULL THEN
5437                 SELECT outermost_lpn_id
5438                   INTO l_outermost_lpn_id
5439                   FROM wms_license_plate_numbers
5440                   WHERE lpn_id = l_mtl_reservation_tbl(j).lpn_id;
5441 
5442                 IF (l_debug = 1) THEN
5443                    DEBUG('l_outermost_lpn_id: ' || l_outermost_lpn_id, 'Stage_LPNs');
5444                    DEBUG('l_outer_lpn.lpn_id: ' || l_outer_lpn.lpn_id, 'Stage_LPNs');
5445                 END IF;
5446                 IF l_outer_lpn.lpn_id = l_outermost_lpn_id THEN
5447                    l_lpn_count := l_lpn_count + 1;
5448                    IF i = 1 THEN  -- get the total per LPN only once for multiple eligible WDDs
5449                       l_total_lpn_rsv_qty := l_total_lpn_rsv_qty +
5450                              l_mtl_reservation_tbl(j).primary_reservation_quantity; --MR:added for 4440809
5451                    END IF;
5452                    IF (l_debug = 1) THEN
5453                       DEBUG('l_total_lpn_rsv_qty : ' || l_total_lpn_rsv_qty || ':I:'|| i ,'Stage_LPNs');
5454                    END IF;
5455                 END IF;
5456                 IF (l_debug = 1) THEN
5457                    DEBUG('l_mtl_reservation_tbl(j).lpn_id: ' ||
5458                           l_mtl_reservation_tbl(j).lpn_id ,'Stage_LPNs');
5459                 END IF;
5460              END IF;
5461           END LOOP;     /*Added bug4212476*/
5462           IF (l_debug = 1) THEN
5463              DEBUG('l_lpn_count : ' || l_lpn_count ,'Stage_LPNs');
5464              DEBUG('l_processing_staged_lines : ' || l_processing_staged_lines ,'Stage_LPNs');
5465           END IF;
5466 
5467           /*  change made for 5377437 to match with 4440809
5468               l_lpn_count := l_lpn_count + 1;
5469               IF i = 1 THEN  -- get the total per LPN only once for multiple eligible WDDs
5470                  l_total_lpn_rsv_qty := l_total_lpn_rsv_qty +
5471                  l_mtl_reservation_tbl(j).primary_reservation_quantity; --MR:added for 4440809
5472               END IF;
5473               ELSE
5474                    DEBUG('The Outermost LPN do not match ', 'Stage_LPNs');
5475               END IF;
5476              END IF;
5477            END LOOP;    */
5478 
5479           FOR j IN l_rsv_index .. l_mtl_reservation_tbl.COUNT
5480           LOOP
5481             l_shipping_attr(1).pending_quantity := 0;
5482             IF l_mtl_reservation_tbl(j).lpn_id IS NULL THEN
5483               GOTO next_resv_rec;
5484             END IF;
5485 
5486             IF (l_debug = 1) THEN
5487               DEBUG('Call the select statement for rsv lpn_id  ' || l_mtl_reservation_tbl(j).lpn_id, 'Stage_LPNs');
5488             END IF;
5489 
5490             SELECT outermost_lpn_id
5491               INTO l_outermost_lpn_id
5492               FROM wms_license_plate_numbers
5493              WHERE lpn_id = l_mtl_reservation_tbl(j).lpn_id;
5494 
5495             IF l_outer_lpn.lpn_id = l_outermost_lpn_id THEN
5496               IF (l_debug = 1) THEN
5497                 DEBUG('The Outermost LPN match ', 'Stage_LPNs');
5498                 DEBUG('last index is '||l_last_del_index,'Stage LPNs');
5499               END IF;
5500               IF l_last_del_index <> 0 THEN   -- Added bug 4128854
5501 
5502                  l_shipping_attr(1).source_line_id      := l_mtl_reservation_tbl(j).demand_source_line_id;
5503                  l_shipping_attr(1).ship_from_org_id    := l_mtl_reservation_tbl(j).organization_id;
5504                  l_shipping_attr(1).subinventory        := l_mtl_reservation_tbl(j).subinventory_code;
5505                  l_shipping_attr(1).revision            := l_mtl_reservation_tbl(j).revision;
5506                  l_shipping_attr(1).locator_id          := l_mtl_reservation_tbl(j).locator_id;
5507                  l_shipping_attr(1).lot_number          := l_mtl_reservation_tbl(j).lot_number;
5508                  l_shipping_attr(1).transfer_lpn_id     := l_mtl_reservation_tbl(j).lpn_id;
5509               END IF;
5510                  l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_tab(i).delivery_detail_id;
5511                  l_shipping_attr(1).inventory_item_id  := l_delivery_detail_tab(i).inventory_item_id;
5512 
5513             ELSE
5514              IF (l_debug = 1) THEN
5515                 DEBUG('LPN do not match outerlpn_id  ' || l_outer_lpn.lpn_id, 'Stage_LPNs');
5516               END IF;
5517 
5518               GOTO next_resv_rec;
5519             END IF;
5520 
5521             IF (l_debug = 1) THEN
5522               DEBUG('l_rsv_qty ' || l_rsv_qty, 'STAGE_LPN');
5523             END IF;
5524 
5525             IF l_rsv_qty <= 0 THEN
5526               l_rsv_qty  := l_mtl_reservation_tbl(j).primary_reservation_quantity;
5527             END IF;
5528 
5529            -- Added for bug 4440809
5530             IF l_processing_staged_Lines = 'Y' THEN
5531                l_total_lpn_rsv_qty := l_rsv_qty;
5532             END IF;
5533 
5534             IF (l_debug = 1) THEN
5535                   DEBUG('l_lpn_count: ' || l_lpn_count, 'STAGE_LPNS');
5536                   DEBUG('i : ' || i || ' :l_last_del_index: ' || l_last_del_index , 'STAGE_LPNS');
5537                   DEBUG('l_rsv_qty ' || l_rsv_qty , 'STAGE_LPNS');
5538                   DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
5539                   DEBUG('l_delivery_detail_tab(i).requested_quantity: '
5540                            || l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5541             END IF;
5542 
5543             IF l_rsv_qty >= l_delivery_detail_tab(i).requested_quantity THEN
5544                IF (l_debug = 1) THEN
5545                   DEBUG('Reservation quantity >= delivery detail quantity', 'STAGE_LPNS');
5546                END IF;
5547                -- Bug : 4440809 : Start
5548 
5549                IF l_rsv_qty = l_delivery_detail_tab(i).requested_quantity THEN
5550                   IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
5551                      IF (i >= l_last_del_index) THEN
5552                         l_shipping_attr(1).action_flag      := 'M';
5553                         l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
5554                      ELSE
5555                         l_shipping_attr(1).action_flag      := 'U';
5556                      END IF;
5557                   ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
5558                      l_shipping_attr(1).action_flag      := 'U';
5559                   ELSIF l_rsv_qty > l_total_lpn_rsv_qty  THEN
5560                      IF (l_debug = 1) THEN
5561                         DEBUG('l_rsv_qty > l_total_lpn_rsv_qty : ' , 'STAGE_LPNS');
5562                      END IF;
5563                      null; -- why shld this condition happen
5564                   END IF;
5565                END IF;
5566 
5567                l_shipping_attr(1).picked_quantity := l_rsv_qty;
5568                l_shipping_attr(1).released_status := 'Y';
5569 
5570                IF l_rsv_qty > l_delivery_detail_tab(i).requested_quantity THEN
5571                   IF (i >= l_last_del_index) THEN
5572                      IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
5573                            l_shipping_attr(1).action_flag      := 'M';
5574                           l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
5575                      ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
5576                         l_shipping_attr(1).action_flag      := 'U';
5577                      ELSIF l_rsv_qty > l_total_lpn_rsv_qty  THEN
5578                         IF (l_debug = 1) THEN
5579                            DEBUG('l_rsv_qty > l_total_lpn_rsv_qty : ' , 'STAGE_LPNS');
5580                         END IF;
5581                         null; -- why shld this condition happen
5582                      END IF;
5583                      l_delivery_detail_tab(i).requested_quantity  := l_rsv_qty; -- **MR 01/04/06
5584                      l_total_lpn_rsv_qty                := l_total_lpn_rsv_qty - l_rsv_qty;
5585                      l_rsv_qty := 0;
5586                   ELSE
5587                      l_total_lpn_rsv_qty                :=
5588                          l_total_lpn_rsv_qty - l_delivery_detail_tab(i).requested_quantity;
5589                      l_shipping_attr(1).picked_quantity := l_delivery_detail_tab(i).requested_quantity;
5590                      l_rsv_qty := l_rsv_qty - l_delivery_detail_tab(i).requested_quantity ;
5591                      l_shipping_attr(1).action_flag := 'U';
5592                      l_delivery_detail_tab(i).requested_quantity  := l_rsv_qty; -- **MR 01/04/06
5593                   END IF; -- (i >= l_last_del_index) THEN
5594                ELSIF l_rsv_qty = l_delivery_detail_tab(i).requested_quantity THEN
5595                   l_total_lpn_rsv_qty                := l_total_lpn_rsv_qty - l_rsv_qty;
5596                   l_rsv_qty := 0;
5597                END IF;
5598 
5599                IF (l_debug = 1) THEN
5600                   DEBUG('l_shipping_attr(1).action_flag: ' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
5601                   DEBUG('l_shipping_attr(1).pending_quantity: ' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
5602                   DEBUG('l_shipping_attr(1).picked_quantity: ' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
5603                   DEBUG('l_total_lpn_rsv_qty: '|| l_total_lpn_rsv_qty, 'STAGE_LPNS');
5604                   DEBUG('l_rsv_qty: '|| l_rsv_qty, 'STAGE_LPNS');
5605                   DEBUG('l_delivery_detail_tab(i).requested_quantity: '||
5606                          l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5607 
5608                END IF;
5609                -- Bug : 4440809 : End
5610 
5611               IF (l_rsv_qty <= 0) THEN
5612                 l_rsv_index  := j + 1;
5613               ELSE
5614                 l_rsv_index  := j;
5615               END IF;
5616 
5617               IF (l_debug = 1) THEN
5618                 DEBUG('J : ' || j  , 'STAGE_LPNS');
5619                 DEBUG('l_rsv_index: ' || l_rsv_index , 'STAGE_LPNS');
5620                 DEBUG('l_rsv_qty: ' || l_rsv_qty, 'STAGE_LPNS');
5621               END IF;
5622 
5623               IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial in MSN
5624                 IF (l_debug = 1) THEN
5625                   DEBUG('The serial number control code is in 2,5', 'stage_lpns');
5626                 END IF;
5627 
5628                 IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
5629                   SELECT mtl_material_transactions_s.NEXTVAL
5630                     INTO l_delivery_detail_tab(i).transaction_temp_id
5631                     FROM DUAL;
5632 
5633                   -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
5634                   UPDATE wms_direct_ship_temp
5635                      SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
5636                    WHERE organization_id = l_delivery_detail_tab(i).organization_id
5637                      AND lpn_id = l_delivery_detail_tab(i).lpn_id
5638                      AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
5639                      AND order_header_id = l_delivery_detail_tab(i).order_header_id
5640                      AND order_line_id = l_delivery_detail_tab(i).order_line_id;
5641 
5642                   IF (l_debug = 1) THEN
5643                     DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value=' ||
5644                             l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
5645                   END IF;
5646                 END IF;
5647 
5648                 -- Mark Serial Numbers
5649                 -- ACTION overship l_delivery_detail_tab(i).requested_quantity >> overship qty
5650                 explode_delivery_details(
5651                           x_return_status              => l_return_status
5652                         , x_msg_data                   => l_msg_data
5653                         , x_msg_count                  => l_msg_count
5654                           --Bug No 3390432
5655                         , x_transaction_temp_id        => l_out_transaction_temp_id
5656                         , p_organization_id            => p_organization_id
5657                         , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
5658                         , p_serial_number_control_code => l_serial_number_control_code
5659                         , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
5660                         , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
5661                         , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
5662                         , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
5663                         , p_last_action                => l_shipping_attr(1).action_flag);
5664 
5665                 IF l_return_status = fnd_api.g_ret_sts_error THEN
5666                   IF (l_debug = 1) THEN
5667                     DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
5668                   END IF;
5669 
5670                   RAISE fnd_api.g_exc_error;
5671                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5672                   IF (l_debug = 1) THEN
5673                     DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
5674                   END IF;
5675 
5676                   RAISE fnd_api.g_exc_unexpected_error;
5677                 END IF;
5678               ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
5679                 IF (l_debug = 1) THEN
5680                   DEBUG('The serial number control code is 6', 'stage_lpns');
5681                 END IF;
5682 
5683                 /*******************************
5684                  * Adding a new out Parameter to Comply with
5685                  * New Signature of Explode_delivery_details
5686                  ********************************/
5687                 explode_delivery_details(
5688                          x_return_status              => l_return_status
5689                        , x_msg_data                   => l_msg_data
5690                        , x_msg_count                  => l_msg_count
5691                         -- Bug No 3390432
5692                        , x_transaction_temp_id        => l_out_transaction_temp_id
5693                        , p_organization_id            => p_organization_id
5694                        , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
5695                        , p_serial_number_control_code => l_serial_number_control_code
5696                        , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
5697                        , p_quantity                   => l_delivery_detail_tab(i).requested_quantity
5698                        , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
5699                        , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
5700                        , p_last_action                => l_shipping_attr(1).action_flag);
5701 
5702                 IF l_return_status = fnd_api.g_ret_sts_error THEN
5703                   IF (l_debug = 1) THEN
5704                     DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status E', 'STAGE_LPNS');
5705                   END IF;
5706 
5707                   RAISE fnd_api.g_exc_error;
5708                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5709                   IF (l_debug = 1) THEN
5710                     DEBUG('CALL TO EXPLODE_DELIVERY_DETAILS api returns status U', 'STAGE_LPNS');
5711                   END IF;
5712 
5713                   RAISE fnd_api.g_exc_unexpected_error;
5714                 END IF;
5715               END IF;
5716 
5717               l_invpcinrectype.transaction_temp_id         := l_delivery_detail_tab(i).transaction_temp_id;
5718               l_shipping_attr(1).released_status           := 'Y';
5719 
5720               /*************************************
5721           * Bug No 3390432
5722           * Use the new Transaction Temp Id to update WDD
5723           **************************************/
5724           IF(l_serial_number_control_code = 6) THEN
5725                    l_invpcinrectype.transaction_temp_id         := l_out_transaction_temp_id;
5726             IF (l_debug = 1) THEN
5727                DEBUG('AFTER updating l_invpcinrectype,  l_invpcinrectype.transaction_temp_id = '
5728                       || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
5729             END IF;
5730           END IF;
5731 
5732           IF (l_debug = 1) THEN
5733              DEBUG('l_shipping_attr(1).action_flag     :=' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
5734              DEBUG('l_shipping_attr(1).source_line_id     :=' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
5735              DEBUG('l_shipping_attr(1).transfer_lpn_id    :=' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
5736              DEBUG('l_shipping_attr(1).delivery_detail_id :=' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
5737              DEBUG('l_shipping_attr(1).picked_quantity :=' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
5738              DEBUG('l_shipping_attr(1).pending_quantity   :=' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
5739              DEBUG('l_delivery_detail_tab(i).requested_quantity :=' ||
5740                        l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5741              DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
5742              DEBUG('l_InvPCInRecType.transaction_temp_id :=' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
5743           END IF;
5744 
5745           --Call shipping api to set transaction_temp_id global variable
5746           IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
5747              IF (l_shipping_attr(1).picked_quantity > 1) THEN
5748                   l_shipping_attr(1).serial_number  := NULL;
5749 
5750                 IF (l_debug = 1) THEN
5751                    DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
5752                           l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
5753                 END IF;
5754 
5755                   wsh_integration.set_inv_pc_attributes(
5756                     p_in_attributes              => l_invpcinrectype
5757                   , x_return_status              => l_return_status
5758                   , x_msg_count                  => l_msg_data
5759                   , x_msg_data                   => l_msg_count
5760                   );
5761 
5762                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5763                     IF (l_debug = 1) THEN
5764                       DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
5765                     END IF;
5766 
5767                     RAISE fnd_api.g_exc_error;
5768                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5769                     IF (l_debug = 1) THEN
5770                       DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
5771                     END IF;
5772 
5773                     RAISE fnd_api.g_exc_unexpected_error;
5774                   END IF;
5775                 ELSE
5776                   BEGIN
5777                     SELECT fm_serial_number
5778                       INTO l_shipping_attr(1).serial_number
5779                       FROM mtl_serial_numbers_temp
5780                      WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
5781                        AND ROWNUM < 2;
5782 
5783                     IF (l_debug = 1) THEN
5784                       DEBUG(
5785                            'found fm_serial='
5786                         || l_shipping_attr(1).serial_number
5787                         || ' for transaction_temp_id='
5788                         || l_invpcinrectype.transaction_temp_id
5789                       , 'STAGE_LPN'
5790                       );
5791                     END IF;
5792 
5793                     DELETE FROM mtl_serial_numbers_temp
5794                           WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
5795                   EXCEPTION
5796                     WHEN NO_DATA_FOUND THEN
5797                       IF (l_debug = 1) THEN
5798                         DEBUG('No rows found in MSNT for transaction_temp_id=' ||
5799                                l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
5800                       END IF;
5801                   END;
5802                 END IF;
5803               END IF;
5804               IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL AND
5805                   l_delivery_detail_tab(i).ont_pricing_qty_source='S')
5806               THEN
5807                   l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT
5808                                   (l_shipping_attr(1).ship_from_org_id
5809                                   ,l_shipping_attr(1).transfer_lpn_id
5810                                   ,l_shipping_attr(1).inventory_item_id
5811                                   ,l_shipping_attr(1).revision
5812                                   ,l_shipping_attr(1).lot_number
5813                                   ,l_shipping_attr(1).picked_quantity);
5814           END IF;
5815 
5816               wsh_interface.update_shipping_attributes
5817                     (   p_source_code        => 'INV'
5818                       , p_changed_attributes => l_shipping_attr
5819                       , x_return_status      => l_return_status);
5820 
5821               IF (l_debug = 1) THEN
5822                 DEBUG('after update shipping attributes', 'stage_lpns');
5823               END IF;
5824 
5825               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5826                 IF (l_debug = 1) THEN
5827                   DEBUG('return error from update shipping attributes 3', 'STAGE_LPN');
5828                 END IF;
5829 
5830                 RAISE fnd_api.g_exc_error;
5831               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5832                 IF (l_debug = 1) THEN
5833                   DEBUG('return unexpected error from update shipping attributes', 'STAGE_LPN');
5834                 END IF;
5835 
5836                 RAISE fnd_api.g_exc_unexpected_error;
5837               END IF;
5838 
5839               --EXIT; -- commented in P-I to split on extra reservations
5840               l_delivery_detail_tab(i).requested_quantity  := 0;
5841 
5842               --
5843               IF l_delivery_detail_tab(i).requested_quantity = 0
5844                  AND i < l_last_del_index THEN
5845                 EXIT;
5846               END IF;
5847             ----
5848             ----End of IF-1
5849             ----Start of IF-2
5850             ELSIF l_rsv_qty < l_delivery_detail_tab(i).requested_quantity THEN
5851               IF (l_debug = 1) THEN
5852                  DEBUG('The l_rsv_qty < requested_quantity ', 'STAGE_LPNs');
5853                  DEBUG('rsv lpn_id:  ' || l_mtl_reservation_tbl(j).lpn_id, 'Stage_LPNs');
5854                  DEBUG('l_rsv_qty ' || l_rsv_qty , 'STAGE_LPNS');
5855                  DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
5856                  DEBUG('l_delivery_detail_tab(i).requested_quantity: '
5857                           || l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
5858               END IF;
5859 
5860               -- Bug : 4440809 : Start
5861               IF l_rsv_qty < l_total_lpn_rsv_qty  THEN
5862                  l_shipping_attr(1).action_flag      := 'M';
5863                  l_shipping_attr(1).pending_quantity := (l_total_lpn_rsv_qty - l_rsv_qty);
5864               ELSIF l_rsv_qty = l_total_lpn_rsv_qty  THEN
5865                  IF l_delivery_detail_tab(i).requested_quantity - l_rsv_qty > 0
5866                  THEN
5867                    l_shipping_attr(1).action_flag       := 'M';
5868                    l_shipping_attr(1).pending_quantity :=
5869                             l_delivery_detail_tab(i).requested_quantity - l_rsv_qty;
5870                  ELSE
5871                     l_shipping_attr(1).action_flag      := 'U';
5872                  END IF;
5873               END IF;-- l_rsv_qty and l_total_lpn_rsv_qty  THEN
5874 
5875               l_shipping_attr(1).picked_quantity := l_rsv_qty;
5876               l_shipping_attr(1).released_status := 'Y';
5877               l_total_lpn_rsv_qty                := l_total_lpn_rsv_qty - l_rsv_qty;
5878 
5879 
5880               /*   4440809
5881               l_shipping_attr(1).pending_quantity          := 0;
5882               l_shipping_attr(1).released_status           := 'Y';
5883               l_shipping_attr(1).picked_quantity           := l_rsv_qty;
5884               l_shipping_attr(1).action_flag               := 'U'; */
5885 
5886               IF (l_debug = 1) THEN
5887                  DEBUG('l_total_lpn_rsv_qty ' || l_total_lpn_rsv_qty , 'STAGE_LPNS');
5888                  DEBUG('i=' || i || ' j= ' || j, 'xxxx');
5889                  DEBUG(' l_shipping_attr(1).action_flag :' || l_shipping_attr(1).action_flag, 'STAGE_LPNs');
5890               END IF;
5891 
5892               IF (l_serial_number_control_code IN(2, 5)) THEN -- Serial Control Code
5893                 IF (l_debug = 1) THEN
5894                   DEBUG('The serial number control code is in 2,5', 'stage_lpns');
5895                 END IF;
5896 
5897                 IF (l_delivery_detail_tab(i).transaction_temp_id IS NULL) THEN
5898                   SELECT mtl_material_transactions_s.NEXTVAL
5899                     INTO l_delivery_detail_tab(i).transaction_temp_id
5900                     FROM DUAL;
5901 
5902                     l_invpcinrectype.transaction_temp_id := l_delivery_detail_tab(i).transaction_temp_id;
5903 
5904                   -- update wds with transaction_temp_id, will be used in unload lpn if required bug# 2829514
5905                   UPDATE wms_direct_ship_temp
5906                      SET transaction_temp_id = l_delivery_detail_tab(i).transaction_temp_id
5907                    WHERE organization_id = l_delivery_detail_tab(i).organization_id
5908                      AND lpn_id = l_delivery_detail_tab(i).lpn_id
5909                      AND dock_door_id = l_delivery_detail_tab(i).dock_door_id
5910                      AND order_header_id = l_delivery_detail_tab(i).order_header_id
5911                      AND order_line_id = l_delivery_detail_tab(i).order_line_id;
5912 
5913                   IF (l_debug = 1) THEN
5914                     DEBUG( 'l_delivery_detail_tab(i).transaction_temp_id was null new value='
5915                             || l_delivery_detail_tab(i).transaction_temp_id , 'STAGE_LPNS');
5916                   END IF;
5917                 END IF;
5918 
5919                 /**************************************
5920                 * Adding new out parameter to comply with new signature
5921                 ***************************************/
5922                 -- Mark Serial Numbers
5923                 explode_delivery_details(
5924                   x_return_status              => l_return_status
5925                 , x_msg_data                   => l_msg_data
5926                 , x_msg_count                  => l_msg_count
5927                   --Bug No 3390432
5928                 , x_transaction_temp_id        => l_out_transaction_temp_id
5929                 , p_organization_id            => p_organization_id
5930                 , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
5931                 , p_serial_number_control_code => l_serial_number_control_code
5932                 , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
5933                 , p_quantity                   => l_rsv_qty
5934                 , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
5935                 , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
5936                 , p_last_action                => l_shipping_attr(1).action_flag
5937                 );
5938 
5939                 IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5940                   IF (l_debug = 1) THEN
5941                     DEBUG('return error from update shipping attributes 3', 'stage_lpns');
5942                   END IF;
5943 
5944                   RAISE fnd_api.g_exc_error;
5945                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5946                   IF (l_debug = 1) THEN
5947                     DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
5948                   END IF;
5949 
5950                   RAISE fnd_api.g_exc_unexpected_error;
5951                 END IF;
5952               ELSIF(l_serial_number_control_code = 6) THEN -- Serial in MSNT
5953                 IF (l_debug = 1) THEN
5954                   DEBUG('The serial number control code is 6', 'stage_lpns');
5955                 END IF;
5956 
5957                 /*****************************************
5958                 * Retreving the new Transaction temp id to use in Splitting
5959                 * existing WDD line
5960                 ******************************************/
5961                 explode_delivery_details(
5962                   x_return_status              => l_return_status
5963                 , x_msg_data                   => l_msg_data
5964                 , x_msg_count                  => l_msg_count
5965                  --Bug No 3390432
5966                 , x_transaction_temp_id        => l_out_transaction_temp_id
5967                 , p_organization_id            => p_organization_id
5968                 , p_lpn_id                     => l_mtl_reservation_tbl(j).lpn_id
5969                 , p_serial_number_control_code => l_serial_number_control_code
5970                 , p_delivery_detail_id         => l_delivery_detail_tab(i).delivery_detail_id
5971                 , p_quantity                   => l_rsv_qty
5972                 , p_transaction_temp_id        => l_delivery_detail_tab(i).transaction_temp_id
5973                 , p_reservation_id             => l_mtl_reservation_tbl(j).reservation_id
5974                 , p_last_action                => l_shipping_attr(1).action_flag
5975                 );
5976 
5977                 IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5978                   IF (l_debug = 1) THEN
5979                     DEBUG('return error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
5980                   END IF;
5981 
5982                   RAISE fnd_api.g_exc_error;
5983                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5984                   IF (l_debug = 1) THEN
5985                     DEBUG('return unexpected error from EXPLODE_DELIVERY_DETAILS 3', 'stage_lpns');
5986                   END IF;
5987 
5988                   RAISE fnd_api.g_exc_unexpected_error;
5989                 END IF;
5990               END IF; -- Serial Control Code
5991 
5992               /*******************************************
5993           * Bug No 3390432
5994           * Update WDD with the  newly created Transaction_temp_id
5995           ********************************************/
5996               l_invpcinrectype.transaction_temp_id := l_out_transaction_temp_id;
5997               l_shipping_attr(1).released_status   := 'Y';
5998 
5999               IF (l_debug = 1) THEN
6000                 DEBUG('l_shipping_attr(1).action_flag        =' || l_shipping_attr(1).action_flag, 'STAGE_LPNS');
6001                 DEBUG('l_shipping_attr(1).source_line_id     =' || l_shipping_attr(1).source_line_id, 'STAGE_LPNS');
6002                 DEBUG('l_shipping_attr(1).transfer_lpn_id    =' || l_shipping_attr(1).transfer_lpn_id, 'STAGE_LPNS');
6003                 DEBUG('l_shipping_attr(1).delivery_detail_id =' || l_shipping_attr(1).delivery_detail_id, 'STAGE_LPNS');
6004                 DEBUG('l_shipping_attr(1).picked_quantity    =' || l_shipping_attr(1).picked_quantity, 'STAGE_LPNS');
6005                 DEBUG('l_shipping_attr(1).pending_quantity   =' || l_shipping_attr(1).pending_quantity, 'STAGE_LPNS');
6006                 DEBUG('l_delivery_detail_tab(i).requested_quantity =' ||
6007                        l_delivery_detail_tab(i).requested_quantity, 'STAGE_LPNS');
6008                 DEBUG('l_shipping_attr(1).lot_number : ' || l_shipping_attr(1).lot_number, 'STAGE_LPNS');
6009                 DEBUG('l_InvPCInRecType.transaction_temp_id  =' || l_invpcinrectype.transaction_temp_id, 'STAGE_LPNS');
6010               END IF;
6011 
6012               --Call shipping api to set transaction_temp_id global variable
6013               IF ( l_invpcinrectype.transaction_temp_id IS NOT NULL ) THEN
6014                 IF ( l_shipping_attr(1).picked_quantity > 1 ) THEN
6015                   l_shipping_attr(1).serial_number  := NULL;
6016 
6017                   IF ( l_debug = 1 ) THEN
6018                     DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6019                            l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6020                   END IF;
6021 
6022                   WSH_INTEGRATION.Set_INV_PC_Attributes(
6023                     p_in_attributes => l_invpcinrectype
6024                   , x_return_status => l_return_status
6025                   , x_msg_count     => l_msg_data
6026                   , x_msg_data      => l_msg_count );
6027 
6028                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6029                     IF (l_debug = 1) THEN
6030                       DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6031                     END IF;
6032 
6033                     RAISE fnd_api.g_exc_error;
6034                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6035                     IF (l_debug = 1) THEN
6036                       DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6037                     END IF;
6038 
6039                     RAISE fnd_api.g_exc_unexpected_error;
6040                   END IF;
6041                 ELSE
6042                   BEGIN
6043                     SELECT fm_serial_number
6044                       INTO l_shipping_attr(1).serial_number
6045                       FROM mtl_serial_numbers_temp
6046                      WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6047                        AND ROWNUM < 2;
6048 
6049                     IF ( l_debug = 1 ) THEN
6050                       DEBUG('found fm_serial='|| l_shipping_attr(1).serial_number||
6051                             ' for transaction_temp_id='||l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6052                     END IF;
6053 
6054                     DELETE FROM mtl_serial_numbers_temp
6055                     WHERE  transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6056                   EXCEPTION
6057                     WHEN NO_DATA_FOUND THEN
6058                       IF (l_debug = 1) THEN
6059                         DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6060                                l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6061                       END IF;
6062                   END;
6063                 END IF;
6064               END IF; -- for serial controlled items
6065 
6066           --patchset J.  Shipping API cleanup
6067           l_delivery_detail_tab(i).requested_quantity := l_delivery_detail_tab(i).requested_quantity - l_rsv_qty;
6068           /* 4440809
6069              WSH_DELIVERY_DETAILS_PUB.split_line
6070                  (p_api_version   => 1.0,
6071                   p_init_msg_list => fnd_api.g_false,
6072                   p_commit        => fnd_api.g_false,
6073                   x_return_status => l_return_status,
6074                   x_msg_count     => l_msg_count,
6075                   x_msg_data      => l_msg_data,
6076                   p_from_detail_id => l_delivery_detail_tab(i).delivery_detail_id,
6077                   x_new_detail_id => l_new_delivery_detail_id,
6078                   x_split_quantity => l_delivery_detail_tab(i).requested_quantity,
6079                   x_split_quantity2 => l_dummy_num_var);
6080 
6081                  IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6082                    IF (l_debug = 1) THEN
6083                      DEBUG('return error from split_delivery_details ', 'stage_lpns');
6084                    END IF;
6085 
6086                    RAISE fnd_api.g_exc_error;
6087                  ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6088                    IF (l_debug = 1) THEN
6089                      DEBUG('return unexpected error from split_delivery_details', 'stage_lpns');
6090                    END IF;
6091 
6092                    RAISE fnd_api.g_exc_unexpected_error;
6093                  END IF;
6094              --\Shipping API cleanup
6095 
6096                 IF (l_debug = 1) THEN
6097                       DEBUG('l_new_delivery_detail_id:=' || l_new_delivery_detail_id, 'STAGE_LPNS');
6098                 END IF; 4440809 */
6099 
6100               --Call shipping api to set transaction_temp_id global variable
6101               IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
6102                 IF (l_shipping_attr(1).picked_quantity > 1) THEN
6103                   l_shipping_attr(1).serial_number  := NULL;
6104 
6105                   IF (l_debug = 1) THEN
6106                     DEBUG('Calling Set_Inv_PC_Attributes transaction_temp_id=' ||
6107                            l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6108                   END IF;
6109 
6110                   wsh_integration.set_inv_pc_attributes(
6111                     p_in_attributes              => l_invpcinrectype
6112                   , x_return_status              => l_return_status
6113                   , x_msg_count                  => l_msg_data
6114                   , x_msg_data                   => l_msg_count
6115                   );
6116 
6117                   IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6118                     IF (l_debug = 1) THEN
6119                       DEBUG('return error E from Set_Inv_PC_Attributes', 'STAGE_LPN');
6120                     END IF;
6121 
6122                     RAISE fnd_api.g_exc_error;
6123                   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6124                     IF (l_debug = 1) THEN
6125                       DEBUG('return error U from Set_Inv_PC_Attributes', 'STAGE_LPN');
6126                     END IF;
6127 
6128                     RAISE fnd_api.g_exc_unexpected_error;
6129                   END IF;
6130                 ELSE
6131                   BEGIN
6132                     SELECT fm_serial_number
6133                       INTO l_shipping_attr(1).serial_number
6134                       FROM mtl_serial_numbers_temp
6135                      WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id
6136                        AND ROWNUM < 2;
6137 
6138                     IF (l_debug = 1) THEN
6139                       DEBUG(
6140                            'found fm_serial='
6141                         || l_shipping_attr(1).serial_number
6142                         || ' for transaction_temp_id='
6143                         || l_invpcinrectype.transaction_temp_id
6144                       , 'STAGE_LPN'
6145                       );
6146                     END IF;
6147 
6148                     DELETE FROM mtl_serial_numbers_temp
6149                           WHERE transaction_temp_id = l_invpcinrectype.transaction_temp_id;
6150                   EXCEPTION
6151                     WHEN NO_DATA_FOUND THEN
6152                       IF (l_debug = 1) THEN
6153                         DEBUG('No rows found in MSNT for transaction_temp_id=' ||
6154                                l_invpcinrectype.transaction_temp_id, 'STAGE_LPN');
6155                       END IF;
6156                   END;
6157                 END IF;
6158               END IF;
6159               IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL AND
6160               l_delivery_detail_tab(i).ont_pricing_qty_source='S') THEN
6161                  IF (l_debug = 1) THEN
6162                    DEBUG('Catch Weight updates ', 'stage_lpns');
6163                  END IF;
6164                 l_shipping_attr(1).picked_quantity2 := GET_CATCH_WEIGHT(l_shipping_attr(1).ship_from_org_id
6165                                   ,l_shipping_attr(1).transfer_lpn_id
6166                                   ,l_shipping_attr(1).inventory_item_id
6167                                   ,l_shipping_attr(1).revision
6168                                   ,l_shipping_attr(1).lot_number
6169                                   ,l_shipping_attr(1).picked_quantity);
6170               END IF;
6171               IF (l_debug = 1) THEN
6172                 DEBUG('Call  update shipping attributes', 'stage_lpns');
6173               END IF;
6174               wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
6175               , x_return_status              => l_return_status);
6176 
6177               IF (l_debug = 1) THEN
6178                 DEBUG('after update shipping attributes', 'stage_lpns');
6179               END IF;
6180 
6181               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6182                 IF (l_debug = 1) THEN
6183                   DEBUG('return error from update shipping attributes 2', 'stage_lpns');
6184                 END IF;
6185 
6186                 RAISE fnd_api.g_exc_error;
6187               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6188                 IF (l_debug = 1) THEN
6189                   DEBUG('return unexpected error from update shipping attributes', 'stage_lpns');
6190                 END IF;
6191 
6192                 RAISE fnd_api.g_exc_unexpected_error;
6193               END IF;
6194 
6195           --Since we have split the WDD line, we need to process
6196           --this newly split line also.
6197           --Setting these variables will process the newly split line
6198           /* 4440809: l_delivery_detail_tab(i).delivery_detail_id := l_new_delivery_detail_id; */
6199               /*****************************************
6200           * Bug No 3390432
6201           * Only changing the Transaction_temp_id of WDD to Null value when
6202           * serial control code is 2 or 5
6203           ******************************************/
6204           IF(l_serial_number_control_code IN (2,5)) THEN -- Serial NOT in MSNT
6205             l_delivery_detail_tab(i).transaction_temp_id := NULL;
6206           END IF;
6207           l_invpcinrectype.transaction_temp_id :=
6208         l_delivery_detail_tab(i).transaction_temp_id;
6209 
6210 
6211               l_rsv_index                                  := j + 1;
6212               l_rsv_qty                                    := 0;
6213             END IF; --Res. Qty Match l_rsv_qty < l_delivery_detail_tab(i).requested_quantity
6214             -- IF-2
6215 
6216             <<next_resv_rec>>
6217             IF (l_rsv_index > l_mtl_reservation_tbl.COUNT) THEN
6218               l_rsv_index  := 0;
6219               l_rsv_qty    := 0;
6220               EXIT delivery_detail_loop; -- 2529382
6221             END IF;
6222 
6223           END LOOP; -- Reservation Loop
6224         END LOOP; -- Delivery Detail Loop
6225       ELSE -- Non Reservable
6226         IF (l_debug = 1) THEN
6227           DEBUG('Item or Sub in non reservable so stage the line without updating the inventory details');
6228         END IF;
6229 
6230         FOR i IN 1 .. l_delivery_detail_tab.COUNT LOOP
6231           l_shipping_attr.DELETE;
6232           l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_tab(i).delivery_detail_id;
6233           l_shipping_attr(1).released_status     := 'Y';
6234           wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
6235           , x_return_status              => l_return_status);
6236 
6237           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6238             IF (l_debug = 1) THEN
6239               DEBUG('Non-Reservable: return error from update shipping attributes', 'stage_lpns');
6240             END IF;
6241 
6242             RAISE fnd_api.g_exc_error;
6243           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6244             IF (l_debug = 1) THEN
6245               DEBUG('Non-Reservable: return unexpected error from update shipping attributes', 'stage_lpns');
6246             END IF;
6247 
6248             RAISE fnd_api.g_exc_unexpected_error;
6249           END IF;
6250         END LOOP; -- Non-Reservable delivery_details
6251       END IF; -- Reservable
6252     END LOOP; -- STAGE_LINE;
6253 
6254     CLOSE stage_lines;
6255   END LOOP; -- OUTER LPN
6256 
6257   CLOSE outer_lpn;
6258 
6259   BEGIN
6260     OPEN outer_lpn;
6261 
6262     LOOP
6263       FETCH outer_lpn INTO l_outer_lpn;
6264       EXIT WHEN outer_lpn%NOTFOUND;
6265 
6266       IF (l_debug = 1) THEN
6267         DEBUG('populate_wstt   ', 'stage_lpns');
6268       END IF;
6269 
6270       wms_shipping_transaction_pub.populate_wstt(
6271         x_return                     => l_return
6272       , x_msg_code                   => x_msg_data
6273       , p_organization_id            => p_organization_id
6274       , p_lpn_id                     => l_outer_lpn.lpn_id
6275       , p_trip_id                    => 0
6276       , p_dock_door_id               => p_dock_door_id
6277       , p_direct_ship_flag           => 'Y'
6278       );
6279 
6280       IF l_return = 1 THEN
6281         x_return_status  := fnd_api.g_ret_sts_error;
6282         RAISE fnd_api.g_exc_error;
6283       END IF;
6284 
6285       SELECT COUNT(*)
6286         INTO l_trip_id
6287         FROM wms_shipping_transaction_temp
6288        WHERE outermost_lpn_id = l_outer_lpn.lpn_id;
6289 
6290       IF (l_debug = 1) THEN
6291         DEBUG('After Populate WSTT, No of LPNs loaded :' || TO_CHAR(l_trip_id), 'stage_lpns');
6292       END IF;
6293 
6294       -- Update container Hierarchy so that container so that following cols has same value as delivery lines
6295       -- SHIP_TO_LOCATION_ID, SHIP_TO_LOCATION_ID, DELIVER_TO_LOCATION_ID
6296       BEGIN
6297 	 IF l_trip_id > 0 THEN
6298 	    UPDATE wms_license_plate_numbers
6299 	      SET lpn_context = 9
6300 	      WHERE outermost_lpn_id = l_outer_lpn.lpn_id;
6301 
6302 	    IF (l_debug = 1) THEN
6303 	       DEBUG('Update LPN Context to Loaded to Dock, LPN :' || TO_CHAR(l_outer_lpn.lpn_id), 'Stage_LPNs');
6304 	    END IF;
6305 	 END IF;
6306       EXCEPTION
6307 	 WHEN NO_DATA_FOUND THEN
6308 	    NULL;
6309       END;
6310 
6311       wms_shipping_transaction_pub.create_delivery(
6312 						  p_outermost_lpn_id           => l_outer_lpn.lpn_id
6313 						  , p_trip_id                    => NULL
6314 						  , p_organization_id            => p_organization_id
6315 						  , p_dock_door_id               => p_dock_door_id
6316 						  , x_delivery_id                => l_delivery_id
6317 						  , x_return_status              => l_return_status
6318 						  , x_message                    => l_msg_data
6319 						  , p_direct_ship_flag           => 'Y'
6320 						  );
6321 
6322      IF (l_debug = 1) THEN
6323         DEBUG('Create Delivery  :' || TO_CHAR(l_delivery_id) || ' Return Status ' || l_return_status, 'stage_lpns');
6324      END IF;
6325 
6326      IF l_return_status IN('S') THEN
6327 	IF (l_debug = 1) THEN
6328 	   DEBUG('return success from create_delivery ', 'stage_lpns');
6329 	END IF;
6330 
6331 	x_return_status  := fnd_api.g_ret_sts_success;
6332       ELSIF l_return_status IN('E') THEN
6333 	IF (l_debug = 1) THEN
6334 	   DEBUG('return error from create_delivery', 'stage_lpns');
6335 	END IF;
6336 
6337 	x_return_status  := fnd_api.g_ret_sts_error;
6338 	RAISE fnd_api.g_exc_error;
6339       ELSE
6340 	IF (l_debug = 1) THEN
6341 	   DEBUG('return unexpected error from create_delivery', 'stage_lpns');
6342 	END IF;
6343 
6344 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
6345 	RAISE fnd_api.g_exc_unexpected_error;
6346      END IF;
6347 
6348      --- <Changes for Delivery Merge>
6349      IF g_wms_current_release_level >= g_j_release_level THEN
6350 
6351 	l_action_prms.caller := 'WMS_DLMG';
6352 	l_action_prms.event := wsh_interface_ext_grp.g_start_of_shipping;
6353 	l_action_prms.action_code := 'ADJUST-PLANNED-FLAG';
6354 
6355 	l_delivery_id_tab(1) := l_delivery_id;
6356 
6357 	wsh_interface_ext_grp.delivery_action
6358 	  (p_api_version_number     => 1.0,
6359 	   p_init_msg_list          => fnd_api.g_false,
6360 	   p_commit                 => fnd_api.g_false,
6361 	   p_action_prms            => l_action_prms,
6362 	   p_delivery_id_tab        => l_delivery_id_tab,
6363 	   x_delivery_out_rec       => l_delivery_out_rec,
6364 	   x_return_status          => l_return_status,
6365 	   x_msg_count              => l_msg_count,
6366 	   x_msg_data               => l_msg_data);
6367 
6368 	IF (l_debug = 1) THEN
6369 	   debug('Called Adjust Planned Status with return status: ' || l_return_status, 'stage_lpns');
6370 	END IF;
6371 
6372 	-- Do not error out even if the call fails.
6373 
6374 
6375 	-- g-log changes
6376 
6377 	IF WSH_UTIL_CORE.GC3_IS_INSTALLED = 'Y' THEN
6378 	   IF (l_debug = 1) THEN
6379 	      debug('G-Log Changes: G-Log installed', 'LPN_CHECK');
6380 	   END IF;
6381 
6382 	   OPEN c_Get_OTM_flag(l_delivery_id);
6383 	   FETCH c_Get_OTM_flag INTO l_ignore_for_planning, l_tms_interface_flag;
6384 	   IF (l_debug = 1) THEN
6385 	      debug('l_ignore_for_planning : '|| l_ignore_for_planning, 'LPN_CHECK');
6386 	      debug('l_tms_interface_flag : '|| l_tms_interface_flag, 'LPN_CHECK');
6387 	   END IF;
6388 
6389 	   IF (c_Get_OTM_flag%NOTFOUND) THEN
6390 	      IF (l_debug = 1) THEN
6391 		 debug('No WDDs found for the delivery created ', 'LPN_CHECK');
6392 	      END IF;
6393 	   END IF;
6394 	   CLOSE c_Get_OTM_flag;
6395 
6396 	   --Important Note: Irrespective of the severity level of 'CR' exception for the delivery just
6397 	   --created, we have to mark the delivery to ignore_for_planning so that the
6398 	   --transaction goes through fine.
6399 	   -- Hence there is no call to WSH_INTERFACE_EXT_GRP.OTM_PRE_SHIP_CONFIRM().
6400 	   -- Here delivery was created in the backend for the line that the use
6401 	   -- chose to ship confirm. it IS ALL happening in the backend.
6402 
6403 	   IF l_ignore_for_planning = 'N' AND l_tms_interface_flag = 'CR' THEN
6404 	      l_action_prms.action_code := 'IGNORE_PLAN';
6405 	      wsh_interface_ext_grp.delivery_action
6406 		(p_api_version_number     => 1.0,
6407 		 p_init_msg_list          => fnd_api.g_false,
6408 		 p_commit                 => fnd_api.g_false,
6409 		 p_action_prms            => l_action_prms,
6410 		 p_delivery_id_tab        => l_delivery_id_tab,
6411 		 x_delivery_out_rec       => l_delivery_out_rec,
6412 		 x_return_status          => l_return_status,
6413 		 x_msg_count              => l_msg_count,
6414 		 x_msg_data               => l_msg_data);
6415 
6416 	      IF (l_debug = 1) THEN
6417 		 debug('Called wsh_interface_ext_grp.delivery_action with action_code IGNORE_PLAN and return status: ' || l_return_status, 'stage_lpns');
6418 	      END IF;
6419 
6420 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
6421 		 x_msg_data := 'WMS_DELIVERY_ACTION_FAIL';
6422 		 RAISE fnd_api.g_exc_error;
6423 	       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6424 		 x_msg_data := 'WMS_DELIVERY_ACTION_FAIL';
6425 		 RAISE fnd_api.g_exc_unexpected_error;
6426 	      END IF;
6427 	   END IF;
6428 
6429 	END IF; --g-log changes
6430 
6431      END IF;
6432      -- </Changes for delivery merge>
6433 
6434     END LOOP;
6435 
6436     CLOSE outer_lpn;
6437   END;        -- Populate WSTT and Update the LPN context
6438               -- Stage the delivery details that have been updated just now
6439   IF (l_debug = 1) THEN
6440      DEBUG('Exiting  Stage LPNs..: ' , 'STAGE_LPN');
6441   END IF;
6442        --COMMIT;
6443 EXCEPTION
6444    WHEN fnd_api.g_exc_error THEN
6445       x_return_status  := fnd_api.g_ret_sts_error;
6446 
6447       IF (l_debug = 1) THEN
6448 	 DEBUG('In exception type E', 'stage_lpns');
6449       END IF;
6450       --      ROLLBACK;
6451       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6452 
6453    WHEN fnd_api.g_exc_unexpected_error THEN
6454       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6455 
6456       IF (l_debug = 1) THEN
6457 	 DEBUG('In exception type U', 'stage_lpns');
6458       END IF;
6459 
6460       --      ROLLBACK;
6461     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6462    WHEN OTHERS THEN
6463       x_return_status  := wsh_util_core.g_ret_sts_unexp_error;
6464 
6465       IF (l_debug = 1) THEN
6466 	 DEBUG('Exception When Others ' || SQLERRM, 'Stage_LPNS');
6467       END IF;
6468 END stage_lpns;
6469 
6470 PROCEDURE load_truck(
6471   x_return_status OUT NOCOPY    VARCHAR2
6472 , x_msg_data      OUT NOCOPY    VARCHAR2
6473 , x_msg_count     OUT NOCOPY    NUMBER
6474 , p_group_id      IN            NUMBER
6475 , p_org_id        IN            NUMBER
6476 , p_dock_door_id  IN            NUMBER
6477 ) IS
6478 BEGIN
6479   --bug #2782201
6480   NULL;
6481 /*
6482   This procedure is no more used from Patchset I. For corresponding functionality
6483   please look load_lpn.
6484  */
6485 END load_truck;
6486 
6487 PROCEDURE close_truck(
6488   x_return_status    OUT NOCOPY    VARCHAR2
6489 , x_msg_data         OUT NOCOPY    VARCHAR2
6490 , x_msg_count        OUT NOCOPY    NUMBER
6491 , x_error_code       OUT NOCOPY    NUMBER
6492 , x_missing_item_cur OUT NOCOPY    t_genref
6493 , p_dock_door_id     IN            NUMBER
6494 , p_group_id         IN            NUMBER
6495 , p_org_id           IN            NUMBER
6496 ) IS
6497   CURSOR trip_cursor IS
6498     SELECT DISTINCT trip_id
6499                FROM wms_shipping_transaction_temp
6500               WHERE dock_door_id = p_dock_door_id
6501                 AND organization_id = p_org_id
6502                 AND direct_ship_flag = 'Y';
6503 
6504   l_return_status       VARCHAR2(1);
6505   l_msg_count           NUMBER;
6506   l_msg_data            VARCHAR2(20000);
6507   l_trip_cursor         trip_cursor%ROWTYPE;
6508   l_vehicle_item_id     NUMBER;   --Bug#6878013
6509   l_debug               NUMBER                            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6510   l_ship_method_code    wsh_trips.ship_method_code%TYPE;
6511   l_enforce_ship_method VARCHAR2(1);
6512   l_trip_name           wsh_trips.NAME%TYPE;
6513   no_ship_method_code   EXCEPTION;
6514 
6515 BEGIN
6516   --SAVEPOINT closetruck;
6517   x_return_status  := fnd_api.g_ret_sts_success;
6518 
6519   IF (l_debug = 1) THEN
6520     inv_trx_util_pub.TRACE('In close truck program ', 'close_truck', 9);
6521     inv_trx_util_pub.TRACE('p_dock_door_id     : ' || p_dock_door_id     , 'close_truck', 9);
6522     inv_trx_util_pub.TRACE('p_group_id         : ' || p_group_id         , 'close_truck', 9);
6523     inv_trx_util_pub.TRACE('p_org_id           : ' || p_org_id           , 'close_truck', 9);
6524   END IF;
6525 
6526   IF (l_debug = 1) THEN
6527     DEBUG('Before call to Confirm All deliveries API close_truck');
6528   END IF;
6529 
6530   wms_direct_ship_pvt.confirm_all_deliveries(
6531     x_return_status              => l_return_status
6532   , x_msg_count                  => l_msg_count
6533   , x_msg_data                   => l_msg_data
6534   , x_missing_item_cur           => x_missing_item_cur
6535   , x_error_code                 => x_error_code
6536   , p_delivery_id                => NULL
6537   , p_net_weight                 => NULL
6538   , p_gross_weight               => NULL
6539   , p_wt_uom_code                => NULL
6540   , p_waybill                    => NULL
6541   , p_ship_method_code           => NULL
6542   , p_fob_code                   => NULL
6543   , p_fob_location_id            => NULL
6544   , p_freight_term_code          => NULL
6545   , p_freight_term_name          => NULL
6546   , p_intmed_shipto_loc_id       => NULL
6547   , p_org_id                     => p_org_id
6548   , p_dock_door_id               => p_dock_door_id
6549   );
6550 
6551   IF l_return_status = fnd_api.g_ret_sts_error THEN
6552     IF (l_debug = 1) THEN
6553       inv_trx_util_pub.TRACE('Confirm All deliveries API failed with status E ', 'close_truck', 9);
6554     END IF;
6555 
6556     IF (l_debug = 1) THEN
6557       DEBUG('Confirm All deliveries API failed with status E close_truck');
6558     END IF;
6559 
6560     RAISE fnd_api.g_exc_error;
6561   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6562     IF (l_debug = 1) THEN
6563       inv_trx_util_pub.TRACE('Confirm All deliveries failed with status U', 'close_truck', 9);
6564     END IF;
6565 
6566     IF (l_debug = 1) THEN
6567       DEBUG('Confirm All deliveries failed with status U close_truck');
6568     END IF;
6569 
6570     RAISE fnd_api.g_exc_unexpected_error;
6571   END IF;
6572 
6573   IF (l_debug = 1) THEN
6574     inv_trx_util_pub.TRACE('Confirm All deliveries successfully completed ', 'close_truck', 9);
6575   END IF;
6576 
6577   IF (l_debug = 1) THEN
6578      DEBUG('Confirm All deliveries successfully completed  close_truck');
6579      debug('Get the enforce_ship_method flag','close_truck');
6580   END IF;
6581 
6582   INV_SHIPPING_TRANSACTION_PUB.get_enforce_ship
6583     (p_org_id         => p_org_id
6584      ,x_enforce_ship  => l_enforce_ship_method
6585      ,x_return_status => l_return_status
6586      ,x_msg_data      => l_msg_data
6587      ,x_msg_count     => l_msg_count);
6588 
6589   IF (l_debug=1) THEN
6590      debug('get_enforce_ship returned status: ' || l_return_status,'Close_truck');
6591      debug('Enforce_ship_method flag: ' || l_enforce_ship_method,'Close_truck');
6592   END IF;
6593 
6594   OPEN trip_cursor;
6595 
6596   LOOP
6597     FETCH trip_cursor INTO l_trip_cursor;
6598     EXIT WHEN trip_cursor%NOTFOUND;
6599 
6600     IF (l_debug = 1) THEN
6601       DEBUG('Before call to Close Trip API ', 'close_truck');
6602     END IF;
6603 
6604     /*Bug 2980013:Check if the Ship method Code is populated if the Enforce Ship method Flag is set to Y*/
6605     BEGIN
6606       SELECT ship_method_code
6607            , NAME
6608         INTO l_ship_method_code
6609            , l_trip_name
6610         FROM wsh_trips_ob_grp_v
6611        WHERE trip_id = l_trip_cursor.trip_id;
6612     EXCEPTION
6613       WHEN NO_DATA_FOUND THEN
6614         l_ship_method_code  := NULL;
6615     END;
6616 
6617     IF l_enforce_ship_method = 'Y'
6618        AND l_ship_method_code IS NULL THEN
6619       RAISE no_ship_method_code;
6620     END IF;
6621 
6622      /*Bug6878013 If the Vehicle Item Id is not null, pass its value else pass NULL*/
6623      BEGIN
6624         SELECT vehicle_item_id
6625             INTO l_vehicle_item_id
6626             FROM wsh_trips_ob_grp_v
6627             WHERE trip_id = l_trip_cursor.trip_id;
6628       EXCEPTION
6629         WHEN NO_DATA_FOUND THEN
6630           l_vehicle_item_id := NULL;
6631       END;
6632 
6633       wms_direct_ship_pvt.close_trip(
6634         x_return_status              => l_return_status
6635       , x_msg_count                  => l_msg_count
6636       , x_msg_data                   => l_msg_data
6637       , p_trip_id                    => l_trip_cursor.trip_id
6638       , p_vehicle_item_id            => l_vehicle_item_id  --Bug#6878013
6639       , p_vehicle_num_prefix         => NULL
6640       , p_vehicle_num                => NULL
6641       , p_seal_code                  => NULL
6642       , p_document_set_id            => NULL
6643       , p_org_id                     => p_org_id
6644       , p_dock_door_id               => p_dock_door_id
6645       , p_ship_method_code           => l_ship_method_code
6646       );
6647 
6648       IF (l_debug = 1) THEN
6649         DEBUG('After call to Close Trip API close_truck');
6650       END IF;
6651 
6652       IF l_return_status = fnd_api.g_ret_sts_error THEN
6653         IF (l_debug = 1) THEN
6654           DEBUG('Close trip API failed with status E ', 'close_truck');
6655         END IF;
6656 
6657         RAISE fnd_api.g_exc_error;
6658       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6659         IF (l_debug = 1) THEN
6660           DEBUG('Close trip failed with status U', 'close_truck');
6661         END IF;
6662 
6663         RAISE fnd_api.g_exc_unexpected_error;
6664       END IF;
6665 
6666       IF (l_debug = 1) THEN
6667         DEBUG('Close trip API completed successfully ', 'close_truck');
6668       END IF;
6669 
6670   END LOOP;
6671 
6672   CLOSE trip_cursor;
6673 
6674   IF (l_debug = 1) THEN
6675     DEBUG('End of close truck ', 'close_truck', 9);
6676   END IF;
6677 EXCEPTION
6678   WHEN no_ship_method_code THEN
6679     x_return_status  := fnd_api.g_ret_sts_error;
6680     fnd_message.set_name('WMS', 'WMS_SHIP_METHOD_CODE');
6681     fnd_message.set_token('TRIP_NAME', l_trip_name);
6682     /* No Ship method code provided for the Trip .This is required */
6683     fnd_msg_pub.ADD;
6684     /*fnd_msg_pub.count_and_get( p_count => x_msg_count,
6685                           p_data  => x_msg_data
6686                         );*/
6687     DEBUG('In exception no_ship_method_code ', 'Close Trip');
6688   WHEN fnd_api.g_exc_error THEN
6689     x_return_status  := fnd_api.g_ret_sts_error;
6690     ROLLBACK; --TO closetruck;
6691     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6692   WHEN fnd_api.g_exc_unexpected_error THEN
6693     x_return_status  := fnd_api.g_ret_sts_unexp_error;
6694     ROLLBACK; --TO closetruck;
6695     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6696   WHEN OTHERS THEN
6697     x_return_status  := fnd_api.g_ret_sts_unexp_error;
6698     ROLLBACK; --TO closetruck;
6699     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6700 END;
6701 
6702   --CLOSE_TRUCK
6703 
6704 PROCEDURE get_lpn_available_quantity(
6705   x_return_status     OUT NOCOPY    VARCHAR2
6706 , x_msg_count         OUT NOCOPY    NUMBER
6707 , x_msg_data          OUT NOCOPY    VARCHAR2
6708 , p_organization_id   IN            NUMBER
6709 , p_lpn_id            IN            NUMBER
6710 , p_inventory_item_id IN            NUMBER
6711 , p_revision          IN            VARCHAR2
6712 , p_line_id           IN            NUMBER
6713 , p_header_id         IN            NUMBER
6714 , x_qoh               OUT NOCOPY    NUMBER
6715 , x_att               OUT NOCOPY    NUMBER
6716 ) IS
6717   CURSOR inner_lpn(p_lpn NUMBER) IS
6718     SELECT lpn_id
6719          , subinventory_code
6720          , locator_id
6721       FROM wms_license_plate_numbers
6722      WHERE outermost_lpn_id = p_lpn;
6723 
6724   l_rqoh                  NUMBER;
6725   l_qr                    NUMBER;
6726   l_qs                    NUMBER;
6727   l_atr                   NUMBER;
6728   l_revision_control      NUMBER;
6729   l_lot_control           NUMBER;
6730   l_serial_control        NUMBER;
6731   l_is_revision_control   BOOLEAN             := FALSE;
6732   l_is_lot_control        BOOLEAN             := FALSE;
6733   l_is_serial_control     BOOLEAN             := FALSE;
6734   l_inner_lpn             inner_lpn%ROWTYPE;
6735   l_msg_data              VARCHAR2(20000);
6736   l_order_source_id       NUMBER;
6737   l_demand_source_type_id NUMBER;
6738   l_debug                 NUMBER              := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6739 BEGIN
6740   x_return_status  := fnd_api.g_ret_sts_success;
6741 
6742   IF (l_debug = 1) THEN
6743     DEBUG('The value of lpn_id is ' || p_lpn_id, 'GET_LPN_AVAILABLE_QUANTITY');
6744     DEBUG('The value of p_inventory_item_id is ' || p_inventory_item_id, 'GET_LPN_AVAILABLE_QUANTITY');
6745     DEBUG('The value of p_revision is ' || NVL(p_revision, 'A'), 'GET_LPN_AVAILABLE_QUANTITY');
6746     DEBUG('The value of p_line_id is ' || p_line_id, 'GET_LPN_AVAILABLE_QUANTITY');
6747     DEBUG('The value of p_header_id is ' || p_header_id, 'GET_LPN_AVAILABLE_QUANTITY');
6748   END IF;
6749 
6750   inv_quantity_tree_pub.clear_quantity_cache;
6751 
6752   SELECT revision_qty_control_code
6753        , lot_control_code
6754        , serial_number_control_code
6755     INTO l_revision_control
6756        , l_lot_control
6757        , l_serial_control
6758     FROM mtl_system_items
6759    WHERE organization_id = p_organization_id
6760      AND inventory_item_id = p_inventory_item_id;
6761 
6762   IF (l_revision_control = 2) THEN
6763     l_is_revision_control  := TRUE;
6764   END IF;
6765 
6766   IF (l_lot_control = 2) THEN
6767     l_is_lot_control  := TRUE;
6768   END IF;
6769 
6770   IF (l_serial_control = 2
6771       OR l_serial_control = 5
6772       OR l_serial_control = 6) THEN
6773     l_is_serial_control  := TRUE;
6774   END IF;
6775 
6776   SELECT order_source_id
6777     INTO l_order_source_id
6778     FROM oe_order_headers_all
6779    WHERE header_id = p_header_id;
6780 
6781   IF l_order_source_id = 10 THEN
6782     l_demand_source_type_id  := 8;
6783   ELSE
6784     l_demand_source_type_id  := 2;
6785   END IF;
6786 
6787   x_att            := 0;
6788   OPEN inner_lpn(p_lpn_id);
6789 
6790   LOOP
6791     FETCH inner_lpn INTO l_inner_lpn;
6792     EXIT WHEN inner_lpn%NOTFOUND;
6793     /* Bug 2440408: All the Trees in the cache have to be cleared so that trees are rebuild */
6794     inv_quantity_tree_pub.clear_quantity_cache;
6795     inv_quantity_tree_pub.query_quantities(
6796       p_api_version_number         => 1.0
6797     , p_init_msg_lst               => fnd_api.g_false
6798     , x_return_status              => x_return_status
6799     , x_msg_count                  => x_msg_count
6800     , x_msg_data                   => x_msg_data
6801     , p_organization_id            => p_organization_id
6802     , p_inventory_item_id          => p_inventory_item_id
6803     , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
6804     , p_is_revision_control        => l_is_revision_control
6805     , p_is_lot_control             => l_is_lot_control
6806     , p_is_serial_control          => l_is_serial_control
6807     , p_demand_source_type_id      => l_demand_source_type_id
6808     , p_demand_source_header_id    => inv_salesorder.get_salesorder_for_oeheader(p_header_id)
6809     , p_demand_source_line_id      => p_line_id
6810     , p_revision                   => p_revision
6811     , p_lot_number                 => NULL
6812     , p_subinventory_code          => NULL
6813     , p_locator_id                 => NULL
6814     , x_qoh                        => x_qoh
6815     , x_rqoh                       => l_rqoh
6816     , x_qr                         => l_qr
6817     , x_qs                         => l_qs
6818     , x_att                        => x_att
6819     , x_atr                        => l_atr
6820     , p_lpn_id                     => l_inner_lpn.lpn_id
6821     );
6822 
6823     IF (l_debug = 1) THEN
6824       DEBUG('The value of x_att is ' || x_att, 'GET_LPN_AVAILABLE_QUANTITY');
6825     END IF;
6826 
6827     -- If the qty tree returns and error raise an exception.
6828     IF x_return_status <> fnd_api.g_ret_sts_success THEN
6829       IF (l_debug = 1) THEN
6830         inv_log_util.TRACE('Qty Tree Failed' || l_msg_data, 'INV_VMI_VALIDATIONS', 9);
6831       END IF;
6832 
6833       RAISE fnd_api.g_exc_unexpected_error;
6834     END IF;
6835 
6836     IF (x_att > 0) THEN
6837       IF (l_debug = 1) THEN
6838         DEBUG('The value of x_att is - in if ' || x_att, 'GET_LPN_AVAILABLE_QUANTITY');
6839       END IF;
6840 
6841       x_return_status  := fnd_api.g_ret_sts_success;
6842       EXIT;
6843     END IF;
6844   END LOOP;
6845 
6846   x_return_status  := fnd_api.g_ret_sts_success;
6847 EXCEPTION
6848   WHEN OTHERS THEN
6849     IF (l_debug = 1) THEN
6850       inv_log_util.TRACE('When others Exception in get_available_vmi_quantity', 'GET_LPN_AVAILABLE_QUANTITY', 9);
6851     END IF;
6852 
6853     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6854     x_return_status  := fnd_api.g_ret_sts_unexp_error;
6855     RETURN;
6856 END get_lpn_available_quantity;
6857 
6858 PROCEDURE create_update_containers(
6859   x_return_status    OUT NOCOPY    VARCHAR2
6860 , x_msg_count        OUT NOCOPY    NUMBER
6861 , x_msg_data         OUT nocopy    VARCHAR2
6862 , p_org_id           IN            NUMBER
6863 , p_outermost_lpn_id IN NUMBER
6864 , p_delivery_id      IN  NUMBER DEFAULT NULL
6865 ) IS
6866   CURSOR lpn_details IS
6867     SELECT lpn_id
6868          , license_plate_number
6869          , subinventory_code
6870          , locator_id
6871          , inventory_item_id
6872          , revision
6873          , lot_number
6874          , serial_number
6875          , gross_weight_uom_code
6876          , gross_weight
6877          , tare_weight_uom_code
6878          , tare_weight
6879          , content_volume_uom_code
6880          , content_volume
6881       -- Release 12 (K): LPN Synchronization
6882       -- Add following new columns
6883          , container_volume
6884          , container_volume_uom
6885          , organization_id
6886       FROM wms_license_plate_numbers
6887      WHERE organization_id = p_org_id
6888        AND outermost_lpn_id = p_outermost_lpn_id;
6889 
6890   l_lpn_ids            wsh_util_core.id_tab_type;
6891   l_container_ids      wsh_util_core.id_tab_type;
6892   l_lpn_id             NUMBER;
6893   l_segmentarray       fnd_flex_ext.segmentarray;
6894   l_count              NUMBER                                     := 0;
6895   l_net_weight         NUMBER;
6896   l_deliv_det_id       NUMBER;
6897   l_container_name     wsh_delivery_details.container_name%TYPE;
6898   l_container_flag     wsh_delivery_details.container_flag%TYPE;
6899   l_container_rec      wsh_container_grp.changedattributetabtype;
6900   -- added for bug 2529382
6901   l_status_code        VARCHAR2(2);
6902   l_container_new_name VARCHAR2(30);
6903   --Bug number:2701925:
6904   l_delivery_detail_id NUMBER;
6905   l_delivery_name VARCHAR2(30) := NULL;
6906   l_debug              NUMBER                                     := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6907 
6908   -- Release 12 (K): LPN Synchronization/Convergence
6909   -- Types needed for WSH_WMS_LPN_GRP.Create_Update_Containers
6910   l_lpn_cur       WMS_Data_Type_Definitions_PUB.LPNRecordType;
6911   l_wsh_dd_rec    WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Rec_Type;
6912   l_wsh_dd_upd_rec  WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Rec_Type;
6913   wsh_create_tbl  WSH_GLBL_VAR_STRCT_GRP.delivery_details_Attr_tbl_Type;
6914   wsh_update_tbl  WSH_GLBL_VAR_STRCT_GRP.delivery_details_Attr_tbl_Type;
6915   l_IN_rec        WSH_GLBL_VAR_STRCT_GRP.detailInRecType;
6916   l_OUT_rec       WSH_GLBL_VAR_STRCT_GRP.detailOutRecType;
6917 
6918 
6919 BEGIN
6920   x_return_status  := fnd_api.g_ret_sts_success;
6921 
6922   /* Release 12(K), changed to use l_lpn_cur as type of WMS_Data_Type_Definitions_PUB.LPNRecordType; */
6923   OPEN lpn_details;
6924   LOOP
6925     l_lpn_cur := null;
6926     FETCH lpn_details INTO
6927                l_lpn_cur.lpn_id
6928              , l_lpn_cur.license_plate_number
6929              , l_lpn_cur.subinventory_code
6930              , l_lpn_cur.locator_id
6931              , l_lpn_cur.inventory_item_id
6932              , l_lpn_cur.revision
6933              , l_lpn_cur.lot_number
6934              , l_lpn_cur.serial_number
6935              , l_lpn_cur.gross_weight_uom_code
6936              , l_lpn_cur.gross_weight
6937              , l_lpn_cur.tare_weight_uom_code
6938              , l_lpn_cur.tare_weight
6939              , l_lpn_cur.content_volume_uom_code
6940              , l_lpn_cur.content_volume
6941              , l_lpn_cur.container_volume
6942              , l_lpn_cur.container_volume_uom
6943              , l_lpn_cur.organization_id;
6944 
6945     IF lpn_details%NOTFOUND THEN
6946       IF (l_debug = 1) THEN
6947         DEBUG('No more LPNs found from lpn_details to process', 'create_update_containers');
6948       END IF;
6949       EXIT;
6950     ELSE
6951       IF (l_debug = 1) THEN
6952         DEBUG('Found from lpn_details lpn '||l_lpn_cur.license_plate_number||', lpn_id='||l_lpn_cur.lpn_id, 'create_update_containers');
6953       END IF;
6954     END IF;
6955 
6956 
6957   --FOR l_lpn_cur IN lpn_details LOOP -- Changed for Release 12
6958     --Reset the counter
6959     l_count                                := 0;
6960     l_lpn_id                               := l_lpn_cur.lpn_id;
6961 
6962 -- commented the below code for -- For LPN reuse ER : 6845650
6963 
6964     /* part of bug fix 2529382 */
6965 /*  BEGIN
6966       SELECT wdd.released_status
6967            , wdd.delivery_detail_id
6968         INTO l_status_code
6969            , l_delivery_detail_id
6970         FROM wsh_delivery_details_ob_grp_v wdd
6971        WHERE wdd.container_name = l_lpn_cur.license_plate_number
6972        AND wdd.released_status = 'X';  --Bug#6878521 Made change bt need to chk this
6973 
6974       IF l_status_code = 'C' THEN
6975         /* Release 12(K): LPN Synchronization
6976            1. Uniqueness constraint on WDD.container_name is removed
6977               So it is not required to append characters to the LPNs
6978               to get a new containers name
6979            2. Replace API call to wsh_container_grp.update_container
6980               with new API call WSH_WMS_LPN_GRP.Create_Update_Containers
6981            */
6982 /*
6983         IF (l_debug = 1) THEN
6984           DEBUG('Release status is C, Updating delivery detail to NULL out LPN_ID', 'create_update_containers');
6985           DEBUG(' Calling Create_Update_Containers with caller WMS, action code UPDATE_NULL', 'create_update_containers');
6986           DEBUG('  delivery_detail_id = '||l_delivery_detail_id);
6987         END IF;
6988 
6989         l_wsh_dd_upd_rec.delivery_detail_id := l_delivery_detail_id;
6990         l_wsh_dd_upd_rec.lpn_id := NULL;
6991 
6992         wsh_update_tbl(1) := l_wsh_dd_upd_rec;
6993 
6994         l_IN_rec.caller      := 'WMS';
6995         l_IN_rec.action_code := 'UPDATE_NULL';
6996 
6997         WSH_WMS_LPN_GRP.Create_Update_Containers (
6998           p_api_version     => 1.0
6999         , p_init_msg_list   => fnd_api.g_false
7000         , p_commit          => fnd_api.g_false
7001         , x_return_status   => x_return_status
7002         , x_msg_count       => x_msg_count
7003         , x_msg_data        => x_msg_data
7004         , p_detail_info_tab => wsh_update_tbl
7005         , p_IN_rec          => l_IN_rec
7006         , x_OUT_rec         => l_OUT_rec );
7007 
7008 
7009         IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
7010           IF (l_debug = 1) THEN
7011             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns error', 'create_update_containers');
7012           END IF;
7013 
7014           RAISE fnd_api.g_exc_error;
7015         ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
7016           IF (l_debug = 1) THEN
7017             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns unexpected error', 'create_update_containers');
7018           END IF;
7019 
7020           RAISE fnd_api.g_exc_unexpected_error;
7021         ELSE
7022           l_count  := 0;
7023           IF (l_debug = 1) THEN
7024             DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns success, AF Container exists, set lpn_id to NULL', 'create_update_containers');
7025           END IF;
7026         END IF;
7027 
7028       ELSE
7029         l_count  := 1;
7030 
7031         IF (l_debug = 1) THEN
7032           DEBUG(' LPN with status ' || l_status_code || 'found in wdd. Check for data corruption');
7033         END IF;
7034       END IF;
7035     EXCEPTION
7036       WHEN NO_DATA_FOUND THEN
7037         l_count  := 0;
7038     END;
7039 
7040     /* end of bug fix 2529382 */
7041 
7042     IF (l_debug = 1) THEN
7043       DEBUG('LPN EXISTS: ' || l_count, 'create_update_containers');
7044     END IF;
7045 
7046     --If the LPN does not exist in in wsh_delivery_details, create a new container
7047     IF l_count = 0 THEN
7048 
7049       /* Release 12 (K): LPN Synchronization
7050          Replace the call to wsh_container_grp.create_containers
7051          with new API call to WSH_WMS_LPN_GRP.Create_Update_Containers
7052 
7053          The pre-R12 code was doing in two steps, create_container then update_container
7054          With the new API call, it replaces both previous API calls */
7055 
7056       /* Call function to create wsh delivery detail record */
7057       l_wsh_dd_rec := WMS_CONTAINER_PVT.To_DeliveryDetailsRecType(l_lpn_cur);
7058 
7059       /*Add l_wsh_dd_rec into record table */
7060 
7061       wsh_create_tbl(NVL(wsh_create_tbl.last, 0) + 1) := l_wsh_dd_rec;
7062 
7063     END IF;
7064 
7065   END LOOP;
7066 
7067   IF (l_debug = 1) THEN
7068     DEBUG('End of Loop of lpn_details, found '||wsh_create_tbl.count||' records in wsh_create_tbl to process', 'create_update_containers');
7069     DEBUG('Calling WSH_WMS_LPN_GRP.Create_Update_Containers with caller as WMS and CREATE', 'create_update_containers');
7070   END IF;
7071 
7072   l_IN_rec.caller      := 'WMS';
7073   l_IN_rec.action_code := 'CREATE';
7074 
7075   WSH_WMS_LPN_GRP.Create_Update_Containers (
7076     p_api_version     => 1.0
7077   , p_init_msg_list   => fnd_api.g_false
7078   , p_commit          => fnd_api.g_false
7079   , x_return_status   => x_return_status
7080   , x_msg_count       => x_msg_count
7081   , x_msg_data        => x_msg_data
7082   , p_detail_info_tab => wsh_create_tbl
7083   , p_IN_rec          => l_IN_rec
7084   , x_OUT_rec         => l_OUT_rec );
7085 
7086 
7087   IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
7088     IF (l_debug = 1) THEN
7089       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns error', 'create_update_containers');
7090     END IF;
7091 
7092     RAISE fnd_api.g_exc_error;
7093   ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
7094     IF (l_debug = 1) THEN
7095       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns unexpected error', 'create_update_containers');
7096     END IF;
7097 
7098     RAISE fnd_api.g_exc_unexpected_error;
7099   ELSE
7100     IF (l_debug = 1) THEN
7101       DEBUG('WSH_WMS_LPN_GRP.Create_Update_Containers returns success', 'create_update_containers');
7102     END IF;
7103   END IF;
7104   -- End of Release 12 change
7105 
7106   IF (l_debug = 1) THEN
7107     DEBUG('The API return status is ' || x_return_status, 'create_update_containers');
7108   END IF;
7109 EXCEPTION
7110   WHEN fnd_api.g_exc_error THEN
7111     x_return_status  := fnd_api.g_ret_sts_error;
7112     ROLLBACK;
7113     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7114 
7115     IF (l_debug = 1) THEN
7116       DEBUG('Execution Error in Create_Update_Container:' || SUBSTR(SQLERRM, 1, 240), 9);
7117     END IF;
7118   WHEN fnd_api.g_exc_unexpected_error THEN
7119     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7120     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7121 
7122     IF (l_debug = 1) THEN
7123       DEBUG('Unexpected Error in Create_Update_Container:' || SUBSTR(SQLERRM, 1, 240), 'create_update_containers');
7124     END IF;
7125   WHEN OTHERS THEN
7126      --x_error_code := 9999;
7127      IF l_debug = 1 THEN
7128     debug('Others exception raised: ' ||  SUBSTR(SQLERRM, 1, 240),'create_update_containers');
7129      END IF;
7130     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7131     ROLLBACK;
7132 
7133     IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7134       fnd_msg_pub.add_exc_msg('WMS_DIRECT_SHIP_PVT', 'create_update_containers');
7135     END IF;
7136 
7137     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7138 END create_update_containers;
7139 
7140 PROCEDURE update_shipped_quantity(
7141   x_return_status OUT NOCOPY    VARCHAR2
7142 , x_msg_count     OUT NOCOPY    NUMBER
7143 , x_msg_data      OUT NOCOPY    VARCHAR2
7144 , p_delivery_id   IN            NUMBER
7145 , p_org_id        IN            NUMBER DEFAULT NULL
7146 ) IS
7147   -- Cursor to get the delivery qty (sum of all the delivery_details )
7148   -- for the given delivery_id
7149 
7150   CURSOR delivery_item_qty IS
7151     SELECT   wdd.inventory_item_id
7152            , wdd.revision
7153            , wdd.lot_number
7154            , SUM(wdd.picked_quantity)
7155         FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
7156        WHERE wstt.delivery_id = p_delivery_id
7157          AND wdd.delivery_detail_id = wstt.delivery_detail_id
7158          AND wdd.released_status = 'Y'
7159     GROUP BY wdd.inventory_item_id, wdd.revision, wdd.lot_number
7160     ORDER BY wdd.inventory_item_id, wdd.revision, wdd.lot_number;
7161 
7162   -- Cursor to get the item qty in the LPN corresponding to the given delivery_id
7163   -- and the inventory item,revision
7164   CURSOR lpn_item_qty(p_delivery_id NUMBER, p_item_id NUMBER, p_revision VARCHAR2, p_lot_number VARCHAR2) IS
7165     SELECT SUM(wlc.quantity)
7166       FROM wms_lpn_contents wlc, wms_license_plate_numbers lpn,
7167       wms_shipping_transaction_temp wstt
7168       WHERE wlc.parent_lpn_id = lpn.lpn_id
7169       and wstt.delivery_id = p_delivery_id
7170       and lpn.outermost_lpn_id = wstt.outermost_lpn_id
7171       and wstt.inventory_item_id = p_item_id
7172       AND wstt.inventory_item_id = wlc.inventory_item_id
7173       AND NVL(wlc.revision, '#') = NVL(p_revision, '#')
7174       AND NVL(wlc.lot_number, '#') = NVL(p_lot_number, '#');
7175 
7176   l_item_id             NUMBER;
7177   l_revision            VARCHAR2(3);
7178 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
7179   l_lot_number          VARCHAR2(80);
7180   l_del_qty             NUMBER;
7181   l_shipping_attr       wsh_delivery_details_pub.changedattributetabtype;
7182   l_delivery_detail_id  NUMBER;
7183   l_picked_qty          NUMBER;
7184   l_extra_qty           NUMBER;
7185   l_lpn_qty             NUMBER;
7186   l_lpn_item_count      NUMBER;
7187   l_delivery_item_count NUMBER;
7188   l_init_msg_list       VARCHAR2(1)                                      := fnd_api.g_false;
7189   l_commit              VARCHAR2(1)                                      := fnd_api.g_false;
7190   l_return_status       VARCHAR2(1);
7191   l_msg_data            VARCHAR2(20000);
7192   l_msg_count           NUMBER;
7193   x_error_code          NUMBER;
7194   l_debug               NUMBER                                           := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7195 BEGIN
7196   x_return_status  := fnd_api.g_ret_sts_success;
7197 
7198   BEGIN
7199     --Query to get the total no. of distinct items in the lpn to be shipped
7200 
7201     SELECT   COUNT(COUNT(*))
7202         INTO l_lpn_item_count
7203         FROM wms_license_plate_numbers wlpn, wms_lpn_contents wlc, wms_shipping_transaction_temp wstt
7204        WHERE wstt.delivery_id = p_delivery_id
7205          AND wstt.outermost_lpn_id = wlpn.outermost_lpn_id
7206          AND wlpn.lpn_id = wlc.parent_lpn_id
7207     GROUP BY wlc.inventory_item_id, wlc.revision;
7208 
7209     --Query to get the total no. of distinct items in the delivery
7210 
7211     SELECT   COUNT(COUNT(*))
7212         INTO l_delivery_item_count
7213         FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
7214        WHERE wstt.delivery_id = p_delivery_id
7215          AND wdd.delivery_detail_id = wstt.delivery_detail_id
7216          AND wdd.released_status = 'Y'
7217     GROUP BY wdd.inventory_item_id, wdd.revision;
7218 
7219     --Check if lpn has items other than those for the delivery
7220 
7221     IF (l_lpn_item_count > l_delivery_item_count) THEN
7222       IF (l_debug = 1) THEN
7223         DEBUG('LPN contains items not belonging to the delivery.', 'Update_shipped_Quantity');
7224         DEBUG('Cannot ship', 'Update_shipped_Quantity');
7225       END IF;
7226 
7227       RAISE fnd_api.g_exc_error;
7228     END IF;
7229   EXCEPTION
7230     WHEN fnd_api.g_exc_error THEN
7231       -- Log a message for item count mismatch
7232       x_return_status  := fnd_api.g_ret_sts_error;
7233       x_error_code     := 10;
7234 
7235       IF (l_debug = 1) THEN
7236         DEBUG('LPN has more items than Delivery Details', 'Update_Shipped_Quantity');
7237         DEBUG('Some Items are not assigned to delivery details', 'Update_Shipped_Quantity');
7238       END IF;
7239   END; -- END Item Count
7240 
7241   OPEN delivery_item_qty;
7242 
7243   LOOP
7244     FETCH delivery_item_qty INTO l_item_id, l_revision, l_lot_number, l_del_qty;
7245     EXIT WHEN delivery_item_qty%NOTFOUND;
7246     OPEN lpn_item_qty(p_delivery_id, l_item_id, l_revision, l_lot_number);
7247     FETCH lpn_item_qty INTO l_lpn_qty;
7248     CLOSE lpn_item_qty;
7249 
7250     -- If LPN has more than delivery and we are shipping more.
7251     -- Update the delivery detail with the extra qty
7252 
7253     IF (l_lpn_qty > l_del_qty) THEN
7254       l_extra_qty                            := l_lpn_qty - l_del_qty;
7255 
7256       -- SELECT the first DELIVERY LINE returned from WDD
7257 
7258       BEGIN
7259         SELECT wdd.delivery_detail_id
7260              , wdd.picked_quantity
7261           INTO l_delivery_detail_id
7262              , l_picked_qty
7263           FROM wms_shipping_transaction_temp wstt, wsh_delivery_details_ob_grp_v wdd
7264          WHERE wstt.delivery_id = p_delivery_id
7265            AND wstt.inventory_item_id = l_item_id
7266            AND wdd.delivery_detail_id = wstt.delivery_detail_id
7267            AND wdd.inventory_item_id = wstt.inventory_item_id
7268            AND NVL(wdd.revision, '#') = NVL(l_revision, '#')
7269            AND NVL(wdd.lot_number, '#') = NVL(l_lot_number, '#')
7270            AND ROWNUM = 1;
7271       EXCEPTION
7272         WHEN NO_DATA_FOUND THEN
7273           NULL;
7274       END;
7275 
7276       -- Call WSH_DELIVERY_DETAILS to update the shipped_qty
7277       l_shipping_attr(1).shipped_quantity    := l_picked_qty + l_extra_qty;
7278       l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_id;
7279       wsh_delivery_details_pub.update_shipping_attributes(
7280         p_api_version_number         => 1.0
7281       , p_source_code                => 'OE'
7282       , p_init_msg_list              => l_init_msg_list
7283       , p_commit                     => l_commit
7284       , p_changed_attributes         => l_shipping_attr
7285       , x_return_status              => l_return_status
7286       , x_msg_count                  => l_msg_count
7287       , x_msg_data                   => l_msg_data
7288       );
7289 
7290       IF (l_debug = 1) THEN
7291         DEBUG('after calling Update_shipping_attributes', 'Update_shipped_Quantity');
7292       END IF;
7293 
7294       IF (l_return_status = fnd_api.g_ret_sts_error) THEN
7295         IF (l_debug = 1) THEN
7296           DEBUG('return error from update shipping attributes 2', 'Update_shipped_Quantity');
7297         END IF;
7298 
7299         RAISE fnd_api.g_exc_error;
7300       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7301         IF (l_debug = 1) THEN
7302           DEBUG('return unexpected error from update shipping attributes', 'Update_shipped_Quantity');
7303         END IF;
7304 
7305         RAISE fnd_api.g_exc_unexpected_error;
7306       ELSIF l_return_status = 'S' THEN
7307         IF (l_debug = 1) THEN
7308           DEBUG('Shipping attributes updated successfully');
7309         END IF;
7310 
7311         x_return_status  := fnd_api.g_ret_sts_success;
7312       END IF;
7313     END IF; -- end if l_lpn_qty > l_del_qty
7314   END LOOP;
7315 
7316   CLOSE delivery_item_qty;
7317 EXCEPTION
7318   WHEN fnd_api.g_exc_error THEN
7319     x_return_status  := fnd_api.g_ret_sts_error;
7320     x_error_code     := 10;
7321     ROLLBACK;
7322     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7323 
7324     IF (l_debug = 1) THEN
7325       DEBUG('Execution Error in Update_shipped_Quantity', 'Update_shipped_Quantity');
7326       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
7327     END IF;
7328   WHEN fnd_api.g_exc_unexpected_error THEN
7329     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7330     x_error_code     := 20;
7331     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7332 
7333     IF (l_debug = 1) THEN
7334       DEBUG('Unexpected Error in Update_shipped_Quantity:', 'Update_shipped_Quantity');
7335       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
7336     END IF;
7337   WHEN OTHERS THEN
7338     x_error_code     := 30;
7339     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7340     ROLLBACK;
7341     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7342 
7343     IF (l_debug = 1) THEN
7344       DEBUG('Could not update shipping attributes', 'Update_shipped_Quantity');
7345     END IF;
7346 END update_shipped_quantity;
7347 
7348 
7349 
7350 PROCEDURE container_nesting(
7351   x_return_status    OUT NOCOPY    VARCHAR2
7352 , x_msg_count        OUT NOCOPY    VARCHAR2
7353 , x_msg_data         OUT NOCOPY    VARCHAR2
7354 , p_organization_id  IN            NUMBER
7355 , p_outermost_lpn_id IN            NUMBER
7356 , p_action_code      IN            VARCHAR2 DEFAULT 'PACK') IS
7357   CURSOR lpn_hierarchy IS
7358     SELECT DISTINCT NVL(parent_lpn_id, outermost_lpn_id)
7359                FROM wms_license_plate_numbers
7360               WHERE organization_id = p_organization_id
7361               AND outermost_lpn_id = p_outermost_lpn_id
7362               AND outermost_lpn_id <> lpn_id;
7363 
7364   CURSOR lpn_childs(l_parent_lpn_id NUMBER) IS
7365     SELECT lpn_id
7366       FROM wms_license_plate_numbers
7367      WHERE organization_id = p_organization_id
7368        AND parent_lpn_id = l_parent_lpn_id;
7369 
7370   CURSOR container_details(p_organization_id NUMBER, l_lpn_id NUMBER) IS
7371     SELECT delivery_detail_id
7372       FROM wsh_delivery_details_ob_grp_v
7373      WHERE organization_id = p_organization_id
7374        AND lpn_id = l_lpn_id
7375        AND released_status = 'X';  -- For LPN reuse ER : 6845650
7376 
7377   l_child_lpn_id      NUMBER;
7378   l_parent_lpn_id     NUMBER;
7379   l_par_del_det_id    NUMBER;
7380   l_child_del_det_id  NUMBER;
7381   l_child_del_det_tab wsh_util_core.id_tab_type;
7382   l_par_del_det_tab   wsh_util_core.id_tab_type;
7383 
7384   l_count             NUMBER                    := 0;
7385   l_parent_counter       NUMBER                    := 0;
7386   l_debug             NUMBER                    := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7387 
7388   l_dummy_number   NUMBER;
7389   l_container_flag VARCHAR2(1);
7390 
7391   l_delivery_id    NUMBER;
7392   l_delivery_planned_flag VARCHAR2(1);
7393   l_action_prms      wsh_interface_ext_grp.del_action_parameters_rectype;
7394   l_delivery_id_tab  wsh_util_core.id_tab_type;
7395   l_delivery_out_rec wsh_interface_ext_grp.del_action_out_rec_type;
7396 
7397   l_trip_action_prms WSH_INTERFACE_EXT_GRP.trip_action_parameters_rectype;
7398   l_trip_out_rec     WSH_INTERFACE_EXT_GRP.tripactionoutrectype;
7399   l_trip_id_tab      wsh_util_core.id_tab_type;
7400 
7401   -- Release 12 : LPN SyncUp
7402   l_wsh_lpn_id_tbl wsh_util_core.id_tab_type;
7403   l_wsh_del_det_id_tbl wsh_util_core.id_tab_type;
7404   l_wsh_action_prms WSH_GLBL_VAR_STRCT_GRP.dd_action_parameters_rec_type;
7405   l_wsh_defaults        WSH_GLBL_VAR_STRCT_GRP.dd_default_parameters_rec_type;
7406   l_wsh_action_out_rec  WSH_GLBL_VAR_STRCT_GRP.dd_action_out_rec_type;
7407 
7408 BEGIN
7409   x_return_status  := fnd_api.g_ret_sts_success;
7410 
7411   IF l_debug = 1 THEN
7412      debug('Entered container_nesting with p_action_code: ' ||p_action_code,
7413        'Container_Nesting');
7414   END IF;
7415 
7416   IF p_action_code = 'PACK' THEN
7417      l_container_flag := 'N';
7418    ELSIF p_action_code = 'UNPACK' THEN
7419      l_container_flag := 'Y';
7420    ELSE
7421      IF l_debug = 1 THEN
7422     debug('Invalid p_action_code passed in','Container_Nesting');
7423      END IF;
7424 
7425      RAISE fnd_api.g_exc_unexpected_error;
7426   END IF;
7427 
7428   --Loop through containers that have nested container
7429   OPEN lpn_hierarchy;
7430 
7431   LOOP
7432     FETCH lpn_hierarchy INTO l_parent_lpn_id;
7433     EXIT WHEN lpn_hierarchy%NOTFOUND;
7434 
7435 
7436 
7437     IF (l_debug = 1) THEN
7438       DEBUG('lpn:' || l_child_lpn_id || ' parent:' || l_parent_lpn_id, 'Container_Nesting');
7439     END IF;
7440 
7441 
7442     --Get the delivery detail id for the parent lpn
7443     OPEN container_details(p_organization_id, l_parent_lpn_id);
7444     FETCH container_details INTO l_par_del_det_id;
7445     CLOSE container_details;
7446 
7447     BEGIN
7448        SELECT 1
7449      INTO l_dummy_number
7450      FROM wms_shipping_transaction_temp
7451      WHERE parent_lpn_id = l_parent_lpn_id
7452      AND ROWNUM = 1;
7453 
7454        IF (l_debug = 1) THEN
7455       DEBUG('lpn_id ' || l_parent_lpn_id || ' attached to an item line', 'Container_Nesting');
7456       debug('No need to unassign from delivery','Container_nesting');
7457        END IF;
7458 
7459     EXCEPTION
7460        WHEN no_data_found THEN
7461       IF (l_debug = 1) THEN
7462          debug('no item line attached to lpn_id: ' || l_parent_lpn_id,'container_nesting');
7463          debug('put it in to unassign from delivery' || 'Container_nesting');
7464       END IF;
7465       l_parent_counter := l_parent_counter + 1;
7466       l_par_del_det_tab(l_parent_counter) := l_par_del_det_id;
7467     END;
7468 
7469     IF l_debug = 1 THEN
7470        debug('Parent lpn delivery_detail_id: ' || l_par_del_det_id, 'Container_Nesting');
7471     END IF;
7472 
7473     l_count  := 0;
7474 
7475     -- Release 12: Use new API
7476     --l_child_del_det_tab.DELETE;
7477     l_wsh_lpn_id_tbl.DELETE;
7478 
7479     --getting all the children containers of current parent container
7480     OPEN lpn_childs(l_parent_lpn_id);
7481     LOOP
7482       FETCH lpn_childs INTO l_child_lpn_id;
7483       EXIT WHEN lpn_childs%NOTFOUND;
7484       l_count                       := l_count + 1;
7485       --Get the delivery detail id for the child lpn
7486       -- Release 12, do not need to get delivery detail ID for child lpns
7487       --  use lpn_ids directly
7488       /*OPEN container_details(p_organization_id, l_child_lpn_id);
7489       FETCH container_details INTO l_child_del_det_id;
7490       CLOSE container_details;
7491       l_child_del_det_tab(l_count)  := l_child_del_det_id;
7492       IF (l_debug = 1) THEN
7493         DEBUG('child det:' || l_child_del_det_tab(l_count) || ' parent: ' || l_par_del_det_id, 'Container_Nesting');
7494       END IF;*/
7495 
7496       l_wsh_lpn_id_tbl(l_count) := l_child_lpn_id;
7497       IF (l_debug = 1) THEN
7498         DEBUG('child lpn:' || l_wsh_lpn_id_tbl(l_count) || ' parent: ' || l_par_del_det_id, 'Container_Nesting');
7499       END IF;
7500 
7501     END LOOP;-- lpn_childs loop
7502 
7503     CLOSE lpn_childs;
7504 
7505     IF l_wsh_lpn_id_tbl.COUNT > 0 THEN
7506       -- Relase 12: LPN SyncUp
7507       -- Replaced call to wsh_container_grp.container_actions
7508       --  with new API call WSH_WMS_LPN_GRP.Delivery_Detail_Action
7509       l_wsh_action_prms.caller                  := 'WMS';
7510       l_wsh_action_prms.action_code             := p_action_code;
7511       l_wsh_action_prms.lpn_rec.organization_id := p_organization_id;
7512       l_wsh_action_prms.lpn_rec.lpn_id          := l_parent_lpn_id;
7513 
7514       IF (l_debug = 1) THEN
7515         DEBUG('Calling WSH_WMS_LPN_GRP.Delivery_Detail_Action with ', 'Container_Nesting');
7516         DEBUG('  Caller: WMS, action_code:'||p_action_code, 'Container_Nesting');
7517         DEBUG('  organization_id:'||p_organization_id, 'Container_Nesting');
7518         DEBUG('  p_action_prms.lpn_rec.lpn_id='||l_parent_lpn_id,  'Container_Nesting');
7519         DEBUG('  p_lpn_id_tbl with '||l_wsh_lpn_id_tbl.count()||' records',  'Container_Nesting');
7520       END IF;
7521 
7522       WSH_WMS_LPN_GRP.Delivery_Detail_Action(
7523         p_api_version_number        => 1.0,
7524         p_init_msg_list             => fnd_api.g_false,
7525         p_commit                    => fnd_api.g_false,
7526         x_return_status             => x_return_status,
7527         x_msg_count                 => x_msg_count,
7528         x_msg_data                  => x_msg_data,
7529         p_lpn_id_tbl                => l_wsh_lpn_id_tbl,
7530         p_del_det_id_tbl            => l_wsh_del_det_id_tbl,
7531         p_action_prms               => l_wsh_action_prms,
7532         x_defaults                  => l_wsh_defaults,
7533         x_action_out_rec            => l_wsh_action_out_rec
7534       );
7535 
7536       IF (x_return_status IN(fnd_api.g_ret_sts_error)) THEN
7537         --debug('WSH_Container_Grp.Container_Actions returns an exec. error','Pack_Lpns);
7538         IF (l_debug = 1) THEN
7539           DEBUG('Exec. error in packing' || SUBSTR(SQLERRM, 1, 240) || ' ret st:' || x_return_status, 'Container_Nesting');
7540         END IF;
7541 
7542         RAISE fnd_api.g_exc_error;
7543       ELSIF(x_return_status = fnd_api.g_ret_sts_unexp_error) THEN
7544         --debug('WSH_Container_Grp.Container_Actions returns an unexp. error','Pack_Lpns);
7545         IF (l_debug = 1) THEN
7546           DEBUG('Unexp. error in packing', 'Container_Nesting');
7547         END IF;
7548 
7549         RAISE fnd_api.g_exc_unexpected_error;
7550       ELSE
7551         IF (l_debug = 1) THEN
7552           DEBUG('Nesting is success. Outermost LPN ID: ' || p_outermost_lpn_id, 'Container_Nesting');
7553         END IF;
7554       END IF;
7555     END IF;
7556   END LOOP; --lpn_hierarchy cursor loop
7557 
7558   CLOSE lpn_hierarchy;
7559 
7560 
7561   --if unpacking, also unassign the outer LPNs from delivery so that STF will
7562   --show the same state as before load.  The inner-most LPN will not be unassigned
7563   IF p_action_code = 'UNPACK' AND l_par_del_det_tab.COUNT > 0 THEN
7564        BEGIN
7565       --make sure delivery is assigned to the LPNs
7566       --Any of the LPN will do since they should be assigned to the same delivery
7567       SELECT wda.delivery_id
7568         INTO l_delivery_id
7569         FROM wsh_delivery_assignments_v wda
7570         WHERE wda.delivery_detail_id = l_par_del_det_tab(1)
7571         AND wda.delivery_id IS NOT NULL;
7572 
7573       DEBUG('Unpack delivery_id is: ' || l_delivery_id, 'Container_Nesting');
7574 
7575       SELECT wnd.planned_flag
7576         INTO l_delivery_planned_flag
7577         FROM wsh_new_deliveries_ob_grp_v wnd
7578         WHERE wnd.delivery_id = l_delivery_id;
7579 
7580       DEBUG('Unpack delivery planned status: ' || l_delivery_planned_flag,'Container_Nesting');
7581        EXCEPTION
7582       WHEN OTHERS THEN
7583          IF (l_debug = 1) THEN
7584         DEBUG('Unpack other exception..delivery not assign to LPN? ','Container_Nesting');
7585         debug(SQLERRM, 'Container_Nesting');
7586          END IF;
7587          RAISE fnd_api.g_exc_unexpected_error;
7588        END;
7589 
7590        IF l_delivery_planned_flag NOT IN ('Y','F') THEN
7591       DEBUG('Delivery not planned. Just unassign the outer LPN lines','Container_Nesting');
7592       wsh_container_grp.container_actions
7593         (p_api_version => 1.0,
7594          p_init_msg_list => G_TRUE,
7595          p_commit => G_FALSE,
7596          p_validation_level => fnd_api.g_valid_level_full,
7597          x_return_status => x_return_status,
7598          x_msg_count => x_msg_count,
7599          x_msg_data => x_msg_data,
7600          p_detail_tab => l_par_del_det_tab,
7601          p_delivery_flag => 'Y',
7602          p_action_code => 'UNASSIGN');
7603 
7604 
7605 
7606       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR)
7607         THEN
7608          debug('Unassign from delivery failed' , 'Container_Nesting');
7609          debug('wsh_container_grp.container_actions returned ' ||
7610            x_return_status,'Container_Nesting');
7611          RAISE fnd_api.g_exc_unexpected_error;
7612       END IF;
7613     ELSE
7614       DEBUG('Delivery is planned.','Container_Nesting');
7615       IF l_delivery_planned_flag = 'F' THEN
7616          DEBUG('Unfirming trip first','Container_Nesting');
7617          l_trip_action_prms.caller := 'WMS';
7618          l_trip_action_prms.action_code := 'UNPLAN';
7619 
7620          BEGIN
7621         SELECT DISTINCT trip_id
7622           INTO l_trip_id_tab(1)
7623           FROM wms_shipping_transaction_temp
7624           WHERE delivery_id = l_delivery_id;
7625 
7626         debug('Trip id: ' || l_trip_id_tab(1),'Container_Nesting');
7627          EXCEPTION
7628         WHEN OTHERS THEN
7629            debug('Cannot find trip ID?','Container_Nesting');
7630            debug(SQLERRM,'Container_Nesting');
7631            RAISE fnd_api.g_exc_unexpected_error;
7632          END;
7633 
7634          wsh_interface_ext_grp.trip_action
7635            (p_api_version_number     => 1.0,
7636         p_init_msg_list          => fnd_api.g_false,
7637         p_commit                 => fnd_api.g_false,
7638         p_action_prms            => l_trip_action_prms,
7639         p_entity_id_tab        => l_trip_id_tab,
7640         x_trip_out_rec       => l_trip_out_rec,
7641         x_return_status          => x_return_status,
7642         x_msg_count              => x_msg_count,
7643         x_msg_data               => x_msg_data
7644         );
7645 
7646          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
7647         DEBUG('Unfirming trip failed!','Container_Nesting');
7648         debug('msg_data: ' || x_msg_data,'Container_Nesting');
7649         RAISE fnd_api.g_exc_unexpected_error;
7650          END IF;
7651       END IF;
7652 
7653       l_action_prms.caller := 'WMS';
7654       l_action_prms.action_code := 'UNPLAN';
7655 
7656       l_delivery_id_tab(1) := l_delivery_id;
7657 
7658       DEBUG('Unfirm delivery','Container_Nesting');
7659       wsh_interface_ext_grp.delivery_action
7660         (p_api_version_number     => 1.0,
7661          p_init_msg_list          => fnd_api.g_false,
7662          p_commit                 => fnd_api.g_false,
7663          p_action_prms            => l_action_prms,
7664          p_delivery_id_tab        => l_delivery_id_tab,
7665          x_delivery_out_rec       => l_delivery_out_rec,
7666          x_return_status          => x_return_status,
7667          x_msg_count              => x_msg_count,
7668          x_msg_data               => x_msg_data);
7669 
7670       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
7671          DEBUG('Unfirming delivery failed!','Container_Nesting');
7672          debug('msg_data: ' || x_msg_data,'Container_Nesting');
7673          RAISE fnd_api.g_exc_unexpected_error;
7674       END IF;
7675 
7676       DEBUG('Unassign the lines from delivery','Container_Nesting');
7677       wsh_container_grp.container_actions
7678         (p_api_version => 1.0,
7679          p_init_msg_list => G_TRUE,
7680          p_commit => G_FALSE,
7681          p_validation_level => fnd_api.g_valid_level_full,
7682          x_return_status => x_return_status,
7683          x_msg_count => x_msg_count,
7684          x_msg_data => x_msg_data,
7685          p_detail_tab => l_par_del_det_tab,
7686          p_delivery_flag => 'Y',
7687          p_action_code => 'UNASSIGN');
7688 
7689       IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
7690          DEBUG('Unassigning from delivery failed!','Container_Nesting');
7691          debug('msg_data: ' || x_msg_data,'Container_Nesting');
7692          RAISE fnd_api.g_exc_unexpected_error;
7693       END IF;
7694 
7695       IF l_delivery_planned_flag = 'Y' THEN
7696 
7697          l_action_prms.action_code := 'PLAN';
7698          DEBUG('Planned flag is Y need to firm back the delivery','Container_Nesting');
7699          wsh_interface_ext_grp.delivery_action
7700            (p_api_version_number     => 1.0,
7701         p_init_msg_list          => fnd_api.g_false,
7702         p_commit                 => fnd_api.g_false,
7703         p_action_prms            => l_action_prms,
7704         p_delivery_id_tab        => l_delivery_id_tab,
7705         x_delivery_out_rec       => l_delivery_out_rec,
7706         x_return_status          => x_return_status,
7707         x_msg_count              => x_msg_count,
7708         x_msg_data               => x_msg_data);
7709 
7710          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
7711         DEBUG('Firming delivery failed!','Container_Nesting');
7712         debug('msg_data: ' || x_msg_data,'Container_Nesting');
7713         RAISE fnd_api.g_exc_unexpected_error;
7714          END IF;
7715 
7716        else
7717          debug('Planned flag is F. Firm back the trip: ' || l_trip_id_tab(1),'Container_Nesting');
7718          l_trip_action_prms.action_code := 'FIRM';
7719 
7720          wsh_interface_ext_grp.trip_action
7721            (p_api_version_number     => 1.0,
7722         p_init_msg_list          => fnd_api.g_false,
7723         p_commit                 => fnd_api.g_false,
7724         p_action_prms            => l_trip_action_prms,
7725         p_entity_id_tab        => l_trip_id_tab,
7726         x_trip_out_rec       => l_trip_out_rec,
7727         x_return_status          => x_return_status,
7728         x_msg_count              => x_msg_count,
7729         x_msg_data               => x_msg_data
7730         );
7731 
7732          IF x_return_status IN (G_RET_STS_ERROR,G_RET_STS_UNEXP_ERROR) THEN
7733         DEBUG('Firming trip failed!','Container_Nesting');
7734         debug('msg_data: ' || x_msg_data,'Container_Nesting');
7735         RAISE fnd_api.g_exc_unexpected_error;
7736          END IF;
7737       END IF;
7738        END IF;
7739   END IF;--p_action_code = 'UNPACK'
7740 
7741 EXCEPTION
7742   WHEN fnd_api.g_exc_error THEN
7743     x_return_status  := fnd_api.g_ret_sts_error;
7744     ROLLBACK;
7745     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7746 
7747     IF container_details%ISOPEN THEN
7748       CLOSE container_details;
7749     END IF;
7750 
7751     IF lpn_hierarchy%ISOPEN THEN
7752       CLOSE lpn_hierarchy;
7753     END IF;
7754 
7755     IF (l_debug = 1) THEN
7756       DEBUG('Execution Error in lpn_hiearchy:' || x_msg_data || '-' || SUBSTR(SQLERRM, 1, 240), 'Container_Nesting');
7757     END IF;
7758   WHEN fnd_api.g_exc_unexpected_error THEN
7759     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7760     ROLLBACK;
7761     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7762 
7763     IF (l_debug = 1) THEN
7764       DEBUG('Unexpected Error in lpn_hiearchy:' || SUBSTR(SQLERRM, 1, 240) || SQLCODE, 'Container_Nesting');
7765     END IF;
7766 
7767     IF container_details%ISOPEN THEN
7768       CLOSE container_details;
7769     END IF;
7770 
7771     IF lpn_hierarchy%ISOPEN THEN
7772       CLOSE lpn_hierarchy;
7773     END IF;
7774   WHEN OTHERS THEN
7775     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7776     ROLLBACK;
7777     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7778 
7779     --debug('Unexpected Error in pack_lpns:' || substr(SQLERRM,1,240),9);
7780     IF (l_debug = 1) THEN
7781       DEBUG('When others in lpn_hiearchy:' || SUBSTR(SQLERRM, 1, 240) || SQLCODE, 'Container_Nesting');
7782     END IF;
7783 
7784     IF container_details%ISOPEN THEN
7785       CLOSE container_details;
7786     END IF;
7787 
7788     IF lpn_hierarchy%ISOPEN THEN
7789       CLOSE lpn_hierarchy;
7790     END IF;
7791 END container_nesting;
7792 
7793 PROCEDURE check_order_line_split(
7794   x_return_status OUT NOCOPY    VARCHAR2
7795 , x_msg_count     OUT NOCOPY    NUMBER
7796 , x_msg_data      OUT NOCOPY    VARCHAR2
7797 , x_error_code    OUT NOCOPY    NUMBER
7798 , p_delivery_id   IN            NUMBER
7799 ) IS
7800   CURSOR lpn_cur IS
7801     SELECT   parent_lpn_id
7802            , COUNT(parent_lpn_id) cnt
7803         FROM wms_shipping_transaction_temp
7804        WHERE delivery_id = p_delivery_id
7805     GROUP BY parent_lpn_id;
7806 
7807   l_lpn_cur          lpn_cur%ROWTYPE;
7808   l_par_del_det_id   NUMBER;
7809   l_count_del_assign NUMBER;
7810   lpn_name           VARCHAR2(30);
7811   l_debug            NUMBER            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7812 BEGIN
7813   x_return_status  := fnd_api.g_ret_sts_success;
7814   x_error_code     := NULL;
7815   OPEN lpn_cur;
7816 
7817   LOOP
7818     FETCH lpn_cur INTO l_lpn_cur;
7819     EXIT WHEN lpn_cur%NOTFOUND;
7820 
7821     SELECT delivery_detail_id
7822       INTO l_par_del_det_id
7823       FROM wsh_delivery_details_ob_grp_v
7824      WHERE lpn_id = l_lpn_cur.parent_lpn_id
7825      AND released_status = 'X'; -- For LPN reuse ER : 6845650
7826 
7827     SELECT COUNT(*)
7828       INTO l_count_del_assign
7829       FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd
7830      WHERE wda.parent_delivery_detail_id = l_par_del_det_id
7831        AND wda.delivery_detail_id = wdd.delivery_detail_id
7832        AND NVL(wdd.container_flag, 'N') = 'N';
7833 
7834     IF l_count_del_assign <> l_lpn_cur.cnt THEN
7835       SELECT license_plate_number
7836         INTO lpn_name
7837         FROM wms_license_plate_numbers
7838        WHERE lpn_id = l_lpn_cur.parent_lpn_id;
7839 
7840       fnd_message.set_name('WMS', 'WMS_ORDER_LINE_SPLIT');
7841       fnd_message.set_token('CONTAINER_NAME', lpn_name);
7842       fnd_msg_pub.ADD;
7843       x_error_code  := 7;
7844       RAISE fnd_api.g_exc_error;
7845 
7846       --dbms_output.put_line('Contents of the container '||lpn_name||' are split in order management');
7847       IF (l_debug = 1) THEN
7848         DEBUG('Contents of the container ' || lpn_name || ' are split in order management', 'CHECK_ORDER_LINE_SPLIT');
7849       END IF;
7850     END IF;
7851   END LOOP;
7852 
7853   CLOSE lpn_cur;
7854 EXCEPTION
7855   WHEN fnd_api.g_exc_error THEN
7856     x_return_status  := fnd_api.g_ret_sts_error;
7857 
7858     IF lpn_cur%ISOPEN THEN
7859       CLOSE lpn_cur;
7860     END IF;
7861 
7862     IF (l_debug = 1) THEN
7863       DEBUG('In Exception (expected error) - E ', 'CHECK_ORDER_LINE_SPLIT');
7864     END IF;
7865 
7866     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7867   WHEN fnd_api.g_exc_unexpected_error THEN
7868     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7869 
7870     IF lpn_cur%ISOPEN THEN
7871       CLOSE lpn_cur;
7872     END IF;
7873 
7874     IF (l_debug = 1) THEN
7875       DEBUG('In UnException (unexpected error) - U ', 'CHECK_ORDER_LINE_SPLIT');
7876     END IF;
7877 
7878     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7879   WHEN OTHERS THEN
7880     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7881 
7882     IF lpn_cur%ISOPEN THEN
7883       CLOSE lpn_cur;
7884     END IF;
7885 
7886     IF (l_debug = 1) THEN
7887       DEBUG('In Exception (When others) - U ', 'CHECK_ORDER_LINE_SPLIT');
7888     END IF;
7889 
7890     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7891 END check_order_line_split;
7892 
7893 PROCEDURE check_missing_item_cur(
7894   p_delivery_id     IN            NUMBER
7895 , p_dock_door_id    IN            NUMBER
7896 , p_organization_id IN            NUMBER
7897 , x_return_status   OUT NOCOPY    VARCHAR2
7898 , x_missing_count   OUT NOCOPY    NUMBER
7899 ) IS
7900   l_count NUMBER;
7901   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7902 BEGIN
7903   x_missing_count  := 0;
7904   x_return_status  := fnd_api.g_ret_sts_success;
7905 
7906   SELECT COUNT(*)
7907     INTO l_count
7908     FROM wsh_delivery_details_ob_grp_v wdd, wsh_delivery_assignments_v wda, mtl_system_items_kfv msik
7909    WHERE wda.delivery_detail_id = wdd.delivery_detail_id
7910      AND NVL(wdd.container_flag, 'N') = 'N'
7911      AND wda.delivery_id = p_delivery_id
7912      AND wdd.inventory_item_id = msik.inventory_item_id
7913      AND wdd.organization_id = msik.organization_id
7914      AND(
7915          (wda.parent_delivery_detail_id IS NULL
7916           AND msik.mtl_transactions_enabled_flag <> 'N')
7917          OR wdd.released_status IS NULL
7918          OR wdd.released_status NOT IN('X', 'Y')
7919         );
7920 
7921   IF l_count > 0 THEN
7922     x_missing_count  := l_count;
7923   END IF;
7924 EXCEPTION
7925   WHEN NO_DATA_FOUND THEN
7926     l_count  := 0;
7927   WHEN OTHERS THEN
7928     x_return_status  := fnd_api.g_ret_sts_unexp_error;
7929 
7930     IF (l_debug = 1) THEN
7931       DEBUG('Unexp-Error in missing item procedure ', 'CHECK_MISSING_ITEM_CUR');
7932     END IF;
7933 END check_missing_item_cur;
7934 
7935 PROCEDURE chk_del_for_direct_ship(
7936   x_return_status OUT NOCOPY    VARCHAR2
7937 , x_msg_count     OUT NOCOPY    NUMBER
7938 , x_msg_data      OUT NOCOPY    VARCHAR2
7939 , p_delivery_id   IN            NUMBER
7940 ) IS
7941   l_flag_y                NUMBER;
7942   l_flag_n                NUMBER;
7943   l_num                   NUMBER;
7944 
7945   TYPE parent_del_detail_ids IS TABLE OF NUMBER
7946     INDEX BY BINARY_INTEGER;
7947 
7948   l_parent_del_detail_ids parent_del_detail_ids;
7949 
7950   CURSOR parent_del_detail IS
7951     SELECT delivery_detail_id
7952       FROM wsh_delivery_details_ob_grp_v
7953      WHERE lpn_id IN(SELECT parent_lpn_id
7954                        FROM wms_shipping_transaction_temp
7955                       WHERE delivery_id = p_delivery_id
7956                         AND direct_ship_flag = 'Y');
7957 
7958   CURSOR del_detail IS
7959     SELECT parent_delivery_detail_id
7960       FROM wsh_delivery_assignments_v
7961      WHERE delivery_id = p_delivery_id
7962        AND delivery_detail_id NOT IN(SELECT delivery_detail_id
7963                                        FROM wsh_delivery_details_ob_grp_v
7964                                       WHERE lpn_id IN(SELECT parent_lpn_id
7965                                                         FROM wms_shipping_transaction_temp
7966                                                        WHERE delivery_id = p_delivery_id
7967                                                          AND direct_ship_flag = 'Y'));
7968 
7969   l_debug                 NUMBER                := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7970 BEGIN
7971   x_return_status  := fnd_api.g_ret_sts_success;
7972 
7973   BEGIN
7974     SELECT NVL(SUM(DECODE(direct_ship_flag, 'Y', 1)), 0) l_flag_y
7975          , NVL(SUM(DECODE(direct_ship_flag, 'N', 1)), 0) l_flag_n
7976       INTO l_flag_y
7977          , l_flag_n
7978       FROM wms_shipping_transaction_temp wstt
7979      WHERE wstt.delivery_id = p_delivery_id
7980        AND dock_appoint_flag = 'N';
7981 
7982     IF l_flag_n = 0 THEN
7983       IF (l_debug = 1) THEN
7984         DEBUG('No records in wstt for direct ship flag =N', 'chk_del_for_direct_ship');
7985       END IF;
7986 
7987       RAISE NO_DATA_FOUND;
7988     ELSIF l_flag_n > 0 THEN
7989       IF (l_debug = 1) THEN
7990         DEBUG('Records in wstt for direct ship flag =N', 'chk_del_for_direct_ship');
7991       END IF;
7992 
7993       RAISE fnd_api.g_exc_error;
7994     END IF;
7995   --DBMS_OUTPUT.PUT_LINE('tHERE ARE NO LINES WITH DIRECT_FLAG =N');
7996   EXCEPTION
7997     WHEN NO_DATA_FOUND THEN
7998       BEGIN
7999         SELECT 1
8000           INTO l_num
8001           FROM DUAL
8002          WHERE EXISTS(
8003                  SELECT 1
8004                    FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd
8005                   WHERE wdd.delivery_detail_id = wda.delivery_detail_id
8006                     AND wda.delivery_id = p_delivery_id
8007                     AND wdd.released_status = 'Y'
8008                     AND NVL(container_flag, 'N') = 'N'
8009                     AND NOT EXISTS(
8010                          SELECT 1
8011                            FROM wms_shipping_transaction_temp wstt
8012                           WHERE wstt.delivery_detail_id = wdd.delivery_detail_id
8013                             AND wstt.delivery_id = p_delivery_id
8014                             AND wstt.direct_ship_flag = 'Y'
8015                             AND wstt.dock_appoint_flag = 'N'));
8016 
8017         IF (l_debug = 1) THEN
8018           DEBUG('This delivery has lines from other source othan than direct_ship', 'chk_del_for_direct_ship');
8019         END IF;
8020 
8021         --dbms_output.put_line('This delivery has lines from other source othan than direct_ship');
8022         IF (l_debug = 1) THEN
8023           DEBUG('Checking for line split', 'chk_del_for_direct_ship');
8024         END IF;
8025 
8026         l_parent_del_detail_ids.DELETE;
8027 
8028         FOR l_p_del_detail IN parent_del_detail LOOP
8029           l_parent_del_detail_ids(l_p_del_detail.delivery_detail_id)  := 1;
8030         END LOOP;
8031 
8032         --
8033         FOR l_del_detail IN del_detail LOOP
8034           IF NOT l_parent_del_detail_ids.EXISTS(l_del_detail.parent_delivery_detail_id) THEN
8035             IF (l_debug = 1) THEN
8036               DEBUG(
8037                 'Delivery line ' || l_del_detail.parent_delivery_detail_id || ' is packed in different container'
8038               , 'chk_del_for_direct_ship'
8039               );
8040               DEBUG('Check failed', 'chk_del_for_direct_ship');
8041             END IF;
8042 
8043             RAISE fnd_api.g_exc_error;
8044           END IF;
8045         END LOOP;
8046       EXCEPTION
8047         WHEN NO_DATA_FOUND THEN
8048           IF (l_debug = 1) THEN
8049             DEBUG('This delivery has lines that were loaded through Direct ship ', 'chk_del_for_direct_ship');
8050           END IF;
8051 
8052           NULL;
8053       END;
8054   END;
8055 EXCEPTION
8056   WHEN fnd_api.g_exc_error THEN
8057     x_return_status  := fnd_api.g_ret_sts_error;
8058     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8059   WHEN fnd_api.g_exc_unexpected_error THEN
8060     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8061     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8062   WHEN OTHERS THEN
8063     x_return_status  := fnd_api.g_ret_sts_unexp_error;
8064     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8065 END chk_del_for_direct_ship;
8066 
8067 
8068 --Bug#5262108.This is to determine the qty remaining to pick
8069 -- considering the overship tolerance , existing reservation, etc.
8070 FUNCTION Get_qty_to_pick(
8071    p_order_header_id NUMBER,
8072    p_order_line_id   NUMBER,
8073    p_org_id          NUMBER) RETURN NUMBER
8074 IS
8075   l_allowed_flag              VARCHAR2(1);
8076   l_max_quantity_allowed      NUMBER := 0 ;
8077   l_avail_req_quantity        NUMBER := 0 ;
8078   l_staged_qty                NUMBER := 0 ;
8079   l_total_resvd_qty           NUMBER := 0 ;
8080   l_return_status             VARCHAR2(30);
8081   l_line_set_id               NUMBER  ;
8082   l_debug                     NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8083 BEGIN
8084 
8085         IF (l_debug = 1) THEN
8086                 DEBUG('In Get_qty_to_pick  p_order_header_id '|| p_order_header_id , 'Get_qty_to_pick');
8087                 DEBUG('p_order_line_id '|| p_order_line_id ||',org:'||p_org_id, 'Get_qty_to_pick');
8088         END IF;
8089 
8090          --Call Shipping API to ger the qty remaining to pick
8091 	WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
8092                p_order_line_id         =>p_order_line_id,
8093                p_quantity_to_pick      => 0,
8094                x_allowed_flag          => l_allowed_flag,
8095                x_max_quantity_allowed  => l_max_quantity_allowed,
8096                x_avail_req_quantity    => l_avail_req_quantity,
8097                x_return_status         => l_return_status) ;
8098 
8099 	IF (l_debug = 1) THEN
8100         	DEBUG('l_allowed_flag='||l_allowed_flag,'Get_qty_to_pick');
8101           	DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Get_qty_to_pick');
8102           	DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Get_qty_to_pick');
8103           	DEBUG('l_return_status='||l_return_status,'Get_qty_to_pick');
8104         END IF;
8105 	IF l_return_status = fnd_api.g_ret_sts_error THEN
8106               IF (l_debug = 1) THEN
8107                 DEBUG('Check_Quantity_To_Pick API failed , ERROR ', 'Get_qty_to_pick');
8108               END IF;
8109               RAISE fnd_api.g_exc_error;
8110         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8111               IF (l_debug = 1) THEN
8112                 DEBUG('Check_Quantity_To_Pick API failed,UNEXPECTED ERROR', 'Get_qty_to_pick');
8113               END IF;
8114               RAISE fnd_api.g_exc_unexpected_error;
8115         END IF;
8116 
8117 	BEGIN
8118 
8119          SELECT nvl(wdd.source_line_set_id,wdd.source_line_id) into l_line_set_id
8120          FROM wsh_delivery_details wdd
8121 	 WHERE wdd.source_header_id = p_order_header_id
8122          AND wdd.source_line_id= p_order_line_id
8123 	 AND rownum<2;
8124 
8125 	 IF (l_debug = 1) THEN
8126         	DEBUG('l_line_set_id/line_id : '  || l_line_set_id,'Get_qty_to_pick');
8127          END IF;
8128 
8129           --Calculate the staged qty for the line set which curr line belongs to.
8130           SELECT  nvl(sum(nvl(picked_quantity,0)),0) INTO l_staged_qty
8131 	  FROM wsh_delivery_details wdd
8132 	  WHERE source_header_id = p_order_header_id
8133 	  AND released_status='Y'
8134 	  AND nvl(source_line_set_id,source_line_id) =  l_line_set_id;
8135 
8136          IF (l_debug = 1) THEN
8137         	DEBUG('l_staged_qty : '  || l_staged_qty,'Get_qty_to_pick');
8138          END IF;
8139 
8140           --Calculate the reserved qty for the line set which curr line belongs to.
8141 	  SELECT nvl(SUM(nvl(primary_reservation_quantity,0)),0) INTO l_total_resvd_qty
8142           FROM mtl_reservations
8143           WHERE organization_id= p_org_id
8144 	  AND nvl(staged_flag,'N') = 'Y'
8145 	  AND demand_source_line_id in ( SELECT source_line_id
8146                                          FROM wsh_delivery_details wdd
8147 	                                 WHERE source_header_id = p_order_header_id
8148 	                                 AND released_status='Y'
8149 	                                 AND nvl(source_line_set_id,source_line_id) =  l_line_set_id );
8150 
8151 
8152 	 IF (l_debug = 1) THEN
8153         	DEBUG('l_total_resvd_qty : '  || l_total_resvd_qty,'Get_qty_to_pick');
8154          END IF;
8155        EXCEPTION
8156         WHEN OTHERS THEN
8157           IF (l_debug = 1) THEN
8158 	    DEBUG(' OTHERS EXCEPTION !!! ' ,'Get_qty_to_pick');
8159 	  END IF;
8160 	  RAISE fnd_api.g_exc_unexpected_error;
8161           return -1;
8162        END;
8163 
8164        return ( nvl(l_max_quantity_allowed,0) + nvl(l_staged_qty,0) - nvl(l_total_resvd_qty,0) );
8165 
8166 END Get_qty_to_pick;
8167 
8168 /* Start of Patchset I procedures */
8169 
8170 /* This procedure process all the lines reserved against a LPN passed as parameter.
8171    Following tasks are performed here
8172    (a) table lpn_contents_tab is populated from l_lpn_contents_cur;
8173    (b) lpn material is checked for valid material status
8174    (c) each reserved line is checked if it is booked or not
8175    (d) PJM parameters are checked for each reserved line
8176    (e) delivery grouping rules are checked for each reserved line
8177    (f) User holds are checked for each reserved line
8178    (g) WDS is populated for all the reserved lines
8179  */
8180 
8181 PROCEDURE process_lpn(
8182   p_lpn_id                       IN            NUMBER
8183 , p_org_id                       IN            NUMBER
8184 , p_dock_door_id                               NUMBER
8185 , x_remaining_qty                OUT NOCOPY    NUMBER
8186 , x_num_line_processed           OUT NOCOPY    NUMBER
8187 , x_project_id                   OUT NOCOPY    NUMBER
8188 , x_task_id                      OUT NOCOPY    NUMBER
8189 , x_cross_project_allowed        OUT NOCOPY    VARCHAR2
8190 , x_cross_unit_allowed           OUT NOCOPY    VARCHAR2
8191 , x_group_by_customer_flag       OUT NOCOPY    VARCHAR2
8192 , x_group_by_fob_flag            OUT NOCOPY    VARCHAR2
8193 , x_group_by_freight_terms_flag  OUT NOCOPY    VARCHAR2
8194 , x_group_by_intmed_ship_flag    OUT NOCOPY    VARCHAR2
8195 , x_group_by_ship_method_flag    OUT NOCOPY    VARCHAR2
8196 , x_group_by_ship_to_loc_value   OUT NOCOPY    VARCHAR2
8197 , x_group_by_ship_from_loc_value OUT NOCOPY    VARCHAR2
8198 , x_group_by_customer_value      OUT NOCOPY    VARCHAR2
8199 , x_group_by_fob_value           OUT NOCOPY    VARCHAR2
8200 , x_group_by_freight_terms_value OUT NOCOPY    VARCHAR2
8201 , x_group_by_intmed_value        OUT NOCOPY    VARCHAR2
8202 , x_group_by_ship_method_value   OUT NOCOPY    VARCHAR2
8203 , x_ct_wt_enabled                OUT NOCOPY    NUMBER
8204 , x_return_status                OUT NOCOPY    VARCHAR2
8205 , x_msg_count                    OUT NOCOPY    NUMBER
8206 , x_msg_data                     OUT NOCOPY    VARCHAR2
8207 ) IS
8208   TYPE processed_line_rec IS RECORD(
8209     header_id            NUMBER
8210   , line_id              NUMBER
8211   , inventory_item_id    NUMBER
8212   , processed_flag       VARCHAR2(1)
8213   , serial_required_flag VARCHAR(1)
8214   , processed_quantity   NUMBER
8215   );
8216 
8217   TYPE order_line_rec IS RECORD(
8218     header_id            NUMBER
8219   , line_id              NUMBER
8220   , inventory_item_id    NUMBER
8221   , revision             VARCHAR2(3)
8222   , ordered_quantity     NUMBER
8223   , project_id           NUMBER
8224   , task_id              NUMBER
8225   , serial_control_code  NUMBER
8226   , end_item_unit_number VARCHAR2(30)
8227   , flow_status_code     VARCHAR2(30)
8228   );
8229 
8230   TYPE processed_lines_tab IS TABLE OF processed_line_rec
8231     INDEX BY BINARY_INTEGER;
8232 
8233   -- local variables
8234   l_sub_reservable_type         NUMBER;
8235   l_lpn_contents_lookup_rec     lpn_contents_lookup_rec;
8236   l_group_by_ship_to_loc_flag   VARCHAR2(1)                                     := 'Y';
8237   l_group_by_ship_from_loc_flag VARCHAR2(1)                                     := 'Y';
8238 
8239   -- Bug# 3464013
8240   TYPE l_lpn_content_cur_rec_typ IS RECORD(
8241     lpn_id                       wms_license_plate_numbers.lpn_id%TYPE
8242   , subinventory_code            wms_license_plate_numbers.subinventory_code%TYPE
8243   , locator_id                   wms_license_plate_numbers.locator_id%TYPE
8244   , inventory_item_id            wms_lpn_contents.inventory_item_id%TYPE
8245   , revision             wms_lpn_contents.revision%TYPE
8246   , lot_number                   wms_lpn_contents.lot_number%TYPE
8247   , quantity                     wms_lpn_contents.quantity%TYPE
8248   , revision_control             VARCHAR2(5)
8249   , lot_control                  VARCHAR2(5)
8250   , serial_control               VARCHAR2(5)
8251   , serial_control_code          mtl_system_items_b.serial_number_control_code%TYPE
8252   , reservable_type              mtl_system_items_b.reservable_type%TYPE
8253   , end_item_unit_number         mtl_serial_numbers.end_item_unit_number%TYPE
8254   , ont_pricing_qty_source       VARCHAR2(30)
8255   );
8256   l_lpn_content_cur_rec l_lpn_content_cur_rec_typ;
8257 
8258   l_qry_reservation_record      inv_reservation_global.mtl_reservation_rec_type;
8259   l_mtl_reservation_tab         inv_reservation_global.mtl_reservation_tbl_type;
8260 
8261   l_old_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type; --bug#5262108
8262   l_new_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type; --bug#5262108
8263   l_upd_dummy_sn                inv_reservation_global.serial_number_tbl_type;   --bug#5262108
8264 
8265   l_processed_lines_tab         processed_lines_tab;
8266   l_order_line_rec              order_line_rec;
8267   l_start_index                 NUMBER                                          := 1;
8268   l_end_index                   NUMBER                                          := 1;
8269   l_prev_item_id                NUMBER                                          := 0;
8270   l_num_line_processed          NUMBER                                          := 0;
8271   l_oe_order_header_id          NUMBER;
8272   l_return_status               VARCHAR2(30);
8273   l_msg_data                    VARCHAR2(20000);
8274   l_msg_count                   NUMBER;
8275   l_error_code                  NUMBER;
8276   l_temp_index                  NUMBER;
8277   l_mtl_reservation_tab_count   NUMBER;
8278   l_trx_temp_id                 NUMBER;
8279   l_order_line_status           NUMBER;
8280 
8281   -- for end unit validation
8282 
8283   TYPE skipped_line_tab IS TABLE OF NUMBER
8284     INDEX BY BINARY_INTEGER;
8285 
8286   l_skipped_line_tab            skipped_line_tab;
8287   l_debug                       NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8288 
8289   -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
8290   l_sql_query                   VARCHAR2(10000);
8291   TYPE l_lpn_contents_cur_type IS REF CURSOR;
8292   l_lpn_contents_cur            l_lpn_contents_cur_type;
8293   l_qty_to_pick                 NUMBER :=0 ;  --Bug#5262108
8294 BEGIN
8295   IF (l_debug = 1) THEN
8296     DEBUG('Process_LPN called with parameters : p_lpn_id = ' || p_lpn_id || ' : p_org_id = ' || p_org_id, 'Process_LPN');
8297   END IF;
8298 
8299   x_return_status       := fnd_api.g_ret_sts_success;
8300   x_ct_wt_enabled := 0;
8301   --clear all cached data structures
8302   g_lpn_contents_tab.DELETE;
8303   g_lpn_contents_lookup_tab.DELETE;
8304   g_total_lpn_quantity  := 0;
8305   g_checked_delivery_tab.DELETE;
8306   l_processed_lines_tab.DELETE;
8307   g_subinventory_code   := NULL;
8308   g_locator_id          := NULL;
8309   g_del_grp_rls_flags.DELETE;
8310   g_del_grp_rls_fld_value.DELETE;
8311   g_del_grp_rls_fld_temp.DELETE;
8312   g_checked_deliveries.DELETE;
8313   l_skipped_line_tab.DELETE;
8314   -- initializa who columns for session
8315   g_creation_date       := SYSDATE;
8316   g_created_by          := fnd_global.user_id;
8317   g_last_updated_by     := g_created_by;
8318   g_last_update_login   := fnd_global.login_id;
8319   g_last_update_date    := g_creation_date;
8320   -- cleaning temp data if the delivery was backordered for this lpn somethimes in past
8321   wms_direct_ship_pvt.cleanup_orphan_rec(p_org_id);
8322 
8323   IF (l_debug = 1) THEN
8324     DEBUG('Corrupted temp data cleaned ', 'Process_LPN');
8325   END IF;
8326 
8327   -- check if the sub in which lpn resides is reservable or not
8328   BEGIN
8329     SELECT sub.reservable_type
8330       INTO l_sub_reservable_type
8331       FROM mtl_secondary_inventories sub
8332      WHERE sub.secondary_inventory_name = (SELECT subinventory_code
8333                                              FROM wms_license_plate_numbers
8334                                             WHERE lpn_id = p_lpn_id)
8335        AND organization_id = p_org_id;
8336 
8337     IF (l_debug = 1) THEN
8338       DEBUG('l_sub_reservable_type= ' || l_sub_reservable_type, 'Process_LPN');
8339     END IF;
8340   EXCEPTION
8341     WHEN NO_DATA_FOUND THEN
8342       IF (l_debug = 1) THEN
8343         DEBUG('Exception getting reservable type of sub', 'Process_LPN');
8344       END IF;
8345 
8346       RAISE fnd_api.g_exc_error;
8347   END;
8348 
8349   IF l_sub_reservable_type <> 1 THEN
8350     IF (l_debug = 1) THEN
8351       DEBUG('LPN containes non reservable items', 'Process_LPN');
8352     END IF;
8353 
8354     x_return_status  := fnd_api.g_ret_sts_error;
8355     fnd_message.set_name('WMS', 'WMS_SUB_NON_RESERVABLE');
8356     fnd_msg_pub.ADD;
8357     RETURN;
8358   ELSE
8359     IF (l_debug = 1) THEN
8360       DEBUG('Before getting LPN contents ', 'Process_LPN');
8361     END IF;
8362 
8363     -- Bug# 3464013: Replaced the static cursor with dynamic ref cursor
8364     l_sql_query :=
8365     '    SELECT   wlpn.lpn_id ' ||
8366     '           , wlpn.subinventory_code ' ||
8367     '           , wlpn.locator_id ' ||
8368     '           , wlc.inventory_item_id ' ||
8369     '           , wlc.revision ' ||
8370     '           , wlc.lot_number ' ||
8371     '           , DECODE( ' ||
8372     '               wlc.uom_code ' ||
8373     '             , msi.primary_uom_code, NVL(msn.quantity, wlc.quantity) ' ||
8374     '             , GREATEST(inv_convert.inv_um_convert(NULL, NULL, NVL(msn.quantity, wlc.quantity), wlc.uom_code, msi.primary_uom_code, NULL ' ||
8375     '                 , NULL) ' ||
8376     '               , 0) ' ||
8377     '             ) quantity ' ||
8378     '           , DECODE(msi.revision_qty_control_code, 2, ''TRUE'', ''FALSE'') revision_control ' ||
8379     '           , DECODE(msi.lot_control_code, 2, ''TRUE'', ''FALSE'') lot_control ' ||
8380     '           , DECODE(msi.serial_number_control_code, 1, ''FALSE'', ''TRUE'') serial_control ' ||
8381     '           , msi.serial_number_control_code serial_control_code ' ||
8382     '           , msi.reservable_type ' ||
8383     '           , msn.end_item_unit_number ';
8384 
8385     IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL) THEN
8386       l_sql_query := l_sql_query || '      , msi.ont_pricing_qty_source ';
8387     ELSE
8388       l_sql_query := l_sql_query || '      , NULL ont_pricing_qty_source ';
8389     END IF;
8390 
8391     l_sql_query := l_sql_query ||
8392     '        FROM wms_lpn_contents wlc ' ||
8393     '           , wms_license_plate_numbers wlpn ' ||
8394     '           , mtl_system_items_b msi ' ||
8395     '           , (SELECT   lpn_id ' ||
8396     '                , revision ' ||
8397     '            , lot_number ' ||
8398     '                     , inventory_item_id ' ||
8399     '                     , end_item_unit_number ' ||
8400     '                     , COUNT(1) quantity ' ||
8401     '                  FROM mtl_serial_numbers ' ||
8402     '                 WHERE lpn_id IN(SELECT lpn_id ' ||
8403     '                                   FROM wms_license_plate_numbers ' ||
8404     '                                  WHERE organization_id = :p_org_id ' ||
8405     '                                    AND outermost_lpn_id = :p_lpn_id) ' ||
8406     '              GROUP BY lpn_id, revision, lot_number, inventory_item_id, end_item_unit_number) msn ' ||
8407     '       WHERE msi.inventory_item_id = wlc.inventory_item_id ' ||
8408     '         AND wlpn.lpn_id = wlc.parent_lpn_id ' ||
8409     '         AND wlpn.outermost_lpn_id = :p_lpn_id ' ||
8410     '         AND wlpn.organization_id = :p_org_id ' ||
8411     '         AND msi.organization_id = :p_org_id ' ||
8412     '         AND msn.lpn_id(+) = wlc.parent_lpn_id ' ||
8413     '         AND msn.inventory_item_id(+) = wlc.inventory_item_id ' ||
8414     '         AND NVL(msn.lot_number(+),''#NULL#'') = NVL(wlc.lot_number,''#NULL#'') ' ||
8415     '         AND NVL(msn.revision(+),''#NULL#'') = NVL(wlc.revision,''#NULL#'') ' ||
8416     '    ORDER BY wlc.inventory_item_id ';
8417 
8418     -- This cursor fetches lpn contents data for all the inner lpns packed into outer lpn.
8419     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;
8420     LOOP
8421       FETCH l_lpn_contents_cur INTO l_lpn_content_cur_rec;
8422       EXIT WHEN l_lpn_contents_cur%NOTFOUND;
8423 
8424       -- check if item is no reservable stop processing lpn
8425       IF l_lpn_content_cur_rec.reservable_type <> 1 THEN
8426         IF (l_debug = 1) THEN
8427           DEBUG('LPN containes non reservable items', 'Process_LPN');
8428         END IF;
8429 
8430         x_return_status  := fnd_api.g_ret_sts_error;
8431         fnd_message.set_name('WMS', 'WMS_ITEM_NON_RESERVABLE');
8432         fnd_msg_pub.ADD;
8433         RETURN;
8434       END IF;
8435 
8436       l_end_index                                            := l_lpn_contents_cur%ROWCOUNT;
8437       g_lpn_contents_tab(l_end_index).lpn_id                 := l_lpn_content_cur_rec.lpn_id;
8438       g_lpn_contents_tab(l_end_index).subinventory_code      := l_lpn_content_cur_rec.subinventory_code;
8439       g_lpn_contents_tab(l_end_index).locator_id             := l_lpn_content_cur_rec.locator_id;
8440       g_lpn_contents_tab(l_end_index).inventory_item_id      := l_lpn_content_cur_rec.inventory_item_id;
8441       g_lpn_contents_tab(l_end_index).revision               := l_lpn_content_cur_rec.revision;
8442       g_lpn_contents_tab(l_end_index).lot_number             := l_lpn_content_cur_rec.lot_number;
8443       g_lpn_contents_tab(l_end_index).serial_control_code    := l_lpn_content_cur_rec.serial_control_code;
8444       g_lpn_contents_tab(l_end_index).quantity               := l_lpn_content_cur_rec.quantity;
8445       g_lpn_contents_tab(l_end_index).end_item_unit_number   := l_lpn_content_cur_rec.end_item_unit_number;
8446       g_total_lpn_quantity                                   := g_total_lpn_quantity + l_lpn_content_cur_rec.quantity;
8447 
8448       -- For Catch Weight
8449       IF (G_WMS_CURRENT_RELEASE_LEVEL >= G_J_RELEASE_LEVEL AND
8450           x_ct_wt_enabled = 0 AND l_lpn_content_cur_rec.ont_pricing_qty_source = 'S') THEN
8451         x_ct_wt_enabled := 1;
8452       END IF;
8453 
8454       IF l_lpn_content_cur_rec.serial_control = 'TRUE' THEN
8455         g_lpn_contents_tab(l_end_index).serial_control  := TRUE;
8456       ELSE
8457         g_lpn_contents_tab(l_end_index).serial_control  := FALSE;
8458       END IF;
8459 
8460       IF l_lpn_content_cur_rec.lot_control = 'TRUE' THEN
8461         g_lpn_contents_tab(l_end_index).lot_control  := TRUE;
8462       ELSE
8463         g_lpn_contents_tab(l_end_index).lot_control  := FALSE;
8464       END IF;
8465 
8466       IF l_lpn_content_cur_rec.revision_control = 'TRUE' THEN
8467         g_lpn_contents_tab(l_end_index).revision_control  := TRUE;
8468       ELSE
8469         g_lpn_contents_tab(l_end_index).revision_control  := FALSE;
8470       END IF;
8471 
8472       -- populate l_lpn_contents_lookup_tab
8473       IF l_prev_item_id = 0 THEN
8474         l_prev_item_id  := l_lpn_content_cur_rec.inventory_item_id;
8475       END IF;
8476 
8477       IF (l_prev_item_id <> l_lpn_content_cur_rec.inventory_item_id) THEN
8478         l_start_index   := l_end_index;
8479         l_prev_item_id  := l_lpn_content_cur_rec.inventory_item_id;
8480       END IF;
8481 
8482       g_lpn_contents_lookup_tab(l_prev_item_id).start_index  := l_start_index;
8483       g_lpn_contents_lookup_tab(l_prev_item_id).end_index    := l_end_index;
8484 
8485       -- add current record quantity to g_total_lpn_quantity
8486       IF (l_debug = 1) THEN
8487         DEBUG('LPN_CONTENT_RECORDS', 'Process_LPN');
8488         DEBUG(
8489              'LPN_ID='
8490           || l_lpn_content_cur_rec.lpn_id
8491           || ' :ITEM_ID= '
8492           || l_lpn_content_cur_rec.inventory_item_id
8493           || ' :REVISION= '
8494           || l_lpn_content_cur_rec.revision
8495           || ' :LOT_NUMBER '
8496           || l_lpn_content_cur_rec.lot_number
8497           || ' :QUANTITY= '
8498           || l_lpn_content_cur_rec.quantity
8499           || ' :EIUN= '
8500           || l_lpn_content_cur_rec.end_item_unit_number
8501         , 'Process_LPN'
8502         );
8503       END IF;
8504     END LOOP; -- l_lpn_content_cur
8505 
8506     CLOSE l_lpn_contents_cur;
8507 
8508     IF (l_debug = 1) THEN
8509       DEBUG('Number of LPN content records ' || g_lpn_contents_tab.COUNT, 'Process_LPN');
8510       DEBUG('G_total_lpn_quantity =' || g_total_lpn_quantity, 'Process_LPN');
8511     END IF;
8512 
8513     IF g_lpn_contents_tab.COUNT = 0 THEN
8514       x_return_status  := fnd_api.g_ret_sts_error;
8515       fnd_message.set_name('WMS', 'WMS_EMPTY_LPN');
8516       fnd_msg_pub.ADD;
8517 
8518       IF (l_debug = 1) THEN
8519         DEBUG('LPN is empty', 'Process_LPN');
8520       END IF;
8521 
8522       RETURN;
8523     END IF;
8524 
8525     IF (l_debug = 1) THEN
8526       DEBUG('After getting LPN contents ', 'Process_LPN');
8527     END IF;
8528 
8529     -- perform material status check
8530     IF (l_debug = 1) THEN
8531       DEBUG('Before call to material status check', 'Process_LPN');
8532     END IF;
8533 
8534     wms_direct_ship_pvt.validate_status_lpn_contents(x_return_status => l_return_status, x_msg_count => l_msg_count
8535     , x_msg_data                   => l_msg_data, p_lpn_id => p_lpn_id, p_org_id => p_org_id);
8536 
8537     IF l_return_status = fnd_api.g_ret_sts_error THEN
8538       IF (l_debug = 1) THEN
8539         DEBUG('Validate Status LPN Contents procedure failed with error status E ', 'Process_LPN');
8540       END IF;
8541 
8542       x_return_status  := fnd_api.g_ret_sts_error;
8543       fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
8544       fnd_msg_pub.ADD;
8545       RETURN;
8546     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8547       IF (l_debug = 1) THEN
8548         DEBUG('Validate Status LPN Contents procedure failed with error status U ', 'Process_LPN');
8549       END IF;
8550 
8551       x_return_status  := fnd_api.g_ret_sts_error;
8552       fnd_message.set_name('WMS', 'WMS_INVALID_MAT_STATUS');
8553       fnd_msg_pub.ADD;
8554       RETURN;
8555     END IF;
8556 
8557     IF (l_debug = 1) THEN
8558       DEBUG('Validate status is success ', 'Process_LPN');
8559       DEBUG('After call to material status check', 'Process_LPN');
8560     END IF;
8561 
8562     -- set subinventory and locator
8563     g_subinventory_code      := g_lpn_contents_tab(1).subinventory_code;
8564     g_locator_id             := g_lpn_contents_tab(1).locator_id;
8565 
8566     -- get PJM parameters
8567     BEGIN
8568       SELECT allow_cross_proj_issues
8569            , allow_cross_unitnum_issues
8570         INTO g_cross_project_allowed
8571            , g_cross_unit_allowed
8572         FROM pjm_org_parameters
8573        WHERE organization_id = p_org_id;
8574 
8575       IF (l_debug = 1) THEN
8576         DEBUG('g_cross_project_allowed= ' || g_cross_project_allowed || ' g_cross_unit_allowed= ' || g_cross_unit_allowed, 'Process_LPN');
8577       END IF;
8578     EXCEPTION
8579       WHEN NO_DATA_FOUND THEN
8580         g_cross_project_allowed  := 'Y';
8581         g_cross_unit_allowed     := 'Y';
8582       WHEN OTHERS THEN
8583         IF (l_debug = 1) THEN
8584           DEBUG('Exception getting PJM Parameters ', 'Process_LPN');
8585         END IF;
8586 
8587         x_return_status  := fnd_api.g_ret_sts_error;
8588         RAISE fnd_api.g_exc_error;
8589     END;
8590 
8591     x_cross_project_allowed  := g_cross_project_allowed;
8592     x_cross_unit_allowed     := g_cross_unit_allowed;
8593 
8594     IF (l_debug = 1) THEN
8595       DEBUG('g_cross_project_allowed= ' || g_cross_project_allowed, 'Process_LPN');
8596       DEBUG('g_cross_unit_allowed= ' || g_cross_unit_allowed, 'Process_LPN');
8597     END IF;
8598 
8599     -- get project and task id
8600     IF g_cross_project_allowed = 'N' THEN
8601       BEGIN
8602         SELECT project_id
8603              , task_id
8604           INTO g_project_id
8605              , g_task_id
8606           FROM mtl_item_locations
8607          WHERE inventory_location_id = g_lpn_contents_tab(1).locator_id;
8608 
8609         IF (l_debug = 1) THEN
8610           DEBUG('g_project_id= ' || g_project_id || ' g_task_id= ' || g_task_id, 'Process_LPN');
8611         END IF;
8612       EXCEPTION
8613         WHEN OTHERS THEN
8614           IF (l_debug = 1) THEN
8615             DEBUG('Exception getting Project and Task Ids ', 'Process_LPN');
8616           END IF;
8617 
8618           x_return_status  := fnd_api.g_ret_sts_error;
8619           RAISE fnd_api.g_exc_error;
8620       END; -- get project and task id
8621 
8622       x_project_id  := g_project_id;
8623       x_task_id     := g_task_id;
8624     END IF;
8625 
8626     -- get Delivery Grouping Rule flags
8627     BEGIN
8628       SELECT group_by_customer_flag
8629            , group_by_fob_flag
8630            , group_by_freight_terms_flag
8631            , group_by_intmed_ship_to_flag
8632            , group_by_ship_method_flag
8633         INTO x_group_by_customer_flag
8634            , x_group_by_fob_flag
8635            , x_group_by_freight_terms_flag
8636            , x_group_by_intmed_ship_flag
8637            , x_group_by_ship_method_flag
8638         FROM wsh_shipping_parameters
8639        WHERE organization_id = p_org_id;
8640 
8641       IF (l_debug = 1) THEN
8642         DEBUG('l_group_by_customer_flag= ' || x_group_by_customer_flag, 'Process_LPN');
8643         DEBUG('l_group_by_fob_flag= ' || x_group_by_fob_flag, 'Process_LPN');
8644         DEBUG('l_group_by_freight_terms_flag= ' || x_group_by_freight_terms_flag, 'Process_LPN');
8645         DEBUG('l_group_by_intmed_ship_flag= ' || x_group_by_intmed_ship_flag, 'Process_LPN');
8646         DEBUG('l_group_by_ship_method_flag= ' || x_group_by_ship_method_flag, 'Process_LPN');
8647       END IF;
8648 
8649       -- populate delivery grouping flags table
8650       g_del_grp_rls_flags(1)  := l_group_by_ship_from_loc_flag;
8651       g_del_grp_rls_flags(2)  := l_group_by_ship_to_loc_flag;
8652       g_del_grp_rls_flags(3)  := x_group_by_customer_flag;
8653       g_del_grp_rls_flags(4)  := x_group_by_fob_flag;
8654       g_del_grp_rls_flags(5)  := x_group_by_freight_terms_flag;
8655       g_del_grp_rls_flags(6)  := x_group_by_intmed_ship_flag;
8656       g_del_grp_rls_flags(7)  := x_group_by_ship_method_flag;
8657     EXCEPTION
8658       WHEN NO_DATA_FOUND THEN
8659         IF (l_debug = 1) THEN
8660           DEBUG('No data found for delivery grouping flags', 'Process_LPN');
8661         END IF;
8662 
8663         x_return_status  := fnd_api.g_ret_sts_error;
8664         RAISE fnd_api.g_exc_error;
8665       WHEN OTHERS THEN
8666         IF (l_debug = 1) THEN
8667           DEBUG('Exception getting delivery grouping rule flags', 'Process_LPN');
8668         END IF;
8669 
8670         x_return_status  := fnd_api.g_ret_sts_error;
8671         RAISE fnd_api.g_exc_error;
8672     END;
8673 
8674     -- start process lines reserved against this lpn
8675     IF (l_debug = 1) THEN
8676       DEBUG('Starting processing of reserved lines', 'Process_LPN');
8677     END IF;
8678 
8679     FOR c_index IN 1 .. g_lpn_contents_tab.COUNT LOOP
8680       -- query reservations for a line
8681       IF (l_debug = 1) THEN
8682         DEBUG('Querying reservations for lpn_contents_rec ' || c_index, 'Process_LPN');
8683       END IF;
8684 
8685       l_qry_reservation_record.inventory_item_id      := g_lpn_contents_tab(c_index).inventory_item_id;
8686       l_qry_reservation_record.revision               := g_lpn_contents_tab(c_index).revision;
8687       l_qry_reservation_record.lot_number             := g_lpn_contents_tab(c_index).lot_number;
8688       l_qry_reservation_record.lpn_id                 := g_lpn_contents_tab(c_index).lpn_id;
8689       l_qry_reservation_record.supply_source_type_id  := inv_reservation_global.g_source_type_inv;
8690       l_mtl_reservation_tab.DELETE;
8691       inv_reservation_pub.query_reservation(
8692         p_api_version_number         => 1.0
8693       , p_init_msg_lst               => fnd_api.g_false
8694       , x_return_status              => l_return_status
8695       , x_msg_count                  => l_msg_count
8696       , x_msg_data                   => l_msg_data
8697       , p_query_input                => l_qry_reservation_record
8698       , p_lock_records               => fnd_api.g_false
8699       , x_mtl_reservation_tbl        => l_mtl_reservation_tab
8700       , x_mtl_reservation_tbl_count  => l_mtl_reservation_tab_count
8701       , x_error_code                 => l_error_code
8702       );
8703 
8704       IF l_return_status = fnd_api.g_ret_sts_error THEN
8705         IF (l_debug = 1) THEN
8706           DEBUG('Reservation query api failed', 'Process_LPN');
8707         END IF;
8708 
8709         RAISE fnd_api.g_exc_error;
8710       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8711         IF (l_debug = 1) THEN
8712           DEBUG('Reservation query api failed', 'Process_LPN');
8713         END IF;
8714 
8715         RAISE fnd_api.g_exc_unexpected_error;
8716       END IF;
8717 
8718       IF (l_debug = 1) THEN
8719         DEBUG('Reservation records found = ' || l_mtl_reservation_tab.COUNT, 'Process_LPN');
8720       END IF;
8721 
8722       IF l_mtl_reservation_tab.COUNT > 0 THEN
8723         FOR resv_index IN 1 .. l_mtl_reservation_tab.COUNT LOOP
8724           --  fetch line data
8725           BEGIN
8726             SELECT oel.header_id
8727                  , oel.line_id
8728                  , oel.inventory_item_id
8729                  , oel.item_revision
8730                  , DECODE(
8731                      oel.order_quantity_uom
8732                    , msi.primary_uom_code, oel.ordered_quantity
8733                    , GREATEST(
8734                        inv_convert.inv_um_convert(NULL, NULL, oel.ordered_quantity, oel.order_quantity_uom, msi.primary_uom_code, NULL
8735                        , NULL)
8736                      , 0
8737                      )
8738                    ) ordered_quantity
8739                  , oel.project_id
8740                  , oel.task_id
8741                  , msi.serial_number_control_code
8742                  , oel.end_item_unit_number
8743                  , oel.flow_status_code
8744               INTO l_order_line_rec
8745               FROM oe_order_lines_all oel, mtl_system_items msi
8746              WHERE msi.organization_id = p_org_id
8747                AND oel.inventory_item_id = msi.inventory_item_id
8748                AND line_id = l_mtl_reservation_tab(resv_index).demand_source_line_id;
8749           EXCEPTION
8750             WHEN NO_DATA_FOUND THEN
8751               IF (l_debug = 1) THEN
8752                 DEBUG('No data found for line', 'Process_LPN');
8753               END IF;
8754 
8755               x_return_status  := fnd_api.g_ret_sts_error;
8756               RAISE fnd_api.g_exc_error;
8757             WHEN OTHERS THEN
8758               IF (l_debug = 1) THEN
8759                 DEBUG('Exception getting data for line ', 'Process_LPN');
8760               END IF;
8761 
8762               x_return_status  := fnd_api.g_ret_sts_error;
8763               RAISE fnd_api.g_exc_error;
8764           END;
8765 
8766           IF (l_debug = 1) THEN
8767             DEBUG('Processing line: ', 'Process_LPN');
8768             DEBUG('l_order_line_rec.header_id: ' || l_order_line_rec.header_id, 'Process_LPN');
8769             DEBUG('l_order_line_rec.line_id: ' || l_order_line_rec.line_id, 'Process_LPN');
8770             DEBUG('l_order_line_rec.inventory_item_id: ' || l_order_line_rec.inventory_item_id, 'Process_LPN');
8771             --DEBUG('l_order_line_rec.item_revision: ' || l_order_line_rec.item_revision, 'Process_LPN');
8772             DEBUG('l_order_line_rec.ordered_quantity: ' || l_order_line_rec.ordered_quantity, 'Process_LPN');
8773             DEBUG('l_order_line_rec.project_id: ' || l_order_line_rec.project_id, 'Process_LPN');
8774             DEBUG('l_order_line_rec.task_id: ' || l_order_line_rec.task_id, 'Process_LPN');
8775             --DEBUG('l_order_line_rec.serial_number_control_code: ' ||
8776                   -- l_order_line_rec.serial_number_control_code, 'Process_LPN');
8777             DEBUG('l_order_line_rec.end_item_unit_number: ' ||
8778                    l_order_line_rec.end_item_unit_number, 'Process_LPN');
8779             DEBUG('l_order_line_rec.flow_status_code: ' ||
8780                    l_order_line_rec.flow_status_code, 'Process_LPN');
8781 
8782           END IF;
8783 
8784           /*IF l_order_line_rec.flow_status_code NOT IN ('AWAITING_SHIPPING', THEN
8785              IF (l_debug = 1) THEN
8786                 DEBUG('Order Line not booked','Process_LPN');
8787              END IF;
8788              x_return_status:=FND_API.G_RET_STS_ERROR;
8789              FND_MESSAGE.SET_NAME('WMS','WMS_LINE_NOT_BOOKED');
8790              FND_MSG_PUB.ADD;
8791              RETURN;
8792           END IF;*/
8793           --Bug 2909327:Changed the call to check wsh_delivery_details instead of oel*/
8794           BEGIN
8795             SELECT 1
8796               INTO l_order_line_status
8797               FROM DUAL
8798              WHERE EXISTS(SELECT delivery_detail_id
8799                           FROM wsh_delivery_details_ob_grp_v
8800                           WHERE released_status IN('R', 'B')
8801                           AND source_line_id = l_order_line_rec.line_id
8802                           UNION ALL -- bug 4232713 - Need to consider staged WDD lines for overpicking scenarios
8803                           SELECT wdd.delivery_detail_id
8804                           FROM wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
8805                           WHERE wdd.released_status = 'Y'
8806                           AND wdd.source_line_id = wds.order_line_id);
8807           EXCEPTION
8808             WHEN NO_DATA_FOUND THEN
8809               IF (l_debug = 1) THEN
8810                 DEBUG('Order Line not booked', 'Process_LPN');
8811               END IF;
8812 
8813               x_return_status  := fnd_api.g_ret_sts_error;
8814               fnd_message.set_name('WMS', 'WMS_LINE_NOT_BOOKED');
8815               fnd_msg_pub.ADD;
8816               RETURN;
8817           END;
8818           IF (l_debug = 1) THEN
8819              DEBUG('l_order_line_status: '|| l_order_line_status, 'Process_LPN');
8820              DEBUG('g_cross_project_allowed: '|| g_cross_project_allowed, 'Process_LPN');
8821           END IF;
8822 
8823           -- check PJM Parameters
8824           IF g_cross_project_allowed <> 'Y' THEN
8825             IF g_project_id <> l_order_line_rec.project_id
8826                OR g_task_id <> l_order_line_rec.task_id THEN
8827               IF (l_debug = 1) THEN
8828                 DEBUG('Validation of PJM parameters failed', 'Process_LPN');
8829               END IF;
8830 
8831               x_return_status  := fnd_api.g_ret_sts_error;
8832               fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
8833               fnd_msg_pub.ADD;
8834               RETURN;
8835             END IF;
8836           END IF;
8837 
8838           IF (l_debug = 1) THEN
8839               DEBUG('validate_end_unit_num_at ' ,'Process_LPN');
8840           END IF;
8841           -- check end_item_unit_num
8842           IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(c_index, l_order_line_rec.end_item_unit_number) THEN
8843             l_skipped_line_tab(l_order_line_rec.line_id)  := 1;
8844 
8845             IF (l_debug = 1) THEN
8846               DEBUG('line ' || l_order_line_rec.line_id || ' put into skipped line', 'Process_LPN');
8847             END IF;
8848 
8849             GOTO skip_line; -- line
8850           ELSE
8851             IF l_skipped_line_tab.EXISTS(l_order_line_rec.line_id) THEN
8852               l_skipped_line_tab.DELETE(l_order_line_rec.line_id);
8853 
8854               IF (l_debug = 1) THEN
8855                 DEBUG('line ' || l_order_line_rec.line_id || ' removed from skipped lines', 'Process_LPN');
8856               END IF;
8857 
8858               -- check if lpn has enough quantity to satisfy reservation for end unit num
8859               IF g_lpn_contents_tab(c_index).quantity < l_mtl_reservation_tab(resv_index).primary_reservation_quantity THEN
8860                 IF (l_debug = 1) THEN
8861                   DEBUG('LPN quantity is less then the reserved quantity', 'Process_LPN');
8862                 END IF;
8863 
8864                 x_return_status  := fnd_api.g_ret_sts_error;
8865                 fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
8866                 fnd_msg_pub.ADD;
8867                 RETURN;
8868               END IF;
8869             END IF;
8870           END IF;
8871 
8872           -- validate delivery grouping rules
8873           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
8874             IF (l_debug = 1) THEN
8875               DEBUG('Delivery grouping failed', 'Process_LPN');
8876             END IF;
8877 
8878             x_return_status  := fnd_api.g_ret_sts_error;
8879             fnd_message.set_name('WMS', 'WMS_DEL_GRP_FAILED');
8880             fnd_msg_pub.ADD;
8881             RETURN;
8882           END IF;
8883 
8884           -- check delivery for direct ship
8885           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
8886                 , l_order_line_rec.line_id) THEN
8887             IF (l_debug = 1) THEN
8888               DEBUG('Checl del for direct ship failed', 'Process_LPN');
8889             END IF;
8890 
8891             x_return_status  := fnd_api.g_ret_sts_error;
8892             fnd_message.set_name('WMS', 'WMS_DEL_LINES_MIX');
8893             fnd_msg_pub.ADD;
8894             RETURN;
8895           END IF;
8896 
8897           IF l_num_line_processed = 0 THEN                                -- needs to be set only once
8898                                            -- set out parameters
8899             x_group_by_ship_from_loc_value  := g_del_grp_rls_fld_temp(1);
8900             x_group_by_ship_to_loc_value    := g_del_grp_rls_fld_temp(2);
8901             x_group_by_customer_value       := g_del_grp_rls_fld_temp(3);
8902             x_group_by_fob_value            := g_del_grp_rls_fld_temp(4);
8903             x_group_by_freight_terms_value  := g_del_grp_rls_fld_temp(5);
8904             x_group_by_intmed_value         := g_del_grp_rls_fld_temp(6);
8905             x_group_by_ship_method_value    := g_del_grp_rls_fld_temp(7);
8906 
8907             IF (l_debug = 1) THEN
8908               DEBUG('Delivery grouping field values set to return parameters', 'Process_LPN');
8909             END IF;
8910           END IF;
8911 
8912           -- check holds
8913           check_holds(
8914             p_order_header_id            => l_order_line_rec.header_id
8915           , p_order_line_id              => l_order_line_rec.line_id
8916           , x_return_status              => l_return_status
8917           , x_msg_count                  => l_msg_count
8918           , x_msg_data                   => l_msg_data
8919           );
8920 
8921           IF l_return_status = fnd_api.g_ret_sts_error THEN
8922             IF (l_debug = 1) THEN
8923               DEBUG('Check Holds Failed', 'Process_LPN');
8924             END IF;
8925 
8926             x_return_status  := fnd_api.g_ret_sts_error;
8927             fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
8928             fnd_msg_pub.ADD;
8929             RETURN;
8930           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8931             IF (l_debug = 1) THEN
8932               DEBUG('Check holds Failed :Unexpected', 'Process_LPN');
8933             END IF;
8934 
8935             x_return_status  := fnd_api.g_ret_sts_error;
8936             fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
8937             fnd_msg_pub.ADD;
8938             RETURN;
8939           ELSE
8940             IF (l_debug = 1) THEN
8941               DEBUG('Check Holds Succeeded', 'Process LPN');
8942             END IF;
8943           END IF;
8944 
8945           -- get oe_order_header_id from sales order header id
8946           inv_salesorder.get_oeheader_for_salesorder(
8947             p_salesorder_id              => l_mtl_reservation_tab(resv_index).demand_source_header_id
8948           , x_oe_header_id               => l_oe_order_header_id
8949           , x_return_status              => l_return_status
8950           );
8951 
8952           IF (l_debug = 1) THEN
8953             DEBUG('l_oe_order_header_id = ' || l_oe_order_header_id, 'Process_LPN');
8954             DEBUG('l_oe_order_line_id = ' || l_mtl_reservation_tab(resv_index).demand_source_line_id, 'Process_LPN');
8955           END IF;
8956 
8957           --Bug5262108. Begin. Check qty to pick considering overship tolerance.
8958 	  l_qty_to_pick := WMS_DIRECT_SHIP_PVT.Get_qty_to_pick(
8959 	                     l_oe_order_header_id,
8960 		             l_mtl_reservation_tab(resv_index).demand_source_line_id ,
8961 		             p_org_id);
8962            IF (l_debug = 1) THEN
8963              DEBUG('l_qty_to_pick = ' || l_qty_to_pick, 'Process_LPN');
8964 	     DEBUG('reservation_id= ' || l_mtl_reservation_tab(resv_index).reservation_id, 'Process_LPN');
8965              DEBUG('primary_reservation_quantity='||l_mtl_reservation_tab(resv_index).primary_reservation_quantity, 'Process_LPN');
8966 
8967            END IF;
8968 
8969           IF (l_qty_to_pick > 0 ) THEN
8970 	     IF ( l_mtl_reservation_tab(resv_index).primary_reservation_quantity > l_qty_to_pick ) THEN
8971                  l_mtl_reservation_tab(resv_index).primary_reservation_quantity := l_qty_to_pick ;
8972 		   --Need to update the reservation.
8973 	          l_old_upd_resv_rec.reservation_id               := l_mtl_reservation_tab(resv_index).reservation_id;
8974                   l_new_upd_resv_rec.primary_reservation_quantity := l_mtl_reservation_tab(resv_index).primary_reservation_quantity ;
8975                   l_new_upd_resv_rec.reservation_quantity         := l_mtl_reservation_tab(resv_index).primary_reservation_quantity ;
8976                   l_new_upd_resv_rec.staged_flag                  :='Y';
8977 
8978                   inv_reservation_pub.update_reservation(
8979 		      p_api_version_number         => 1.0
8980 	            , p_init_msg_lst               => fnd_api.g_false
8981 	            , x_return_status              => l_return_status
8982 		    , x_msg_count                  => l_msg_count
8983 	            , x_msg_data                   => l_msg_data
8984 		    , p_original_rsv_rec           => l_old_upd_resv_rec
8985 	            , p_to_rsv_rec                 => l_new_upd_resv_rec
8986 	            , p_original_serial_number     => l_upd_dummy_sn
8987 	            , p_to_serial_number           => l_upd_dummy_sn
8988 	            , p_validation_flag            => fnd_api.g_true
8989 		    );
8990             ELSE
8991 	          inv_staged_reservation_util.update_staged_flag(
8992 		           x_return_status              => l_return_status,
8993                            x_msg_count                  => l_msg_count ,
8994                            x_msg_data                   => l_msg_data,
8995                            p_reservation_id             => l_mtl_reservation_tab(resv_index).reservation_id ,
8996                            p_staged_flag                => 'Y'
8997                   );
8998             END IF;
8999 
9000             IF l_return_status = fnd_api.g_ret_sts_error THEN
9001               IF (l_debug = 1) THEN
9002                 DEBUG('Update reservation failed for reservation_id ' || l_old_upd_resv_rec.reservation_id, 'Process_LPN');
9003               END IF;
9004               RAISE fnd_api.g_exc_error;
9005             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9006               IF (l_debug = 1) THEN
9007                 DEBUG('Update reservation failed for reservation_id with status U' ||l_old_upd_resv_rec.reservation_id, 'Process_LPN');
9008               END IF;
9009               RAISE fnd_api.g_exc_unexpected_error;
9010             END IF;
9011 
9012          ELSE
9013 	   EXIT;
9014 	 END IF;
9015          --Bug#5262108.ends
9016 
9017           -- update processed quantity
9018           IF l_processed_lines_tab.EXISTS(l_mtl_reservation_tab(resv_index).demand_source_line_id) THEN
9019             -- set processed quantity
9020             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity  :=
9021                  l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
9022                + l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
9023           ELSE
9024             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity  :=
9025                                                                              l_mtl_reservation_tab(resv_index).primary_reservation_quantity;
9026             -- set header id
9027             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).header_id           := l_oe_order_header_id;
9028             -- set line id
9029             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).line_id             :=
9030                                                                                     l_mtl_reservation_tab(resv_index).demand_source_line_id;
9031             -- set item id
9032             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).inventory_item_id   :=
9033                                                                                         l_mtl_reservation_tab(resv_index).inventory_item_id;
9034 
9035             IF g_lpn_contents_tab(c_index).serial_control_code = 6 THEN
9036               l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).serial_required_flag  := 'Y';
9037 
9038               IF (l_debug = 1) THEN
9039                 DEBUG('Serial control code = ' || g_lpn_contents_tab(c_index).serial_control_code, 'Process_LPN');
9040               END IF;
9041             ELSE
9042               l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).serial_required_flag  := 'N';
9043 
9044               IF (l_debug = 1) THEN
9045                 DEBUG('Serial control code = ' || g_lpn_contents_tab(c_index).serial_control_code, 'Process_LPN');
9046               END IF;
9047             END IF;
9048 
9049             -- increment the number of lines processed
9050             l_num_line_processed                                                                               := l_num_line_processed + 1;
9051           END IF;
9052 
9053           -- update lpn_content_rec and g_total_lpn_quantity
9054           g_lpn_contents_tab(c_index).quantity  :=
9055                                      (
9056                                       g_lpn_contents_tab(c_index).quantity - l_mtl_reservation_tab(resv_index).primary_reservation_quantity
9057                                      );
9058           g_total_lpn_quantity                  :=(g_total_lpn_quantity - l_mtl_reservation_tab(resv_index).primary_reservation_quantity);
9059 
9060           IF l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity =
9061                                                                                                            l_order_line_rec.ordered_quantity THEN
9062             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_flag  := 'Y';
9063           ELSE
9064             l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_flag  := 'N';
9065           END IF;
9066 
9067           IF (l_debug = 1) THEN
9068             DEBUG(
9069               'Processed quantity=' || l_processed_lines_tab(l_mtl_reservation_tab(resv_index).demand_source_line_id).processed_quantity
9070             , 'Process_LPN'
9071             );
9072           END IF;
9073 
9074           <<skip_line>>
9075           IF (l_debug = 1) THEN
9076             DEBUG('dummy', 'Process_LPN');
9077           END IF;
9078         END LOOP; --l_mtl_reservation_tab.count
9079       END IF; -- l_mtl_reservation_tab.count>0
9080     END LOOP;           -- for g_lpn_contents_tab
9081               -- if there is any record in l_skipped_line_tab then fail because for atleat one line
9082               -- end unit validation failed
9083 
9084     IF l_skipped_line_tab.COUNT > 0 THEN
9085       IF (l_debug = 1) THEN
9086         DEBUG('End item unit validation failed', 'Process_LPN');
9087       END IF;
9088 
9089       x_return_status  := fnd_api.g_ret_sts_error;
9090       fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
9091       fnd_msg_pub.ADD;
9092       RETURN;
9093     END IF;
9094 
9095     IF (l_debug = 1) THEN
9096       DEBUG('All reserved lines processed. Num= ' || l_num_line_processed, 'Process_LPN');
9097     END IF;
9098 
9099     x_num_line_processed     := l_num_line_processed;
9100     x_remaining_qty          := g_total_lpn_quantity;
9101 
9102     IF (l_debug = 1) THEN
9103       DEBUG('G_total_lpn_quantity =' || g_total_lpn_quantity, 'Process_LPN');
9104     END IF;
9105 
9106     -- get group_id
9107     BEGIN
9108       SELECT mtl_material_transactions_s.NEXTVAL
9109         INTO g_group_id
9110         FROM DUAL;
9111     EXCEPTION
9112       WHEN NO_DATA_FOUND THEN
9113         g_group_id  := 0;
9114     END;
9115 
9116     IF (l_debug = 1) THEN
9117       DEBUG('Group Id= ' || g_group_id, 'Process_LPN');
9118     END IF;
9119 
9120     -- insert data into wds
9121      IF l_num_line_processed > 0 THEN
9122     IF (l_debug = 1) THEN
9123       DEBUG('Inserting records into WDS', 'Process_LPN');
9124     END IF;
9125 
9126        l_temp_index  := l_processed_lines_tab.FIRST;
9127 
9128       WHILE l_temp_index IS NOT NULL LOOP
9129         IF (l_debug = 1) THEN
9130           DEBUG('l_temp_index = ' || l_temp_index, 'Process_LPN');
9131           DEBUG('header_id= ' || l_processed_lines_tab(l_temp_index).header_id, '');
9132           DEBUG('line_id= ' || l_processed_lines_tab(l_temp_index).line_id, '');
9133           DEBUG('processed_flag= ' || l_processed_lines_tab(l_temp_index).processed_flag, '');
9134           DEBUG('processed_quantity= ' || l_processed_lines_tab(l_temp_index).processed_quantity, '');
9135           DEBUG('serial_required_flag= ' || l_processed_lines_tab(l_temp_index).serial_required_flag, '');
9136         END IF;
9137 
9138         IF (l_processed_lines_tab(l_temp_index).serial_required_flag = 'Y') THEN
9139           BEGIN
9140             SELECT mtl_material_transactions_s.NEXTVAL
9141               INTO l_trx_temp_id
9142               FROM DUAL;
9143           EXCEPTION
9144             WHEN NO_DATA_FOUND THEN
9145               IF (l_debug = 1) THEN
9146                 DEBUG('Unable to get transaction_temp_id', 'Process_LPN');
9147               END IF;
9148 
9149               x_return_status  := fnd_api.g_ret_sts_error;
9150               RAISE fnd_api.g_exc_error;
9151           END;
9152         ELSE
9153           l_trx_temp_id  := NULL;
9154         END IF;
9155 
9156         INSERT INTO wms_direct_ship_temp
9157                     (
9158                      GROUP_ID
9159                    , organization_id
9160                    , dock_door_id
9161                    , lpn_id
9162                    , order_header_id
9163                    , order_line_id
9164                    , line_item_id
9165                    , processed_quantity
9166                    , processed_flag
9167                    , serial_required_flag
9168                    , transaction_temp_id
9169                    , creation_date
9170                    , created_by
9171                    , last_update_date
9172                    , last_updated_by
9173                    , last_update_login
9174                     )
9175              VALUES (
9176                      g_group_id
9177                    , p_org_id
9178                    , p_dock_door_id
9179                    , p_lpn_id
9180                    , l_processed_lines_tab(l_temp_index).header_id
9181                    , l_processed_lines_tab(l_temp_index).line_id
9182                    , l_processed_lines_tab(l_temp_index).inventory_item_id
9183                    , l_processed_lines_tab(l_temp_index).processed_quantity
9184                    , l_processed_lines_tab(l_temp_index).processed_flag
9185                    , l_processed_lines_tab(l_temp_index).serial_required_flag
9186                    , l_trx_temp_id
9187                    , g_creation_date
9188                    , g_created_by
9189                    , g_last_update_date
9190                    , g_last_updated_by
9191                    , g_last_update_login
9192                     );
9193 
9194         l_temp_index  := l_processed_lines_tab.NEXT(l_temp_index);
9195       END LOOP;
9196     END IF;
9197   END IF; --l_sub_reservable_type
9198 
9199   IF (l_debug = 1) THEN
9200     DEBUG('Completed successfully ', 'Process_LPN');
9201   END IF;
9202 EXCEPTION
9203   WHEN OTHERS THEN
9204     IF (l_debug = 1) THEN
9205       DEBUG('Unexpected error occured: ' || SQLERRM, 'Process_LPN');
9206     END IF;
9207 
9208     x_return_status  := fnd_api.g_ret_sts_unexp_error;
9209     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
9210     fnd_msg_pub.ADD;
9211 END process_lpn;
9212 
9213 /* This procedure creates reservations for a line if it is not there and
9214    inserts processed line record into WDS.
9215 */
9216 
9217 PROCEDURE process_line(
9218   p_lpn_id               IN            NUMBER
9219 , p_org_id               IN            NUMBER
9220 , p_dock_door_id                       NUMBER
9221 , p_order_header_id      IN            NUMBER
9222 , p_order_line_id        IN            NUMBER
9223 , p_inventory_item_id    IN            NUMBER
9224 , p_revision             IN            VARCHAR2
9225 , p_end_item_unit_number IN            VARCHAR2
9226 , p_ordered_quantity     IN            NUMBER
9227 , p_processed_quantity   IN            NUMBER
9228 , p_date_requested       IN            DATE
9229 , p_primary_uom_code     IN            VARCHAR2
9230 , x_remaining_quantity   OUT NOCOPY    NUMBER
9231 , x_return_status        OUT NOCOPY    VARCHAR2
9232 , x_msg_count            OUT NOCOPY    NUMBER
9233 , x_msg_data             OUT NOCOPY    VARCHAR2
9234 ) IS
9235   -- for query reservation
9236   l_qry_reservation_record    inv_reservation_global.mtl_reservation_rec_type;
9237   l_mtl_reservation_tab       inv_reservation_global.mtl_reservation_tbl_type;
9238   l_mtl_reservation_tab_temp  inv_reservation_global.mtl_reservation_tbl_type;
9239   l_mtl_reservation_tab_count NUMBER;
9240   l_mtl_resv_tab_count_temp   NUMBER;
9241   l_return_status             VARCHAR2(10);
9242   l_msg_count                 NUMBER;
9243   l_msg_data                  VARCHAR2(20000);
9244   l_error_code                NUMBER;
9245   -- genaral
9246   l_resv_rec                  inv_reservation_global.mtl_reservation_rec_type;
9247   l_demand_source_header_id   NUMBER;
9248   l_order_source_id           NUMBER;
9249   l_demand_source_type_id     NUMBER;
9250   l_quantity_reserved         NUMBER                                          := 0;
9251   l_quantity_to_transfer_resv NUMBER                                          := 0;
9252   l_quantity_create_resv      NUMBER                                          := 0;
9253   l_qty_reserved_tmp          NUMBER                                          := 0;
9254   l_create_reservation_flag   VARCHAR2(1)                                     := 'N';
9255   l_reservable_type           NUMBER;
9256   l_processed_quantity        NUMBER                                          := p_processed_quantity;
9257   l_start_index               NUMBER;
9258   l_end_index                 NUMBER;
9259   l_lpn_cont_rec              lpn_content_rec;
9260   l_serial_required_flag      VARCHAR2(1)                                     := 'N';
9261   l_trx_temp_id               NUMBER;
9262   l_temp_var                  NUMBER;
9263   -- For quantity tree declaration
9264   l_transactable_qty          NUMBER;
9265   l_qoh                       NUMBER;
9266   l_rqoh                      NUMBER;
9267   l_qr                        NUMBER;
9268   l_qs                        NUMBER;
9269   l_atr                       NUMBER;
9270   -- For transfer reservation
9271   l_old_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
9272   l_new_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
9273   l_dummy_sn                  inv_reservation_global.serial_number_tbl_type;
9274   l_new_rsv_id                NUMBER;
9275   l_current_rsv_rem_qty      NUMBER;
9276   -- For create_reservation
9277   l_reservation_record        inv_reservation_global.mtl_reservation_rec_type;
9278   l_quantity_reserved_tmp     NUMBER;
9279   l_reservation_id            NUMBER;
9280   -- For Update reservation
9281   l_upd_resv_rec              inv_reservation_global.mtl_reservation_rec_type;
9282   l_old_upd_resv_rec          inv_reservation_global.mtl_reservation_rec_type;
9283   l_upd_dummy_sn              inv_reservation_global.serial_number_tbl_type;
9284   l_chk_resv_qty              NUMBER;
9285   l_resv_id                   NUMBER;
9286   l_other_resv_qty            NUMBER;
9287   l_debug                     NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
9288   l_count_wdst                NUMBER; --Bug#4546137.
9289   l_overship_case             VARCHAR2(1) := 'N' ;   --Bug#5262108
9290   l_qty_to_pick               NUMBER; --Bug#5262108.
9291 BEGIN
9292   IF (l_debug = 1) THEN
9293     DEBUG('Procedure Process_Line called with parameters....', 'Process_Line');
9294     DEBUG('p_lpn_id ' || p_lpn_id, 'Process_Line');
9295     DEBUG('p_org_id ' || p_org_id, 'Process_Line');
9296     DEBUG('p_order_header_id ' || p_order_header_id, 'Process_Line');
9297     DEBUG('p_order_line_id ' || p_order_line_id, 'Process_Line');
9298     DEBUG('p_inventory_item_id ' || p_inventory_item_id, 'Process_Line');
9299     DEBUG('p_revision ' || p_revision, 'Process_Line');
9300     DEBUG('p_end_item_unit_number ' || p_end_item_unit_number, 'Process_Line');
9301     DEBUG('p_ordered_quantity ' || p_ordered_quantity, 'Process_Line');
9302     DEBUG('p_processed_quantity ' || p_processed_quantity, 'Process_Line');
9303   END IF;
9304 
9305   -- get start_index and end_index for inventory_item_id in g_lpn_contents_tab
9306   IF g_lpn_contents_lookup_tab.EXISTS(p_inventory_item_id) THEN
9307     l_start_index  := g_lpn_contents_lookup_tab(p_inventory_item_id).start_index;
9308     l_end_index    := g_lpn_contents_lookup_tab(p_inventory_item_id).end_index;
9309 
9310     -- set serial required flag
9311     IF g_lpn_contents_tab(l_start_index).serial_control_code = 6 THEN
9312       l_serial_required_flag  := 'Y';
9313 
9314       IF (l_debug = 1) THEN
9315         DEBUG('Serial control code = ' || g_lpn_contents_tab(l_start_index).serial_control_code, 'Process_Line');
9316       END IF;
9317     ELSE
9318       l_serial_required_flag  := 'N';
9319 
9320       IF (l_debug = 1) THEN
9321         DEBUG('Serial control code = ' || g_lpn_contents_tab(l_start_index).serial_control_code, 'Process_Line');
9322       END IF;
9323     END IF;
9324   ELSE
9325     IF (l_debug = 1) THEN
9326       DEBUG('Line item not found in LPN', 'Process_Line');
9327     END IF;
9328 
9329     x_return_status  := fnd_api.g_ret_sts_error;
9330     fnd_message.set_name('WMS', 'WMS_ITEM_NOT_AVAILABLE');
9331     fnd_msg_pub.ADD;
9332     RETURN;
9333   END IF;
9334 
9335   --ACTION
9336   -- validate end unit
9337   IF NOT wms_direct_ship_pvt.validate_end_unit_num(p_inventory_item_id, p_end_item_unit_number) THEN
9338     IF (l_debug = 1) THEN
9339       DEBUG('End item unit validation failed', 'Process_Line');
9340     END IF;
9341 
9342     x_return_status  := fnd_api.g_ret_sts_error;
9343     fnd_message.set_name('WMS', 'WMS_PJM_VALIDATION_FAILED');
9344     fnd_msg_pub.ADD;
9345     RETURN;
9346   END IF;
9347 
9348   -- checkk delivery for irect ship
9349   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
9350     IF (l_debug = 1) THEN
9351       DEBUG('Checl del for direct ship failed', 'Process_Line');
9352     END IF;
9353 
9354     x_return_status  := fnd_api.g_ret_sts_error;
9355     fnd_message.set_name('WMS', 'WMS_DEL_LINES_MIX');
9356     fnd_msg_pub.ADD;
9357     RETURN;
9358   END IF;
9359 
9360   -- check holds
9361   check_holds(
9362     p_order_header_id            => p_order_header_id
9363   , p_order_line_id              => p_order_line_id
9364   , x_return_status              => l_return_status
9365   , x_msg_count                  => l_msg_count
9366   , x_msg_data                   => l_msg_data
9367   );
9368 
9369   IF l_return_status = fnd_api.g_ret_sts_error THEN
9370     IF (l_debug = 1) THEN
9371       DEBUG('Check Holds Failed', 'Process_Line');
9372     END IF;
9373 
9374     x_return_status  := fnd_api.g_ret_sts_error;
9375     fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
9376     fnd_msg_pub.ADD;
9377     RETURN;
9378   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9379     IF (l_debug = 1) THEN
9380       DEBUG('Check Holds Failed', 'Process_Line');
9381     END IF;
9382 
9383     x_return_status  := fnd_api.g_ret_sts_error;
9384     fnd_message.set_name('WMS', 'WMS_HOLD_APPLIED');
9385     fnd_msg_pub.ADD;
9386     RETURN;
9387   ELSE
9388     IF (l_debug = 1) THEN
9389       DEBUG('Check Holds Succeeded', 'Process Line');
9390     END IF;
9391   END IF;
9392 
9393   -- get demand source type
9394   BEGIN
9395     SELECT order_source_id
9396       INTO l_order_source_id
9397       FROM oe_order_headers_all
9398      WHERE header_id = p_order_header_id;
9399 
9400     IF l_order_source_id = 10 THEN
9401       l_demand_source_type_id  := 8;
9402     ELSE
9403       l_demand_source_type_id  := 2;
9404     END IF;
9405 
9406     IF (l_debug = 1) THEN
9407       DEBUG('l_demand_source_type_id =' || l_demand_source_type_id, 'Process_Line');
9408     END IF;
9409   EXCEPTION
9410     WHEN NO_DATA_FOUND THEN
9411       IF (l_debug = 1) THEN
9412         DEBUG('Exception getting l_demand_source_type_id ', 'Process_Line');
9413       END IF;
9414 
9415       RAISE fnd_api.g_exc_error;
9416   END;
9417 
9418    --Bug#5262108. This to decide if it is overship or not.
9419    --If no WDD is present to be staged, the qty will be zero to
9420    --facilitate overshipping
9421    IF p_ordered_quantity  = 0 AND p_processed_quantity = 0  THEN
9422      l_overship_case := 'Y' ;
9423    END IF;
9424 
9425   -- query reservations for header_id, line_id
9426   l_demand_source_header_id                         := inv_salesorder.get_salesorder_for_oeheader(p_order_header_id);
9427   l_qry_reservation_record.demand_source_header_id  := l_demand_source_header_id;
9428   l_qry_reservation_record.demand_source_line_id    := p_order_line_id;
9429   l_qry_reservation_record.supply_source_type_id    := inv_reservation_global.g_source_type_inv;
9430 
9431   IF (l_debug = 1) THEN
9432     DEBUG('Before call to query reservation', 'Prpcess_Line');
9433   END IF;
9434 
9435   inv_reservation_pub.query_reservation(
9436     p_api_version_number         => 1.0
9437   , p_init_msg_lst               => fnd_api.g_false
9438   , x_return_status              => l_return_status
9439   , x_msg_count                  => l_msg_count
9440   , x_msg_data                   => l_msg_data
9441   , p_query_input                => l_qry_reservation_record
9442   , p_lock_records               => fnd_api.g_false
9443   , x_mtl_reservation_tbl        => l_mtl_reservation_tab_temp
9444   , x_mtl_reservation_tbl_count  => l_mtl_resv_tab_count_temp
9445   , x_error_code                 => l_error_code
9446   );
9447 
9448   IF l_return_status = fnd_api.g_ret_sts_error THEN
9449     IF (l_debug = 1) THEN
9450       DEBUG('Query reservation failed', 'Process_Line');
9451     END IF;
9452 
9453     RAISE fnd_api.g_exc_error;
9454   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9455     IF (l_debug = 1) THEN
9456       DEBUG('Query reservation failed', 'Process_Line');
9457     END IF;
9458 
9459     RAISE fnd_api.g_exc_unexpected_error;
9460   END IF;
9461 
9462   IF (l_debug = 1) THEN
9463     DEBUG('After call to query reservation', 'Prpcess_Line');
9464     DEBUG('reservation records before sub and loc filter = ' || l_mtl_resv_tab_count_temp, 'Process_Line');
9465   END IF;
9466 
9467   -- filter reservation records based on sub and locator
9468   l_mtl_reservation_tab_count                       := 0;
9469 
9470   IF l_mtl_resv_tab_count_temp > 0 THEN
9471     l_other_resv_qty  := 0;
9472 
9473     FOR rec IN 1 .. l_mtl_resv_tab_count_temp LOOP
9474       l_resv_rec  := l_mtl_reservation_tab_temp(rec);
9475 
9476       IF (
9477           (l_resv_rec.subinventory_code = g_subinventory_code
9478            OR l_resv_rec.subinventory_code IS NULL)
9479           AND(l_resv_rec.locator_id = g_locator_id
9480               OR l_resv_rec.locator_id IS NULL)
9481          ) THEN
9482         l_mtl_reservation_tab_count                         := l_mtl_reservation_tab_count + 1;
9483         l_mtl_reservation_tab(l_mtl_reservation_tab_count)  := l_mtl_reservation_tab_temp(rec);
9484       ELSE
9485         l_other_resv_qty  := l_other_resv_qty + l_resv_rec.primary_reservation_quantity;
9486       END IF;
9487     END LOOP; -- for rec
9488   END IF; --l_mtl_reservation_tab_count_temp>0
9489 
9490   IF (l_debug = 1) THEN
9491     DEBUG('reservation records after sub and loc filter = ' || l_mtl_reservation_tab_count, 'Process_Line');
9492     DEBUG('l_other_resv_qty=' || l_other_resv_qty, 'Process_Line');
9493   END IF;
9494 
9495   -- calculate quantity_reserved, quantity_transfer_resv and quantity_create_resv
9496   IF l_mtl_reservation_tab_count > 0 THEN
9497     FOR resv_rec IN 1 .. l_mtl_reservation_tab_count LOOP
9498       IF l_mtl_reservation_tab(resv_rec).lpn_id IS NOT NULL THEN   /* 3322799 */
9499         IF (l_mtl_reservation_tab(resv_rec).lpn_id = p_lpn_id) THEN
9500            l_quantity_reserved  := l_quantity_reserved + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
9501         ELSIF l_mtl_reservation_tab(resv_rec).staged_flag='Y' THEN --Bug#5262108. Added ELSE part.
9502 	   l_other_resv_qty := l_other_resv_qty + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
9503         END IF;
9504       ELSE
9505         l_quantity_to_transfer_resv  := l_quantity_to_transfer_resv + l_mtl_reservation_tab(resv_rec).primary_reservation_quantity;
9506       END IF;
9507     END LOOP; --l_mtl_reservation_tab_count
9508 
9509     IF (l_quantity_to_transfer_resv + l_quantity_reserved) < p_ordered_quantity THEN
9510       l_quantity_create_resv     := (p_ordered_quantity - l_other_resv_qty) -(l_quantity_to_transfer_resv + l_quantity_reserved);
9511       l_create_reservation_flag  := 'Y';
9512 
9513       IF  l_quantity_create_resv < 0 THEN --bug5262108. This is because more reservation becoz of overship.
9514              l_overship_case := 'Y' ;
9515       END IF;
9516 
9517       IF (l_debug = 1) THEN
9518         DEBUG('Reserved quantity is less than the requeated quantity ', 'Process_Line');
9519       END IF;
9520     ELSE
9521       l_quantity_create_resv     := 0;
9522       l_create_reservation_flag  := 'N';
9523     END IF;
9524 
9525     IF (l_debug = 1) THEN
9526       DEBUG('l_quantity_reserved : ' || l_quantity_reserved, 'Process_Line');
9527       DEBUG('l_quantity_to_transfer_resv : ' || l_quantity_to_transfer_resv, 'Process_Line');
9528       DEBUG('l_quantity_create_resv : ' || l_quantity_create_resv, 'Process_Line');
9529     END IF;
9530   ELSE -- no reservation found reservation has to be created for all the quantity
9531     IF (l_debug = 1) THEN
9532       DEBUG('No reservation record found', 'Process_Line');
9533       DEBUG('p_ordered_quantity=' || p_ordered_quantity, 'Process_Line');
9534       DEBUG('l_other_resv_qty=' || l_other_resv_qty, 'Process_Line');
9535     END IF;
9536 
9537     l_quantity_create_resv     := p_ordered_quantity - NVL(l_other_resv_qty, 0);
9538     l_create_reservation_flag  := 'Y';
9539 
9540     IF (l_debug = 1) THEN
9541       DEBUG('l_quantity_create_resv=' || l_quantity_create_resv, 'Process_Line');
9542     END IF;
9543   END IF;        --l_mtl_reservation_tab_count>0
9544           -- transfer reservations
9545 
9546   IF l_mtl_reservation_tab_count > 0 THEN
9547     FOR resv_rec IN 1 .. l_mtl_reservation_tab_count LOOP
9548       l_resv_rec  := l_mtl_reservation_tab(resv_rec);
9549 
9550       -- bug 4285681
9551       l_current_rsv_rem_qty := l_resv_rec.primary_reservation_quantity;
9552 
9553       IF l_resv_rec.lpn_id IS NULL THEN
9554         FOR lpnc_rec IN l_start_index .. l_end_index LOOP
9555           l_lpn_cont_rec  := g_lpn_contents_tab(lpnc_rec);
9556 
9557           IF (
9558               (l_lpn_cont_rec.quantity > 0)
9559               AND(l_resv_rec.revision = l_lpn_cont_rec.revision
9560                   OR l_resv_rec.revision IS NULL)
9561               AND(l_resv_rec.lot_number = l_lpn_cont_rec.lot_number
9562                   OR l_resv_rec.lot_number IS NULL)
9563               AND wms_direct_ship_pvt.validate_end_unit_num_at(lpnc_rec, p_end_item_unit_number)
9564              ) THEN
9565             -- clear quantity tree cache
9566             inv_quantity_tree_pub.clear_quantity_cache;
9567 
9568             -- query quantity tree
9569             IF (l_debug = 1) THEN
9570               DEBUG('Before query quantity tree ', 'Process_Line');
9571               DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Process_Line');
9572               DEBUG('p_order_line_id =' || p_order_line_id, 'Process_Line');
9573               DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Process_Line');
9574               DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Process_Line');
9575               DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Process_Line');
9576               DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Process_Line');
9577               DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Process_Line');
9578             END IF;
9579 
9580             inv_quantity_tree_pub.query_quantities(
9581               x_return_status              => l_return_status
9582             , x_msg_count                  => l_msg_count
9583             , x_msg_data                   => l_msg_data
9584             , x_qoh                        => l_qoh
9585             , x_rqoh                       => l_rqoh
9586             , x_qr                         => l_qr
9587             , x_qs                         => l_qs
9588             , x_att                        => l_transactable_qty
9589             , x_atr                        => l_atr
9590             , p_api_version_number         => 1.0
9591             , p_init_msg_lst               => fnd_api.g_false
9592             , p_organization_id            => p_org_id
9593             , p_inventory_item_id          => p_inventory_item_id
9594             , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode --inv_quantity_tree_pub.g_reservation_mode  Changed bug 4128854
9595             , p_is_revision_control        => l_lpn_cont_rec.revision_control
9596             , p_is_lot_control             => l_lpn_cont_rec.lot_control
9597             , p_is_serial_control          => l_lpn_cont_rec.serial_control
9598             , p_demand_source_type_id      => l_demand_source_type_id
9599             , p_demand_source_header_id    => l_demand_source_header_id
9600             , p_demand_source_line_id      => p_order_line_id
9601             , p_revision                   => l_lpn_cont_rec.revision
9602             , p_lot_number                 => l_lpn_cont_rec.lot_number
9603             , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
9604             , p_locator_id                 => l_lpn_cont_rec.locator_id
9605             , p_lpn_id                     => l_lpn_cont_rec.lpn_id
9606             );
9607 
9608             IF l_return_status = fnd_api.g_ret_sts_error THEN
9609               IF (l_debug = 1) THEN
9610                 DEBUG(
9611                      'Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
9612                   || p_order_header_id
9613                   || ' '
9614                   || p_order_line_id
9615                 , 'Process_Line'
9616                 );
9617               END IF;
9618 
9619               RAISE fnd_api.g_exc_error;
9620             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9621               IF (l_debug = 1) THEN
9622                 DEBUG(
9623                      'Validation failed for inv_quantity_tree_pub.query_quantities for combination of '
9624                   || p_order_header_id
9625                   || ' '
9626                   || p_order_line_id
9627                 , 'Process_Line'
9628                 );
9629               END IF;
9630 
9631               RAISE fnd_api.g_exc_unexpected_error;
9632             END IF;
9633 
9634             IF (l_debug = 1) THEN
9635               DEBUG('Query tree Return status is ' || l_return_status, 'Process_Line');
9636               DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Process_Line');
9637               DEBUG('After query quantity tree ', 'Process_Line');
9638             END IF;
9639 
9640             -- bug 4285681
9641             l_transactable_qty := Least(l_transactable_qty, g_lpn_contents_tab(lpnc_rec).quantity);
9642 
9643 
9644             IF l_transactable_qty > 0 THEN
9645               l_old_resv_rec.reservation_id                := l_resv_rec.reservation_id;
9646 
9647               IF (l_debug = 1) THEN
9648                 DEBUG('The value of old reservation_id is ' || l_old_resv_rec.reservation_id, 'Process_Line');
9649               END IF;
9650 
9651               l_new_resv_rec.subinventory_code             := g_subinventory_code;
9652               l_new_resv_rec.locator_id                    := g_locator_id;
9653               l_new_resv_rec.organization_id               := p_org_id;
9654               l_new_resv_rec.inventory_item_id             := p_inventory_item_id;
9655               l_new_resv_rec.lot_number                    := l_lpn_cont_rec.lot_number;
9656               l_new_resv_rec.revision                      := l_lpn_cont_rec.revision;
9657               l_new_resv_rec.demand_source_header_id       := l_demand_source_header_id;
9658               l_new_resv_rec.demand_source_line_id         := p_order_line_id;
9659               l_new_resv_rec.lpn_id                        := l_lpn_cont_rec.lpn_id;
9660               l_new_resv_rec.primary_reservation_quantity  :=
9661                                              LEAST(l_transactable_qty, l_resv_rec.primary_reservation_quantity, l_quantity_to_transfer_resv);
9662               l_new_resv_rec.reservation_quantity          :=
9663                                              LEAST(l_transactable_qty, l_resv_rec.primary_reservation_quantity, l_quantity_to_transfer_resv);
9664 
9665 	      l_new_resv_rec.staged_flag                   := 'Y' ; --Bug#5262108
9666 
9667               IF (l_debug = 1) THEN
9668                 DEBUG('Reservation transfered for quantity := ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
9669                 DEBUG('Before call to transfer reservation ', 'Process_Line');
9670               END IF;
9671 
9672               inv_reservation_pub.transfer_reservation(
9673                 p_api_version_number         => 1.0
9674               , p_init_msg_lst               => fnd_api.g_true
9675               , x_return_status              => l_return_status
9676               , x_msg_count                  => l_msg_count
9677               , x_msg_data                   => l_msg_data
9678               , p_is_transfer_supply         => fnd_api.g_true
9679               , p_original_rsv_rec           => l_old_resv_rec
9680               , p_to_rsv_rec                 => l_new_resv_rec
9681               , p_original_serial_number     => l_dummy_sn -- no serial contorl
9682               , p_to_serial_number           => l_dummy_sn -- no serial control
9683               , p_validation_flag            => fnd_api.g_true
9684               , x_to_reservation_id          => l_new_rsv_id
9685               , p_over_reservation_flag      => 3
9686               );
9687 
9688               IF l_return_status = fnd_api.g_ret_sts_error THEN
9689                 IF (l_debug = 1) THEN
9690                   DEBUG(
9691                     'Unexpected error during transfer of Reservations, Line_id ' || p_order_line_id || ' LPN_ID= ' || l_lpn_cont_rec.lpn_id
9692                   , 'Process_Line'
9693                   );
9694                 END IF;
9695 
9696                 RAISE fnd_api.g_exc_error;
9697               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9698                 IF (l_debug = 1) THEN
9699                   DEBUG(
9700                     'Unexpected error during transfer of Reservations, Line_id ' || p_order_line_id || ' LPN_ID= ' || l_lpn_cont_rec.lpn_id
9701                   , 'Process_Line'
9702                   );
9703                 END IF;
9704 
9705                 RAISE fnd_api.g_exc_unexpected_error;
9706               END IF;
9707 
9708               IF (l_debug = 1) THEN
9709                 DEBUG('Transfer reservation successful, reservation_id= ' || l_new_rsv_id, 'Process_Line');
9710                 DEBUG('After call to transfer reservation ', 'Process_Line');
9711               END IF;
9712 
9713               IF (l_debug = 1) THEN
9714                 DEBUG('l_quantity_to_transfer_resv= ' || l_quantity_to_transfer_resv, 'Process_Line');
9715                 DEBUG('l_current_rsv_rem_qty= ' || l_current_rsv_rem_qty, 'Process_Line');
9716                 DEBUG('g_lpn_contents_tab(lpnc_rec).quantity= ' || g_lpn_contents_tab(lpnc_rec).quantity, 'Process_Line');
9717                 DEBUG('l_new_resv_rec.primary_reservation_quantity= ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
9718                 DEBUG('l_processed_quantity= ' || l_processed_quantity, 'Process_Line');
9719                 DEBUG('g_total_lpn_quantity= ' || g_total_lpn_quantity, 'Process_Line');
9720               END IF;
9721 
9722               --update quantities
9723               l_quantity_to_transfer_resv := l_quantity_to_transfer_resv - l_new_resv_rec.primary_reservation_quantity;
9724 
9725               -- bug 4285681
9726               l_current_rsv_rem_qty := l_current_rsv_rem_qty - l_new_resv_rec.primary_reservation_quantity;
9727 
9728               g_lpn_contents_tab(lpnc_rec).quantity := g_lpn_contents_tab(lpnc_rec).quantity
9729                 - l_new_resv_rec.primary_reservation_quantity;
9730               g_total_lpn_quantity := g_total_lpn_quantity - l_new_resv_rec.primary_reservation_quantity;
9731               l_processed_quantity := l_processed_quantity + l_new_resv_rec.primary_reservation_quantity;
9732 
9733 
9734               IF (l_debug = 1) THEN
9735                 DEBUG('l_quantity_to_transfer_resv= ' || l_quantity_to_transfer_resv, 'Process_Line');
9736                 DEBUG('l_current_rsv_rem_qty= ' || l_current_rsv_rem_qty, 'Process_Line');
9737                 DEBUG('g_lpn_contents_tab(lpnc_rec).quantity= ' || g_lpn_contents_tab(lpnc_rec).quantity, 'Process_Line');
9738                 DEBUG('l_new_resv_rec.primary_reservation_quantity= ' || l_new_resv_rec.primary_reservation_quantity, 'Process_Line');
9739                 DEBUG('l_processed_quantity= ' || l_processed_quantity, 'Process_Line');
9740                 DEBUG('g_total_lpn_quantity= ' || g_total_lpn_quantity, 'Process_Line');
9741               END IF;
9742 
9743               IF (l_debug = 1) THEN
9744                 DEBUG('Total pocessed quantity for line:= ' || l_processed_quantity, 'Process_Line');
9745               END IF;
9746 
9747               -- bug 4285681
9748               -- if line processed then exit
9749               IF l_quantity_to_transfer_resv = 0 OR l_current_rsv_rem_qty <= 0 THEN
9750                 EXIT;
9751               END IF; --l_quantity_to_transfer_resv=0
9752             END IF; --l_transactable_qty>0
9753           END IF; -- lpn_cont_rec
9754         END LOOP; --l_start_index..l_end_index
9755       END IF; -- lpn_id is null
9756     END LOOP; --1..l_mtl_reservation_tab_count
9757   END IF;        --l_mtl_reservation_tab_count>0
9758 
9759   --Bug#5262108.Begin.
9760   --This is to handle overship case. Once the requested qty for the line is satisfied,
9761   --we will try to overship against the line.
9762   IF l_overship_case = 'Y'   THEN
9763         l_qty_to_pick := WMS_DIRECT_SHIP_PVT.Get_qty_to_pick( p_order_header_id,p_order_line_id,p_org_id);
9764 
9765       IF (l_debug = 1) THEN
9766 	    DEBUG('l_qty_to_pick '||l_qty_to_pick ,'Process_Line');
9767       END IF;
9768       --Calculate the quantity to create reservation.
9769       l_quantity_create_resv :=  least(l_qty_to_pick,g_total_lpn_quantity );
9770       IF (l_debug = 1) THEN
9771 	    DEBUG('l_quantity_create_resv '||l_quantity_create_resv ,'Process_Line');
9772       END IF;
9773   END IF;  --Bug#5262108
9774 
9775 
9776           -- update or create reservations
9777           --IF l_quantity_create_resv>0 OR l_mtl_reservation_tab_count=0 THEN
9778 
9779   IF l_quantity_create_resv > 0 THEN
9780     FOR lpnc_rec IN l_start_index .. l_end_index LOOP
9781       IF (l_debug = 1) THEN
9782         DEBUG('Quantity for create reservation= ' || l_quantity_create_resv, 'Process_Line');
9783         DEBUG('lpnc_rec=' || lpnc_rec, 'Process_Line');
9784         DEBUG('p_end_item_unit_number=' || p_end_item_unit_number, 'Process_Line');
9785       END IF;
9786 
9787       l_lpn_cont_rec  := g_lpn_contents_tab(lpnc_rec);
9788 
9789       IF (
9790           (l_lpn_cont_rec.quantity > 0)
9791           AND(p_revision = l_lpn_cont_rec.revision
9792               OR p_revision IS NULL)
9793           AND wms_direct_ship_pvt.validate_end_unit_num_at(lpnc_rec, p_end_item_unit_number)
9794          ) THEN
9795         -- clear quantity tree cache
9796         inv_quantity_tree_pub.clear_quantity_cache;
9797 
9798         -- query quantity tree
9799         IF (l_debug = 1) THEN
9800           DEBUG('Before query quantity tree ', 'Process_Line');
9801           DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Process_Line');
9802           DEBUG('p_order_line_id =' || p_order_line_id, 'Process_Line');
9803           DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Process_Line');
9804           DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Process_Line');
9805           DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Process_Line');
9806           DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Process_Line');
9807           DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Process_Line');
9808         END IF;
9809 
9810         inv_quantity_tree_pub.query_quantities(
9811           x_return_status              => l_return_status
9812         , x_msg_count                  => l_msg_count
9813         , x_msg_data                   => l_msg_data
9814         , x_qoh                        => l_qoh
9815         , x_rqoh                       => l_rqoh
9816         , x_qr                         => l_qr
9817         , x_qs                         => l_qs
9818         , x_att                        => l_transactable_qty
9819         , x_atr                        => l_atr
9820         , p_api_version_number         => 1.0
9821         , p_init_msg_lst               => fnd_api.g_false
9822         , p_organization_id            => p_org_id
9823         , p_inventory_item_id          => p_inventory_item_id
9824         , p_tree_mode                  => inv_quantity_tree_pub.g_reservation_mode
9825         , p_is_revision_control        => l_lpn_cont_rec.revision_control
9826         , p_is_lot_control             => l_lpn_cont_rec.lot_control
9827         , p_is_serial_control          => l_lpn_cont_rec.serial_control
9828         , p_demand_source_type_id      => l_demand_source_type_id
9829         , p_demand_source_header_id    => l_demand_source_header_id
9830         , p_demand_source_line_id      => p_order_line_id
9831         , p_revision                   => l_lpn_cont_rec.revision
9832         , p_lot_number                 => l_lpn_cont_rec.lot_number
9833         , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
9834         , p_locator_id                 => l_lpn_cont_rec.locator_id
9835         , p_lpn_id                     => l_lpn_cont_rec.lpn_id
9836         );
9837 
9838         IF l_return_status = fnd_api.g_ret_sts_error THEN
9839           IF (l_debug = 1) THEN
9840             DEBUG(
9841               'Validation failed for inv_quantity_tree_pub.query_quantities for combination of ' || p_order_header_id || ' '
9842               || p_order_line_id
9843             , 'Process_Line'
9844             );
9845           END IF;
9846 
9847           RAISE fnd_api.g_exc_error;
9848         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9849           IF (l_debug = 1) THEN
9850             DEBUG(
9851               'Validation failed for inv_quantity_tree_pub.query_quantities for combination of ' || p_order_header_id || ' '
9852               || p_order_line_id
9853             , 'Process_Line'
9854             );
9855           END IF;
9856 
9857           RAISE fnd_api.g_exc_unexpected_error;
9858         END IF;
9859 
9860         IF (l_debug = 1) THEN
9861           DEBUG('Query tree Return status is ' || l_return_status, 'Process_Line');
9862           DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Process_Line');
9863           DEBUG('After query quantity tree ', 'Process_Line');
9864           DEBUG('l_qty_reserved_tmp= ' || l_qty_reserved_tmp, 'Process_Line');
9865         END IF;
9866 
9867         IF l_transactable_qty > 0 THEN
9868 
9869 	 /* Commented for bug 5262108. We have calculated reservation for LPN above.
9870           -- find out if a reservation for this lpn,rev and lot exists with p_order_line_id
9871           IF l_quantity_create_resv > 0 THEN
9872             BEGIN
9873               SELECT primary_reservation_quantity
9874                    , reservation_id
9875                 INTO l_chk_resv_qty
9876                    , l_resv_id
9877                 FROM mtl_reservations
9878                WHERE demand_source_line_id = p_order_line_id
9879                  AND lpn_id = l_lpn_cont_rec.lpn_id
9880                  AND NVL(revision, '@@@') = NVL(l_lpn_cont_rec.revision, '@@@')
9881                  AND NVL(lot_number, '@@@') = NVL(l_lpn_cont_rec.lot_number, '@@@')
9882                  AND demand_source_line_detail IS NULL;
9883 
9884               IF (l_debug = 1) THEN
9885                 DEBUG('Prior reservations exists for quantity  ' || l_chk_resv_qty || ' rsv_id ' || l_resv_id, 'Process_Line');
9886               END IF;
9887 
9888               l_quantity_create_resv  := p_ordered_quantity - l_chk_resv_qty;
9889             EXCEPTION
9890               WHEN NO_DATA_FOUND THEN
9891                 l_chk_resv_qty  := 0;
9892             END;
9893           END IF; --l_quantity_create_resv
9894 
9895           IF (l_debug = 1) THEN
9896             DEBUG('l_quantity_create_resv:= ' || l_quantity_create_resv, 'Process_Line');
9897           END IF;
9898 
9899           --   IF l_chk_resv_qty >0 THEN
9900           l_reservation_record.reservation_quantity          := LEAST(l_transactable_qty, l_quantity_create_resv);
9901           l_reservation_record.primary_reservation_quantity  := LEAST(l_transactable_qty, l_quantity_create_resv);
9902 
9903           --   ELSE
9904           --      l_reservation_record.reservation_quantity := least(l_transactable_qty,p_ordered_quantity-l_qty_reserved_tmp);
9905           --      l_reservation_record.primary_reservation_quantity :=least(l_transactable_qty,p_ordered_quantity-l_qty_reserved_tmp);
9906           --   END IF;
9907           IF l_chk_resv_qty > 0 THEN -- update reservation
9908             IF (l_debug = 1) THEN
9909               DEBUG('l_chk_resv_qty >0 ', 'Process_Line');
9910             END IF;
9911 
9912             l_old_upd_resv_rec.reservation_id            := l_resv_id;
9913             l_upd_resv_rec.primary_reservation_quantity  := l_reservation_record.primary_reservation_quantity + l_chk_resv_qty;
9914             l_upd_resv_rec.reservation_quantity          := l_reservation_record.reservation_quantity + l_chk_resv_qty;
9915 
9916             IF (l_debug = 1) THEN
9917               DEBUG('The reservation_id to update = ' || l_old_upd_resv_rec.reservation_id, 'Process_Line');
9918               DEBUG('Before call to update reservation', 'Process_Line');
9919               DEBUG('Quantity to update reservation= ' || l_upd_resv_rec.reservation_quantity, 'Process_Line');
9920             END IF;
9921 
9922             inv_reservation_pub.update_reservation(
9923               p_api_version_number         => 1.0
9924             , p_init_msg_lst               => fnd_api.g_false
9925             , x_return_status              => l_return_status
9926             , x_msg_count                  => l_msg_count
9927             , x_msg_data                   => l_msg_data
9928             , p_original_rsv_rec           => l_old_upd_resv_rec
9929             , p_to_rsv_rec                 => l_upd_resv_rec
9930             , p_original_serial_number     => l_upd_dummy_sn
9931             , p_to_serial_number           => l_upd_dummy_sn
9932             , p_validation_flag            => fnd_api.g_true
9933             , p_over_reservation_flag      => 3
9934             );
9935 
9936             IF l_return_status = fnd_api.g_ret_sts_error THEN
9937               IF (l_debug = 1) THEN
9938                 DEBUG('Update reservation failed for reservation_id ' || l_reservation_id, 'Process_Line');
9939               END IF;
9940 
9941               RAISE fnd_api.g_exc_error;
9942             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9943               IF (l_debug = 1) THEN
9944                 DEBUG('Update reservation failed for reservation_id with status U' || l_reservation_id, 'Process_Line');
9945               END IF;
9946 
9947               RAISE fnd_api.g_exc_unexpected_error;
9948             END IF;
9949 
9950             IF (l_debug = 1) THEN
9951               DEBUG('Update reservation is successful ', 'Process_Line');
9952               DEBUG('After call to update reservation', 'Process_Line');
9953             END IF;
9954           ELSE -- create reservation   commented for 5262108 */
9955 
9956             l_reservation_record.reservation_quantity          := LEAST(l_transactable_qty, l_quantity_create_resv); --Bug#5262108
9957             l_reservation_record.primary_reservation_quantity  := LEAST(l_transactable_qty, l_quantity_create_resv); --Bug#5262108
9958 
9959             IF (l_debug = 1) THEN
9960               DEBUG('Quantity to create reservation= ' || l_reservation_record.primary_reservation_quantity, 'Process_Line');
9961             END IF;
9962 
9963             l_reservation_record.organization_id            := p_org_id;
9964             l_reservation_record.inventory_item_id          := p_inventory_item_id;
9965             l_reservation_record.demand_source_header_id    := l_demand_source_header_id;
9966             l_reservation_record.demand_source_line_id      := p_order_line_id;
9967             l_reservation_record.reservation_uom_id         := NULL;
9968             l_reservation_record.reservation_uom_code       := p_primary_uom_code;
9969             l_reservation_record.primary_uom_code           := p_primary_uom_code;
9970             l_reservation_record.primary_uom_id             := NULL;
9971             l_reservation_record.supply_source_type_id      := 13;
9972             l_reservation_record.demand_source_type_id      := l_demand_source_type_id;
9973             l_reservation_record.ship_ready_flag            := 2;
9974             l_reservation_record.attribute1                 := NULL;
9975             l_reservation_record.attribute2                 := NULL;
9976             l_reservation_record.attribute3                 := NULL;
9977             l_reservation_record.attribute4                 := NULL;
9978             l_reservation_record.attribute5                 := NULL;
9979             l_reservation_record.attribute6                 := NULL;
9980             l_reservation_record.attribute7                 := NULL;
9981             l_reservation_record.attribute8                 := NULL;
9982             l_reservation_record.attribute9                 := NULL;
9983             l_reservation_record.attribute10                := NULL;
9984             l_reservation_record.attribute11                := NULL;
9985             l_reservation_record.attribute12                := NULL;
9986             l_reservation_record.attribute13                := NULL;
9987             l_reservation_record.attribute14                := NULL;
9988             l_reservation_record.attribute15                := NULL;
9989             l_reservation_record.attribute_category         := NULL;
9990             l_reservation_record.lpn_id                     := l_lpn_cont_rec.lpn_id;
9991             l_reservation_record.pick_slip_number           := NULL;
9992             l_reservation_record.lot_number_id              := NULL;
9993             l_reservation_record.lot_number                 := l_lpn_cont_rec.lot_number;
9994             l_reservation_record.locator_id                 := l_lpn_cont_rec.locator_id;
9995             l_reservation_record.subinventory_id            := NULL;
9996             l_reservation_record.subinventory_code          := g_subinventory_code;
9997             l_reservation_record.revision                   := l_lpn_cont_rec.revision;
9998             l_reservation_record.supply_source_line_detail  := NULL;
9999             l_reservation_record.supply_source_name         := NULL;
10000             l_reservation_record.supply_source_line_id      := p_order_line_id;
10001             l_reservation_record.supply_source_header_id    := l_demand_source_header_id;
10002             l_reservation_record.external_source_line_id    := NULL;
10003             l_reservation_record.external_source_code       := NULL;
10004             l_reservation_record.autodetail_group_id        := NULL;
10005             l_reservation_record.demand_source_delivery     := NULL;
10006             l_reservation_record.demand_source_name         := NULL;
10007             l_reservation_record.requirement_date           := p_date_requested;
10008             l_reservation_record.staged_flag                := 'Y' ; --Bug#5262108
10009 
10010             IF (l_debug = 1) THEN
10011               DEBUG('Before call to create reservation', 'Process_Line');
10012             END IF;
10013 
10014             inv_reservation_pub.create_reservation(
10015               x_return_status              => l_return_status
10016             , x_msg_count                  => l_msg_count
10017             , x_msg_data                   => l_msg_data
10018             , x_serial_number              => l_dummy_sn
10019             , x_quantity_reserved          => l_quantity_reserved_tmp
10020             , x_reservation_id             => l_reservation_id
10021             , p_api_version_number         => 1.0
10022             , p_init_msg_lst               => fnd_api.g_false
10023             , p_rsv_rec                    => l_reservation_record
10024             , p_partial_reservation_flag   => fnd_api.g_true
10025             , p_force_reservation_flag     => fnd_api.g_true
10026             , p_serial_number              => l_dummy_sn
10027             , p_validation_flag            => fnd_api.g_true
10028             , p_over_reservation_flag      => 3
10029             );
10030 
10031             IF l_return_status = fnd_api.g_ret_sts_error THEN
10032               IF (l_debug = 1) THEN
10033                 DEBUG('Create reservation failed for lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
10034               END IF;
10035 
10036               RAISE fnd_api.g_exc_error;
10037             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10038               IF (l_debug = 1) THEN
10039                 DEBUG('Unexpected error during create of Reservations lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Process_Line');
10040               END IF;
10041 
10042               RAISE fnd_api.g_exc_unexpected_error;
10043             END IF;
10044 
10045             IF (l_debug = 1) THEN
10046 	      DEBUG('Res qty created : '|| l_reservation_record.primary_reservation_quantity , 'Process_Line');
10047               DEBUG('Create reservations is successful ' || l_reservation_id, 'Process_Line');
10048               DEBUG('After call to create reservation', 'Process_Line');
10049             END IF;
10050         --  END IF;        --l_chk_resv_qty >0 coomented for bug#5262108.
10051                   -- update quantities
10052 
10053           g_lpn_contents_tab(lpnc_rec).quantity              :=
10054                                                     g_lpn_contents_tab(lpnc_rec).quantity
10055                                                     - l_reservation_record.primary_reservation_quantity;
10056           l_processed_quantity                               := l_processed_quantity + l_reservation_record.primary_reservation_quantity;
10057           g_total_lpn_quantity                               := g_total_lpn_quantity - l_reservation_record.primary_reservation_quantity;
10058 
10059           IF l_create_reservation_flag = 'Y' THEN
10060             l_quantity_create_resv  := l_quantity_create_resv - l_reservation_record.primary_reservation_quantity;
10061           ELSE
10062             l_qty_reserved_tmp  := l_qty_reserved_tmp + l_reservation_record.primary_reservation_quantity;
10063           END IF;
10064 
10065           IF l_quantity_create_resv = 0 THEN
10066             EXIT;
10067           ELSIF(p_ordered_quantity - l_qty_reserved_tmp) = 0 THEN
10068             EXIT;
10069           END IF;
10070         END IF; --l_transactable_qty > 0
10071       END IF; -- l_lpn_cont_rec
10072 
10073       IF (l_debug = 1) THEN
10074         DEBUG('g_lpn_contents_tab(' || lpnc_rec || ').quantity=' || g_lpn_contents_tab(lpnc_rec).quantity, 'Process_Line');
10075       END IF;
10076     END LOOP; --lpnc_rec
10077   END IF; --l_mtl_reservation_tab_count=0
10078 
10079   x_remaining_quantity                              := g_total_lpn_quantity;
10080 
10081   IF (l_debug = 1) THEN
10082     DEBUG('x_remaining_quantity=' || x_remaining_quantity, 'Process_Line');
10083   END IF;
10084 
10085   -- INSER DATA INTO WDS
10086   IF l_processed_quantity > 0 THEN
10087     IF (l_debug = 1) THEN
10088       DEBUG('Inserting data into WDS', 'Process_Line');
10089     END IF;
10090 
10091     -- if record exist then update
10092     BEGIN
10093       SELECT order_line_id
10094         INTO l_temp_var
10095         FROM wms_direct_ship_temp
10096        WHERE organization_id = p_org_id
10097          AND dock_door_id = p_dock_door_id
10098          AND lpn_id = p_lpn_id
10099          AND order_header_id = p_order_header_id
10100          AND order_line_id = p_order_line_id;
10101 
10102       -- no excaption means record exist
10103       -- update
10104       UPDATE wms_direct_ship_temp
10105          SET processed_flag = 'Y'
10106            , processed_quantity = l_processed_quantity
10107        WHERE organization_id = p_org_id
10108          AND dock_door_id = p_dock_door_id
10109          AND lpn_id = p_lpn_id
10110          AND order_header_id = p_order_header_id
10111          AND order_line_id = p_order_line_id;
10112     EXCEPTION
10113       WHEN NO_DATA_FOUND THEN
10114         -- first time line is being processed
10115         -- insert
10116         IF (l_serial_required_flag = 'Y') THEN
10117           BEGIN
10118             SELECT mtl_material_transactions_s.NEXTVAL
10119               INTO l_trx_temp_id
10120               FROM DUAL;
10121           EXCEPTION
10122             WHEN NO_DATA_FOUND THEN
10123               IF (l_debug = 1) THEN
10124                 DEBUG('Error getting transaction_temp_id', 'Process_Line');
10125               END IF;
10126 
10127               x_return_status  := fnd_api.g_ret_sts_error;
10128               RAISE fnd_api.g_exc_error;
10129           END;
10130         ELSE
10131           l_trx_temp_id  := NULL;
10132         END IF;
10133 
10134         INSERT INTO wms_direct_ship_temp
10135                     (
10136                      GROUP_ID
10137                    , organization_id
10138                    , dock_door_id
10139                    , lpn_id
10140                    , order_header_id
10141                    , order_line_id
10142                    , line_item_id
10143                    , processed_quantity
10144                    , processed_flag
10145                    , serial_required_flag
10146                    , transaction_temp_id
10147                    , creation_date
10148                    , created_by
10149                    , last_update_date
10150                    , last_updated_by
10151                    , last_update_login
10152                     )
10153              VALUES (
10154                      g_group_id
10155                    , p_org_id
10156                    , p_dock_door_id
10157                    , p_lpn_id
10158                    , p_order_header_id
10159                    , p_order_line_id
10160                    , p_inventory_item_id
10161                    , l_processed_quantity
10162                    , 'Y'
10163                    , l_serial_required_flag
10164                    , l_trx_temp_id
10165                    , g_creation_date
10166                    , g_created_by
10167                    , g_last_update_date
10168                    , g_last_updated_by
10169                    , g_last_update_login
10170                     );
10171     END; -- update / insert
10172 
10173   /* Bug#5262108. We do not need the following code because we are now creating
10174  	      reservation as soon as the user selects the order line.So commenting the below code.
10175 
10176   ELSE
10177    IF l_mtl_resv_tab_count_temp >0 then  Added bug4128854 for overshipping staged lines
10178 
10179     Bug#4349836.Before inserting a dummy record,check if one is already there for the same LPN
10180     SELECT count(1) INTO l_count_wdst
10181     FROM wms_direct_ship_temp wdst
10182     WHERE wdst.group_id = g_group_id
10183       AND wdst.organization_id=p_org_id
10184       AND wdst.dock_door_id = p_dock_door_id
10185       AND wdst.lpn_id=p_lpn_id
10186       AND wdst.order_header_id = p_order_header_id
10187       AND wdst.order_line_id = p_order_line_id
10188       AND wdst.line_item_id = p_inventory_item_id;
10189    IF (l_count_wdst = 0 ) then  --Bug#4546137 Added IF Block.
10190 
10191        IF (l_debug = 1) THEN
10192           DEBUG('Processed quantity for line is zero. Record with zero inserted into wds', 'Process_Line');
10193        END IF;
10194             INSERT INTO wms_direct_ship_temp
10195                     (
10196                      GROUP_ID
10197                    , organization_id
10198                    , dock_door_id
10199                    , lpn_id
10200                    , order_header_id
10201                    , order_line_id
10202                    , line_item_id
10203                    , processed_quantity
10204                    , processed_flag
10205                    , serial_required_flag
10206                    , transaction_temp_id
10207                    , creation_date
10208                    , created_by
10209                    , last_update_date
10210                    , last_updated_by
10211                    , last_update_login
10212                     )
10213              VALUES (
10214                      g_group_id
10215                    , p_org_id
10216                    , p_dock_door_id
10217                    , p_lpn_id
10218                    , p_order_header_id
10219                    , p_order_line_id
10220                    , p_inventory_item_id
10221                    , l_processed_quantity
10222                    , 'Y'
10223                    , l_serial_required_flag
10224                    , decode(l_serial_required_flag,'Y',mtl_material_transactions_s.NEXTVAL,l_trx_temp_id)
10225                    , g_creation_date
10226                    , g_created_by
10227                    , g_last_update_date
10228                    , g_last_updated_by
10229                    , g_last_update_login
10230                     );
10231         fnd_msg_pub.Initialize;
10232       END IF;
10233     else
10234         fnd_message.set_name('WMS', 'WMS_ITEM_NOT_AVAILABLE');
10235         fnd_msg_pub.ADD;
10236     IF (l_debug = 1) THEN
10237             DEBUG('Processed quantity for line is zero. Record not inserted into wds', 'Process_Line');
10238         END IF;
10239     end if;
10240     Bug #5262108 . Commented upto here */
10241   END IF; --l_processed_quantity
10242 
10243   x_return_status                                   := fnd_api.g_ret_sts_success;
10244 EXCEPTION
10245   WHEN OTHERS THEN
10246     IF (l_debug = 1) THEN
10247       DEBUG('Unexpected error occured', 'Process_Line');
10248     END IF;
10249 
10250     x_return_status  := fnd_api.g_ret_sts_unexp_error;
10251     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LINE');
10252     fnd_msg_pub.ADD;
10253 END process_line;
10254 
10255 /* This functions validates the line being packect into LPN for
10256    Delivery grouping rules.
10257  */
10258 
10259 FUNCTION validate_del_grp_rules(p_line_processed IN NUMBER, p_header_id IN NUMBER, p_line_id IN NUMBER)
10260   RETURN BOOLEAN IS
10261   CURSOR del_detail IS
10262     SELECT ship_from_location_id
10263          , ship_to_location_id
10264          , customer_id
10265          , intmed_ship_to_location_id
10266          , ship_method_code
10267          , fob_code
10268          , freight_terms_code
10269       FROM wsh_delivery_details_ob_grp_v
10270      WHERE source_header_id = p_header_id
10271        AND source_line_id = p_line_id;
10272 
10273   l_del_detail_rec del_detail%ROWTYPE;
10274   l_count          NUMBER               := 0;
10275   l_debug          NUMBER               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10276 BEGIN
10277   IF (l_debug = 1) THEN
10278     DEBUG('p_line_processed= ' || p_line_processed, 'Validate_Del_Grp_Rules');
10279     DEBUG('p_header_id= ' || p_header_id, 'Validate_Del_Grp_Rules');
10280     DEBUG('p_line_id= ' || p_line_id, 'Validate_Del_Grp_Rules');
10281   END IF;
10282 
10283   OPEN del_detail;
10284 
10285   LOOP
10286     FETCH del_detail INTO l_del_detail_rec;
10287     EXIT WHEN del_detail%NOTFOUND;
10288     -- populate delivery grouping fld values into temp table
10289     g_del_grp_rls_fld_temp(1)  := l_del_detail_rec.ship_from_location_id;
10290     g_del_grp_rls_fld_temp(2)  := l_del_detail_rec.ship_to_location_id;
10291     g_del_grp_rls_fld_temp(3)  := l_del_detail_rec.customer_id;
10292     g_del_grp_rls_fld_temp(4)  := l_del_detail_rec.fob_code;
10293     g_del_grp_rls_fld_temp(5)  := l_del_detail_rec.freight_terms_code;
10294     g_del_grp_rls_fld_temp(6)  := l_del_detail_rec.intmed_ship_to_location_id;
10295     g_del_grp_rls_fld_temp(7)  := l_del_detail_rec.ship_method_code;
10296 
10297     IF (l_debug = 1) THEN
10298       DEBUG('g_del_grp_rls_fld_temp populated', 'Validate_Del_Grp_Rules');
10299     END IF;
10300 
10301     l_count                    := l_count + 1;
10302     EXIT; -- only one records values are enough
10303   END LOOP;
10304 
10305   IF l_count = 0 THEN
10306     IF (l_debug = 1) THEN
10307       DEBUG('Record not found in wdd for line_id=' || p_line_id, 'Validate_Del_Grp_Rules');
10308     END IF;
10309 
10310     RAISE fnd_api.g_exc_error;
10311   END IF;
10312 
10313   IF p_line_processed = 0 THEN
10314     -- loading first line no need to do comparision
10315     -- just populate g_del_grp_rls_fld_value from
10316     -- g_del_grp_rls_fld_temp
10317     g_del_grp_rls_fld_value(1)  := g_del_grp_rls_fld_temp(1);
10318     g_del_grp_rls_fld_value(2)  := g_del_grp_rls_fld_temp(2);
10319     g_del_grp_rls_fld_value(3)  := g_del_grp_rls_fld_temp(3);
10320     g_del_grp_rls_fld_value(4)  := g_del_grp_rls_fld_temp(4);
10321     g_del_grp_rls_fld_value(5)  := g_del_grp_rls_fld_temp(5);
10322     g_del_grp_rls_fld_value(6)  := g_del_grp_rls_fld_temp(6);
10323     g_del_grp_rls_fld_value(7)  := g_del_grp_rls_fld_temp(7);
10324 
10325     IF (l_debug = 1) THEN
10326       DEBUG('SUCCESS', 'Validate_Del_Grp_Rules');
10327     END IF;
10328 
10329     RETURN TRUE;
10330   ELSE
10331     -- compare values from first line to current line
10332     FOR l_flag IN 1 .. 7 LOOP
10333       IF g_del_grp_rls_flags(l_flag) = 'Y' THEN
10334         -- comparision needed
10335         IF g_del_grp_rls_fld_value(l_flag) <> g_del_grp_rls_fld_temp(l_flag) THEN
10336           IF (l_debug = 1) THEN
10337             DEBUG('g_del_grp_rls_fld_value(l_flag)=' || g_del_grp_rls_fld_value(l_flag), 'Validate_Del_Grp_Rules');
10338             DEBUG('g_del_grp_rls_fld_temp(l_flag)=' || g_del_grp_rls_fld_temp(l_flag), 'Validate_Del_Grp_Rules');
10339             DEBUG('FAILURE', 'Validate_Del_Grp_Rules');
10340           END IF;
10341 
10342           RETURN FALSE;
10343         END IF;
10344       END IF;
10345     END LOOP;
10346 
10347     IF (l_debug = 1) THEN
10348       DEBUG('SUCCESS', 'Validate_Del_Grp_Rules');
10349     END IF;
10350 
10351     RETURN TRUE;
10352   END IF;
10353 EXCEPTION
10354   WHEN OTHERS THEN
10355     IF (l_debug = 1) THEN
10356       DEBUG('Exception occured', 'Validate_Del_Grp_Rules');
10357     END IF;
10358 
10359     RAISE fnd_api.g_exc_error;
10360 END validate_del_grp_rules;
10361 
10362 /*
10363   This method checks that there should be no record for this lpn in wstt (delivery)
10364   having direct_ship_flag N or loaded by some other method than direct ship.
10365  */
10366 
10367 FUNCTION validate_del_for_ds(p_lpn_id IN NUMBER, p_org_id IN NUMBER, p_dock_door_id IN NUMBER, p_header_id IN NUMBER, p_line_id IN NUMBER)
10368   RETURN BOOLEAN IS
10369   CURSOR del_cur IS
10370     SELECT wda.delivery_id
10371       FROM wsh_delivery_assignments_v wda, wsh_delivery_details_ob_grp_v wdd, wms_direct_ship_temp wds
10372      WHERE wds.organization_id = p_org_id
10373        AND wds.dock_door_id = p_dock_door_id
10374        AND wds.lpn_id = p_lpn_id
10375        AND wds.order_header_id = p_header_id
10376        AND wds.order_line_id = p_line_id
10377        AND wds.order_line_id = wdd.source_line_id
10378        AND wds.order_header_id = wdd.source_header_id
10379        AND wdd.delivery_detail_id = wda.delivery_detail_id;
10380 
10381   l_del_cur_rec   del_cur%ROWTYPE;
10382   l_return_status VARCHAR2(3);
10383   l_msg_count     NUMBER;
10384   l_msg_data      VARCHAR(20000);
10385   l_result        BOOLEAN           := TRUE;
10386   l_debug         NUMBER            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10387 BEGIN
10388   IF (l_debug = 1) THEN
10389     DEBUG('Checking delivery for direct ship', 'Validate_Del_For_DS');
10390     DEBUG('delivery id:'||l_del_cur_rec.delivery_id, 'Validate_Del_For_DS');
10391   END IF;
10392 
10393   OPEN del_cur;
10394 
10395   LOOP
10396     FETCH del_cur INTO l_del_cur_rec;
10397     EXIT WHEN del_cur%NOTFOUND;
10398 
10399     -- first checjk cache table
10400     IF g_checked_deliveries.EXISTS(l_del_cur_rec.delivery_id) THEN
10401       l_result  := TRUE;
10402     ELSE -- not already validated go throug normal procedure
10403       wms_direct_ship_pvt.chk_del_for_direct_ship(
10404         x_return_status              => l_return_status
10405       , x_msg_count                  => l_msg_count
10406       , x_msg_data                   => l_msg_data
10407       , p_delivery_id                => l_del_cur_rec.delivery_id
10408       );
10409 
10410       IF l_return_status = fnd_api.g_ret_sts_error THEN
10411         -- ACTION
10412         IF (l_debug = 1) THEN
10413           DEBUG('Delivery grouping failed', 'Validate_Del_For_DS');
10414         END IF;
10415 
10416         l_result  := FALSE;
10417       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10418         IF (l_debug = 1) THEN
10419           DEBUG('Delivery grouping failed with exception', 'Validate_Del_For_DS');
10420         END IF;
10421 
10422         l_result  := FALSE;
10423       END IF;
10424 
10425       -- success
10426       -- put delivery into cache table
10427       IF (l_del_cur_rec.delivery_id IS NOT NULL) THEN --Bug#6071394
10428        g_checked_deliveries(l_del_cur_rec.delivery_id)  := 1;
10429       END IF;
10430     END IF;
10431   END LOOP;
10432 
10433   CLOSE del_cur;
10434   --      debug('Delivery check result= '||l_result,'Validate_Del_For_DS');
10435   RETURN l_result;
10436 EXCEPTION
10437   WHEN OTHERS THEN
10438     IF (l_debug = 1) THEN
10439       DEBUG('Exception occured', 'Validate_Del_For_DS');
10440     END IF;
10441 
10442     RAISE fnd_api.g_exc_error;
10443 END validate_del_for_ds;
10444 
10445 /*
10446   This procedure perform the following processing for a lpn.
10447   1. Update staged flag of all reservations for all the lines packed into LPN.
10448   2. Stage LPN
10449   3. Update Freight Cost for LPN
10450  */
10451 
10452  /* bug#2798970 */
10453  /*
10454   jaysingh : The initail code of this procedure was doing wrong reservation staging.
10455              If two different LPNs was reserved against one line and we try to ship
10456        one LPN through DS, reservations for both the LPNs was staged. We should
10457        stage reservations only for LPNs which are inner LPNs of the outer LPN
10458        and the outermost LPN itself.
10459 */
10460 
10461    PROCEDURE load_lpn(
10462 		      x_return_status OUT NOCOPY    VARCHAR2
10463 		      , x_msg_count     OUT NOCOPY    NUMBER
10464 		      , x_msg_data      OUT NOCOPY    VARCHAR2
10465 		      , p_lpn_id        IN            NUMBER
10466 		      , p_org_id        IN            NUMBER
10467 		      , p_dock_door_id  IN            NUMBER
10468 		      ) IS
10469  --
10470   TYPE number_table IS TABLE OF NUMBER
10471     INDEX BY BINARY_INTEGER;
10472 
10473   --
10474   CURSOR wds_cur IS
10475     SELECT wds.order_header_id
10476          , wds.order_line_id
10477       FROM wms_direct_ship_temp wds
10478      WHERE wds.organization_id = p_org_id
10479        AND wds.dock_door_id = p_dock_door_id
10480        AND wds.lpn_id = p_lpn_id;
10481 
10482   --
10483   CURSOR lpn_cur IS
10484     SELECT lpn_id
10485       FROM wms_license_plate_numbers
10486      WHERE outermost_lpn_id = p_lpn_id
10487        AND organization_id = p_org_id;
10488 
10489   -- query reservations
10490   l_qry_reservation_record    inv_reservation_global.mtl_reservation_rec_type;
10491   l_mtl_reservation_tab       inv_reservation_global.mtl_reservation_tbl_type;
10492   l_mtl_reservation_tab_count NUMBER;
10493   l_lpn_ids_tab               number_table;
10494   l_return_status             VARCHAR2(3);
10495   l_msg_count                 NUMBER;
10496   l_msg_data                  VARCHAR2(20000);
10497   l_error_code                NUMBER;
10498   l_debug                     NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10499 BEGIN
10500   IF (l_debug = 1) THEN
10501     DEBUG('Procedure Load_LPN called with parameters', 'Load_LPN');
10502     DEBUG('p_org_id=' || p_org_id, 'Load_LPN');
10503     DEBUG('p_dock_door_id=' || p_dock_door_id, 'Load_LPN');
10504     DEBUG('p_lpn_id=' || p_lpn_id, 'Load_LPN');
10505   END IF;
10506 
10507   -- stage reservations
10508   IF (l_debug = 1) THEN
10509     DEBUG('Staging reservations', 'Load_LPN');
10510   END IF;
10511 
10512   -- get all lpn ids.
10513   IF l_lpn_ids_tab.COUNT > 0 THEN -- clear the cache
10514     l_lpn_ids_tab.DELETE;
10515   END IF;
10516 
10517   FOR l_lpn_cur IN lpn_cur LOOP
10518     l_lpn_ids_tab(l_lpn_cur.lpn_id)  := 0;
10519   END LOOP;
10520 
10521   -- for each line in WDS from p_lpn_id
10522   FOR l_wds_cur IN wds_cur LOOP
10523     l_qry_reservation_record.demand_source_line_id    := l_wds_cur.order_line_id;
10524     l_qry_reservation_record.demand_source_header_id  := inv_salesorder.get_salesorder_for_oeheader(l_wds_cur.order_header_id);
10525     l_qry_reservation_record.supply_source_type_id    := inv_reservation_global.g_source_type_inv;
10526 
10527     IF (l_debug = 1) THEN
10528       DEBUG('Before call to query reservation', 'Load_LPN');
10529     END IF;
10530 
10531     inv_reservation_pub.query_reservation(
10532       p_api_version_number         => 1.0
10533     , p_init_msg_lst               => fnd_api.g_false
10534     , x_return_status              => l_return_status
10535     , x_msg_count                  => l_msg_count
10536     , x_msg_data                   => l_msg_data
10537     , p_query_input                => l_qry_reservation_record
10538     , p_lock_records               => fnd_api.g_false
10539     , x_mtl_reservation_tbl        => l_mtl_reservation_tab
10540     , x_mtl_reservation_tbl_count  => l_mtl_reservation_tab_count
10541     , x_error_code                 => l_error_code
10542     );
10543 
10544     IF l_return_status = fnd_api.g_ret_sts_error THEN
10545       IF (l_debug = 1) THEN
10546         DEBUG('Query reservation failed', 'Load_LPN');
10547       END IF;
10548 
10549       RAISE fnd_api.g_exc_error;
10550     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10551       IF (l_debug = 1) THEN
10552         DEBUG('Query reservation failed', 'Load_LPN');
10553       END IF;
10554 
10555       RAISE fnd_api.g_exc_unexpected_error;
10556     END IF;
10557 
10558     IF (l_debug = 1) THEN
10559       DEBUG('After call to query reservation', 'Load_LPN');
10560     END IF;
10561 
10562     -- now stage reservations which has lpn_id present in l_lpn_ids_tab.
10563     FOR l_rec IN 1 .. l_mtl_reservation_tab_count LOOP
10564       IF l_lpn_ids_tab.EXISTS(l_mtl_reservation_tab(l_rec).lpn_id) THEN
10565         -- everything fine stage the reservation
10566         inv_staged_reservation_util.update_staged_flag(
10567           x_return_status              => l_return_status
10568         , x_msg_count                  => l_msg_count
10569         , x_msg_data                   => l_msg_data
10570         , p_reservation_id             => l_mtl_reservation_tab(l_rec).reservation_id
10571         , p_staged_flag                => 'Y'
10572         );
10573 
10574         IF l_return_status = fnd_api.g_ret_sts_error THEN
10575           IF (l_debug = 1) THEN
10576             DEBUG('Update_staged_flag failed with error', 'Load_LPN');
10577           END IF;
10578 
10579           RAISE fnd_api.g_exc_error;
10580         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10581           IF (l_debug = 1) THEN
10582             DEBUG('Update_staged_flag failed with exception', 'Load_LPN');
10583           END IF;
10584 
10585           RAISE fnd_api.g_exc_unexpected_error;
10586         END IF;
10587 
10588         IF (l_debug = 1) THEN
10589           DEBUG(
10590                'Reservation staged for resv_id='
10591             || l_mtl_reservation_tab(l_rec).reservation_id
10592             || ' : lpn_id='
10593             || l_mtl_reservation_tab(l_rec).lpn_id
10594             || ' : line_id='
10595             || l_mtl_reservation_tab(l_rec).demand_source_line_id
10596           , 'Load_LPN'
10597           );
10598         END IF;
10599       END IF;
10600     END LOOP; -- stage
10601   END LOOP; -- wds_cur
10602 
10603   IF (l_debug = 1) THEN
10604     DEBUG('Staging reservations completed', 'Load_LPN');
10605   END IF;
10606 
10607   -- stage lpns
10608   IF (l_debug = 1) THEN
10609     DEBUG('Staging LPNs ', 'Load_LPN');
10610   END IF;
10611 
10612   wms_direct_ship_pvt.stage_lpns(
10613     x_return_status              => l_return_status
10614   , x_msg_count                  => l_msg_count
10615   , x_msg_data                   => l_msg_data
10616   , p_group_id                   => g_group_id
10617   , p_organization_id            => p_org_id
10618   , p_dock_door_id               => p_dock_door_id
10619   );
10620 
10621   IF l_return_status = fnd_api.g_ret_sts_error THEN
10622      x_msg_data := l_msg_data;
10623      IF (l_debug = 1) THEN
10624 	DEBUG('Stage LPN API failed with status E ', 'Load_LPN');
10625      END IF;
10626 
10627      RAISE fnd_api.g_exc_error;
10628    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10629      x_msg_data := l_msg_data;
10630      IF (l_debug = 1) THEN
10631 	DEBUG('Stage LPN API failed with status U', 'Load_LPN');
10632      END IF;
10633 
10634      RAISE fnd_api.g_exc_unexpected_error;
10635   END IF;
10636 
10637   IF (l_debug = 1) THEN
10638     DEBUG('Staging LPNs completed', 'Load_LPN');
10639   END IF;
10640 
10641   -- update freight cost
10642   IF (l_debug = 1) THEN
10643     DEBUG('Updating freight cost', 'Load_LPN');
10644   END IF;
10645 
10646   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
10647   , p_lpn_id                     => p_lpn_id);
10648 
10649   IF l_return_status = fnd_api.g_ret_sts_error THEN
10650     IF (l_debug = 1) THEN
10651       DEBUG('Update Freight Cost API failed with status E ', 'Load_LPN');
10652     END IF;
10653 
10654     RAISE fnd_api.g_exc_error;
10655   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10656     IF (l_debug = 1) THEN
10657       DEBUG('Update Freight Cost failed with status U', 'Load_LPN');
10658     END IF;
10659 
10660     RAISE fnd_api.g_exc_unexpected_error;
10661   END IF;
10662 
10663   IF (l_debug = 1) THEN
10664     DEBUG('Update freight cost completed', 'Load_LPN');
10665     DEBUG('LPN loaded successfully', 'Load_LPN');
10666   END IF;
10667 
10668   x_return_status  := fnd_api.g_ret_sts_success;
10669 EXCEPTION
10670   WHEN OTHERS THEN
10671     IF (l_debug = 1) THEN
10672       DEBUG('Exception loading lpn', 'Load_LPN');
10673     END IF;
10674 
10675     x_return_status  := fnd_api.g_ret_sts_unexp_error;
10676     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
10677     fnd_msg_pub.ADD;
10678 END load_lpn;
10679 
10680 /*
10681   This procedure distributes the un-used quantity in the lpn among all the loaded lines.
10682   First it checks if for a lpn_content record a exact matching reservation is found the
10683   update the existing reservation else create new reservation.
10684  */
10685 
10686 PROCEDURE perform_overship_distribution(
10687   p_lpn_id        IN            NUMBER
10688 , p_org_id        IN            NUMBER
10689 , p_dock_door_id  IN            NUMBER
10690 , x_return_status OUT NOCOPY    VARCHAR2
10691 , x_msg_count     OUT NOCOPY    NUMBER
10692 , x_msg_data      OUT NOCOPY    VARCHAR2
10693 ) IS
10694   CURSOR loaded_lines(p_item_id IN NUMBER) IS
10695     SELECT wds.order_header_id
10696          , wds.order_line_id
10697          , oel.item_revision revision
10698          , oel.end_item_unit_number
10699          , oel.request_date
10700          , msi.primary_uom_code
10701       FROM wms_direct_ship_temp wds, oe_order_lines_all oel, mtl_system_items_kfv msi
10702      WHERE wds.organization_id = p_org_id
10703        AND wds.GROUP_ID = g_group_id
10704        AND wds.dock_door_id = p_dock_door_id
10705        AND wds.lpn_id = p_lpn_id
10706        AND wds.line_item_id = p_item_id
10707        AND oel.header_id = wds.order_header_id
10708        AND oel.line_id = wds.order_line_id
10709        AND msi.organization_id = wds.organization_id
10710        AND msi.inventory_item_id = wds.line_item_id;
10711 
10712   -- bug#2830138
10713   TYPE lpn_cont_qty_used IS TABLE OF NUMBER
10714     INDEX BY BINARY_INTEGER;
10715 
10716   l_lpn_cont_qty_used       lpn_cont_qty_used;
10717   l_old_total_lpn_qty       NUMBER                                          := g_total_lpn_quantity;
10718   l_loaded_lines_rec        loaded_lines%ROWTYPE;
10719   l_lpn_cont_rec            lpn_content_rec;
10720   -- for query reservation
10721   l_qry_reservation_record  inv_reservation_global.mtl_reservation_rec_type;
10722   l_mtl_reservation_tab     inv_reservation_global.mtl_reservation_tbl_type;
10723   l_mtl_resv_tab_count      NUMBER;
10724   -- general
10725   l_order_line_id           NUMBER;
10726   l_order_header_id         NUMBER;
10727 
10728    /* Commented for bug 5262108
10729   -- to query tolerance
10730   l_minmaxinrectype         wsh_integration.minmaxinrectype;
10731   l_minmaxoutrectype        wsh_integration.minmaxoutrectype;
10732   l_minmaxinoutrectype      wsh_integration.minmaxinoutrectype; */
10733   l_max_shippable_quantity  NUMBER;
10734   l_total_resvd_qty         NUMBER;
10735   -- For create_reservation
10736   l_reservation_record      inv_reservation_global.mtl_reservation_rec_type;
10737   l_quantity_reserved_tmp   NUMBER;
10738   l_reservation_id          NUMBER;
10739   l_dummy_sn                inv_reservation_global.serial_number_tbl_type;
10740   l_demand_source_header_id NUMBER;
10741   l_demand_source_type_id   NUMBER;
10742   l_order_source_id         NUMBER;
10743   -- For Update reservation
10744   l_upd_resv_rec            inv_reservation_global.mtl_reservation_rec_type;
10745   l_old_upd_resv_rec        inv_reservation_global.mtl_reservation_rec_type;
10746   l_upd_dummy_sn            inv_reservation_global.serial_number_tbl_type;
10747   l_chk_resv_qty            NUMBER;
10748   l_resv_id                 NUMBER;
10749   l_qty_updt_resv           NUMBER;
10750   l_qty_overship            NUMBER;
10751   -- For quantity tree declaration
10752   l_transactable_qty        NUMBER;
10753   l_qoh                     NUMBER;
10754   l_rqoh                    NUMBER;
10755   l_qr                      NUMBER;
10756   l_qs                      NUMBER;
10757   l_atr                     NUMBER;
10758   -- for pjm validation
10759   l_end_item_unit_number    VARCHAR2(30);
10760   l_return_status           VARCHAR2(10);
10761   l_msg_count               NUMBER;
10762   l_msg_data                VARCHAR2(20000);
10763   l_error_code              NUMBER;
10764   l_temp_count              NUMBER;
10765   l_debug                   NUMBER                                          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
10766    --Added for bug 5262108
10767   l_allowed_flag            VARCHAR2(1);
10768   l_max_quantity_allowed    NUMBER;
10769   l_avail_req_quantity      NUMBER;
10770   l_staged_qty              NUMBER := 0 ;  -- Bug#5262108
10771 BEGIN
10772   IF (l_debug = 1) THEN
10773     DEBUG('p_lpn_id=' || p_lpn_id, 'Perform_Overship_Distribution');
10774     DEBUG('p_org_id=' || p_org_id, 'Perform_Overship_Distribution');
10775     DEBUG('p_dock_door_id=' || p_dock_door_id, 'Perform_Overship_Distribution');
10776     DEBUG('lpn_content_record count=' || g_lpn_contents_tab.COUNT, 'Perform_Overship_Distribution');
10777   END IF;
10778 
10779   x_return_status  := fnd_api.g_ret_sts_success;
10780 
10781   -- clear cash
10782   IF l_lpn_cont_qty_used.COUNT > 0 THEN
10783     l_lpn_cont_qty_used.DELETE;
10784   END IF;
10785 
10786   FOR l_index IN 1 .. g_lpn_contents_tab.COUNT LOOP
10787     l_lpn_cont_rec  := g_lpn_contents_tab(l_index);
10788 
10789     IF (l_debug = 1) THEN
10790       DEBUG('lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
10791     END IF;
10792 
10793     IF l_lpn_cont_rec.quantity > 0 THEN -- overship needed
10794       l_qry_reservation_record.lpn_id                 := l_lpn_cont_rec.lpn_id;
10795       l_qry_reservation_record.inventory_item_id      := l_lpn_cont_rec.inventory_item_id;
10796       l_qry_reservation_record.revision               := l_lpn_cont_rec.revision;
10797       l_qry_reservation_record.lot_number             := l_lpn_cont_rec.lot_number;
10798       l_qry_reservation_record.supply_source_type_id  := inv_reservation_global.g_source_type_inv;
10799 
10800       IF (l_debug = 1) THEN
10801         DEBUG('Before call to query reservation', 'Perform_Overship_Distribution');
10802       END IF;
10803 
10804       inv_reservation_pub.query_reservation(
10805         p_api_version_number         => 1.0
10806       , p_init_msg_lst               => fnd_api.g_false
10807       , x_return_status              => l_return_status
10808       , x_msg_count                  => l_msg_count
10809       , x_msg_data                   => l_msg_data
10810       , p_query_input                => l_qry_reservation_record
10811       , p_lock_records               => fnd_api.g_false
10812       , x_mtl_reservation_tbl        => l_mtl_reservation_tab
10813       , x_mtl_reservation_tbl_count  => l_mtl_resv_tab_count
10814       , x_error_code                 => l_error_code
10815       );
10816 
10817       IF l_return_status = fnd_api.g_ret_sts_error THEN
10818         IF (l_debug = 1) THEN
10819           DEBUG('Query reservation failed', 'Perform_Overship_Distribution');
10820         END IF;
10821 
10822         RAISE fnd_api.g_exc_error;
10823       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10824         IF (l_debug = 1) THEN
10825           DEBUG('Query reservation failed', 'Perform_Overship_Distribution');
10826         END IF;
10827 
10828         RAISE fnd_api.g_exc_unexpected_error;
10829       END IF;
10830 
10831       IF (l_debug = 1) THEN
10832         DEBUG('After call to query reservation', 'Perform_Overship_Distribution');
10833       END IF;
10834 
10835       -- if reservation exists try to overship on already reaserved line
10836       IF (l_debug = 1) THEN
10837         DEBUG('l_mtl_resv_tab_count= ' || l_mtl_resv_tab_count, 'Perform_Overship_Distribution');
10838       END IF;
10839 
10840       IF l_mtl_resv_tab_count > 0 THEN
10841         FOR l_index_r IN 1 .. l_mtl_resv_tab_count LOOP
10842           l_order_line_id                       := l_mtl_reservation_tab(l_index_r).demand_source_line_id;
10843           -- get oe_order_header_id from sales order header id
10844           inv_salesorder.get_oeheader_for_salesorder(
10845             p_salesorder_id              => l_mtl_reservation_tab(l_index_r).demand_source_header_id
10846           , x_oe_header_id               => l_order_header_id
10847           , x_return_status              => l_return_status
10848           );
10849 
10850           IF (l_debug = 1) THEN
10851             DEBUG('l_oe_order_header_id = ' || l_order_header_id, 'Perform_Overship_Distribution');
10852           END IF;
10853 
10854           l_demand_source_header_id             := l_mtl_reservation_tab(l_index_r).demand_source_header_id;
10855 
10856           -- validate end unit
10857           BEGIN
10858             SELECT end_item_unit_number
10859               INTO l_end_item_unit_number
10860               FROM oe_order_lines_all
10861              WHERE line_id = l_order_line_id;
10862 
10863             IF (l_debug = 1) THEN
10864               DEBUG('l_end_item_unit_number=' || l_end_item_unit_number, 'Perform_Overship_Distribution');
10865             END IF;
10866 
10867             IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(l_index, l_end_item_unit_number) THEN
10868               IF (l_debug = 1) THEN
10869                 DEBUG('End unit not matching, skipping line', 'Perform_Overship_Distribution');
10870               END IF;
10871 
10872               EXIT;
10873             END IF;
10874           EXCEPTION
10875             WHEN NO_DATA_FOUND THEN
10876               IF (l_debug = 1) THEN
10877                 DEBUG('No data found for line=' || l_order_line_id, 'Perform_Overship_Distribution');
10878               END IF;
10879 
10880               RAISE fnd_api.g_exc_error;
10881           END;
10882 
10883           /* Commented for bug  5262108
10884           -- get tolerance quantity
10885           l_minmaxinrectype.api_version_number  := 1.0;
10886           l_minmaxinrectype.source_code         := 'OE';
10887           l_minmaxinrectype.line_id             := l_order_line_id; */
10888 
10889           IF (l_debug = 1) THEN
10890             DEBUG('Before call to WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick', 'Perform_Overship_Distribution');
10891             DEBUG('l_order_line_id=' || l_order_line_id, 'Perform_Overship_Distribution');
10892           END IF;
10893 
10894          /* Commented for bug 5262108
10895           wsh_integration.get_min_max_tolerance_quantity(
10896             p_in_attributes              => l_minmaxinrectype
10897           , p_out_attributes             => l_minmaxoutrectype
10898           , p_inout_attributes           => l_minmaxinoutrectype
10899           , x_return_status              => l_return_status
10900           , x_msg_count                  => l_msg_count
10901           , x_msg_data                   => l_msg_data
10902           );  */
10903 
10904 
10905 	    --Begin Bug 5262108
10906 	  WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
10907                p_order_line_id         => l_order_line_id,
10908                p_quantity_to_pick      => 0,
10909                x_allowed_flag          => l_allowed_flag,
10910                x_max_quantity_allowed  => l_max_quantity_allowed,
10911                x_avail_req_quantity    => l_avail_req_quantity,
10912                x_return_status         => l_return_status) ;
10913 
10914 	 IF (l_debug = 1) THEN
10915 		DEBUG('l_allowed_flag='||l_allowed_flag,'Perform_Overship_Distribution');
10916           	DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Perform_Overship_Distribution');
10917           	DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Perform_Overship_Distribution');
10918           	DEBUG('l_return_status='||l_return_status,'Perform_Overship_Distribution');
10919          END IF;
10920 
10921 	 --End bug 5262108
10922 
10923           IF l_return_status = fnd_api.g_ret_sts_error THEN
10924             IF (l_debug = 1) THEN
10925               DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
10926             END IF;
10927 
10928             RAISE fnd_api.g_exc_error;
10929           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10930             IF (l_debug = 1) THEN
10931               DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
10932             END IF;
10933 
10934             RAISE fnd_api.g_exc_unexpected_error;
10935           END IF;
10936 
10937 
10938         -- commented for 5262108  l_max_shippable_quantity := l_minmaxoutrectype.max_remaining_quantity; --Removed trunc bug3994915
10939 
10940 	  l_max_shippable_quantity := l_max_quantity_allowed;  -- 5262108
10941 
10942           IF (l_debug = 1) THEN
10943            --DEBUG('max_remaining_quantity = ' || l_max_shippable_quantity, 'Perform_Overship_Distribution');
10944 	   DEBUG('max_remaining_quantity = ' || l_max_quantity_allowed, 'Perform_Overship_Distribution');
10945           END IF;
10946 
10947           -- get total reserved quantity for this line
10948           BEGIN
10949             SELECT SUM(primary_reservation_quantity)
10950               INTO l_total_resvd_qty
10951               FROM mtl_reservations
10952              WHERE demand_source_header_id = l_mtl_reservation_tab(l_index_r).demand_source_header_id
10953                AND demand_source_line_id = l_order_line_id
10954                AND demand_source_line_detail IS NULL
10955 	       AND nvl(staged_flag,'N') = 'Y'; --Bug#5262108
10956           EXCEPTION
10957             WHEN OTHERS THEN
10958               IF (l_debug = 1) THEN
10959                 DEBUG('Exception getting total reserved quantity for line = ' || l_order_line_id, 'Perform_Overship_Distribution');
10960               END IF;
10961 
10962               RAISE fnd_api.g_exc_unexpected_error;
10963           END;
10964 
10965           DEBUG('DRH 1 ' || l_order_header_id);
10966           --
10967           -- get demand source type
10968           BEGIN
10969             SELECT order_source_id
10970               INTO l_order_source_id
10971               FROM oe_order_headers_all
10972       --       WHERE header_id = 64801;
10973              WHERE header_id = l_order_header_id;
10974 
10975             IF l_order_source_id = 10 THEN
10976               l_demand_source_type_id  := 8;
10977             ELSE
10978               l_demand_source_type_id  := 2;
10979             END IF;
10980 
10981             IF (l_debug = 1) THEN
10982               DEBUG('l_demand_source_type_id =' || l_demand_source_type_id, 'Perform_Overship_Distribution');
10983             END IF;
10984           EXCEPTION
10985             WHEN NO_DATA_FOUND THEN
10986               IF (l_debug = 1) THEN
10987                 DEBUG('Exception getting l_demand_source_type_id ', 'Perform_Overship_Distribution');
10988               END IF;
10989 
10990               RAISE fnd_api.g_exc_unexpected_error;
10991           END;
10992 
10993           -- clear quantity tree cache
10994           inv_quantity_tree_pub.clear_quantity_cache;
10995 
10996           -- query quantity tree
10997           IF (l_debug = 1) THEN
10998             DEBUG('Before query quantity tree ', 'Perform_Overship_Distribution');
10999             DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Perform_Overship_Distribution');
11000             DEBUG('l_order_line_id =' || l_order_line_id, 'Perform_Overship_Distribution');
11001             DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Perform_Overship_Distribution');
11002             DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Perform_Overship_Distribution');
11003             DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Perform_Overship_Distribution');
11004             DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Perform_Overship_Distribution');
11005             DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
11006           END IF;
11007 
11008           inv_quantity_tree_pub.query_quantities(
11009             x_return_status              => l_return_status
11010           , x_msg_count                  => l_msg_count
11011           , x_msg_data                   => l_msg_data
11012           , x_qoh                        => l_qoh
11013           , x_rqoh                       => l_rqoh
11014           , x_qr                         => l_qr
11015           , x_qs                         => l_qs
11016           , x_att                        => l_transactable_qty
11017           , x_atr                        => l_atr
11018           , p_api_version_number         => 1.0
11019           , p_init_msg_lst               => fnd_api.g_false
11020           , p_organization_id            => p_org_id
11021           , p_inventory_item_id          => l_lpn_cont_rec.inventory_item_id
11022           , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
11023           , p_is_revision_control        => l_lpn_cont_rec.revision_control
11024           , p_is_lot_control             => l_lpn_cont_rec.lot_control
11025           , p_is_serial_control          => l_lpn_cont_rec.serial_control
11026           , p_demand_source_type_id      => l_demand_source_type_id
11027           , p_demand_source_header_id    => l_demand_source_header_id
11028           , p_demand_source_line_id      => l_order_line_id
11029           , p_revision                   => l_lpn_cont_rec.revision
11030           , p_lot_number                 => l_lpn_cont_rec.lot_number
11031           , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
11032           , p_locator_id                 => l_lpn_cont_rec.locator_id
11033           , p_lpn_id                     => l_lpn_cont_rec.lpn_id
11034           );
11035 
11036           IF l_return_status = fnd_api.g_ret_sts_error THEN
11037             IF (l_debug = 1) THEN
11038               DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
11039             END IF;
11040 
11041             RAISE fnd_api.g_exc_error;
11042           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11043             IF (l_debug = 1) THEN
11044               DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
11045             END IF;
11046 
11047             RAISE fnd_api.g_exc_unexpected_error;
11048           END IF;
11049 
11050           IF (l_debug = 1) THEN
11051             DEBUG('Query tree Return status is ' || l_return_status, 'Perform_Overship_Distribution');
11052             DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Perform_Overship_Distribution');
11053             DEBUG('After query quantity tree ', 'Perform_Overship_Distribution');
11054           END IF;
11055 
11056           IF l_transactable_qty > 0 THEN
11057             -- updating existing reservation record
11058 
11059 	    --Bug#5262108.query the staged qty.
11060  	    SELECT NVL(SUM(picked_quantity),0) INTO l_staged_qty
11061  	    FROM wsh_delivery_details
11062  	    WHERE source_header_id = l_order_header_id
11063  	    AND  source_line_id =l_order_line_id
11064  	    AND released_status='Y';
11065 
11066             --Bug#5262108.Added l_staged_qty to the following line.
11067             l_qty_overship   := LEAST(l_lpn_cont_rec.quantity,(l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty), l_transactable_qty);
11068             l_qty_updt_resv  := l_mtl_reservation_tab(l_index_r).primary_reservation_quantity + l_qty_overship;
11069 
11070             IF (l_debug = 1) THEN
11071                 DEBUG('l_staged_qty  ' || l_staged_qty , 'Perform_Overship_Distribution');
11072                 DEBUG('l_lpn_cont_rec.quantity  ' || l_lpn_cont_rec.quantity, 'Perform_Overship_Distribution');
11073                 DEBUG('l_total_resvd_qty '||l_total_resvd_qty, 'Perform_Overship_Distribution');
11074                 DEBUG('l_qty_overship '||l_qty_overship, 'Perform_Overship_Distribution');
11075                 DEBUG('l_qty_updt_resv '||l_qty_updt_resv, 'Perform_Overship_Distribution');
11076              END IF;
11077 
11078             IF l_qty_overship > 0 THEN
11079               l_old_upd_resv_rec.reservation_id            := l_mtl_reservation_tab(l_index_r).reservation_id;
11080               l_upd_resv_rec.primary_reservation_quantity  := l_qty_updt_resv;
11081               l_upd_resv_rec.reservation_quantity          := l_qty_updt_resv;
11082 
11083               IF (l_debug = 1) THEN
11084                 DEBUG('Quantity update reservation is ' || l_qty_updt_resv, 'Perform_Overship_Distribution');
11085                 DEBUG('The reservation_id to update = ' || l_old_upd_resv_rec.reservation_id, 'Perform_Overship_Distribution');
11086                 DEBUG('Before call to update reservation', 'Perform_Overship_Distribution');
11087                 DEBUG('Quantity to update reservation= ' || l_upd_resv_rec.reservation_quantity, 'Perform_Overship_Distribution');
11088               END IF;
11089 
11090               inv_reservation_pub.update_reservation(
11091                 p_api_version_number         => 1.0
11092               , p_init_msg_lst               => fnd_api.g_false
11093               , x_return_status              => l_return_status
11094               , x_msg_count                  => l_msg_count
11095               , x_msg_data                   => l_msg_data
11096               , p_original_rsv_rec           => l_old_upd_resv_rec
11097               , p_to_rsv_rec                 => l_upd_resv_rec
11098               , p_original_serial_number     => l_upd_dummy_sn
11099               , p_to_serial_number           => l_upd_dummy_sn
11100               , p_validation_flag            => fnd_api.g_true
11101               , p_over_reservation_flag      => 3
11102               );
11103 
11104               IF l_return_status = fnd_api.g_ret_sts_error THEN
11105                 IF (l_debug = 1) THEN
11106                   DEBUG('Update reservation failed ', 'Perform_Overship_Distribution');
11107                 END IF;
11108 
11109                 RAISE fnd_api.g_exc_error;
11110               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11111                 IF (l_debug = 1) THEN
11112                   DEBUG('Update reservation failed', 'Perform_Overship_Distribution');
11113                 END IF;
11114 
11115                 RAISE fnd_api.g_exc_unexpected_error;
11116               END IF;
11117 
11118               IF (l_debug = 1) THEN
11119                 DEBUG('Update reservation is successful ', 'Perform_Overship_Distribution');
11120                 DEBUG('After call to update reservation', 'Perform_Overship_Distribution');
11121               END IF;
11122 
11123               -- update cache quantities
11124               g_lpn_contents_tab(l_index).quantity         := g_lpn_contents_tab(l_index).quantity - l_qty_overship;
11125               l_lpn_cont_rec.quantity                      := l_lpn_cont_rec.quantity - l_qty_overship;
11126               g_total_lpn_quantity                         := g_total_lpn_quantity - l_qty_overship;
11127 
11128               -- bug#2830138
11129               -- make backup of quantities used
11130               -- will be used if overship has to be rollbacked
11131               IF l_lpn_cont_qty_used.EXISTS(l_index) THEN
11132                 l_lpn_cont_qty_used(l_index)  := l_lpn_cont_qty_used(l_index) + l_qty_overship;
11133               ELSE
11134                 l_lpn_cont_qty_used(l_index)  := l_qty_overship;
11135               END IF;
11136 
11137               -- update wds for processed_quantity=processed_quantity+g_total_lpn_quantity
11138               BEGIN
11139                 UPDATE wms_direct_ship_temp
11140                    SET processed_quantity =(processed_quantity + l_qty_overship)
11141                  WHERE organization_id = p_org_id
11142                    AND GROUP_ID = g_group_id
11143                    AND lpn_id = p_lpn_id
11144                    AND order_line_id = l_order_line_id;
11145               EXCEPTION
11146                 WHEN OTHERS THEN
11147                   IF (l_debug = 1) THEN
11148                     DEBUG('Exception updating WDS', 'Perform_Overship_Distribution');
11149                   END IF;
11150 
11151                   RAISE fnd_api.g_exc_unexpected_error;
11152               END;
11153             END IF;         -- l_qty_overship >0
11154                     -- if all quantity consumed exit to lpn_content loop
11155 
11156             IF l_lpn_cont_rec.quantity = 0 THEN
11157               EXIT;
11158             END IF;
11159           END IF; --l_transactable_qty>0
11160         END LOOP; --l_mtl_resv_tab_count
11161       END IF; --l_mtl_resv_tab_count
11162 
11163       IF (l_debug = 1) THEN
11164         DEBUG('Reservation update completed', 'Perform_Overship_Distribution');
11165       END IF;
11166 
11167        /* Bug#6062135. If still qty left in LPN, it may be because of multipple WLC records.
11168          We have to see if theyc an be over shipped */
11169 
11170       -- if more quantity remaining in lpn_content record the create new reservation
11171       IF l_lpn_cont_rec.quantity > 0 THEN                                -- create reservation
11172                                           -- get all lines loaded to this lpn
11173         OPEN loaded_lines(l_lpn_cont_rec.inventory_item_id);
11174 
11175         LOOP
11176           FETCH loaded_lines INTO l_loaded_lines_rec;
11177           EXIT WHEN loaded_lines%NOTFOUND;
11178 
11179           IF (l_debug = 1) THEN
11180             DEBUG('Inside create reservation part', 'Perform_Overship_Distribution');
11181           END IF;
11182 
11183           --validate end unit
11184           IF NOT wms_direct_ship_pvt.validate_end_unit_num_at(l_index, l_loaded_lines_rec.end_item_unit_number) THEN
11185             IF (l_debug = 1) THEN
11186               DEBUG('End unit not matching, skipping line', 'Perform_Overship_Distribution');
11187             END IF;
11188 
11189             EXIT;
11190           END IF;
11191 
11192           IF (l_loaded_lines_rec.revision = l_lpn_cont_rec.revision
11193               OR l_loaded_lines_rec.revision IS NULL) THEN
11194             -- get demand source header id
11195             l_demand_source_header_id             := inv_salesorder.get_salesorder_for_oeheader(l_loaded_lines_rec.order_header_id);
11196             l_order_line_id                       := l_loaded_lines_rec.order_line_id;
11197 
11198             -- get total reserved quantity for line
11199             BEGIN
11200               SELECT SUM(primary_reservation_quantity)
11201                 INTO l_total_resvd_qty
11202                 FROM mtl_reservations
11203                WHERE demand_source_header_id = l_demand_source_header_id
11204                  AND demand_source_line_id = l_order_line_id
11205                  AND demand_source_line_detail IS NULL
11206                  AND NVL(staged_flag,'N') = 'Y' ;
11207             EXCEPTION
11208               WHEN OTHERS THEN
11209                 IF (l_debug = 1) THEN
11210                   DEBUG('Exception getting total reserved quantity for line = ' || l_order_line_id, 'Perform_Overship_Distribution');
11211                 END IF;
11212 
11213                 RAISE fnd_api.g_exc_unexpected_error;
11214             END;
11215 
11216             /*Commented for bug 6071394
11217             -- get max shippble quantity
11218             l_minmaxinrectype.api_version_number  := 1.0;
11219             l_minmaxinrectype.source_code         := 'OE';
11220             l_minmaxinrectype.line_id             := l_loaded_lines_rec.order_line_id;
11221             wsh_integration.get_min_max_tolerance_quantity(
11222               p_in_attributes              => l_minmaxinrectype
11223             , p_out_attributes             => l_minmaxoutrectype
11224             , p_inout_attributes           => l_minmaxinoutrectype
11225             , x_return_status              => l_return_status
11226             , x_msg_count                  => l_msg_count
11227             , x_msg_data                   => l_msg_data
11228             );
11229             */
11230           --Begin Bug 4908571
11231 	  WSH_DETAILS_VALIDATIONS.Check_Quantity_To_Pick(
11232                p_order_line_id         => l_order_line_id,
11233                p_quantity_to_pick      => 0,
11234                x_allowed_flag          => l_allowed_flag,
11235                x_max_quantity_allowed  => l_max_quantity_allowed,
11236                x_avail_req_quantity    => l_avail_req_quantity,
11237                x_return_status         => l_return_status) ;
11238 
11239 	 IF (l_debug = 1) THEN
11240 		DEBUG('l_allowed_flag='||l_allowed_flag,'Perform_Overship_Distribution');
11241           	DEBUG('l_max_quantity_allowed='||l_max_quantity_allowed,'Perform_Overship_Distribution');
11242           	DEBUG('l_avail_req_quantity='||l_avail_req_quantity,'Perform_Overship_Distribution');
11243           	DEBUG('l_return_status='||l_return_status,'Perform_Overship_Distribution');
11244          END IF;
11245 	 --End bug 4908571
11246 
11247 
11248             IF l_return_status = fnd_api.g_ret_sts_error THEN
11249               IF (l_debug = 1) THEN
11250                 DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
11251               END IF;
11252 
11253               RAISE fnd_api.g_exc_error;
11254             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11255               IF (l_debug = 1) THEN
11256                 DEBUG('Tolerance API failed', 'Perform_Overship_Distribution');
11257               END IF;
11258 
11259               RAISE fnd_api.g_exc_unexpected_error;
11260             END IF;
11261 
11262             -- get demand source type
11263             BEGIN
11264               SELECT order_source_id
11265                 INTO l_order_source_id
11266                 FROM oe_order_headers_all
11267                WHERE header_id = l_loaded_lines_rec.order_header_id;
11268 
11269               IF l_order_source_id = 10 THEN
11270                 l_demand_source_type_id  := 8;
11271               ELSE
11272                 l_demand_source_type_id  := 2;
11273               END IF;
11274 
11275               IF (l_debug = 1) THEN
11276                 DEBUG('l_demand_source_type_id =' || l_demand_source_type_id, 'Perform_Overship_Distribution');
11277               END IF;
11278             EXCEPTION
11279               WHEN NO_DATA_FOUND THEN
11280                 IF (l_debug = 1) THEN
11281                   DEBUG('Exception getting l_demand_source_type_id ', 'Perform_Overship_Distribution');
11282                 END IF;
11283 
11284                 RAISE fnd_api.g_exc_unexpected_error;
11285             END;
11286 
11287             -- clear quantity tree cache
11288             inv_quantity_tree_pub.clear_quantity_cache;
11289 
11290             -- query quantity tree
11291             IF (l_debug = 1) THEN
11292               DEBUG('Before query quantity tree ', 'Perform_Overship_Distribution');
11293               DEBUG('l_demand_source_header_id =' || l_demand_source_header_id, 'Perform_Overship_Distribution');
11294               DEBUG('l_order_line_id =' || l_order_line_id, 'Perform_Overship_Distribution');
11295               DEBUG('l_lpn_cont_rec.revision =' || l_lpn_cont_rec.revision, 'Perform_Overship_Distribution');
11296               DEBUG('l_lpn_cont_rec.lot_number =' || l_lpn_cont_rec.lot_number, 'Perform_Overship_Distribution');
11297               DEBUG('l_lpn_cont_rec.subinventory_code =' || l_lpn_cont_rec.subinventory_code, 'Perform_Overship_Distribution');
11298               DEBUG('l_lpn_cont_rec.locator_id =' || l_lpn_cont_rec.locator_id, 'Perform_Overship_Distribution');
11299               DEBUG('l_lpn_cont_rec.lpn_id=' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
11300             END IF;
11301 
11302             inv_quantity_tree_pub.query_quantities(
11303               x_return_status              => l_return_status
11304             , x_msg_count                  => l_msg_count
11305             , x_msg_data                   => l_msg_data
11306             , x_qoh                        => l_qoh
11307             , x_rqoh                       => l_rqoh
11308             , x_qr                         => l_qr
11309             , x_qs                         => l_qs
11310             , x_att                        => l_transactable_qty
11311             , x_atr                        => l_atr
11312             , p_api_version_number         => 1.0
11313             , p_init_msg_lst               => fnd_api.g_false
11314             , p_organization_id            => p_org_id
11315             , p_inventory_item_id          => l_lpn_cont_rec.inventory_item_id
11316             , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
11317             , p_is_revision_control        => l_lpn_cont_rec.revision_control
11318             , p_is_lot_control             => l_lpn_cont_rec.lot_control
11319             , p_is_serial_control          => l_lpn_cont_rec.serial_control
11320             , p_demand_source_type_id      => l_demand_source_type_id
11321             , p_demand_source_header_id    => l_demand_source_header_id
11322             , p_demand_source_line_id      => l_order_line_id
11323             , p_revision                   => l_lpn_cont_rec.revision
11324             , p_lot_number                 => l_lpn_cont_rec.lot_number
11325             , p_subinventory_code          => l_lpn_cont_rec.subinventory_code
11326             , p_locator_id                 => l_lpn_cont_rec.locator_id
11327             , p_lpn_id                     => l_lpn_cont_rec.lpn_id
11328             );
11329 
11330             IF l_return_status = fnd_api.g_ret_sts_error THEN
11331               IF (l_debug = 1) THEN
11332                 DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
11333               END IF;
11334 
11335               RAISE fnd_api.g_exc_error;
11336             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11337               IF (l_debug = 1) THEN
11338                 DEBUG('Validation failed for inv_quantity_tree_pub.query_quantities', 'Perform_Overship_Distribution');
11339               END IF;
11340 
11341               RAISE fnd_api.g_exc_unexpected_error;
11342             END IF;
11343 
11344             IF (l_debug = 1) THEN
11345               DEBUG('Query tree Return status is ' || l_return_status, 'Perform_Overship_Distribution');
11346               DEBUG('l_transactable_qty= ' || l_transactable_qty, 'Perform_Overship_Distribution');
11347               DEBUG('After query quantity tree ', 'Perform_Overship_Distribution');
11348             END IF;
11349 
11350             IF l_transactable_qty > 0 THEN
11351               l_max_shippable_quantity  := TRUNC(l_max_quantity_allowed);
11352 
11353               IF (l_debug = 1) THEN
11354                 DEBUG('max_remaining_quantity = ' || l_max_quantity_allowed, 'Perform_Overship_Distribution');
11355               END IF;
11356 
11357              --Bug#6071394.query the staged qty.
11358              SELECT NVL(SUM(picked_quantity),0) INTO l_staged_qty
11359 	     FROM wsh_delivery_details
11360 	     WHERE source_header_id = l_order_header_id
11361 	     AND  source_line_id =l_order_line_id
11362 	     AND  released_status='Y';
11363 
11364 	      IF (l_debug = 1) THEN
11365                  DEBUG(' l_total_resvd_qty = ' ||  l_total_resvd_qty, 'Perform_Overship_Distribution');
11366 		 DEBUG(' l_staged_qty = ' ||  l_staged_qty, 'Perform_Overship_Distribution');
11367 		 DEBUG(' l_transactable_qty = ' ||  l_transactable_qty, 'Perform_Overship_Distribution');
11368               END IF;
11369 
11370 	     --Bug#6071394.Added l_staged_qty  tothe following line.
11371              l_qty_overship   := LEAST(l_lpn_cont_rec.quantity,(l_max_shippable_quantity + l_staged_qty - l_total_resvd_qty), l_transactable_qty);
11372              l_qty_updt_resv           := l_qty_overship;
11373 
11374               IF l_qty_overship > 0 THEN
11375                 -- create reservation
11376                 l_reservation_record.primary_reservation_quantity  := l_qty_overship;
11377                 l_reservation_record.reservation_quantity          := l_qty_overship;
11378 
11379                 IF (l_debug = 1) THEN
11380                   DEBUG(
11381                     'Quantity to create reservation= ' || l_reservation_record.primary_reservation_quantity
11382                   , 'Perform_Overship_Distribution'
11383                   );
11384                 END IF;
11385 
11386                 l_reservation_record.organization_id               := p_org_id;
11387                 l_reservation_record.inventory_item_id             := l_lpn_cont_rec.inventory_item_id;
11388                 l_reservation_record.demand_source_header_id       := l_demand_source_header_id;
11389                 l_reservation_record.demand_source_line_id         := l_loaded_lines_rec.order_line_id;
11390                 l_reservation_record.reservation_uom_id            := NULL;
11391                 l_reservation_record.reservation_uom_code          := l_loaded_lines_rec.primary_uom_code;
11392                 l_reservation_record.primary_uom_code              := l_loaded_lines_rec.primary_uom_code;
11393                 l_reservation_record.primary_uom_id                := NULL;
11394                 l_reservation_record.supply_source_type_id         := 13;
11395                 l_reservation_record.demand_source_type_id         := l_demand_source_type_id;
11396                 l_reservation_record.ship_ready_flag               := 2;
11397                 l_reservation_record.attribute1                    := NULL;
11398                 l_reservation_record.attribute2                    := NULL;
11399                 l_reservation_record.attribute3                    := NULL;
11400                 l_reservation_record.attribute4                    := NULL;
11401                 l_reservation_record.attribute5                    := NULL;
11402                 l_reservation_record.attribute6                    := NULL;
11403                 l_reservation_record.attribute7                    := NULL;
11404                 l_reservation_record.attribute8                    := NULL;
11405                 l_reservation_record.attribute9                    := NULL;
11406                 l_reservation_record.attribute10                   := NULL;
11407                 l_reservation_record.attribute11                   := NULL;
11408                 l_reservation_record.attribute12                   := NULL;
11409                 l_reservation_record.attribute13                   := NULL;
11410                 l_reservation_record.attribute14                   := NULL;
11411                 l_reservation_record.attribute15                   := NULL;
11412                 l_reservation_record.attribute_category            := NULL;
11413                 l_reservation_record.lpn_id                        := l_lpn_cont_rec.lpn_id;
11414                 l_reservation_record.pick_slip_number              := NULL;
11415                 l_reservation_record.lot_number_id                 := NULL;
11416                 l_reservation_record.lot_number                    := l_lpn_cont_rec.lot_number;
11417                 l_reservation_record.locator_id                    := l_lpn_cont_rec.locator_id;
11418                 l_reservation_record.subinventory_id               := NULL;
11419                 l_reservation_record.subinventory_code             := g_subinventory_code;
11420                 l_reservation_record.revision                      := l_lpn_cont_rec.revision;
11421                 l_reservation_record.supply_source_line_detail     := NULL;
11422                 l_reservation_record.supply_source_name            := NULL;
11423                 l_reservation_record.supply_source_line_id         := l_loaded_lines_rec.order_line_id;
11424                 l_reservation_record.supply_source_header_id       := l_demand_source_header_id;
11425                 l_reservation_record.external_source_line_id       := NULL;
11426                 l_reservation_record.external_source_code          := NULL;
11427                 l_reservation_record.autodetail_group_id           := NULL;
11428                 l_reservation_record.demand_source_delivery        := NULL;
11429                 l_reservation_record.demand_source_name            := NULL;
11430                 l_reservation_record.requirement_date              := l_loaded_lines_rec.request_date;
11431 
11432                 IF (l_debug = 1) THEN
11433                   DEBUG('Before call to create reservation', 'Perform_Overship_Distribution');
11434                 END IF;
11435 
11436                 inv_reservation_pub.create_reservation(
11437                   x_return_status              => l_return_status
11438                 , x_msg_count                  => l_msg_count
11439                 , x_msg_data                   => l_msg_data
11440                 , x_serial_number              => l_dummy_sn
11441                 , x_quantity_reserved          => l_quantity_reserved_tmp
11442                 , x_reservation_id             => l_reservation_id
11443                 , p_api_version_number         => 1.0
11444                 , p_init_msg_lst               => fnd_api.g_false
11445                 , p_rsv_rec                    => l_reservation_record
11446                 , p_partial_reservation_flag   => fnd_api.g_true
11447                 , p_force_reservation_flag     => fnd_api.g_true
11448                 , p_serial_number              => l_dummy_sn
11449                 , p_validation_flag            => fnd_api.g_true
11450                 , p_over_reservation_flag      => 3
11451                 );
11452 
11453                 IF l_return_status = fnd_api.g_ret_sts_error THEN
11454                   IF (l_debug = 1) THEN
11455                     DEBUG('Create reservation failed for lpn_id= ' || l_lpn_cont_rec.lpn_id, 'Perform_Overship_Distribution');
11456                   END IF;
11457 
11458                   RAISE fnd_api.g_exc_error;
11459                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11460                   IF (l_debug = 1) THEN
11461                     DEBUG('Unexpected error during create of Reservations lpn_id= ' || l_lpn_cont_rec.lpn_id
11462                     , 'Perform_Overship_Distribution');
11463                   END IF;
11464 
11465                   RAISE fnd_api.g_exc_unexpected_error;
11466                 END IF;
11467 
11468                 IF (l_debug = 1) THEN
11469                   DEBUG('Create reservations is successful ' || l_reservation_id, 'Perform_Overship_Distribution');
11470                   DEBUG('After call to create reservation', 'Perform_Overship_Distribution');
11471                 END IF;
11472 
11473                 -- update cache quantities
11474                 g_lpn_contents_tab(l_index).quantity               := g_lpn_contents_tab(l_index).quantity - l_qty_overship;
11475                 l_lpn_cont_rec.quantity                            := l_lpn_cont_rec.quantity - l_qty_overship;
11476                 g_total_lpn_quantity                               := g_total_lpn_quantity - l_qty_overship;
11477 
11478                 -- bug#2830138
11479                 -- make backup of quantities used
11480                 -- will be used if overship has to be rollbacked
11481                 IF l_lpn_cont_qty_used.EXISTS(l_index) THEN
11482                   l_lpn_cont_qty_used(l_index)  := l_lpn_cont_qty_used(l_index) + l_qty_overship;
11483                 ELSE
11484                   l_lpn_cont_qty_used(l_index)  := l_qty_overship;
11485                 END IF;
11486 
11487                 -- debug('Line '||l_loaded_lines_rec.order_line_id||'overshipped for quantity '||l_qty_updt_resv,'Perform_Overship_Distribution');
11488                  -- if all quantity consumed exit to lpn_content loop
11489                  -- update wds for processed_quantity=processed_quantity+g_total_lpn_quantity
11490                 BEGIN
11491                   UPDATE wms_direct_ship_temp
11492                      SET processed_quantity =(processed_quantity + l_qty_overship)
11493                    WHERE organization_id = p_org_id
11494                      AND GROUP_ID = g_group_id
11495                      AND lpn_id = p_lpn_id
11496                      AND order_line_id = l_order_line_id;
11497                 EXCEPTION
11498                   WHEN OTHERS THEN
11499                     IF (l_debug = 1) THEN
11500                       DEBUG('Exception updating WDS', 'Perform_Overship_Distribution');
11501                     END IF;
11502 
11503                     RAISE fnd_api.g_exc_unexpected_error;
11504                 END;
11505               END IF; -- l_qty_overship>0
11506             END IF; -- l_transactable_qty>0
11507 
11508             IF l_lpn_cont_rec.quantity = 0 THEN
11509               EXIT;
11510             END IF;
11511           END IF; --revision
11512         END LOOP; -- loaded lines
11513 
11514         CLOSE loaded_lines;
11515       END IF;        -- quantity>0
11516 
11517           -- if still more quantity left lpn canot be loaded
11518 
11519       IF (l_debug = 1) THEN
11520         DEBUG('l_lpn_cont_rec.quantity=' || l_lpn_cont_rec.quantity, 'Perform_Overship_Distribution');
11521       END IF;
11522 
11523       IF l_lpn_cont_rec.quantity > 0 THEN
11524         IF (l_debug = 1) THEN
11525           DEBUG('Lpn cannot be loaded, unused quantity found', 'Perform_Overship_Distribution');
11526         END IF;
11527 
11528         x_return_status  := fnd_api.g_ret_sts_error;
11529 
11530         -- bug#2830138
11531         -- revert back the changes to lpn_contents from backup
11532         IF l_lpn_cont_qty_used.COUNT > 0 THEN
11533           l_temp_count          := l_lpn_cont_qty_used.FIRST;
11534 
11535           WHILE l_temp_count IS NOT NULL LOOP
11536             g_lpn_contents_tab(l_temp_count).quantity  := g_lpn_contents_tab(l_temp_count).quantity + l_lpn_cont_qty_used(l_temp_count);
11537             l_temp_count                               := l_lpn_cont_qty_used.NEXT(l_temp_count);
11538           END LOOP; --while
11539 
11540           g_total_lpn_quantity  := l_old_total_lpn_qty;
11541 
11542           IF (l_debug = 1) THEN
11543             DEBUG('lpn contents restored from backup', 'Perform_Overship_Distribution');
11544             DEBUG('g_total_lpn_quantity=' || g_total_lpn_quantity, 'Perform_Overship_Distribution');
11545           END IF;
11546         END IF;
11547 
11548         fnd_message.set_name('WMS', 'WMS_LPN_NOT_CONSUMED');
11549         fnd_msg_pub.ADD;
11550         RETURN;
11551       END IF;
11552     END IF; -- quantity>0
11553 
11554     IF (l_debug = 1) THEN
11555       DEBUG('l_index= ' || l_index, 'Perform_Overship_Distribution');
11556     END IF;
11557   END LOOP; -- g_lpn_contents_tab
11558 
11559   x_return_status  := fnd_api.g_ret_sts_success;
11560 EXCEPTION
11561   WHEN OTHERS THEN
11562     IF (l_debug = 1) THEN
11563       DEBUG('Exception occured', 'Perform_Overship_Distribution');
11564     END IF;
11565 
11566     x_return_status  := fnd_api.g_ret_sts_unexp_error;
11567     fnd_message.set_name('WMS', 'WMS_ERROR_LOADING_LPN');
11568     fnd_msg_pub.ADD;
11569 END perform_overship_distribution;
11570 
11571 /*
11572    This procedure checks any type of hold applied on the order line.
11573  */
11574 
11575 PROCEDURE check_holds(
11576   p_order_header_id IN            NUMBER
11577 , p_order_line_id   IN            NUMBER
11578 , x_return_status   OUT NOCOPY    VARCHAR2
11579 , x_msg_count       OUT NOCOPY    NUMBER
11580 , x_msg_data        OUT NOCOPY    VARCHAR2
11581 ) IS
11582   l_delivery_detail_id NUMBER;
11583   l_return_status      VARCHAR2(10);
11584   l_msg_count          NUMBER;
11585   l_msg_data           VARCHAR2(20000);
11586   l_debug              NUMBER          := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11587 BEGIN
11588   IF (l_debug = 1) THEN
11589     DEBUG('In the check holds procedure', 'Check_holds');
11590     DEBUG('header id:' || p_order_header_id, 'Check_holds');
11591     DEBUG('line id:' || p_order_line_id, 'Check_Holds');
11592   END IF;
11593 
11594   x_return_status  := fnd_api.g_ret_sts_success;
11595 
11596   SELECT delivery_detail_id
11597     INTO l_delivery_detail_id
11598     FROM wsh_delivery_details_ob_grp_v
11599    WHERE source_header_id = p_order_header_id
11600      AND source_line_id = p_order_line_id
11601      AND ROWNUM = 1;
11602 
11603   IF (l_debug = 1) THEN
11604     DEBUG('delivery detial id:' || l_delivery_detail_id, 'Check_Holds');
11605   END IF;
11606 
11607   wsh_details_validations.check_credit_holds(p_detail_id => l_delivery_detail_id, p_activity_type => 'PICK', p_source_code => 'OE'
11608   , x_return_status              => l_return_status);
11609 
11610   IF l_return_status = fnd_api.g_ret_sts_error THEN
11611     IF (l_debug = 1) THEN
11612       DEBUG('Credit Check holds API failed with status E ', 'Check_holds');
11613     END IF;
11614 
11615     RAISE fnd_api.g_exc_error;
11616   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11617     IF (l_debug = 1) THEN
11618       DEBUG('Check Credit Holds API failed with status U', 'Check_holds');
11619     END IF;
11620 
11621     RAISE fnd_api.g_exc_unexpected_error;
11622   END IF;
11623 EXCEPTION
11624   WHEN fnd_api.g_exc_error THEN
11625     x_return_status  := fnd_api.g_ret_sts_error;
11626 
11627     IF (l_debug = 1) THEN
11628       DEBUG('fnd_api.g_exe_error ' || SQLERRM, 'Check_holds');
11629     END IF;
11630 
11631     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11632   WHEN fnd_api.g_exc_unexpected_error THEN
11633     x_return_status  := fnd_api.g_ret_sts_unexp_error;
11634 
11635     IF (l_debug = 1) THEN
11636       DEBUG('FND_API.G_UNEXPECTED_ERR' || SQLERRM, 'check_holds');
11637     END IF;
11638 
11639     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11640   WHEN OTHERS THEN
11641     x_return_status  := fnd_api.g_ret_sts_unexp_error;
11642 
11643     IF (l_debug = 1) THEN
11644       DEBUG('FND_API.G_UNEXPECTED_ERR' || SQLERRM, 'check_holds');
11645     END IF;
11646 
11647     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11648 END check_holds;
11649 
11650 /* Bug 2994099:This procedure cleanup all the temp data for a backordered delivery for this lpn */
11651 
11652 PROCEDURE cleanup_orphan_rec(p_org_id IN NUMBER) IS
11653   PRAGMA AUTONOMOUS_TRANSACTION;
11654 
11655   CURSOR wstt_del IS
11656     SELECT DISTINCT delivery_id
11657                FROM wms_shipping_transaction_temp
11658               WHERE /*direct_ship_flag = 'Y'
11659                 AND*/ organization_id = p_org_id;
11660 
11661   --
11662   CURSOR closed_del(p_del_id NUMBER) IS
11663     SELECT delivery_id
11664       FROM wsh_new_deliveries_ob_grp_v
11665      WHERE delivery_id = p_del_id
11666        AND status_code IN('CL', 'IT');
11667 
11668   --
11669   CURSOR lpn_cur(p_del_id NUMBER) IS
11670     SELECT lpn_id
11671       FROM wms_license_plate_numbers
11672      WHERE lpn_id IN(SELECT outermost_lpn_id
11673                        FROM wms_shipping_transaction_temp
11674                       WHERE delivery_id = p_del_id);
11675 
11676       -- AND LPN_CONTEXT=1;   As even during Ship Confirm the records werent gettng deleted
11677   --
11678   l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11679 BEGIN
11680   FOR l_wstt_del IN wstt_del LOOP
11681     FOR l_closed_del IN closed_del(l_wstt_del.delivery_id) LOOP
11682       FOR l_lpn_cur IN lpn_cur(l_closed_del.delivery_id) LOOP
11683         IF (l_debug = 1) THEN
11684           DEBUG('Clean up bad data for lpn=' || l_lpn_cur.lpn_id, 'Cleanup_Orphan_Rec');
11685         END IF;
11686 
11687         DELETE      wms_direct_ship_temp
11688               WHERE lpn_id = l_lpn_cur.lpn_id;
11689 
11690         DELETE      wms_freight_cost_temp
11691               WHERE lpn_id = l_lpn_cur.lpn_id;
11692 
11693         DELETE      wms_freight_cost_temp
11694               WHERE delivery_id = l_closed_del.delivery_id;
11695 
11696         DELETE      wms_freight_cost_temp
11697               WHERE trip_id IN(SELECT DISTINCT trip_id
11698                                           FROM wms_shipping_transaction_temp
11699                                          WHERE outermost_lpn_id = l_lpn_cur.lpn_id);
11700 
11701         DELETE      wms_shipping_transaction_temp
11702               WHERE outermost_lpn_id = l_lpn_cur.lpn_id;
11703       END LOOP;
11704     END LOOP;
11705   END LOOP;
11706 
11707   COMMIT;
11708 EXCEPTION
11709   WHEN OTHERS THEN
11710     IF (l_debug = 1) THEN
11711       DEBUG('Exception occured', 'Cleanup_Orphan_Rec');
11712     END IF;
11713 
11714     RAISE fnd_api.g_exc_unexpected_error;
11715 END cleanup_orphan_rec;
11716 
11717 /*
11718  This function finds out if there is any record in lpn contents having
11719  available quantity >0 and end_item_unit_number=p_end_unit_number
11720  */
11721 
11722 FUNCTION validate_end_unit_num(p_item_id IN NUMBER, p_end_unit_number IN VARCHAR2)
11723   RETURN BOOLEAN IS
11724   l_start_index NUMBER;
11725   l_end_index   NUMBER;
11726   l_result      BOOLEAN := FALSE;
11727   l_debug       NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11728 BEGIN
11729   IF (l_debug = 1) THEN
11730     DEBUG('p_item_id=' || p_item_id, 'Validate_End_Unit_Num');
11731     DEBUG('p_end_unit_number=' || p_end_unit_number, 'Validate_End_Unit_Num');
11732   END IF;
11733 
11734   l_start_index  := g_lpn_contents_lookup_tab(p_item_id).start_index;
11735   l_end_index    := g_lpn_contents_lookup_tab(p_item_id).end_index;
11736 
11737   IF g_lpn_contents_tab(l_start_index).serial_control_code NOT IN(2, 5) THEN
11738     IF (l_debug = 1) THEN
11739       DEBUG('SUCCESS', 'Validate_End_Unit_Num');
11740     END IF;
11741 
11742     RETURN TRUE;
11743   ELSE
11744     IF g_cross_unit_allowed = 'Y' THEN
11745       IF (l_debug = 1) THEN
11746         DEBUG('SUCCESS', 'Validate_End_Unit_Num');
11747       END IF;
11748 
11749       RETURN TRUE;
11750     ELSE
11751       FOR l_rec IN l_start_index .. l_end_index LOOP
11752         IF (l_debug = 1) THEN
11753           DEBUG('EIUN lpn=' || g_lpn_contents_tab(l_rec).end_item_unit_number, 'Validate_End_Unit_Num');
11754         END IF;
11755 
11756         IF g_lpn_contents_tab(l_rec).quantity > 0 THEN
11757           IF NVL(g_lpn_contents_tab(l_rec).end_item_unit_number, '@@') = NVL(p_end_unit_number, '@@') THEN
11758             IF (l_debug = 1) THEN
11759               DEBUG('SUCCESS', 'Validate_End_Unit_Num');
11760             END IF;
11761 
11762             l_result  := TRUE;
11763           END IF;
11764         END IF;
11765       END LOOP;
11766     END IF;
11767   END IF;
11768 
11769   RETURN l_result;
11770 END validate_end_unit_num;
11771 
11772 /* This function finds out that the item in g_lpn_contents_tab at index is having available quantity
11773    and its end_item_unit_number matches p_end_unit_number.
11774 */
11775 
11776 FUNCTION validate_end_unit_num_at(p_index IN NUMBER, p_end_unit_number IN VARCHAR2)
11777   RETURN BOOLEAN IS
11778   l_result BOOLEAN := FALSE;
11779   l_debug  NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
11780 BEGIN
11781   IF (l_debug = 1) THEN
11782     DEBUG('p_index=' || p_index, 'Validate_End_Unit_Num_At');
11783     DEBUG('p_end_unit_number=' || p_end_unit_number, 'Validate_End_Unit_Num_At');
11784   END IF;
11785 
11786   IF g_lpn_contents_tab(p_index).serial_control_code NOT IN(2, 5) THEN
11787     IF (l_debug = 1) THEN
11788       DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
11789     END IF;
11790 
11791     RETURN TRUE;
11792   ELSE
11793     IF g_cross_unit_allowed = 'Y' THEN
11794       IF (l_debug = 1) THEN
11795         DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
11796       END IF;
11797 
11798       RETURN TRUE;
11799     ELSE
11800       IF g_lpn_contents_tab(p_index).quantity > 0 THEN
11801         IF NVL(g_lpn_contents_tab(p_index).end_item_unit_number, '@@') = NVL(p_end_unit_number, '@@') THEN
11802           IF (l_debug = 1) THEN
11803             DEBUG('SUCCESS', 'Validate_End_Unit_Num_At');
11804           END IF;
11805 
11806           l_result  := TRUE;
11807         END IF;
11808       END IF;
11809     END IF;
11810   END IF;
11811 
11812   RETURN l_result;
11813 END validate_end_unit_num_at;
11814 
11815 /* End of Patchset I procedures */
11816 
11817 FUNCTION GET_CATCH_WEIGHT
11818                         (P_ORG_ID IN NUMBER
11819                         ,P_LPN_ID IN NUMBER
11820                         ,P_INVENTORY_ITEM_ID IN NUMBER
11821                         ,P_REVISION IN VARCHAR2
11822                         ,P_LOT_NUMBER IN VARCHAR2
11823                         ,P_PICKED_QUANTITY_IN_PRI_UOM IN NUMBER
11824                         ) RETURN NUMBER IS
11825 CURSOR C        (C_ORG_ID IN NUMBER
11826                 ,C_LPN_ID IN NUMBER
11827                 ,C_INVENTORY_ITEM_ID IN NUMBER
11828                 ,C_REVISION IN VARCHAR2
11829                 ,C_LOT_NUMBER IN VARCHAR2)
11830         IS
11831         SELECT
11832                 t.ORG_ID
11833                 ,t.LPN_ID
11834                 ,t.INNER_LPN_ID
11835                 ,t.INVENTORY_ITEM_ID
11836                 ,t.REVISION
11837                 ,t.LOT_NUMBER
11838                 ,DECODE(t.PICKED_UOM_CODE, msi.primary_uom_code, t.PICKED_QUANTITY
11839                 ,GREATEST(inv_convert.inv_um_convert(NULL, NULL,
11840                   t.PICKED_QUANTITY, t.PICKED_UOM_CODE, msi.primary_uom_code,
11841                   NULL, NULL), 0)) PICKED_QUANTITY_IN_PRI_UOM
11842                 ,t.SECONDARY_UOM_CODE
11843                 ,t.SECONDARY_QUANTITY
11844         FROM mtl_system_items msi, WMS_DS_CT_WT_GTEMP t
11845         WHERE t.INVENTORY_ITEM_ID = C_INVENTORY_ITEM_ID /* THIS HAS INDEX */
11846         AND t.ORG_ID = C_ORG_ID
11847         AND msi.INVENTORY_ITEM_ID = t.INVENTORY_ITEM_ID
11848         AND msi.ORGANIZATION_ID = t.ORG_ID
11849         AND NVL(t.INNER_LPN_ID, t.LPN_ID) = C_LPN_ID
11850         AND NVL(t.REVISION,'#NULL#') = NVL(C_REVISION,'#NULL#')
11851         AND NVL(t.LOT_NUMBER,'#NULL#') = NVL(C_LOT_NUMBER,'#NULL#')
11852         AND t.SECONDARY_QUANTITY IS NOT NULL;
11853   CTWT_REC C%rowtype;
11854   RET_VAL NUMBER := NULL;
11855 BEGIN
11856   IF (G_debug=1) THEN
11857     debug('P_ORG_ID = ' || P_ORG_ID, 'GET_CATCH_WEIGHT');
11858     debug('P_LPN_ID = ' || P_LPN_ID, 'GET_CATCH_WEIGHT');
11859     debug('P_INVENTORY_ITEM_ID = ' || P_INVENTORY_ITEM_ID, 'GET_CATCH_WEIGHT');
11860     debug('P_REVISION = ' || P_REVISION, 'GET_CATCH_WEIGHT');
11861     debug('P_LOT_NUMBER = ' || P_LOT_NUMBER, 'GET_CATCH_WEIGHT');
11862     debug('P_PICKED_QUANTITY_IN_PRI_UOM = ' || P_PICKED_QUANTITY_IN_PRI_UOM, 'GET_CATCH_WEIGHT');
11863   END IF;
11864   OPEN C (P_ORG_ID, P_LPN_ID, P_INVENTORY_ITEM_ID, P_REVISION, P_LOT_NUMBER);
11865   FETCH C INTO CTWT_REC;
11866   IF C%NOTFOUND THEN
11867     ret_val := null;
11868   ELSE
11869     IF P_PICKED_QUANTITY_IN_PRI_UOM = CTWT_REC.PICKED_QUANTITY_IN_PRI_UOM  THEN
11870       -- LPN Qty and Delivery Detail Line Qty matches
11871       RET_VAL := CTWT_REC.SECONDARY_QUANTITY;
11872     ELSE
11873       -- LPN Qty and Delivery Detail Line Qty doesn't match.
11874       -- Hence calculate/derive the
11875       -- Delivery Line's sec_qty as follows
11876       RET_VAL  := ((P_PICKED_QUANTITY_IN_PRI_UOM / CTWT_REC.PICKED_QUANTITY_IN_PRI_UOM) * CTWT_REC.SECONDARY_QUANTITY);
11877     END IF;
11878   END IF;
11879 
11880   IF ( RET_VAL < 0 ) THEN
11881     RET_VAL := NULL;
11882   END IF;
11883   IF (G_debug=1) THEN
11884     debug('Calculated SECONDARY_QUANTITY = ' || RET_VAL, 'GET_CATCH_WEIGHT');
11885   END IF;
11886   RETURN RET_VAL;
11887 END GET_CATCH_WEIGHT;
11888 
11889 END WMS_DIRECT_SHIP_PVT;