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