DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_TRANSFER_ORDER_PVT

Source


1 PACKAGE BODY inv_transfer_order_pvt AS
2   /* $Header: INVVTROB.pls 120.34.12010000.2 2008/07/29 13:50:01 ptkumar ship $ */
3 
4   --  Global constant holding the package name
5   g_pkg_name       CONSTANT VARCHAR2(30)            := 'INV_Transfer_Order_PVT';
6   isdebug                   BOOLEAN                 := TRUE;
7      debug_mode                NUMBER                  := 1;
8   g_is_pickrelease_set         NUMBER;
9   g_debug                      NUMBER;
10 
11   g_retain_ato_profile VARCHAR2(1) := FND_PROFILE.VALUE('WSH_RETAIN_ATO_RESERVATIONS');
12   --Bug 1620576
13   --These tables are needed in Finalize_Pick_Confirm for overpicking.
14   TYPE mo_picked_quantity_rec IS RECORD(
15     picked_quantity               NUMBER
16   , primary_picked_quantity       NUMBER
17   , picked_uom                    VARCHAR2(3) --Bug5950172
18   , sec_picked_quantity           NUMBER
19   );
20 
21   TYPE rsv_picked_quantity_rec IS RECORD(
22     picked_quantity               NUMBER
23   , sec_picked_quantity           NUMBER
24   );
25 
26   -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
27   TYPE sub_reservable_rec_type IS RECORD(
28     reservable_type   NUMBER
29   , org_id            NUMBER
30   , subinventory_code VARCHAR2(10)
31   );
32 
33   TYPE mo_picked_quantity_tbl IS TABLE OF mo_picked_quantity_rec
34     INDEX BY BINARY_INTEGER;
35 
36   TYPE rsv_picked_quantity_tbl IS TABLE OF rsv_picked_quantity_rec
37     INDEX BY BINARY_INTEGER;
38 
39   -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
40   TYPE sub_reservable_type IS TABLE OF sub_reservable_rec_type
41     INDEX BY BINARY_INTEGER;
42 
43   g_mo_picked_quantity_tbl  mo_picked_quantity_tbl;
44   g_rsv_picked_quantity_tbl rsv_picked_quantity_tbl;
45 
46   -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
47   g_is_sub_reservable       sub_reservable_type;
48 
49   --Bug :4994950(Actual bug #4762505)
50   --Caching MMTT records that were already processed for this MO line
51   --This will be used to close the move order line
52   TYPE mmtt_tbl_tp IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
53   g_mmtt_cache_tbl mmtt_tbl_tp;
54 
55   -- Bug 5074402
56   g_omh_installed               NUMBER;
57   PROCEDURE DEBUG(p_message IN VARCHAR2, p_module IN VARCHAR2) IS
58     --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
59   BEGIN
60     IF  isdebug AND debug_mode = 1 THEN
61       --inv_debug.message(p_message);
62       --null;
63       --inv_trx_util_pub.trace(p_message, p_module, 3);
64       inv_pick_wave_pick_confirm_pub.tracelog(p_message, p_module);
65       gmi_reservation_util.println(p_module||p_message);
66     ELSIF  isdebug AND debug_mode = 2 THEN
67       --dbms_output.put_line(p_message);
68       NULL;
69     END IF;
70   END;
71 
72   PROCEDURE increment_max_line_number IS
73   BEGIN
74     inv_globals.g_max_line_num  := NVL(inv_globals.g_max_line_num, 0) + 1;
75   --debug('Line Num:'||to_char(Inv_Globals.g_max_line_num));
76   END increment_max_line_number;
77 
78   PROCEDURE reset_max_line_number IS
79   BEGIN
80     inv_globals.g_max_line_num  := NULL;
81   --debug('Line Num:'||to_char(Inv_Globals.g_max_line_num));
82   END reset_max_line_number;
83 
84   FUNCTION get_next_header_id(p_organization_id NUMBER := NULL)
85     RETURN NUMBER IS
86     l_header_id         NUMBER;
87     l_dummy             VARCHAR2(1);
88     l_request_number_ok BOOLEAN     := FALSE;
89   BEGIN
90     WHILE NOT l_request_number_ok LOOP
91       SELECT mtl_txn_request_headers_s.NEXTVAL
92         INTO l_header_id
93         FROM DUAL;
94 
95       BEGIN
96         SELECT 'X'
97           INTO l_dummy
98           FROM mtl_txn_request_headers
99          WHERE request_number = TO_CHAR(l_header_id)
100            AND organization_id = NVL(p_organization_id, organization_id);
101       EXCEPTION
102         WHEN NO_DATA_FOUND THEN
103           l_request_number_ok  := TRUE;
104       END;
105     END LOOP;
106 
107     RETURN l_header_id;
108   END get_next_header_id;
109 
110   FUNCTION unique_order(p_organization_id IN NUMBER, p_request_number IN VARCHAR2)
111     RETURN BOOLEAN IS
112     l_dummy VARCHAR2(1);
113   BEGIN
114     SELECT 'X'
115       INTO l_dummy
116       FROM mtl_txn_request_headers
117      WHERE request_number = p_request_number
118        AND organization_id = p_organization_id;
119 
120     RETURN FALSE;
121   EXCEPTION
122     WHEN NO_DATA_FOUND THEN
123       RETURN TRUE;
124   END unique_order;
125 
126   FUNCTION unique_line(p_organization_id IN NUMBER, p_header_id IN NUMBER, p_line_number IN NUMBER)
127     RETURN BOOLEAN IS
128     l_dummy VARCHAR2(1);
129   BEGIN
130     SELECT 'X'
131       INTO l_dummy
132       FROM mtl_txn_request_lines
133      WHERE header_id = p_header_id
134        AND organization_id = p_organization_id
135        AND line_number = p_line_number;
136 
137     RETURN FALSE;
138   EXCEPTION
139     WHEN NO_DATA_FOUND THEN
140       RETURN TRUE;
141   END unique_line;
142 
143   FUNCTION get_primary_quantity(p_item_id IN NUMBER, p_organization_id IN NUMBER, p_from_quantity IN NUMBER, p_from_unit IN VARCHAR2)
144     RETURN NUMBER IS
145     l_primary_uom      VARCHAR2(3);
146     l_primary_quantity NUMBER;
147   BEGIN
148     SELECT primary_uom_code
149       INTO l_primary_uom
150       FROM mtl_system_items
151      WHERE organization_id = p_organization_id
152        AND inventory_item_id = p_item_id;
153 
154     l_primary_quantity  := inv_convert.inv_um_convert(item_id => p_item_id, PRECISION => NULL, from_quantity => p_from_quantity, from_unit => p_from_unit, to_unit => l_primary_uom, from_name => NULL, to_name => NULL);
155     RETURN l_primary_quantity;
156   END get_primary_quantity;
157 
158   PROCEDURE delete_troldt(x_return_status OUT NOCOPY VARCHAR2, x_msg_data OUT NOCOPY VARCHAR2, x_msg_count OUT NOCOPY NUMBER, p_troldt_tbl IN inv_mo_line_detail_util.g_mmtt_tbl_type, p_move_order_type IN NUMBER) IS
159     l_troldt_tbl            inv_mo_line_detail_util.g_mmtt_tbl_type;
160     l_mtl_reservation       inv_reservation_global.mtl_reservation_tbl_type;
161     l_mtl_reservation_count NUMBER;
162     l_rsv_temp_rec          inv_reservation_global.mtl_reservation_rec_type;
163     l_to_rsv_rec            inv_reservation_global.mtl_reservation_rec_type;
164     l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
165     l_msg_count             NUMBER;
166     l_msg_data              VARCHAR2(2000);
167     l_error_code            NUMBER;
168     l_return_status         VARCHAR2(1);
169     l_success               BOOLEAN;
170   BEGIN
171     l_troldt_tbl     := p_troldt_tbl;
172 
173     FOR l_counter IN 1 .. l_troldt_tbl.COUNT LOOP
174       -- if this is a pick wave move order, update the reservation first
175       IF (p_move_order_type = 3) THEN
176         l_rsv_temp_rec.reservation_id   := l_troldt_tbl(l_counter).reservation_id;
177         inv_reservation_pub.query_reservation(
178           p_api_version_number         => 1.0
179         , p_init_msg_lst               => fnd_api.g_true
180         , x_return_status              => l_return_status
181         , x_msg_count                  => l_msg_count
182         , x_msg_data                   => l_msg_data
183         , p_query_input                => l_rsv_temp_rec
184         , x_mtl_reservation_tbl        => l_mtl_reservation
185         , x_mtl_reservation_tbl_count  => l_mtl_reservation_count
186         , x_error_code                 => l_error_code
187         );
188 
189         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
190           RAISE fnd_api.g_exc_unexpected_error;
191         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
192           RAISE fnd_api.g_exc_error;
193         END IF;
194 
195         l_rsv_temp_rec                  := l_mtl_reservation(1);
196         l_to_rsv_rec                    := l_mtl_reservation(1);
197         l_to_rsv_rec.detailed_quantity  := l_to_rsv_rec.detailed_quantity - l_troldt_tbl(l_counter).transaction_quantity;
198 
199 
200 --INVCONV - Make sure Qty2 are NULL if nor present
201         IF (  l_to_rsv_rec.secondary_uom_code IS NULL ) THEN
202               l_to_rsv_rec.secondary_reservation_quantity := NULL;
203               l_to_rsv_rec.secondary_detailed_quantity    := NULL;
204         END IF;
205 
206         inv_reservation_pub.update_reservation(
207           p_api_version_number         => 1.0
208         , p_init_msg_lst               => fnd_api.g_true
209         , x_return_status              => l_return_status
210         , x_msg_count                  => l_msg_count
211         , x_msg_data                   => l_msg_data
212         , p_original_rsv_rec           => l_rsv_temp_rec
213         , p_to_rsv_rec                 => l_to_rsv_rec
214         , p_original_serial_number     => l_dummy_sn
215         , p_to_serial_number           => l_dummy_sn
216         , p_validation_flag            => fnd_api.g_true
217         );
218 
219         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
220           RAISE fnd_api.g_exc_unexpected_error;
221         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
222           RAISE fnd_api.g_exc_error;
223         END IF;
224       END IF;
225 
226       BEGIN
227         inv_replenish_detail_pub.clear_record(p_trx_tmp_id => l_troldt_tbl(l_counter).transaction_temp_id,
228                                               p_success => l_success);
229         if( not l_success ) then
230           raise FND_API.G_EXC_ERROR;
231         end if;
232       EXCEPTION
233         WHEN OTHERS THEN
234           RAISE fnd_api.g_exc_unexpected_error;
235       END;
236       inv_mo_line_detail_util.delete_row(x_return_status => l_return_status, p_line_id => l_troldt_tbl(l_counter).move_order_line_id, p_line_detail_id => l_troldt_tbl(l_counter).transaction_temp_id);
237     END LOOP;
238 
239     x_return_status  := l_return_status;
240     x_msg_data       := l_msg_data;
241     x_msg_count      := l_msg_count;
242   EXCEPTION
243     WHEN fnd_api.g_exc_error THEN
244       RAISE;
245     WHEN fnd_api.g_exc_unexpected_error THEN
246       RAISE;
247     WHEN OTHERS THEN
248       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
249         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Trolins');
250       END IF;
251 
252       RAISE fnd_api.g_exc_unexpected_error;
253   END delete_troldt;
254 
255   PROCEDURE update_lots_temp(
256     x_return_status    OUT NOCOPY VARCHAR2
257   , x_msg_data         OUT NOCOPY VARCHAR2
258   , x_msg_count        OUT NOCOPY NUMBER
259   , p_operation            VARCHAR2
260   , p_item_id              NUMBER
261   , p_org_id               NUMBER
262   , p_trx_temp_id          NUMBER
263   , p_cancel_qty           NUMBER
264   , p_trx_uom              VARCHAR2
265   , p_primary_uom          VARCHAR2
266   , p_last_updated_by      NUMBER
267   , p_last_update_date     DATE
268   , p_creation_date        DATE
269   , p_created_by           NUMBER
270   ) IS
271     l_mtlt_cancel_qty  NUMBER;
272     l_mtlt_trx_qty     NUMBER;
273     l_mtlt_sec_trx_qty NUMBER; --INVCONV
274     l_lot_count        NUMBER;
275     l_mtlt_primary_qty NUMBER;
276     l_lot_number       VARCHAR2(80);
277     l_tracking_quantity_ind VARCHAR2(30);
278     l_secondary_uom_code VARCHAR2(10);
279     l_sec_quantity_cancel NUMBER;
280 
281     CURSOR c_mtlt IS
282       SELECT        transaction_quantity,
283                     secondary_quantity         --INVCONV
284                     , lot_number -- nsinghi bug#5724815.
285                FROM mtl_transaction_lots_temp
286               WHERE transaction_temp_id = p_trx_temp_id
287            ORDER BY creation_date
288       FOR UPDATE OF transaction_temp_id;
289 
290     -- nsinghi bug#5724815 Added new cursor to fetch the secondary uom code for item
291     CURSOR c_get_sec_uom IS
292       SELECT tracking_quantity_ind, secondary_uom_code
293       FROM   mtl_system_items_b
294       WHERE  inventory_item_id = p_item_id
295       AND    organization_id = p_org_id;
296 
297     l_debug number;
298   BEGIN
299     -- Initialize API return status to success
300     x_return_status  := fnd_api.g_ret_sts_success;
301 
302     -- Use cache to get value for l_debug
303     IF g_is_pickrelease_set IS NULL THEN
304        g_is_pickrelease_set := 2;
305        IF INV_CACHE.is_pickrelease THEN
306           g_is_pickrelease_set := 1;
307        END IF;
308     END IF;
309     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
310        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
311     END IF;
312 
313     l_debug := g_debug;
314 
315     IF p_operation = 'DELETE' THEN -- when cancel qty >sum(mmtt trx qty)
316       IF (l_debug = 1) THEN
317          DEBUG('Deleting the row in mtlt ', 'UPDATE LOTS TEMP');
318       END IF;
319 
320       DELETE FROM mtl_transaction_lots_temp
321             WHERE transaction_temp_id = p_trx_temp_id;
322     ELSIF p_operation = 'UPDATE' THEN -- when cancel qty < sum(mmtt trx qty)
323       SELECT COUNT(*)
324         INTO l_lot_count
325         FROM mtl_transaction_lots_temp
326        WHERE transaction_temp_id = p_trx_temp_id;
327 
328       -- nsinghi bug#5724815
329       OPEN c_get_sec_uom;
330       FETCH c_get_sec_uom INTO l_tracking_quantity_ind, l_secondary_uom_code;
331       CLOSE c_get_sec_uom;
332 
333       OPEN c_mtlt;
334       l_mtlt_cancel_qty  := p_cancel_qty;
335 
336       FOR i IN 1 .. l_lot_count LOOP
337 --        FETCH c_mtlt INTO l_mtlt_trx_qty, l_mtlt_sec_trx_qty;
338         FETCH c_mtlt INTO l_mtlt_trx_qty, l_mtlt_sec_trx_qty, l_lot_number;
339 
340         IF l_mtlt_trx_qty <= l_mtlt_cancel_qty THEN
341           l_mtlt_cancel_qty  := l_mtlt_cancel_qty - l_mtlt_trx_qty;
342           IF (l_debug = 1) THEN
343              DEBUG('Delete current row ', 'UPDATE LOTS TEMP');
344           END IF;
345 
346           -- Delete the current row from mtlt since mtlt cancel qty > mtlt trx qty
347           DELETE FROM mtl_transaction_lots_temp
348                 WHERE  CURRENT OF c_mtlt;
349         ELSIF l_mtlt_trx_qty > l_mtlt_cancel_qty THEN
350           IF (l_debug = 1) THEN
351              DEBUG('Update current row ', 'UPDATE LOTS TEMP');
352           END IF;
353           -- update the mtlt row from mtlt since mtlt cancel qty < mtlt trx qty
354           l_mtlt_trx_qty      := l_mtlt_trx_qty - l_mtlt_cancel_qty;
355           -- nsinghi bug#5724815 Determine the secondary qty to be deducted from mtlt
356           -- START
357           IF NVL(l_tracking_quantity_ind, 'P') = 'PS' AND l_mtlt_sec_trx_qty IS NOT NULL THEN
358             l_sec_quantity_cancel := inv_convert.inv_um_convert
359                             (       item_id => p_item_id,
360                                     lot_number => l_lot_number,
361                                     organization_id => p_org_id,
362                                     PRECISION => NULL,
363                                     from_quantity => l_mtlt_cancel_qty,
364                                     from_unit => p_trx_uom,
365                                     to_unit => l_secondary_uom_code,
366                                     from_name => NULL,
367                                     to_name => NULL
368                             );
369             IF (l_sec_quantity_cancel = -99999) THEN
370               IF (l_debug = 1) THEN
371                  DEBUG('Cannot convert uom to secondary uom', 'UPDATE LOTS TEMP');
372               END IF;
373               fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
374               fnd_message.set_token('VALUE1', l_secondary_uom_code);
375               fnd_message.set_token('VALUE2', p_trx_uom);
376               fnd_msg_pub.ADD;
377               RAISE fnd_api.g_exc_unexpected_error;
378             END IF;
379 
380             l_mtlt_sec_trx_qty := l_mtlt_sec_trx_qty - l_sec_quantity_cancel;
381             IF l_mtlt_sec_trx_qty < 0 THEN
382                l_mtlt_sec_trx_qty := 0;
383             END IF;
384           END IF;
385           -- END
386 
387           l_mtlt_primary_qty  := inv_convert.inv_um_convert(item_id => p_item_id, PRECISION => NULL, from_quantity => l_mtlt_trx_qty, from_unit => p_trx_uom, to_unit => p_primary_uom, from_name => NULL, to_name => NULL);
388 
389           UPDATE mtl_transaction_lots_temp
390              SET transaction_quantity = l_mtlt_trx_qty
391                , secondary_quantity = l_mtlt_sec_trx_qty -- INVCONV
392                , primary_quantity = l_mtlt_primary_qty
393                , last_update_date = p_last_update_date
394                , last_updated_by = p_last_updated_by
395                , creation_date = p_creation_date
396                , created_by = p_created_by
397            WHERE  CURRENT OF c_mtlt;
398 
399           l_mtlt_cancel_qty   := 0;
400         END IF;
401 
402         IF l_mtlt_cancel_qty <= 0 THEN
403           RETURN;
404         END IF;
405       END LOOP;
406     END IF;
407   EXCEPTION
408     WHEN fnd_api.g_exc_error THEN
409       x_return_status  := fnd_api.g_ret_sts_error;
410       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
411     WHEN fnd_api.g_exc_unexpected_error THEN
412       x_return_status  := fnd_api.g_ret_sts_unexp_error;
413       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
414     WHEN OTHERS THEN
415       x_return_status  := fnd_api.g_ret_sts_unexp_error;
416 
417       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
418         fnd_msg_pub.add_exc_msg(g_pkg_name, 'update lots temp');
419       END IF;
420 
421       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
422   END update_lots_temp;
423 
424   -- bug 2195303
425 
426   PROCEDURE update_serial_temp(x_return_status OUT NOCOPY VARCHAR2, x_msg_data OUT NOCOPY VARCHAR2, x_msg_count OUT NOCOPY NUMBER, p_operation VARCHAR2, p_trx_temp_id NUMBER, p_cancel_qty NUMBER) IS
427     l_serial_temp_id        NUMBER;
428     l_serial_count          NUMBER;
429     del_serial_number       NUMBER       := 0;
430     l_header_id             NUMBER       := -1;
431     unmarked_value          NUMBER       := -1;
432     l_fm_serial_number      VARCHAR2(10);
433     l_to_serial_number      VARCHAR2(10);
434     l_transaction_header_id NUMBER;
435 
436     CURSOR serial_temp_csr IS
437       SELECT        fm_serial_number
438                   , to_serial_number
439                   , group_header_id
440                FROM mtl_serial_numbers_temp
441               WHERE transaction_temp_id = p_trx_temp_id
442            ORDER BY fm_serial_number DESC
443       FOR UPDATE OF transaction_temp_id;
444   BEGIN
445     -- Initialize API return status to success
446     x_return_status  := fnd_api.g_ret_sts_success;
447 
448     IF p_operation = 'DELETE' THEN -- when cancel qty >sum(mmtt trx qty)
449       DELETE FROM mtl_serial_numbers_temp
450             WHERE transaction_temp_id = p_trx_temp_id;
451     ELSIF p_operation = 'UPDATE' THEN -- when cancel qty < sum(mmtt trx qty)
452       SELECT transaction_header_id
453         INTO l_transaction_header_id
454         FROM mtl_material_transactions_temp
455        WHERE transaction_temp_id = p_trx_temp_id;
456 
457       SELECT COUNT(*)
458         INTO l_serial_count
459         FROM mtl_serial_numbers_temp
460        WHERE transaction_temp_id = p_trx_temp_id;
461 
462       IF (l_serial_count > 0) THEN
463         -- dbms_output.put_line('inside serial count> 0');
464         FOR i IN 1 .. l_serial_count LOOP
465           OPEN serial_temp_csr;
466           FETCH serial_temp_csr INTO l_fm_serial_number, l_to_serial_number, l_header_id;
467 
468           IF l_to_serial_number IS NOT NULL THEN
469             IF l_to_serial_number = l_fm_serial_number THEN
470               --ranges individually assigned ranges
471               DELETE FROM mtl_serial_numbers_temp
472                     WHERE  CURRENT OF serial_temp_csr;
473 
474              /*** {{ R12 Enhanced reservations code changes,
475               *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
476               UPDATE mtl_serial_numbers
477                  SET line_mark_id = unmarked_value
478                    , group_mark_id = unmarked_value
479                    , lot_line_mark_id = unmarked_value
480                WHERE group_mark_id IN (p_trx_temp_id, l_transaction_header_id)
481                  AND serial_number >= NVL(l_fm_serial_number, serial_number)
482                  AND serial_number <= NVL(l_to_serial_number, NVL(l_fm_serial_number, serial_number))
483                  AND LENGTH(serial_number) = LENGTH(NVL(l_fm_serial_number, serial_number));
484               *** End R12 }} ***/
485 
486              /*** {{ R12 Enhanced reservations code changes ***/
487               serial_check.inv_unmark_rsv_serial
488                  (from_serial_number   => l_fm_serial_number
489                  ,to_serial_number     => l_to_serial_number
490                  ,serial_code          => null
491                  ,hdr_id               => l_transaction_header_id
492                  ,temp_id              => p_trx_temp_id
493                  ,p_update_reservation => fnd_api.g_true);
494              /*** End R12 }} ***/
495 
496               del_serial_number  := del_serial_number + 1;
497 
498               --dbms_output.put_line('Del serial number'||Del_serial_number);
499 
500               IF del_serial_number = p_cancel_qty THEN
501                 --dbms_output.put_line('Del serial number = cancel_qty'||Del_serial_number);
502                 RETURN;
503               END IF;
504             END IF; -- end if range individually assigned serials
505           END IF; -- end if l_to_serial number null check
506 
507           CLOSE serial_temp_csr;
508         END LOOP;
509       END IF;
510     END IF;
511   EXCEPTION
512     WHEN fnd_api.g_exc_error THEN
513       x_return_status  := fnd_api.g_ret_sts_error;
514       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
515     WHEN fnd_api.g_exc_unexpected_error THEN
516       x_return_status  := fnd_api.g_ret_sts_unexp_error;
517       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
518     WHEN OTHERS THEN
519       x_return_status  := fnd_api.g_ret_sts_unexp_error;
520 
521       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
522         fnd_msg_pub.add_exc_msg(g_pkg_name, 'update serial temp');
523       END IF;
524 
525       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
526   END update_serial_temp;
527 
528   PROCEDURE update_troldt(
529     x_return_status   OUT    NOCOPY VARCHAR2
530   , x_msg_data        OUT    NOCOPY VARCHAR2
531   , x_msg_count       OUT    NOCOPY NUMBER
532   , p_trolin_rec      IN     inv_move_order_pub.trolin_rec_type
533   , p_old_trolin_rec  IN     inv_move_order_pub.trolin_rec_type
534   , p_troldt_tbl      IN     inv_mo_line_detail_util.g_mmtt_tbl_type
535   , p_move_order_type IN     NUMBER
536   , x_trolin_rec      IN OUT    NOCOPY inv_move_order_pub.trolin_rec_type
537   , p_delete_mmtt     IN     VARCHAR2 DEFAULT 'YES'  --Added bug3524130
538   ) IS
539     l_troldt_tbl            inv_mo_line_detail_util.g_mmtt_tbl_type;
540     l_trolin_rec            inv_move_order_pub.trolin_rec_type;
541     l_old_trolin_rec        inv_move_order_pub.trolin_rec_type;
542     l_quantity_cancel       NUMBER;
543     l_mtl_reservation       inv_reservation_global.mtl_reservation_tbl_type;
544     l_mtl_reservation_count NUMBER;
545     l_rsv_temp_rec          inv_reservation_global.mtl_reservation_rec_type;
546     l_to_rsv_rec            inv_reservation_global.mtl_reservation_rec_type;
547     l_dummy_sn              inv_reservation_global.serial_number_tbl_type;
548     l_msg_count             NUMBER;
549     l_msg_data              VARCHAR2(2000);
550     l_error_code            NUMBER;
551     l_return_status         VARCHAR2(1);
552     --    l_move_order_type             NUMBER;
553     l_rsv_quantity          NUMBER;
554     l_transaction_qty       NUMBER;
555     l_transaction_uom       VARCHAR2(3);
556     l_primary_qty           NUMBER;
557     l_primary_uom           VARCHAR2(3);
558     l_lot_control_code      NUMBER;
559     l_serial_control_code   NUMBER;
560     l_debug number;
561     l_success               BOOLEAN;
562     l_secondary_uom_qty     NUMBER;
563     l_sec_quantity_cancel   NUMBER;
564   BEGIN
565     -- Use cache to get value for l_debug
566     IF g_is_pickrelease_set IS NULL THEN
567        g_is_pickrelease_set := 2;
568        IF INV_CACHE.is_pickrelease THEN
569           g_is_pickrelease_set := 1;
570        END IF;
571     END IF;
572     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
573        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
574     END IF;
575 
576     l_debug := g_debug;
577 
578     l_trolin_rec      := p_trolin_rec;
579     l_old_trolin_rec  := p_old_trolin_rec;
580     l_troldt_tbl      := p_troldt_tbl;
581 
582     -- if the update is update quantity and decrease the quantity
583     IF (l_trolin_rec.quantity < l_old_trolin_rec.quantity) THEN
584       IF (l_trolin_rec.quantity <= 0) THEN
585         l_return_status  := fnd_api.g_ret_sts_error;
586         RAISE fnd_api.g_exc_error;
587       END IF;
588 
589       l_quantity_cancel               := l_old_trolin_rec.quantity - l_trolin_rec.quantity;
590 
591       IF (l_quantity_cancel > (l_trolin_rec.quantity - l_trolin_rec.quantity_delivered)) THEN
592         l_return_status  := fnd_api.g_ret_sts_error;
593         RAISE fnd_api.g_exc_error;
594       END IF;
595 
596       l_trolin_rec.quantity_detailed  := l_trolin_rec.quantity_detailed - l_quantity_cancel;
597       -- nsinghi bug#5724815 Update the secondary_quantity_detailed also. Fetch l_quantity_cancel in sec uom and deduct from secondary_quantity_detailed.
598       -- START
599       IF l_old_trolin_rec.secondary_uom IS NOT NULL THEN
600         l_sec_quantity_cancel := inv_convert.inv_um_convert
601                         (       item_id => l_old_trolin_rec.inventory_item_id,
602                                 PRECISION => NULL,
603                                 from_quantity => l_quantity_cancel,
604                                 from_unit => l_trolin_rec.uom_code,
605                                 to_unit => l_old_trolin_rec.secondary_uom,
606                                 from_name => NULL,
607                                 to_name => NULL
608                         );
609 
610         IF (l_sec_quantity_cancel = -99999) THEN
611           IF (l_debug = 1) THEN
612              DEBUG('Cannot convert uom to secondary uom', 'UPDATE TROLNDT ');
613           END IF;
614           fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
615           fnd_message.set_token('VALUE1', l_old_trolin_rec.secondary_uom);
616           fnd_message.set_token('VALUE2', l_trolin_rec.uom_code);
617           fnd_msg_pub.ADD;
618           RAISE fnd_api.g_exc_unexpected_error;
619         END IF;
620         IF l_trolin_rec.secondary_quantity_detailed IS NOT NULL THEN
621            l_trolin_rec.secondary_quantity_detailed := l_trolin_rec.secondary_quantity_detailed - l_sec_quantity_cancel;
622 	   IF l_trolin_rec.secondary_quantity_detailed < 0 THEN
623 	      l_trolin_rec.secondary_quantity_detailed := 0;
624 	   END IF;
625         END IF;
626       END IF;
627       -- END
628 
629       IF (l_debug = 1) THEN
630          DEBUG(l_quantity_cancel, 'UPDATE TROLNDT ');
631       END IF;
632     END IF;
633 
634     IF (l_old_trolin_rec.quantity <> l_trolin_rec.quantity
635         OR l_old_trolin_rec.quantity_detailed <> l_trolin_rec.quantity_detailed
636         OR l_old_trolin_rec.line_status <> l_trolin_rec.line_status
637        ) THEN
638       FOR l_counter IN 1 .. l_troldt_tbl.COUNT LOOP -- Start of FOR LOOP
639         -- get the reservation record if this is a pick wave move order
640         IF (l_debug = 1) THEN
641            DEBUG('inside the loop', 'UPDATE TROLNDT');
642         END IF;
643 
644         SELECT lot_control_code
645              , serial_number_control_code
646           INTO l_lot_control_code
647              , l_serial_control_code
648           FROM mtl_system_items
649          WHERE inventory_item_id = l_troldt_tbl(l_counter).inventory_item_id
650            AND organization_id = l_troldt_tbl(l_counter).organization_id;
651 
652         IF (l_debug = 1) THEN
653            DEBUG('Lot control code '|| TO_CHAR(l_lot_control_code), 'UPDATE TROLNDT');
654         END IF;
655 
656         IF (p_move_order_type = 3) THEN
657           l_rsv_temp_rec.reservation_id  := l_troldt_tbl(l_counter).reservation_id;
658           inv_reservation_pub.query_reservation(
659             p_api_version_number         => 1.0
660           , x_return_status              => l_return_status
661           , x_msg_count                  => l_msg_count
662           , x_msg_data                   => l_msg_data
663           , p_query_input                => l_rsv_temp_rec
664           , x_mtl_reservation_tbl        => l_mtl_reservation
665           , x_mtl_reservation_tbl_count  => l_mtl_reservation_count
666           , x_error_code                 => l_error_code
667           );
668 
669           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
670             RAISE fnd_api.g_exc_unexpected_error;
671           ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
672             RAISE fnd_api.g_exc_error;
673           END IF;
674 
675           l_to_rsv_rec                   := l_mtl_reservation(1);
676           l_rsv_temp_rec                 := l_mtl_reservation(1);
677         END IF;
678 
679         IF (l_trolin_rec.quantity < l_old_trolin_rec.quantity) THEN
680           IF (l_debug = 1) THEN
681              DEBUG('inside the decrease quantity condition', 'UPDATE TROLNDT');
682           END IF;
683 
684           /* decrease quantity request on the move order line
685           we need to see if any detail exists for this move order line,
686           if detail exist, we need to delete the line detail for the delta quantity
687           and update the detailed_qty on reservation if it exist for the particular detail record */
688           IF (l_troldt_tbl(l_counter).transaction_quantity <= l_quantity_cancel) THEN
689             IF (l_debug = 1) THEN
690                DEBUG('delete detail row', 'UPDATE TROLNDT');
691             END IF;
692 
693             IF l_lot_control_code = 2 THEN
694               update_lots_temp(
695                 x_return_status              => l_return_status
696               , x_msg_count                  => l_msg_count
697               , x_msg_data                   => l_msg_data
698               , p_operation                  => 'DELETE'
699               , p_item_id                    => l_troldt_tbl(l_counter).inventory_item_id
700               , p_org_id                     => l_troldt_tbl(l_counter).organization_id
701               , p_trx_temp_id                => l_troldt_tbl(l_counter).transaction_temp_id
702               , p_cancel_qty                 => l_quantity_cancel
703               , p_trx_uom                    => l_troldt_tbl(l_counter).transaction_uom
704               , p_primary_uom                => l_troldt_tbl(l_counter).item_primary_uom_code
705               , p_last_updated_by            => l_troldt_tbl(l_counter).last_updated_by
706               , p_last_update_date           => l_troldt_tbl(l_counter).last_update_date
707               , p_creation_date              => l_troldt_tbl(l_counter).creation_date
708               , p_created_by                 => l_troldt_tbl(l_counter).created_by
709               );
710 
711               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
712                 RAISE fnd_api.g_exc_unexpected_error;
713               ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
714                 RAISE fnd_api.g_exc_error;
715               END IF;
716             END IF;
717 
718             -- bug 2195303
719             IF l_serial_control_code IN (2, 5) THEN
720               update_serial_temp(x_return_status => l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data, p_operation => 'DELETE', p_trx_temp_id => l_troldt_tbl(l_counter).transaction_temp_id, p_cancel_qty => l_quantity_cancel);
721 
722               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
723                 RAISE fnd_api.g_exc_unexpected_error;
724               ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
725                 RAISE fnd_api.g_exc_error;
726               END IF;
727             END IF;
728 
729             l_quantity_cancel  := l_quantity_cancel - l_troldt_tbl(l_counter).transaction_quantity;
730             l_rsv_quantity     := l_troldt_tbl(l_counter).transaction_quantity;
731             BEGIN
732               inv_replenish_detail_pub.clear_record(
733                                               p_trx_tmp_id => l_troldt_tbl(l_counter).transaction_temp_id,
734                                               p_success => l_success);
735               if( not l_success ) then
736                 raise FND_API.G_EXC_ERROR;
737               end if;
738             EXCEPTION
739               WHEN OTHERS THEN
740                 RAISE fnd_api.g_exc_unexpected_error;
741             END;
742             IF p_delete_mmtt = 'YES' THEN --Added bug3524130
743             inv_mo_line_detail_util.delete_row(x_return_status => l_return_status, p_line_id => l_trolin_rec.line_id, p_line_detail_id => l_troldt_tbl(l_counter).transaction_temp_id);
744             END IF;
745 
746             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
747               RAISE fnd_api.g_exc_unexpected_error;
748             ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
749               RAISE fnd_api.g_exc_error;
750             END IF;
751           ELSIF (l_troldt_tbl(l_counter).transaction_quantity > l_quantity_cancel) THEN
752             IF (l_debug = 1) THEN
753                DEBUG('only need to update the line detail', 'UPDATE TROLNDT ');
754             END IF;
755 
756             IF l_lot_control_code = 2 THEN
757               update_lots_temp(
758                 x_return_status              => l_return_status
759               , x_msg_count                  => l_msg_count
760               , x_msg_data                   => l_msg_data
761               , p_operation                  => 'UPDATE'
762               , p_item_id                    => l_troldt_tbl(l_counter).inventory_item_id
763               , p_org_id                     => l_troldt_tbl(l_counter).organization_id
764               , p_trx_temp_id                => l_troldt_tbl(l_counter).transaction_temp_id
765               , p_cancel_qty                 => l_quantity_cancel
766               , p_trx_uom                    => l_troldt_tbl(l_counter).transaction_uom
767               , p_primary_uom                => l_troldt_tbl(l_counter).item_primary_uom_code
768               , p_last_updated_by            => l_troldt_tbl(l_counter).last_updated_by
769               , p_last_update_date           => l_troldt_tbl(l_counter).last_update_date
770               , p_creation_date              => l_troldt_tbl(l_counter).creation_date
771               , p_created_by                 => l_troldt_tbl(l_counter).created_by
772               );
773 
774               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
775                 RAISE fnd_api.g_exc_unexpected_error;
776               ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
777                 RAISE fnd_api.g_exc_error;
778               END IF;
779             END IF;
780 
781             -- bug 2195303
782             IF l_serial_control_code IN (2, 5) THEN
783               update_serial_temp(x_return_status => l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data, p_operation => 'DELETE', p_trx_temp_id => l_troldt_tbl(l_counter).transaction_temp_id, p_cancel_qty => l_quantity_cancel);
784 
785               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
786                 RAISE fnd_api.g_exc_unexpected_error;
787               ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
788                 RAISE fnd_api.g_exc_error;
789               END IF;
790             END IF;
791 
792             l_troldt_tbl(l_counter).transaction_quantity  := l_troldt_tbl(l_counter).transaction_quantity - l_quantity_cancel;
793             l_transaction_qty                             := l_troldt_tbl(l_counter).transaction_quantity;
794             l_transaction_uom                             := l_troldt_tbl(l_counter).transaction_uom;
795             l_primary_uom                                 := l_troldt_tbl(l_counter).item_primary_uom_code;
796             l_primary_qty                                 :=
797                      inv_convert.inv_um_convert(item_id => l_troldt_tbl(l_counter).inventory_item_id, PRECISION => NULL, from_quantity => l_transaction_qty, from_unit => l_transaction_uom, to_unit => l_primary_uom, from_name => NULL, to_name => NULL);
798             l_troldt_tbl(l_counter).primary_quantity      := l_primary_qty;
799             -- nsinghi bug#5724815 Update the secondary qty too in MMTT record.
800             -- START
801             IF l_troldt_tbl(l_counter).secondary_uom_code IS NOT NULL THEN
802               l_secondary_uom_qty                         := inv_convert.inv_um_convert
803                         (       item_id => l_troldt_tbl(l_counter).inventory_item_id,
804                                 PRECISION => NULL,
805                                 from_quantity => l_transaction_qty,
806                                 from_unit => l_transaction_uom,
807                                 to_unit => l_troldt_tbl(l_counter).secondary_uom_code,
808                                 from_name => NULL,
809                                 to_name => NULL
810                         );
811               IF (l_secondary_uom_qty = -99999) THEN
812                 IF (l_debug = 1) THEN
813                    DEBUG('Cannot convert uom to secondary uom', 'UPDATE TROLNDT ');
814                 END IF;
815                 fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
816                 fnd_message.set_token('VALUE1', l_troldt_tbl(l_counter).secondary_uom_code);
817                 fnd_message.set_token('VALUE2', l_transaction_uom);
818                 fnd_msg_pub.ADD;
819                 RAISE fnd_api.g_exc_unexpected_error;
820               END IF;
821 	      IF l_secondary_uom_qty < 0 THEN
822 	         l_secondary_uom_qty := 0;
823 	      END IF;
824               l_troldt_tbl(l_counter).secondary_transaction_quantity  := l_secondary_uom_qty;
825             END IF;
826             -- END
827 
828             l_rsv_quantity                                := l_quantity_cancel;
829             inv_mo_line_detail_util.update_row(x_return_status => l_return_status, p_mo_line_detail_rec => l_troldt_tbl(l_counter));
830 
831             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
832               RAISE fnd_api.g_exc_unexpected_error;
833             ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
834               RAISE fnd_api.g_exc_error;
835             END IF;
836 
837             l_quantity_cancel                             := 0;
838           END IF;
839 
840           IF (p_move_order_type = 3) THEN
841             l_to_rsv_rec.detailed_quantity  := l_to_rsv_rec.detailed_quantity - l_rsv_quantity;
842 
843 --INVCONV - Make sure Qty2 are NULL if nor present
844         IF (  l_to_rsv_rec.secondary_uom_code IS NULL ) THEN
845               l_to_rsv_rec.secondary_reservation_quantity := NULL;
846               l_to_rsv_rec.secondary_detailed_quantity    := NULL;
847         END IF;
848             inv_reservation_pub.update_reservation(
849               p_api_version_number         => 1.0
850             , x_return_status              => l_return_status
851             , x_msg_count                  => l_msg_count
852             , x_msg_data                   => l_msg_data
853             , p_original_rsv_rec           => l_rsv_temp_rec
854             , p_to_rsv_rec                 => l_to_rsv_rec
855             , p_original_serial_number     => l_dummy_sn
856             , p_to_serial_number           => l_dummy_sn
857             , p_validation_flag            => fnd_api.g_true
858             );
859 
860             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
861               RAISE fnd_api.g_exc_unexpected_error;
862             ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
863               RAISE fnd_api.g_exc_error;
864             END IF;
865           END IF;
866 
867           IF (l_quantity_cancel <= 0) THEN
868             EXIT;
869           END IF;
870         ELSIF (l_trolin_rec.line_status <> l_old_trolin_rec.line_status
871                AND l_trolin_rec.line_status = 6
872               ) THEN
873           --l_trolin_rec.return_status := l_return_status;
874           IF (l_debug = 1) THEN
875              DEBUG('change status', 'UPDATE TROLNDT');
876           END IF;
877 
878           IF (p_move_order_type = 3) THEN
879             l_to_rsv_rec                    := l_mtl_reservation(1);
880             l_rsv_temp_rec                  := l_mtl_reservation(1);
881             l_to_rsv_rec.detailed_quantity  := l_to_rsv_rec.detailed_quantity - l_troldt_tbl(l_counter).transaction_quantity;
882 
883 --INVCONV - Make sure Qty2 are NULL if nor present
884         IF (  l_to_rsv_rec.secondary_uom_code IS NULL ) THEN
885               l_to_rsv_rec.secondary_reservation_quantity := NULL;
886               l_to_rsv_rec.secondary_detailed_quantity    := NULL;
887         END IF;
888             inv_reservation_pub.update_reservation(
889               p_api_version_number         => 1.0
890             , x_return_status              => l_return_status
891             , x_msg_count                  => x_msg_count
892             , x_msg_data                   => x_msg_data
893             , p_original_rsv_rec           => l_rsv_temp_rec
894             , p_to_rsv_rec                 => l_to_rsv_rec
895             , p_original_serial_number     => l_dummy_sn
896             , p_to_serial_number           => l_dummy_sn
897             , p_validation_flag            => fnd_api.g_true
898             );
899 
900             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
901               RAISE fnd_api.g_exc_unexpected_error;
902             ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
903               RAISE fnd_api.g_exc_error;
904             END IF;
905           END IF;
906 
907           BEGIN
908             inv_replenish_detail_pub.clear_record(
909                                               p_trx_tmp_id => l_troldt_tbl(l_counter).transaction_temp_id,
910                                               p_success => l_success);
911             if( not l_success ) then
912               raise FND_API.G_EXC_ERROR;
913             end if;
914           EXCEPTION
915             WHEN OTHERS THEN
916               RAISE fnd_api.g_exc_unexpected_error;
917           END;
918           inv_mo_line_detail_util.delete_row(x_return_status => l_return_status, p_line_id => l_trolin_rec.line_id, p_line_detail_id => l_troldt_tbl(l_counter).transaction_temp_id);
919 
920           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
921             RAISE fnd_api.g_exc_unexpected_error;
922           ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
923             RAISE fnd_api.g_exc_error;
924           END IF;
925         END IF;
926       END LOOP;
927     END IF;
928 
929     x_trolin_rec      := l_trolin_rec;
930   EXCEPTION
931     WHEN fnd_api.g_exc_error THEN
932       RAISE;
933     WHEN fnd_api.g_exc_unexpected_error THEN
934       RAISE;
935     WHEN OTHERS THEN
936       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
937         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Trolins');
938       END IF;
939 
940       RAISE fnd_api.g_exc_unexpected_error;
941   END update_troldt;
942 
943   PROCEDURE trohdr(
944     p_validation_level IN     NUMBER
945   , p_control_rec      IN     inv_globals.control_rec_type
946   , p_trohdr_rec       IN     inv_move_order_pub.trohdr_rec_type
947   , p_trohdr_val_rec   IN     inv_move_order_pub.trohdr_val_rec_type
948   , p_old_trohdr_rec   IN     inv_move_order_pub.trohdr_rec_type
949   , x_trohdr_rec       IN OUT    NOCOPY inv_move_order_pub.trohdr_rec_type
950   , x_old_trohdr_rec   IN OUT    NOCOPY inv_move_order_pub.trohdr_rec_type
951   ) IS
952     l_return_status  VARCHAR2(1);
953     l_control_rec    inv_globals.control_rec_type;
954     l_trohdr_rec     inv_move_order_pub.trohdr_rec_type := p_trohdr_rec;
955 --Bug# 4554438, this var will be used to avoid NOCOPY issues
956     l_tmp_trohdr_rec     inv_move_order_pub.trohdr_rec_type;
957     l_old_trohdr_rec inv_move_order_pub.trohdr_rec_type := p_old_trohdr_rec;
958     l_trohdr_val_rec inv_move_order_pub.trohdr_val_rec_type;
959 
960   BEGIN
961 
962 
963     --  Load API control record
964     l_control_rec               := inv_globals.init_control_rec(p_operation => l_trohdr_rec.operation, p_control_rec => p_control_rec);
965     --  Set record return status.
966     l_trohdr_rec.return_status  := fnd_api.g_ret_sts_success;
967 
968     --  Prepare record.
969     IF l_trohdr_rec.operation = inv_globals.g_opr_create THEN
970       l_trohdr_rec.db_flag  := fnd_api.g_false;
971       --  Set missing old record elements to NULL.
972       l_old_trohdr_rec      := inv_trohdr_util.convert_miss_to_null(l_old_trohdr_rec);
973     ELSIF l_trohdr_rec.operation = inv_globals.g_opr_update
974           OR l_trohdr_rec.operation = inv_globals.g_opr_delete THEN
975       l_trohdr_rec.db_flag  := fnd_api.g_true;
976 
977       --  Query Old if missing
978       IF l_old_trohdr_rec.header_id = fnd_api.g_miss_num THEN
979         l_old_trohdr_rec  := inv_trohdr_util.query_row(p_header_id => l_trohdr_rec.header_id);
980       ELSE
981         --  Set missing old record elements to NULL.
982         l_old_trohdr_rec  := inv_trohdr_util.convert_miss_to_null(l_old_trohdr_rec);
983       END IF;
984 
985       --  Complete new record from old
986       l_trohdr_rec          := inv_trohdr_util.complete_record(p_trohdr_rec => l_trohdr_rec, p_old_trohdr_rec => l_old_trohdr_rec);
987     END IF;
988 
989    	--Bug 4756455 (11.5.10 bug 4755172 )
990 	--Reverted change of Bug 4329971 here and removed the code
991 
992     IF (l_trohdr_rec.operation = inv_globals.g_opr_update
993         OR l_trohdr_rec.operation = inv_globals.g_opr_create
994         OR l_trohdr_rec.operation = inv_globals.g_opr_delete)
995     THEN
996       --  Attribute level validation.
997       IF l_control_rec.default_attributes
998          OR l_control_rec.change_attributes THEN
999         --  Default missing attributes
1000 
1001 --Bug# 4554438.Start
1002 -- l_tmp_trohdr_rec is passed as there is NOCOPY for x_trohdr_rec
1003 --        inv_default_trohdr.ATTRIBUTES(p_trohdr_rec => l_trohdr_rec, x_trohdr_rec => l_trohdr_rec);
1004         inv_default_trohdr.ATTRIBUTES(p_trohdr_rec => l_trohdr_rec, x_trohdr_rec => l_tmp_trohdr_rec);
1005         l_trohdr_rec := l_tmp_trohdr_rec;
1006 
1007 --Bug# 4554438.End
1008 
1009         IF p_validation_level > fnd_api.g_valid_level_none THEN
1010           -- Bug#2536932: Setting Missing elements of P_TROHDR_VAL_REC to NULL values.
1011           l_trohdr_val_rec := inv_trohdr_util.convert_miss_to_null(p_trohdr_val_rec);
1012           inv_validate_trohdr.ATTRIBUTES(x_return_status => l_return_status, p_trohdr_rec => l_trohdr_rec, p_trohdr_val_rec => l_trohdr_val_rec, p_old_trohdr_rec => l_old_trohdr_rec);
1013 
1014           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1015             RAISE fnd_api.g_exc_unexpected_error;
1016           ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1017             RAISE fnd_api.g_exc_error;
1018           END IF;
1019         END IF;
1020       END IF;
1021 
1022       --  Clear dependent attributes.
1023 
1024       IF l_control_rec.change_attributes THEN
1025         inv_trohdr_util.clear_dependent_attr(p_trohdr_rec => l_trohdr_rec, p_old_trohdr_rec => l_old_trohdr_rec, x_trohdr_rec => l_trohdr_rec);
1026       END IF;
1027 
1028       --  Apply attribute changes
1029       IF l_control_rec.default_attributes
1030          OR l_control_rec.change_attributes THEN
1031         --debug('Apply Attr');
1032         inv_trohdr_util.apply_attribute_changes(p_trohdr_rec => l_trohdr_rec, p_old_trohdr_rec => l_old_trohdr_rec, x_trohdr_rec => l_trohdr_rec);
1033       END IF;
1034 
1035       --  Entity level validation.
1036       IF l_control_rec.validate_entity THEN
1037         IF l_trohdr_rec.operation = inv_globals.g_opr_delete THEN
1038           inv_validate_trohdr.entity_delete(x_return_status => l_return_status, p_trohdr_rec => l_trohdr_rec);
1039         ELSE
1040           inv_validate_trohdr.entity(x_return_status => l_return_status, p_trohdr_rec => l_trohdr_rec, p_old_trohdr_rec => l_old_trohdr_rec);
1041         END IF;
1042 
1043         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1044           RAISE fnd_api.g_exc_unexpected_error;
1045         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1046           RAISE fnd_api.g_exc_error;
1047         END IF;
1048       END IF;
1049 
1050       --  Step 4. Write to DB
1051       IF l_control_rec.write_to_db THEN
1052         IF l_trohdr_rec.operation = inv_globals.g_opr_delete THEN
1053           inv_trohdr_util.delete_row(p_header_id => l_trohdr_rec.header_id);
1054         ELSE
1055           --  Get Who Information
1056           l_trohdr_rec.last_update_date   := SYSDATE;
1057           l_trohdr_rec.last_updated_by    := fnd_global.user_id;
1058           l_trohdr_rec.last_update_login  := fnd_global.login_id;
1059 
1060           IF l_trohdr_rec.operation = inv_globals.g_opr_update THEN
1061             inv_trohdr_util.update_row(l_trohdr_rec);
1062           ELSIF l_trohdr_rec.operation = inv_globals.g_opr_create THEN
1063             l_trohdr_rec.creation_date  := SYSDATE;
1064             l_trohdr_rec.created_by     := fnd_global.user_id;
1065             inv_trohdr_util.insert_row(l_trohdr_rec);
1066           END IF;
1067         END IF;
1068       END IF;
1069     END IF;
1070 
1071     --  Load OUT parameters
1072     --debug(l_trohdr_rec.header_id);
1073     x_trohdr_rec                := l_trohdr_rec;
1074     --debug(l_trohdr_rec.header_id);
1075     x_old_trohdr_rec            := l_old_trohdr_rec;
1076   EXCEPTION
1077     WHEN fnd_api.g_exc_error THEN
1078       l_trohdr_rec.return_status  := fnd_api.g_ret_sts_error;
1079       x_trohdr_rec                := l_trohdr_rec;
1080       x_old_trohdr_rec            := l_old_trohdr_rec;
1081       RAISE;
1082     WHEN fnd_api.g_exc_unexpected_error THEN
1083       l_trohdr_rec.return_status  := fnd_api.g_ret_sts_unexp_error;
1084       x_trohdr_rec                := l_trohdr_rec;
1085       x_old_trohdr_rec            := l_old_trohdr_rec;
1086       RAISE;
1087     WHEN OTHERS THEN
1088       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1089         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Trohdr');
1090       END IF;
1091 
1092       l_trohdr_rec.return_status  := fnd_api.g_ret_sts_unexp_error;
1093       x_trohdr_rec                := l_trohdr_rec;
1094       x_old_trohdr_rec            := l_old_trohdr_rec;
1095       RAISE fnd_api.g_exc_unexpected_error;
1096   END trohdr;
1097 
1098   --  Trolins
1099 
1100   PROCEDURE trolins(
1101     p_validation_level IN        NUMBER
1102   , p_control_rec      IN        inv_globals.control_rec_type
1103   , p_trolin_tbl       IN        inv_move_order_pub.trolin_tbl_type
1104   , p_trolin_val_tbl   IN        inv_move_order_pub.trolin_val_tbl_type
1105   , p_old_trolin_tbl   IN        inv_move_order_pub.trolin_tbl_type
1106   , p_move_order_type  IN        NUMBER
1107   , x_trolin_tbl       IN OUT    NOCOPY inv_move_order_pub.trolin_tbl_type
1108   , x_old_trolin_tbl   IN OUT    NOCOPY inv_move_order_pub.trolin_tbl_type
1109   , p_delete_mmtt      IN        VARCHAR2 DEFAULT 'YES' --Added bug3524130
1110   ) IS
1111     l_return_status         VARCHAR2(1);
1112     l_control_rec           inv_globals.control_rec_type;
1113     l_trolin_rec            inv_move_order_pub.trolin_rec_type;
1114     --Bug #4347016
1115     l_tmp_trolin_rec        inv_move_order_pub.trolin_rec_type;
1116     l_trolin_val_rec        inv_move_order_pub.trolin_val_rec_type;
1117     l_trolin_tbl            inv_move_order_pub.trolin_tbl_type;
1118     l_old_trolin_rec        inv_move_order_pub.trolin_rec_type;
1119     l_old_trolin_tbl        inv_move_order_pub.trolin_tbl_type;
1120     l_troldt_tbl            inv_mo_line_detail_util.g_mmtt_tbl_type;
1121     l_quantity_cancel       NUMBER;
1122     l_mtl_reservations      inv_reservation_global.mtl_reservation_tbl_type;
1123     l_mtl_reservation_count NUMBER;
1124     l_rsv_temp_rec          inv_reservation_global.mtl_reservation_rec_type;
1125     l_to_rsv_rec            inv_reservation_global.mtl_reservation_rec_type;
1126     l_msg_count             NUMBER;
1127     l_msg_data              VARCHAR2(2000);
1128     l_move_order_type       NUMBER;
1129     l_trohdr_rec            inv_move_order_pub.trohdr_rec_type;
1130     l_failed_ship_set_id    NUMBER   := NULL;
1131     l_marked_failed_shipset BOOLEAN  := FALSE;
1132     l_count                 NUMBER;
1133     l_current_ship_set_id   NUMBER   := NULL;
1134     l_debug number;
1135     l_cur_mfg_org_id        NUMBER;  --Bug #5204255
1136     l_cur_line_org_id       NUMBER;
1137   BEGIN
1138     -- Use cache to get value for l_debug
1139     IF g_is_pickrelease_set IS NULL THEN
1140        g_is_pickrelease_set := 2;
1141        IF INV_CACHE.is_pickrelease THEN
1142           g_is_pickrelease_set := 1;
1143        END IF;
1144     END IF;
1145     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
1146        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1147     END IF;
1148 
1149     l_debug := g_debug;
1150 
1151     --  Init local table variables.
1152     l_trolin_tbl      := p_trolin_tbl;
1153     l_old_trolin_tbl  := p_old_trolin_tbl;
1154 
1155     FOR i IN 1 .. l_trolin_tbl.COUNT LOOP
1156       BEGIN
1157         --  Load local records.
1158         l_trolin_rec                := l_trolin_tbl(i);
1159 
1160 	/* Fix for Bug# 3409435. Calling the INV_Validate_Trolin.init procedure
1161 	   to initialize the global variables used in  inv_validate_trolin package */
1162 	   INV_Validate_Trolin.init;
1163 
1164         /* Bug 2504964: Tracking Ship Set : Code Starts */
1165         IF  l_trolin_rec.ship_set_id IS NOT NULL
1166             AND l_trolin_rec.ship_set_id <> NVL(l_current_ship_set_id, -99) THEN
1167           l_current_ship_set_id    := l_trolin_rec.ship_set_id;
1168           SAVEPOINT MO_SHIPSET;
1169           l_marked_failed_shipset  := FALSE;
1170           l_failed_ship_set_id     := NULL;
1171         END IF;
1172 
1173         IF NVL(l_trolin_rec.ship_set_id, -99) = NVL(l_failed_ship_set_id, -999) THEN
1174           DEBUG('MO Line failed due to Ship Set validation: ' || to_char(l_trolin_rec.line_id),  'Inv_Transfer_Order_PVT.Trolins');
1175           RAISE fnd_api.g_exc_error;
1176         END IF;
1177 
1178         /* Bug 2504964: Tracking Ship Set : Code Ends */
1179 
1180         IF p_trolin_val_tbl.EXISTS(i) THEN
1181           l_trolin_val_rec  := p_trolin_val_tbl(i);
1182           -- Bug#2536932: Setting Missing Values as NULL.
1183           l_trolin_val_rec  := inv_trolin_util.convert_miss_to_null(l_trolin_val_rec);
1184         ELSE
1185           l_trolin_val_rec  := NULL;
1186         END IF;
1187 
1188         IF l_old_trolin_tbl.EXISTS(i) THEN
1189           l_old_trolin_rec  := l_old_trolin_tbl(i);
1190         ELSE
1191           l_old_trolin_rec  := inv_move_order_pub.g_miss_trolin_rec;
1192         END IF;
1193 
1194         --  Load API control record
1195         l_control_rec               := inv_globals.init_control_rec(p_operation => l_trolin_rec.operation, p_control_rec => p_control_rec);
1196               -- load header information record
1197               --l_trohdr_rec := inv_trohdr_util.query_row(l_trolin_tbl(I).header_id);
1198         --l_move_order_type :=  l_trohdr_rec.move_order_type;
1199 
1200         --  Set record return status.
1201         l_trolin_rec.return_status  := fnd_api.g_ret_sts_success;
1202         --  Prepare record.
1203         IF (l_debug = 1) THEN
1204            DEBUG('Trolin operation:'|| l_trolin_rec.operation, 'Inv_Transfer_Order_PVT.Trolins');
1205         END IF;
1206 
1207         IF l_trolin_rec.operation = inv_globals.g_opr_create THEN
1208           l_trolin_rec.db_flag  := fnd_api.g_false;
1209           --  Set missing old record elements to NULL.
1210           --debug('Trolin convert');
1211           IF (l_debug = 1) THEN
1212              DEBUG('Trolin convert', 'Inv_Transfer_Order_PVT.Trolins');
1213           END IF;
1214           l_old_trolin_rec      := inv_trolin_util.convert_miss_to_null(l_old_trolin_rec);
1215         ELSIF l_trolin_rec.operation = inv_globals.g_opr_update
1216               OR l_trolin_rec.operation = inv_globals.g_opr_delete THEN
1217           l_trolin_rec.db_flag  := fnd_api.g_true;
1218 
1219           --  Query Old if missing
1220           IF l_old_trolin_rec.line_id = fnd_api.g_miss_num THEN
1221             --debug('Trolin line_id miss num');
1222             IF (l_debug = 1) THEN
1223                DEBUG('Trolin line_id miss num', 'Inv_Transfer_Order_PVT.Trolins');
1224             END IF;
1225             l_old_trolin_rec  := inv_trolin_util.query_row(p_line_id => l_trolin_rec.line_id);
1226           ELSE
1227             --  Set missing old record elements to NULL.
1228             l_old_trolin_rec  := inv_trolin_util.convert_miss_to_null(l_old_trolin_rec);
1229           END IF;
1230 
1231           --  Complete new record from old
1232           l_trolin_rec          := inv_trolin_util.complete_record(p_trolin_rec => l_trolin_rec, p_old_trolin_rec => l_old_trolin_rec);
1233         END IF;
1234 
1235 	      --Bug 4756455 (11.5.10 bug 4755172 )
1236 	      --Reverted change of Bug 4329971here and removed the code
1237 
1238 	      -- bug 4662395 set the profile mfg_organization_id so
1239         -- the call to PJM_PROJECTS_V will return data.
1240         -- The call to PJM_PROJECTS_V is made indirectly through
1241         -- inv_default_trolin.ATTRIBUTES and inv_validate_trolin.ATTRIBUTES
1242         -- both of which can be found in the next few lines of code.
1243 
1244         --Bug #5204255
1245         --We should set the profile value only if it is NULL/G_MISS_NUM
1246         --and the current line's organization_id is not NULL/G_MISS_NUM
1247         l_cur_mfg_org_id  := TO_NUMBER(FND_PROFILE.VALUE('MFG_ORGANIZATION_ID'));
1248         l_cur_line_org_id := l_trolin_rec.organization_id;
1249 
1250         IF ( ( (l_cur_mfg_org_id IS NULL) OR
1251                (l_cur_mfg_org_id IS NOT NULL AND l_cur_mfg_org_id = FND_API.G_MISS_NUM)
1252              ) AND
1253              (l_cur_line_org_id IS NOT NULL AND l_cur_line_org_id <> FND_API.G_MISS_NUM)
1254            ) THEN
1255           FND_PROFILE.put('MFG_ORGANIZATION_ID',l_trolin_rec.organization_id);
1256         END IF;
1257 
1258         --  Attribute level validation.
1259         IF l_control_rec.default_attributes
1260            OR l_control_rec.change_attributes THEN
1261                 --  Default missing attributes
1262           --debug('default missing attributes');
1263           IF (l_debug = 1) THEN
1264              DEBUG('default missing attributes', 'Inv_Transfer_Order_PVT.Trolins');
1265           END IF;
1266 
1267           --Bug #4347016 - Creating a temp variable to store output record
1268           --as there is a NOCOPY in inv_default_trolin.ATTRIBUTES
1269           inv_default_trolin.ATTRIBUTES(
1270                       p_trolin_rec => l_trolin_rec
1271                     , x_trolin_rec => l_tmp_trolin_rec);
1272 
1273           l_trolin_rec := l_tmp_trolin_rec;
1274 
1275           IF p_validation_level > fnd_api.g_valid_level_none THEN
1276       inv_validate_trolin.ATTRIBUTES(x_return_status => l_return_status, p_trolin_rec => l_trolin_rec, p_trolin_val_rec => l_trolin_val_rec, p_old_trolin_rec => l_old_trolin_rec);
1277 
1278             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1279               --debug('error in validate_attributes');
1280               IF (l_debug = 1) THEN
1281                 DEBUG('error in validate_attributes', 'Inv_Transfer_Order_PVT.Trolins');
1282               END IF;
1283               RAISE fnd_api.g_exc_unexpected_error;
1284             ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1285               IF (l_debug = 1) THEN
1286                 DEBUG('exc error in validate_attributes', 'Inv_Transfer_Order_PVT.Trolins');
1287               END IF;
1288               RAISE fnd_api.g_exc_error;
1289             END IF;
1290           END IF;
1291         END IF;
1292 
1293         --debug(l_trolin_rec.line_id);
1294             --  Clear dependent attributes.
1295         IF l_control_rec.change_attributes THEN
1296           --debug('calling clear dependent attr');
1297           IF (l_debug = 1) THEN
1298             DEBUG('calling clear dependent attr', 'Inv_Transfer_Order_PVT.Trolins');
1299           END IF;
1300           inv_trolin_util.clear_dependent_attr(p_trolin_rec => l_trolin_rec, p_old_trolin_rec => l_old_trolin_rec, x_trolin_rec => l_trolin_rec);
1301         --debug('after calling dependent attr');
1302         END IF;
1303 
1304         --  Apply attribute changes
1305         IF l_control_rec.default_attributes
1306            OR l_control_rec.change_attributes THEN
1307           --debug('Trolin Apply attr');
1308           IF (l_debug = 1) THEN
1309             DEBUG('Trolin Apply attr',  'Inv_Transfer_Order_PVT.Trolins');
1310           END IF;
1311           inv_trolin_util.apply_attribute_changes(p_trolin_rec => l_trolin_rec, p_old_trolin_rec => l_old_trolin_rec, x_trolin_rec => l_trolin_rec);
1312         END IF;
1313 
1314         --debug('AFter Trolin APply attr');
1315 
1316         --  Entity level validation.
1317         IF l_control_rec.validate_entity THEN
1318           IF l_trolin_rec.operation = inv_globals.g_opr_delete THEN
1319             inv_validate_trolin.entity_delete(x_return_status => l_return_status, p_trolin_rec => l_trolin_rec);
1320           ELSE
1321             --debug('Trolin Validate Entity');
1322             IF (l_debug = 1) THEN
1323               DEBUG('Trolin Validate Entity', 'Inv_Transfer_Order_PVT.Trolins');
1324             END IF;
1325             inv_validate_trolin.entity(x_return_status => l_return_status, p_trolin_rec => l_trolin_rec, p_old_trolin_rec => l_old_trolin_rec, p_move_order_type => p_move_order_type);
1326           END IF;
1327 
1328           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1329             l_trolin_rec.return_status  := l_return_status;
1330             RAISE fnd_api.g_exc_unexpected_error;
1331           ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1332             l_trolin_rec.return_status  := l_return_status;
1333             IF (l_debug = 1) THEN
1334               DEBUG('Error from Trolin Validate Entity', 'Inv_Transfer_Order_PVT.Trolins');
1335             END IF;
1336             RAISE fnd_api.g_exc_error;
1337           END IF;
1338         END IF;
1339 
1340                 --debug('AFter Trolin Validate Entity');
1341         -- populate primary quantity
1342         IF (l_trolin_rec.inventory_item_id IS NOT NULL
1343             AND l_trolin_rec.quantity IS NOT NULL
1344             AND l_trolin_rec.uom_code IS NOT NULL
1345            ) THEN
1346 
1347            --Bug3467711. The quantity field should should have a precision
1348            --of 5 irrespective of the value passed.
1349 	  /*6971965: Adding if condition for WIP generated MO*/
1350 	  IF (l_debug = 1) THEN
1351             DEBUG('Transaction_type_id: '||to_char(l_trolin_rec.transaction_type_id),'Inv_Transfer_Order_PVT.Trolins');
1352           END IF;
1353 	  IF l_trolin_rec.transaction_type_id = 35 THEN
1354             l_trolin_rec.quantity          := ROUND(l_trolin_rec.quantity , 6);
1355           ELSE
1356 	    l_trolin_rec.quantity          := ROUND(l_trolin_rec.quantity , 5);
1357           END IF;
1358           /*l_trolin_rec.quantity          := ROUND(l_trolin_rec.quantity , 5);*/
1359           l_trolin_rec.primary_quantity  := get_primary_quantity(p_item_id => l_trolin_rec.inventory_item_id, p_organization_id => l_trolin_rec.organization_id, p_from_quantity => l_trolin_rec.quantity, p_from_unit => l_trolin_rec.uom_code);
1360           -- nsinghi bug#5724815 need to populate the secondary qty too for dual items.
1361           -- START
1362           IF l_old_trolin_rec.secondary_uom IS NOT NULL THEN
1363             l_trolin_rec.secondary_quantity := inv_convert.inv_um_convert
1364                       (       item_id => l_trolin_rec.inventory_item_id,
1365                               PRECISION => NULL,
1366                               from_quantity => l_trolin_rec.quantity,
1367                               from_unit => l_trolin_rec.uom_code,
1368                               to_unit => l_old_trolin_rec.secondary_uom,
1369                               from_name => NULL,
1370                               to_name => NULL
1371                       );
1372 
1373             IF (l_trolin_rec.secondary_quantity = -99999) THEN
1374               IF (l_debug = 1) THEN
1375                  DEBUG('Cannot convert uom to secondary uom', 'Inv_Transfer_Order_PVT.Trolins');
1376               END IF;
1377               fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
1378               fnd_message.set_token('VALUE1', l_old_trolin_rec.secondary_uom);
1379               fnd_message.set_token('VALUE2', l_trolin_rec.uom_code);
1380               fnd_msg_pub.ADD;
1381               RAISE fnd_api.g_exc_unexpected_error;
1382             END IF;
1383           END IF;
1384           -- END
1385 
1386           IF (l_debug = 1) THEN
1387             DEBUG('Populated primary quantity: ' || to_char(l_trolin_rec.primary_quantity),  'Inv_Transfer_Order_PVT.Trolins');
1388           END IF;
1389         END IF;
1390 
1391         --  Step 4. Write to DB
1392         IF l_control_rec.write_to_db THEN
1393           IF l_trolin_rec.operation = inv_globals.g_opr_delete THEN
1394             --debug('Calling inv_mold_query_rows');
1395             IF (l_debug = 1) THEN
1396               DEBUG('Calling inv_mold_query_rows for Line Id: ' || to_char(l_trolin_rec.line_id), 'Inv_Transfer_Order_PVT.Trolins');
1397             END IF;
1398             l_troldt_tbl  := inv_mo_line_detail_util.query_rows(p_line_id => l_trolin_rec.line_id);
1399 
1400             IF (l_troldt_tbl.COUNT > 0) THEN
1401               IF (l_debug = 1) THEN
1402                  DEBUG('Calling Delete_Troldt', 'Inv_Transfer_Order_PVT.Trolins');
1403               END IF;
1404               delete_troldt(x_return_status => l_return_status, x_msg_data => l_msg_data, x_msg_count => l_msg_count, p_troldt_tbl => l_troldt_tbl, p_move_order_type => l_move_order_type);
1405 
1406               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1407                 l_trolin_rec.return_status  := l_return_status;
1408                 RAISE fnd_api.g_exc_unexpected_error;
1409               ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1410                 l_trolin_rec.return_status  := l_return_status;
1411                 RAISE fnd_api.g_exc_error;
1412               END IF;
1413             END IF;
1414 
1415             inv_trolin_util.delete_row(p_line_id => l_trolin_rec.line_id);
1416           ELSIF l_trolin_rec.operation = inv_globals.g_opr_update THEN
1417             --  Get Who Information
1418 	    -- Bug 3030538. Creation_date becomes null when both an update and
1419 	    -- create are done in move orders form
1420 
1421             l_trolin_rec.creation_date      := l_old_trolin_rec.creation_date;
1422 	    l_trolin_rec.created_by         := l_old_trolin_rec.created_by;
1423             l_trolin_rec.last_update_date   := SYSDATE;
1424             l_trolin_rec.last_updated_by    := fnd_global.user_id;
1425             l_trolin_rec.last_update_login  := fnd_global.login_id;
1426             IF (l_debug = 1) THEN
1427                DEBUG(l_trolin_rec.quantity, 'Inv_Transfer_Order_PVT.Trolins');
1428                DEBUG(l_old_trolin_rec.quantity, 'Inv_Transfer_Order_PVT.Trolins');
1429                DEBUG(l_trolin_rec.quantity_detailed, 'Inv_Transfer_Order_PVT.Trolins');
1430                DEBUG(l_old_trolin_rec.quantity_detailed, 'Inv_Transfer_Order_PVT.Trolins');
1431                DEBUG(l_trolin_rec.line_status, 'Inv_Transfer_Order_PVT.Trolins');
1432                DEBUG(l_old_trolin_rec.line_status, 'Inv_Transfer_Order_PVT.Trolins');
1433                DEBUG('l_trolin_Rec.required_quantity'|| l_trolin_rec.required_quantity, 'Inv_Transfer_Order_PVT.Trolins');
1434                DEBUG('l_old_trolin_Rec.required_quantity'|| l_old_trolin_rec.required_quantity, 'Inv_Transfer_Order_PVT.Trolins');
1435             END IF;
1436 
1437             IF l_old_trolin_rec.required_quantity IS NOT NULL THEN
1438               l_trolin_rec.required_quantity  := l_old_trolin_rec.required_quantity;
1439             END IF;
1440 
1441             IF (l_old_trolin_rec.quantity <> l_trolin_rec.quantity
1442                 OR l_old_trolin_rec.line_status <> l_trolin_rec.line_status
1443                 OR l_old_trolin_rec.quantity_detailed <> l_trolin_rec.quantity_detailed
1444                ) THEN
1445               IF (l_debug = 1) THEN
1446                  DEBUG('calling mold query rows', 'Inv_Transfer_Order_PVT.Trolins');
1447               END IF;
1448               l_troldt_tbl  := inv_mo_line_detail_util.query_rows(p_line_id => l_trolin_rec.line_id);
1449               IF (l_debug = 1) THEN
1450                  DEBUG('after mold query rows', 'Inv_Transfer_Order_PVT.Trolins');
1451               END IF;
1452 
1453               IF (l_troldt_tbl.COUNT > 0) THEN
1454                 IF (l_debug = 1) THEN
1455                    DEBUG('calling update troldt', 'Inv_Transfer_Order_PVT.Trolins');
1456                 END IF;
1457                 update_troldt(
1458                   x_return_status              => l_return_status
1459                 , x_msg_data                   => l_msg_data
1460                 , x_msg_count                  => l_msg_count
1461                 , p_trolin_rec                 => l_trolin_rec
1462                 , p_old_trolin_rec             => l_old_trolin_rec
1463                 , p_troldt_tbl                 => l_troldt_tbl
1464                 , p_move_order_type            => p_move_order_type
1465                 , x_trolin_rec                 => l_trolin_rec
1466 		, p_delete_mmtt                => p_delete_mmtt  --Added bug 3524130
1467                 );
1468 
1469                 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1470                   IF (l_debug = 1) THEN
1471                      DEBUG('got error', 'Inv_Transfer_Order_PVT.Trolins');
1472                   END IF;
1473                   l_trolin_rec.return_status  := l_return_status;
1474                   RAISE fnd_api.g_exc_unexpected_error;
1475                 ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1476                   l_trolin_rec.return_status  := l_return_status;
1477                   RAISE fnd_api.g_exc_error;
1478                 END IF;
1479               END IF;
1480             END IF;
1481 
1482             IF (l_debug = 1) THEN
1483                DEBUG('calling update row', 'Inv_Transfer_Order_PVT.Trolins');
1484                DEBUG(l_trolin_rec.line_id, 'Inv_Transfer_Order_PVT.Trolins');
1485             END IF;
1486             inv_trolin_util.update_row(l_trolin_rec);
1487             IF (l_debug = 1) THEN
1488                DEBUG('after update row', 'Inv_Transfer_Order_PVT.Trolins');
1489             END IF;
1490           ELSIF l_trolin_rec.operation = inv_globals.g_opr_create THEN
1491             l_trolin_rec.creation_date      := SYSDATE;
1492             l_trolin_rec.created_by         := fnd_global.user_id;
1493             l_trolin_rec.last_update_date   := SYSDATE;
1494             l_trolin_rec.last_updated_by    := fnd_global.user_id;
1495             l_trolin_rec.last_update_login  := fnd_global.login_id;
1496             IF (l_debug = 1) THEN
1497                DEBUG('create row', 'Inv_Transfer_Order_PVT.Trolins');
1498             END IF;
1499             /*Bug#5764123. Added the below line to convert the MISSING data
1500               to NULL.*/
1501             l_trolin_rec := inv_trolin_util.convert_miss_to_null(l_trolin_rec);
1502             inv_trolin_util.insert_row(l_trolin_rec);
1503           END IF;
1504         END IF;
1505 
1506         IF (l_debug = 1) THEN
1507            DEBUG('AFter Trolin Write to db', 'Inv_Transfer_Order_PVT.Trolins');
1508         END IF;
1509         --  Load tables.
1510         l_trolin_tbl(i)             := l_trolin_rec;
1511         l_old_trolin_tbl(i)         := l_old_trolin_rec;
1512       --  For loop exception handler.
1513       EXCEPTION
1514         WHEN fnd_api.g_exc_error THEN
1515           l_trolin_rec.return_status  := fnd_api.g_ret_sts_error;
1516           l_trolin_tbl(i)             := l_trolin_rec;
1517           l_old_trolin_tbl(i)         := l_old_trolin_rec;
1518 
1519           /* Bug 2504964: One of the MO Lines in the ShipSet failed.
1520              Mark all the MO Lines belonging to that ShipSet as Failed.
1521              Note this is not done again for the same ShipSet later. All are done atonce*/
1522           IF  l_trolin_rec.ship_set_id IS NOT NULL AND NOT l_marked_failed_shipset THEN
1523             l_failed_ship_set_id     := l_trolin_rec.ship_set_id;
1524             ROLLBACK TO MO_SHIPSET;
1525             IF (l_debug = 1) THEN
1526               DEBUG('MO Lines in ShipSet failure', 'Inv_Transfer_Order_PVT.Trolins');
1527             END IF;
1528 
1529             FOR l_count IN 1 .. l_trolin_tbl.COUNT LOOP
1530               IF l_trolin_tbl(l_count).ship_set_id = NVL(l_failed_ship_set_id, -99) THEN
1531                 l_trolin_tbl(l_count).return_status  := fnd_api.g_ret_sts_error;
1532               END IF;
1533             END LOOP;
1534 
1535             l_marked_failed_shipset  := TRUE;
1536           END IF;
1537 
1538 
1539 	  --Bug 4756455 (11.5.10 bug 4755172 )
1540 	  --Reverted change of Bug 4329971,4699505 here and removed the code
1541 
1542 
1543         WHEN fnd_api.g_exc_unexpected_error THEN
1544           l_trolin_rec.return_status  := fnd_api.g_ret_sts_unexp_error;
1545           l_trolin_tbl(i)             := l_trolin_rec;
1546           l_old_trolin_tbl(i)         := l_old_trolin_rec;
1547           RAISE fnd_api.g_exc_unexpected_error;
1548         WHEN OTHERS THEN
1549           l_trolin_rec.return_status  := fnd_api.g_ret_sts_unexp_error;
1550           l_trolin_tbl(i)             := l_trolin_rec;
1551           l_old_trolin_tbl(i)         := l_old_trolin_rec;
1552 
1553           IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1554             fnd_msg_pub.add_exc_msg(g_pkg_name, 'Trolins');
1555           END IF;
1556 
1557           RAISE fnd_api.g_exc_unexpected_error;
1558       END;
1559     END LOOP;
1560 
1561     --  Load OUT parameters
1562 
1563     x_trolin_tbl      := l_trolin_tbl;
1564     x_old_trolin_tbl  := l_old_trolin_tbl;
1565   EXCEPTION
1566     WHEN fnd_api.g_exc_error THEN
1567       RAISE;
1568     WHEN fnd_api.g_exc_unexpected_error THEN
1569       RAISE;
1570     WHEN OTHERS THEN
1571       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1572         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Trolins');
1573       END IF;
1574 
1575       RAISE fnd_api.g_exc_unexpected_error;
1576   END trolins;
1577 
1578   /*Procedure Get_Reservations(
1579       x_return_status OUT VARCHAR2,
1580       x_msg_count     OUT NUMBER,
1581       x_msg_data      OUT VARCHAR2,
1582       p_source_header_id  IN NUMBER,
1583       p_source_line_id    IN NUMBER,
1584       p_source_delivery_id        IN NUMBER,
1585       p_organization_id   IN NUMBER,
1586       p_inventory_item_id IN NUMBER,
1587       p_subinventory_code IN VARCHAR2 := FND_API.G_MISS_CHAR,
1588       p_locator_id        IN NUMBER := FND_API.G_MISS_NUM,
1589       p_revision          IN VARCHAR2 := FND_API.G_MISS_CHAR,
1590       p_lot_number        IN VARCHAR2 := FND_API.G_MISS_CHAR,
1591       p_serial_number     IN VARCHAR2 := FND_API.G_MISS_CHAR,
1592       x_mtl_reservation_tbl OUT INV_RESERVATION_GLOBAL.MTL_RESERVATION_TBL_TYPE
1593   )
1594   IS
1595       l_rsv_temp_rec INV_RESERVATION_GLOBAL.MTL_RESERVATION_REC_TYPE;
1596       l_mtl_reservation INV_RESERVATION_GLOBAL.MTL_RESERVATION_TBL_TYPE;
1597       l_mtl_reservation_count NUMBER;
1598       l_rsv_result_tbl INV_RESERVATION_GLOBAL.MTL_RESERVATION_TBL_TYPE;
1599   BEGIN
1600       l_rsv_temp_rec.demand_source_header_id := p_source_header_id;
1601       l_rsv_temp_Rec.demand_source_line_id := p_source_line_id;
1602       l_rsv_temp_rec.demand_source_delivery := p_source_delivery_id;
1603       l_rsv_temp_rec.inventory_item_id := p_inventory_item_id;
1604       l_rsv_temp_rec.organization_id := p_organization_id;
1605 
1606       INV_RESERVATION_PUB.Query_Reservation
1607       (
1608            p_api_version_number => 1.0,
1609            x_return_status => l_return_status,
1610            x_msg_count => x_msg_count,
1611            x_msg_data  => x_msg_data,
1612            p_query_input => l_rsv_temp_rec,
1613            x_mtl_reservation_tbl => l_mtl_reservation,
1614            x_mtl_reservation_tbl_count => l_mtl_reservation_count,
1615            x_error_code => l_error_code
1616        );*/
1617      /*  if( l_mtl_reservation_count > 0 ) then
1618            for l_count in 1 ..l_mtl_reservation_count LOOP
1619                   l_rsv_temp_rec := l_mtl_reservation(l_count);
1620                   if( l_mtl_reservation(l_count).subinventory_code is null ) then
1621                       if( l_mtl_reservation(l_count).locator_id is null) then
1622                           if(l_mtl_reservation(l_count).revision is null ) then
1623                              if( l_mtl_reservation(l_count).lot_number is null) then
1624                                   if( l_mtl_reservation(l_count).serial_number is null) then
1625                                       l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1626                                   elsif( p_serial_number <> FND_API.G_MISS_CHAR AND l_mtl_reservation(l_count).serial_number =p_serial_number) then
1627                                       l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1628                                   end if;
1629                              elsif( p_lot_number <> FND_API.G_MISS_CHAR AND l_mtl_reservation(l_count).lot_number =p_lot_number) then
1630                                   l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1631                              end if;
1632                           elsif( p_revision <> FND_API.G_MISS_CHAR AND l_mtl_reservation(l_count).revision =p_revision) then
1633                              l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1634                           end if;
1635                       elsif( p_locator_id <> FND_API.G_MISS_NUM AND l_mtl_reservation(l_count).locator_id =p_locator_id) then
1636                              l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1637                       end if;
1638                   elsif( p_subinventory_code <> FND_API.G_MISS_CHAR AND l_mtl_reservation(l_count).subinventory_code =p_subinventory_code) then
1639                       l_rsv_result_tbl(l_count) := l_mtl_reservation(l_count);
1640                   end if;
1641            end loop;
1642        end if;    */
1643   /*     x_mtl_reservation_tbl := l_rsv_result_tbl;
1644   EXCEPTION
1645 
1646       WHEN FND_API.G_EXC_ERROR THEN
1647           RAISE;
1648 
1649       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1650           RAISE;
1651 
1652       WHEN OTHERS THEN
1653           IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1654           THEN
1655               FND_MSG_PUB.Add_Exc_Msg
1656               (   G_PKG_NAME
1657               ,   'Trolins'
1658               );
1659           END IF;
1660           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1661   END Get_Reservations;*/
1662 
1663   --  Start of Comments
1664   --  API name    Process_Transfer_Order
1665   --  Type        Private
1666   --  Function
1667   --
1668   --  Pre-reqs
1669   --
1670   --  Parameters
1671   --
1672   --  Version     Current version = 1.0
1673   --              Initial version = 1.0
1674   --
1675   --  Notes
1676   --
1677   --  End of Comments
1678 
1679   PROCEDURE process_transfer_order(
1680     p_api_version_number IN     NUMBER
1681   , p_init_msg_list      IN     VARCHAR2 := fnd_api.g_false
1682   , p_commit             IN     VARCHAR2 := fnd_api.g_false
1683   , p_validation_level   IN     NUMBER := fnd_api.g_valid_level_full
1684   , p_control_rec        IN     inv_globals.control_rec_type := inv_globals.g_miss_control_rec
1685   , x_return_status      OUT    NOCOPY VARCHAR2
1686   , x_msg_count          OUT    NOCOPY NUMBER
1687   , x_msg_data           OUT    NOCOPY VARCHAR2
1688   , p_trohdr_rec         IN     inv_move_order_pub.trohdr_rec_type := inv_move_order_pub.g_miss_trohdr_rec
1689   , p_trohdr_val_rec     IN     inv_move_order_pub.trohdr_val_rec_type := inv_move_order_pub.g_miss_trohdr_val_rec
1690   , p_old_trohdr_rec     IN     inv_move_order_pub.trohdr_rec_type := inv_move_order_pub.g_miss_trohdr_rec
1691   , p_trolin_tbl         IN     inv_move_order_pub.trolin_tbl_type := inv_move_order_pub.g_miss_trolin_tbl
1692   , p_trolin_val_tbl     IN     inv_move_order_pub.trolin_val_tbl_type := inv_move_order_pub.g_miss_trolin_val_tbl
1693   , p_old_trolin_tbl     IN     inv_move_order_pub.trolin_tbl_type := inv_move_order_pub.g_miss_trolin_tbl
1694   , x_trohdr_rec         IN OUT    NOCOPY inv_move_order_pub.trohdr_rec_type
1695   , x_trolin_tbl         IN OUT    NOCOPY inv_move_order_pub.trolin_tbl_type
1696   , p_delete_mmtt        IN      VARCHAR2 DEFAULT 'YES' --Added bug3524130
1697   ) IS
1698     l_api_version_number CONSTANT NUMBER                             := 1.0;
1699     l_api_name           CONSTANT VARCHAR2(30)                       := 'Process_Transfer_Order';
1700     l_return_status               VARCHAR2(1);
1701     l_control_rec                 inv_globals.control_rec_type;
1702     l_trohdr_rec                  inv_move_order_pub.trohdr_rec_type := p_trohdr_rec;
1703     l_old_trohdr_rec              inv_move_order_pub.trohdr_rec_type := p_old_trohdr_rec;
1704     l_trolin_rec                  inv_move_order_pub.trolin_rec_type;
1705     l_trolin_tbl                  inv_move_order_pub.trolin_tbl_type;
1706     l_old_trolin_rec              inv_move_order_pub.trolin_rec_type;
1707     l_old_trolin_tbl              inv_move_order_pub.trolin_tbl_type;
1708     l_debug number;
1709   BEGIN
1710     --  Standard call to check for call compatibility
1711     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
1712       RAISE fnd_api.g_exc_unexpected_error;
1713     END IF;
1714 
1715     --  Initialize message list.
1716 
1717     IF fnd_api.to_boolean(p_init_msg_list) THEN
1718       fnd_msg_pub.initialize;
1719     END IF;
1720 
1721     -- Use cache to get value for l_debug
1722     IF g_is_pickrelease_set IS NULL THEN
1723        g_is_pickrelease_set := 2;
1724        IF INV_CACHE.is_pickrelease THEN
1725           g_is_pickrelease_set := 1;
1726        END IF;
1727     END IF;
1728     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
1729        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1730     END IF;
1731 
1732     l_debug := g_debug;
1733 
1734     --  Init local table variables.
1735     l_trolin_tbl      := p_trolin_tbl;
1736     l_old_trolin_tbl  := p_old_trolin_tbl;
1737 
1738     --  Trohdr
1739     IF (p_control_rec.process_entity = inv_globals.g_entity_all
1740         OR p_control_rec.process_entity = inv_globals.g_entity_trohdr
1741        ) THEN
1742       trohdr(
1743         p_validation_level           => p_validation_level
1744       , p_control_rec                => p_control_rec
1745       , p_trohdr_rec                 => l_trohdr_rec
1746       , p_trohdr_val_rec             => p_trohdr_val_rec
1747       , p_old_trohdr_rec             => l_old_trohdr_rec
1748       , x_trohdr_rec                 => l_trohdr_rec
1749       , x_old_trohdr_rec             => l_old_trohdr_rec
1750       );
1751     END IF;
1752 
1753     IF (l_debug = 1) THEN
1754        DEBUG(l_trohdr_rec.header_id, 'Inv_Transfer_Order_PVT.Process_Transfer_Orders');
1755     END IF;
1756     --  Load parent key if missing and operation is create.
1757     IF (l_debug = 1) THEN
1758        DEBUG('Calling trolin 1', 'Inv_Transfer_Order_PVT.Process_Transfer_Orders');
1759     END IF;
1760 
1761     FOR i IN 1 .. l_trolin_tbl.COUNT LOOP
1762       l_trolin_rec  := l_trolin_tbl(i);
1763 
1764       IF  l_trolin_rec.operation = inv_globals.g_opr_create
1765           AND (l_trolin_rec.header_id IS NULL
1766                OR l_trolin_rec.header_id = fnd_api.g_miss_num
1767               ) THEN
1768         --  Copy parent_id.
1769         IF (l_debug = 1) THEN
1770            DEBUG('Header:'|| TO_CHAR(l_trohdr_rec.header_id), 'Inv_Transfer_Order_PVT.Process_Transfer_Orders');
1771         END IF;
1772         l_trolin_tbl(i).header_id  := l_trohdr_rec.header_id;
1773       --l_trolin_Tbl(I).grouping_rule_id := l_trohdr_Rec.grouping_rule_id;
1774       END IF;
1775     END LOOP;
1776 
1777     --  Trolins
1778 
1779     IF (p_control_rec.process_entity = inv_globals.g_entity_all
1780         OR p_control_rec.process_entity = inv_globals.g_entity_trolin
1781        ) THEN
1782       trolins(
1783         p_validation_level           => p_validation_level
1784       , p_control_rec                => p_control_rec
1785       , p_trolin_tbl                 => l_trolin_tbl
1786       , p_trolin_val_tbl             => p_trolin_val_tbl
1787       , p_old_trolin_tbl             => l_old_trolin_tbl
1788       , p_move_order_type            => p_trohdr_rec.move_order_type
1789       , x_trolin_tbl                 => l_trolin_tbl
1790       , x_old_trolin_tbl             => l_old_trolin_tbl
1791       , p_delete_mmtt                => p_delete_mmtt  --Added bug 3524130
1792       );
1793     END IF;
1794 
1795      --debug('AFter trolins 2');
1796     --  Done processing, load OUT parameters.
1797     x_trohdr_rec      := l_trohdr_rec;
1798     --debug(x_trohdr_rec.header_id);
1799     x_trolin_tbl      := l_trolin_tbl;
1800      --debug('AFter trolins 4');
1801     --  Derive return status.
1802     x_return_status   := fnd_api.g_ret_sts_success;
1803 
1804 
1805     --Bug #4777248
1806     --Modified both the if loops by removing p_control_rec.process from the condition
1807     IF  (p_control_rec.process_entity = inv_globals.g_entity_all
1808              OR p_control_rec.process_entity = inv_globals.g_entity_trohdr
1809             ) THEN
1810       IF l_trohdr_rec.return_status = fnd_api.g_ret_sts_error THEN
1811         x_return_status  := fnd_api.g_ret_sts_error;
1812       END IF;
1813     END IF;
1814 
1815     IF (p_control_rec.process_entity = inv_globals.g_entity_all
1816              OR p_control_rec.process_entity = inv_globals.g_entity_trolin
1817             ) THEN
1818       FOR i IN 1 .. l_trolin_tbl.COUNT LOOP
1819         --debug('In Loop :'||to_char(I));
1820         IF l_trolin_tbl(i).return_status = fnd_api.g_ret_sts_error THEN
1821           x_return_status  := fnd_api.g_ret_sts_error;
1822         END IF;
1823       END LOOP;
1824     END IF;
1825     --End Bug #4777248
1826 
1827     --  Get message count and data
1828     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1829   EXCEPTION
1830     WHEN fnd_api.g_exc_error THEN
1831       x_return_status  := fnd_api.g_ret_sts_error;
1832       --  Get message count and data
1833       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1834     WHEN fnd_api.g_exc_unexpected_error THEN
1835       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1836       --  Get message count and data
1837       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1838     WHEN OTHERS THEN
1839       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1840 
1841       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1842         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Process_Transfer_Order');
1843       END IF;
1844 
1845       --  Get message count and data
1846       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1847   END process_transfer_order;
1848 
1849   --  Start of Comments
1850   --  API name    Lock_Transfer_Order
1851   --  Type        Private
1852   --  Function
1853   --
1854   --  Pre-reqs
1855   --
1856   --  Parameters
1857   --
1858   --  Version     Current version = 1.0
1859   --              Initial version = 1.0
1860   --
1861   --  Notes
1862   --
1863   --  End of Comments
1864 
1865   PROCEDURE lock_transfer_order(
1866     p_api_version_number IN     NUMBER
1867   , p_init_msg_list      IN     VARCHAR2 := fnd_api.g_false
1868   , x_return_status      OUT    NOCOPY VARCHAR2
1869   , x_msg_count          OUT    NOCOPY NUMBER
1870   , x_msg_data           OUT    NOCOPY VARCHAR2
1871   , p_trohdr_rec         IN     inv_move_order_pub.trohdr_rec_type := inv_move_order_pub.g_miss_trohdr_rec
1872   , p_trolin_tbl         IN     inv_move_order_pub.trolin_tbl_type := inv_move_order_pub.g_miss_trolin_tbl
1873   , x_trohdr_rec         IN OUT    NOCOPY inv_move_order_pub.trohdr_rec_type
1874   , x_trolin_tbl         IN OUT    NOCOPY inv_move_order_pub.trolin_tbl_type
1875   ) IS
1876     l_api_version_number CONSTANT NUMBER                             := 1.0;
1877     l_api_name           CONSTANT VARCHAR2(30)                       := 'Lock_Transfer_Order';
1878     l_return_status               VARCHAR2(1)                        := fnd_api.g_ret_sts_success;
1879     l_trolin_rec                  inv_move_order_pub.trolin_rec_type;
1880   BEGIN
1881     --  Standard call to check for call compatibility
1882     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
1883       RAISE fnd_api.g_exc_unexpected_error;
1884     END IF;
1885 
1886     --  Initialize message list.
1887     IF fnd_api.to_boolean(p_init_msg_list) THEN
1888       fnd_msg_pub.initialize;
1889     END IF;
1890 
1891     --  Set Savepoint
1892     SAVEPOINT lock_transfer_order_pvt;
1893 
1894     --  Lock trohdr
1895     IF p_trohdr_rec.operation = inv_globals.g_opr_lock THEN
1896       inv_trohdr_util.lock_row(p_trohdr_rec => p_trohdr_rec, x_trohdr_rec => x_trohdr_rec, x_return_status => l_return_status);
1897 
1898       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1899         RAISE fnd_api.g_exc_unexpected_error;
1900       ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1901         RAISE fnd_api.g_exc_error;
1902       END IF;
1903     END IF;
1904 
1905     --  Lock trolin
1906     FOR i IN 1 .. p_trolin_tbl.COUNT LOOP
1907       IF p_trolin_tbl(i).operation = inv_globals.g_opr_lock THEN
1908         inv_trolin_util.lock_row(p_trolin_rec => p_trolin_tbl(i), x_trolin_rec => l_trolin_rec, x_return_status => l_return_status);
1909 
1910         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1911           RAISE fnd_api.g_exc_unexpected_error;
1912         ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
1913           RAISE fnd_api.g_exc_error;
1914         END IF;
1915 
1916         x_trolin_tbl(i)  := l_trolin_rec;
1917       END IF;
1918     END LOOP;
1919 
1920     --  Set return status
1921     x_return_status  := fnd_api.g_ret_sts_success;
1922     --  Get message count and data
1923     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1924   EXCEPTION
1925     WHEN fnd_api.g_exc_error THEN
1926       x_return_status  := fnd_api.g_ret_sts_error;
1927       --  Get message count and data
1928       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1929       --  Rollback
1930       ROLLBACK TO lock_transfer_order_pvt;
1931     WHEN fnd_api.g_exc_unexpected_error THEN
1932       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1933       --  Get message count and data
1934       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1935       --  Rollback
1936       ROLLBACK TO lock_transfer_order_pvt;
1937     WHEN OTHERS THEN
1938       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1939 
1940       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1941         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Lock_Transfer_Order');
1942       END IF;
1943 
1944       --  Get message count and data
1945       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1946       --  Rollback
1947       ROLLBACK TO lock_transfer_order_pvt;
1948   END lock_transfer_order;
1949 
1950   --  Start of Comments
1951   --  API name    Get_Transfer_Order
1952   --  Type        Private
1953   --  Function
1954   --
1955   --  Pre-reqs
1956   --
1957   --  Parameters
1958   --
1959   --  Version     Current version = 1.0
1960   --              Initial version = 1.0
1961   --
1962   --  Notes
1963   --
1964   --  End of Comments
1965 
1966   PROCEDURE get_transfer_order(
1967     p_api_version_number IN     NUMBER
1968   , p_init_msg_list      IN     VARCHAR2 := fnd_api.g_false
1969   , x_return_status      OUT    NOCOPY VARCHAR2
1970   , x_msg_count          OUT    NOCOPY NUMBER
1971   , x_msg_data           OUT    NOCOPY VARCHAR2
1972   , p_header_id          IN     NUMBER
1973   , x_trohdr_rec         OUT    NOCOPY inv_move_order_pub.trohdr_rec_type
1974   , x_trolin_tbl         OUT    NOCOPY inv_move_order_pub.trolin_tbl_type
1975   ) IS
1976     l_api_version_number CONSTANT NUMBER                             := 1.0;
1977     l_api_name           CONSTANT VARCHAR2(30)                       := 'Get_Transfer_Order';
1978     l_trohdr_rec                  inv_move_order_pub.trohdr_rec_type;
1979     l_trolin_tbl                  inv_move_order_pub.trolin_tbl_type;
1980   BEGIN
1981     --  Standard call to check for call compatibility
1982     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
1983       RAISE fnd_api.g_exc_unexpected_error;
1984     END IF;
1985 
1986     --  Initialize message list.
1987     IF fnd_api.to_boolean(p_init_msg_list) THEN
1988       fnd_msg_pub.initialize;
1989     END IF;
1990 
1991     --  Get trohdr ( parent = trohdr )
1992     l_trohdr_rec     := inv_trohdr_util.query_row(p_header_id => p_header_id);
1993      --  Get trolin ( parent = trohdr )
1994     --debug('TRO : in get_transfer_order '||to_char(p_header_id)||' '||to_char(l_trohdr_rec.header_id));
1995     l_trolin_tbl     := inv_trolin_util.query_rows(p_header_id => l_trohdr_rec.header_id);
1996     --  Load out parameters
1997     x_trohdr_rec     := l_trohdr_rec;
1998     x_trolin_tbl     := l_trolin_tbl;
1999     --  Set return status
2000     x_return_status  := fnd_api.g_ret_sts_success;
2001     --  Get message count and data
2002     fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2003   EXCEPTION
2004     WHEN fnd_api.g_exc_error THEN
2005       x_return_status  := fnd_api.g_ret_sts_error;
2006       --  Get message count and data
2007       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2008     WHEN fnd_api.g_exc_unexpected_error THEN
2009       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2010       --  Get message count and data
2011       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2012     WHEN OTHERS THEN
2013       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2014 
2015       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2016         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Get_Transfer_Order');
2017       END IF;
2018 
2019       --  Get message count and data
2020       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2021   END get_transfer_order;
2022 
2023   FUNCTION validate_from_subinventory(p_from_subinventory_code IN VARCHAR2, p_organization_id IN NUMBER, p_inventory_item_id IN NUMBER, p_transaction_type_id IN NUMBER, p_restrict_subinventories_code IN NUMBER)
2024     RETURN BOOLEAN IS
2025     l_org                inv_validate.org;
2026     l_item               inv_validate.item;
2027     l_acct_txn           NUMBER;
2028     l_return             NUMBER;
2029     l_result             BOOLEAN;
2030     l_error_msg          VARCHAR2(2000);
2031     l_txn_action_id      NUMBER;
2032     l_txn_source_type_id NUMBER;
2033   BEGIN
2034     g_from_sub.secondary_inventory_name  := p_from_subinventory_code;
2035     l_org.organization_id                := p_organization_id;
2036     l_item.organization_id               := p_organization_id;
2037     l_item.inventory_item_id             := p_inventory_item_id;
2038     l_item.restrict_subinventories_code  := p_restrict_subinventories_code;
2039 
2040     IF (inv_validate.transaction_type(p_transaction_type_id, l_txn_action_id, l_txn_source_type_id) = inv_validate.t) THEN
2041       IF (l_txn_action_id = 1) THEN
2042         l_acct_txn  := 1;
2043       ELSE
2044         l_acct_txn  := 0;
2045       END IF;
2046     ELSE
2047       RETURN FALSE;
2048     END IF;
2049 
2050     l_return                             := inv_validate.from_subinventory(g_from_sub, l_org, l_item, l_acct_txn);
2051 
2052     IF (l_return = inv_validate.f) THEN
2053       l_result  := FALSE;
2054     ELSE
2055       l_result  := TRUE;
2056     END IF;
2057 
2058     RETURN l_result;
2059   END validate_from_subinventory;
2060 
2061   FUNCTION validate_to_subinventory(
2062     p_to_subinventory_code         IN VARCHAR2
2063   , p_organization_id              IN NUMBER
2064   , p_inventory_item_id            IN NUMBER
2065   , p_transaction_type_id          IN NUMBER
2066   , p_restrict_subinventories_code IN NUMBER
2067   , p_asset_item                   IN VARCHAR2
2068   , p_from_sub_asset               IN NUMBER
2069   )
2070     RETURN BOOLEAN IS
2071     l_org      inv_validate.org;
2072     l_item     inv_validate.item;
2073     l_sub      inv_validate.sub;
2074     l_acct_txn NUMBER;
2075     l_return   NUMBER;
2076     l_result   BOOLEAN;
2077   BEGIN
2078     l_sub.secondary_inventory_name       := p_to_subinventory_code;
2079     l_org.organization_id                := p_organization_id;
2080     l_item.organization_id               := p_organization_id;
2081     l_item.inventory_item_id             := p_inventory_item_id;
2082     l_item.restrict_subinventories_code  := p_restrict_subinventories_code;
2083 
2084     IF (p_transaction_type_id = 63) THEN
2085       l_acct_txn  := 1;
2086     ELSE
2087       l_acct_txn  := 0;
2088     END IF;
2089 
2090     l_item.inventory_asset_flag          := p_asset_item;
2091     --  g_from_sub.asset_inventory := p_from_sub_asset;
2092     l_return                             := inv_validate.to_subinventory(l_sub, l_org, l_item, g_from_sub, l_acct_txn);
2093 
2094     IF (l_return = inv_validate.f) THEN
2095       --l_error_msg := FND_MSG_PUB.GET(p_encoded => FND_API.G_FALSE);
2096       l_result  := FALSE;
2097     ELSE
2098       l_result  := TRUE;
2099     END IF;
2100 
2101     RETURN l_result;
2102   END validate_to_subinventory;
2103 
2104   --Update_Txn_Source_Line
2105   --
2106   -- This procedure updates the move order line indicated by p_line_id
2107   -- with a new transaction source line id (p_new_source_line_id).
2108   -- It also updates all of the allocation lines with the new source line id.
2109   -- This procedure is called from Shipping when the delivery detail is split
2110   -- after pick release has occurred, but before pick confirm.
2111   -- This procedure also transfers the detailed portion of the
2112   -- reservations from the old order line to the new order line
2113   PROCEDURE update_txn_source_line(p_line_id IN NUMBER,
2114 				   p_new_source_line_id IN NUMBER)
2115     IS
2116        l_debug number;
2117 
2118        CURSOR cur_mmtt IS
2119 	  SELECT reservation_id, transaction_quantity, primary_quantity
2120 	    FROM mtl_material_transactions_temp
2121 	    WHERE move_order_line_id = p_line_id;
2122 
2123        l_original_reservation_rec    inv_reservation_global.mtl_reservation_rec_type;
2124        l_new_reservation_rec         inv_reservation_global.mtl_reservation_rec_type;
2125 
2126        l_mtl_reservation_tbl         inv_reservation_global.mtl_reservation_tbl_type;
2127        l_mtl_reservation_tbl_count   NUMBER;
2128        l_reservation_id              NUMBER;
2129        l_original_serial_number      inv_reservation_global.serial_number_tbl_type;
2130 
2131        l_msg_count                   NUMBER;
2132        l_msg_data                    VARCHAR2(2000);
2133        l_return_status               VARCHAR2(1);
2134        l_error_code                  NUMBER;
2135 
2136   BEGIN
2137      -- Use cache to get value for l_debug
2138      IF g_is_pickrelease_set IS NULL THEN
2139         g_is_pickrelease_set := 2;
2140         IF INV_CACHE.is_pickrelease THEN
2141            g_is_pickrelease_set := 1;
2142         END IF;
2143      END IF;
2144      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
2145         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2146      END IF;
2147 
2148      l_debug := g_debug;
2149 
2150      UPDATE mtl_txn_request_lines
2151        SET txn_source_line_id = p_new_source_line_id
2152        WHERE line_id = p_line_id;
2153 
2154      UPDATE mtl_material_transactions_temp
2155        SET trx_source_line_id = p_new_source_line_id
2156        WHERE move_order_line_id = p_line_id;
2157 
2158      /* Rolling back changes made for bug 2919186.
2159      IF wsh_code_control.get_code_release_level >= '110509' THEN
2160 	FOR rec_mmtt IN cur_mmtt LOOP
2161 	   IF (rec_mmtt.reservation_id IS NOT NULL) THEN
2162 	      l_original_reservation_rec.reservation_id  := rec_mmtt.reservation_id;
2163 
2164 	      IF (l_debug = 1) THEN
2165 		 debug('About to call inv_reservation_pub.query_reservations', 'update_txn_source_line');
2166 	      END IF;
2167 
2168 	      -- Query the reservation record corresponding to the allocation line
2169 	      inv_reservation_pub.query_reservation
2170 		(p_api_version_number         => 1.0,
2171 		 x_return_status              => l_return_status,
2172 		 x_msg_count                  => l_msg_count,
2173 		 x_msg_data                   => l_msg_data,
2174 		 p_query_input                => l_original_reservation_rec,
2175 		 x_mtl_reservation_tbl        => l_mtl_reservation_tbl,
2176 		 x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count,
2177 		 x_error_code                 => l_error_code);
2178 
2179 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
2180 
2181 		 IF (l_debug = 1) THEN
2182 		    debug('Error from query_reservations', 'update_txn_source_line');
2183 		 END IF;
2184 
2185 		 RAISE fnd_api.g_exc_error;
2186 	       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2187 
2188 		 IF (l_debug = 1) THEN
2189 		    debug('Error from query_reservations', 'update_txn_source_line');
2190 		 END IF;
2191 
2192 		 RAISE fnd_api.g_exc_unexpected_error;
2193 	      END IF;
2194 
2195 	      IF (l_debug = 1) THEN
2196 		 debug('Original Reservation_ID: ' || rec_mmtt.reservation_id, 'update_txn_source_line');
2197 	      END IF;
2198 
2199 	      -- Transfer reservation for the transaction quantity. Make sure
2200 	      -- that the new reservation record has the appropriate quantity detailed
2201 	      l_new_reservation_rec.demand_source_line_id         := p_new_source_line_id;
2202 	      l_new_reservation_rec.primary_reservation_quantity  := Least(l_mtl_reservation_tbl(1).primary_reservation_quantity,
2203 									   rec_mmtt.primary_quantity);
2204 	      l_new_reservation_rec.reservation_quantity          := Least(l_mtl_reservation_tbl(1).reservation_quantity,
2205 									   rec_mmtt.transaction_quantity);
2206 	      l_new_reservation_rec.detailed_quantity             := Least(l_mtl_reservation_tbl(1).reservation_quantity,
2207 									   rec_mmtt.transaction_quantity);
2208 
2209 	      inv_reservation_pub.transfer_reservation
2210 		(p_api_version_number         => 1.0,
2211 		 p_init_msg_lst               => fnd_api.g_false,
2212 		 x_return_status              => l_return_status,
2213 		 x_msg_count                  => l_msg_count,
2214 		 x_msg_data                   => l_msg_data,
2215 		 p_original_rsv_rec           => l_mtl_reservation_tbl(1),
2216 		 p_to_rsv_rec                 => l_new_reservation_rec,
2217 		 p_original_serial_number     => l_original_serial_number,
2218 		 p_to_serial_number           => l_original_serial_number,
2219 		 p_validation_flag            => fnd_api.g_false,
2220 		 x_to_reservation_id          => l_reservation_id);
2221 
2222 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
2223 		 IF (l_debug = 1) THEN
2224 		    debug('Return from transfer_reservation with error E', 'Finalize_Pick_Confirm');
2225 		 END IF;
2226 
2227 		 RAISE fnd_api.g_exc_error;
2228 	       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2229 
2230 		 IF (l_debug = 1) THEN
2231 		    debug('Return from transfer_reservation with error U', 'Finalize_Pick_Confirm');
2232 		 END IF;
2233 
2234 		 RAISE fnd_api.g_exc_unexpected_error;
2235 	      END IF;
2236 
2237 	      IF (l_debug = 1) THEN
2238 		 debug('New Reservation_ID: '|| l_reservation_id, 'Finalize_Pick_Confirm');
2239 		 debug('l_return_status is '|| l_return_status, 'Finalize_Pick_Confirm');
2240 	      END IF;
2241 
2242 	   END IF;
2243 	END LOOP;
2244      END IF;
2245        */
2246   END update_txn_source_line;
2247 
2248   -- Bug 1620576
2249   -- This procedure is called from inltpu to delete the table
2250   -- mo_picked_quantity_tbl;
2251   -- This procedure is called everytime mmtt records are deleted in
2252   -- inltpu.
2253   PROCEDURE clear_picked_quantity IS
2254   BEGIN
2255     g_mo_picked_quantity_tbl.DELETE;
2256     g_rsv_picked_quantity_tbl.DELETE;
2257     g_mmtt_cache_tbl.DELETE;   --Bug: 4994950 (Actual bug #4762505)
2258   END clear_picked_quantity;
2259 
2260   -- This procedure deletes any unstaged reservations for a sales order if
2261   -- there are only staged or shipped line delivery detail lines for that order
2262   PROCEDURE clean_reservations
2263   ( p_source_line_id  IN  NUMBER
2264   , x_return_status   OUT NOCOPY  VARCHAR2
2265   , x_msg_count       OUT NOCOPY  NUMBER
2266   , x_msg_data        OUT NOCOPY  VARCHAR2
2267   ) IS
2268     -- bug 2115082
2269     -- should only delete reservations for supply type inventory
2270     CURSOR unstaged_reservations_csr IS
2271       SELECT reservation_id
2272         FROM mtl_reservations
2273        WHERE NVL(staged_flag, 'N') = 'N'
2274          AND supply_source_type_id = 13
2275          AND demand_source_type_id IN (2, 8)
2276          AND demand_source_line_id = p_source_line_id
2277          AND demand_source_line_detail IS NULL;
2278 
2279     l_return_status          VARCHAR2(1);
2280     l_msg_count              NUMBER;
2281     l_msg_data               VARCHAR2(2000);
2282     l_unstaged_so_exists     NUMBER                                          := 0;
2283     l_mtl_reservation_rec    inv_reservation_global.mtl_reservation_rec_type;
2284     l_original_serial_number inv_reservation_global.serial_number_tbl_type;
2285     l_ato_item number := 0 ;
2286     l_debug number;
2287   BEGIN
2288     -- Use cache to get value for l_debug
2289     IF g_is_pickrelease_set IS NULL THEN
2290        g_is_pickrelease_set := 2;
2291        IF INV_CACHE.is_pickrelease THEN
2292           g_is_pickrelease_set := 1;
2293        END IF;
2294     END IF;
2295     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
2296        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2297     END IF;
2298 
2299     l_debug := g_debug;
2300 
2301     x_return_status  := fnd_api.g_ret_sts_success;
2302     IF (l_debug = 1) THEN
2303        DEBUG('Cleaning reservations', 'Clean_Reservations');
2304     END IF;
2305 
2306     -- If this is the last allocation line for the last move order
2307     -- line for the sales order then delete all the unstaged
2308     -- reservations for this record. This will also delete
2309     -- reservations which are against the backordered lines(except for
2310     -- ato items where the reservations are not deleted )
2311 
2312    -- Bug2621481 , Reservations are retained for ATO items.In case any
2313    -- reservations exist and delivery detail is in Backordered status then the
2314    -- reservations remain for ATO. All other cases where the item is not
2315    -- ATO and if there are no backordered delivery details process normally
2316    -- The profile WSH_RETAIN_ATO_RESERVATIONS is checked and if the value
2317    -- is set to Y and if the item is ATO then the reservations are not
2318    -- relieved.
2319    IF g_retain_ato_profile = 'Y' Then
2320    BEGIN
2321     SELECT     1
2322     INTO  l_ato_item
2323     FROM dual
2324     WHERE EXISTS (SELECT msi.inventory_item_id
2325                   FROM mtl_system_items msi, mtl_Reservations mtr
2326                   WHERE msi.inventory_item_id = mtr.inventory_item_id
2327                   AND msi.organization_id = mtr.organization_id
2328                   AND bom_item_type = 4
2329                   AND replenish_to_order_flag = 'Y'
2330                   AND mtr.demand_source_line_id  =  p_source_line_id
2331                   AND mtr.demand_source_line_detail IS NULL);
2332      EXCEPTION
2333      WHEN OTHERS THEN
2334        IF (l_debug = 1) THEN
2335           DEBUG('Not an ATO item ', 'Clean_Reservations');
2336        END IF;
2337        l_ato_item := 0;
2338     END;
2339    END IF;
2340    -- If l_ato_item = 1 then item is ATO item  and proceed to check if
2341    -- backordered or  'R' and 'S' delivery details exist
2342    -- The reservations against these lines are not relieved and the
2343    -- value of l_unstaged_so_exists is set to 1
2344     IF l_ato_item = 1 THEN
2345              BEGIN
2346               SELECT 1
2347               INTO  l_unstaged_so_exists
2348               FROM dual
2349               WHERE EXISTS ( SELECT delivery_Detail_id
2350                              FROM wsh_delivery_Details
2351                              WHERE source_line_id = p_source_line_id
2352                              AND released_status in ( 'B','R','S'));
2353              EXCEPTION
2354              WHEN NO_DATA_FOUND THEN
2355                   IF (l_debug = 1) THEN
2356                      DEBUG('No Backordered ATO reservation', 'Clean_Reservations');
2357                   END IF;
2358                   l_unstaged_so_exists := 0;
2359              END;
2360     ELSE
2361 
2362     --Bug#2666971. Reservations were not being cleared for
2363     --orders that were in status 'D'(Cancelled) or 'X'(Not Applicable)
2364     --Changed the below sql. the earlier condition was
2365     --AND (released_status <> 'Y' AND released_status <> 'C'
2366     --AND released_status <> 'B'
2367 
2368     --Bug 6264551, Adding 'B' to retain reservation for backordered lines as well
2369     --this is to retain the manually created reservations prior to pick release.
2370 
2371      BEGIN
2372 
2373         SELECT 1
2374           INTO l_unstaged_so_exists
2375           FROM DUAL
2376          WHERE EXISTS( SELECT /*+ index(wsh_delivery_details WSH_DELIVERY_DETAILS_N3) */ delivery_detail_id
2377                          FROM wsh_delivery_details
2378                         WHERE source_line_id = p_source_line_id
2379                           AND (released_status IN ('B','R','S')));
2380     EXCEPTION
2381       WHEN NO_DATA_FOUND THEN
2382         IF (l_debug = 1) THEN
2383            DEBUG('No unstaged SO', 'Clean_Reservations');
2384         END IF;
2385         l_unstaged_so_exists  := 0;
2386     END;
2387  END IF;
2388 
2389     IF NVL(l_unstaged_so_exists, 0) <> 1 THEN
2390       -- The current line was the last move order line for the
2391       -- sales order
2392       FOR l_unstaged_reservation IN unstaged_reservations_csr LOOP
2393         l_mtl_reservation_rec.reservation_id  := l_unstaged_reservation.reservation_id;
2394         IF (l_debug = 1) THEN
2395            DEBUG('Deleting reservation: '|| l_unstaged_reservation.reservation_id, 'Clean_Reservations');
2396         END IF;
2397         -- {{
2398         -- Create a crossdock peg for a partial qty of sales order line
2399         -- and pick release the rest from inventory.  Before receipt
2400         -- of crossdock material, stage the move order line from Inventory.
2401         -- Ensure that the crossdock peg is not deleted }}
2402         inv_reservation_pub.delete_reservation(
2403           p_api_version_number         => 1.0
2404         , p_init_msg_lst               => fnd_api.g_true
2405         , x_return_status              => l_return_status
2406         , x_msg_count                  => l_msg_count
2407         , x_msg_data                   => l_msg_data
2408         , p_rsv_rec                    => l_mtl_reservation_rec
2409         , p_serial_number              => l_original_serial_number
2410         );
2411         IF (l_debug = 1) THEN
2412            DEBUG('after delete reservation return status is '|| l_return_status, 'Clean_Reservations');
2413         END IF;
2414 
2415         IF l_return_status = fnd_api.g_ret_sts_error THEN
2416           RAISE fnd_api.g_exc_error;
2417         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2418           RAISE fnd_api.g_exc_unexpected_error;
2419         END IF;
2420       END LOOP;
2421     END IF;
2422   EXCEPTION
2423     WHEN fnd_api.g_exc_error THEN
2424       x_return_status  := fnd_api.g_ret_sts_error;
2425       --  Get message count and data
2426       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2427     WHEN fnd_api.g_exc_unexpected_error THEN
2428       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2429       --  Get message count and data
2430       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2431     WHEN OTHERS THEN
2432       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2433 
2434       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2435         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Clean Reservations');
2436       END IF;
2437 
2438       --  Get message count and data
2439       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2440   END clean_reservations;
2441 
2442   --
2443   -- Finalize pick confirm processes the move orders that are being
2444   -- picked. In case of pick wave move orders it handles the reservations and
2445   -- updates the shipping attributes.
2446   PROCEDURE finalize_pick_confirm
2447            (p_init_msg_list IN VARCHAR2 := fnd_api.g_false
2448            , x_return_status OUT NOCOPY VARCHAR2
2449            , x_msg_count OUT NOCOPY NUMBER
2450            , x_msg_data OUT NOCOPY VARCHAR2
2451            , p_transaction_temp_id IN NUMBER
2452            , p_transaction_id IN NUMBER
2453            , p_xfr_transaction_id NUMBER DEFAULT NULL
2454            )
2455   IS
2456     l_api_name    CONSTANT VARCHAR2(30) := 'Finalize_Pick_Confirm';
2457 
2458     l_mtl_reservation_tbl         inv_reservation_global.mtl_reservation_tbl_type;
2459     l_query_reservation_rec       inv_reservation_global.mtl_reservation_rec_type;
2460     l_mtl_reservation_rec         inv_reservation_global.mtl_reservation_rec_type;
2461     l_mtl_reservation_rec2        inv_reservation_global.mtl_reservation_rec_type;
2462     l_mtl_reservation_rec3        inv_reservation_global.mtl_reservation_rec_type;
2463     l_mtl_reservation_tbl_count   NUMBER;
2464     l_return_status               VARCHAR2(1);
2465     l_msg_count                   NUMBER;
2466     l_msg_data                    VARCHAR2(2000);
2467     l_error_code                  NUMBER;
2468     l_trolin_rec                  inv_move_order_pub.trolin_rec_type;
2469     l_mmtt_rec                    inv_mo_line_detail_util.g_mmtt_rec;
2470     l_mmtt_count                  NUMBER;
2471     l_reservation_id              NUMBER;
2472     l_original_serial_number      inv_reservation_global.serial_number_tbl_type;
2473     l_to_serial_number            inv_reservation_global.serial_number_tbl_type;
2474     l_shipping_attr               wsh_interface.changedattributetabtype;
2475     l_source_header_id            NUMBER;
2476     l_source_line_id              NUMBER;
2477     l_delivery_detail_id          NUMBER;
2478     l_move_order_type             NUMBER;
2479     l_quantity_reserved           NUMBER;
2480     l_message                     VARCHAR2(2000);
2481     l_released_status             VARCHAR2(1);
2482     l_customer_item_id            NUMBER;
2483     l_subinventory                VARCHAR2(20);
2484     l_locator_id                  NUMBER;
2485     l_lot_count                   NUMBER;
2486     l_lot_control_code            NUMBER;
2487     l_serial_control_code         NUMBER;
2488     l_serial_trx_temp_id          NUMBER;
2489     l_transaction_temp_id         NUMBER;
2490     l_lot_number                  VARCHAR2(80);
2491     l_previous_lot_number         VARCHAR2(80) := '@';
2492     l_lot_primary_quantity        NUMBER;
2493     l_lot_transaction_quantity    NUMBER;
2494     l_lot_secondary_quantity      NUMBER;
2495     l_lot_secondary_uom           VARCHAR2(25);
2496     l_serial_number               VARCHAR2(30);
2497     l_serial_secondary_quantity   NUMBER;
2498     l_reservable_type             NUMBER;
2499     l_reservable_type_item        NUMBER;
2500     l_action_flag                 VARCHAR2(1);
2501     l_transaction_quantity        NUMBER;
2502     l_pending_quantity            NUMBER;
2503     l_primary_pending_quantity    NUMBER;
2504     l_mmt_transaction_quantity    NUMBER;
2505     l_rsv_primary_quantity        NUMBER;
2506     l_rsv_detailed_quantity       NUMBER;
2507     l_mmtt_rsv_quantity           NUMBER;
2508     l_rsv_changed                 BOOLEAN;
2509     l_remaining_quantity          NUMBER;
2510     l_unalloc_quantity            NUMBER;
2511 
2512     l_sec_transaction_quantity        NUMBER;
2513     l_sec_pending_quantity            NUMBER;
2514     l_sec_mmt_transaction_quantity    NUMBER;
2515     l_sec_rsv_quantity                NUMBER;
2516     l_sec_rsv_detailed_quantity       NUMBER;
2517     l_sec_mmtt_rsv_quantity           NUMBER;
2518     l_sec_rsv_changed                 BOOLEAN;
2519     l_sec_remaining_quantity          NUMBER;
2520     l_sec_unalloc_quantity            NUMBER;
2521     l_grade_code		      VARCHAR2(150);
2522 
2523     -- Contains the portion of the quantity in the current MMTT line, which
2524     -- will be used to update shipping attributes and reservations. This is required
2525     -- for reduction in the sales order quantity
2526     l_shipping_quantity           NUMBER                                          := 0;
2527     l_primary_shipping_quantity   NUMBER                                          := 0;
2528     l_lot_shipping_quantity       NUMBER                                          := 0;
2529     l_lot_prim_shipping_quantity  NUMBER                                          := 0;
2530     l_remaining_shipping_quantity NUMBER                                          := 0;
2531 
2532     l_sec_shipping_quantity           NUMBER                                          := 0;
2533     l_sec_lot_shipping_quantity       NUMBER                                          := 0;
2534     l_sec_remaining_shp_quantity      NUMBER                                          := 0;
2535 
2536     l_ser_prim_shipping_quantity  NUMBER                                          := 0;
2537     l_serial_quantity             NUMBER                                          := 0;
2538     l_unstaged_so_exists          NUMBER                                          := 0;
2539     l_update_shipping             BOOLEAN;
2540     l_lpn_id                      NUMBER;
2541     l_serial_transaction_temp_id  NUMBER;
2542     l_dummy_num                   NUMBER;
2543     l_container_name		  VARCHAR2(30);
2544     l_new_container_name          VARCHAR2(30);
2545     l_status_code                 VARCHAR2(3);
2546     l_container_delivery_det_id   NUMBER;
2547     l_container_rec               wsh_container_grp.changedattributetabtype;
2548     l_InvPCInRecType              wsh_integration.invpcinrectype;
2549     l_wms_org_flag                BOOLEAN;
2550     l_omh_installed               NUMBER;
2551     l_catch_weight_enabled        VARCHAR(30);
2552     l_organization_id             NUMBER;            --For Bug#3153166
2553     l_return_value                BOOLEAN := TRUE;   --For Bug#3153166
2554     l_primary_uom_code            VARCHAR2(3); --INVCONV
2555     l_secondary_uom_code          VARCHAR2(3); --INVCONV
2556     l_tracking_quantity_ind       VARCHAR2(30); --INVCONV
2557     l_wip_entity_type             NUMBER; --INVCONV
2558     l_hash_value                  NUMBER; -- Bug 5535030
2559     /*** {{ R12 Enhanced reservations code changes ***/
2560     l_serial_count                NUMBER;
2561     /*** End R12 }} ***/
2562 
2563 --INVCONV
2564     CURSOR  get_item_details (l_org_id NUMBER, l_item_id NUMBER) IS
2565       SELECT primary_uom_code, secondary_uom_code, tracking_quantity_ind
2566       FROM   mtl_system_items
2567       WHERE  inventory_item_id = l_item_id
2568       AND    organization_id   = l_org_id;
2569 --INVCONV
2570     CURSOR lot_csr(l_trx_temp_id NUMBER) IS
2571       SELECT lot_number
2572            , primary_quantity
2573            , transaction_quantity
2574            , secondary_quantity
2575            , secondary_unit_of_measure
2576            , serial_transaction_temp_id
2577            , grade_code
2578         FROM mtl_transaction_lots_temp
2579        WHERE transaction_temp_id = l_trx_temp_id
2580        ORDER BY lot_number;
2581 
2582     CURSOR serial_csr(l_trx_temp_id NUMBER, l_lot_control_code NUMBER, l_ser_trx_temp_id NUMBER,
2583                       l_org_id NUMBER, l_item_id NUMBER) IS
2584       SELECT msn.serial_number
2585         FROM mtl_serial_numbers msn, mtl_serial_numbers_temp msnt
2586        WHERE msnt.transaction_temp_id = DECODE(l_lot_control_code, 1, l_trx_temp_id, l_ser_trx_temp_id)
2587          AND msn.current_organization_id = l_org_id
2588          AND msn.inventory_item_id = l_item_id
2589          AND msn.serial_number BETWEEN msnt.fm_serial_number AND NVL(msnt.to_serial_number, msnt.fm_serial_number)
2590          AND length(msn.serial_number) = length(msnt.fm_serial_number);
2591 
2592     CURSOR reservations_csr IS
2593       SELECT reservation_id
2594            , primary_reservation_quantity
2595            , detailed_quantity
2596         FROM mtl_reservations
2597        WHERE demand_source_line_id = l_trolin_rec.txn_source_line_id
2598          AND NVL(detailed_quantity, 0) > 0
2599          AND NVL(staged_flag, 'N') = 'N'
2600          AND demand_source_line_detail IS NULL;
2601 
2602     CURSOR unstaged_reservations_csr IS
2603       SELECT reservation_id
2604         FROM mtl_reservations
2605        WHERE NVL(staged_flag, 'N') = 'N'
2606          AND demand_source_line_id = l_trolin_rec.txn_source_line_id
2607          AND demand_source_line_detail IS NULL;
2608 
2609 
2610     /*** {{ R12 Enhanced reservations code changes ***/
2611     CURSOR serial_reserved_csr(l_trx_temp_id NUMBER, l_reservation_id NUMBER) IS
2612       SELECT msn.serial_number
2613         FROM mtl_serial_numbers msn, mtl_serial_numbers_temp msnt
2614        WHERE msnt.transaction_temp_id = l_trx_temp_id
2615          AND msn.serial_number BETWEEN msnt.fm_serial_number AND NVL(msnt.to_serial_number, msnt.fm_serial_number)
2616          AND length(msn.serial_number) = length(msnt.fm_serial_number)
2617          AND msn.reservation_id = l_reservation_id;
2618     /*** End R12 }} ***/
2619 
2620     Cursor get_wip_entity_type is
2621       Select entity_type
2622       From wip_entities
2623       Where wip_entity_id = l_trolin_rec.txn_source_id;
2624 
2625    -- Bug5950172.Added following cursor
2626    CURSOR mmtt_pending_qty_csr (  p_mo_line_id NUMBER) IS
2627        SELECT  ABS(transaction_quantity) , mmtt.transaction_uom, ABS(primary_quantity), NVL(ABS(secondary_transaction_quantity), 0)
2628          FROM mtl_material_transactions_temp mmtt
2629        WHERE move_order_line_id = p_mo_line_id;
2630 
2631       l_curr_mmtt_txn_uom   VARCHAR2(3) ; -- Bug5950172
2632       l_curr_mmtt_txn_qty   NUMBER;  -- Bug5950172
2633       l_curr_mmtt_pri_qty   NUMBER;  -- Bug5950172
2634       l_curr_mmtt_sec_qty   NUMBER;  -- Bug5950172
2635 
2636     l_debug number;
2637     l_other_mmtt_rec              NUMBER;
2638     l_mo_line_closed_flag         BOOLEAN := FALSE;           --Bug 4994950
2639 
2640   BEGIN
2641     -- Use cache to get value for l_debug
2642     IF g_is_pickrelease_set IS NULL THEN
2643        g_is_pickrelease_set := 2;
2644        IF INV_CACHE.is_pickrelease THEN
2645           g_is_pickrelease_set := 1;
2646        END IF;
2647     END IF;
2648     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
2649        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2650     END IF;
2651 
2652     l_debug := g_debug;
2653 
2654     IF (l_debug = 1) THEN
2655        DEBUG('Call to Finalize_Pick_Confirm trxtmpid='||p_transaction_temp_id||' trxid='||p_transaction_id||' xfrtxnid='||p_xfr_transaction_id, 'Finalize Pick Confirm');
2656     END IF;
2657 
2658     IF p_transaction_temp_id IS NULL THEN
2659       x_return_status  := fnd_api.g_ret_sts_error;
2660       RAISE fnd_api.g_exc_error;
2661     END IF;
2662 
2663     -- query the move order line detail for the move order line
2664     -- detail id passed in
2665     l_mmtt_rec                          := inv_mo_line_detail_util.query_row(p_transaction_temp_id);
2666 --INVCONV
2667     OPEN get_item_details (l_mmtt_rec.organization_id, l_mmtt_rec.inventory_item_id);
2668     FETCH get_item_details INTO l_primary_uom_code, l_secondary_uom_code, l_tracking_quantity_ind;
2669     CLOSE get_item_details;
2670 --INVCONV
2671 
2672     IF (l_debug = 1) THEN
2673        DEBUG('after calling mold_query_row', 'Finalize_Pick_Confirm');
2674        DEBUG('move order line id is '|| l_mmtt_rec.move_order_line_id, 'Finalize_Pick_Confirm');
2675     END IF;
2676     -- query the move order line for that move order line detail
2677     l_trolin_rec                        := inv_trolin_util.query_row(l_mmtt_rec.move_order_line_id);
2678     IF (l_debug = 1) THEN
2679        DEBUG('after calling trolin query row', 'Finalize_Pick_Confirm');
2680        DEBUG('transaction_quantity is '|| l_mmtt_rec.transaction_quantity, 'Finalize_Pick_Confirm');
2681        DEBUG('quantity detailed '|| l_trolin_rec.quantity_detailed, 'Finalize_Pick_Confirm');
2682        DEBUG('quantity required '|| l_trolin_rec.required_quantity, 'Finalize_Pick_Confirm');
2683        DEBUG('secondary quantity detailed '|| l_trolin_rec.secondary_quantity_detailed, 'Finalize_Pick_Confirm');
2684        DEBUG('secondary quantity required '|| l_trolin_rec.secondary_required_quantity, 'Finalize_Pick_Confirm');
2685     END IF;
2686 
2687     -- Cannot transact a closed move order line
2688     IF l_trolin_rec.line_status = 5 THEN
2689        fnd_message.set_name('INV', 'INV_CANNOT_TRX_CLOSED_MO');
2690        x_return_status  := fnd_api.g_ret_sts_error;
2691        RAISE fnd_api.g_exc_error;
2692     END IF;
2693 
2694     -- update the quantity delivered in the move order line record
2695     IF l_trolin_rec.uom_code = l_mmtt_rec.transaction_uom
2696     THEN
2697        l_trolin_rec.quantity_delivered := NVL(l_trolin_rec.quantity_delivered, 0)
2698                                           + ABS(l_mmtt_rec.transaction_quantity);
2699     ELSE
2700        l_trolin_rec.quantity_delivered := NVL(l_trolin_rec.quantity_delivered, 0)
2701                                           + ABS( inv_convert.inv_um_convert
2702                                                  ( item_id       => l_mmtt_rec.inventory_item_id
2703                                                  , precision     => NULL
2704                                                  , from_quantity => l_mmtt_rec.transaction_quantity
2705                                                  , from_unit     => l_mmtt_rec.transaction_uom
2706                                                  , to_unit       => l_trolin_rec.uom_code
2707                                                  , from_name     => NULL
2708                                                  , to_name       => NULL
2709                                                  )
2710                                                );
2711     END IF;
2712 
2713 --INVCONV
2714     IF l_tracking_quantity_ind <> 'P' THEN
2715        l_trolin_rec.secondary_quantity_delivered := NVL(l_trolin_rec.secondary_quantity_delivered, 0)
2716                                           + ABS(l_mmtt_rec.secondary_transaction_quantity);
2717     END IF;
2718 --INVCONV
2719     IF (l_debug = 1) THEN
2720        DEBUG('quantity_delivered = '|| l_trolin_rec.quantity_delivered, 'Finalize_Pick_Confirm');
2721        DEBUG('secondary_quantity_delivered = '|| l_trolin_rec.secondary_quantity_delivered, 'Finalize_Pick_Confirm');
2722     END IF;
2723 
2724     SELECT moh.move_order_type
2725       INTO l_move_order_type
2726       FROM mtl_txn_request_headers moh, mtl_txn_request_lines mol
2727      WHERE mol.line_id = l_trolin_rec.line_id
2728        AND mol.header_id = moh.header_id;
2729 
2730     IF (l_debug = 1) THEN
2731        DEBUG('after get move_order_type '|| l_move_order_type, 'Finalize_Pick_Confirm');
2732     END IF;
2733 
2734     -- Check if the organization is a WMS organization
2735     --for Bug#3153166: Performace Issue, Will Check the cache before
2736     --calling inv_install.adv_inv_installed, to check wms installed
2737     --or not.
2738     /*l_wms_org_flag := wms_install.check_install
2739       (x_return_status   => x_return_status,
2740        x_msg_count       => x_msg_count,
2741        x_msg_data        => x_msg_data,
2742        p_organization_id => l_trolin_rec.organization_id);*/
2743 
2744     l_organization_id := l_trolin_rec.organization_id;
2745     l_return_value := INV_CACHE.set_wms_installed(l_organization_id);
2746     If NOT l_return_value Then
2747           RAISE fnd_api.g_exc_unexpected_error;
2748     End If;
2749     l_wms_org_flag := INV_CACHE.wms_installed;
2750 
2751     -- End of Changes for Bug#3153166
2752 
2753     -- Cannot overpick in a WMS organization in Release I and belw.Feature enabled in J:Bug 3415741
2754     IF  (INV_CONTROL.G_CURRENT_RELEASE_LEVEL < INV_RELEASE.G_J_RELEASE_LEVEL) THEN
2755       IF (l_debug = 1) THEN
2756         DEBUG('Patchset Level I:Overpicking not allowed for WMS orgs', 'Finalize_Pick_Confirm');
2757       END IF;
2758       IF l_wms_org_flag AND l_move_order_type = 3
2759         AND l_trolin_rec.quantity_delivered > l_trolin_rec.quantity THEN
2760         fnd_message.set_name('INV', 'INV_CANNOT_OVERPICK_WMS_SO');
2761         x_return_status  := fnd_api.g_ret_sts_error;
2762         RAISE fnd_api.g_exc_error;
2763       END IF;
2764 
2765     END IF;
2766 
2767     -- Bug 2666620: BackFlush MO Type Removed
2768     IF l_move_order_type IN (3, 5) THEN -- Pick wave or WIP move order
2769       -- If the current MMTT line is the one that causes the quantity
2770       -- delivered > required quantity, store this information. We will update
2771       -- the reservations and the delivery details appropriately with this
2772       -- Also if the required quantity is not less than quantity we allow overpicking
2773       If l_move_order_type = 5 then   -- try to see if it is WIP or GME
2774         Open get_wip_entity_type;
2775         Fetch get_wip_entity_type into l_wip_entity_type;
2776         Close get_wip_entity_type;
2777       End if;
2778       IF  (l_trolin_rec.quantity_delivered >= NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity))
2779           AND (NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity) < l_trolin_rec.quantity) THEN
2780         l_shipping_quantity       := NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity)
2781                                       - (l_trolin_rec.quantity_delivered - ABS(l_mmtt_rec.transaction_quantity));
2782         l_sec_shipping_quantity   := NVL(l_trolin_rec.secondary_required_quantity, l_trolin_rec.secondary_quantity)
2783                                       - (l_trolin_rec.secondary_quantity_delivered - ABS(l_mmtt_rec.secondary_transaction_quantity));
2784         l_trolin_rec.line_status  := 9; -- Set the move order status to cancelled
2785 
2786         IF l_shipping_quantity < 0 THEN
2787           l_shipping_quantity          := 0;
2788           l_sec_shipping_quantity      := 0;
2789           l_primary_shipping_quantity  := 0;
2790         ELSE
2791           IF l_mmtt_rec.transaction_uom <> l_mmtt_rec.item_primary_uom_code THEN
2792             l_primary_shipping_quantity  := inv_convert.inv_um_convert(
2793                                               item_id                      => l_mmtt_rec.inventory_item_id
2794                                             , PRECISION                    => NULL
2795                                             , from_quantity                => l_shipping_quantity
2796                                             , from_unit                    => l_mmtt_rec.transaction_uom
2797                                             , to_unit                      => l_mmtt_rec.item_primary_uom_code
2798                                             , from_name                    => NULL
2799                                             , to_name                      => NULL
2800                                             );
2801 
2802             IF (l_primary_shipping_quantity = -99999) THEN
2803               IF (l_debug = 1) THEN
2804                  DEBUG('Cannot convert uom to primary uom', 'Finalize_Pick_Confirm');
2805               END IF;
2806               fnd_message.set_name('INV', 'INV-CANNOT CONVERT');
2807               fnd_message.set_token('UOM', l_mtl_reservation_rec.primary_uom_code);
2808               fnd_message.set_token('ROUTINE', 'Pick Confirm process');
2809               fnd_msg_pub.ADD;
2810               RAISE fnd_api.g_exc_unexpected_error;
2811             END IF;
2812           ELSE
2813             l_primary_shipping_quantity  := l_shipping_quantity;
2814           END IF;
2815         END IF;
2816       ELSE
2817         l_shipping_quantity          := ABS(l_mmtt_rec.transaction_quantity);
2818         l_sec_shipping_quantity      := ABS(l_mmtt_rec.secondary_transaction_quantity);
2819         l_primary_shipping_quantity  := ABS(l_mmtt_rec.primary_quantity);
2820       END IF;
2821 
2822       IF (l_debug = 1) THEN
2823          DEBUG('Shipping Quantity: '|| l_shipping_quantity, 'Finalize_Pick_Confirm');
2824          DEBUG('sec Shipping Quantity: '|| l_sec_shipping_quantity, 'Finalize_Pick_Confirm');
2825       END IF;
2826 
2827       -- Query MMTT to get the quantity remaining to be picked for this line;
2828       -- Pending quantity includes the current transaction;
2829       -- Remaining quantity does not include the current transaction.
2830       -- Pending quantity is used to update shipping;
2831       -- primary_pending_quantity is used to update reservations;
2832       -- remaining_quantity is used to update move orders.
2833 
2834       /*Bug5950712. start of fix */
2835       l_primary_pending_quantity := 0 ;
2836       l_pending_quantity         := 0 ;
2837       l_sec_pending_quantity     := 0 ;
2838       OPEN mmtt_pending_qty_csr(l_trolin_rec.line_id);
2839        LOOP
2840          FETCH   mmtt_pending_qty_csr INTO l_curr_mmtt_txn_qty, l_curr_mmtt_txn_uom , l_curr_mmtt_pri_qty, l_curr_mmtt_sec_qty ;
2841          EXIT WHEN mmtt_pending_qty_csr%NOTFOUND;
2842          l_primary_pending_quantity := l_primary_pending_quantity +  l_curr_mmtt_pri_qty ;
2843          l_sec_pending_quantity   :=   l_sec_pending_quantity + l_curr_mmtt_sec_qty ;
2844          IF ( l_mmtt_rec.transaction_uom <> l_curr_mmtt_txn_uom ) THEN
2845              l_curr_mmtt_txn_qty :=  inv_convert.inv_um_convert(
2846                                               item_id                      => l_mmtt_rec.inventory_item_id
2847                                             , precision                    => NULL
2848                                             , from_quantity                => l_curr_mmtt_txn_qty
2849                                             , from_unit                    => l_curr_mmtt_txn_uom
2850                                             , to_unit                      => l_mmtt_rec.transaction_uom
2851                                             , from_name                    => NULL
2852                                             , to_name                      => NULL );
2853 
2854              IF (l_curr_mmtt_txn_qty  = -99999) THEN
2855                IF (l_debug = 1) THEN
2856                   DEBUG('Cannot convert uom of other MMTTs to current transaction_uom uom', 'Finalize_Pick_Confirm');
2857                END IF;
2858                fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
2859                fnd_message.set_token('UOM1', l_curr_mmtt_txn_uom);
2860                fnd_message.set_token('UOM2', l_mmtt_rec.transaction_uom);
2861                fnd_message.set_token('ROUTINE', 'Pick Confirm process');
2862                fnd_msg_pub.ADD;
2863                RAISE fnd_api.g_exc_unexpected_error;
2864              END IF;
2865          END IF;
2866 
2867          l_pending_quantity := l_pending_quantity + l_curr_mmtt_txn_qty ;
2868       END LOOP;
2869       CLOSE  mmtt_pending_qty_csr;
2870       /*Bug5950172. End of fix */
2871 
2872      /*5950172.Commented the following
2873       SELECT NVL(SUM(ABS(transaction_quantity)), 0)
2874            , NVL(SUM(ABS(primary_quantity)), 0)
2875            , NVL(SUM(ABS(secondary_transaction_quantity)), 0)
2876         INTO l_pending_quantity
2877            , l_primary_pending_quantity
2878            , l_sec_pending_quantity
2879         FROM mtl_material_transactions_temp
2880        WHERE move_order_line_id = l_trolin_rec.line_id; */
2881 
2882       IF (l_debug = 1) THEN
2883          DEBUG('Pending Quantity from MMTT: '|| l_pending_quantity, 'Finalize_Pick_Confirm');
2884          DEBUG('Pending Sec Quantity from MMTT: '|| l_sec_pending_quantity, 'Finalize_Pick_Confirm');
2885       END IF;
2886 
2887       --Because MMTT records are deleted after all of the records
2888       -- are processed for a transaction_header_Id, it's possible
2889       -- that records exist in MMTT which are no longer pending -
2890       -- finalize_pick_confirm has already been called on these records.
2891       -- So we don't overcount the pending quantity, we keep track
2892       -- of the quantity we've already picked in a global table.  The
2893       -- table gets deleted when MMTT records are deleted.
2894       IF g_mo_picked_quantity_tbl.EXISTS(l_trolin_rec.line_id) THEN
2895         IF (l_debug = 1) THEN
2896            DEBUG('Cached picked quantity: '|| g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity, 'Finalize_Pick_Confirm');
2897            DEBUG('Cached sec picked quantity: '|| g_mo_picked_quantity_tbl(l_trolin_rec.line_id).sec_picked_quantity, 'Finalize_Pick_Confirm');
2898            DEBUG('Cached primary picked quantity: '|| g_mo_picked_quantity_tbl(l_trolin_rec.line_id).primary_picked_quantity, 'Finalize_Pick_Confirm');
2899            DEBUG('Cached picked uom: '|| g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom, 'Finalize_Pick_Confirm');
2900         END IF;
2901 
2902 	--Bug5950172. Fix starts
2903 	IF (g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom <> l_mmtt_rec.transaction_uom ) THEN
2904           g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity :=  inv_convert.inv_um_convert(
2905                                              item_id                      => l_mmtt_rec.inventory_item_id
2906                                            , precision                    => NULL
2907                                            , from_quantity                => g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity
2908                                            , from_unit                    => g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom
2909                                            , to_unit                      => l_mmtt_rec.transaction_uom
2910                                            , from_name                    => NULL
2911                                            , to_name                      => NULL );
2912             IF (g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity  = -99999) THEN
2913               IF (l_debug = 1) THEN
2914                  DEBUG('Cannot convert uom of cached qty to current transaction_uom ', 'Finalize_Pick_Confirm');
2915               END IF;
2916               fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
2917               fnd_message.set_token('UOM1', g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom );
2918               fnd_message.set_token('UOM2', l_mmtt_rec.transaction_uom);
2919               fnd_message.set_token('ROUTINE', 'Pick Confirm process');
2920               fnd_msg_pub.ADD;
2921               RAISE fnd_api.g_exc_unexpected_error;
2922             END IF;
2923 	   g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom      := l_mmtt_rec.transaction_uom ;
2924 	END If;
2925       	--Bug5950172. Fix Ends
2926 
2927         l_pending_quantity                                                      := l_pending_quantity - g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity;
2928         l_sec_pending_quantity                                                  := l_sec_pending_quantity - g_mo_picked_quantity_tbl(l_trolin_rec.line_id).sec_picked_quantity;
2929         l_primary_pending_quantity                                              := l_primary_pending_quantity - g_mo_picked_quantity_tbl(l_trolin_rec.line_id).primary_picked_quantity;
2930         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity          := g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity + ABS(l_mmtt_rec.transaction_quantity);
2931         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).sec_picked_quantity      := g_mo_picked_quantity_tbl(l_trolin_rec.line_id).sec_picked_quantity + ABS(l_mmtt_rec.secondary_transaction_quantity);
2932         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).primary_picked_quantity  := g_mo_picked_quantity_tbl(l_trolin_rec.line_id).primary_picked_quantity + ABS(l_mmtt_rec.primary_quantity);
2933       ELSE
2934         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_quantity          := ABS(l_mmtt_rec.transaction_quantity);
2935         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).sec_picked_quantity      := ABS(l_mmtt_rec.secondary_transaction_quantity);
2936         g_mo_picked_quantity_tbl(l_trolin_rec.line_id).primary_picked_quantity  := ABS(l_mmtt_rec.primary_quantity);
2937 	g_mo_picked_quantity_tbl(l_trolin_rec.line_id).picked_uom               := l_mmtt_rec.transaction_uom ; --Bug5950172
2938       END IF;
2939 
2940       l_remaining_quantity  := l_pending_quantity - ABS(l_mmtt_rec.transaction_quantity);
2941       l_sec_remaining_quantity  := l_sec_pending_quantity - ABS(l_mmtt_rec.secondary_transaction_quantity);
2942       IF (l_debug = 1) THEN
2943          DEBUG('Remaining Quantity: '|| l_remaining_quantity, 'Finalize_Pick_Confirm');
2944          DEBUG('Sec Remaining Quantity: '|| l_sec_remaining_quantity, 'Finalize_Pick_Confirm');
2945       END IF;
2946 
2947       IF NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity) < l_trolin_rec.quantity THEN
2948         l_pending_quantity  := LEAST(l_pending_quantity, l_trolin_rec.required_quantity - l_trolin_rec.quantity_delivered + l_shipping_quantity);
2949         l_sec_pending_quantity  := LEAST(l_sec_pending_quantity, l_trolin_rec.secondary_required_quantity - l_trolin_rec.secondary_quantity_delivered + l_sec_shipping_quantity);
2950 
2951         IF l_pending_quantity < 0 THEN
2952           l_pending_quantity  := 0;
2953           l_sec_pending_quantity  := 0;
2954         END IF;
2955       END IF;
2956 
2957       IF (l_debug = 1) THEN
2958          DEBUG('Pending Quantity: '|| l_pending_quantity, 'Finalize_Pick_Confirm');
2959          DEBUG('Sec Pending Quantity: '|| l_sec_pending_quantity, 'Finalize_Pick_Confirm');
2960       END IF;
2961 
2962       IF  (l_mmtt_rec.transaction_uom <> l_mmtt_rec.item_primary_uom_code)
2963           AND l_pending_quantity <> 0 THEN
2964         l_primary_pending_quantity  := inv_convert.inv_um_convert(
2965                                          item_id                      => l_mmtt_rec.inventory_item_id
2966                                        , PRECISION                    => NULL
2967                                        , from_quantity                => l_pending_quantity
2968                                        , from_unit                    => l_mmtt_rec.transaction_uom
2969                                        , to_unit                      => l_mmtt_rec.item_primary_uom_code
2970                                        , from_name                    => NULL
2971                                        , to_name                      => NULL
2972                                        );
2973 
2974         IF (l_primary_pending_quantity = -99999) THEN
2975           IF (l_debug = 1) THEN
2976              DEBUG('Cannot convert uom to primary uom', 'Finalize_Pick_Confirm');
2977           END IF;
2978           fnd_message.set_name('INV', 'INV-CANNOT CONVERT');
2979           fnd_message.set_token('UOM', l_mtl_reservation_rec.primary_uom_code);
2980           fnd_message.set_token('ROUTINE', 'Pick Confirm process');
2981           fnd_msg_pub.ADD;
2982           RAISE fnd_api.g_exc_unexpected_error;
2983         END IF;
2984       ELSE
2985         l_primary_pending_quantity  := l_pending_quantity;
2986       END IF;
2987 
2988       IF l_shipping_quantity = ABS(l_mmtt_rec.transaction_quantity) THEN
2989         l_remaining_shipping_quantity  := l_remaining_quantity;
2990       ELSE
2991         l_remaining_shipping_quantity  := 0;
2992       END IF;
2993 
2994       IF (l_debug = 1) THEN
2995          DEBUG('remaining shipping quantity = '|| l_remaining_shipping_quantity, 'Finalize_Pick_Confirm');
2996       END IF;
2997 
2998       IF l_mmtt_rec.reservation_id IS NOT NULL THEN
2999         SELECT NVL(SUM(ABS(primary_quantity)), 0)
3000             ,  NVL(SUM(ABS(secondary_transaction_quantity)), 0)
3001           INTO l_rsv_primary_quantity
3002             ,  l_sec_rsv_quantity
3003           FROM mtl_material_transactions_temp
3004          WHERE reservation_id = l_mmtt_rec.reservation_id;
3005 
3006         IF g_rsv_picked_quantity_tbl.EXISTS(l_mmtt_rec.reservation_id) THEN
3007           l_rsv_primary_quantity
3008                     := l_rsv_primary_quantity
3009                           - g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).picked_quantity;
3010           l_sec_rsv_quantity
3011                     := l_sec_rsv_quantity
3012                           - g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).sec_picked_quantity;
3013           g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).picked_quantity
3014                     := g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).picked_quantity
3015                              + ABS(l_mmtt_rec.primary_quantity);
3016           g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).sec_picked_quantity
3017                     := g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).sec_picked_quantity
3018                              + ABS(l_mmtt_rec.secondary_transaction_quantity);
3019         ELSE
3020           g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).picked_quantity
3021                     := ABS(l_mmtt_rec.primary_quantity);
3022           g_rsv_picked_quantity_tbl(l_mmtt_rec.reservation_id).sec_picked_quantity
3023                     := ABS(l_mmtt_rec.secondary_transaction_quantity);
3024         END IF;
3025       ELSE
3026         l_rsv_primary_quantity  := 0;
3027         l_sec_rsv_quantity  := 0;
3028       END IF;
3029 
3030       IF (l_debug = 1) THEN
3031          DEBUG('pending quantity = '|| l_pending_quantity, 'Finalize_Pick_Confirm');
3032          DEBUG('sec pending quantity = '|| l_sec_pending_quantity, 'Finalize_Pick_Confirm');
3033          DEBUG('prim pending quantity = '|| l_primary_pending_quantity, 'Finalize_Pick_Confirm');
3034          DEBUG('remaining quantity = '|| l_remaining_quantity, 'Finalize_Pick_Confirm');
3035          DEBUG('sec remaining quantity = '|| l_sec_remaining_quantity, 'Finalize_Pick_Confirm');
3036          DEBUG('total allocatd rsv quantity = '|| l_rsv_primary_quantity, 'Finalize_Pick_Confirm');
3037          DEBUG('total sec allocatd rsv quantity = '|| l_sec_rsv_quantity, 'Finalize_Pick_Confirm');
3038       END IF;
3039     END IF; -- Move order type IN 3,5
3040 
3041     -- The move order lines are closed when
3042     -- a.  for pick wave and WIP move orders, the pending quantity = 0
3043     -- b.  for all other move orders, when quantity delivered >
3044     --     quantity requested
3045     -- Bug 2666620: BackFlush MO Type Removed
3046     IF (l_move_order_type IN (3, 5) AND l_remaining_quantity = 0)
3047        OR (l_move_order_type NOT IN (3, 5) AND l_trolin_rec.quantity_delivered >= l_trolin_rec.quantity)
3048     THEN
3049       -- If it is a WIP move order, and the line has been underpicked,
3050       -- unallocate the balance
3051       -- Bug 2666620: BackFlush MO Type Removed
3052       IF l_move_order_type = 5 and l_wip_entity_type not in (9,10) THEN   -- only for WIP not for GME
3053         l_unalloc_quantity  := l_trolin_rec.quantity - l_trolin_rec.quantity_delivered;
3054 
3055         IF l_unalloc_quantity > 0 THEN
3056           IF l_trolin_rec.uom_code <> l_mmtt_rec.item_primary_uom_code THEN
3057             l_unalloc_quantity  := inv_convert.inv_um_convert(
3058                                      item_id                      => l_mmtt_rec.inventory_item_id
3059                                    , PRECISION                    => NULL
3060                                    , from_quantity                => l_unalloc_quantity
3061                                    , from_unit                    => l_mmtt_rec.transaction_uom
3062                                    , to_unit                      => l_mmtt_rec.item_primary_uom_code
3063                                    , from_name                    => NULL
3064                                    , to_name                      => NULL
3065                                    );
3066 
3067             IF (l_unalloc_quantity = -99999) THEN
3068               IF (l_debug = 1) THEN
3069                  DEBUG('Calculating unalloc qty: cannot convert uom to primary uom', 'Finalize_Pick_Confirm');
3070               END IF;
3071               fnd_message.set_name('INV', 'INV-CANNOT CONVERT');
3072               fnd_message.set_token('UOM', l_mmtt_rec.item_primary_uom_code);
3073               fnd_message.set_token('ROUTINE', 'Pick Confirm process');
3074               fnd_msg_pub.ADD;
3075               RAISE fnd_api.g_exc_unexpected_error;
3076             END IF;
3077           END IF; -- if UOM code not primary UOM
3078 
3079           IF (l_debug = 1) THEN
3080              DEBUG(
3081                'Calling wip_picking_pub.unallocate_material with '
3082             || 'WIP entity ID='
3083             || TO_CHAR(l_trolin_rec.txn_source_id)
3084             || ', oper seq num='
3085             || TO_CHAR(l_trolin_rec.txn_source_line_id)
3086             || ', item ID='
3087             || TO_CHAR(l_trolin_rec.inventory_item_id)
3088             || ', rep sch ID='
3089             || TO_CHAR(l_trolin_rec.reference_id)
3090             || ', unalloc qty='
3091             || TO_CHAR(l_unalloc_quantity)
3092           , 'Finalize_Pick_Confirm'
3093           );
3094           END IF;
3095           wip_picking_pub.unallocate_material(
3096             p_wip_entity_id              => l_trolin_rec.txn_source_id
3097           , p_operation_seq_num          => l_trolin_rec.txn_source_line_id
3098           , p_inventory_item_id          => l_trolin_rec.inventory_item_id
3099           , p_repetitive_schedule_id     => l_trolin_rec.reference_id
3100           , p_primary_quantity           => l_unalloc_quantity
3101           , x_return_status              => l_return_status
3102           , x_msg_data                   => l_msg_data
3103           );
3104 
3105           IF l_return_status = 'L' THEN
3106             IF (l_debug = 1) THEN
3107                DEBUG('Error from wip_picking_pub.unallocate_material', 'Finalize_Pick_Confirm');
3108                DEBUG('Unable to lock the work order line for update', 'Finalize_Pick_Confirm');
3109             END IF;
3110             fnd_message.set_name('INV', 'INV_WIP_WORK_ORDER_LOCKED');
3111             fnd_msg_pub.ADD;
3112             RAISE fnd_api.g_exc_error;
3113           ELSIF l_return_status <> fnd_api.g_ret_sts_success THEN
3114             IF (l_debug = 1) THEN
3115                DEBUG('Error from wip_picking_pub.unallocate_material', 'Finalize_Pick_Confirm');
3116             END IF;
3117             RAISE fnd_api.g_exc_unexpected_error;
3118           END IF;
3119         END IF; -- if unalloc qty > 0
3120       END IF; -- if WIP move order (MO type is 5 or 7)
3121 
3122       IF (l_debug = 1) THEN
3123          DEBUG('old line_status is '|| l_trolin_rec.line_status, 'Finalize_Pick_Confirm');
3124          DEBUG('MO Line ID:'||TO_CHAR(l_trolin_rec.line_id)
3125                ||' TxnTmpID:'||TO_CHAR(l_mmtt_rec.transaction_temp_id), 'Finalize_Pick_Confirm');
3126       END IF;
3127       --
3128       -- Start Bug 4756651
3129       --
3130       BEGIN
3131        --
3132        SELECT COUNT(*)
3133        INTO l_other_mmtt_rec
3134        FROM mtl_material_transactions_temp
3135        WHERE move_order_line_id = l_trolin_rec.line_id
3136        AND transaction_temp_id <> l_mmtt_rec.transaction_temp_id;
3137        --
3138       EXCEPTION
3139        --
3140        WHEN NO_DATA_FOUND THEN
3141         l_other_mmtt_rec := 0;
3142       END;
3143       --
3144       IF (l_move_order_type = 2 AND l_other_mmtt_rec > 0) THEN
3145         IF (l_debug = 1) THEN
3146            DEBUG('Other allocations exist. Do not close the MO Line yet.', 'Finalize_Pick_Confirm');
3147         END IF;
3148       ELSE
3149         /* Bug:4994950(Actual bug is 4762505). Added the complete logic in the following IF condition
3150          * to consider the g_mmtt_cache table to close the move order line.
3151          */
3152         -- If there are pending allocations do not close the move order line
3153         IF (l_move_order_type <> 2 AND l_other_mmtt_rec > 0) THEN
3154           --Bug 4994950 :Start of code changes
3155           IF (g_mmtt_cache_tbl.EXISTS(l_trolin_rec.line_id) AND g_mmtt_cache_tbl(l_trolin_rec.line_id) > 0) THEN
3156             IF (l_debug = 1) THEN
3157               DEBUG('Other MMTT Count: ' || l_other_mmtt_rec, 'Finalize_Pick_Confirm');
3158               DEBUG('Processed MMTT Count: ' || g_mmtt_cache_tbl(l_trolin_rec.line_id), 'Finalize_Pick_Confirm');
3159             END IF;
3160             IF (l_other_mmtt_rec <> g_mmtt_cache_tbl(l_trolin_rec.line_id)) THEN
3161               IF (l_debug = 1) THEN
3162                 DEBUG('There are other pending MMTTs yet to be processed. Do not close MO line', 'Finalize_Pick_Confirm');
3163               END IF;
3164             ELSE
3165               IF (l_debug = 1) THEN
3166                 DEBUG('All other MMTTs processed. Set MO line status to closed', 'Finalize_Pick_Confirm');
3167               END IF;
3168               l_trolin_rec.line_status  := 5;
3169               l_mo_line_closed_flag  :=TRUE;
3170               g_mmtt_cache_tbl.DELETE(l_trolin_rec.line_id);
3171             END IF;   --END IF l_other_mmtt_rec <> g_mmtt_cache_tbl.COUNT
3172           ELSE
3173             IF (l_debug = 1) THEN
3174               DEBUG('There are other pending MMTTs yet to be processed and cache is empty. Do not close MO line', 'Finalize_Pick_Confirm');
3175             END IF;
3176           END IF;   --END IF g_mmtt_cache_tbl.COUNT > 0
3177         --Bug 4994950 : End of code changes
3178         --No other pending allocations
3179         ELSE
3180           l_trolin_rec.line_status  := 5;
3181           l_mo_line_closed_flag :=TRUE;      --Bug:4994950
3182           IF (l_debug = 1) THEN
3183            DEBUG('Closing move order line', 'Finalize_Pick_Confirm');
3184           END IF;
3185           g_mmtt_cache_tbl.DELETE(l_trolin_rec.line_id);  --Bug:4994950
3186         END IF;
3187       END IF;
3188       --
3189       -- End Bug 4756651
3190       --
3191       -- For pick wave move orders, if we underpick, update the
3192       -- requested quantity of the move order line to reflect the actual
3193       -- quantity moved
3194       IF l_trolin_rec.quantity > l_trolin_rec.quantity_delivered THEN
3195         l_trolin_rec.quantity  := l_trolin_rec.quantity_delivered;
3196       END IF;
3197 
3198     END IF;
3199 
3200     l_trolin_rec.transaction_header_id  := l_mmtt_rec.transaction_header_id;
3201     -- Update the Move Order line
3202     IF (l_debug = 1) THEN
3203        DEBUG('calling update_row', 'Finalize_Pick_Confirm');
3204     END IF;
3205     inv_trolin_util.update_row(l_trolin_rec);
3206 
3207     /* Bug:4994950( Actual Bug 4762505). First checking if the current mo_line_id is closed or not.
3208      * Then checking if the  record count exists for the current line_id.
3209      * If exists then increment the mmtt record count.
3210      * Else initialize the count for the current mo_line_id to 1
3211      */
3212     --Bug 4994950: Start of code changes
3213       -- Bug 5059984, Added condition so that g_mmtt_cache_tbl is not updated for requisition move orders
3214 
3215     if (l_mo_line_closed_flag = FALSE ) then
3216       if (l_move_order_type <> 2) then
3217         IF (g_mmtt_cache_tbl.EXISTS(l_trolin_rec.line_id) ) THEN
3218           g_mmtt_cache_tbl(l_trolin_rec.line_id) :=  g_mmtt_cache_tbl(l_trolin_rec.line_id) + 1;
3219         ELSE
3220           g_mmtt_cache_tbl(l_trolin_rec.line_id) := 1 ;
3221         END IF;
3222       else
3223         IF (l_debug = 1) THEN
3224           DEBUG('Replenishment Move Order will not update g_mmtt_cache_tbl', 'Finalize_Pick_Confirm');
3225         END IF;
3226       end if; -- end if check move order type
3227 
3228     end if;   -- end if mo_line_closed_flag
3229     --Bug 4994950: End of code changes
3230 
3231 
3232     IF (l_debug = 1) THEN
3233        DEBUG('reservation id = '|| l_mmtt_rec.reservation_id, 'Finalize_Pick_Confirm');
3234     END IF;
3235 
3236     /*
3237        Start of bug# 5643004
3238        Moved the below query before unmarking serials since,
3239        Unmarking serials requires l_lot_control_code and
3240        l_serial_controlled_code to be initialized.
3241     */
3242 
3243     SELECT lot_control_code
3244            , serial_number_control_code
3245            , reservable_type
3246         INTO l_lot_control_code
3247            , l_serial_control_code
3248            , l_reservable_type_item
3249         FROM mtl_system_items
3250        WHERE inventory_item_id = l_mmtt_rec.inventory_item_id
3251          AND organization_id = l_mmtt_rec.organization_id;
3252 
3253       IF (l_debug = 1) THEN
3254          DEBUG('After select lot_control_code = '|| l_lot_control_code, 'Finalize_Pick_confirm');
3255          DEBUG('After select l_serial_control_code = '|| l_serial_control_code, 'Finalize_Pick_Confirm');
3256       END IF;
3257 
3258     -- Unmark all the serials
3259     IF  (l_move_order_type <> 3 OR l_shipping_quantity <= 0 )
3260         AND l_mmtt_rec.transaction_action_id = 28 THEN
3261       IF (l_debug = 1) THEN
3262          DEBUG('Unmarking Serials ', 'Finalize_Pick_Confirm');
3263       END IF;
3264 
3265       IF (l_lot_control_code > 1 AND l_serial_control_code NOT IN (1, 6)) THEN
3266         -- Lot and serial controlled
3267         OPEN lot_csr (p_transaction_temp_id);
3268 
3269         LOOP
3270           FETCH lot_csr INTO l_lot_number, l_lot_primary_quantity, l_lot_transaction_quantity,
3271                              l_lot_secondary_quantity, l_lot_secondary_uom, l_serial_trx_temp_id,l_grade_code;
3272           EXIT WHEN lot_csr%NOTFOUND;
3273           OPEN serial_csr(p_transaction_temp_id, l_lot_control_code, l_serial_trx_temp_id,
3274                           l_mmtt_rec.organization_id, l_mmtt_rec.inventory_item_id);
3275 
3276           LOOP
3277             FETCH serial_csr INTO l_serial_number;
3278             EXIT WHEN serial_csr%NOTFOUND;
3279             IF (l_debug = 1) THEN
3280                DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
3281             END IF;
3282 
3283            /*** {{ R12 Enhanced reservations code changes,
3284             *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
3285             UPDATE mtl_serial_numbers
3286                SET group_mark_id = NULL
3287              WHERE serial_number = l_serial_number
3288                AND inventory_item_id = l_mmtt_rec.inventory_item_id;
3289             *** End R12 }} ***/
3290 
3291            /*** {{ R12 Enhanced reservations code changes ***/
3292             serial_check.inv_unmark_rsv_serial
3293                 (from_serial_number   => l_serial_number
3294                 ,to_serial_number     => null
3295                 ,serial_code          => null
3296                 ,hdr_id               => null
3297                 ,p_inventory_item_id  => l_mmtt_rec.inventory_item_id
3298                 ,p_update_reservation => fnd_api.g_true);
3299            /*** End R12 }} ***/
3300           END LOOP;
3301 
3302           CLOSE serial_csr;
3303         END LOOP;
3304 
3305         CLOSE lot_csr;
3306       ELSIF  l_lot_control_code = 1
3307              AND l_serial_control_code NOT IN (1, 6) THEN
3308         -- Only Serial controlled
3309         OPEN serial_csr(p_transaction_temp_id, l_lot_control_code, l_serial_trx_temp_id,
3310                         l_mmtt_rec.organization_id, l_mmtt_rec.inventory_item_id);
3311 
3312         LOOP
3313           FETCH serial_csr INTO l_serial_number;
3314           EXIT WHEN serial_csr%NOTFOUND;
3315           IF (l_debug = 1) THEN
3316              DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
3317           END IF;
3318 
3319          /*** {{ R12 Enhanced reservations code changes,
3320           *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
3321           UPDATE mtl_serial_numbers
3322              SET group_mark_id = NULL
3323            WHERE serial_number = l_serial_number
3324              AND inventory_item_id = l_mmtt_rec.inventory_item_id;
3325           *** End R12 }} ***/
3326 
3327          /*** {{ R12 Enhanced reservations code changes ***/
3328           serial_check.inv_unmark_rsv_serial
3329                 (from_serial_number   => l_serial_number
3330                 ,to_serial_number     => null
3331                 ,serial_code          => null
3332                 ,hdr_id               => null
3333                 ,p_inventory_item_id  => l_mmtt_rec.inventory_item_id
3334                 ,p_update_reservation => fnd_api.g_true);
3335          /*** End R12 }} ***/
3336         END LOOP;
3337 
3338         CLOSE serial_csr;
3339       END IF;
3340     END IF;
3341 
3342     IF (l_move_order_type = 3 AND l_shipping_quantity > 0) THEN
3343       IF (l_debug = 1) THEN
3344          DEBUG('inside l_move_ordeR_ytpe = 3', 'Finalize_Pick_Confirm');
3345       END IF;
3346 
3347       -- Bug 5535030: cache subinventory reservable type
3348       l_hash_value := DBMS_UTILITY.get_hash_value
3349                       ( NAME      => to_char(l_mmtt_rec.organization_id)
3350                                      ||'-'|| l_mmtt_rec.transfer_subinventory
3351                       , base      => 1
3352                       , hash_size => POWER(2, 25)
3353                       );
3354       IF g_is_sub_reservable.EXISTS(l_hash_value) AND
3355          g_is_sub_reservable(l_hash_value).org_id = l_mmtt_rec.organization_id AND
3356          g_is_sub_reservable(l_hash_value).subinventory_code = l_mmtt_rec.transfer_subinventory
3357       THEN
3358          l_reservable_type := g_is_sub_reservable(l_hash_value).reservable_type;
3359       ELSE
3360          SELECT reservable_type
3361            INTO l_reservable_type
3362            FROM mtl_secondary_inventories
3363           WHERE organization_id = l_mmtt_rec.organization_id
3364             AND secondary_inventory_name = l_mmtt_rec.transfer_subinventory;
3365          g_is_sub_reservable(l_hash_value).reservable_type := l_reservable_type;
3366          g_is_sub_reservable(l_hash_value).org_id := l_mmtt_rec.organization_id;
3367          g_is_sub_reservable(l_hash_value).subinventory_code := l_mmtt_rec.transfer_subinventory;
3368       END IF;
3369 
3370       -- Bug 1620576 - Overpicking
3371       -- Look at remaining quantity, instead of quantity delivered to set
3372       -- action flag
3373       -- Action flag meanings:
3374       -- M: updates for lot control or serial control
3375       -- S: this update is not the last update, so shipping will have
3376       --    to split the delivery line
3377       -- U: this is the last update to shipping, so shipping can just
3378       --    update the existing delivery detail without splitting
3379       -- If (l_trolin_rec.quantity_delivered < l_trolin_rec.quantity ) Then
3380 
3381       IF l_remaining_shipping_quantity > 0 THEN
3382         IF (l_lot_control_code > 1 OR NVL(l_serial_control_code, 1) NOT IN (1, 6)) THEN
3383           l_action_flag  := 'M';
3384         ELSE
3385           l_action_flag  := 'S';
3386         END IF;
3387       ELSE
3388         IF (l_lot_control_code > 1 OR NVL(l_serial_control_code, 1) NOT IN (1, 6)) THEN
3389           l_action_flag  := 'M';
3390         ELSE
3391           l_action_flag  := 'U';
3392         END IF;
3393       END IF;
3394 
3395       IF (l_debug = 1) THEN
3396          DEBUG('l_action_flag is  = '|| l_action_flag, 'Finalize_Pick_confirm');
3397       END IF;
3398       l_mtl_reservation_tbl_count            := 0;
3399 
3400       IF (l_mmtt_rec.reservation_id IS NOT NULL) THEN
3401         l_query_reservation_rec.reservation_id  := l_mmtt_rec.reservation_id;
3402         IF (l_debug = 1) THEN
3403            DEBUG('about to call inv_reservation_pub.query_reservations ', 'Finalize_Pick_confirm');
3404         END IF;
3405         inv_reservation_pub.query_reservation(
3406           p_api_version_number         => 1.0
3407         , x_return_status              => l_return_status
3408         , x_msg_count                  => l_msg_count
3409         , x_msg_data                   => l_msg_data
3410         , p_query_input                => l_query_reservation_rec
3411         , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
3412         , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
3413         , x_error_code                 => l_error_code
3414         );
3415 
3416         IF l_return_status = fnd_api.g_ret_sts_error THEN
3417           IF (l_debug = 1) THEN
3418              DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
3419           END IF;
3420           RAISE fnd_api.g_exc_error;
3421         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3422           IF (l_debug = 1) THEN
3423              DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
3424           END IF;
3425           RAISE fnd_api.g_exc_unexpected_error;
3426         END IF;
3427 
3428         IF (l_debug = 1) THEN
3429            DEBUG('reservation count = '|| l_mtl_reservation_tbl.COUNT, 'Finalize_Pick_Confirm');
3430            DEBUG('after query_reservation', 'Finalize_Pick_Confirm');
3431         END IF;
3432             -- We need to handle case here where reservation count = 0
3433             -- i.e. the reservation id on the MMTT record is now invalid
3434             -- currently, if reservation count = 0, we raise no data found
3435             -- when we try to get the first record of the reservation table.
3436             -- To solve this problem, add end if and then a new if stmt
3437             -- to make sure query returned a record.  If rec count = 0,
3438             -- create a new reservation on the staging sub.
3439       END IF;
3440 
3441       l_lpn_id                               := NVL(l_mmtt_rec.content_lpn_id, l_mmtt_rec.transfer_lpn_id);
3442 
3443       IF l_mtl_reservation_tbl_count > 0 THEN
3444         l_mtl_reservation_rec  := l_mtl_reservation_tbl(1);
3445         IF (l_debug = 1) THEN
3446            DEBUG('lot number in the original reservation is '|| l_mtl_reservation_rec.lot_number, 'Finalize_Pick_Confirm');
3447         END IF;
3448 
3449         -- Bug 1620576 - Overpicking
3450         -- To support overpicking, we have to increase the reservation
3451         -- quantity when the user enters a transaction quantity which
3452         -- is greater than the requested quantity.
3453         -- We increase the reservation before transferring it to the
3454         -- staging subinventory.
3455 
3456         IF l_rsv_primary_quantity > l_mtl_reservation_rec.primary_reservation_quantity THEN
3457           IF (l_debug = 1) THEN
3458              DEBUG('Increasing reservation quantity for overpicking', 'Finalize_Pick_Confirm');
3459              DEBUG('Old rsv prim. quantity: '|| l_mtl_reservation_rec.primary_reservation_quantity, 'Finalize_Pick_Confirm');
3460              DEBUG('New rsv prim. quantity: '|| l_rsv_primary_quantity, 'Finalize_Pick_Confirm');
3461           END IF;
3462           l_mtl_reservation_rec.primary_reservation_quantity     := l_rsv_primary_quantity;
3463           l_mtl_reservation_rec.detailed_quantity                := l_rsv_primary_quantity;
3464           /*Bug 5436227/5436033 Pass only primary qty and let reservation apis calculate secondary qty
3465           This is beacuse we do not know whether the reservation is a High level or lot level and
3466           hence the uom conversion to be used i.e lot or item conversion, will be determined by reservation api*/
3467           --l_mtl_reservation_rec.secondary_reservation_quantity   := l_sec_rsv_quantity;
3468           --l_mtl_reservation_rec.secondary_detailed_quantity      := l_sec_rsv_quantity;
3469           l_mtl_reservation_rec.secondary_reservation_quantity   := NULL;
3470           l_mtl_reservation_rec.secondary_detailed_quantity      := NULL;
3471           l_mtl_reservation_rec.reservation_quantity             := NULL;
3472 
3473 --INVCONV - Make sure Qty2 are NULL if nor present
3474         IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
3475               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
3476               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
3477         END IF;
3478           inv_reservation_pub.update_reservation(
3479             p_api_version_number         => 1.0
3480           , p_init_msg_lst               => fnd_api.g_false
3481           , x_return_status              => l_return_status
3482           , x_msg_count                  => x_msg_count
3483           , x_msg_data                   => x_msg_data
3484           , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
3485           , p_to_rsv_rec                 => l_mtl_reservation_rec
3486           , p_original_serial_number     => l_original_serial_number
3487           , p_to_serial_number           => l_to_serial_number
3488           , p_validation_flag            => fnd_api.g_true -- Explicitly set the validation flag to true with respect to the Bug 4004597
3489           , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
3490 	  );
3491           IF (l_debug = 1) THEN
3492              DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
3493           END IF;
3494 
3495           IF l_return_status = fnd_api.g_ret_sts_error THEN
3496             RAISE fnd_api.g_exc_error;
3497           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3498             RAISE fnd_api.g_exc_unexpected_error;
3499           END IF;
3500 
3501           --requery reservation to reflect updated data
3502           inv_reservation_pub.query_reservation(
3503             p_api_version_number         => 1.0
3504           , x_return_status              => l_return_status
3505           , x_msg_count                  => l_msg_count
3506           , x_msg_data                   => l_msg_data
3507           , p_query_input                => l_query_reservation_rec
3508           , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
3509           , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
3510           , x_error_code                 => l_error_code
3511           );
3512 
3513           IF l_return_status = fnd_api.g_ret_sts_error THEN
3514             IF (l_debug = 1) THEN
3515                DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
3516             END IF;
3517             RAISE fnd_api.g_exc_error;
3518           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3519             IF (l_debug = 1) THEN
3520                DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
3521             END IF;
3522             RAISE fnd_api.g_exc_unexpected_error;
3523           END IF;
3524         END IF;
3525 
3526         -- Create a new reservation on a staging sub
3527         -- for the transaction_quantity;
3528         -- initialize l_mtl_reservation_rec with the record transacted.
3529         -- change the the value of changed attributes
3530         -- in l_mtl_reservation_rec
3531         IF (l_debug = 1) THEN
3532            DEBUG('l_mmtt_rec.transaction_temp_id is '|| l_mmtt_rec.transaction_temp_id, 'Finalize_Pick_confirm');
3533         END IF;
3534 
3535         SELECT COUNT(*) INTO l_lot_count
3536           FROM mtl_transaction_lots_temp
3537          WHERE transaction_temp_id = p_transaction_temp_id;
3538 
3539         IF (l_debug = 1) THEN
3540            DEBUG('l_lot_count is '|| l_lot_count, 'Finalize_Pick_Confirm');
3541         END IF;
3542 
3543 
3544         /*** {{ R12 Enhanced reservations code changes ***/
3545         -- If we delete/update reservation for unreservable subinventory, and serial number
3546         -- is reserved for the reservation, we want to null out the reservation_id for
3547         -- the serial number in MSN but retain the marked group_mark_id
3548         -- therefore we will null out the reservation_id in MSN before calling update_reservation
3549         -- so that the reservation API will not null out both reservation_id and group_mark_id.
3550         -- For reservable subinventory, there is no need to retain the reservation_id of
3551         -- the serial number after we finalize pick confirm because the reserved serials are
3552         -- being moved to staging sub, but we still want to retain group_mark_id
3553         -- the reservation_id will be null out for the reserved serial number
3554         -- when we transfer to staging sub to reduce the complexity of handling
3555         -- reserved serial number after staging
3556         IF (l_mtl_reservation_tbl(1).reservation_id is not NULL) THEN
3557            IF (l_lot_count > 0 AND l_serial_control_code NOT IN (1, 6)) THEN
3558               -- lot and serial controlled
3559 
3560               l_serial_count := 0;
3561 
3562               OPEN lot_csr(p_transaction_temp_id);
3563 
3564               LOOP
3565                 FETCH lot_csr INTO l_lot_number, l_lot_primary_quantity, l_lot_transaction_quantity,
3566                                    l_lot_secondary_quantity, l_lot_secondary_uom, l_serial_trx_temp_id, l_grade_code;
3567                 EXIT WHEN lot_csr%NOTFOUND;
3568 
3569 
3570                 OPEN serial_reserved_csr(l_serial_trx_temp_id, l_mtl_reservation_tbl(1).reservation_id);
3571 
3572                 LOOP
3573                   FETCH serial_reserved_csr INTO l_serial_number;
3574                   EXIT WHEN serial_reserved_csr%NOTFOUND;
3575                   IF (l_debug = 1) THEN
3576                      DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
3577                   END IF;
3578 
3579                   UPDATE mtl_serial_numbers
3580                   SET    reservation_id = null
3581                   WHERE  reservation_id = l_mtl_reservation_tbl(1).reservation_id
3582                   AND    serial_number = l_serial_number;
3583 
3584                   l_serial_count := SQL%ROWCOUNT + l_serial_count;
3585                 END LOOP;
3586 
3587                 CLOSE serial_reserved_csr;
3588 
3589               END LOOP;
3590 
3591               CLOSE lot_csr;
3592 
3593               IF (l_debug = 1) THEN
3594                  DEBUG('serial count with updated null reservation_id = ' || l_serial_count, 'Finalize_Pick_Confirm');
3595               END IF;
3596 
3597               IF (l_serial_count > 0) THEN
3598                   UPDATE mtl_reservations
3599                   SET    serial_reservation_quantity = serial_reservation_quantity - l_serial_count
3600                   WHERE  reservation_id = l_mtl_reservation_tbl(1).reservation_id;
3601               END IF;
3602 
3603            ELSIF (l_lot_control_code = 1 AND l_serial_control_code NOT IN (1, 6)) THEN
3604               -- serial controlled only
3605               l_serial_count := 0;
3606 
3607               OPEN serial_reserved_csr(p_transaction_temp_id, l_mtl_reservation_tbl(1).reservation_id);
3608 
3609               LOOP
3610                 FETCH serial_reserved_csr INTO l_serial_number;
3611                 EXIT WHEN serial_reserved_csr%NOTFOUND;
3612                 IF (l_debug = 1) THEN
3613                    DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
3614                 END IF;
3615 
3616                 UPDATE mtl_serial_numbers
3617                 SET    reservation_id = null
3618                 WHERE  reservation_id = l_mtl_reservation_tbl(1).reservation_id
3619                 AND    serial_number = l_serial_number;
3620 
3621                 l_serial_count := SQL%ROWCOUNT + l_serial_count;
3622               END LOOP;
3623 
3624               CLOSE serial_reserved_csr;
3625 
3626               IF (l_debug = 1) THEN
3627                  DEBUG('serial count with updated null reservation_id = ' || l_serial_count, 'Finalize_Pick_Confirm');
3628               END IF;
3629 
3630               IF (l_serial_count > 0) THEN
3631                   UPDATE mtl_reservations
3632                   SET    serial_reservation_quantity = serial_reservation_quantity - l_serial_count
3633                   WHERE  reservation_id = l_mtl_reservation_tbl(1).reservation_id;
3634               END IF;
3635            END IF;
3636         END IF;
3637         /*** End R12 }} ***/
3638 
3639         IF (l_reservable_type = 2) THEN
3640           IF (l_debug = 1) THEN
3641              DEBUG('not reservable staging subinventory, '|| 'delete org wide reservation', 'Finalize_Pick_Confirm');
3642           END IF;
3643           l_mtl_reservation_rec                       := l_mtl_reservation_tbl(1);
3644           -- reservation quantity should be NULL; it will be
3645           -- determined based on primary quantity
3646           l_mtl_reservation_rec.reservation_quantity  := NULL;
3647 
3648           IF NVL(l_mtl_reservation_rec.primary_reservation_quantity, 0) > ABS(l_mmtt_rec.primary_quantity) THEN
3649             l_mtl_reservation_rec.primary_reservation_quantity
3650                              := NVL(l_mtl_reservation_rec.primary_reservation_quantity, 0)
3651                                       - ABS(l_mmtt_rec.primary_quantity);
3652             l_mtl_reservation_rec.secondary_reservation_quantity
3653                              := NVL(l_mtl_reservation_rec.secondary_reservation_quantity, 0)
3654                                       - ABS(l_mmtt_rec.secondary_transaction_quantity);
3655           ELSE -- if qty > rsv qty, delete reservation
3656             l_mtl_reservation_rec.primary_reservation_quantity  := 0;
3657             l_mtl_reservation_rec.secondary_reservation_quantity  := 0;
3658           END IF;
3659 
3660           --need to decrement from detailed quantity
3661           IF NVL(l_mtl_reservation_rec.detailed_quantity, 0) > ABS(l_mmtt_rec.primary_quantity) THEN
3662             l_mtl_reservation_rec.detailed_quantity
3663                              := NVL(l_mtl_reservation_rec.detailed_quantity, 0)
3664                                       - ABS(l_mmtt_rec.primary_quantity);
3665             l_mtl_reservation_rec.secondary_detailed_quantity
3666                              := NVL(l_mtl_reservation_rec.secondary_detailed_quantity, 0)
3667                                       - ABS(l_mmtt_rec.secondary_transaction_quantity);
3668           ELSE
3669             l_mtl_reservation_rec.detailed_quantity  := 0;
3670             l_mtl_reservation_rec.secondary_detailed_quantity  := 0;
3671           END IF;
3672 
3673           IF (l_debug = 1) THEN
3674              DEBUG('primary reservation quantity is '|| l_mtl_reservation_rec.primary_reservation_quantity, 'Finalize_Pick_Confirm');
3675           END IF;
3676 
3677 --INVCONV - Make sure Qty2 are NULL if nor present
3678           IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
3679                 l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
3680                 l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
3681           END IF;
3682 
3683           inv_reservation_pub.update_reservation(
3684             p_api_version_number         => 1.0
3685           , p_init_msg_lst               => fnd_api.g_false
3686           , x_return_status              => l_return_status
3687           , x_msg_count                  => x_msg_count
3688           , x_msg_data                   => x_msg_data
3689           , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
3690           , p_to_rsv_rec                 => l_mtl_reservation_rec
3691           , p_original_serial_number     => l_original_serial_number
3692           , p_to_serial_number           => l_to_serial_number
3693           , p_validation_flag            => fnd_api.g_true
3694           , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
3695           );
3696           IF (l_debug = 1) THEN
3697              DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
3698           END IF;
3699 
3700           IF l_return_status = fnd_api.g_ret_sts_error THEN
3701             RAISE fnd_api.g_exc_error;
3702           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3703             RAISE fnd_api.g_exc_unexpected_error;
3704           END IF;
3705         ELSE
3706           IF (l_debug = 1) THEN
3707              DEBUG('reservable staging subinventory, '|| 'transfer reservation to staging', 'Finalize_Pick_Confirm');
3708           END IF;
3709 
3710           IF (l_lot_count > 0) THEN
3711             l_transaction_temp_id         := l_mmtt_rec.transaction_temp_id;
3712             l_lot_shipping_quantity       := 0;
3713             l_lot_prim_shipping_quantity  := 0;
3714             OPEN lot_csr(p_transaction_temp_id);
3715 
3716             LOOP
3717               FETCH lot_csr
3718               INTO l_lot_number
3719                  , l_lot_primary_quantity
3720                  , l_lot_transaction_quantity
3721                  , l_lot_secondary_quantity
3722                  , l_lot_secondary_uom
3723                  , l_serial_trx_temp_id
3724                  , l_grade_code;
3725               l_lot_shipping_quantity       := l_lot_shipping_quantity + l_lot_transaction_quantity;
3726               l_sec_lot_shipping_quantity   := l_sec_lot_shipping_quantity + l_lot_secondary_quantity;
3727               l_lot_prim_shipping_quantity  := l_lot_prim_shipping_quantity + l_lot_primary_quantity;
3728 
3729               IF l_lot_shipping_quantity > l_shipping_quantity THEN
3730                 l_lot_transaction_quantity  := l_lot_transaction_quantity
3731                                                - (l_lot_shipping_quantity - l_shipping_quantity);
3732                 l_lot_primary_quantity      := l_lot_primary_quantity
3733                                                - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
3734                 l_lot_secondary_quantity  := l_lot_secondary_quantity
3735                                                - (l_sec_lot_shipping_quantity - l_sec_shipping_quantity);
3736               END IF;
3737 
3738               EXIT WHEN lot_csr%NOTFOUND
3739                      OR l_lot_transaction_quantity <= 0;
3740               IF (l_debug = 1) THEN
3741                  DEBUG('lot number is '|| l_mtl_reservation_rec.lot_number, 'Finalize_Pick_Confirm');
3742               END IF;
3743               l_mtl_reservation_rec.reservation_id                := NULL;
3744 
3745               -- bug 3703983
3746               --l_mtl_reservation_rec.requirement_date              := SYSDATE;
3747               l_mtl_reservation_rec.primary_reservation_quantity  := ABS(l_lot_primary_quantity);
3748               l_mtl_reservation_rec.reservation_quantity          := ABS(l_lot_transaction_quantity);
3749               l_mtl_reservation_rec.reservation_uom_code          := l_mmtt_rec.transaction_uom;
3750               l_mtl_reservation_rec.subinventory_code             := l_mmtt_rec.transfer_subinventory;
3751               l_mtl_reservation_rec.detailed_quantity             := 0;
3752               -- bug 5354515, lgao blank out the qty2 and uom 2 if not tracked by PS
3753               if l_tracking_quantity_ind <> 'P' then   -- dual uom tracked
3754                  l_mtl_reservation_rec.secondary_reservation_quantity  := ABS(l_lot_secondary_quantity);
3755                  l_mtl_reservation_rec.secondary_uom_code            := l_mmtt_rec.secondary_uom_code;
3756                  l_mtl_reservation_rec.secondary_detailed_quantity   := 0;
3757               else   -- primary tracked
3758                  l_mtl_reservation_rec.secondary_reservation_quantity  := null;
3759                  l_mtl_reservation_rec.secondary_uom_code            := '';
3760                  l_mtl_reservation_rec.secondary_detailed_quantity   := null;
3761               end if;
3762               l_mtl_reservation_rec.locator_id                    := l_mmtt_rec.transfer_to_location;
3763               l_mtl_reservation_rec.ship_ready_flag               := 1;
3764               l_mtl_reservation_rec.lot_number                    := l_lot_number;
3765               l_mtl_reservation_rec.revision                      := l_mmtt_rec.revision;
3766 
3767               -- bug 3703983
3768               l_mtl_reservation_rec.staged_flag                   := 'Y';
3769 
3770               --don't reserve LPN in staging sub unless LPN was
3771               -- reserved on original reservation
3772               IF l_mtl_reservation_tbl(1).lpn_id IS NOT NULL THEN
3773                 l_mtl_reservation_rec.lpn_id  := l_lpn_id;
3774               END IF;
3775 
3776               inv_reservation_pub.transfer_reservation(
3777                 p_api_version_number         => 1.0
3778               , p_init_msg_lst               => fnd_api.g_false
3779               , x_return_status              => l_return_status
3780               , x_msg_count                  => x_msg_count
3781               , x_msg_data                   => x_msg_data
3782               , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
3783               , p_to_rsv_rec                 => l_mtl_reservation_rec
3784               , p_original_serial_number     => l_to_serial_number
3785               , p_to_serial_number           => l_to_serial_number
3786               , p_validation_flag            => fnd_api.g_false
3787               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
3788               , x_to_reservation_id          => l_reservation_id
3789               );
3790               IF (l_debug = 1) THEN
3791                  DEBUG('new reservation id is '|| l_reservation_id, 'Finalize_Pick_Confirm');
3792                  DEBUG('after create new  reservation', 'Finalize_Pick_Confirm');
3793                  DEBUG('l_return_status is '|| l_return_status, 'Finalize_Pick_Confirm');
3794               END IF;
3795 
3796               IF l_return_status = fnd_api.g_ret_sts_error THEN
3797                 IF (l_debug = 1) THEN
3798                    DEBUG('return from transfer_reservation with error E', 'Finalize_Pick_Confirm');
3799                 END IF;
3800                 RAISE fnd_api.g_exc_error;
3801               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3802                 IF (l_debug = 1) THEN
3803                    DEBUG('return from transfer_reservation with error U', 'Finalize_Pick_Confirm');
3804                 END IF;
3805                 RAISE fnd_api.g_exc_unexpected_error;
3806               END IF;
3807 
3808               -- bug 3703983
3809               -- inv_staged_reservation_util.update_staged_flag(x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data, p_reservation_id => l_reservation_id, p_staged_flag => 'Y');
3810 
3811               --IF l_return_status = fnd_api.g_ret_sts_error THEN
3812               -- (l_debug = 1) THEN
3813               --    DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
3814               -- END IF;
3815               -- RAISE fnd_api.g_exc_error;
3816               --ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3817               -- IF (l_debug = 1) THEN
3818               --    DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
3819               -- END IF;
3820               -- RAISE fnd_api.g_exc_unexpected_error;
3821               --END IF;
3822             END LOOP;
3823 
3824             IF (l_debug = 1) THEN
3825                DEBUG('after end loop', 'Finalize_Pick_Confirm');
3826             END IF;
3827             CLOSE lot_csr;
3828           ELSE
3829             IF (l_debug = 1) THEN
3830                DEBUG('no lot records', 'Finalize_Pick_Confirm');
3831             END IF;
3832             l_mtl_reservation_rec.reservation_id                := NULL;
3833             -- bug 3703983
3834             --l_mtl_reservation_rec.requirement_date              := SYSDATE;
3835             l_mtl_reservation_rec.primary_reservation_quantity  := l_primary_shipping_quantity;
3836             l_mtl_reservation_rec.reservation_quantity          := l_shipping_quantity;
3837             l_mtl_reservation_rec.reservation_uom_code          := l_mmtt_rec.transaction_uom;
3838             -- bug 5354515, lgao blank out the qty2 and uom 2 if not tracked by PS
3839             if l_tracking_quantity_ind <> 'P' then   -- dual uom tracked
3840               l_mtl_reservation_rec.secondary_reservation_quantity  := l_sec_shipping_quantity;
3841               l_mtl_reservation_rec.secondary_uom_code            := l_mmtt_rec.secondary_uom_code;
3842             else   -- primary tracked
3843               l_mtl_reservation_rec.secondary_reservation_quantity  := null;
3844               l_mtl_reservation_rec.secondary_uom_code            := '';
3845             end if;
3846             l_mtl_reservation_rec.subinventory_code             := l_mmtt_rec.transfer_subinventory;
3847             l_mtl_reservation_rec.detailed_quantity             := NULL;
3848             l_mtl_reservation_rec.secondary_detailed_quantity   := NULL;
3849             l_mtl_reservation_rec.locator_id                    := l_mmtt_rec.transfer_to_location;
3850             l_mtl_reservation_rec.ship_ready_flag               := 1;
3851             l_mtl_reservation_rec.revision                      := l_mmtt_rec.revision;
3852             -- bug 3703983
3853             l_mtl_reservation_rec.staged_flag                   := 'Y';
3854 
3855             --don't reserve LPN in staging sub unless LPN was
3856             -- reserved on original reservation
3857             IF l_mtl_reservation_tbl(1).lpn_id IS NOT NULL THEN
3858               l_mtl_reservation_rec.lpn_id  := l_lpn_id;
3859             END IF;
3860 
3861             IF (l_debug = 1) THEN
3862                DEBUG('l_primary_shipping_quantity: '|| l_primary_shipping_quantity, 'Finalize_Pick_Confirm');
3863             END IF;
3864 
3865             inv_reservation_pub.transfer_reservation(
3866               p_api_version_number         => 1.0
3867             , p_init_msg_lst               => fnd_api.g_false
3868             , x_return_status              => l_return_status
3869             , x_msg_count                  => x_msg_count
3870             , x_msg_data                   => x_msg_data
3871             , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
3872             , p_to_rsv_rec                 => l_mtl_reservation_rec
3873             , p_original_serial_number     => l_to_serial_number
3874             , p_to_serial_number           => l_to_serial_number
3875             , p_validation_flag            => fnd_api.g_false
3876             , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
3877             , x_to_reservation_id          => l_reservation_id
3878             );
3879 
3880             IF (l_debug = 1) THEN
3881                DEBUG('new reservation id is '|| l_reservation_id, 'Finalize_Pick_Confirm');
3882                DEBUG('after create new reservation', 'Finalize_Pick_Confirm');
3883                DEBUG('l_return_status is '|| l_return_status, 'Finalize_Pick_Confirm');
3884             END IF;
3885 
3886             IF l_return_status = fnd_api.g_ret_sts_error THEN
3887               IF (l_debug = 1) THEN
3888                  DEBUG('return from transfer_reservation with error E', 'Finalize_Pick_Confirm');
3889               END IF;
3890               RAISE fnd_api.g_exc_error;
3891             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3892               IF (l_debug = 1) THEN
3893                  DEBUG('return from transfer_reservation with error U', 'Finalize_Pick_Confirm');
3894               END IF;
3895               RAISE fnd_api.g_exc_unexpected_error;
3896             END IF;
3897 
3898             IF (l_debug = 1) THEN
3899                DEBUG('still inside if no lot records', 'Finalize_Pick_Confirm');
3900             END IF;
3901 
3902             -- bug 3703983
3903             --inv_staged_reservation_util.update_staged_flag(x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data, p_reservation_id => l_reservation_id, p_staged_flag => 'Y');
3904 
3905             --IF l_return_status = fnd_api.g_ret_sts_error THEN
3906             --  IF (l_debug = 1) THEN
3907             --     DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
3908             --  END IF;
3909             --  RAISE fnd_api.g_exc_error;
3910             --ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3911             --  IF (l_debug = 1) THEN
3912             --     DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
3913             --  END IF;
3914             --  RAISE fnd_api.g_exc_unexpected_error;
3915             --END IF;
3916           END IF; -- lot or not lot control
3917         END IF; -- reservable or not
3918       ELSE -- query reservation returns 0 records
3919         -- Reservation does not exist, we need to create one if the
3920         -- staging sub is reservable and item is reservable
3921         IF  l_reservable_type = 1 AND l_reservable_type_item = 1 THEN
3922           -- If the staging subinventory  is reservable
3923           l_mtl_reservation_rec.reservation_id             := NULL; -- cannot know
3924           l_mtl_reservation_rec.organization_id            := l_mmtt_rec.organization_id;
3925           l_mtl_reservation_rec.inventory_item_id          := l_mmtt_rec.inventory_item_id;
3926           l_mtl_reservation_rec.demand_source_type_id      := l_mmtt_rec.transaction_source_type_id;
3927           l_mtl_reservation_rec.demand_source_name         := NULL;
3928           l_mtl_reservation_rec.demand_source_header_id    := l_mmtt_rec.transaction_source_id;
3929           l_mtl_reservation_rec.demand_source_line_id      := TO_NUMBER(l_mmtt_rec.trx_source_line_id);
3930           l_mtl_reservation_rec.demand_source_delivery     := NULL;
3931           l_mtl_reservation_rec.primary_uom_code           := l_mmtt_rec.item_primary_uom_code;
3932           l_mtl_reservation_rec.secondary_uom_code         := l_mmtt_rec.secondary_uom_code;
3933           l_mtl_reservation_rec.primary_uom_id             := NULL;
3934           l_mtl_reservation_rec.secondary_uom_id           := NULL;
3935           l_mtl_reservation_rec.reservation_uom_code       := l_mmtt_rec.transaction_uom;
3936           l_mtl_reservation_rec.reservation_uom_id         := NULL;
3937           l_mtl_reservation_rec.autodetail_group_id        := NULL;
3938           l_mtl_reservation_rec.external_source_code       := NULL;
3939           l_mtl_reservation_rec.external_source_line_id    := NULL;
3940           l_mtl_reservation_rec.supply_source_type_id      := inv_reservation_global.g_source_type_inv;
3941           l_mtl_reservation_rec.supply_source_header_id    := NULL;
3942           l_mtl_reservation_rec.supply_source_line_id      := NULL;
3943           l_mtl_reservation_rec.supply_source_name         := NULL;
3944           l_mtl_reservation_rec.supply_source_line_detail  := NULL;
3945           l_mtl_reservation_rec.revision                   := l_mmtt_rec.revision;
3946           l_mtl_reservation_rec.subinventory_code          := l_mmtt_rec.transfer_subinventory;
3947           l_mtl_reservation_rec.subinventory_id            := NULL;
3948           l_mtl_reservation_rec.locator_id                 := l_mmtt_rec.transfer_to_location;
3949           l_mtl_reservation_rec.lot_number_id              := NULL;
3950           l_mtl_reservation_rec.pick_slip_number           := NULL;
3951           --we reserve LPN only on user created reservations
3952           l_mtl_reservation_rec.lpn_id                     := NULL;
3953           l_mtl_reservation_rec.attribute_category         := NULL;
3954           l_mtl_reservation_rec.attribute1                 := NULL;
3955           l_mtl_reservation_rec.attribute2                 := NULL;
3956           l_mtl_reservation_rec.attribute3                 := NULL;
3957           l_mtl_reservation_rec.attribute4                 := NULL;
3958           l_mtl_reservation_rec.attribute5                 := NULL;
3959           l_mtl_reservation_rec.attribute6                 := NULL;
3960           l_mtl_reservation_rec.attribute7                 := NULL;
3961           l_mtl_reservation_rec.attribute8                 := NULL;
3962           l_mtl_reservation_rec.attribute9                 := NULL;
3963           l_mtl_reservation_rec.attribute10                := NULL;
3964           l_mtl_reservation_rec.attribute11                := NULL;
3965           l_mtl_reservation_rec.attribute12                := NULL;
3966           l_mtl_reservation_rec.attribute13                := NULL;
3967           l_mtl_reservation_rec.attribute14                := NULL;
3968           l_mtl_reservation_rec.attribute15                := NULL;
3969           l_mtl_reservation_rec.ship_ready_flag            := 1;
3970           l_mtl_reservation_rec.detailed_quantity          := 0;
3971           l_mtl_reservation_rec.secondary_detailed_quantity := 0;
3972 
3973           IF l_lot_control_code = 2 THEN
3974             --Lot control
3975             l_transaction_temp_id         := l_mmtt_rec.transaction_temp_id;
3976             l_lot_shipping_quantity       := 0;
3977             l_lot_prim_shipping_quantity  := 0;
3978             OPEN lot_csr(p_transaction_temp_id);
3979 
3980             LOOP
3981               FETCH lot_csr
3982               INTO l_lot_number
3983                  , l_lot_primary_quantity
3984                  , l_lot_transaction_quantity
3985                  , l_lot_secondary_quantity
3986                  , l_lot_secondary_uom
3987                  , l_serial_trx_temp_id
3988                  , l_grade_code;
3989               l_lot_shipping_quantity           := l_lot_shipping_quantity + l_lot_transaction_quantity;
3990               l_sec_lot_shipping_quantity       := l_sec_lot_shipping_quantity + l_lot_secondary_quantity;
3991               l_lot_prim_shipping_quantity      := l_lot_prim_shipping_quantity + l_lot_primary_quantity;
3992 
3993               IF l_lot_shipping_quantity > l_shipping_quantity THEN
3994                 l_lot_transaction_quantity  := l_lot_transaction_quantity - (l_lot_shipping_quantity - l_shipping_quantity);
3995                 l_lot_secondary_quantity    := l_lot_secondary_quantity - (l_sec_lot_shipping_quantity - l_sec_shipping_quantity);
3996                 l_lot_primary_quantity      := l_lot_primary_quantity - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
3997               END IF;
3998 
3999               EXIT WHEN lot_csr%NOTFOUND
4000                      OR l_lot_transaction_quantity <= 0;
4001               l_mtl_reservation_rec.lot_number  := l_lot_number;
4002               -- query to see whether a record with the key
4003               -- attributes already exists
4004               -- if there is, use update instead of create
4005               inv_reservation_pub.query_reservation(
4006                 p_api_version_number         => 1.0
4007               , p_init_msg_lst               => fnd_api.g_false
4008               , x_return_status              => l_return_status
4009               , x_msg_count                  => x_msg_count
4010               , x_msg_data                   => x_msg_data
4011               , p_query_input                => l_mtl_reservation_rec
4012               , p_lock_records               => fnd_api.g_true
4013               , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
4014               , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
4015               , x_error_code                 => l_error_code
4016               );
4017 
4018               IF l_return_status = fnd_api.g_ret_sts_error THEN
4019                 RAISE fnd_api.g_exc_error;
4020               END IF;
4021 
4022               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4023                 RAISE fnd_api.g_exc_unexpected_error;
4024               END IF;
4025 
4026               IF l_mtl_reservation_tbl_count > 0 THEN
4027                 l_mtl_reservation_rec.primary_reservation_quantity  := l_mtl_reservation_tbl(1).primary_reservation_quantity + ABS(l_lot_primary_quantity);
4028                 l_mtl_reservation_rec.reservation_quantity          := l_mtl_reservation_tbl(1).reservation_quantity + ABS(l_lot_transaction_quantity);
4029                 l_mtl_reservation_rec.secondary_reservation_quantity
4030                        := l_mtl_reservation_tbl(1).secondary_reservation_quantity + ABS(l_lot_secondary_quantity);
4031                 l_mtl_reservation_rec.requirement_date              := SYSDATE;
4032 
4033 --INVCONV - Make sure Qty2 are NULL if nor present
4034         IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
4035               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
4036               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
4037         END IF;
4038                 inv_reservation_pub.update_reservation(
4039                   p_api_version_number         => 1.0
4040                 , p_init_msg_lst               => fnd_api.g_false
4041                 , x_return_status              => l_return_status
4042                 , x_msg_count                  => x_msg_count
4043                 , x_msg_data                   => x_msg_data
4044                 , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
4045                 , p_to_rsv_rec                 => l_mtl_reservation_rec
4046                 , p_original_serial_number     => l_original_serial_number
4047                 , p_to_serial_number           => l_to_serial_number
4048                 , p_validation_flag            => fnd_api.g_true
4049                 , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
4050                 );
4051                 IF (l_debug = 1) THEN
4052                    DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
4053                 END IF;
4054 
4055                 IF l_return_status = fnd_api.g_ret_sts_error THEN
4056                   RAISE fnd_api.g_exc_error;
4057                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4058                   RAISE fnd_api.g_exc_unexpected_error;
4059                 END IF;
4060               ELSE
4061                 l_mtl_reservation_rec.primary_reservation_quantity  := ABS(l_lot_primary_quantity);
4062                 l_mtl_reservation_rec.reservation_quantity          := ABS(l_lot_transaction_quantity);
4063                 l_mtl_reservation_rec.secondary_reservation_quantity := ABS(l_lot_secondary_quantity);
4064                 l_mtl_reservation_rec.requirement_date              := SYSDATE;
4065                 inv_reservation_pub.create_reservation(
4066                   p_api_version_number         => 1.0
4067                 , p_init_msg_lst               => fnd_api.g_false
4068                 , x_return_status              => l_return_status
4069                 , x_msg_count                  => x_msg_count
4070                 , x_msg_data                   => x_msg_data
4071                 , p_rsv_rec                    => l_mtl_reservation_rec
4072                 , p_serial_number              => l_to_serial_number
4073                 , x_serial_number              => l_to_serial_number
4074                 , p_partial_reservation_flag   => fnd_api.g_true
4075                 , p_force_reservation_flag     => fnd_api.g_false
4076                 , p_validation_flag            => fnd_api.g_true
4077                 , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
4078                 , x_quantity_reserved          => l_quantity_reserved
4079                 , x_reservation_id             => l_reservation_id
4080                 );
4081                 IF (l_debug = 1) THEN
4082                    DEBUG('Quantity reserved: '|| l_quantity_reserved, 'Finalize_Pick_Confirm');
4083                    DEBUG('Reservation ID: '|| l_reservation_id, 'Finalize_Pick_Confirm');
4084                 END IF;
4085 
4086                 IF l_return_status = fnd_api.g_ret_sts_error THEN
4087                   IF (l_debug = 1) THEN
4088                      DEBUG('Error in creating reservation for lot', 'Finalize_Pick_Confirm');
4089                   END IF;
4090                   RAISE fnd_api.g_exc_error;
4091                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4092                   IF (l_debug = 1) THEN
4093                      DEBUG('Unexpected error in creating reservation for lot', 'Finalize_Pick_Confirm');
4094                   END IF;
4095                   RAISE fnd_api.g_exc_unexpected_error;
4096                 END IF;
4097 
4098                 --bug 1402436 - set the reservations staged flag
4099                 inv_staged_reservation_util.update_staged_flag(x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data, p_reservation_id => l_reservation_id, p_staged_flag => 'Y');
4100 
4101                 IF l_return_status = fnd_api.g_ret_sts_error THEN
4102                   IF (l_debug = 1) THEN
4103                      DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
4104                   END IF;
4105                   RAISE fnd_api.g_exc_error;
4106                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4107                   IF (l_debug = 1) THEN
4108                      DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
4109                   END IF;
4110                   RAISE fnd_api.g_exc_unexpected_error;
4111                 END IF;
4112               END IF; -- Create or Update
4113             END LOOP; -- Lot loop
4114 
4115             IF (l_debug = 1) THEN
4116                DEBUG('after end of lot loop...', 'Finalize_Pick_Confirm');
4117             END IF;
4118             CLOSE lot_csr;
4119           ELSE
4120             --No Lot control
4121             l_mtl_reservation_rec.lot_number  := NULL;
4122             -- query to see whether a record with the key
4123             -- attributes already exists
4124             -- if there is, use update instead of create
4125             inv_reservation_pub.query_reservation(
4126               p_api_version_number         => 1.0
4127             , p_init_msg_lst               => fnd_api.g_false
4128             , x_return_status              => l_return_status
4129             , x_msg_count                  => x_msg_count
4130             , x_msg_data                   => x_msg_data
4131             , p_query_input                => l_mtl_reservation_rec
4132             , p_lock_records               => fnd_api.g_true
4133             , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
4134             , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
4135             , x_error_code                 => l_error_code
4136             );
4137 
4138             IF l_return_status = fnd_api.g_ret_sts_error THEN
4139               RAISE fnd_api.g_exc_error;
4140             END IF;
4141 
4142             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4143               RAISE fnd_api.g_exc_unexpected_error;
4144             END IF;
4145 
4146             IF l_mtl_reservation_tbl_count > 0 THEN
4147               l_mtl_reservation_rec.primary_reservation_quantity  := l_mtl_reservation_tbl(1).primary_reservation_quantity + l_primary_shipping_quantity;
4148               l_mtl_reservation_rec.reservation_quantity          := l_mtl_reservation_tbl(1).reservation_quantity + l_shipping_quantity;
4149               l_mtl_reservation_rec.secondary_reservation_quantity
4150                                 := l_mtl_reservation_tbl(1).secondary_reservation_quantity + l_sec_shipping_quantity;
4151               l_mtl_reservation_rec.requirement_date              := SYSDATE;
4152 
4153 --INVCONV - Make sure Qty2 are NULL if nor present
4154         IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
4155               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
4156               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
4157         END IF;
4158               inv_reservation_pub.update_reservation(
4159                 p_api_version_number         => 1.0
4160               , p_init_msg_lst               => fnd_api.g_false
4161               , x_return_status              => l_return_status
4162               , x_msg_count                  => x_msg_count
4163               , x_msg_data                   => x_msg_data
4164               , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
4165               , p_to_rsv_rec                 => l_mtl_reservation_rec
4166               , p_original_serial_number     => l_original_serial_number
4167               , p_to_serial_number           => l_to_serial_number
4168               , p_validation_flag            => fnd_api.g_true
4169               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
4170               );
4171               IF (l_debug = 1) THEN
4172                  DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
4173               END IF;
4174 
4175               IF l_return_status = fnd_api.g_ret_sts_error THEN
4176                 RAISE fnd_api.g_exc_error;
4177               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4178                 RAISE fnd_api.g_exc_unexpected_error;
4179               END IF;
4180             ELSE
4181               l_mtl_reservation_rec.primary_reservation_quantity  := l_primary_shipping_quantity;
4182               l_mtl_reservation_rec.reservation_quantity          := l_shipping_quantity;
4183               l_mtl_reservation_rec.secondary_reservation_quantity  := l_sec_shipping_quantity;
4184               l_mtl_reservation_rec.requirement_date              := SYSDATE;
4185               inv_reservation_pub.create_reservation(
4186                 p_api_version_number         => 1.0
4187               , p_init_msg_lst               => fnd_api.g_false
4188               , x_return_status              => l_return_status
4189               , x_msg_count                  => x_msg_count
4190               , x_msg_data                   => x_msg_data
4191               , p_rsv_rec                    => l_mtl_reservation_rec
4192               , p_serial_number              => l_to_serial_number
4193               , x_serial_number              => l_to_serial_number
4194               , p_partial_reservation_flag   => fnd_api.g_true
4195               , p_force_reservation_flag     => fnd_api.g_false
4196               , p_validation_flag            => fnd_api.g_true
4197               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
4198               , x_quantity_reserved          => l_quantity_reserved
4199               , x_reservation_id             => l_reservation_id
4200               );
4201               IF (l_debug = 1) THEN
4202                  DEBUG('Quantity reserved: '|| l_quantity_reserved, 'Finalize_Pick_Confirm');
4203                  DEBUG('Reservation ID: '|| l_reservation_id, 'Finalize_Pick_Confirm');
4204               END IF;
4205 
4206               IF l_return_status = fnd_api.g_ret_sts_error THEN
4207                 IF (l_debug = 1) THEN
4208                    DEBUG('Error in creating reservation for lot', 'Finalize_Pick_Confirm');
4209                 END IF;
4210                 RAISE fnd_api.g_exc_error;
4211               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4212                 IF (l_debug = 1) THEN
4213                    DEBUG('Unexpected error in creating reservation for lot', 'Finalize_Pick_Confirm');
4214                 END IF;
4215                 RAISE fnd_api.g_exc_unexpected_error;
4216               END IF;
4217 
4218               --bug 1402436 - set the reservations staged flag
4219               inv_staged_reservation_util.update_staged_flag
4220                     (x_return_status        => l_return_status
4221                     , x_msg_count           => x_msg_count
4222                     , x_msg_data            => x_msg_data
4223                     , p_reservation_id      => l_reservation_id
4224                     , p_staged_flag         => 'Y'
4225                     );
4226 
4227               IF l_return_status = fnd_api.g_ret_sts_error THEN
4228                 IF (l_debug = 1) THEN
4229                    DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
4230                 END IF;
4231                 RAISE fnd_api.g_exc_error;
4232               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4233                 IF (l_debug = 1) THEN
4234                    DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
4235                 END IF;
4236                 RAISE fnd_api.g_exc_unexpected_error;
4237               END IF;
4238             END IF; -- Create or Update
4239           END IF; -- Lot control or not
4240         END IF; -- Staging sub reservable or not
4241       END IF; -- if reservation exists
4242 
4243       -- Call update shipping for the record transacted
4244       -- assign the changed attribute to shipping attribute.
4245       IF (l_debug = 1) THEN
4246          DEBUG('before select delivery_detail_id', 'Finalize_Pick_Confirm');
4247       END IF;
4248 
4249       BEGIN
4250         /* BUG 5570553 added the index hint with the suggestion of apps performance team */
4251         SELECT /*+index (WDD WSH_DELIVERY_DETAILS_N7)*/
4252                   delivery_detail_id, source_header_id, source_line_id
4253               INTO l_delivery_detail_id, l_source_header_id, l_source_line_id
4254               FROM wsh_delivery_details WDD
4255              WHERE WDD.move_order_line_id = l_mmtt_rec.move_order_line_id
4256                AND WDD.move_order_line_id IS NOT NULL
4257                AND WDD.released_status = 'S'
4258         FOR UPDATE NOWAIT;
4259       EXCEPTION
4260         WHEN NO_DATA_FOUND THEN
4261           fnd_message.set_name('INV', 'INV_DELIV_INFO_MISSING');
4262           fnd_msg_pub.ADD;
4263           RAISE fnd_api.g_exc_error;
4264         WHEN OTHERS THEN
4265           --could not lock row
4266           IF SQLCODE = -54 THEN
4267             fnd_message.set_name('INV', 'INV_DELIV_INFO_LOCKED');
4268             fnd_message.set_token('LINEID', l_trolin_rec.line_id);
4269             fnd_msg_pub.ADD;
4270             RAISE fnd_api.g_exc_error;
4271           ELSE
4272             RAISE fnd_api.g_exc_unexpected_error;
4273           END IF;
4274       END;
4275 
4276       if (l_return_status = FND_API.G_RET_STS_ERROR) then
4277         RAISE FND_API.G_EXC_ERROR;
4278       elsif (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) then
4279         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4280       end if;
4281 
4282       -- When calling shipping, pending quantity should be the total
4283       -- quantity remaining that Shipping has not been updated with
4284       IF (l_debug = 1) THEN
4285          DEBUG('after select delivery_detail_id', 'Finalize_Pick_Confirm');
4286          DEBUG('delivery_detail_id = '|| l_delivery_detail_id, 'Finalize_Pick_Confirm');
4287          DEBUG('p_xfr_transaction_id '||p_xfr_transaction_id,'Finalize_Pick_Confirm');
4288       END IF;
4289 --bug 2678601 pass Transaction_id to shipping
4290 
4291 
4292       IF (l_debug = 1) THEN
4293         DEBUG('delivery_detail_id = '|| l_delivery_detail_id, 'Finalize_Pick_Confirm');
4294       END IF;
4295       l_InvPCInRecType.transaction_id :=p_xfr_transaction_id;
4296       l_InvPCInRecType.source_code :='INV';
4297       l_InvPCInRecType.api_version_number :=1.0;
4298       WSH_INTEGRATION.Set_Inv_PC_Attributes
4299 	( p_in_attributes         =>   l_InvPCInRecType,
4300 	  x_return_status         =>  l_return_status,
4301 	  x_msg_count             =>   l_msg_count,
4302 	  x_msg_data             =>    l_msg_data );
4303         IF  (l_debug = 1) THEN
4304          DEBUG('after Set_Inv_PC_Attributes Ret status'||l_return_status, 'Finalize_Pick_Confirm');        END IF;
4305 
4306               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4307                 IF (l_debug = 1) THEN
4308                   DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize_Pick_Confirm');
4309                 END IF;
4310                 RAISE fnd_api.g_exc_error;
4311               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4312                 IF (l_debug = 1) THEN
4313                   DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize_Pick_Confirm');
4314                 END IF;
4315                 RAISE fnd_api.g_exc_unexpected_error;
4316               END IF;
4317 
4318       l_shipping_attr(1).source_header_id    := l_source_header_id;
4319       l_shipping_attr(1).source_line_id      := l_source_line_id;
4320       l_shipping_attr(1).ship_from_org_id    := l_mmtt_rec.organization_id;
4321       l_shipping_attr(1).subinventory        := l_mmtt_rec.transfer_subinventory;
4322       l_shipping_attr(1).revision            := l_mmtt_rec.revision;
4323       l_shipping_attr(1).locator_id          := l_mmtt_rec.transfer_to_location;
4324       l_shipping_attr(1).released_status     := 'Y';
4325       l_shipping_attr(1).delivery_detail_id  := l_delivery_detail_id;
4326 
4327       IF (l_mmtt_rec.content_lpn_id IS NOT NULL) THEN
4328         l_shipping_attr(1).transfer_lpn_id  := l_mmtt_rec.content_lpn_id;
4329       ELSE
4330         l_shipping_attr(1).transfer_lpn_id  := l_mmtt_rec.transfer_lpn_id;
4331       END IF;
4332 
4333         -- jaysingh
4334 	-- we need to re-name already used container
4335 	-- so that we can use it again
4336 	/* part of bug fix 2640966 */
4337 
4338 
4339    ---- ** Commented below code againt ER : 6845650
4340 /*
4341 	    BEGIN
4342 
4343 		 -- first get the container name
4344 		 SELECT license_plate_number
4345 		 INTO l_container_name
4346 		 FROM wms_license_plate_numbers
4347 		 WHERE organization_id= l_mmtt_rec.organization_id
4348 		 AND lpn_id=l_shipping_attr(1).transfer_lpn_id;
4349 
4350 		  SELECT wdd.released_status,wdd.delivery_detail_id
4351 		  INTO l_status_code,l_container_delivery_det_id
4352 		  FROM   wsh_delivery_details wdd
4353 		  WHERE wdd.container_name =l_container_name
4354           AND wdd.released_status = 'X';  -- ER : 6845650
4355 
4356 		  if l_status_code ='C' then
4357                   /* Release 12: LPN Synchronization
4358                      Uniqueness constraint on WDD.container_name is removed
4359                      So it is not required to append characters to the LPNs
4360                      to get a new containers name
4361                      Removed the following call to get_container_name */
4362                   /* l_new_container_name:=wms_shipping_transaction_pub.get_container_name(l_container_name);
4363                      l_container_rec(1).container_name:=l_new_container_name; */
4364 /*                     l_container_rec(1).container_name:=l_container_name;
4365                      l_container_rec(1).delivery_detail_id:=l_container_delivery_det_id;
4366                      l_container_rec(1).lpn_id:=NULL;
4367                      l_container_rec(1).container_flag:='Y';
4368                      --Bug:2701925:REplaced the direct update statement with the call to the API
4369 
4370 		     IF (l_debug = 1) THEN
4371    		     debug('BF CONTAINER EXISTS, Renamed to : ' || l_new_container_name,'Finalize_Pick_Confirm');
4372 		     END IF;
4373                      WSH_CONTAINER_GRP.Update_Container(
4374                           p_api_version => 1.0,
4375                           p_init_msg_list => FND_API.G_FALSE,
4376              	          p_commit =>FND_API.G_FALSE,
4377 	                  p_validation_level => FND_API.G_VALID_LEVEL_FULL,
4378                           x_return_status =>  x_return_status,
4379                           x_msg_count => x_msg_count,
4380                           x_msg_data => x_msg_data,
4381                           p_container_rec => l_container_rec
4382                          );
4383                     IF( x_return_status in (FND_API.G_RET_STS_ERROR) ) THEN
4384                       IF (l_debug = 1) THEN
4385                          debug('WSH_Container_Grp.Update_Containers returns error','Finalize Pick Confirm');
4386                       END IF;
4387                       RAISE FND_API.G_EXC_ERROR;
4388                     ELSIF ( x_return_status = FND_API.G_RET_STS_UNEXP_ERROR)  THEN
4389                      IF (l_debug = 1) THEN
4390                         debug('WSH_Container_Grp.Update_Containers returns success','Finalize Pick Confirm');
4391                      END IF;
4392                      RAISE  fnd_api.g_exc_unexpected_error;
4393                     ELSE
4394                       IF (l_debug = 1) THEN
4395                          debug(' AF CONTAINER EXISTS, Renamed to : ' || l_new_container_name,'Finalize Pick Confirm');
4396                       END IF;
4397                     END IF;
4398 
4399 
4400 		    /*UPDATE WSH_DELIVERY_DETAILS
4401 		    SET CONTAINER_NAME=l_new_container_name,
4402 				       lpn_id=NULL,
4403 				       last_update_date=sysdate,
4404 				       last_updated_by=fnd_global.user_id,
4405 				       last_update_login=fnd_global.login_id
4406 		    WHERE container_name =  l_container_name ;
4407 		      */
4408            /*
4409            IF (l_debug = 1) THEN
4410               debug('CONTAINER EXISTS, Renamed to : ' || l_new_container_name,'Finalize_Pick_Confirm');
4411            END IF;
4412             */
4413 /*		  else
4414 		    IF (l_debug = 1) THEN
4415    		    debug('LPN with status '|| l_status_code || 'found in wdd. Check for data corruption', 'Finalize_Pick_Confirm');
4416 		    END IF;
4417 		  end if;
4418 	    EXCEPTION
4419 	       WHEN NO_DATA_FOUND THEN
4420 		null;
4421 	    END;
4422 */
4423 	/* end of bug fix 2640966 */
4424 
4425       l_shipping_attr(1).action_flag         := l_action_flag;
4426       IF (l_debug = 1) THEN
4427          DEBUG('l_source_header_id'|| l_source_header_id, 'Finalize_Pick_Confirm');
4428          DEBUG('l_source_line_id '|| l_source_line_id, 'Finalize_Pick_Confirm');
4429          DEBUG('l_organization_id '|| l_shipping_attr(1).ship_from_org_id, 'Finalize_Pick_Confirm');
4430          DEBUG('subinventory '|| l_shipping_attr(1).subinventory, 'Finalize_Pick_Confirm');
4431          DEBUG('revision '|| l_shipping_attr(1).revision, 'Finalize_Pick_Confirm');
4432          DEBUG('lot_number '|| l_shipping_attr(1).lot_number, 'Finalize_Pick_Confirm');
4433          DEBUG('locator_id '|| l_shipping_attr(1).locator_id, 'Finalize_Pick_Confirm');
4434          DEBUG('release status = '|| l_shipping_attr(1).released_status, 'Finalize_Pick_Confirm');
4435          DEBUG('delivery_detail_id '|| l_shipping_attr(1).delivery_detail_id, 'Finalize_Pick_Confirm');
4436          DEBUG('action flag is '|| l_shipping_attr(1).action_flag, 'Finalize_Pick_Confirm');
4437          DEBUG('about to call update shipping attributes', 'Finalize_Pick_Confirm');
4438       END IF;
4439 
4440       --initalizing l_InvPCInRecType to use for updating wdd with transaction_temp_id
4441       --l_InvPCInRecType.transaction_id needs to be preserved for each call to Set_Inv_PC_Attributes
4442       l_InvPCInRecType.source_code :='INV';
4443       l_InvPCInRecType.api_version_number :=1.0;
4444 
4445       IF ( (l_mmtt_rec.transaction_source_type_id = INV_GLOBALS.G_SourceType_SalesOrder) AND
4446            (l_mmtt_rec.transaction_action_id = INV_GLOBALS.G_Action_Stgxfr) ) THEN
4447         -- Call to new api to check if item is catch weight enabled.
4448         l_catch_weight_enabled := WMS_CATCH_WEIGHT_PVT.Get_Ont_Pricing_Qty_Source (
4449                                     p_api_version       => 1.0
4450                                   , x_return_status     => x_return_status
4451                                   , x_msg_count         => x_msg_count
4452                                   , x_msg_data          => x_msg_data
4453                                   , p_organization_id   => l_mmtt_rec.organization_id
4454                                   , p_inventory_item_id => l_mmtt_rec.inventory_item_id );
4455         IF ( x_return_status <> fnd_api.g_ret_sts_success ) THEN
4456           fnd_message.set_name('INV', 'WMS_GET_CATCH_WEIGHT_ATT_FAIL');
4457           fnd_msg_pub.ADD;
4458           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4459         END IF;
4460 
4461         IF  (l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY AND (NVL(l_lpn_id,-1)<=0)) THEN
4462           fnd_message.set_name('INV', 'WMS_CATCH_WEIGHT_NO_LPN_ERR');
4463           fnd_msg_pub.ADD;
4464           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4465         END IF;
4466 
4467       END IF;
4468 
4469       IF ( l_wms_org_flag ) THEN
4470         -- The check for this table is included to determine if the env is at wms I level
4471         IF ( G_WMS_I_OR_ABOVE IS NULL ) THEN
4472           BEGIN
4473             SELECT 1 INTO l_dummy_num
4474             FROM fnd_tables
4475             WHERE table_name = 'WMS_OP_PLANS_B'
4476             AND rownum < 2;
4477             G_WMS_I_OR_ABOVE := TRUE;
4478           EXCEPTION
4479             WHEN NO_DATA_FOUND THEN
4480               G_WMS_I_OR_ABOVE := FALSE;
4481           END;
4482         END IF;
4483       ELSE -- Inventory only organization
4484         G_WMS_I_OR_ABOVE := FALSE;
4485 
4486         -- Check if OM at H level is installed flag is set meaning
4487         -- that shipping serial range enhancement should be used
4488         -- BUG 5074402 - cached this value for performance
4489         if g_omh_installed IS  NULL THEN
4490            g_omh_installed := NVL(FND_PROFILE.VALUE('INV_OMFPC2_INSTALLED'), 2);
4491         end if;
4492         l_omh_installed := g_omh_installed;
4493       END IF;
4494 
4495       IF (l_debug = 1) THEN
4496          DEBUG('om h installed: ' || l_omh_installed, 'Finalize_Pick_Confirm');
4497       END IF;
4498 
4499       IF ( l_lot_control_code > 1 AND l_serial_control_code NOT IN (1, 6) ) THEN
4500         -- Lot and serial controlled
4501         l_lot_shipping_quantity       := 0;
4502         l_lot_prim_shipping_quantity  := 0;
4503         l_ser_prim_shipping_quantity  := 0;
4504         l_serial_quantity             := 1;
4505         OPEN lot_csr(p_transaction_temp_id);
4506 
4507         LOOP
4508           FETCH lot_csr
4509           INTO l_lot_number
4510               , l_lot_primary_quantity
4511               , l_lot_transaction_quantity
4512               , l_lot_secondary_quantity
4513               , l_lot_secondary_uom
4514               , l_serial_trx_temp_id
4515               , l_grade_code;
4516           EXIT WHEN lot_csr%NOTFOUND;
4517           l_update_shipping  := TRUE;
4518 
4519           -- If this item is catch weight enabled, check to see if any other MTLT lines for
4520           -- this LPN.  If any do not have secondary quantity defined, do not populate
4521           -- picked_quantity2
4522           IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY AND
4523                l_lot_number <> l_previous_lot_number ) THEN
4524             l_previous_lot_number := l_lot_number;
4525 
4526             BEGIN
4527               SELECT WMS_CATCH_WEIGHT_PVT.G_PRICE_PRIMARY INTO l_catch_weight_enabled FROM DUAL
4528               WHERE EXISTS (
4529                 SELECT 1
4530                 FROM mtl_material_transactions_temp mmtt,
4531                      mtl_transaction_lots_temp mtlt
4532                 WHERE mmtt.organization_id = l_mmtt_rec.organization_id
4533                 AND   mmtt.inventory_item_id = l_mmtt_rec.inventory_item_id
4534                 AND   NVL(mmtt.revision, '@') = NVL(l_mmtt_rec.revision, '@')
4535                 AND   mmtt.transaction_source_type_id = INV_GLOBALS.G_SourceType_SalesOrder
4536                 AND   mmtt.transaction_action_id = INV_GLOBALS.G_Action_Stgxfr
4537                 AND   NVL(mmtt.content_lpn_id, mmtt.transfer_lpn_id) = l_lpn_id
4538                 AND   mtlt.transaction_temp_id = mmtt.transaction_temp_id
4539                 AND   mtlt.lot_number = l_lot_number
4540                 AND   (mtlt.secondary_quantity IS NULL OR mtlt.secondary_unit_of_measure IS NULL) );
4541             EXCEPTION
4542               WHEN OTHERS THEN
4543                 l_catch_weight_enabled := WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY;
4544             END;
4545             IF (l_debug = 1) THEN
4546               DEBUG('itemid='||l_mmtt_rec.inventory_item_id||' rev='||l_mmtt_rec.revision||' lot='||l_lot_number||' lpnid='||l_lpn_id||' cwe='||l_catch_weight_enabled, 'Finalize_Pick_Confirm');
4547             END IF;
4548           END IF;
4549 
4550           -- Only enable shipping serial range enhancement for WMS.I or non wms orgs with profile set
4551           IF ( ( G_WMS_I_OR_ABOVE OR ( NOT l_wms_org_flag AND l_omh_installed = 1 )  ) AND
4552                l_lot_primary_quantity <> 1 ) THEN
4553             --Serial numbers not stored in WDD, handle as a non sn controlled item
4554             IF l_lot_shipping_quantity > l_shipping_quantity THEN
4555               l_lot_transaction_quantity  := l_lot_transaction_quantity - (l_lot_shipping_quantity - l_shipping_quantity);
4556               l_lot_secondary_quantity  := l_lot_secondary_quantity - (l_sec_lot_shipping_quantity - l_sec_shipping_quantity);
4557               l_lot_primary_quantity      := l_lot_primary_quantity - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
4558             END IF;
4559 
4560             EXIT WHEN l_lot_transaction_quantity <= 0;
4561             l_shipping_attr(1).serial_number     := NULL;
4562             l_shipping_attr(1).lot_number        := l_lot_number;
4563             l_shipping_attr(1).preferred_grade   := l_grade_code;
4564             l_shipping_attr(1).picked_quantity   := ABS(l_lot_primary_quantity);
4565             l_shipping_attr(1).picked_quantity2  := ABS(l_lot_secondary_quantity);
4566             --update pending quantity to reflect new pending quantity
4567             l_primary_pending_quantity           := l_primary_pending_quantity - ABS(l_lot_primary_quantity);
4568             l_sec_pending_quantity               := l_sec_pending_quantity - ABS(l_lot_secondary_quantity);
4569             l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
4570             l_shipping_attr(1).pending_quantity2 := l_sec_pending_quantity;
4571             l_return_status                      := '';
4572             IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4573               l_shipping_attr(1).picked_quantity2 := ABS(l_lot_secondary_quantity);
4574               l_shipping_attr(1).ordered_quantity_uom2 := l_lot_secondary_uom;
4575             END IF;
4576 
4577             --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
4578             --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
4579 
4580             --Bug #3306493
4581             --Pass the serial_transaction_temp_id from MTLT stored in l_serial_trx_temp_id
4582             --when setting the transaction_temp_id attribute in INVPCInRecType
4583             --l_InvPCInRecType.transaction_temp_id := l_serial_transaction_temp_id;
4584             l_InvPCInRecType.transaction_temp_id := l_serial_trx_temp_id;
4585 
4586             IF (l_debug = 1) THEN
4587               DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' sertrxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
4588             END IF;
4589 
4590             WSH_INTEGRATION.Set_Inv_PC_Attributes
4591             ( p_in_attributes         =>   l_InvPCInRecType,
4592               x_return_status         =>   l_return_status,
4593               x_msg_count             =>   l_msg_count,
4594               x_msg_data              =>   l_msg_data );
4595             IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4596               IF (l_debug = 1) THEN
4597                 DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4598               END IF;
4599               RAISE fnd_api.g_exc_error;
4600             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4601               IF (l_debug = 1) THEN
4602                 DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4603               END IF;
4604               RAISE fnd_api.g_exc_unexpected_error;
4605             END IF;
4606 
4607             IF (l_debug = 1) THEN
4608                DEBUG('Calling Update Shipping Attributes for a serial range of lot items', 'Finalize Pick Confirm');
4609                DEBUG('Lot number  : '|| l_shipping_attr(1).lot_number, 'Finalize_Pick_Confirm');
4610                DEBUG('Picked qty  : '|| ABS(l_lot_primary_quantity), 'Finalize_Pick_Confirm');
4611                DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
4612                DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
4613                DEBUG('Pending qty : '|| l_shipping_attr(1).pending_quantity, 'Finalize_Pick_Confirm');
4614             END IF;
4615 
4616             --Check to see if it is the case of overpicking, if yes, then adjust the serials in MSNT
4617             IF  (l_trolin_rec.quantity_delivered > NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity)) THEN
4618               --Bug #3306493
4619               --Adjust_serial_numbers_in_MSNT(l_serial_transaction_temp_id,l_lot_primary_quantity);
4620               Adjust_serial_numbers_in_MSNT(l_serial_trx_temp_id,l_lot_primary_quantity);
4621             END IF;
4622 
4623             wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
4624             IF (l_debug = 1) THEN
4625                DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
4626             END IF;
4627 
4628             IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4629               IF (l_debug = 1) THEN
4630                  DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4631               END IF;
4632               RAISE fnd_api.g_exc_error;
4633             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4634               IF (l_debug = 1) THEN
4635                  DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4636               END IF;
4637               RAISE fnd_api.g_exc_unexpected_error;
4638             END IF;
4639           ELSE -- Either only a single serial item or not WMS.I process the old way
4640             IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4641               -- Calculate the secondary quantity for a single serial number
4642               l_serial_secondary_quantity := l_lot_secondary_quantity*(1/l_lot_primary_quantity);
4643             END IF;
4644 
4645             OPEN serial_csr(p_transaction_temp_id, l_lot_control_code, l_serial_trx_temp_id,
4646                             l_mmtt_rec.organization_id, l_mmtt_rec.inventory_item_id);
4647             LOOP
4648               FETCH serial_csr INTO l_serial_number;
4649               EXIT WHEN serial_csr%NOTFOUND;
4650               IF (l_debug = 1) THEN
4651                  DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
4652               END IF;
4653               --bug 2123867
4654               -- ser_prim_shipping_quantity was being incremented
4655               -- even when serial_csr returned no data found.  Only half
4656               -- of the serial numbers were being passed to shipping.
4657               -- Moved Exit condition of notfound before increment.
4658               l_ser_prim_shipping_quantity  := l_ser_prim_shipping_quantity + 1;
4659 
4660               IF l_ser_prim_shipping_quantity > l_primary_shipping_quantity THEN
4661                 IF (l_debug = 1) THEN
4662                    DEBUG('Serial quantity > shipping quantity', 'Finalize_Pick_Confirm');
4663                    DEBUG('Serial Qty: '|| l_ser_prim_shipping_quantity, 'Finalize_Pick_Confirm');
4664                 END IF;
4665                 l_serial_quantity  := 0;
4666                 l_update_shipping  := FALSE;
4667               END IF;
4668 
4669               IF l_update_shipping = TRUE THEN
4670                 l_shipping_attr(1).serial_number     := l_serial_number;
4671                 l_shipping_attr(1).lot_number        := l_lot_number;
4672                 l_shipping_attr(1).preferred_grade   := l_grade_code;
4673                 l_shipping_attr(1).picked_quantity   := 1;
4674                 l_shipping_attr(1).picked_quantity2  := null;
4675                 --update pending quantity to reflect new pending quantity
4676                 l_primary_pending_quantity           := l_primary_pending_quantity - 1;
4677                 l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
4678                 l_shipping_attr(1).pending_quantity2 := null;
4679                 l_return_status                      := '';
4680 
4681                 IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4682                   l_shipping_attr(1).picked_quantity2 := l_serial_secondary_quantity;
4683                   l_shipping_attr(1).ordered_quantity_uom2 := l_lot_secondary_uom;
4684                 END IF;
4685 
4686                 --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
4687                 --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
4688                 l_InvPCInRecType.transaction_temp_id := NULL;
4689 
4690                 IF (l_debug = 1) THEN
4691                   DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' trxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
4692                 END IF;
4693 
4694                 WSH_INTEGRATION.Set_Inv_PC_Attributes
4695                 ( p_in_attributes         =>   l_InvPCInRecType,
4696                   x_return_status         =>   l_return_status,
4697                   x_msg_count             =>   l_msg_count,
4698                   x_msg_data              =>   l_msg_data );
4699                 IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4700                   IF (l_debug = 1) THEN
4701                     DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4702                   END IF;
4703                   RAISE fnd_api.g_exc_error;
4704                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4705                   IF (l_debug = 1) THEN
4706                     DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4707                   END IF;
4708                   RAISE fnd_api.g_exc_unexpected_error;
4709                 END IF;
4710 
4711                 IF (l_debug = 1) THEN
4712                    DEBUG('Calling Update Shipping Attributes', 'Finalize Pick Confirm');
4713                    DEBUG('Lot number  : '|| l_shipping_attr(1).lot_number, 'Finalize_Pick_Confirm');
4714                    DEBUG('Picked qty  : '|| l_shipping_attr(1).picked_quantity, 'Finalize_Pick_Confirm');
4715                    DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
4716                    DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
4717                    DEBUG('Pending qty : '|| l_shipping_attr(1).pending_quantity, 'Finalize_Pick_Confirm');
4718                 END IF;
4719                 wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
4720                 IF (l_debug = 1) THEN
4721                    DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
4722                 END IF;
4723 
4724                 IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4725                   IF (l_debug = 1) THEN
4726                      DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4727                   END IF;
4728                   RAISE fnd_api.g_exc_error;
4729                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4730                   IF (l_debug = 1) THEN
4731                      DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4732                   END IF;
4733                   RAISE fnd_api.g_exc_unexpected_error;
4734                 END IF;
4735               ELSE
4736                 -- Unmark the remaining serials
4737                /*** {{ R12 Enhanced reservations code changes,
4738                 *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
4739                 UPDATE mtl_serial_numbers
4740                    SET group_mark_id = NULL
4741                  WHERE serial_number = l_serial_number
4742                    AND inventory_item_id = l_mmtt_rec.inventory_item_id;
4743                 *** End R12 }} ***/
4744 
4745                /*** {{ R12 Enhanced reservations code changes ***/
4746                 serial_check.inv_unmark_rsv_serial
4747                    (from_serial_number   => l_serial_number
4748                    ,to_serial_number     => null
4749                    ,serial_code          => null
4750                    ,hdr_id               => null
4751                    ,p_inventory_item_id  => l_mmtt_rec.inventory_item_id
4752                    ,p_update_reservation => fnd_api.g_true);
4753                /*** End R12 }} ***/
4754               END IF;
4755             END LOOP;
4756             CLOSE serial_csr;
4757 
4758             IF ( l_lot_primary_quantity <> 1 ) THEN
4759               -- MSNT records need to be deleted since TM does not
4760               DELETE FROM mtl_serial_numbers_temp
4761               WHERE transaction_temp_id = l_serial_trx_temp_id;
4762             END IF;
4763           END IF;
4764         END LOOP;
4765 
4766         CLOSE lot_csr;
4767       ELSIF ( l_lot_control_code = 1 AND l_serial_control_code NOT IN (1, 6) ) THEN
4768         -- If this item is catch weight enabled, check to see if any other MMTT lines for
4769         -- this LPN.  If any do not have secondary quantity defined, do not populate
4770         -- picked_quantity2
4771         IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4772           BEGIN
4773             SELECT WMS_CATCH_WEIGHT_PVT.G_PRICE_PRIMARY INTO l_catch_weight_enabled FROM DUAL
4774             WHERE EXISTS (
4775               SELECT 1
4776               FROM mtl_material_transactions_temp mmtt
4777               WHERE mmtt.organization_id = l_mmtt_rec.organization_id
4778               AND   mmtt.inventory_item_id = l_mmtt_rec.inventory_item_id
4779               AND   NVL(mmtt.revision, '@') = NVL(l_mmtt_rec.revision, '@')
4780               AND   mmtt.transaction_source_type_id = INV_GLOBALS.G_SourceType_SalesOrder
4781               AND   mmtt.transaction_action_id = INV_GLOBALS.G_Action_Stgxfr
4782               AND   NVL(mmtt.content_lpn_id, mmtt.transfer_lpn_id) = l_lpn_id
4783               AND   (mmtt.secondary_transaction_quantity IS NULL OR mmtt.secondary_uom_code IS NULL) );
4784           EXCEPTION
4785             WHEN OTHERS THEN
4786               l_catch_weight_enabled := WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY;
4787           END;
4788           IF (l_debug = 1) THEN
4789             DEBUG('itemid='||l_mmtt_rec.inventory_item_id||' rev='||l_mmtt_rec.revision||' lot='||l_lot_number||' lpnid='||l_lpn_id||' cwe='||l_catch_weight_enabled, l_api_name);
4790           END IF;
4791         END IF;
4792 
4793         -- Only enable shipping serial range enhancement for WMS.I or non wms orgs with profile set
4794           IF ( ( G_WMS_I_OR_ABOVE OR ( NOT l_wms_org_flag AND l_omh_installed = 1 )  ) AND
4795                l_primary_shipping_quantity <> 1 )THEN
4796           --Serial numbers not stored in WDD, handle as a non sn controlled item
4797           l_shipping_attr(1).serial_number     := NULL;
4798           l_shipping_attr(1).picked_quantity   := l_primary_shipping_quantity;
4799           l_shipping_attr(1).picked_quantity2  := l_sec_shipping_quantity;
4800           --update pending quantity to reflect new pending quantity
4801           l_primary_pending_quantity           := l_primary_pending_quantity - l_primary_shipping_quantity;
4802           l_sec_pending_quantity               := l_sec_pending_quantity - l_sec_shipping_quantity;
4803           l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
4804           l_shipping_attr(1).pending_quantity2 := l_sec_pending_quantity;
4805           l_return_status                      := '';
4806           IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4807             l_shipping_attr(1).picked_quantity2 := l_mmtt_rec.secondary_transaction_quantity;
4808             l_shipping_attr(1).ordered_quantity_uom2 := l_mmtt_rec.secondary_uom_code;
4809           END IF;
4810 
4811           --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
4812           --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
4813           l_InvPCInRecType.transaction_temp_id := p_transaction_temp_id;
4814 
4815           IF (l_debug = 1) THEN
4816             DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' trxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
4817           END IF;
4818 
4819           WSH_INTEGRATION.Set_Inv_PC_Attributes
4820           ( p_in_attributes         =>   l_InvPCInRecType,
4821             x_return_status         =>   l_return_status,
4822             x_msg_count             =>   l_msg_count,
4823             x_msg_data              =>   l_msg_data );
4824           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4825             IF (l_debug = 1) THEN
4826               DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4827             END IF;
4828             RAISE fnd_api.g_exc_error;
4829           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4830             IF (l_debug = 1) THEN
4831               DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4832             END IF;
4833             RAISE fnd_api.g_exc_unexpected_error;
4834           END IF;
4835 
4836           IF (l_debug = 1) THEN
4837              DEBUG('Calling Update Shipping Attributes for a serial range', 'Finalize Pick Confirm');
4838              DEBUG('Picked qty  : '|| l_shipping_attr(1).picked_quantity, 'Finalize_Pick_Confirm');
4839              DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
4840              DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
4841              DEBUG('Pending qty : '|| l_shipping_attr(1).pending_quantity, 'Finalize_Pick_Confirm');
4842           END IF;
4843 
4844           --Check to see if it is the case of overpicking, if yes, then adjust the serials in MSNT
4845           IF  (l_trolin_rec.quantity_delivered > NVL(l_trolin_rec.required_quantity, l_trolin_rec.quantity)) THEN
4846                  Adjust_serial_numbers_in_MSNT(p_transaction_temp_id,l_shipping_attr(1).picked_quantity);
4847           END IF;
4848 
4849           wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
4850           IF (l_debug = 1) THEN
4851              DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
4852           END IF;
4853 
4854           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4855             IF (l_debug = 1) THEN
4856                DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4857             END IF;
4858             RAISE fnd_api.g_exc_error;
4859           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4860             IF (l_debug = 1) THEN
4861                DEBUG('return unexpected error from update shipping attributes', 'Finalize_Pick_Confirm');
4862             END IF;
4863             RAISE fnd_api.g_exc_unexpected_error;
4864           END IF;
4865         ELSE -- Either only a single serial item or not WMS.I process the old way
4866           l_serial_trx_temp_id          := 0;
4867           l_ser_prim_shipping_quantity  := 0;
4868           l_serial_quantity             := 1;
4869           l_update_shipping             := TRUE;
4870           IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4871             -- Calculate the secondary quantity for a single serial number
4872             l_serial_secondary_quantity := l_mmtt_rec.secondary_transaction_quantity*(1/l_primary_shipping_quantity);
4873           END IF;
4874 
4875           OPEN serial_csr(p_transaction_temp_id, l_lot_control_code, l_serial_trx_temp_id,
4876                           l_mmtt_rec.organization_id, l_mmtt_rec.inventory_item_id);
4877           LOOP
4878             FETCH serial_csr INTO l_serial_number;
4879             EXIT WHEN serial_csr%NOTFOUND;
4880             IF (l_debug = 1) THEN
4881                DEBUG('serial number:'|| l_serial_number, 'Finalize_Pick_Confirm');
4882             END IF;
4883             l_ser_prim_shipping_quantity  := l_ser_prim_shipping_quantity + 1;
4884 
4885             IF l_ser_prim_shipping_quantity > l_primary_shipping_quantity THEN
4886               IF (l_debug = 1) THEN
4887                  DEBUG('Serial quantity > shipping quantity', 'Finalize_Pick_Confirm');
4888                  DEBUG('Serial Qty: '|| l_ser_prim_shipping_quantity, 'Finalize_Pick_Confirm');
4889               END IF;
4890               l_serial_quantity  := 0;
4891               l_update_shipping  := FALSE;
4892             END IF;
4893 
4894             IF l_update_shipping = TRUE THEN
4895               l_shipping_attr(1).serial_number     := l_serial_number;
4896               l_shipping_attr(1).picked_quantity   := 1;
4897               l_shipping_attr(1).picked_quantity2  := null;
4898               --update pending quantity to reflect new pending quantity
4899               l_primary_pending_quantity           := l_primary_pending_quantity - 1;
4900               l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
4901               l_shipping_attr(1).pending_quantity2 := null;
4902               l_return_status                      := '';
4903               IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
4904                 l_shipping_attr(1).picked_quantity2 := l_serial_secondary_quantity;
4905             	l_shipping_attr(1).ordered_quantity_uom2 := l_mmtt_rec.secondary_uom_code;
4906               END IF;
4907 
4908               --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
4909               --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
4910               l_InvPCInRecType.transaction_temp_id := NULL;
4911 
4912               IF (l_debug = 1) THEN
4913                 DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' trxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
4914               END IF;
4915 
4916               WSH_INTEGRATION.Set_Inv_PC_Attributes
4917               ( p_in_attributes         =>   l_InvPCInRecType,
4918                 x_return_status         =>   l_return_status,
4919                 x_msg_count             =>   l_msg_count,
4920                 x_msg_data              =>   l_msg_data );
4921               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4922                 IF (l_debug = 1) THEN
4923                   DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4924                 END IF;
4925                 RAISE fnd_api.g_exc_error;
4926               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4927                 IF (l_debug = 1) THEN
4928                   DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
4929                 END IF;
4930                 RAISE fnd_api.g_exc_unexpected_error;
4931               END IF;
4932 
4933               IF (l_debug = 1) THEN
4934                  DEBUG('Calling Update Shipping Attributes', 'Finalize Pick Confirm');
4935                  DEBUG('Picked qty  : '|| l_shipping_attr(1).picked_quantity, 'Finalize_Pick_Confirm');
4936                  DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
4937                  DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
4938                  DEBUG('Pending qty : '|| l_shipping_attr(1).pending_quantity, 'Finalize_Pick_Confirm');
4939               END IF;
4940               wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
4941               IF (l_debug = 1) THEN
4942                  DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
4943               END IF;
4944 
4945               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
4946                 IF (l_debug = 1) THEN
4947                    DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4948                 END IF;
4949                 RAISE fnd_api.g_exc_error;
4950               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4951                 IF (l_debug = 1) THEN
4952                    DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
4953                 END IF;
4954                 RAISE fnd_api.g_exc_unexpected_error;
4955               END IF;
4956             ELSE
4957               -- Unmark the remaining serials
4958              /*** {{ R12 Enhanced reservations code changes,
4959               *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
4960               UPDATE mtl_serial_numbers
4961                  SET group_mark_id = NULL
4962                WHERE serial_number = l_serial_number
4963                  AND inventory_item_id = l_mmtt_rec.inventory_item_id;
4964               *** End R12 }} ***/
4965 
4966              /*** {{ R12 Enhanced reservations code changes ***/
4967               serial_check.inv_unmark_rsv_serial
4968                  (from_serial_number   => l_serial_number
4969                  ,to_serial_number     => null
4970                  ,serial_code          => null
4971                  ,hdr_id               => null
4972                  ,p_inventory_item_id  => l_mmtt_rec.inventory_item_id
4973                  ,p_update_reservation => fnd_api.g_true);
4974              /*** End R12 }} ***/
4975             END IF;
4976           END LOOP;
4977           CLOSE serial_csr;
4978 
4979           IF ( l_primary_shipping_quantity <> 1 ) THEN
4980             -- MSNT records need to be deleted since TM does not
4981             DELETE FROM mtl_serial_numbers_temp
4982             WHERE transaction_temp_id = p_transaction_temp_id;
4983           END IF;
4984         END IF;
4985       ELSIF ( l_lot_control_code > 1 AND l_serial_control_code IN (1, 6)) THEN
4986         -- Only lot controlled
4987         l_lot_shipping_quantity       := 0;
4988         l_lot_prim_shipping_quantity  := 0;
4989         OPEN lot_csr(p_transaction_temp_id);
4990 
4991         LOOP
4992           FETCH lot_csr INTO l_lot_number, l_lot_primary_quantity, l_lot_transaction_quantity,
4993                              l_lot_secondary_quantity, l_lot_secondary_uom, l_serial_trx_temp_id, l_grade_code;
4994           EXIT WHEN lot_csr%NOTFOUND;
4995 
4996           -- If this item is catch weight enabled, check to see if any other MTLT lines for
4997           -- this LPN.  If any do not have secondary quantity defined, do not populate
4998           -- picked_quantity2
4999           IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY AND
5000                l_lot_number <> l_previous_lot_number ) THEN
5001             l_previous_lot_number := l_lot_number;
5002 
5003             BEGIN
5004               SELECT WMS_CATCH_WEIGHT_PVT.G_PRICE_PRIMARY INTO l_catch_weight_enabled FROM DUAL
5005               WHERE EXISTS (
5006                 SELECT 1
5007                 FROM mtl_material_transactions_temp mmtt,
5008                      mtl_transaction_lots_temp mtlt
5009                 WHERE mmtt.organization_id = l_mmtt_rec.organization_id
5010                 AND   mmtt.inventory_item_id = l_mmtt_rec.inventory_item_id
5011                 AND   NVL(mmtt.revision, '@') = NVL(l_mmtt_rec.revision, '@')
5012                 AND   mmtt.transaction_source_type_id = INV_GLOBALS.G_SourceType_SalesOrder
5013                 AND   mmtt.transaction_action_id = INV_GLOBALS.G_Action_Stgxfr
5014                 AND   NVL(mmtt.content_lpn_id, mmtt.transfer_lpn_id) = l_lpn_id
5015                 AND   mtlt.transaction_temp_id = mmtt.transaction_temp_id
5016                 AND   mtlt.lot_number = l_lot_number
5017                 AND   (mtlt.secondary_quantity IS NULL OR mtlt.secondary_unit_of_measure IS NULL) );
5018             EXCEPTION
5019               WHEN OTHERS THEN
5020                 l_catch_weight_enabled := WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY;
5021             END;
5022             IF (l_debug = 1) THEN
5023               DEBUG('itemid='||l_mmtt_rec.inventory_item_id||' rev='||l_mmtt_rec.revision||' lot='||l_lot_number||' lpnid='||l_lpn_id||' cwe='||l_catch_weight_enabled, l_api_name);
5024             END IF;
5025           END IF;
5026 
5027           l_lot_shipping_quantity              := l_lot_shipping_quantity + l_lot_transaction_quantity;
5028           l_lot_prim_shipping_quantity         := l_lot_prim_shipping_quantity + l_lot_primary_quantity;
5029 
5030           IF l_lot_shipping_quantity > l_shipping_quantity THEN
5031             l_lot_transaction_quantity  := l_lot_transaction_quantity - (l_lot_shipping_quantity - l_shipping_quantity);
5032             l_lot_primary_quantity      := l_lot_primary_quantity - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
5033           END IF;
5034 
5035           EXIT WHEN l_lot_transaction_quantity <= 0;
5036           l_shipping_attr(1).lot_number        := l_lot_number;
5037           l_shipping_attr(1).preferred_grade   := l_grade_code;
5038           l_shipping_attr(1).picked_quantity   := ABS(l_lot_primary_quantity);
5039           l_shipping_attr(1).picked_quantity2  := ABS(l_lot_secondary_quantity);
5040           --update pending quantity to reflect new pending quantity
5041           l_primary_pending_quantity           := l_primary_pending_quantity - ABS(l_lot_primary_quantity);
5042           l_sec_pending_quantity               := l_sec_pending_quantity - ABS(l_lot_secondary_quantity);
5043           l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
5044           l_shipping_attr(1).pending_quantity2 := l_sec_pending_quantity;
5045           l_return_status                      := '';
5046           IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
5047             l_shipping_attr(1).picked_quantity2 := ABS(l_lot_secondary_quantity);
5048             l_shipping_attr(1).ordered_quantity_uom2 := l_lot_secondary_uom;
5049           END IF;
5050 
5051           --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
5052           --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
5053           l_InvPCInRecType.transaction_temp_id := NULL;
5054 
5055           IF (l_debug = 1) THEN
5056             DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' trxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
5057           END IF;
5058 
5059           WSH_INTEGRATION.Set_Inv_PC_Attributes
5060           ( p_in_attributes         =>   l_InvPCInRecType,
5061             x_return_status         =>   l_return_status,
5062             x_msg_count             =>   l_msg_count,
5063             x_msg_data              =>   l_msg_data );
5064           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5065             IF (l_debug = 1) THEN
5066               DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
5067             END IF;
5068             RAISE fnd_api.g_exc_error;
5069           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5070             IF (l_debug = 1) THEN
5071               DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
5072             END IF;
5073             RAISE fnd_api.g_exc_unexpected_error;
5074           END IF;
5075 
5076           IF (l_debug = 1) THEN
5077              DEBUG('Calling Update Shipping Attributes', 'Finalize Pick Confirm');
5078              DEBUG('Picked qty  : '|| ABS(l_lot_primary_quantity), 'Finalize_Pick_Confirm');
5079              DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
5080              DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
5081              DEBUG('Pending qty : '|| l_primary_pending_quantity, 'Finalize_Pick_Confirm');
5082           END IF;
5083           wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
5084           IF (l_debug = 1) THEN
5085              DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
5086           END IF;
5087 
5088           IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5089             IF (l_debug = 1) THEN
5090                DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
5091             END IF;
5092             RAISE fnd_api.g_exc_error;
5093           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5094             IF (l_debug = 1) THEN
5095                DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
5096             END IF;
5097             RAISE fnd_api.g_exc_unexpected_error;
5098           END IF;
5099         END LOOP;
5100 
5101         CLOSE lot_csr;
5102       ELSE
5103         -- No lot or serial control
5104         -- If this item is catch weight enabled, check to see if any other MMTT lines for
5105         -- this LPN.  If any do not have secondary quantity defined, do not populate
5106         -- picked_quantity2
5107         IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
5108           BEGIN
5109             SELECT WMS_CATCH_WEIGHT_PVT.G_PRICE_PRIMARY INTO l_catch_weight_enabled FROM DUAL
5110             WHERE EXISTS (
5111               SELECT 1
5112               FROM mtl_material_transactions_temp mmtt
5113               WHERE mmtt.organization_id = l_mmtt_rec.organization_id
5114               AND   mmtt.inventory_item_id = l_mmtt_rec.inventory_item_id
5115               AND   NVL(mmtt.revision, '@') = NVL(l_mmtt_rec.revision, '@')
5116               AND   mmtt.transaction_source_type_id = INV_GLOBALS.G_SourceType_SalesOrder
5117               AND   mmtt.transaction_action_id = INV_GLOBALS.G_Action_Stgxfr
5118               AND   NVL(mmtt.content_lpn_id, mmtt.transfer_lpn_id) = l_lpn_id
5119               AND   (mmtt.secondary_transaction_quantity IS NULL OR mmtt.secondary_uom_code IS NULL) );
5120           EXCEPTION
5121             WHEN OTHERS THEN
5122               l_catch_weight_enabled := WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY;
5123           END;
5124           IF (l_debug = 1) THEN
5125             DEBUG('itemid='||l_mmtt_rec.inventory_item_id||' rev='||l_mmtt_rec.revision||' lot='||l_lot_number||' lpnid='||l_lpn_id||' cwe='||l_catch_weight_enabled, l_api_name);
5126           END IF;
5127         END IF;
5128 
5129         l_shipping_attr(1).picked_quantity   := l_primary_shipping_quantity;
5130         l_shipping_attr(1).picked_quantity2  := l_sec_shipping_quantity;
5131         --update pending quantity to reflect new pending quantity
5132         l_primary_pending_quantity           := l_primary_pending_quantity - l_primary_shipping_quantity;
5133         l_sec_pending_quantity               := l_sec_pending_quantity - l_sec_shipping_quantity;
5134         l_shipping_attr(1).pending_quantity  := l_primary_pending_quantity;
5135         l_shipping_attr(1).pending_quantity2 := l_sec_pending_quantity;
5136         l_return_status                      := '';
5137         IF ( l_catch_weight_enabled = WMS_CATCH_WEIGHT_PVT.G_PRICE_SECONDARY ) THEN
5138           l_shipping_attr(1).picked_quantity2 := ABS(l_mmtt_rec.secondary_transaction_quantity);
5139           l_shipping_attr(1).ordered_quantity_uom2 := l_mmtt_rec.secondary_uom_code;
5140         END IF;
5141 
5142         --Use Set_Inv_PC_Attributes to set trx temp id to be populated in wdd
5143         --Fix for dependency issue on shipping's WSHDDINS.pls 115.64
5144         l_InvPCInRecType.transaction_temp_id := NULL;
5145 
5146         IF (l_debug = 1) THEN
5147           DEBUG('Calling Set_Inv_PC_Attributes with trxid='||l_InvPCInRecType.transaction_id||' trxtmpid='||l_InvPCInRecType.transaction_temp_id, 'Finalize Pick Confirm');
5148         END IF;
5149 
5150         WSH_INTEGRATION.Set_Inv_PC_Attributes
5151         ( p_in_attributes         =>   l_InvPCInRecType,
5152           x_return_status         =>   l_return_status,
5153           x_msg_count             =>   l_msg_count,
5154           x_msg_data              =>   l_msg_data );
5155         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5156           IF (l_debug = 1) THEN
5157             DEBUG('return error E from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
5158           END IF;
5159           RAISE fnd_api.g_exc_error;
5160         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5161           IF (l_debug = 1) THEN
5162             DEBUG('return error U from Set_Inv_PC_Attributes', 'Finalize Pick Confirm');
5163           END IF;
5164           RAISE fnd_api.g_exc_unexpected_error;
5165         END IF;
5166 
5167         IF (l_debug = 1) THEN
5168            DEBUG('Calling Update Shipping Attributes', 'Finalize Pick Confirm');
5169            DEBUG('Picked qty  : '|| l_primary_shipping_quantity, 'Finalize_Pick_Confirm');
5170            DEBUG('Picked qty2 : '|| l_shipping_attr(1).picked_quantity2, l_api_name);
5171            DEBUG('Ordered uom2: '|| l_shipping_attr(1).ordered_quantity_uom2, l_api_name);
5172            DEBUG('Pending qty : '|| l_primary_pending_quantity, 'Finalize_Pick_Confirm');
5173         END IF;
5174         wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr, x_return_status => l_return_status);
5175         IF (l_debug = 1) THEN
5176            DEBUG('after update shipping attributes', 'Finalize_Pick_Confirm');
5177         END IF;
5178 
5179         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5180           IF (l_debug = 1) THEN
5181              DEBUG('return error from update shipping attributes', 'Finalize_Pick_Confirm');
5182           END IF;
5183           RAISE fnd_api.g_exc_error;
5184         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5185           IF (l_debug = 1) THEN
5186              DEBUG('return unexpected error from update shipping attributes', 'Finalize_Pick_Confirm');
5187           END IF;
5188           RAISE fnd_api.g_exc_unexpected_error;
5189         END IF;
5190       END IF; -- lot control and serial control
5191 
5192       IF l_trolin_rec.line_status = 5 THEN
5193         -- If this is the last allocation line for the last move order
5194         -- line for the sales order then delete all the unstaged
5195         -- reservations for this record. This will also delete
5196         -- reservations which are against the backordered lines.
5197         clean_reservations(p_source_line_id => l_trolin_rec.txn_source_line_id, x_return_status => l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data);
5198 
5199         IF (l_return_status = fnd_api.g_ret_sts_error) THEN
5200           IF (l_debug = 1) THEN
5201              DEBUG('return error from clean reservations', 'Finalize_Pick_Confirm');
5202           END IF;
5203           RAISE fnd_api.g_exc_error;
5204         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5205           IF (l_debug = 1) THEN
5206              DEBUG('return unexpected error from clean reservations', 'Finalize_Pick_Confirm');
5207           END IF;
5208           RAISE fnd_api.g_exc_unexpected_error;
5209         END IF;
5210 
5211         -- Bug 1838450
5212         -- Fix the detailed quantity on all reservations for this
5213         --  sales order line. Necessary for underpicking.
5214         IF (l_debug = 1) THEN
5215            DEBUG('Cleaning up reservations', 'Finalize_Pick_Confirm');
5216         END IF;
5217         OPEN reservations_csr;
5218 
5219         LOOP
5220           FETCH reservations_csr INTO l_reservation_id, l_rsv_primary_quantity, l_rsv_detailed_quantity;
5221           EXIT WHEN reservations_csr%NOTFOUND;
5222           IF (l_debug = 1) THEN
5223              DEBUG('Found first reservation: '|| l_reservation_id, 'Finalize_Pick_Confirm');
5224           END IF;
5225 
5226           --for each reservation, check to see what the actual
5227           --allocated quantity is
5228           SELECT NVL(SUM(ABS(primary_quantity)), 0)
5229               ,  NVL(SUM(ABS(secondary_transaction_quantity)), 0)
5230             INTO l_mmtt_rsv_quantity
5231               ,  l_sec_mmtt_rsv_quantity
5232             FROM mtl_material_transactions_temp
5233            WHERE reservation_id = l_reservation_id;
5234 
5235           -- need to subtract from the mmtt rsv quantity the
5236           --  quantity of the reservations that have already been
5237           --  picked, since the rsv's detailed quantity does not
5238           --  include this quantity
5239           IF g_rsv_picked_quantity_tbl.EXISTS(l_reservation_id) THEN
5240             l_mmtt_rsv_quantity  := l_mmtt_rsv_quantity - g_rsv_picked_quantity_tbl(l_reservation_id).picked_quantity;
5241           END IF;
5242 
5243           -- Update reservation if necessary
5244           IF l_mmtt_rsv_quantity < l_rsv_detailed_quantity THEN
5245             --call update reservation
5246             l_mtl_reservation_rec2.reservation_id     := l_reservation_id;
5247             l_mtl_reservation_rec3.reservation_id     := l_reservation_id;
5248             l_mtl_reservation_rec3.detailed_quantity  := l_mmtt_rsv_quantity;
5249             l_mtl_reservation_rec3.secondary_detailed_quantity  := l_sec_mmtt_rsv_quantity;
5250             IF (l_debug = 1) THEN
5251                DEBUG('reservation id is '|| l_mtl_reservation_rec2.reservation_id, 'Finalize_Pick_Confirm');
5252                DEBUG('primary_reservation quantity is '|| l_mtl_reservation_rec3.primary_reservation_quantity, 'Finalize_Pick_Confirm');
5253                DEBUG('detailed quantity is '|| l_mtl_reservation_rec3.detailed_quantity, 'Finalize_Pick_Confirm');
5254             END IF;
5255 
5256 --INVCONV - Make sure Qty2 are NULL if nor present
5257         IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
5258               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
5259               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
5260         END IF;
5261             inv_reservation_pub.update_reservation(
5262               p_api_version_number         => 1.0
5263             , p_init_msg_lst               => fnd_api.g_false
5264             , x_return_status              => l_return_status
5265             , x_msg_count                  => x_msg_count
5266             , x_msg_data                   => x_msg_data
5267             , p_original_rsv_rec           => l_mtl_reservation_rec2
5268             , p_to_rsv_rec                 => l_mtl_reservation_rec3
5269             , p_original_serial_number     => l_original_serial_number
5270             , p_to_serial_number           => l_to_serial_number
5271             , p_validation_flag            => fnd_api.g_true
5272             , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5273             );
5274             IF (l_debug = 1) THEN
5275                DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
5276             END IF;
5277 
5278             IF l_return_status = fnd_api.g_ret_sts_error THEN
5279               RAISE fnd_api.g_exc_error;
5280             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5281               RAISE fnd_api.g_exc_unexpected_error;
5282             END IF;
5283           END IF; -- if allocated quantity < detailed rsv quantity
5284         END LOOP;
5285         IF reservations_csr%ISOPEN THEN
5286           CLOSE reservations_csr;
5287         END IF;
5288       END IF; -- line status = 5
5289     END IF; -- if move order type = 3
5290 
5291     If l_move_order_type = 5 and l_wip_entity_type in (9,10) then -- GME move orders
5292       IF (l_debug = 1) THEN
5293          DEBUG('inside l_move_order_ytpe = 5, and GME move orders', 'Finalize_Pick_Confirm');
5294       END IF;
5295 
5296       SELECT lot_control_code
5297            , serial_number_control_code
5298            , reservable_type
5299         INTO l_lot_control_code
5300            , l_serial_control_code
5301            , l_reservable_type_item
5302         FROM mtl_system_items
5303        WHERE inventory_item_id = l_mmtt_rec.inventory_item_id
5304          AND organization_id = l_mmtt_rec.organization_id;
5305 
5306       IF (l_debug = 1) THEN
5307          DEBUG('After select lot_control_code = '|| l_lot_control_code, 'Finalize_Pick_confirm');
5308          DEBUG('After select l_serial_control_code = '|| l_serial_control_code, 'Finalize_Pick_Confirm');
5309       END IF;
5310 
5311       -- Bug 5535030: cache subinventory reservable type
5312       l_hash_value := DBMS_UTILITY.get_hash_value
5313                       ( NAME      => to_char(l_mmtt_rec.organization_id)
5314                                      ||'-'|| l_mmtt_rec.transfer_subinventory
5315                       , base      => 1
5316                       , hash_size => POWER(2, 25)
5317                       );
5318       IF g_is_sub_reservable.EXISTS(l_hash_value) AND
5319          g_is_sub_reservable(l_hash_value).org_id = l_mmtt_rec.organization_id AND
5320          g_is_sub_reservable(l_hash_value).subinventory_code = l_mmtt_rec.transfer_subinventory
5321       THEN
5322          l_reservable_type := g_is_sub_reservable(l_hash_value).reservable_type;
5323       ELSE
5324          SELECT reservable_type
5325            INTO l_reservable_type
5326            FROM mtl_secondary_inventories
5327           WHERE organization_id = l_mmtt_rec.organization_id
5328             AND secondary_inventory_name = l_mmtt_rec.transfer_subinventory;
5329          g_is_sub_reservable(l_hash_value).reservable_type := l_reservable_type;
5330          g_is_sub_reservable(l_hash_value).org_id := l_mmtt_rec.organization_id;
5331          g_is_sub_reservable(l_hash_value).subinventory_code := l_mmtt_rec.transfer_subinventory;
5332       END IF;
5333 
5334       l_mtl_reservation_tbl_count            := 0;
5335 
5336       IF (l_mmtt_rec.reservation_id IS NOT NULL) THEN
5337         l_query_reservation_rec.reservation_id  := l_mmtt_rec.reservation_id;
5338         IF (l_debug = 1) THEN
5339            DEBUG('reservation_id is  = '|| l_mmtt_rec.reservation_id, 'Finalize_Pick_confirm');
5340            DEBUG('about to call inv_reservation_pub.query_reservations ', 'Finalize_Pick_confirm');
5341         END IF;
5342         inv_reservation_pub.query_reservation(
5343           p_api_version_number         => 1.0
5344         , x_return_status              => l_return_status
5345         , x_msg_count                  => l_msg_count
5346         , x_msg_data                   => l_msg_data
5347         , p_query_input                => l_query_reservation_rec
5348         , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
5349         , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5350         , x_error_code                 => l_error_code
5351         );
5352 
5353         IF l_return_status = fnd_api.g_ret_sts_error THEN
5354           IF (l_debug = 1) THEN
5355              DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
5356           END IF;
5357           RAISE fnd_api.g_exc_error;
5358         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5359           IF (l_debug = 1) THEN
5360              DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
5361           END IF;
5362           RAISE fnd_api.g_exc_unexpected_error;
5363         END IF;
5364 
5365         IF (l_debug = 1) THEN
5366            DEBUG('reservation count = '|| l_mtl_reservation_tbl.COUNT, 'Finalize_Pick_Confirm');
5367            DEBUG('after query_reservation', 'Finalize_Pick_Confirm');
5368         END IF;
5369       END IF;
5370 
5371       l_lpn_id := NVL(l_mmtt_rec.content_lpn_id, l_mmtt_rec.transfer_lpn_id);
5372 
5373       IF l_mtl_reservation_tbl_count > 0 THEN
5374         l_mtl_reservation_rec  := l_mtl_reservation_tbl(1);
5375         IF (l_debug = 1) THEN
5376            DEBUG('Reservation exist total #'|| l_mtl_reservation_tbl_count, 'Finalize_Pick_Confirm');
5377            DEBUG('lot number in the original reservation is '|| l_mtl_reservation_rec.lot_number, 'Finalize_Pick_Confirm');
5378         END IF;
5379 
5380         -- To support overpicking, we have to increase the reservation
5381         -- quantity when the user enters a transaction quantity which
5382         -- is greater than the requested quantity.
5383         -- We increase the reservation before transferring it to the
5384         -- staging subinventory.
5385 
5386         IF l_rsv_primary_quantity > l_mtl_reservation_rec.primary_reservation_quantity THEN
5387           IF (l_debug = 1) THEN
5388              DEBUG('Increasing reservation quantity for overpicking', 'Finalize_Pick_Confirm');
5389              DEBUG('Old rsv prim. quantity: '|| l_mtl_reservation_rec.primary_reservation_quantity, 'Finalize_Pick_Confirm');
5390              DEBUG('New rsv prim. quantity: '|| l_rsv_primary_quantity, 'Finalize_Pick_Confirm');
5391           END IF;
5392           l_mtl_reservation_rec.primary_reservation_quantity     := l_rsv_primary_quantity;
5393           l_mtl_reservation_rec.detailed_quantity                := l_rsv_primary_quantity;
5394           l_mtl_reservation_rec.secondary_reservation_quantity   := l_sec_rsv_quantity;
5395           l_mtl_reservation_rec.secondary_detailed_quantity      := l_sec_rsv_quantity;
5396           l_mtl_reservation_rec.reservation_quantity             := NULL;
5397           IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
5398               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
5399               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
5400           END IF;
5401           inv_reservation_pub.update_reservation(
5402             p_api_version_number         => 1.0
5403           , p_init_msg_lst               => fnd_api.g_false
5404           , x_return_status              => l_return_status
5405           , x_msg_count                  => x_msg_count
5406           , x_msg_data                   => x_msg_data
5407           , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5408           , p_to_rsv_rec                 => l_mtl_reservation_rec
5409           , p_original_serial_number     => l_original_serial_number
5410           , p_to_serial_number           => l_to_serial_number
5411           , p_validation_flag            => fnd_api.g_true -- Explicitly set the validation flag to true with respect to the Bug 4004597
5412           , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5413           );
5414           IF (l_debug = 1) THEN
5415              DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
5416           END IF;
5417 
5418           IF l_return_status = fnd_api.g_ret_sts_error THEN
5419             RAISE fnd_api.g_exc_error;
5420           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5421             RAISE fnd_api.g_exc_unexpected_error;
5422           END IF;
5423 
5424           --requery reservation to reflect updated data
5425           inv_reservation_pub.query_reservation(
5426             p_api_version_number         => 1.0
5427           , x_return_status              => l_return_status
5428           , x_msg_count                  => l_msg_count
5429           , x_msg_data                   => l_msg_data
5430           , p_query_input                => l_query_reservation_rec
5431           , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
5432           , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5433           , x_error_code                 => l_error_code
5434           );
5435 
5436           IF l_return_status = fnd_api.g_ret_sts_error THEN
5437             IF (l_debug = 1) THEN
5438                DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
5439             END IF;
5440             RAISE fnd_api.g_exc_error;
5441           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5442             IF (l_debug = 1) THEN
5443                DEBUG('Error from query_reservations', 'Finalize_Pick_Confirm');
5444             END IF;
5445             RAISE fnd_api.g_exc_unexpected_error;
5446           END IF;
5447         END IF;
5448 
5449         -- Create a new reservation on a staging sub
5450         -- for the transaction_quantity;
5451         -- initialize l_mtl_reservation_rec with the record transacted.
5452         -- change the the value of changed attributes
5453         -- in l_mtl_reservation_rec
5454         IF (l_debug = 1) THEN
5455            DEBUG('l_mmtt_rec.transaction_temp_id is '|| l_mmtt_rec.transaction_temp_id, 'Finalize_Pick_confirm');
5456         END IF;
5457 
5458         SELECT COUNT(*) INTO l_lot_count
5459           FROM mtl_transaction_lots_temp
5460          WHERE transaction_temp_id = p_transaction_temp_id;
5461 
5462         IF (l_debug = 1) THEN
5463            DEBUG('l_lot_count is '|| l_lot_count, 'Finalize_Pick_Confirm');
5464         END IF;
5465 
5466         IF (l_reservable_type = 2) THEN
5467           IF (l_debug = 1) THEN
5468              DEBUG('not reservable staging subinventory, '|| 'delete org wide reservation', 'Finalize_Pick_Confirm');
5469           END IF;
5470           l_mtl_reservation_rec                       := l_mtl_reservation_tbl(1);
5471           -- reservation quantity should be NULL; it will be
5472           -- determined based on primary quantity
5473           l_mtl_reservation_rec.reservation_quantity  := NULL;
5474 
5475           IF NVL(l_mtl_reservation_rec.primary_reservation_quantity, 0) > ABS(l_mmtt_rec.primary_quantity) THEN
5476             l_mtl_reservation_rec.primary_reservation_quantity
5477                              := NVL(l_mtl_reservation_rec.primary_reservation_quantity, 0)
5478                                       - ABS(l_mmtt_rec.primary_quantity);
5479             l_mtl_reservation_rec.secondary_reservation_quantity
5480                              := NVL(l_mtl_reservation_rec.secondary_reservation_quantity, 0)
5481                                       - ABS(l_mmtt_rec.secondary_transaction_quantity);
5482           ELSE -- if qty > rsv qty, delete reservation
5483             l_mtl_reservation_rec.primary_reservation_quantity  := 0;
5484             l_mtl_reservation_rec.secondary_reservation_quantity  := 0;
5485           END IF;
5486 
5487           --need to decrement from detailed quantity
5488           IF NVL(l_mtl_reservation_rec.detailed_quantity, 0) > ABS(l_mmtt_rec.primary_quantity) THEN
5489             l_mtl_reservation_rec.detailed_quantity
5490                              := NVL(l_mtl_reservation_rec.detailed_quantity, 0)
5491                                       - ABS(l_mmtt_rec.primary_quantity);
5492             l_mtl_reservation_rec.secondary_detailed_quantity
5493                              := NVL(l_mtl_reservation_rec.secondary_detailed_quantity, 0)
5494                                       - ABS(l_mmtt_rec.secondary_transaction_quantity);
5495           ELSE
5496             l_mtl_reservation_rec.detailed_quantity  := 0;
5497             l_mtl_reservation_rec.secondary_detailed_quantity  := 0;
5498           END IF;
5499 
5500           IF (l_debug = 1) THEN
5501              DEBUG('primary reservation quantity is '|| l_mtl_reservation_rec.primary_reservation_quantity, 'Finalize_Pick_Confirm');
5502           END IF;
5503           IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
5504               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
5505               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
5506           END IF;
5507           inv_reservation_pub.update_reservation(
5508             p_api_version_number         => 1.0
5509           , p_init_msg_lst               => fnd_api.g_false
5510           , x_return_status              => l_return_status
5511           , x_msg_count                  => x_msg_count
5512           , x_msg_data                   => x_msg_data
5513           , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5514           , p_to_rsv_rec                 => l_mtl_reservation_rec
5515           , p_original_serial_number     => l_original_serial_number
5516           , p_to_serial_number           => l_to_serial_number
5517           , p_validation_flag            => fnd_api.g_true
5518           , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5519           );
5520           IF (l_debug = 1) THEN
5521              DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
5522           END IF;
5523 
5524           IF l_return_status = fnd_api.g_ret_sts_error THEN
5525             RAISE fnd_api.g_exc_error;
5526           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5527             RAISE fnd_api.g_exc_unexpected_error;
5528           END IF;
5529         ELSE   -- reservable type <> 2
5530           IF (l_debug = 1) THEN
5531              DEBUG('reservable staging subinventory, '|| 'transfer reservation to staging', 'Finalize_Pick_Confirm');
5532           END IF;
5533 
5534           IF (l_lot_count > 0) THEN
5535              IF (l_debug = 1) THEN
5536                  DEBUG('Lot records exist l_lot_count '|| l_lot_count, 'Finalize_Pick_Confirm');
5537              END IF;
5538             l_transaction_temp_id         := l_mmtt_rec.transaction_temp_id;
5539             l_lot_shipping_quantity       := 0;
5540             l_lot_prim_shipping_quantity  := 0;
5541             OPEN lot_csr(p_transaction_temp_id);
5542 
5543             LOOP
5544               FETCH lot_csr
5545               INTO l_lot_number
5546                  , l_lot_primary_quantity
5547                  , l_lot_transaction_quantity
5548                  , l_lot_secondary_quantity
5549                  , l_lot_secondary_uom
5550                  , l_serial_trx_temp_id
5551                  , l_grade_code;
5552               l_lot_shipping_quantity       := l_lot_shipping_quantity + l_lot_transaction_quantity;
5553               l_sec_lot_shipping_quantity   := l_sec_lot_shipping_quantity + l_lot_secondary_quantity;
5554               l_lot_prim_shipping_quantity  := l_lot_prim_shipping_quantity + l_lot_primary_quantity;
5555               IF l_lot_shipping_quantity > l_shipping_quantity THEN
5556                 l_lot_transaction_quantity  := l_lot_transaction_quantity
5557                                                - (l_lot_shipping_quantity - l_shipping_quantity);
5558                 l_lot_primary_quantity      := l_lot_primary_quantity
5559                                                - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
5560                 l_lot_secondary_quantity  := l_lot_secondary_quantity
5561                                                - (l_sec_lot_shipping_quantity - l_sec_shipping_quantity);
5562               END IF;
5563               EXIT WHEN lot_csr%NOTFOUND
5564                      OR l_lot_transaction_quantity <= 0;
5565               IF (l_debug = 1) THEN
5566                  DEBUG('lot number is '|| l_mtl_reservation_rec.lot_number, 'Finalize_Pick_Confirm');
5567               END IF;
5568               l_mtl_reservation_rec.reservation_id                := NULL;
5569 
5570               -- bug 3703983
5571               --l_mtl_reservation_rec.requirement_date              := SYSDATE;
5572               l_mtl_reservation_rec.primary_reservation_quantity  := ABS(l_lot_primary_quantity);
5573               l_mtl_reservation_rec.reservation_quantity          := ABS(l_lot_transaction_quantity);
5574               l_mtl_reservation_rec.reservation_uom_code          := l_mmtt_rec.transaction_uom;
5575               l_mtl_reservation_rec.subinventory_code             := l_mmtt_rec.transfer_subinventory;
5576               l_mtl_reservation_rec.detailed_quantity             := 0;
5577               l_mtl_reservation_rec.secondary_reservation_quantity  := ABS(l_lot_secondary_quantity);
5578               l_mtl_reservation_rec.secondary_uom_code            := l_mmtt_rec.secondary_uom_code;
5579               l_mtl_reservation_rec.secondary_detailed_quantity   := 0;
5580               l_mtl_reservation_rec.locator_id                    := l_mmtt_rec.transfer_to_location;
5581               l_mtl_reservation_rec.ship_ready_flag               := 1;
5582               l_mtl_reservation_rec.lot_number                    := l_lot_number;
5583               l_mtl_reservation_rec.revision                      := l_mmtt_rec.revision;
5584 
5585               -- bug 3703983
5586               l_mtl_reservation_rec.staged_flag                   := 'Y';
5587 
5588               --don't reserve LPN in staging sub unless LPN was
5589               -- reserved on original reservation
5590               IF l_mtl_reservation_tbl(1).lpn_id IS NOT NULL THEN
5591                 l_mtl_reservation_rec.lpn_id  := l_lpn_id;
5592               END IF;
5593 
5594               IF (l_debug = 1) THEN
5595                  DEBUG('Transfering reservations', 'Finalize_Pick_Confirm');
5596               end if;
5597               inv_reservation_pub.transfer_reservation(
5598                 p_api_version_number         => 1.0
5599               , p_init_msg_lst               => fnd_api.g_false
5600               , x_return_status              => l_return_status
5601               , x_msg_count                  => x_msg_count
5602               , x_msg_data                   => x_msg_data
5603               , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5604               , p_to_rsv_rec                 => l_mtl_reservation_rec
5605               , p_original_serial_number     => l_to_serial_number
5606               , p_to_serial_number           => l_to_serial_number
5607               , p_validation_flag            => fnd_api.g_false
5608               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5609               , x_to_reservation_id          => l_reservation_id
5610               );
5611               IF (l_debug = 1) THEN
5612                  DEBUG('new reservation id is '|| l_reservation_id, 'Finalize_Pick_Confirm');
5613                  DEBUG('after create new  reservation', 'Finalize_Pick_Confirm');
5614                  DEBUG('l_return_status is '|| l_return_status, 'Finalize_Pick_Confirm');
5615               END IF;
5616 
5617               IF l_return_status = fnd_api.g_ret_sts_error THEN
5618                 IF (l_debug = 1) THEN
5619                    DEBUG('return from transfer_reservation with error E', 'Finalize_Pick_Confirm');
5620                 END IF;
5621                 RAISE fnd_api.g_exc_error;
5622               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5623                 IF (l_debug = 1) THEN
5624                    DEBUG('return from transfer_reservation with error U', 'Finalize_Pick_Confirm');
5625                 END IF;
5626                 RAISE fnd_api.g_exc_unexpected_error;
5627               END IF;
5628 
5629               -- bug 3703983
5630               -- inv_staged_reservation_util.update_staged_flag(x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data, p_reservation_id => l_reservation_id, p_staged_flag => 'Y');
5631 
5632               --IF l_return_status = fnd_api.g_ret_sts_error THEN
5633               -- (l_debug = 1) THEN
5634               --    DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
5635               -- END IF;
5636               -- RAISE fnd_api.g_exc_error;
5637               --ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5638               -- IF (l_debug = 1) THEN
5639               --    DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
5640               -- END IF;
5641               -- RAISE fnd_api.g_exc_unexpected_error;
5642               --END IF;
5643             END LOOP;
5644 
5645             IF (l_debug = 1) THEN
5646                DEBUG('after end loop', 'Finalize_Pick_Confirm');
5647             END IF;
5648             CLOSE lot_csr;
5649           ELSE
5650             IF (l_debug = 1) THEN
5651                DEBUG('no lot records', 'Finalize_Pick_Confirm');
5652             END IF;
5653             l_mtl_reservation_rec.reservation_id                := NULL;
5654             l_mtl_reservation_rec.primary_reservation_quantity  := l_primary_shipping_quantity;
5655             l_mtl_reservation_rec.reservation_quantity          := l_shipping_quantity;
5656             l_mtl_reservation_rec.reservation_uom_code          := l_mmtt_rec.transaction_uom;
5657             l_mtl_reservation_rec.secondary_reservation_quantity  := l_sec_shipping_quantity;
5658             l_mtl_reservation_rec.secondary_uom_code            := l_mmtt_rec.secondary_uom_code;
5659             l_mtl_reservation_rec.subinventory_code             := l_mmtt_rec.transfer_subinventory;
5660             l_mtl_reservation_rec.detailed_quantity             := NULL;
5661             l_mtl_reservation_rec.secondary_detailed_quantity   := NULL;
5662             l_mtl_reservation_rec.locator_id                    := l_mmtt_rec.transfer_to_location;
5663             l_mtl_reservation_rec.ship_ready_flag               := 1;
5664             l_mtl_reservation_rec.revision                      := l_mmtt_rec.revision;
5665             l_mtl_reservation_rec.staged_flag                   := 'Y';
5666 
5667             --don't reserve LPN in staging sub unless LPN was
5668             -- reserved on original reservation
5669             IF l_mtl_reservation_tbl(1).lpn_id IS NOT NULL THEN
5670               l_mtl_reservation_rec.lpn_id  := l_lpn_id;
5671             END IF;
5672 
5673             IF (l_debug = 1) THEN
5674                DEBUG('Transfering reservation ', 'Finalize_Pick_Confirm');
5675                DEBUG('l_primary_shipping_quantity: '|| l_primary_shipping_quantity, 'Finalize_Pick_Confirm');
5676             END IF;
5677             inv_reservation_pub.transfer_reservation(
5678               p_api_version_number         => 1.0
5679             , p_init_msg_lst               => fnd_api.g_false
5680             , x_return_status              => l_return_status
5681             , x_msg_count                  => x_msg_count
5682             , x_msg_data                   => x_msg_data
5683             , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5684             , p_to_rsv_rec                 => l_mtl_reservation_rec
5685             , p_original_serial_number     => l_to_serial_number
5686             , p_to_serial_number           => l_to_serial_number
5687             , p_validation_flag            => fnd_api.g_false
5688             , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5689             , x_to_reservation_id          => l_reservation_id
5690             );
5691             IF (l_debug = 1) THEN
5692                DEBUG('new reservation id is '|| l_reservation_id, 'Finalize_Pick_Confirm');
5693                DEBUG('after create new reservation', 'Finalize_Pick_Confirm');
5694                DEBUG('l_return_status is '|| l_return_status, 'Finalize_Pick_Confirm');
5695             END IF;
5696             IF l_return_status = fnd_api.g_ret_sts_error THEN
5697               IF (l_debug = 1) THEN
5698                  DEBUG('return from transfer_reservation with error E', 'Finalize_Pick_Confirm');
5699               END IF;
5700               RAISE fnd_api.g_exc_error;
5701             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5702               IF (l_debug = 1) THEN
5703                  DEBUG('return from transfer_reservation with error U', 'Finalize_Pick_Confirm');
5704               END IF;
5705               RAISE fnd_api.g_exc_unexpected_error;
5706             END IF;
5707             IF (l_debug = 1) THEN
5708                DEBUG('still inside if no lot records', 'Finalize_Pick_Confirm');
5709             END IF;
5710           END IF; -- lot or not lot control
5711         END IF; -- reservable or not
5712       ELSE -- query reservation returns 0 records
5713         DEBUG('NO reservations exist for this line', 'Finalize_Pick_Confirm');
5714         -- Reservation does not exist, we need to create one if the
5715         -- staging sub is reservable and item is reservable
5716         -- check the function to see if sub/locator is reservable, lot is later
5717         IF  l_reservable_type = 1 AND l_reservable_type_item = 1 THEN
5718           -- If the staging subinventory  is reservable
5719           l_mtl_reservation_rec.reservation_id             := NULL; -- cannot know
5720           l_mtl_reservation_rec.organization_id            := l_mmtt_rec.organization_id;
5721           l_mtl_reservation_rec.inventory_item_id          := l_mmtt_rec.inventory_item_id;
5722           --l_mtl_reservation_rec.demand_source_type_id      := l_mmtt_rec.transaction_source_type_id;
5723           l_mtl_reservation_rec.demand_source_type_id      := 5 ; --l_mmtt_rec.transaction_source_type_id;
5724           l_mtl_reservation_rec.demand_source_name         := NULL;
5725           l_mtl_reservation_rec.demand_source_header_id    := l_mmtt_rec.transaction_source_id;
5726           l_mtl_reservation_rec.demand_source_line_id      := TO_NUMBER(l_mmtt_rec.trx_source_line_id);
5727           l_mtl_reservation_rec.demand_source_delivery     := NULL;
5728           l_mtl_reservation_rec.primary_uom_code           := l_mmtt_rec.item_primary_uom_code;
5729           l_mtl_reservation_rec.secondary_uom_code         := l_mmtt_rec.secondary_uom_code;
5730           l_mtl_reservation_rec.primary_uom_id             := NULL;
5731           l_mtl_reservation_rec.secondary_uom_id           := NULL;
5732           l_mtl_reservation_rec.reservation_uom_code       := l_mmtt_rec.transaction_uom;
5733           l_mtl_reservation_rec.reservation_uom_id         := NULL;
5734           l_mtl_reservation_rec.autodetail_group_id        := NULL;
5735           l_mtl_reservation_rec.external_source_code       := NULL;
5736           l_mtl_reservation_rec.external_source_line_id    := NULL;
5737           l_mtl_reservation_rec.supply_source_type_id      := inv_reservation_global.g_source_type_inv;
5738           l_mtl_reservation_rec.supply_source_header_id    := NULL;
5739           l_mtl_reservation_rec.supply_source_line_id      := NULL;
5740           l_mtl_reservation_rec.supply_source_name         := NULL;
5741           l_mtl_reservation_rec.supply_source_line_detail  := NULL;
5742           l_mtl_reservation_rec.revision                   := l_mmtt_rec.revision;
5743           l_mtl_reservation_rec.subinventory_code          := l_mmtt_rec.transfer_subinventory;
5744           l_mtl_reservation_rec.subinventory_id            := NULL;
5745           l_mtl_reservation_rec.locator_id                 := l_mmtt_rec.transfer_to_location;
5746           l_mtl_reservation_rec.lot_number_id              := NULL;
5747           l_mtl_reservation_rec.pick_slip_number           := NULL;
5748           --we reserve LPN only on user created reservations
5749           l_mtl_reservation_rec.lpn_id                     := NULL;
5750           l_mtl_reservation_rec.attribute_category         := NULL;
5751           l_mtl_reservation_rec.attribute1                 := NULL;
5752           l_mtl_reservation_rec.attribute2                 := NULL;
5753           l_mtl_reservation_rec.attribute3                 := NULL;
5754           l_mtl_reservation_rec.attribute4                 := NULL;
5755           l_mtl_reservation_rec.attribute5                 := NULL;
5756           l_mtl_reservation_rec.attribute6                 := NULL;
5757           l_mtl_reservation_rec.attribute7                 := NULL;
5758           l_mtl_reservation_rec.attribute8                 := NULL;
5759           l_mtl_reservation_rec.attribute9                 := NULL;
5760           l_mtl_reservation_rec.attribute10                := NULL;
5761           l_mtl_reservation_rec.attribute11                := NULL;
5762           l_mtl_reservation_rec.attribute12                := NULL;
5763           l_mtl_reservation_rec.attribute13                := NULL;
5764           l_mtl_reservation_rec.attribute14                := NULL;
5765           l_mtl_reservation_rec.attribute15                := NULL;
5766           l_mtl_reservation_rec.ship_ready_flag            := 1;
5767           l_mtl_reservation_rec.detailed_quantity          := 0;
5768           l_mtl_reservation_rec.secondary_detailed_quantity := 0;
5769 
5770           IF (l_debug = 1) THEN
5771              DEBUG('Dest sub '|| l_mtl_reservation_rec.subinventory_code, 'Finalize_Pick_Confirm');
5772              DEBUG('Dest locator '|| l_mtl_reservation_rec.locator_id, 'Finalize_Pick_Confirm');
5773           END IF;
5774           IF l_lot_control_code = 2 THEN
5775             --Lot control
5776             l_transaction_temp_id         := l_mmtt_rec.transaction_temp_id;
5777             l_lot_shipping_quantity       := 0;
5778             l_lot_prim_shipping_quantity  := 0;
5779             OPEN lot_csr(p_transaction_temp_id);
5780 
5781             LOOP
5782               FETCH lot_csr
5783               INTO l_lot_number
5784                  , l_lot_primary_quantity
5785                  , l_lot_transaction_quantity
5786                  , l_lot_secondary_quantity
5787                  , l_lot_secondary_uom
5788                  , l_serial_trx_temp_id
5789                  , l_grade_code;
5790               l_lot_shipping_quantity           := l_lot_shipping_quantity + l_lot_transaction_quantity;
5791               l_sec_lot_shipping_quantity       := l_sec_lot_shipping_quantity + l_lot_secondary_quantity;
5792               l_lot_prim_shipping_quantity      := l_lot_prim_shipping_quantity + l_lot_primary_quantity;
5793 
5794               IF l_lot_shipping_quantity > l_shipping_quantity THEN
5795                 l_lot_transaction_quantity  := l_lot_transaction_quantity - (l_lot_shipping_quantity - l_shipping_quantity);
5796                 l_lot_secondary_quantity    := l_lot_secondary_quantity - (l_sec_lot_shipping_quantity - l_sec_shipping_quantity);
5797                 l_lot_primary_quantity      := l_lot_primary_quantity - (l_lot_prim_shipping_quantity - l_primary_shipping_quantity);
5798               END IF;
5799 
5800               EXIT WHEN lot_csr%NOTFOUND
5801                      OR l_lot_transaction_quantity <= 0;
5802               l_mtl_reservation_rec.lot_number  := l_lot_number;
5803               -- query to see whether a record with the key
5804               -- attributes already exists
5805               -- if there is, use update instead of create
5806               inv_reservation_pub.query_reservation(
5807                 p_api_version_number         => 1.0
5808               , p_init_msg_lst               => fnd_api.g_false
5809               , x_return_status              => l_return_status
5810               , x_msg_count                  => x_msg_count
5811               , x_msg_data                   => x_msg_data
5812               , p_query_input                => l_mtl_reservation_rec
5813               , p_lock_records               => fnd_api.g_true
5814               , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
5815               , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5816               , x_error_code                 => l_error_code
5817               );
5818 
5819               IF l_return_status = fnd_api.g_ret_sts_error THEN
5820                 RAISE fnd_api.g_exc_error;
5821               END IF;
5822 
5823               IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5824                 RAISE fnd_api.g_exc_unexpected_error;
5825               END IF;
5826               IF (l_debug = 1) THEN
5827                  DEBUG('Query reservation get count '|| l_mtl_reservation_tbl_count, 'Finalize_Pick_Confirm');
5828               END IF;
5829 
5830               IF l_mtl_reservation_tbl_count > 0 THEN
5831                 l_mtl_reservation_rec.primary_reservation_quantity  := l_mtl_reservation_tbl(1).primary_reservation_quantity + ABS(l_lot_primary_quantity);
5832                 l_mtl_reservation_rec.reservation_quantity          := l_mtl_reservation_tbl(1).reservation_quantity + ABS(l_lot_transaction_quantity);
5833                 l_mtl_reservation_rec.secondary_reservation_quantity
5834                        := l_mtl_reservation_tbl(1).secondary_reservation_quantity + ABS(l_lot_secondary_quantity);
5835                 l_mtl_reservation_rec.requirement_date              := SYSDATE;
5836                 IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
5837                     l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
5838                     l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
5839                 END IF;
5840                 IF (l_debug = 1) THEN
5841                     DEBUG('Update reservation id '|| l_mtl_reservation_rec.reservation_id, 'Finalize_Pick_Confirm');
5842                 END IF;
5843                 inv_reservation_pub.update_reservation(
5844                   p_api_version_number         => 1.0
5845                 , p_init_msg_lst               => fnd_api.g_false
5846                 , x_return_status              => l_return_status
5847                 , x_msg_count                  => x_msg_count
5848                 , x_msg_data                   => x_msg_data
5849                 , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5850                 , p_to_rsv_rec                 => l_mtl_reservation_rec
5851                 , p_original_serial_number     => l_original_serial_number
5852                 , p_to_serial_number           => l_to_serial_number
5853                 , p_validation_flag            => fnd_api.g_true
5854                 , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5855                 );
5856                 IF (l_debug = 1) THEN
5857                    DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
5858                 END IF;
5859 
5860                 IF l_return_status = fnd_api.g_ret_sts_error THEN
5861                   RAISE fnd_api.g_exc_error;
5862                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5863                   RAISE fnd_api.g_exc_unexpected_error;
5864                 END IF;
5865               ELSE
5866                 -- check to see the lot is reservable...
5867                 l_mtl_reservation_rec.primary_reservation_quantity  := ABS(l_lot_primary_quantity);
5868                 l_mtl_reservation_rec.reservation_quantity          := ABS(l_lot_transaction_quantity);
5869                 l_mtl_reservation_rec.secondary_reservation_quantity := ABS(l_lot_secondary_quantity);
5870                 l_mtl_reservation_rec.requirement_date              := SYSDATE;
5871                 inv_reservation_pub.create_reservation(
5872                   p_api_version_number         => 1.0
5873                 , p_init_msg_lst               => fnd_api.g_false
5874                 , x_return_status              => l_return_status
5875                 , x_msg_count                  => x_msg_count
5876                 , x_msg_data                   => x_msg_data
5877                 , p_rsv_rec                    => l_mtl_reservation_rec
5878                 , p_serial_number              => l_to_serial_number
5879                 , x_serial_number              => l_to_serial_number
5880                 , p_partial_reservation_flag   => fnd_api.g_true
5881                 , p_force_reservation_flag     => fnd_api.g_false
5882                 , p_validation_flag            => fnd_api.g_true
5883                 , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5884                 , x_quantity_reserved          => l_quantity_reserved
5885                 , x_reservation_id             => l_reservation_id
5886                 );
5887                 IF (l_debug = 1) THEN
5888                    DEBUG('Create reservation ', 'Finalize_Pick_Confirm');
5889                    DEBUG('Quantity reserved: '|| l_quantity_reserved, 'Finalize_Pick_Confirm');
5890                    DEBUG('Reservation ID: '|| l_reservation_id, 'Finalize_Pick_Confirm');
5891                 END IF;
5892 
5893                 IF l_return_status = fnd_api.g_ret_sts_error THEN
5894                   IF (l_debug = 1) THEN
5895                      DEBUG('Error in creating reservation for lot', 'Finalize_Pick_Confirm');
5896                   END IF;
5897                   RAISE fnd_api.g_exc_error;
5898                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5899                   IF (l_debug = 1) THEN
5900                      DEBUG('Unexpected error in creating reservation for lot', 'Finalize_Pick_Confirm');
5901                   END IF;
5902                   RAISE fnd_api.g_exc_unexpected_error;
5903                 END IF;
5904 
5905                 --bug 1402436 - set the reservations staged flag
5906                 inv_staged_reservation_util.update_staged_flag(x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data, p_reservation_id => l_reservation_id, p_staged_flag => 'Y');
5907 
5908                 IF l_return_status = fnd_api.g_ret_sts_error THEN
5909                   IF (l_debug = 1) THEN
5910                      DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
5911                   END IF;
5912                   RAISE fnd_api.g_exc_error;
5913                 ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5914                   IF (l_debug = 1) THEN
5915                      DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
5916                   END IF;
5917                   RAISE fnd_api.g_exc_unexpected_error;
5918                 END IF;
5919               END IF; -- Create or Update
5920             END LOOP; -- Lot loop
5921 
5922             IF (l_debug = 1) THEN
5923                DEBUG('after end of lot loop...', 'Finalize_Pick_Confirm');
5924             END IF;
5925             CLOSE lot_csr;
5926           ELSE
5927             --No Lot control
5928             IF (l_debug = 1) THEN
5929                DEBUG('No lot control', 'Finalize_Pick_Confirm');
5930             END IF;
5931             l_mtl_reservation_rec.lot_number  := NULL;
5932             -- query to see whether a record with the key
5933             -- attributes already exists
5934             -- if there is, use update instead of create
5935             inv_reservation_pub.query_reservation(
5936               p_api_version_number         => 1.0
5937             , p_init_msg_lst               => fnd_api.g_false
5938             , x_return_status              => l_return_status
5939             , x_msg_count                  => x_msg_count
5940             , x_msg_data                   => x_msg_data
5941             , p_query_input                => l_mtl_reservation_rec
5942             , p_lock_records               => fnd_api.g_true
5943             , x_mtl_reservation_tbl        => l_mtl_reservation_tbl
5944             , x_mtl_reservation_tbl_count  => l_mtl_reservation_tbl_count
5945             , x_error_code                 => l_error_code
5946             );
5947 
5948             IF l_return_status = fnd_api.g_ret_sts_error THEN
5949               RAISE fnd_api.g_exc_error;
5950             END IF;
5951 
5952             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5953               RAISE fnd_api.g_exc_unexpected_error;
5954             END IF;
5955 
5956             IF l_mtl_reservation_tbl_count > 0 THEN
5957               l_mtl_reservation_rec.primary_reservation_quantity  := l_mtl_reservation_tbl(1).primary_reservation_quantity + l_primary_shipping_quantity;
5958               l_mtl_reservation_rec.reservation_quantity          := l_mtl_reservation_tbl(1).reservation_quantity + l_shipping_quantity;
5959               l_mtl_reservation_rec.secondary_reservation_quantity
5960                                 := l_mtl_reservation_tbl(1).secondary_reservation_quantity + l_sec_shipping_quantity;
5961               l_mtl_reservation_rec.requirement_date              := SYSDATE;
5962               IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
5963                  l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
5964                  l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
5965               END IF;
5966               inv_reservation_pub.update_reservation(
5967                 p_api_version_number         => 1.0
5968               , p_init_msg_lst               => fnd_api.g_false
5969               , x_return_status              => l_return_status
5970               , x_msg_count                  => x_msg_count
5971               , x_msg_data                   => x_msg_data
5972               , p_original_rsv_rec           => l_mtl_reservation_tbl(1)
5973               , p_to_rsv_rec                 => l_mtl_reservation_rec
5974               , p_original_serial_number     => l_original_serial_number
5975               , p_to_serial_number           => l_to_serial_number
5976               , p_validation_flag            => fnd_api.g_true
5977               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
5978               );
5979               IF (l_debug = 1) THEN
5980                  DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
5981               END IF;
5982 
5983               IF l_return_status = fnd_api.g_ret_sts_error THEN
5984                 RAISE fnd_api.g_exc_error;
5985               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
5986                 RAISE fnd_api.g_exc_unexpected_error;
5987               END IF;
5988             ELSE
5989               l_mtl_reservation_rec.primary_reservation_quantity  := l_primary_shipping_quantity;
5990               l_mtl_reservation_rec.reservation_quantity          := l_shipping_quantity;
5991               l_mtl_reservation_rec.secondary_reservation_quantity  := l_sec_shipping_quantity;
5992               l_mtl_reservation_rec.requirement_date              := SYSDATE;
5993               IF (l_debug = 1) THEN
5994                  DEBUG('create reservation demand source'|| l_mtl_reservation_rec.demand_source_type_id, 'Finalize_Pick_Confirm');
5995               END IF;
5996               inv_reservation_pub.create_reservation(
5997                 p_api_version_number         => 1.0
5998               , p_init_msg_lst               => fnd_api.g_false
5999               , x_return_status              => l_return_status
6000               , x_msg_count                  => x_msg_count
6001               , x_msg_data                   => x_msg_data
6002               , p_rsv_rec                    => l_mtl_reservation_rec
6003               , p_serial_number              => l_to_serial_number
6004               , x_serial_number              => l_to_serial_number
6005               , p_partial_reservation_flag   => fnd_api.g_true
6006               , p_force_reservation_flag     => fnd_api.g_false
6007               , p_validation_flag            => fnd_api.g_true
6008               , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
6009               , x_quantity_reserved          => l_quantity_reserved
6010               , x_reservation_id             => l_reservation_id
6011               );
6012               IF (l_debug = 1) THEN
6013                  DEBUG('Quantity reserved: '|| l_quantity_reserved, 'Finalize_Pick_Confirm');
6014                  DEBUG('Reservation ID: '|| l_reservation_id, 'Finalize_Pick_Confirm');
6015               END IF;
6016 
6017               IF l_return_status = fnd_api.g_ret_sts_error THEN
6018                 IF (l_debug = 1) THEN
6019                    DEBUG('Error in creating reservation for lot', 'Finalize_Pick_Confirm');
6020                 END IF;
6021                 RAISE fnd_api.g_exc_error;
6022               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6023                 IF (l_debug = 1) THEN
6024                    DEBUG('Unexpected error in creating reservation for lot', 'Finalize_Pick_Confirm');
6025                 END IF;
6026                 RAISE fnd_api.g_exc_unexpected_error;
6027               END IF;
6028 
6029               --bug 1402436 - set the reservations staged flag
6030               inv_staged_reservation_util.update_staged_flag
6031                     (x_return_status        => l_return_status
6032                     , x_msg_count           => x_msg_count
6033                     , x_msg_data            => x_msg_data
6034                     , p_reservation_id      => l_reservation_id
6035                     , p_staged_flag         => 'Y'
6036                     );
6037 
6038               IF l_return_status = fnd_api.g_ret_sts_error THEN
6039                 IF (l_debug = 1) THEN
6040                    DEBUG('Error in update_staged_flag', 'Finalize_Pick_Confirm');
6041                 END IF;
6042                 RAISE fnd_api.g_exc_error;
6043               ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6044                 IF (l_debug = 1) THEN
6045                    DEBUG('Unexpected error in update_staged_flag', 'Finalize_Pick_Confirm');
6046                 END IF;
6047                 RAISE fnd_api.g_exc_unexpected_error;
6048               END IF;
6049             END IF; -- Create or Update
6050           END IF; -- Lot control or not
6051         END IF; -- Staging sub reservable or not
6052       END IF; -- if reservation exists
6053 
6054       IF (l_debug = 1) THEN
6055          DEBUG('interact with GME if neccesary', 'Finalize_Pick_Confirm');
6056       END IF;
6057 
6058       IF l_trolin_rec.line_status = 5 THEN
6059         -- If this is the last allocation line for the last move order
6060         -- line for the sales order then delete all the unstaged
6061         -- reservations for this record. This will also delete
6062         -- reservations which are against the backordered lines.
6063 
6064         --clean_reservations(p_source_line_id => l_trolin_rec.txn_source_line_id, x_return_status => l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data);
6065 
6066         --IF (l_return_status = fnd_api.g_ret_sts_error) THEN
6067         --  IF (l_debug = 1) THEN
6068         --     DEBUG('return error from clean reservations', 'Finalize_Pick_Confirm');
6069         --  END IF;
6070         --  RAISE fnd_api.g_exc_error;
6071         --ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6072         --  IF (l_debug = 1) THEN
6073         --     DEBUG('return unexpected error from clean reservations', 'Finalize_Pick_Confirm');
6074         --  END IF;
6075         --  RAISE fnd_api.g_exc_unexpected_error;
6076         --END IF;
6077 
6078         -- Fix the detailed quantity on all reservations for this
6079         --  sales order line. Necessary for underpicking.
6080         IF (l_debug = 1) THEN
6081            DEBUG('Cleaning up reservations', 'Finalize_Pick_Confirm');
6082         END IF;
6083 
6084         OPEN reservations_csr;
6085         LOOP
6086           FETCH reservations_csr INTO l_reservation_id, l_rsv_primary_quantity, l_rsv_detailed_quantity;
6087           EXIT WHEN reservations_csr%NOTFOUND;
6088           IF (l_debug = 1) THEN
6089              DEBUG('Found first reservation: '|| l_reservation_id, 'Finalize_Pick_Confirm');
6090           END IF;
6091 
6092           --for each reservation, check to see what the actual
6093           --allocated quantity is
6094           SELECT NVL(SUM(ABS(primary_quantity)), 0)
6095               ,  NVL(SUM(ABS(secondary_transaction_quantity)), 0)
6096             INTO l_mmtt_rsv_quantity
6097               ,  l_sec_mmtt_rsv_quantity
6098             FROM mtl_material_transactions_temp
6099            WHERE reservation_id = l_reservation_id;
6100 
6101           -- L.G. Logic might need to be put here is reservations are not used but
6102           -- user manually allocated the line, which the mmtt still may have the reservation_id as null
6103           -- but the reservations still need to be updated.
6104 
6105           -- need to subtract from the mmtt rsv quantity the
6106           --  quantity of the reservations that have already been
6107           --  picked, since the rsv's detailed quantity does not
6108           --  include this quantity
6109           IF g_rsv_picked_quantity_tbl.EXISTS(l_reservation_id) THEN
6110             l_mmtt_rsv_quantity  := l_mmtt_rsv_quantity - g_rsv_picked_quantity_tbl(l_reservation_id).picked_quantity;
6111           END IF;
6112 
6113           -- Update reservation if necessary
6114           IF l_mmtt_rsv_quantity < l_rsv_detailed_quantity THEN
6115             --call update reservation
6116             l_mtl_reservation_rec2.reservation_id     := l_reservation_id;
6117             l_mtl_reservation_rec3.reservation_id     := l_reservation_id;
6118             l_mtl_reservation_rec3.detailed_quantity  := l_mmtt_rsv_quantity;
6119             l_mtl_reservation_rec3.secondary_detailed_quantity  := l_sec_mmtt_rsv_quantity;
6120             IF (l_debug = 1) THEN
6121                DEBUG('reservation id is '|| l_mtl_reservation_rec2.reservation_id, 'Finalize_Pick_Confirm');
6122                DEBUG('primary_reservation quantity is '|| l_mtl_reservation_rec3.primary_reservation_quantity, 'Finalize_Pick_Confirm');
6123                DEBUG('detailed quantity is '|| l_mtl_reservation_rec3.detailed_quantity, 'Finalize_Pick_Confirm');
6124             END IF;
6125             IF (  l_mtl_reservation_rec.secondary_uom_code IS NULL ) THEN
6126               l_mtl_reservation_rec.secondary_reservation_quantity := NULL;
6127               l_mtl_reservation_rec.secondary_detailed_quantity    := NULL;
6128             END IF;
6129             inv_reservation_pub.update_reservation(
6130               p_api_version_number         => 1.0
6131             , p_init_msg_lst               => fnd_api.g_false
6132             , x_return_status              => l_return_status
6133             , x_msg_count                  => x_msg_count
6134             , x_msg_data                   => x_msg_data
6135             , p_original_rsv_rec           => l_mtl_reservation_rec2
6136             , p_to_rsv_rec                 => l_mtl_reservation_rec3
6137             , p_original_serial_number     => l_original_serial_number
6138             , p_to_serial_number           => l_to_serial_number
6139             , p_validation_flag            => fnd_api.g_true
6140             , p_over_reservation_flag      => 3  -- Bug 4997704, Passing p_over_reservation_flag to reservation API to handle overpicking scenarios
6141             );
6142             IF (l_debug = 1) THEN
6143                DEBUG('after update reservation return status is '|| l_return_status, 'Finalize_Pick_Confirm');
6144             END IF;
6145 
6146             IF l_return_status = fnd_api.g_ret_sts_error THEN
6147               RAISE fnd_api.g_exc_error;
6148             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6149               RAISE fnd_api.g_exc_unexpected_error;
6150             END IF;
6151           END IF; -- if allocated quantity < detailed rsv quantity
6152         END LOOP;
6153         IF reservations_csr%ISOPEN THEN
6154           CLOSE reservations_csr;
6155         END IF;
6156       END IF; -- line status = 5
6157     End if;   -- if move order type = 5 and wip entity type is GME
6158 
6159     IF (l_debug = 1) THEN
6160        DEBUG('before return', 'Finalize_Pick_Confirm');
6161     END IF;
6162     x_return_status                     := fnd_api.g_ret_sts_success;
6163   EXCEPTION
6164     WHEN fnd_api.g_exc_error THEN
6165       x_return_status  := fnd_api.g_ret_sts_error;
6166       --  Get message count and data
6167       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6168     WHEN fnd_api.g_exc_unexpected_error THEN
6169       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6170       --  Get message count and data
6171       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6172     WHEN OTHERS THEN
6173       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6174 
6175       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6176         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Pick Confirm');
6177       END IF;
6178 
6179       --  Get message count and data
6180       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6181   END finalize_pick_confirm;
6182 
6183   -- Bug 2640757: Fill Kill Enhancement
6184   PROCEDURE kill_move_order(
6185       x_return_status         OUT NOCOPY VARCHAR2
6186     , x_msg_count             OUT NOCOPY NUMBER
6187     , x_msg_data              OUT NOCOPY VARCHAR2
6188     , p_transaction_header_id     NUMBER) IS
6189 
6190      -- Bug 5059984, Adding cursor c_rep_mo_mmtt.
6191 
6192      CURSOR c_rep_mo_mmtt IS
6193  	       SELECT DISTINCT move_order_line_id
6194  	         FROM mtl_material_transactions_temp a
6195  	         WHERE transaction_header_id = p_transaction_header_id
6196  	         AND transaction_source_type_id = 4
6197  	         AND transaction_action_id = 2
6198  	         AND move_order_line_id IS NOT NULL
6199  	         AND nvl(transaction_status, 1) <> 2
6200  	         AND process_flag = 'Y'
6201  	         AND NOT EXISTS ( SELECT 1
6202  	                          FROM mtl_material_transactions_temp b
6203  	                          WHERE b.move_order_line_id = a.move_order_line_id
6204  	                          AND (nvl(transaction_status,1) = 2 OR error_code IS NOT NULL));
6205 
6206      CURSOR c_mo_lines IS
6207         SELECT DISTINCT move_order_line_id
6208           FROM mtl_material_transactions_temp a
6209          WHERE transaction_header_id = p_transaction_header_id
6210            AND move_order_line_id IS NOT NULL
6211            AND nvl(transaction_status,1) <> 2
6212            AND process_flag = 'Y'
6213            AND NOT EXISTS ( SELECT 1
6214                               FROM mtl_material_transactions_temp b
6215                              WHERE b.move_order_line_id = a.move_order_line_id
6216                                AND (nvl(transaction_status,1) = 2 OR error_code IS NOT NULL));
6217 
6218      CURSOR c_mo_type (p_mo_line_id NUMBER) IS
6219         SELECT mtrh.move_order_type
6220           FROM mtl_txn_request_headers mtrh
6221              , mtl_txn_request_lines mtrl
6222          WHERE mtrl.line_id = p_mo_line_id
6223            AND mtrh.header_id = mtrl.header_id;
6224 
6225      l_mo_type NUMBER;
6226      l_return_status VARCHAR2(1) ;
6227      l_msg_data VARCHAR2(240);
6228      l_msg_count NUMBER;
6229      l_debug number;
6230      l_kill_mo_profile NUMBER := NVL(FND_PROFILE.VALUE_WNPS('INV_KILL_MOVE_ORDER'),2);
6231      l_trolin_rec inv_move_order_pub.trolin_rec_type; -- Bug 5059984
6232   BEGIN
6233      x_return_status  := FND_API.G_RET_STS_SUCCESS;
6234 
6235      -- Use cache to get value for l_debug
6236      IF g_is_pickrelease_set IS NULL THEN
6237         g_is_pickrelease_set := 2;
6238         IF INV_CACHE.is_pickrelease THEN
6239            g_is_pickrelease_set := 1;
6240         END IF;
6241      END IF;
6242      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
6243         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
6244      END IF;
6245 
6246      l_debug := g_debug;
6247 
6248      IF (l_debug = 1) THEN
6249         DEBUG('Fill Kill Profile  = ' || l_kill_mo_profile,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6250         DEBUG('Transaction Hdr ID = ' || p_transaction_header_id,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6251      END IF;
6252 
6253      -- Check the Profile Fill Kill before doing anything.
6254      IF l_kill_mo_profile = 2 THEN
6255         -- Bug 5059984,  Added following code to handle replenishment move orders
6256         --   Replenishment move order lines with two or more allocations are not closed by
6257  	      --   finalize_pick_confirm. If Fill Kill profile is No, then we need to close them here.
6258 
6259         -- Fetch move order transfer MMTT records
6260  	      FOR l_rep_mmtt_rec IN  c_rep_mo_mmtt LOOP
6261  	        IF (l_debug = 1) THEN
6262  	          DEBUG('Fill Kill profile is No. MO line ID: ' || l_rep_mmtt_rec.move_order_line_id, 'Finalize_Pick_Confirm');
6263           END IF;
6264  	        OPEN c_mo_type(l_rep_mmtt_rec.move_order_line_id);
6265  	        FETCH c_mo_type INTO l_mo_type;
6266  	        IF c_mo_type%NOTFOUND THEN
6267  	          IF (l_debug = 1) THEN
6268  	            DEBUG('MO Header not found for the MO Line = ' || l_rep_mmtt_rec.move_order_line_id,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6269  	          END IF;
6270  	          CLOSE c_mo_type;
6271  	          RAISE FND_API.G_EXC_ERROR;
6272  	        END IF;
6273  	        CLOSE c_mo_type;
6274 
6275           IF (l_debug = 1) THEN
6276  	          DEBUG('Move Order Type = ' || l_mo_type,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6277  	        END IF;
6278  	        --If it is a replenishment move order, then check quantities and close it
6279  	        IF (l_mo_type = 2) THEN
6280  	          l_trolin_rec := inv_trolin_util.query_row(l_rep_mmtt_rec.move_order_line_id);
6281  	          --If the line is not closed, check quantity_delivered against quantity
6282  	          --If it is greater, then close the move order line
6283 
6284             IF l_trolin_rec.line_status NOT IN (5, 6) THEN
6285  	            IF (NVL(l_trolin_rec.quantity_delivered, 0) >= l_trolin_rec.quantity) THEN
6286  	              l_trolin_rec.line_status := 5;
6287  	              IF (l_debug = 1) THEN
6288  	                DEBUG('Closing the replenishment move order','INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6289  	              END IF;
6290  	              inv_trolin_util.update_row(l_trolin_rec);
6291  	            END IF;   --END IF quantity_delivered > quantity
6292  	          END IF;   --END IF line_status not closed, cancelled
6293  	        END IF;   --END IF move_order_type = 2
6294  	      END LOOP;
6295  	   --Fill Kill profile is "Yes"
6296      ELSE
6297 
6298        -- Loop thru all the Move Order Line IDs from MMTT for the passed Transaction Header ID
6299        FOR l_mo_line IN c_mo_lines
6300        LOOP
6301          IF (l_debug = 1) THEN
6302            DEBUG('For Kill Move Order = yes, Move Order Line = ' || l_mo_line.move_order_line_id,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6303          END IF;
6304 
6305          -- Getting the Move Order Type.
6306          OPEN c_mo_type(l_mo_line.move_order_line_id);
6307          FETCH c_mo_type INTO l_mo_type;
6308          IF c_mo_type%NOTFOUND THEN
6309            IF (l_debug = 1) THEN
6310              DEBUG('MO Header not found for the MO Line = ' || l_mo_line.move_order_line_id,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6311            END IF;
6312            CLOSE c_mo_type;
6313            RAISE FND_API.G_EXC_ERROR;
6314          END IF;
6315          CLOSE c_mo_type;
6316 
6317          IF (l_debug = 1) THEN
6318            DEBUG('Move Order Type = ' || l_mo_type,'INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6319          END IF;
6320 
6321          -- Proceed only if Move Order Type is Replenishment
6322          IF l_mo_type = 2 THEN
6323            IF (l_debug = 1) THEN
6324              DEBUG('Replenishment Move Order... Closing the Move Order','INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6325            END IF;
6326            INV_MO_ADMIN_PUB.close_line(1.0,'F','F','F',l_mo_line.move_order_line_id,l_msg_count,l_msg_data,l_return_status);
6327 
6328            IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6329              RAISE FND_API.G_EXC_ERROR;
6330            ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6331              RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6332            END IF;
6333          END IF; -- end if check mo_type = 2
6334        END LOOP;
6335      END IF; -- end if check fill kill profile
6336 
6337   EXCEPTION
6338      WHEN FND_API.G_EXC_ERROR THEN
6339         IF (l_debug = 1) THEN
6340            DEBUG('Expected error while closing the Line','INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6341         END IF;
6342         x_return_status  := FND_API.G_RET_STS_ERROR;
6343         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6344      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6345         IF (l_debug = 1) THEN
6346            DEBUG('Unexpected error while closing the Line','INV_TRANSFER_ORDER_PVT.KILL_MOVE_ORDER');
6347         END IF;
6348         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
6349         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6350      WHEN OTHERS THEN
6351         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
6352 
6353         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6354            fnd_msg_pub.add_exc_msg(g_pkg_name, 'KILL_MOVE_ORDER');
6355         END IF;
6356         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6357   END kill_move_order;
6358 
6359 
6360 --As part of fix for bug 2867490, we need to create the following procedure
6361  --This procedure does the needful to adjust the serial numbers in MSNT
6362  --in case of overpicking. It could either update/delete MSNT range
6363  --or delete the single serial number from MSNT
6364  --INPUT parameters are
6365  --p_transaction_temp_id (if lot+serial then serial_txn_temp_id)
6366  --p_qty is the picked quantity corresponding to txn_temp_id
6367 
6368 
6369 
6370   PROCEDURE adjust_serial_numbers_in_MSNT(p_transaction_temp_id IN NUMBER,
6371                                               p_qty IN NUMBER) IS
6372 
6373     CURSOR serial_cursor IS
6374            SELECT a.rowid
6375                  ,a.FM_SERIAL_NUMBER
6376                  ,a.TO_SERIAL_NUMBER
6377                  ,b.organization_id
6378                  ,b.inventory_item_id
6379            FROM   mtl_Serial_numbers_temp a, mtl_material_transactions_temp b
6380            where  a.transaction_temp_id= p_transaction_temp_id
6381            and   a.transaction_temp_id = b.transaction_temp_id;
6382 
6383     l_Serial_cursor serial_cursor%ROWTYPE;
6384     qty_count NUMBER := 0;
6385     delete_remaining_serial BOOLEAN := FALSE;
6386     x_PREFIX              VARCHAR2(100);
6387     x_QUANTITY            VARCHAR2(100);
6388     l_FROM_NUMBER         VARCHAR2(100);
6389     l_TO_NUMBER           VARCHAR2(100);
6390     l_new_num             NUMBER;
6391     l_new_num_update      NUMBER;
6392     x_errorcode           NUMBER;
6393     l_new_NUM_str         VARCHAR2(100);
6394     l_new_NUM_str_update  VARCHAR2(100);
6395     l_counter             NUMBER := 0;
6396     diff_qty              NUMBER := 0;
6397     l_debug               NUMBER;
6398     x_return_status       VARCHAR2(10);
6399     x_msg_count           NUMBER;
6400     been_here             BOOLEAN := FALSE;
6401     x_msg_data            VARCHAR2(200);
6402 
6403   BEGIN
6404     IF (l_debug = 1) THEN
6405       DEBUG('adjust_serial_numbers_in_MMTT p_transaction_temp_id='||p_transaction_temp_id||' p_qty= '||p_qty,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6406     END IF;
6407 
6408     FOR l_serial_cursor IN serial_cursor LOOP
6409     EXIT WHEN serial_cursor%NOTFOUND;
6410 
6411 
6412     --determine the number of serial numbers associated with this temp id
6413 
6414       diff_qty := INV_SERIAL_NUMBER_PUB.GET_SERIAL_DIFF(l_serial_cursor.fm_Serial_number,l_serial_cursor.to_serial_number);
6415       qty_count := qty_count + diff_qty;
6416       l_counter := l_counter+1;
6417 
6418       IF (l_debug = 1) THEN
6419         DEBUG('p_qty= '||p_qty||' diff_qty= '||diff_qty||' qty_count= '||qty_count||' l_counter='||l_counter,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6420       END IF;
6421 
6422       if (qty_count>p_qty AND NOT delete_remaining_Serial) then
6423     	--get rid of xtra serial numbers from msnt
6424 	--(qty_count-p_qty) serials
6425 	--if the diff_qty>(qty_count-p_qty)
6426 	--if range then reduce this range
6427 	--following logic will derive the to_Serial_number
6428         --If range
6429 
6430         IF (l_debug = 1) THEN
6431           DEBUG('p_qty= '||p_qty||' diff_qty= '||diff_qty||' qty_count= '||qty_count,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6432         END IF;
6433 
6434         IF (l_Serial_cursor.fm_serial_number <> l_Serial_cursor.to_serial_number ) THEN
6435 
6436         --if range then we have to either update the range or delete the whole range
6437           if (diff_qty = qty_count-p_qty) then
6438               --delete the whole range
6439         IF (l_debug = 1) THEN
6440           DEBUG('Inside if p_qty= '||p_qty||' diff_qty= '||diff_qty||' qty_count= '||qty_count||' fm_Serial='||l_Serial_cursor.fm_serial_number||' to_ser='||l_serial_cursor.to_serial_number,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6441         END IF;
6442 
6443               delete from mtl_Serial_numbers_temp
6444               where transaction_temp_id = p_transaction_temp_id
6445               and fm_Serial_number = l_Serial_cursor.fm_serial_number
6446               and to_Serial_number = l_Serial_cursor.to_serial_number;
6447 
6448              /*** {{ R12 Enhanced reservations code changes,
6449               *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
6450               update mtl_Serial_numbers
6451               set group_mark_id = null
6452               where current_organization_id = l_serial_cursor.organization_id
6453               and inventory_item_id = l_Serial_cursor.inventory_item_id
6454               and serial_number between l_Serial_cursor.fm_Serial_number and l_serial_cursor.to_Serial_number;
6455               *** End R12 }} ***/
6456 
6457              /*** {{ R12 Enhanced reservations code changes ***/
6458               serial_check.inv_unmark_rsv_serial
6459                  (from_serial_number   => l_Serial_cursor.fm_Serial_number
6460                  ,to_serial_number     => l_serial_cursor.to_Serial_number
6461                  ,serial_code          => null
6462                  ,hdr_id               => null
6463                  ,p_inventory_item_id  => l_Serial_cursor.inventory_item_id
6464                  ,p_update_reservation => fnd_api.g_true);
6465              /*** End R12 }} ***/
6466 
6467               delete_remaining_serial := TRUE;
6468             been_here := TRUE;
6469           else
6470 
6471               --update the range
6472 
6473             IF (l_debug = 1) THEN
6474                DEBUG('Inside ifelse p_qty= '||p_qty||' diff_qty= '||diff_qty||' qty_count= '||qty_count,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6475                DEBUG(' fm_Ser_num= '||l_Serial_cursor.fm_serial_number ||' to_Ser_num= '||l_Serial_cursor.to_serial_number ,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6476             END IF;
6477 
6478         	IF NOT mtl_serial_check.INV_SERIAL_INFO
6479 	            (p_from_serial_number  =>  l_Serial_cursor.fm_serial_number ,
6480                  p_to_serial_number    =>  l_Serial_cursor.to_serial_number ,
6481     	         x_prefix              =>  x_prefix,
6482                  x_quantity            =>  x_quantity,
6483                  x_from_number         =>  l_from_number,
6484                  x_to_number           =>  l_to_number,
6485                  x_errorcode           =>  x_errorcode) THEN
6486 	       RAISE FND_API.G_EXC_ERROR;
6487            END IF;
6488 
6489            l_new_num:=l_from_number+(diff_qty-((qty_count-p_qty)+1));
6490            l_new_num_update:=l_from_number+(diff_qty-((qty_count-p_qty)+1))+1;
6491            IF (l_debug = 1) THEN
6492              DEBUG('l_new_num= '||l_new_num||' l_new_num_update='||l_new_num_update,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6493            END IF;
6494 
6495            if (length(l_new_num)<length(l_from_number)) then
6496     	       l_new_num_str:=lpad(l_new_num,length(l_from_number),'0');
6497     	       l_new_num_str_update:=lpad(l_new_num_update,length(l_from_number),'0');
6498            end if;
6499            IF (l_debug = 1) THEN
6500              DEBUG('l_new_num_Str= '||l_new_num_str||' l_new_num_Str_update='||l_new_num_str_update,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6501            END IF;
6502 
6503 
6504            update mtl_serial_numbers_temp
6505            set fm_Serial_number = l_Serial_cursor.fm_serial_number
6506            ,to_Serial_number = x_prefix||l_new_num_str
6507            where fm_Serial_number = l_Serial_cursor.fm_serial_number
6508            and to_Serial_number = l_Serial_cursor.to_serial_number
6509            and transaction_temp_id = p_transaction_temp_id;
6510 
6511            IF (l_debug = 1) THEN
6512              DEBUG('after update l_new_num_Str= '||l_new_num_str,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6513            END IF;
6514     	   --set a flag to delete the subsequent records from MSNT
6515 
6516            --unmark these serial numbers in MSN
6517           /*** {{ R12 Enhanced reservations code changes,
6518            *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
6519            update mtl_serial_numbers
6520            set group_mark_id = null
6521            where current_organization_id = l_serial_cursor.organization_id
6522            and inventory_item_id = l_serial_cursor.inventory_item_id
6523            and serial_number between x_prefix||l_new_num_str_update and l_Serial_cursor.to_serial_number;
6524            *** End R12 }} ***/
6525 
6526           /*** {{ R12 Enhanced reservations code changes ***/
6527            serial_check.inv_unmark_rsv_serial
6528               (from_serial_number   => x_prefix||l_new_num_str_update
6529               ,to_serial_number     => l_serial_cursor.to_serial_number
6530               ,serial_code          => null
6531               ,hdr_id               => null
6532               ,p_inventory_item_id  => l_serial_cursor.inventory_item_id
6533               ,p_update_reservation => fnd_api.g_true);
6534           /*** End R12 }} ***/
6535 
6536            delete_remaining_serial := TRUE;
6537             been_here := TRUE;
6538           end if;
6539         else   --END of range
6540             IF (l_debug = 1) THEN
6541                DEBUG('after update fm_Serial_num= '||l_Serial_cursor.fm_serial_number||' to_Ser_num= '||l_serial_cursor.to_serial_number,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6542             END IF;
6543 
6544             delete from mtl_serial_numbers_temp
6545             where fm_Serial_number = l_Serial_cursor.fm_serial_number
6546             and to_serial_number = l_serial_cursor.to_serial_number
6547             and transaction_temp_id = p_transaction_Temp_id;
6548 
6549            /*** {{ R12 Enhanced reservations code changes,
6550             *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
6551             update mtl_serial_numbers
6552             set group_mark_id = null
6553             where current_organization_id = l_serial_cursor.organization_id
6554             and inventory_item_id = l_serial_cursor.inventory_item_id
6555             and serial_number = l_Serial_cursor.fm_serial_number;
6556             *** End R12 }} ***/
6557 
6558            /*** {{ R12 Enhanced reservations code changes ***/
6559             serial_check.inv_unmark_rsv_serial
6560                (from_serial_number   => l_Serial_cursor.fm_serial_number
6561                ,to_serial_number     => null
6562                ,serial_code          => null
6563                ,hdr_id               => null
6564                ,p_inventory_item_id  => l_serial_cursor.inventory_item_id
6565                ,p_update_reservation => fnd_api.g_true);
6566            /*** End R12 }} ***/
6567 
6568             delete_remaining_serial := TRUE;
6569             been_here := TRUE;
6570 
6571         end if;
6572       end if;
6573 
6574       if (delete_remaining_serial and l_counter > 1 and NOT been_here) then
6575             IF (l_debug = 1) THEN
6576               DEBUG('After update fm_Serial_num= '||l_Serial_cursor.fm_serial_number||' to_Ser_num= '||l_serial_cursor.to_serial_number,'INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6577             END IF;
6578 
6579             delete from mtl_serial_numbers_temp
6580             where fm_Serial_number = l_Serial_cursor.fm_serial_number
6581             and to_serial_number = l_serial_cursor.to_serial_number
6582             and transaction_temp_id = p_transaction_Temp_id;
6583 
6584 
6585            /*** {{ R12 Enhanced reservations code changes,
6586             *** call serial_check.inv_unmark_rsv_serial instead of update msn directly
6587             update mtl_serial_numbers
6588             set group_mark_id = null
6589             where current_organization_id = l_serial_cursor.organization_id
6590             and inventory_item_id = l_serial_cursor.inventory_item_id
6591             and serial_number = l_Serial_cursor.to_serial_number;
6592             *** End R12 }} ***/
6593 
6594            /*** {{ R12 Enhanced reservations code changes ***/
6595             serial_check.inv_unmark_rsv_serial
6596                (from_serial_number   => l_Serial_cursor.to_serial_number
6597                ,to_serial_number     => null
6598                ,serial_code          => null
6599                ,hdr_id               => null
6600                ,p_inventory_item_id  => l_serial_cursor.inventory_item_id
6601                ,p_update_reservation => fnd_api.g_true);
6602            /*** End R12 }} ***/
6603 
6604       end if;
6605 
6606             been_here :=FALSE;
6607     END LOOP;
6608 
6609   EXCEPTION
6610      WHEN FND_API.G_EXC_ERROR THEN
6611         IF (l_debug = 1) THEN
6612            DEBUG('Expected error while closing the Line','INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6613         END IF;
6614         x_return_status  := FND_API.G_RET_STS_ERROR;
6615         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6616      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6617         IF (l_debug = 1) THEN
6618            DEBUG('Unexpected error while closing the Line','INV_TRANSFER_ORDER_PVT.adjust_serial_numbers_in_MMTT');
6619         END IF;
6620         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
6621         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6622      WHEN OTHERS THEN
6623         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
6624 
6625         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6626            fnd_msg_pub.add_exc_msg(g_pkg_name, 'adjust_serial_numbers_in_MMTT');
6627         END IF;
6628         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6629   END adjust_serial_numbers_in_MSNT;
6630 
6631 END inv_transfer_order_pvt;